@whatwg-node/fetch 0.4.6 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/create-node-ponyfill.js +21 -21
- package/dist/readableStreamToReadable.js +16 -15
- package/package.json +1 -1
- package/tests/getFormDataMethod.spec.ts +40 -36
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @whatwg-node/fetch
|
|
2
2
|
|
|
3
|
+
## 0.4.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`e59cbb6`](https://github.com/ardatan/whatwg-node/commit/e59cbb667dfcbdd9c0cf609fd56dbd904ac85cbd) Thanks [@ardatan](https://github.com/ardatan)! - Do not patch global Headers if it is native, and support URL as a first parameter of `fetch`
|
|
8
|
+
|
|
3
9
|
## 0.4.6
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -11,7 +11,7 @@ module.exports = function createNodePonyfill(opts = {}) {
|
|
|
11
11
|
const ponyfills = {};
|
|
12
12
|
|
|
13
13
|
if (!opts.useNodeFetch) {
|
|
14
|
-
ponyfills.fetch = globalThis.fetch;
|
|
14
|
+
ponyfills.fetch = globalThis.fetch;
|
|
15
15
|
ponyfills.Headers = globalThis.Headers;
|
|
16
16
|
ponyfills.Request = globalThis.Request;
|
|
17
17
|
ponyfills.Response = globalThis.Response;
|
|
@@ -121,8 +121,7 @@ module.exports = function createNodePonyfill(opts = {}) {
|
|
|
121
121
|
|
|
122
122
|
class Request extends OriginalRequest {
|
|
123
123
|
constructor(requestOrUrl, options) {
|
|
124
|
-
if (typeof requestOrUrl === "string") {
|
|
125
|
-
options = options || {};
|
|
124
|
+
if (typeof requestOrUrl === "string" || requestOrUrl instanceof URL) {
|
|
126
125
|
super(requestOrUrl, options);
|
|
127
126
|
const contentType = this.headers.get("content-type");
|
|
128
127
|
if (contentType && contentType.startsWith("multipart/form-data")) {
|
|
@@ -140,7 +139,7 @@ module.exports = function createNodePonyfill(opts = {}) {
|
|
|
140
139
|
const originalFetch = ponyfills.fetch || undici.fetch;
|
|
141
140
|
|
|
142
141
|
const fetch = function (requestOrUrl, options) {
|
|
143
|
-
if (typeof requestOrUrl === "string") {
|
|
142
|
+
if (typeof requestOrUrl === "string" || requestOrUrl instanceof URL) {
|
|
144
143
|
// We cannot use our ctor because it leaks on Node 18's global fetch
|
|
145
144
|
return originalFetch(requestOrUrl, options);
|
|
146
145
|
}
|
|
@@ -169,7 +168,7 @@ module.exports = function createNodePonyfill(opts = {}) {
|
|
|
169
168
|
if (!ponyfills.Headers) {
|
|
170
169
|
ponyfills.Headers = nodeFetch.Headers;
|
|
171
170
|
// Sveltekit
|
|
172
|
-
if (globalThis.Headers) {
|
|
171
|
+
if (globalThis.Headers && nodeMajor < 18) {
|
|
173
172
|
Object.defineProperty(globalThis.Headers, Symbol.hasInstance, {
|
|
174
173
|
value(obj) {
|
|
175
174
|
return obj && obj.get && obj.set && obj.delete && obj.has && obj.append;
|
|
@@ -192,28 +191,29 @@ module.exports = function createNodePonyfill(opts = {}) {
|
|
|
192
191
|
|
|
193
192
|
class Request extends OriginalRequest {
|
|
194
193
|
constructor(requestOrUrl, options) {
|
|
195
|
-
if (typeof requestOrUrl === "string") {
|
|
194
|
+
if (typeof requestOrUrl === "string" || requestOrUrl instanceof URL) {
|
|
196
195
|
// Support schemaless URIs on the server for parity with the browser.
|
|
197
196
|
// Ex: //github.com/ -> https://github.com/
|
|
198
|
-
if (/^\/\//.test(requestOrUrl)) {
|
|
199
|
-
requestOrUrl = "https:" + requestOrUrl;
|
|
197
|
+
if (/^\/\//.test(requestOrUrl.toString())) {
|
|
198
|
+
requestOrUrl = "https:" + requestOrUrl.toString();
|
|
200
199
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
200
|
+
const fixedOptions = {
|
|
201
|
+
...options
|
|
202
|
+
};
|
|
203
|
+
fixedOptions.headers = new ponyfills.Headers(fixedOptions.headers || {});
|
|
204
|
+
fixedOptions.headers.set('Connection', 'keep-alive');
|
|
205
|
+
if (fixedOptions.body != null) {
|
|
206
|
+
if (fixedOptions.body[Symbol.toStringTag] === 'FormData') {
|
|
207
|
+
const encoder = new formDataEncoderModule.FormDataEncoder(fixedOptions.body)
|
|
207
208
|
for (const headerKey in encoder.headers) {
|
|
208
|
-
|
|
209
|
+
fixedOptions.headers.set(headerKey, encoder.headers[headerKey])
|
|
209
210
|
}
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
options.body = readableStreamToReadable(options.body);
|
|
211
|
+
fixedOptions.body = streams.Readable.from(encoder);
|
|
212
|
+
} else if (fixedOptions.body[Symbol.toStringTag] === 'ReadableStream') {
|
|
213
|
+
fixedOptions.body = readableStreamToReadable(fixedOptions.body);
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
|
-
super(requestOrUrl,
|
|
216
|
+
super(requestOrUrl, fixedOptions);
|
|
217
217
|
} else {
|
|
218
218
|
super(requestOrUrl);
|
|
219
219
|
}
|
|
@@ -222,7 +222,7 @@ module.exports = function createNodePonyfill(opts = {}) {
|
|
|
222
222
|
}
|
|
223
223
|
ponyfills.Request = Request;
|
|
224
224
|
const fetch = function (requestOrUrl, options) {
|
|
225
|
-
if (typeof requestOrUrl === "string") {
|
|
225
|
+
if (typeof requestOrUrl === "string" || requestOrUrl instanceof URL) {
|
|
226
226
|
return fetch(new Request(requestOrUrl, options));
|
|
227
227
|
}
|
|
228
228
|
if (requestOrUrl.url.startsWith('file:')) {
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
const streams = require('stream');
|
|
2
2
|
|
|
3
3
|
module.exports = function readableStreamToReadable(readableStream) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
4
|
+
return streams.Readable.from({
|
|
5
|
+
[Symbol.asyncIterator]() {
|
|
6
|
+
const reader = readableStream.getReader();
|
|
7
|
+
return {
|
|
8
|
+
next() {
|
|
9
|
+
return reader.read();
|
|
10
|
+
},
|
|
11
|
+
async return() {
|
|
12
|
+
reader.cancel();
|
|
13
|
+
reader.releaseLock();
|
|
14
|
+
await readableStream.cancel();
|
|
15
|
+
return { done: true };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
19
20
|
}
|
package/package.json
CHANGED
|
@@ -1,43 +1,47 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createTestContainer } from '../../server/test/create-test-container';
|
|
2
2
|
|
|
3
3
|
describe('getFormDataMethod', () => {
|
|
4
4
|
['fieldsFirst:true', 'fieldsFirst:false'].forEach(fieldsFirstFlag => {
|
|
5
|
-
const fetchAPI = createFetch({
|
|
6
|
-
formDataLimits: {
|
|
7
|
-
fieldsFirst: fieldsFirstFlag === 'fieldsFirst:true',
|
|
8
|
-
},
|
|
9
|
-
});
|
|
10
5
|
describe(fieldsFirstFlag, () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
6
|
+
createTestContainer(
|
|
7
|
+
fetchAPI => {
|
|
8
|
+
it('should parse fields correctly', async () => {
|
|
9
|
+
const formData = new fetchAPI.FormData();
|
|
10
|
+
formData.append('greetings', 'Hello world!');
|
|
11
|
+
formData.append('bye', 'Goodbye world!');
|
|
12
|
+
const request = new fetchAPI.Request('http://localhost:8080', {
|
|
13
|
+
method: 'POST',
|
|
14
|
+
body: formData,
|
|
15
|
+
});
|
|
16
|
+
const formdata = await request.formData();
|
|
17
|
+
expect(formdata.get('greetings')).toBe('Hello world!');
|
|
18
|
+
expect(formdata.get('bye')).toBe('Goodbye world!');
|
|
19
|
+
});
|
|
20
|
+
it('should parse and receive text files correctly', async () => {
|
|
21
|
+
const formData = new fetchAPI.FormData();
|
|
22
|
+
const greetingsFile = new fetchAPI.File(['Hello world!'], 'greetings.txt', { type: 'text/plain' });
|
|
23
|
+
const byeFile = new fetchAPI.File(['Goodbye world!'], 'bye.txt', { type: 'text/plain' });
|
|
24
|
+
formData.append('greetings', greetingsFile);
|
|
25
|
+
formData.append('bye', byeFile);
|
|
26
|
+
const request = new fetchAPI.Request('http://localhost:8080', {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
body: formData,
|
|
29
|
+
});
|
|
30
|
+
const formdata = await request.formData();
|
|
31
|
+
const receivedGreetingsFile = formdata.get('greetings') as File;
|
|
32
|
+
const receivedGreetingsText = await receivedGreetingsFile.text();
|
|
33
|
+
expect(receivedGreetingsText).toBe('Hello world!');
|
|
34
|
+
const receivedByeFile = formdata.get('bye') as File;
|
|
35
|
+
const receivedByeText = await receivedByeFile.text();
|
|
36
|
+
expect(receivedByeText).toBe('Goodbye world!');
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
formDataLimits: {
|
|
41
|
+
fieldsFirst: fieldsFirstFlag === 'fieldsFirst:true',
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
);
|
|
41
45
|
});
|
|
42
46
|
});
|
|
43
47
|
});
|