Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v5 #53

Merged
merged 27 commits into from
Aug 17, 2024
Merged

v5 #53

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bc8a67b
update to node-fetch@3, do bare minimum to get it building. most test…
mistval May 18, 2024
4f304b9
get all ESM tests passing (CJS doesnt work)
mistval May 19, 2024
59e2140
remove timeout which was removed in node-fetch v3
mistval May 19, 2024
3bf0c13
fix type
mistval May 19, 2024
ad69f27
dynamic import node-fetch
mistval Aug 17, 2024
4be7cf9
cleanup
mistval Aug 17, 2024
aa75309
cleanup
mistval Aug 17, 2024
49e4229
add an upgrade guide doc
mistval Aug 17, 2024
e393895
increase oldest supported node version to v16
mistval Aug 17, 2024
f629007
bump ts target
mistval Aug 17, 2024
8863ece
switch ts-node for tsx, update redis plugin
mistval Aug 17, 2024
beef68b
bump version to v5
mistval Aug 17, 2024
889ed65
update upgrade guide
mistval Aug 17, 2024
7860343
replace form-data with formdata-node
mistval Aug 17, 2024
a6f2419
stop testing on node v14
mistval Aug 17, 2024
8dc8a53
run tests on node 18 and up only
mistval Aug 17, 2024
45ff15a
npm link node-fetch-cache into the redis plugin for tests
mistval Aug 17, 2024
8e94379
update upgrade guide
mistval Aug 17, 2024
2d23f69
update upgrade guide
mistval Aug 17, 2024
3c2a614
try to fix flaky test
mistval Aug 17, 2024
a2ddb12
try to fix flaky test
mistval Aug 17, 2024
1349889
try to fix flaky test
mistval Aug 17, 2024
a00b1ff
try to fix flaky test
mistval Aug 17, 2024
3125eb3
lowest supported version to 18.19.0
mistval Aug 17, 2024
385b756
increase TS target
mistval Aug 17, 2024
dd50ad6
default null body to empty stream
mistval Aug 17, 2024
4fc7f6a
empty commit
mistval Aug 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 9 additions & 31 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,21 @@ jobs:
- run: npm ci
- run: npm run tsc
- run: npm run buildcjs
- run: npm link
- run: npm link node-fetch-cache
working-directory: plugins/redis
- run: npm run coverage
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

test_node_14_14_0:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.14.0]

services:
httpbin:
image: kennethreitz/httpbin
ports:
- 3000:80

steps:
- uses: actions/checkout@v2
- name: Use Node.js 20.x
uses: actions/setup-node@v1
with:
node-version: 20.x
- run: npm ci
- run: npm run tsc
- run: npm run buildcjs
- name: Use Node.js Version Under Test
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm run test:core

test_node_other:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 22.x]
node-version: [18.19.0, 18.x, 22.x]

services:
httpbin:
Expand All @@ -79,14 +54,17 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Use Node.js 20.x
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: 20.x
node-version: ${{ matrix.node-version }}
- run: npm ci --prefix plugins/redis
- run: npm ci
- run: npm run tsc
- run: npm run buildcjs
- run: npm link
- run: npm link node-fetch-cache
working-directory: plugins/redis
- name: Use Node.js Version Under Test
uses: actions/setup-node@v1
with:
Expand Down
5 changes: 4 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
{
"type": "node",
"request": "launch",
"name": "Debug Tests",
"name": "Debug Tests Local Server",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "test:debug"],
"console": "integratedTerminal",
"env": {
"HTTP_BIN_BASE_URL": "http://localhost:3000"
},
"runtimeVersion": "20"
}
]
Expand Down
98 changes: 7 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

The first fetch with any given arguments will result in an HTTP request and any subsequent fetch with the same arguments will read the response from the cache.

By default responses are cached in memory, but you can also cache to files on disk, or implement your own cache.
By default responses are cached in memory, but you can also cache to files on disk, cache in Redis, or implement your own cache.

## Usage

