Skip to content

Commit

Permalink
replace form-data with formdata-node
Browse files Browse the repository at this point in the history
  • Loading branch information
mistval committed Aug 17, 2024
1 parent 889ed65 commit 7860343
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 125 deletions.
7 changes: 4 additions & 3 deletions docs/upgrade_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

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

The v5 version of node-fetch-cache upgrades `node-fetch` from v2 to v3.
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 v16.0.0
2. Unlike `node-fetch` v3, `node-fetch-cache` v5 still supports CommonJS.

In addition node-fetch-cache specifics have changed in the following ways:
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.
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

Expand Down
105 changes: 11 additions & 94 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"dependencies": {
"cacache": "^18.0.1",
"form-data": "^4.0.0",
"formdata-node": "^6.0.3",
"locko": "^1.1.0",
"node-fetch": "3.3.2"
},
Expand Down
2 changes: 1 addition & 1 deletion plugins/redis/test_redis_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fileURLToPath } from 'url';
import fs from 'fs';
import assert from 'assert';
import { Agent } from 'http';
import FormData from 'form-data';
import { FormData} from 'formdata-node';
import standardFetch, { Request as StandardFetchRequest } from 'node-fetch';
import FetchCache, {
cacheStrategies,
Expand Down
24 changes: 7 additions & 17 deletions src/helpers/cache_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import crypto from 'crypto';
import assert from 'assert';
import { Buffer } from 'buffer';
import type { Request as NodeFetchRequestType } from 'node-fetch';
import type { FetchInit, FetchResource, FormDataInternal } from '../types.js';
import type { FetchInit, FetchResource } from '../types.js';
import { FormData } from '../types.js';
import { getNodeFetch } from './node_fetch_imports.js';

Expand All @@ -15,20 +15,10 @@ function md5(string_: string) {

function getFormDataCacheKeyJson(formData: FormData) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const cacheKey = { ...formData } as FormDataInternal;
const boundary = formData.getBoundary();

delete cacheKey._boundary;

const boundaryReplaceRegex = new RegExp(boundary, 'g');

cacheKey._streams = cacheKey._streams.map(s => {
if (typeof s === 'string') {
return s.replace(boundaryReplaceRegex, '');
}

return s;
});
const cacheKey = {
type: 'FormData',
entries: Array.from(formData.entries()),
};

return cacheKey;
}
Expand All @@ -39,7 +29,7 @@ function getHeadersCacheKeyJson(headers: string[][]): string[][] {
.filter(([key, value]) => key !== 'cache-control' || value !== 'only-if-cached');
}

function getBodyCacheKeyJson(body: unknown): string | FormDataInternal | undefined {
function getBodyCacheKeyJson(body: unknown): string | object | undefined {
if (!body) {
return undefined;
}
Expand Down Expand Up @@ -94,7 +84,7 @@ export async function calculateCacheKey(resource: FetchResource, init?: FetchIni
: { url: resource, body: undefined };

const initCacheKeyJson = {
body: undefined as (undefined | string | FormDataInternal),
body: undefined as (undefined | string | object),
...init,
headers: getHeadersCacheKeyJson(Object.entries(init?.headers ?? {})),
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Request as NodeFetchRequestType } from 'node-fetch';
import FormData from 'form-data';
import { FormData } from 'formdata-node';
import { getNFCResponseClass as getNFCResponseClass } from './classes/response.js';
import { MemoryCache } from './classes/caching/memory_cache.js';
import { calculateCacheKey } from './helpers/cache_keys.js';
Expand Down
8 changes: 1 addition & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type fs from 'fs';
import type { Response as NodeFetchResponse } from 'node-fetch';
import type fetch from 'node-fetch';
import FormData from 'form-data';
import { FormData } from 'formdata-node';

export type FetchResource = Parameters<typeof fetch>[0];
export type FetchInit = Parameters<typeof fetch>[1];
export type CacheStrategy = (response: NodeFetchResponse) => Promise<boolean> | boolean;

export type FormDataInternal = {
_boundary?: string;
_streams: Array<string | fs.ReadStream>;
} & FormData;

export { FormData };

export type NFCResponseMetadata = {
Expand Down
2 changes: 1 addition & 1 deletion test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs from 'fs';
import assert from 'assert';
import { Agent } from 'http';
import { rimraf } from 'rimraf';
import FormData from 'form-data';
import { FormData } from 'formdata-node';
import standardFetch, { Request as StandardFetchRequest } from 'node-fetch';
import FetchCache, {
MemoryCache,
Expand Down

0 comments on commit 7860343

Please sign in to comment.