@whatwg-node/node-fetch 0.4.12 → 0.4.13-alpha-20230821122741-21a9569

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/Blob.js CHANGED
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillBlob = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
5
5
  const ReadableStream_js_1 = require("./ReadableStream.js");
6
- const utils_js_1 = require("./utils.js");
7
6
  function getBlobPartAsBuffer(blobPart) {
8
7
  if (typeof blobPart === 'string') {
9
8
  return Buffer.from(blobPart);
@@ -48,7 +47,7 @@ class PonyfillBlob {
48
47
  return Buffer.concat(bufferChunks);
49
48
  }
50
49
  arrayBuffer() {
51
- return this.buffer().then(utils_js_1.uint8ArrayToArrayBuffer);
50
+ return this.buffer().then(buf => buf.buffer);
52
51
  }
53
52
  async text() {
54
53
  let text = '';
package/cjs/Body.js CHANGED
@@ -29,11 +29,12 @@ class PonyfillBody {
29
29
  this.contentLength = null;
30
30
  this._bodyFactory = () => null;
31
31
  this._generatedBody = null;
32
- const { bodyFactory, contentType, contentLength, bodyType } = processBodyInit(bodyInit);
32
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
33
33
  this._bodyFactory = bodyFactory;
34
34
  this.contentType = contentType;
35
35
  this.contentLength = contentLength;
36
36
  this.bodyType = bodyType;
37
+ this._buffer = buffer;
37
38
  }
38
39
  generateBody() {
39
40
  if (this._generatedBody) {
@@ -73,13 +74,8 @@ class PonyfillBody {
73
74
  if (this.bodyType === BodyInitType.ArrayBuffer) {
74
75
  return (0, utils_js_1.fakePromise)(this.bodyInit);
75
76
  }
76
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
77
- const typedBodyInit = this.bodyInit;
78
- return (0, utils_js_1.fakePromise)((0, utils_js_1.uint8ArrayToArrayBuffer)(typedBodyInit));
79
- }
80
- if (this.bodyType === BodyInitType.String) {
81
- const buffer = Buffer.from(this.bodyInit);
82
- return (0, utils_js_1.fakePromise)(buffer.buffer);
77
+ if (this._buffer) {
78
+ return (0, utils_js_1.fakePromise)(this._buffer.buffer);
83
79
  }
84
80
  if (this.bodyType === BodyInitType.Blob) {
85
81
  const blob = this.bodyInit;
@@ -111,19 +107,8 @@ class PonyfillBody {
111
107
  if (this.bodyType === BodyInitType.Blob) {
112
108
  return (0, utils_js_1.fakePromise)(this.bodyInit);
113
109
  }
114
- if (this.bodyType === BodyInitType.String ||
115
- this.bodyType === BodyInitType.Buffer ||
116
- this.bodyType === BodyInitType.Uint8Array) {
117
- const bodyInitTyped = this.bodyInit;
118
- const blob = new Blob_js_1.PonyfillBlob([bodyInitTyped], {
119
- type: this.contentType || '',
120
- });
121
- return (0, utils_js_1.fakePromise)(blob);
122
- }
123
- if (this.bodyType === BodyInitType.ArrayBuffer) {
124
- const bodyInitTyped = this.bodyInit;
125
- const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength);
126
- const blob = new Blob_js_1.PonyfillBlob([buf], {
110
+ if (this._buffer) {
111
+ const blob = new Blob_js_1.PonyfillBlob([this._buffer], {
127
112
  type: this.contentType || '',
128
113
  });
129
114
  return (0, utils_js_1.fakePromise)(blob);
@@ -199,8 +184,8 @@ class PonyfillBody {
199
184
  });
200
185
  }
201
186
  buffer() {
202
- if (this.bodyType === BodyInitType.Buffer) {
203
- return (0, utils_js_1.fakePromise)(this.bodyInit);
187
+ if (this._buffer) {
188
+ return (0, utils_js_1.fakePromise)(Buffer.from(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength));
204
189
  }
205
190
  if (this.bodyType === BodyInitType.String) {
206
191
  return (0, utils_js_1.fakePromise)(Buffer.from(this.bodyInit));
@@ -247,6 +232,7 @@ function processBodyInit(bodyInit) {
247
232
  bodyType: BodyInitType.String,
248
233
  contentType: 'text/plain;charset=UTF-8',
249
234
  contentLength,
235
+ buffer,
250
236
  bodyFactory() {
251
237
  const readable = stream_1.Readable.from(buffer);
252
238
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
@@ -259,6 +245,7 @@ function processBodyInit(bodyInit) {
259
245
  bodyType: BodyInitType.Buffer,
260
246
  contentLength,
261
247
  contentType: null,
248
+ buffer: bodyInit,
262
249
  bodyFactory() {
263
250
  const readable = stream_1.Readable.from(bodyInit);
264
251
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
@@ -290,6 +277,7 @@ function processBodyInit(bodyInit) {
290
277
  bodyType: BodyInitType.Uint8Array,
291
278
  contentLength,
292
279
  contentType: null,
280
+ buffer: bodyInit,
293
281
  bodyFactory() {
294
282
  const readable = stream_1.Readable.from(bodyInit);
295
283
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
@@ -303,7 +291,7 @@ function processBodyInit(bodyInit) {
303
291
  contentLength,
304
292
  contentType: null,
305
293
  bodyFactory() {
306
- const buffer = Buffer.from(bodyInit);
294
+ const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
307
295
  const readable = stream_1.Readable.from(buffer);
308
296
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
309
297
  return body;
@@ -312,12 +300,13 @@ function processBodyInit(bodyInit) {
312
300
  }
313
301
  if (bodyInit instanceof ArrayBuffer) {
314
302
  const contentLength = bodyInit.byteLength;
303
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
315
304
  return {
316
305
  bodyType: BodyInitType.ArrayBuffer,
317
306
  contentType: null,
318
307
  contentLength,
308
+ buffer,
319
309
  bodyFactory() {
320
- const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
321
310
  const readable = stream_1.Readable.from(buffer);
322
311
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
323
312
  return body;
package/cjs/fetchCurl.js CHANGED
@@ -2,16 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetchCurl = void 0;
4
4
  const node_stream_1 = require("node:stream");
5
+ const AbortError_js_1 = require("./AbortError.js");
5
6
  const Response_js_1 = require("./Response.js");
6
7
  const utils_js_1 = require("./utils.js");
7
8
  function fetchCurl(fetchRequest) {
8
9
  const { Curl, CurlCode, CurlFeature, CurlPause, CurlProgressFunc } = globalThis['libcurl'];
9
10
  const curlHandle = new Curl();
10
- if (fetchRequest['_signal']) {
11
- fetchRequest['_signal'].onabort = () => {
12
- curlHandle.pause(CurlPause.Recv);
13
- };
14
- }
15
11
  curlHandle.enable(CurlFeature.NoDataParsing);
16
12
  curlHandle.setOpt('URL', fetchRequest.url);
17
13
  curlHandle.setOpt('SSL_VERIFYPEER', false);
@@ -57,6 +53,18 @@ function fetchCurl(fetchRequest) {
57
53
  curlHandle.setOpt('HTTPHEADER', curlHeaders);
58
54
  curlHandle.enable(CurlFeature.NoHeaderParsing);
59
55
  return new Promise(function promiseResolver(resolve, reject) {
56
+ let streamResolved = false;
57
+ if (fetchRequest['_signal']) {
58
+ fetchRequest['_signal'].onabort = () => {
59
+ if (streamResolved) {
60
+ curlHandle.pause(CurlPause.Recv);
61
+ }
62
+ else {
63
+ reject(new AbortError_js_1.PonyfillAbortError());
64
+ curlHandle.close();
65
+ }
66
+ };
67
+ }
60
68
  curlHandle.once('end', function endListener() {
61
69
  curlHandle.close();
62
70
  });
@@ -90,6 +98,7 @@ function fetchCurl(fetchRequest) {
90
98
  headers: headersInit,
91
99
  url: fetchRequest.url,
92
100
  }));
101
+ streamResolved = true;
93
102
  });
94
103
  curlHandle.perform();
95
104
  });
package/cjs/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fakePromise = exports.defaultHeadersSerializer = exports.uint8ArrayToArrayBuffer = exports.getHeadersObj = void 0;
3
+ exports.fakePromise = exports.defaultHeadersSerializer = exports.getHeadersObj = void 0;
4
4
  function getHeadersObj(headers) {
5
5
  if (headers == null || !('forEach' in headers)) {
6
6
  return headers;
@@ -12,10 +12,6 @@ function getHeadersObj(headers) {
12
12
  return obj;
13
13
  }
14
14
  exports.getHeadersObj = getHeadersObj;
15
- function uint8ArrayToArrayBuffer(uint8array) {
16
- return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
17
- }
18
- exports.uint8ArrayToArrayBuffer = uint8ArrayToArrayBuffer;
19
15
  function defaultHeadersSerializer(headers, onContentLength) {
20
16
  const headerArray = [];
21
17
  headers.forEach((value, key) => {
package/esm/Blob.js CHANGED
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
2
2
  import { PonyfillReadableStream } from './ReadableStream.js';
3
- import { uint8ArrayToArrayBuffer } from './utils.js';
4
3
  function getBlobPartAsBuffer(blobPart) {
5
4
  if (typeof blobPart === 'string') {
6
5
  return Buffer.from(blobPart);
@@ -45,7 +44,7 @@ export class PonyfillBlob {
45
44
  return Buffer.concat(bufferChunks);
46
45
  }
47
46
  arrayBuffer() {
48
- return this.buffer().then(uint8ArrayToArrayBuffer);
47
+ return this.buffer().then(buf => buf.buffer);
49
48
  }
50
49
  async text() {
51
50
  let text = '';
package/esm/Body.js CHANGED
@@ -4,7 +4,7 @@ import { PonyfillBlob } from './Blob.js';
4
4
  import { PonyfillFile } from './File.js';
5
5
  import { getStreamFromFormData, PonyfillFormData } from './FormData.js';
6
6
  import { PonyfillReadableStream } from './ReadableStream.js';
7
- import { fakePromise, uint8ArrayToArrayBuffer } from './utils.js';
7
+ import { fakePromise } from './utils.js';
8
8
  var BodyInitType;
9
9
  (function (BodyInitType) {
10
10
  BodyInitType["ReadableStream"] = "ReadableStream";
@@ -25,11 +25,12 @@ export class PonyfillBody {
25
25
  this.contentLength = null;
26
26
  this._bodyFactory = () => null;
27
27
  this._generatedBody = null;
28
- const { bodyFactory, contentType, contentLength, bodyType } = processBodyInit(bodyInit);
28
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
29
29
  this._bodyFactory = bodyFactory;
30
30
  this.contentType = contentType;
31
31
  this.contentLength = contentLength;
32
32
  this.bodyType = bodyType;
33
+ this._buffer = buffer;
33
34
  }
34
35
  generateBody() {
35
36
  if (this._generatedBody) {
@@ -69,13 +70,8 @@ export class PonyfillBody {
69
70
  if (this.bodyType === BodyInitType.ArrayBuffer) {
70
71
  return fakePromise(this.bodyInit);
71
72
  }
72
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
73
- const typedBodyInit = this.bodyInit;
74
- return fakePromise(uint8ArrayToArrayBuffer(typedBodyInit));
75
- }
76
- if (this.bodyType === BodyInitType.String) {
77
- const buffer = Buffer.from(this.bodyInit);
78
- return fakePromise(buffer.buffer);
73
+ if (this._buffer) {
74
+ return fakePromise(this._buffer.buffer);
79
75
  }
80
76
  if (this.bodyType === BodyInitType.Blob) {
81
77
  const blob = this.bodyInit;
@@ -107,19 +103,8 @@ export class PonyfillBody {
107
103
  if (this.bodyType === BodyInitType.Blob) {
108
104
  return fakePromise(this.bodyInit);
109
105
  }
110
- if (this.bodyType === BodyInitType.String ||
111
- this.bodyType === BodyInitType.Buffer ||
112
- this.bodyType === BodyInitType.Uint8Array) {
113
- const bodyInitTyped = this.bodyInit;
114
- const blob = new PonyfillBlob([bodyInitTyped], {
115
- type: this.contentType || '',
116
- });
117
- return fakePromise(blob);
118
- }
119
- if (this.bodyType === BodyInitType.ArrayBuffer) {
120
- const bodyInitTyped = this.bodyInit;
121
- const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength);
122
- const blob = new PonyfillBlob([buf], {
106
+ if (this._buffer) {
107
+ const blob = new PonyfillBlob([this._buffer], {
123
108
  type: this.contentType || '',
124
109
  });
125
110
  return fakePromise(blob);
@@ -195,8 +180,8 @@ export class PonyfillBody {
195
180
  });
196
181
  }
197
182
  buffer() {
198
- if (this.bodyType === BodyInitType.Buffer) {
199
- return fakePromise(this.bodyInit);
183
+ if (this._buffer) {
184
+ return fakePromise(Buffer.from(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength));
200
185
  }
201
186
  if (this.bodyType === BodyInitType.String) {
202
187
  return fakePromise(Buffer.from(this.bodyInit));
@@ -242,6 +227,7 @@ function processBodyInit(bodyInit) {
242
227
  bodyType: BodyInitType.String,
243
228
  contentType: 'text/plain;charset=UTF-8',
244
229
  contentLength,
230
+ buffer,
245
231
  bodyFactory() {
246
232
  const readable = Readable.from(buffer);
247
233
  return new PonyfillReadableStream(readable);
@@ -254,6 +240,7 @@ function processBodyInit(bodyInit) {
254
240
  bodyType: BodyInitType.Buffer,
255
241
  contentLength,
256
242
  contentType: null,
243
+ buffer: bodyInit,
257
244
  bodyFactory() {
258
245
  const readable = Readable.from(bodyInit);
259
246
  const body = new PonyfillReadableStream(readable);
@@ -285,6 +272,7 @@ function processBodyInit(bodyInit) {
285
272
  bodyType: BodyInitType.Uint8Array,
286
273
  contentLength,
287
274
  contentType: null,
275
+ buffer: bodyInit,
288
276
  bodyFactory() {
289
277
  const readable = Readable.from(bodyInit);
290
278
  const body = new PonyfillReadableStream(readable);
@@ -298,7 +286,7 @@ function processBodyInit(bodyInit) {
298
286
  contentLength,
299
287
  contentType: null,
300
288
  bodyFactory() {
301
- const buffer = Buffer.from(bodyInit);
289
+ const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
302
290
  const readable = Readable.from(buffer);
303
291
  const body = new PonyfillReadableStream(readable);
304
292
  return body;
@@ -307,12 +295,13 @@ function processBodyInit(bodyInit) {
307
295
  }
308
296
  if (bodyInit instanceof ArrayBuffer) {
309
297
  const contentLength = bodyInit.byteLength;
298
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
310
299
  return {
311
300
  bodyType: BodyInitType.ArrayBuffer,
312
301
  contentType: null,
313
302
  contentLength,
303
+ buffer,
314
304
  bodyFactory() {
315
- const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
316
305
  const readable = Readable.from(buffer);
317
306
  const body = new PonyfillReadableStream(readable);
318
307
  return body;
package/esm/fetchCurl.js CHANGED
@@ -1,14 +1,10 @@
1
1
  import { Readable } from 'node:stream';
2
+ import { PonyfillAbortError } from './AbortError.js';
2
3
  import { PonyfillResponse } from './Response.js';
3
4
  import { defaultHeadersSerializer } from './utils.js';
4
5
  export function fetchCurl(fetchRequest) {
5
6
  const { Curl, CurlCode, CurlFeature, CurlPause, CurlProgressFunc } = globalThis['libcurl'];
6
7
  const curlHandle = new Curl();
7
- if (fetchRequest['_signal']) {
8
- fetchRequest['_signal'].onabort = () => {
9
- curlHandle.pause(CurlPause.Recv);
10
- };
11
- }
12
8
  curlHandle.enable(CurlFeature.NoDataParsing);
13
9
  curlHandle.setOpt('URL', fetchRequest.url);
14
10
  curlHandle.setOpt('SSL_VERIFYPEER', false);
@@ -54,6 +50,18 @@ export function fetchCurl(fetchRequest) {
54
50
  curlHandle.setOpt('HTTPHEADER', curlHeaders);
55
51
  curlHandle.enable(CurlFeature.NoHeaderParsing);
56
52
  return new Promise(function promiseResolver(resolve, reject) {
53
+ let streamResolved = false;
54
+ if (fetchRequest['_signal']) {
55
+ fetchRequest['_signal'].onabort = () => {
56
+ if (streamResolved) {
57
+ curlHandle.pause(CurlPause.Recv);
58
+ }
59
+ else {
60
+ reject(new PonyfillAbortError());
61
+ curlHandle.close();
62
+ }
63
+ };
64
+ }
57
65
  curlHandle.once('end', function endListener() {
58
66
  curlHandle.close();
59
67
  });
@@ -87,6 +95,7 @@ export function fetchCurl(fetchRequest) {
87
95
  headers: headersInit,
88
96
  url: fetchRequest.url,
89
97
  }));
98
+ streamResolved = true;
90
99
  });
91
100
  curlHandle.perform();
92
101
  });
package/esm/utils.js CHANGED
@@ -8,9 +8,6 @@ export function getHeadersObj(headers) {
8
8
  });
9
9
  return obj;
10
10
  }
11
- export function uint8ArrayToArrayBuffer(uint8array) {
12
- return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
13
- }
14
11
  export function defaultHeadersSerializer(headers, onContentLength) {
15
12
  const headerArray = [];
16
13
  headers.forEach((value, key) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.4.12",
3
+ "version": "0.4.13-alpha-20230821122741-21a9569",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -17,7 +17,7 @@ export declare class PonyfillBlob implements Blob {
17
17
  private encoding;
18
18
  constructor(blobParts: BlobPart[], options?: BlobOptions);
19
19
  buffer(): Promise<Buffer>;
20
- arrayBuffer(): Promise<ArrayBuffer>;
20
+ arrayBuffer(): Promise<ArrayBuffer | SharedArrayBuffer>;
21
21
  text(): Promise<string>;
22
22
  get size(): number;
23
23
  stream(): any;
package/typings/Blob.d.ts CHANGED
@@ -17,7 +17,7 @@ export declare class PonyfillBlob implements Blob {
17
17
  private encoding;
18
18
  constructor(blobParts: BlobPart[], options?: BlobOptions);
19
19
  buffer(): Promise<Buffer>;
20
- arrayBuffer(): Promise<ArrayBuffer>;
20
+ arrayBuffer(): Promise<ArrayBuffer | SharedArrayBuffer>;
21
21
  text(): Promise<string>;
22
22
  get size(): number;
23
23
  stream(): any;
@@ -27,6 +27,7 @@ export declare class PonyfillBody<TJSON = any> implements Body {
27
27
  private bodyType?;
28
28
  private _bodyFactory;
29
29
  private _generatedBody;
30
+ private _buffer?;
30
31
  private generateBody;
31
32
  get body(): PonyfillReadableStream<Uint8Array> | null;
32
33
  arrayBuffer(): Promise<ArrayBuffer>;
package/typings/Body.d.ts CHANGED
@@ -27,6 +27,7 @@ export declare class PonyfillBody<TJSON = any> implements Body {
27
27
  private bodyType?;
28
28
  private _bodyFactory;
29
29
  private _generatedBody;
30
+ private _buffer?;
30
31
  private generateBody;
31
32
  get body(): PonyfillReadableStream<Uint8Array> | null;
32
33
  arrayBuffer(): Promise<ArrayBuffer>;
@@ -1,4 +1,3 @@
1
1
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
2
- export declare function uint8ArrayToArrayBuffer(uint8array: Uint8Array): ArrayBuffer;
3
2
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
4
3
  export declare function fakePromise<T>(value: T): Promise<T>;
@@ -1,4 +1,3 @@
1
1
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
2
- export declare function uint8ArrayToArrayBuffer(uint8array: Uint8Array): ArrayBuffer;
3
2
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
4
3
  export declare function fakePromise<T>(value: T): Promise<T>;