@whatwg-node/node-fetch 0.7.20-alpha-20250516143314-37dc0a3a0593ffa92881049cda6f8affca7b5e59 → 0.7.20
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/cjs/Body.js +8 -24
- package/cjs/Headers.js +8 -30
- package/cjs/Request.js +2 -4
- package/cjs/Response.js +8 -28
- package/cjs/fetchCurl.js +8 -14
- package/cjs/fetchNodeHttp.js +5 -15
- package/cjs/utils.js +0 -9
- package/esm/Body.js +8 -24
- package/esm/Headers.js +8 -30
- package/esm/Request.js +2 -4
- package/esm/Response.js +8 -28
- package/esm/fetchCurl.js +8 -14
- package/esm/fetchNodeHttp.js +5 -15
- package/esm/utils.js +0 -8
- package/package.json +1 -1
- package/typings/Body.d.cts +2 -2
- package/typings/Body.d.ts +2 -2
- package/typings/Request.d.cts +1 -1
- package/typings/Request.d.ts +1 -1
- package/typings/utils.d.cts +0 -1
- package/typings/utils.d.ts +0 -1
package/cjs/Body.js
CHANGED
@@ -28,11 +28,11 @@ class PonyfillBody {
|
|
28
28
|
bodyUsed = false;
|
29
29
|
contentType = null;
|
30
30
|
contentLength = null;
|
31
|
-
|
31
|
+
signal = null;
|
32
32
|
constructor(bodyInit, options = {}) {
|
33
33
|
this.bodyInit = bodyInit;
|
34
34
|
this.options = options;
|
35
|
-
this.
|
35
|
+
this.signal = options.signal || null;
|
36
36
|
const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit, options?.signal);
|
37
37
|
this._bodyFactory = bodyFactory;
|
38
38
|
this.contentType = contentType;
|
@@ -150,13 +150,6 @@ class PonyfillBody {
|
|
150
150
|
if (this._blob) {
|
151
151
|
return (0, utils_js_1.fakePromise)(this._blob);
|
152
152
|
}
|
153
|
-
if (this.bodyType === BodyInitType.String) {
|
154
|
-
this._text = this.bodyInit;
|
155
|
-
this._blob = new Blob_js_1.PonyfillBlob([this._text], {
|
156
|
-
type: this.contentType || 'text/plain;charset=UTF-8',
|
157
|
-
size: this.contentLength,
|
158
|
-
});
|
159
|
-
}
|
160
153
|
if (this.bodyType === BodyInitType.Blob) {
|
161
154
|
this._blob = this.bodyInit;
|
162
155
|
return (0, utils_js_1.fakePromise)(this._blob);
|
@@ -212,8 +205,8 @@ class PonyfillBody {
|
|
212
205
|
limits: formDataLimits,
|
213
206
|
defCharset: 'utf-8',
|
214
207
|
});
|
215
|
-
if (this.
|
216
|
-
(0, node_stream_1.addAbortSignal)(this.
|
208
|
+
if (this.signal) {
|
209
|
+
(0, node_stream_1.addAbortSignal)(this.signal, bb);
|
217
210
|
}
|
218
211
|
let completed = false;
|
219
212
|
const complete = (err) => {
|
@@ -286,17 +279,6 @@ class PonyfillBody {
|
|
286
279
|
if (this._buffer) {
|
287
280
|
return (0, utils_js_1.fakePromise)(this._buffer);
|
288
281
|
}
|
289
|
-
if (this._text) {
|
290
|
-
this._buffer = node_buffer_1.Buffer.from(this._text, 'utf-8');
|
291
|
-
return (0, utils_js_1.fakePromise)(this._buffer);
|
292
|
-
}
|
293
|
-
if (this.bodyType === BodyInitType.String) {
|
294
|
-
return this.text().then(text => {
|
295
|
-
this._text = text;
|
296
|
-
this._buffer = node_buffer_1.Buffer.from(text, 'utf-8');
|
297
|
-
return this._buffer;
|
298
|
-
});
|
299
|
-
}
|
300
282
|
if (this.bodyType === BodyInitType.Blob) {
|
301
283
|
if ((0, Blob_js_1.hasBufferMethod)(this.bodyInit)) {
|
302
284
|
return this.bodyInit.buffer().then(buf => {
|
@@ -376,13 +358,15 @@ function processBodyInit(bodyInit, signal) {
|
|
376
358
|
};
|
377
359
|
}
|
378
360
|
if (typeof bodyInit === 'string') {
|
379
|
-
const
|
361
|
+
const buffer = node_buffer_1.Buffer.from(bodyInit);
|
362
|
+
const contentLength = buffer.byteLength;
|
380
363
|
return {
|
381
364
|
bodyType: BodyInitType.String,
|
382
365
|
contentType: 'text/plain;charset=UTF-8',
|
383
366
|
contentLength,
|
367
|
+
buffer,
|
384
368
|
bodyFactory() {
|
385
|
-
const readable = node_stream_1.Readable.from(
|
369
|
+
const readable = node_stream_1.Readable.from(buffer);
|
386
370
|
return new ReadableStream_js_1.PonyfillReadableStream(readable);
|
387
371
|
},
|
388
372
|
};
|
package/cjs/Headers.js
CHANGED
@@ -4,7 +4,6 @@ exports.PonyfillHeaders = void 0;
|
|
4
4
|
exports.isHeadersLike = isHeadersLike;
|
5
5
|
const node_util_1 = require("node:util");
|
6
6
|
const IteratorObject_js_1 = require("./IteratorObject.js");
|
7
|
-
const utils_js_1 = require("./utils.js");
|
8
7
|
function isHeadersLike(headers) {
|
9
8
|
return headers?.get && headers?.forEach;
|
10
9
|
}
|
@@ -31,7 +30,7 @@ class PonyfillHeaders {
|
|
31
30
|
if (this.headersInit == null) {
|
32
31
|
return null;
|
33
32
|
}
|
34
|
-
if (
|
33
|
+
if (Array.isArray(this.headersInit)) {
|
35
34
|
const found = this.headersInit.filter(([headerKey]) => headerKey.toLowerCase() === normalized);
|
36
35
|
if (found.length === 0) {
|
37
36
|
return null;
|
@@ -67,9 +66,9 @@ class PonyfillHeaders {
|
|
67
66
|
// I could do a getter here, but I'm too lazy to type `getter`.
|
68
67
|
getMap() {
|
69
68
|
if (!this._map) {
|
70
|
-
this._setCookies
|
69
|
+
this._setCookies = [];
|
71
70
|
if (this.headersInit != null) {
|
72
|
-
if (
|
71
|
+
if (Array.isArray(this.headersInit)) {
|
73
72
|
this._map = new Map();
|
74
73
|
for (const [key, value] of this.headersInit) {
|
75
74
|
const normalizedKey = key.toLowerCase();
|
@@ -132,8 +131,7 @@ class PonyfillHeaders {
|
|
132
131
|
return value.toString();
|
133
132
|
}
|
134
133
|
has(name) {
|
135
|
-
|
136
|
-
if (key === 'set-cookie') {
|
134
|
+
if (name === 'set-cookie') {
|
137
135
|
return !!this._setCookies?.length;
|
138
136
|
}
|
139
137
|
return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
|
@@ -144,26 +142,6 @@ class PonyfillHeaders {
|
|
144
142
|
this._setCookies = [value];
|
145
143
|
return;
|
146
144
|
}
|
147
|
-
if (!this._map && this.headersInit != null) {
|
148
|
-
if ((0, utils_js_1.isArray)(this.headersInit)) {
|
149
|
-
const found = this.headersInit.find(([headerKey]) => headerKey.toLowerCase() === key);
|
150
|
-
if (found) {
|
151
|
-
found[1] = value;
|
152
|
-
}
|
153
|
-
else {
|
154
|
-
this.headersInit.push([key, value]);
|
155
|
-
}
|
156
|
-
return;
|
157
|
-
}
|
158
|
-
else if (isHeadersLike(this.headersInit)) {
|
159
|
-
this.headersInit.set(key, value);
|
160
|
-
return;
|
161
|
-
}
|
162
|
-
else {
|
163
|
-
this.headersInit[key] = value;
|
164
|
-
return;
|
165
|
-
}
|
166
|
-
}
|
167
145
|
this.getMap().set(key, value);
|
168
146
|
}
|
169
147
|
delete(name) {
|
@@ -180,7 +158,7 @@ class PonyfillHeaders {
|
|
180
158
|
});
|
181
159
|
if (!this._map) {
|
182
160
|
if (this.headersInit) {
|
183
|
-
if (
|
161
|
+
if (Array.isArray(this.headersInit)) {
|
184
162
|
this.headersInit.forEach(([key, value]) => {
|
185
163
|
callback(value, key, this);
|
186
164
|
});
|
@@ -208,7 +186,7 @@ class PonyfillHeaders {
|
|
208
186
|
}
|
209
187
|
if (!this._map) {
|
210
188
|
if (this.headersInit) {
|
211
|
-
if (
|
189
|
+
if (Array.isArray(this.headersInit)) {
|
212
190
|
yield* this.headersInit.map(([key]) => key)[Symbol.iterator]();
|
213
191
|
return;
|
214
192
|
}
|
@@ -231,7 +209,7 @@ class PonyfillHeaders {
|
|
231
209
|
}
|
232
210
|
if (!this._map) {
|
233
211
|
if (this.headersInit) {
|
234
|
-
if (
|
212
|
+
if (Array.isArray(this.headersInit)) {
|
235
213
|
yield* this.headersInit.map(([, value]) => value)[Symbol.iterator]();
|
236
214
|
return;
|
237
215
|
}
|
@@ -254,7 +232,7 @@ class PonyfillHeaders {
|
|
254
232
|
}
|
255
233
|
if (!this._map) {
|
256
234
|
if (this.headersInit) {
|
257
|
-
if (
|
235
|
+
if (Array.isArray(this.headersInit)) {
|
258
236
|
yield* this.headersInit;
|
259
237
|
return;
|
260
238
|
}
|
package/cjs/Request.js
CHANGED
@@ -57,6 +57,7 @@ class PonyfillRequest extends Body_js_1.PonyfillBody {
|
|
57
57
|
this.redirect = requestInit?.redirect || 'follow';
|
58
58
|
this.referrer = requestInit?.referrer || 'about:client';
|
59
59
|
this.referrerPolicy = requestInit?.referrerPolicy || 'no-referrer';
|
60
|
+
this.signal = requestInit?.signal || new AbortController().signal;
|
60
61
|
this.headersSerializer = requestInit?.headersSerializer;
|
61
62
|
this.duplex = requestInit?.duplex || 'half';
|
62
63
|
this.destination = 'document';
|
@@ -91,10 +92,6 @@ class PonyfillRequest extends Body_js_1.PonyfillBody {
|
|
91
92
|
referrer;
|
92
93
|
referrerPolicy;
|
93
94
|
_url;
|
94
|
-
get signal() {
|
95
|
-
this._signal ||= new AbortController().signal;
|
96
|
-
return this._signal;
|
97
|
-
}
|
98
95
|
get url() {
|
99
96
|
if (this._url == null) {
|
100
97
|
if (this._parsedUrl) {
|
@@ -120,6 +117,7 @@ class PonyfillRequest extends Body_js_1.PonyfillBody {
|
|
120
117
|
}
|
121
118
|
duplex;
|
122
119
|
agent;
|
120
|
+
signal;
|
123
121
|
clone() {
|
124
122
|
return this;
|
125
123
|
}
|
package/cjs/Response.js
CHANGED
@@ -4,7 +4,6 @@ exports.PonyfillResponse = void 0;
|
|
4
4
|
const node_http_1 = require("node:http");
|
5
5
|
const Body_js_1 = require("./Body.js");
|
6
6
|
const Headers_js_1 = require("./Headers.js");
|
7
|
-
const utils_js_1 = require("./utils.js");
|
8
7
|
const JSON_CONTENT_TYPE = 'application/json; charset=utf-8';
|
9
8
|
class PonyfillResponse extends Body_js_1.PonyfillBody {
|
10
9
|
headers;
|
@@ -19,6 +18,7 @@ class PonyfillResponse extends Body_js_1.PonyfillBody {
|
|
19
18
|
this.url = init?.url || '';
|
20
19
|
this.redirected = init?.redirected || false;
|
21
20
|
this.type = init?.type || 'default';
|
21
|
+
this.signal = init?.signal || null;
|
22
22
|
this.handleContentLengthHeader();
|
23
23
|
}
|
24
24
|
get ok() {
|
@@ -49,33 +49,13 @@ class PonyfillResponse extends Body_js_1.PonyfillBody {
|
|
49
49
|
status,
|
50
50
|
});
|
51
51
|
}
|
52
|
-
static json(data, init) {
|
53
|
-
|
54
|
-
init
|
55
|
-
headers
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
}
|
60
|
-
else if (!init.headers) {
|
61
|
-
init.headers = {
|
62
|
-
'content-type': JSON_CONTENT_TYPE,
|
63
|
-
};
|
64
|
-
}
|
65
|
-
else if ((0, Headers_js_1.isHeadersLike)(init.headers)) {
|
66
|
-
if (!init.headers.has('content-type')) {
|
67
|
-
init.headers.set('content-type', JSON_CONTENT_TYPE);
|
68
|
-
}
|
69
|
-
}
|
70
|
-
else if ((0, utils_js_1.isArray)(init.headers)) {
|
71
|
-
if (!init.headers.some(([key]) => key.toLowerCase() === 'content-type')) {
|
72
|
-
init.headers.push(['content-type', JSON_CONTENT_TYPE]);
|
73
|
-
}
|
74
|
-
}
|
75
|
-
else if (typeof init.headers === 'object') {
|
76
|
-
if (init.headers?.['content-type'] == null) {
|
77
|
-
init.headers['content-type'] = JSON_CONTENT_TYPE;
|
78
|
-
}
|
52
|
+
static json(data, init = {}) {
|
53
|
+
init.headers =
|
54
|
+
init?.headers && (0, Headers_js_1.isHeadersLike)(init.headers)
|
55
|
+
? init.headers
|
56
|
+
: new Headers_js_1.PonyfillHeaders(init?.headers);
|
57
|
+
if (!init.headers.has('content-type')) {
|
58
|
+
init.headers.set('content-type', JSON_CONTENT_TYPE);
|
79
59
|
}
|
80
60
|
return new PonyfillResponse(JSON.stringify(data), init);
|
81
61
|
}
|
package/cjs/fetchCurl.js
CHANGED
@@ -21,18 +21,8 @@ function fetchCurl(fetchRequest) {
|
|
21
21
|
curlHandle.setOpt('CAINFO_BLOB', node_tls_1.rootCertificates.join('\n'));
|
22
22
|
}
|
23
23
|
curlHandle.enable(CurlFeature.StreamResponse);
|
24
|
-
let signal;
|
25
|
-
if (fetchRequest._signal === null) {
|
26
|
-
signal = undefined;
|
27
|
-
}
|
28
|
-
else if (fetchRequest._signal) {
|
29
|
-
signal = fetchRequest._signal;
|
30
|
-
}
|
31
|
-
else {
|
32
|
-
signal = fetchRequest.signal;
|
33
|
-
}
|
34
24
|
curlHandle.setStreamProgressCallback(function () {
|
35
|
-
return signal
|
25
|
+
return fetchRequest.signal.aborted ? (process.env.DEBUG ? CurlProgressFunc.Continue : 1) : 0;
|
36
26
|
});
|
37
27
|
if (fetchRequest['bodyType'] === 'String') {
|
38
28
|
curlHandle.setOpt('POSTFIELDS', fetchRequest['bodyInit']);
|
@@ -79,7 +69,9 @@ function fetchCurl(fetchRequest) {
|
|
79
69
|
}
|
80
70
|
}
|
81
71
|
}
|
82
|
-
signal
|
72
|
+
if (fetchRequest.signal) {
|
73
|
+
fetchRequest.signal.addEventListener('abort', onAbort, { once: true });
|
74
|
+
}
|
83
75
|
curlHandle.once('end', function endListener() {
|
84
76
|
try {
|
85
77
|
curlHandle.close();
|
@@ -87,7 +79,9 @@ function fetchCurl(fetchRequest) {
|
|
87
79
|
catch (e) {
|
88
80
|
deferredPromise.reject(e);
|
89
81
|
}
|
90
|
-
signal
|
82
|
+
if (fetchRequest.signal) {
|
83
|
+
fetchRequest.signal.removeEventListener('abort', onAbort);
|
84
|
+
}
|
91
85
|
});
|
92
86
|
curlHandle.once('error', function errorListener(error) {
|
93
87
|
if (streamResolved && !streamResolved.closed && !streamResolved.destroyed) {
|
@@ -109,7 +103,7 @@ function fetchCurl(fetchRequest) {
|
|
109
103
|
curlHandle.once('stream', function streamListener(stream, status, headersBuf) {
|
110
104
|
const outputStream = (0, utils_js_1.wrapIncomingMessageWithPassthrough)({
|
111
105
|
incomingMessage: stream,
|
112
|
-
signal,
|
106
|
+
signal: fetchRequest.signal,
|
113
107
|
onError: deferredPromise.reject,
|
114
108
|
});
|
115
109
|
const headersFlat = headersBuf
|
package/cjs/fetchNodeHttp.js
CHANGED
@@ -28,23 +28,13 @@ function fetchNodeHttp(fetchRequest) {
|
|
28
28
|
if (nodeHeaders['accept-encoding'] == null) {
|
29
29
|
nodeHeaders['accept-encoding'] = 'gzip, deflate, br';
|
30
30
|
}
|
31
|
-
let signal;
|
32
|
-
if (fetchRequest._signal === null) {
|
33
|
-
signal = undefined;
|
34
|
-
}
|
35
|
-
else if (fetchRequest._signal) {
|
36
|
-
signal = fetchRequest._signal;
|
37
|
-
}
|
38
|
-
else {
|
39
|
-
signal = fetchRequest.signal;
|
40
|
-
}
|
41
31
|
let nodeRequest;
|
42
32
|
// If it is our ponyfilled Request, it should have `parsedUrl` which is a `URL` object
|
43
33
|
if (fetchRequest.parsedUrl) {
|
44
34
|
nodeRequest = requestFn(fetchRequest.parsedUrl, {
|
45
35
|
method: fetchRequest.method,
|
46
36
|
headers: nodeHeaders,
|
47
|
-
signal,
|
37
|
+
signal: fetchRequest.signal,
|
48
38
|
agent: fetchRequest.agent,
|
49
39
|
});
|
50
40
|
}
|
@@ -52,7 +42,7 @@ function fetchNodeHttp(fetchRequest) {
|
|
52
42
|
nodeRequest = requestFn(fetchRequest.url, {
|
53
43
|
method: fetchRequest.method,
|
54
44
|
headers: nodeHeaders,
|
55
|
-
signal,
|
45
|
+
signal: fetchRequest.signal,
|
56
46
|
agent: fetchRequest.agent,
|
57
47
|
});
|
58
48
|
}
|
@@ -99,7 +89,7 @@ function fetchNodeHttp(fetchRequest) {
|
|
99
89
|
outputStream = (0, utils_js_1.wrapIncomingMessageWithPassthrough)({
|
100
90
|
incomingMessage: nodeResponse,
|
101
91
|
passThrough: outputStream,
|
102
|
-
signal,
|
92
|
+
signal: fetchRequest.signal,
|
103
93
|
onError: reject,
|
104
94
|
});
|
105
95
|
}
|
@@ -113,12 +103,12 @@ function fetchNodeHttp(fetchRequest) {
|
|
113
103
|
statusText,
|
114
104
|
headers: nodeResponse.headers,
|
115
105
|
url: fetchRequest.url,
|
116
|
-
signal,
|
106
|
+
signal: fetchRequest.signal,
|
117
107
|
});
|
118
108
|
resolve(ponyfillResponse);
|
119
109
|
});
|
120
110
|
if (fetchRequest['_buffer'] != null) {
|
121
|
-
(0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.safeWrite)(fetchRequest['_buffer'], nodeRequest, signal), () => (0, utils_js_1.endStream)(nodeRequest), reject);
|
111
|
+
(0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.safeWrite)(fetchRequest['_buffer'], nodeRequest, fetchRequest.signal), () => (0, utils_js_1.endStream)(nodeRequest), reject);
|
122
112
|
}
|
123
113
|
else {
|
124
114
|
const nodeReadable = (fetchRequest.body != null
|
package/cjs/utils.js
CHANGED
@@ -10,7 +10,6 @@ exports.shouldRedirect = shouldRedirect;
|
|
10
10
|
exports.wrapIncomingMessageWithPassthrough = wrapIncomingMessageWithPassthrough;
|
11
11
|
exports.endStream = endStream;
|
12
12
|
exports.safeWrite = safeWrite;
|
13
|
-
exports.isArray = isArray;
|
14
13
|
const node_events_1 = require("node:events");
|
15
14
|
const node_stream_1 = require("node:stream");
|
16
15
|
const promises_1 = require("node:stream/promises");
|
@@ -21,11 +20,6 @@ function getHeadersObj(headers) {
|
|
21
20
|
if (headers == null || !isHeadersInstance(headers)) {
|
22
21
|
return headers;
|
23
22
|
}
|
24
|
-
// @ts-expect-error - `headersInit` is not a public property
|
25
|
-
if (headers.headersInit && !headers._map && !isHeadersInstance(headers.headersInit)) {
|
26
|
-
// @ts-expect-error - `headersInit` is not a public property
|
27
|
-
return headers.headersInit;
|
28
|
-
}
|
29
23
|
return Object.fromEntries(headers.entries());
|
30
24
|
}
|
31
25
|
function defaultHeadersSerializer(headers, onContentLength) {
|
@@ -79,6 +73,3 @@ function safeWrite(chunk, stream, signal) {
|
|
79
73
|
});
|
80
74
|
}
|
81
75
|
}
|
82
|
-
function isArray(value) {
|
83
|
-
return value?.splice != null;
|
84
|
-
}
|
package/esm/Body.js
CHANGED
@@ -25,11 +25,11 @@ export class PonyfillBody {
|
|
25
25
|
bodyUsed = false;
|
26
26
|
contentType = null;
|
27
27
|
contentLength = null;
|
28
|
-
|
28
|
+
signal = null;
|
29
29
|
constructor(bodyInit, options = {}) {
|
30
30
|
this.bodyInit = bodyInit;
|
31
31
|
this.options = options;
|
32
|
-
this.
|
32
|
+
this.signal = options.signal || null;
|
33
33
|
const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit, options?.signal);
|
34
34
|
this._bodyFactory = bodyFactory;
|
35
35
|
this.contentType = contentType;
|
@@ -147,13 +147,6 @@ export class PonyfillBody {
|
|
147
147
|
if (this._blob) {
|
148
148
|
return fakePromise(this._blob);
|
149
149
|
}
|
150
|
-
if (this.bodyType === BodyInitType.String) {
|
151
|
-
this._text = this.bodyInit;
|
152
|
-
this._blob = new PonyfillBlob([this._text], {
|
153
|
-
type: this.contentType || 'text/plain;charset=UTF-8',
|
154
|
-
size: this.contentLength,
|
155
|
-
});
|
156
|
-
}
|
157
150
|
if (this.bodyType === BodyInitType.Blob) {
|
158
151
|
this._blob = this.bodyInit;
|
159
152
|
return fakePromise(this._blob);
|
@@ -209,8 +202,8 @@ export class PonyfillBody {
|
|
209
202
|
limits: formDataLimits,
|
210
203
|
defCharset: 'utf-8',
|
211
204
|
});
|
212
|
-
if (this.
|
213
|
-
addAbortSignal(this.
|
205
|
+
if (this.signal) {
|
206
|
+
addAbortSignal(this.signal, bb);
|
214
207
|
}
|
215
208
|
let completed = false;
|
216
209
|
const complete = (err) => {
|
@@ -283,17 +276,6 @@ export class PonyfillBody {
|
|
283
276
|
if (this._buffer) {
|
284
277
|
return fakePromise(this._buffer);
|
285
278
|
}
|
286
|
-
if (this._text) {
|
287
|
-
this._buffer = Buffer.from(this._text, 'utf-8');
|
288
|
-
return fakePromise(this._buffer);
|
289
|
-
}
|
290
|
-
if (this.bodyType === BodyInitType.String) {
|
291
|
-
return this.text().then(text => {
|
292
|
-
this._text = text;
|
293
|
-
this._buffer = Buffer.from(text, 'utf-8');
|
294
|
-
return this._buffer;
|
295
|
-
});
|
296
|
-
}
|
297
279
|
if (this.bodyType === BodyInitType.Blob) {
|
298
280
|
if (hasBufferMethod(this.bodyInit)) {
|
299
281
|
return this.bodyInit.buffer().then(buf => {
|
@@ -372,13 +354,15 @@ function processBodyInit(bodyInit, signal) {
|
|
372
354
|
};
|
373
355
|
}
|
374
356
|
if (typeof bodyInit === 'string') {
|
375
|
-
const
|
357
|
+
const buffer = Buffer.from(bodyInit);
|
358
|
+
const contentLength = buffer.byteLength;
|
376
359
|
return {
|
377
360
|
bodyType: BodyInitType.String,
|
378
361
|
contentType: 'text/plain;charset=UTF-8',
|
379
362
|
contentLength,
|
363
|
+
buffer,
|
380
364
|
bodyFactory() {
|
381
|
-
const readable = Readable.from(
|
365
|
+
const readable = Readable.from(buffer);
|
382
366
|
return new PonyfillReadableStream(readable);
|
383
367
|
},
|
384
368
|
};
|
package/esm/Headers.js
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import { inspect } from 'node:util';
|
2
2
|
import { PonyfillIteratorObject } from './IteratorObject.js';
|
3
|
-
import { isArray } from './utils.js';
|
4
3
|
export function isHeadersLike(headers) {
|
5
4
|
return headers?.get && headers?.forEach;
|
6
5
|
}
|
@@ -27,7 +26,7 @@ export class PonyfillHeaders {
|
|
27
26
|
if (this.headersInit == null) {
|
28
27
|
return null;
|
29
28
|
}
|
30
|
-
if (isArray(this.headersInit)) {
|
29
|
+
if (Array.isArray(this.headersInit)) {
|
31
30
|
const found = this.headersInit.filter(([headerKey]) => headerKey.toLowerCase() === normalized);
|
32
31
|
if (found.length === 0) {
|
33
32
|
return null;
|
@@ -63,9 +62,9 @@ export class PonyfillHeaders {
|
|
63
62
|
// I could do a getter here, but I'm too lazy to type `getter`.
|
64
63
|
getMap() {
|
65
64
|
if (!this._map) {
|
66
|
-
this._setCookies
|
65
|
+
this._setCookies = [];
|
67
66
|
if (this.headersInit != null) {
|
68
|
-
if (isArray(this.headersInit)) {
|
67
|
+
if (Array.isArray(this.headersInit)) {
|
69
68
|
this._map = new Map();
|
70
69
|
for (const [key, value] of this.headersInit) {
|
71
70
|
const normalizedKey = key.toLowerCase();
|
@@ -128,8 +127,7 @@ export class PonyfillHeaders {
|
|
128
127
|
return value.toString();
|
129
128
|
}
|
130
129
|
has(name) {
|
131
|
-
|
132
|
-
if (key === 'set-cookie') {
|
130
|
+
if (name === 'set-cookie') {
|
133
131
|
return !!this._setCookies?.length;
|
134
132
|
}
|
135
133
|
return !!this._get(name); // we might need to check if header exists and not just check if it's not nullable
|
@@ -140,26 +138,6 @@ export class PonyfillHeaders {
|
|
140
138
|
this._setCookies = [value];
|
141
139
|
return;
|
142
140
|
}
|
143
|
-
if (!this._map && this.headersInit != null) {
|
144
|
-
if (isArray(this.headersInit)) {
|
145
|
-
const found = this.headersInit.find(([headerKey]) => headerKey.toLowerCase() === key);
|
146
|
-
if (found) {
|
147
|
-
found[1] = value;
|
148
|
-
}
|
149
|
-
else {
|
150
|
-
this.headersInit.push([key, value]);
|
151
|
-
}
|
152
|
-
return;
|
153
|
-
}
|
154
|
-
else if (isHeadersLike(this.headersInit)) {
|
155
|
-
this.headersInit.set(key, value);
|
156
|
-
return;
|
157
|
-
}
|
158
|
-
else {
|
159
|
-
this.headersInit[key] = value;
|
160
|
-
return;
|
161
|
-
}
|
162
|
-
}
|
163
141
|
this.getMap().set(key, value);
|
164
142
|
}
|
165
143
|
delete(name) {
|
@@ -176,7 +154,7 @@ export class PonyfillHeaders {
|
|
176
154
|
});
|
177
155
|
if (!this._map) {
|
178
156
|
if (this.headersInit) {
|
179
|
-
if (isArray(this.headersInit)) {
|
157
|
+
if (Array.isArray(this.headersInit)) {
|
180
158
|
this.headersInit.forEach(([key, value]) => {
|
181
159
|
callback(value, key, this);
|
182
160
|
});
|
@@ -204,7 +182,7 @@ export class PonyfillHeaders {
|
|
204
182
|
}
|
205
183
|
if (!this._map) {
|
206
184
|
if (this.headersInit) {
|
207
|
-
if (isArray(this.headersInit)) {
|
185
|
+
if (Array.isArray(this.headersInit)) {
|
208
186
|
yield* this.headersInit.map(([key]) => key)[Symbol.iterator]();
|
209
187
|
return;
|
210
188
|
}
|
@@ -227,7 +205,7 @@ export class PonyfillHeaders {
|
|
227
205
|
}
|
228
206
|
if (!this._map) {
|
229
207
|
if (this.headersInit) {
|
230
|
-
if (isArray(this.headersInit)) {
|
208
|
+
if (Array.isArray(this.headersInit)) {
|
231
209
|
yield* this.headersInit.map(([, value]) => value)[Symbol.iterator]();
|
232
210
|
return;
|
233
211
|
}
|
@@ -250,7 +228,7 @@ export class PonyfillHeaders {
|
|
250
228
|
}
|
251
229
|
if (!this._map) {
|
252
230
|
if (this.headersInit) {
|
253
|
-
if (isArray(this.headersInit)) {
|
231
|
+
if (Array.isArray(this.headersInit)) {
|
254
232
|
yield* this.headersInit;
|
255
233
|
return;
|
256
234
|
}
|
package/esm/Request.js
CHANGED
@@ -54,6 +54,7 @@ export class PonyfillRequest extends PonyfillBody {
|
|
54
54
|
this.redirect = requestInit?.redirect || 'follow';
|
55
55
|
this.referrer = requestInit?.referrer || 'about:client';
|
56
56
|
this.referrerPolicy = requestInit?.referrerPolicy || 'no-referrer';
|
57
|
+
this.signal = requestInit?.signal || new AbortController().signal;
|
57
58
|
this.headersSerializer = requestInit?.headersSerializer;
|
58
59
|
this.duplex = requestInit?.duplex || 'half';
|
59
60
|
this.destination = 'document';
|
@@ -88,10 +89,6 @@ export class PonyfillRequest extends PonyfillBody {
|
|
88
89
|
referrer;
|
89
90
|
referrerPolicy;
|
90
91
|
_url;
|
91
|
-
get signal() {
|
92
|
-
this._signal ||= new AbortController().signal;
|
93
|
-
return this._signal;
|
94
|
-
}
|
95
92
|
get url() {
|
96
93
|
if (this._url == null) {
|
97
94
|
if (this._parsedUrl) {
|
@@ -117,6 +114,7 @@ export class PonyfillRequest extends PonyfillBody {
|
|
117
114
|
}
|
118
115
|
duplex;
|
119
116
|
agent;
|
117
|
+
signal;
|
120
118
|
clone() {
|
121
119
|
return this;
|
122
120
|
}
|
package/esm/Response.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import { STATUS_CODES } from 'node:http';
|
2
2
|
import { PonyfillBody } from './Body.js';
|
3
3
|
import { isHeadersLike, PonyfillHeaders } from './Headers.js';
|
4
|
-
import { isArray } from './utils.js';
|
5
4
|
const JSON_CONTENT_TYPE = 'application/json; charset=utf-8';
|
6
5
|
export class PonyfillResponse extends PonyfillBody {
|
7
6
|
headers;
|
@@ -16,6 +15,7 @@ export class PonyfillResponse extends PonyfillBody {
|
|
16
15
|
this.url = init?.url || '';
|
17
16
|
this.redirected = init?.redirected || false;
|
18
17
|
this.type = init?.type || 'default';
|
18
|
+
this.signal = init?.signal || null;
|
19
19
|
this.handleContentLengthHeader();
|
20
20
|
}
|
21
21
|
get ok() {
|
@@ -46,33 +46,13 @@ export class PonyfillResponse extends PonyfillBody {
|
|
46
46
|
status,
|
47
47
|
});
|
48
48
|
}
|
49
|
-
static json(data, init) {
|
50
|
-
|
51
|
-
init
|
52
|
-
headers
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
}
|
57
|
-
else if (!init.headers) {
|
58
|
-
init.headers = {
|
59
|
-
'content-type': JSON_CONTENT_TYPE,
|
60
|
-
};
|
61
|
-
}
|
62
|
-
else if (isHeadersLike(init.headers)) {
|
63
|
-
if (!init.headers.has('content-type')) {
|
64
|
-
init.headers.set('content-type', JSON_CONTENT_TYPE);
|
65
|
-
}
|
66
|
-
}
|
67
|
-
else if (isArray(init.headers)) {
|
68
|
-
if (!init.headers.some(([key]) => key.toLowerCase() === 'content-type')) {
|
69
|
-
init.headers.push(['content-type', JSON_CONTENT_TYPE]);
|
70
|
-
}
|
71
|
-
}
|
72
|
-
else if (typeof init.headers === 'object') {
|
73
|
-
if (init.headers?.['content-type'] == null) {
|
74
|
-
init.headers['content-type'] = JSON_CONTENT_TYPE;
|
75
|
-
}
|
49
|
+
static json(data, init = {}) {
|
50
|
+
init.headers =
|
51
|
+
init?.headers && isHeadersLike(init.headers)
|
52
|
+
? init.headers
|
53
|
+
: new PonyfillHeaders(init?.headers);
|
54
|
+
if (!init.headers.has('content-type')) {
|
55
|
+
init.headers.set('content-type', JSON_CONTENT_TYPE);
|
76
56
|
}
|
77
57
|
return new PonyfillResponse(JSON.stringify(data), init);
|
78
58
|
}
|
package/esm/fetchCurl.js
CHANGED
@@ -18,18 +18,8 @@ export function fetchCurl(fetchRequest) {
|
|
18
18
|
curlHandle.setOpt('CAINFO_BLOB', rootCertificates.join('\n'));
|
19
19
|
}
|
20
20
|
curlHandle.enable(CurlFeature.StreamResponse);
|
21
|
-
let signal;
|
22
|
-
if (fetchRequest._signal === null) {
|
23
|
-
signal = undefined;
|
24
|
-
}
|
25
|
-
else if (fetchRequest._signal) {
|
26
|
-
signal = fetchRequest._signal;
|
27
|
-
}
|
28
|
-
else {
|
29
|
-
signal = fetchRequest.signal;
|
30
|
-
}
|
31
21
|
curlHandle.setStreamProgressCallback(function () {
|
32
|
-
return signal
|
22
|
+
return fetchRequest.signal.aborted ? (process.env.DEBUG ? CurlProgressFunc.Continue : 1) : 0;
|
33
23
|
});
|
34
24
|
if (fetchRequest['bodyType'] === 'String') {
|
35
25
|
curlHandle.setOpt('POSTFIELDS', fetchRequest['bodyInit']);
|
@@ -76,7 +66,9 @@ export function fetchCurl(fetchRequest) {
|
|
76
66
|
}
|
77
67
|
}
|
78
68
|
}
|
79
|
-
signal
|
69
|
+
if (fetchRequest.signal) {
|
70
|
+
fetchRequest.signal.addEventListener('abort', onAbort, { once: true });
|
71
|
+
}
|
80
72
|
curlHandle.once('end', function endListener() {
|
81
73
|
try {
|
82
74
|
curlHandle.close();
|
@@ -84,7 +76,9 @@ export function fetchCurl(fetchRequest) {
|
|
84
76
|
catch (e) {
|
85
77
|
deferredPromise.reject(e);
|
86
78
|
}
|
87
|
-
signal
|
79
|
+
if (fetchRequest.signal) {
|
80
|
+
fetchRequest.signal.removeEventListener('abort', onAbort);
|
81
|
+
}
|
88
82
|
});
|
89
83
|
curlHandle.once('error', function errorListener(error) {
|
90
84
|
if (streamResolved && !streamResolved.closed && !streamResolved.destroyed) {
|
@@ -106,7 +100,7 @@ export function fetchCurl(fetchRequest) {
|
|
106
100
|
curlHandle.once('stream', function streamListener(stream, status, headersBuf) {
|
107
101
|
const outputStream = wrapIncomingMessageWithPassthrough({
|
108
102
|
incomingMessage: stream,
|
109
|
-
signal,
|
103
|
+
signal: fetchRequest.signal,
|
110
104
|
onError: deferredPromise.reject,
|
111
105
|
});
|
112
106
|
const headersFlat = headersBuf
|
package/esm/fetchNodeHttp.js
CHANGED
@@ -25,23 +25,13 @@ export function fetchNodeHttp(fetchRequest) {
|
|
25
25
|
if (nodeHeaders['accept-encoding'] == null) {
|
26
26
|
nodeHeaders['accept-encoding'] = 'gzip, deflate, br';
|
27
27
|
}
|
28
|
-
let signal;
|
29
|
-
if (fetchRequest._signal === null) {
|
30
|
-
signal = undefined;
|
31
|
-
}
|
32
|
-
else if (fetchRequest._signal) {
|
33
|
-
signal = fetchRequest._signal;
|
34
|
-
}
|
35
|
-
else {
|
36
|
-
signal = fetchRequest.signal;
|
37
|
-
}
|
38
28
|
let nodeRequest;
|
39
29
|
// If it is our ponyfilled Request, it should have `parsedUrl` which is a `URL` object
|
40
30
|
if (fetchRequest.parsedUrl) {
|
41
31
|
nodeRequest = requestFn(fetchRequest.parsedUrl, {
|
42
32
|
method: fetchRequest.method,
|
43
33
|
headers: nodeHeaders,
|
44
|
-
signal,
|
34
|
+
signal: fetchRequest.signal,
|
45
35
|
agent: fetchRequest.agent,
|
46
36
|
});
|
47
37
|
}
|
@@ -49,7 +39,7 @@ export function fetchNodeHttp(fetchRequest) {
|
|
49
39
|
nodeRequest = requestFn(fetchRequest.url, {
|
50
40
|
method: fetchRequest.method,
|
51
41
|
headers: nodeHeaders,
|
52
|
-
signal,
|
42
|
+
signal: fetchRequest.signal,
|
53
43
|
agent: fetchRequest.agent,
|
54
44
|
});
|
55
45
|
}
|
@@ -96,7 +86,7 @@ export function fetchNodeHttp(fetchRequest) {
|
|
96
86
|
outputStream = wrapIncomingMessageWithPassthrough({
|
97
87
|
incomingMessage: nodeResponse,
|
98
88
|
passThrough: outputStream,
|
99
|
-
signal,
|
89
|
+
signal: fetchRequest.signal,
|
100
90
|
onError: reject,
|
101
91
|
});
|
102
92
|
}
|
@@ -110,12 +100,12 @@ export function fetchNodeHttp(fetchRequest) {
|
|
110
100
|
statusText,
|
111
101
|
headers: nodeResponse.headers,
|
112
102
|
url: fetchRequest.url,
|
113
|
-
signal,
|
103
|
+
signal: fetchRequest.signal,
|
114
104
|
});
|
115
105
|
resolve(ponyfillResponse);
|
116
106
|
});
|
117
107
|
if (fetchRequest['_buffer'] != null) {
|
118
|
-
handleMaybePromise(() => safeWrite(fetchRequest['_buffer'], nodeRequest, signal), () => endStream(nodeRequest), reject);
|
108
|
+
handleMaybePromise(() => safeWrite(fetchRequest['_buffer'], nodeRequest, fetchRequest.signal), () => endStream(nodeRequest), reject);
|
119
109
|
}
|
120
110
|
else {
|
121
111
|
const nodeReadable = (fetchRequest.body != null
|
package/esm/utils.js
CHANGED
@@ -8,11 +8,6 @@ export function getHeadersObj(headers) {
|
|
8
8
|
if (headers == null || !isHeadersInstance(headers)) {
|
9
9
|
return headers;
|
10
10
|
}
|
11
|
-
// @ts-expect-error - `headersInit` is not a public property
|
12
|
-
if (headers.headersInit && !headers._map && !isHeadersInstance(headers.headersInit)) {
|
13
|
-
// @ts-expect-error - `headersInit` is not a public property
|
14
|
-
return headers.headersInit;
|
15
|
-
}
|
16
11
|
return Object.fromEntries(headers.entries());
|
17
12
|
}
|
18
13
|
export function defaultHeadersSerializer(headers, onContentLength) {
|
@@ -65,6 +60,3 @@ export function safeWrite(chunk, stream, signal) {
|
|
65
60
|
});
|
66
61
|
}
|
67
62
|
}
|
68
|
-
export function isArray(value) {
|
69
|
-
return value?.splice != null;
|
70
|
-
}
|
package/package.json
CHANGED
package/typings/Body.d.cts
CHANGED
@@ -16,7 +16,7 @@ export interface FormDataLimits {
|
|
16
16
|
}
|
17
17
|
export interface PonyfillBodyOptions {
|
18
18
|
formDataLimits?: FormDataLimits;
|
19
|
-
signal?: AbortSignal
|
19
|
+
signal?: AbortSignal;
|
20
20
|
}
|
21
21
|
export declare class PonyfillBody<TJSON = any> implements Body {
|
22
22
|
private bodyInit;
|
@@ -24,7 +24,7 @@ export declare class PonyfillBody<TJSON = any> implements Body {
|
|
24
24
|
bodyUsed: boolean;
|
25
25
|
contentType: string | null;
|
26
26
|
contentLength: number | null;
|
27
|
-
|
27
|
+
signal?: AbortSignal | null;
|
28
28
|
constructor(bodyInit: BodyPonyfillInit | null, options?: PonyfillBodyOptions);
|
29
29
|
private bodyType?;
|
30
30
|
private _bodyFactory;
|
package/typings/Body.d.ts
CHANGED
@@ -16,7 +16,7 @@ export interface FormDataLimits {
|
|
16
16
|
}
|
17
17
|
export interface PonyfillBodyOptions {
|
18
18
|
formDataLimits?: FormDataLimits;
|
19
|
-
signal?: AbortSignal
|
19
|
+
signal?: AbortSignal;
|
20
20
|
}
|
21
21
|
export declare class PonyfillBody<TJSON = any> implements Body {
|
22
22
|
private bodyInit;
|
@@ -24,7 +24,7 @@ export declare class PonyfillBody<TJSON = any> implements Body {
|
|
24
24
|
bodyUsed: boolean;
|
25
25
|
contentType: string | null;
|
26
26
|
contentLength: number | null;
|
27
|
-
|
27
|
+
signal?: AbortSignal | null;
|
28
28
|
constructor(bodyInit: BodyPonyfillInit | null, options?: PonyfillBodyOptions);
|
29
29
|
private bodyType?;
|
30
30
|
private _bodyFactory;
|
package/typings/Request.d.cts
CHANGED
@@ -26,12 +26,12 @@ export declare class PonyfillRequest<TJSON = any> extends PonyfillBody<TJSON> im
|
|
26
26
|
referrer: string;
|
27
27
|
referrerPolicy: ReferrerPolicy;
|
28
28
|
_url: string | undefined;
|
29
|
-
get signal(): AbortSignal;
|
30
29
|
get url(): string;
|
31
30
|
_parsedUrl: URL | undefined;
|
32
31
|
get parsedUrl(): URL;
|
33
32
|
duplex: 'half' | 'full';
|
34
33
|
agent: HTTPAgent | HTTPSAgent | false | undefined;
|
34
|
+
signal: AbortSignal;
|
35
35
|
clone(): PonyfillRequest<TJSON>;
|
36
36
|
[Symbol.toStringTag]: string;
|
37
37
|
}
|
package/typings/Request.d.ts
CHANGED
@@ -26,12 +26,12 @@ export declare class PonyfillRequest<TJSON = any> extends PonyfillBody<TJSON> im
|
|
26
26
|
referrer: string;
|
27
27
|
referrerPolicy: ReferrerPolicy;
|
28
28
|
_url: string | undefined;
|
29
|
-
get signal(): AbortSignal;
|
30
29
|
get url(): string;
|
31
30
|
_parsedUrl: URL | undefined;
|
32
31
|
get parsedUrl(): URL;
|
33
32
|
duplex: 'half' | 'full';
|
34
33
|
agent: HTTPAgent | HTTPSAgent | false | undefined;
|
34
|
+
signal: AbortSignal;
|
35
35
|
clone(): PonyfillRequest<TJSON>;
|
36
36
|
[Symbol.toStringTag]: string;
|
37
37
|
}
|
package/typings/utils.d.cts
CHANGED
package/typings/utils.d.ts
CHANGED