Expand All @@ -27,7 +27,7 @@ This module's API is a superset of `node-fetch`'s. You can consult [the node-fet

### Control what's cached

By default node-fetch-cache caches all responses, regardless of the status code or any other response characteristics.
By default node-fetch-cache caches all responses, regardless of the response status or any other response characteristics.

There are two main ways to customize which responses are cached and which are not.

Expand Down Expand Up @@ -249,94 +249,6 @@ const fetch = NodeFetchCache.create({
});
```

## Upgrading node-fetch-cache v3 -> v4

The v4 version of node-fetch-cache has several breaking changes and new features. Please review the below details if you are upgrading from v3.

### Node.js v14.14.0 is now the lowest supported Node.js version

v4 will not work at all on Node.js versions below v14.14.0.

### Specifying a Cache

The syntax to specify a non-default cache has changed. You should rewrite code like this:

```js
import { fetchBuilder, FileSystemCache } from 'node-fetch-cache';
const fetch = fetchBuilder.withCache(new FileSystemCache(options));
```

To this:

```js
import NodeFetchCache, { FileSystemCache } from 'node-fetch-cache';

const fetch = NodeFetchCache.create({
cache: new FileSystemCache(options),
});
```

### Cache-Control: only-if-cached

If you are relying on the `Cache-Control: only-if-cached` header feature, that has been changed to better align with the browser fetch API. It no longer returns `undefined`, but instead returns a `504 Gateway Timeout` response if no cached response is available. The response will also have an `isCacheMiss` property set to true to help you distinguish it from a regular 504 response. You should rewrite code like this:

```js
import fetch from 'node-fetch-cache';

const response = await fetch('https://google.com', {
headers: { 'Cache-Control': 'only-if-cached' }
});

if (response === undefined) {
console.log('No response was found in the cache!');
}
```

To this:

```js
import fetch from 'node-fetch-cache';

const response = await fetch('https://google.com', {
headers: { 'Cache-Control': 'only-if-cached' }
});

if (response.isCacheMiss) {
console.log('No response was found in the cache!');
}
```

### TypeScript

If you were using the `@types/node-fetch-cache` package, that is no longer necessary as v4 includes its own TypeScript definitions, which may be somewhat different.

### ejectFromCache()

While the `ejectFromCache()` function still exists and functions the same way as in v3, you may find the new `shouldCacheResponse` option to be cleaner for many use cases, and it also allows you to keep the response from being cached in the first place which will reduce writes to the cache. So consider rewriting code like this:

```js
fetch('http://google.com')
.then(async response => {
if (!response.ok) {
await response.ejectFromCache();
} else {
return response.text();
}
}).then(text => console.log(text));
```

To this:

```js
fetch(
'http://google.com',
undefined,
{ shouldCacheResponse: response => response.ok },
).then(response => {
return response.text();
}).then(text => console.log(text));
```

## Misc

### Streams
Expand All @@ -353,14 +265,18 @@ node-fetch-cache supports both ESM and CommonJS. If you are using CommonJS, you
const fetch = require('node-fetch-cache');
```

### Upgrading

Upgrading from an older major version? Check the [upgrade guide](https://github.com/mistval/node-fetch-cache/tree/master/docs/upgrade_guide.md).

### Node.js Support Policy

node-fetch-cache will support:
* The current Node.js version
* All non-EOL LTS Node.js versions
* In addition, as far back as is technically easy

Currently the oldest supported Node.js version is v14.14.0, which adds `fs.rmSync()` which is used by a dependency.
Currently the oldest supported Node.js version is v18.19.0.

Automated tests will be run on the current Node.js version, the oldest supported Node.js version, and the latest release of all even-numbered Node.js versions between those two.

Expand Down
103 changes: 103 additions & 0 deletions docs/upgrade_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Upgrade Guide

## Upgrading node-fetch-cache v4 -> v5

The v5 version of `node-fetch-cache` upgrades `node-fetch` from v2 to v3.

Please consult the [node-fetch v2 -> v3 upgrade guide](https://github.com/node-fetch/node-fetch/blob/main/docs/v3-UPGRADE-GUIDE.md) and follow the instructions there, except regarding the following:

1. The minimum supported node version for `node-fetch-cache` is v18.19.0
2. Unlike `node-fetch` v3, `node-fetch-cache` v5 still supports CommonJS.

In addition, `node-fetch-cache` specifics have changed in the following breaking ways, which will affect a minority of use cases:

1. If you are providing a custom `calculateCacheKey` function, it must now be async (returns a promise).
2. `node-fetch-cache` now uses [formdata-node](https://www.npmjs.com/package/formdata-node) instead of [form-data](https://www.npmjs.com/package/form-data) (which has been deprecated in `node-fetch` v3). This may affect you if you are sending request bodies of type FormData.

## Upgrading node-fetch-cache v3 -> v4

The v4 version of `node-fetch-cache` has several breaking changes and new features.

### Node.js v14.14.0 is now the lowest supported Node.js version

v4 will not work at all on Node.js versions below v14.14.0.

### Specifying a Cache

The syntax to specify a non-default cache has changed. You should rewrite code like this:

```js
import { fetchBuilder, FileSystemCache } from 'node-fetch-cache';
const fetch = fetchBuilder.withCache(new FileSystemCache(options));
```

To this:

```js
import NodeFetchCache, { FileSystemCache } from 'node-fetch-cache';

const fetch = NodeFetchCache.create({
cache: new FileSystemCache(options),
});
```

### Cache-Control: only-if-cached

If you are relying on the `Cache-Control: only-if-cached` header feature, that has been changed to better align with the browser fetch API. It no longer returns `undefined`, but instead returns a `504 Gateway Timeout` response if no cached response is available. The response will also have an `isCacheMiss` property set to true to help you distinguish it from a regular 504 response. You should rewrite code like this:

```js
import fetch from 'node-fetch-cache';

const response = await fetch('https://google.com', {
headers: { 'Cache-Control': 'only-if-cached' }
});

if (response === undefined) {
console.log('No response was found in the cache!');
}
```

To this:

```js
import fetch from 'node-fetch-cache';

const response = await fetch('https://google.com', {
headers: { 'Cache-Control': 'only-if-cached' }
});

if (response.isCacheMiss) {
console.log('No response was found in the cache!');
}
```

### TypeScript

If you were using the `@types/node-fetch-cache` package, that is no longer necessary as v4 includes its own TypeScript definitions, which may be somewhat different.

### ejectFromCache()

While the `ejectFromCache()` function still exists and functions the same way as in v3, you may find the new `shouldCacheResponse` option to be cleaner for many use cases, and it also allows you to keep the response from being cached in the first place which will reduce writes to the cache. So consider rewriting code like this:

```js
fetch('http://google.com')
.then(async response => {
if (!response.ok) {
await response.ejectFromCache();
} else {
return response.text();
}
}).then(text => console.log(text));
```

To this:

```js
fetch(
'http://google.com',
undefined,
{ shouldCacheResponse: response => response.ok },
).then(response => {
return response.text();
}).then(text => console.log(text));
```
Loading
Loading