@whatwg-node/node-fetch 0.4.0 → 0.5.0-alpha-20230522130438-430d828

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
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ var _a;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.PonyfillBlob = void 0;
4
5
  const ReadableStream_js_1 = require("./ReadableStream.js");
@@ -28,6 +29,7 @@ function isBlob(obj) {
28
29
  class PonyfillBlob {
29
30
  constructor(blobParts, options) {
30
31
  this.blobParts = blobParts;
32
+ this[_a] = 'Blob';
31
33
  this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream';
32
34
  this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8';
33
35
  }
@@ -117,3 +119,4 @@ class PonyfillBlob {
117
119
  }
118
120
  }
119
121
  exports.PonyfillBlob = PonyfillBlob;
122
+ _a = Symbol.toStringTag;
package/cjs/Body.js CHANGED
@@ -324,17 +324,6 @@ function processBodyInit(bodyInit) {
324
324
  },
325
325
  };
326
326
  }
327
- if ('stream' in bodyInit) {
328
- return {
329
- contentType: bodyInit.type,
330
- contentLength: bodyInit.size,
331
- bodyFactory() {
332
- const bodyStream = bodyInit.stream();
333
- const body = new ReadableStream_js_1.PonyfillReadableStream(bodyStream);
334
- return body;
335
- },
336
- };
337
- }
338
327
  if ('sort' in bodyInit) {
339
328
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
340
329
  return {
@@ -358,6 +347,17 @@ function processBodyInit(bodyInit) {
358
347
  },
359
348
  };
360
349
  }
350
+ if ('stream' in bodyInit) {
351
+ return {
352
+ contentType: bodyInit.type,
353
+ contentLength: bodyInit.size,
354
+ bodyFactory() {
355
+ const bodyStream = bodyInit.stream();
356
+ const body = new ReadableStream_js_1.PonyfillReadableStream(bodyStream);
357
+ return body;
358
+ },
359
+ };
360
+ }
361
361
  if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
362
362
  return {
363
363
  contentType: null,
package/cjs/FormData.js CHANGED
@@ -6,6 +6,9 @@ const ReadableStream_js_1 = require("./ReadableStream.js");
6
6
  class PonyfillFormData {
7
7
  constructor() {
8
8
  this.map = new Map();
9
+ Object.defineProperty(this.constructor, 'name', {
10
+ value: 'FormData',
11
+ });
9
12
  }
10
13
  append(name, value, fileName) {
11
14
  let values = this.map.get(name);
@@ -62,6 +65,10 @@ class PonyfillFormData {
62
65
  callback(value, key, this);
63
66
  }
64
67
  }
68
+ // undici compat
69
+ stream() {
70
+ return getStreamFromFormData(this);
71
+ }
65
72
  }
66
73
  exports.PonyfillFormData = PonyfillFormData;
67
74
  function getStreamFromFormData(formData, boundary = '---') {
package/cjs/fetch.js CHANGED
@@ -2,12 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetchPonyfill = void 0;
4
4
  const fs_1 = require("fs");
5
- const http_1 = require("http");
6
- const https_1 = require("https");
7
5
  const stream_1 = require("stream");
8
6
  const url_1 = require("url");
9
7
  const zlib_1 = require("zlib");
10
- const AbortError_js_1 = require("./AbortError.js");
8
+ const undici_1 = require("undici");
11
9
  const Blob_js_1 = require("./Blob.js");
12
10
  const Request_js_1 = require("./Request.js");
13
11
  const Response_js_1 = require("./Response.js");
@@ -38,113 +36,62 @@ function getResponseForDataUri(url) {
38
36
  },
39
37
  });
40
38
  }
41
- function getRequestFnForProtocol(protocol) {
42
- switch (protocol) {
43
- case 'http:':
44
- return http_1.request;
45
- case 'https:':
46
- return https_1.request;
47
- }
48
- throw new Error(`Unsupported protocol: ${protocol}`);
49
- }
50
39
  const BASE64_SUFFIX = ';base64';
51
- function fetchPonyfill(info, init) {
40
+ async function fetchPonyfill(info, init) {
52
41
  if (typeof info === 'string' || 'href' in info) {
53
42
  const ponyfillRequest = new Request_js_1.PonyfillRequest(info, init);
54
43
  return fetchPonyfill(ponyfillRequest);
55
44
  }
56
45
  const fetchRequest = info;
57
- return new Promise((resolve, reject) => {
58
- var _a;
59
- try {
60
- const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
61
- if (url.protocol === 'data:') {
62
- const response = getResponseForDataUri(url);
63
- resolve(response);
64
- return;
65
- }
66
- if (url.protocol === 'file:') {
67
- const response = getResponseForFile(fetchRequest.url);
68
- resolve(response);
69
- return;
70
- }
71
- const requestFn = getRequestFnForProtocol(url.protocol);
72
- const nodeReadable = (fetchRequest.body != null
73
- ? 'pipe' in fetchRequest.body
74
- ? fetchRequest.body
75
- : stream_1.Readable.from(fetchRequest.body)
76
- : null);
77
- const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
78
- const nodeHeaders = headersSerializer(fetchRequest.headers);
79
- const nodeRequest = requestFn(fetchRequest.url, {
80
- method: fetchRequest.method,
81
- headers: nodeHeaders,
82
- signal: fetchRequest.signal,
83
- });
84
- // TODO: will be removed after v16 reaches EOL
85
- (_a = fetchRequest.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', () => {
86
- if (!nodeRequest.aborted) {
87
- nodeRequest.abort();
88
- }
89
- });
90
- // TODO: will be removed after v16 reaches EOL
91
- nodeRequest.once('abort', (reason) => {
92
- reject(new AbortError_js_1.PonyfillAbortError(reason));
93
- });
94
- nodeRequest.once('response', nodeResponse => {
95
- let responseBody = nodeResponse;
96
- const contentEncoding = nodeResponse.headers['content-encoding'];
97
- switch (contentEncoding) {
98
- case 'x-gzip':
99
- case 'gzip':
100
- responseBody = nodeResponse.pipe((0, zlib_1.createGunzip)());
101
- break;
102
- case 'x-deflate':
103
- case 'deflate':
104
- responseBody = nodeResponse.pipe((0, zlib_1.createInflate)());
105
- break;
106
- case 'br':
107
- responseBody = nodeResponse.pipe((0, zlib_1.createBrotliDecompress)());
108
- break;
109
- }
110
- if (nodeResponse.headers.location) {
111
- if (fetchRequest.redirect === 'error') {
112
- const redirectError = new Error('Redirects are not allowed');
113
- reject(redirectError);
114
- nodeResponse.resume();
115
- return;
116
- }
117
- if (fetchRequest.redirect === 'follow') {
118
- const redirectedUrl = new URL_js_1.PonyfillURL(nodeResponse.headers.location, url);
119
- const redirectResponse$ = fetchPonyfill(redirectedUrl, info);
120
- resolve(redirectResponse$.then(redirectResponse => {
121
- redirectResponse.redirected = true;
122
- return redirectResponse;
123
- }));
124
- nodeResponse.resume();
125
- return;
126
- }
127
- }
128
- const responseHeaders = nodeResponse.headers;
129
- const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
130
- status: nodeResponse.statusCode,
131
- statusText: nodeResponse.statusMessage,
132
- headers: responseHeaders,
133
- url: info.url,
134
- });
135
- resolve(ponyfillResponse);
136
- });
137
- nodeRequest.once('error', reject);
138
- if (nodeReadable) {
139
- nodeReadable.pipe(nodeRequest);
140
- }
141
- else {
142
- nodeRequest.end();
143
- }
144
- }
145
- catch (e) {
146
- reject(e);
147
- }
46
+ const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
47
+ if (url.protocol === 'data:') {
48
+ const response = getResponseForDataUri(url);
49
+ return response;
50
+ }
51
+ if (url.protocol === 'file:') {
52
+ const response = getResponseForFile(fetchRequest.url);
53
+ return response;
54
+ }
55
+ const requestBody = (fetchRequest['bodyInit'] != null
56
+ ? fetchRequest['bodyInit'] :
57
+ fetchRequest.body != null
58
+ ? 'pipe' in fetchRequest.body
59
+ ? fetchRequest.body
60
+ : stream_1.Readable.from(fetchRequest.body)
61
+ : null);
62
+ const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
63
+ const nodeHeaders = headersSerializer(fetchRequest.headers);
64
+ const undiciData = await (0, undici_1.request)(fetchRequest.url, {
65
+ method: fetchRequest.method,
66
+ headers: nodeHeaders,
67
+ body: requestBody,
68
+ signal: fetchRequest.signal,
69
+ maxRedirections: fetchRequest.redirect === 'follow' ? 20 : 0,
70
+ });
71
+ if (fetchRequest.redirect === 'error' && undiciData.headers.location) {
72
+ const redirectError = new Error('Redirects are not allowed');
73
+ throw redirectError;
74
+ }
75
+ let responseBody = undiciData.body;
76
+ const contentEncoding = undiciData.headers['content-encoding'];
77
+ switch (contentEncoding) {
78
+ case 'x-gzip':
79
+ case 'gzip':
80
+ responseBody = responseBody.pipe((0, zlib_1.createGunzip)());
81
+ break;
82
+ case 'x-deflate':
83
+ case 'deflate':
84
+ responseBody = responseBody.pipe((0, zlib_1.createInflate)());
85
+ break;
86
+ case 'br':
87
+ responseBody = responseBody.pipe((0, zlib_1.createBrotliDecompress)());
88
+ break;
89
+ }
90
+ const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
91
+ status: undiciData.statusCode,
92
+ headers: undiciData.headers,
93
+ url: fetchRequest.url,
148
94
  });
95
+ return ponyfillResponse;
149
96
  }
150
97
  exports.fetchPonyfill = fetchPonyfill;
package/esm/Blob.js CHANGED
@@ -1,3 +1,4 @@
1
+ var _a;
1
2
  import { PonyfillReadableStream } from './ReadableStream.js';
2
3
  import { uint8ArrayToArrayBuffer } from './utils.js';
3
4
  function getBlobPartAsBuffer(blobPart) {
@@ -25,6 +26,7 @@ function isBlob(obj) {
25
26
  export class PonyfillBlob {
26
27
  constructor(blobParts, options) {
27
28
  this.blobParts = blobParts;
29
+ this[_a] = 'Blob';
28
30
  this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream';
29
31
  this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8';
30
32
  }
@@ -113,3 +115,4 @@ export class PonyfillBlob {
113
115
  throw new Error('Not implemented');
114
116
  }
115
117
  }
118
+ _a = Symbol.toStringTag;
package/esm/Body.js CHANGED
@@ -319,17 +319,6 @@ function processBodyInit(bodyInit) {
319
319
  },
320
320
  };
321
321
  }
322
- if ('stream' in bodyInit) {
323
- return {
324
- contentType: bodyInit.type,
325
- contentLength: bodyInit.size,
326
- bodyFactory() {
327
- const bodyStream = bodyInit.stream();
328
- const body = new PonyfillReadableStream(bodyStream);
329
- return body;
330
- },
331
- };
332
- }
333
322
  if ('sort' in bodyInit) {
334
323
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
335
324
  return {
@@ -353,6 +342,17 @@ function processBodyInit(bodyInit) {
353
342
  },
354
343
  };
355
344
  }
345
+ if ('stream' in bodyInit) {
346
+ return {
347
+ contentType: bodyInit.type,
348
+ contentLength: bodyInit.size,
349
+ bodyFactory() {
350
+ const bodyStream = bodyInit.stream();
351
+ const body = new PonyfillReadableStream(bodyStream);
352
+ return body;
353
+ },
354
+ };
355
+ }
356
356
  if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
357
357
  return {
358
358
  contentType: null,
package/esm/FormData.js CHANGED
@@ -3,6 +3,9 @@ import { PonyfillReadableStream } from './ReadableStream.js';
3
3
  export class PonyfillFormData {
4
4
  constructor() {
5
5
  this.map = new Map();
6
+ Object.defineProperty(this.constructor, 'name', {
7
+ value: 'FormData',
8
+ });
6
9
  }
7
10
  append(name, value, fileName) {
8
11
  let values = this.map.get(name);
@@ -59,6 +62,10 @@ export class PonyfillFormData {
59
62
  callback(value, key, this);
60
63
  }
61
64
  }
65
+ // undici compat
66
+ stream() {
67
+ return getStreamFromFormData(this);
68
+ }
62
69
  }
63
70
  export function getStreamFromFormData(formData, boundary = '---') {
64
71
  const entries = [];
package/esm/fetch.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import { createReadStream } from 'fs';
2
- import { request as httpRequest } from 'http';
3
- import { request as httpsRequest } from 'https';
4
2
  import { Readable } from 'stream';
5
3
  import { fileURLToPath } from 'url';
6
4
  import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
7
- import { PonyfillAbortError } from './AbortError.js';
5
+ import { request } from 'undici';
8
6
  import { PonyfillBlob } from './Blob.js';
9
7
  import { PonyfillRequest } from './Request.js';
10
8
  import { PonyfillResponse } from './Response.js';
@@ -35,112 +33,61 @@ function getResponseForDataUri(url) {
35
33
  },
36
34
  });
37
35
  }
38
- function getRequestFnForProtocol(protocol) {
39
- switch (protocol) {
40
- case 'http:':
41
- return httpRequest;
42
- case 'https:':
43
- return httpsRequest;
44
- }
45
- throw new Error(`Unsupported protocol: ${protocol}`);
46
- }
47
36
  const BASE64_SUFFIX = ';base64';
48
- export function fetchPonyfill(info, init) {
37
+ export async function fetchPonyfill(info, init) {
49
38
  if (typeof info === 'string' || 'href' in info) {
50
39
  const ponyfillRequest = new PonyfillRequest(info, init);
51
40
  return fetchPonyfill(ponyfillRequest);
52
41
  }
53
42
  const fetchRequest = info;
54
- return new Promise((resolve, reject) => {
55
- var _a;
56
- try {
57
- const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
58
- if (url.protocol === 'data:') {
59
- const response = getResponseForDataUri(url);
60
- resolve(response);
61
- return;
62
- }
63
- if (url.protocol === 'file:') {
64
- const response = getResponseForFile(fetchRequest.url);
65
- resolve(response);
66
- return;
67
- }
68
- const requestFn = getRequestFnForProtocol(url.protocol);
69
- const nodeReadable = (fetchRequest.body != null
70
- ? 'pipe' in fetchRequest.body
71
- ? fetchRequest.body
72
- : Readable.from(fetchRequest.body)
73
- : null);
74
- const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
75
- const nodeHeaders = headersSerializer(fetchRequest.headers);
76
- const nodeRequest = requestFn(fetchRequest.url, {
77
- method: fetchRequest.method,
78
- headers: nodeHeaders,
79
- signal: fetchRequest.signal,
80
- });
81
- // TODO: will be removed after v16 reaches EOL
82
- (_a = fetchRequest.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', () => {
83
- if (!nodeRequest.aborted) {
84
- nodeRequest.abort();
85
- }
86
- });
87
- // TODO: will be removed after v16 reaches EOL
88
- nodeRequest.once('abort', (reason) => {
89
- reject(new PonyfillAbortError(reason));
90
- });
91
- nodeRequest.once('response', nodeResponse => {
92
- let responseBody = nodeResponse;
93
- const contentEncoding = nodeResponse.headers['content-encoding'];
94
- switch (contentEncoding) {
95
- case 'x-gzip':
96
- case 'gzip':
97
- responseBody = nodeResponse.pipe(createGunzip());
98
- break;
99
- case 'x-deflate':
100
- case 'deflate':
101
- responseBody = nodeResponse.pipe(createInflate());
102
- break;
103
- case 'br':
104
- responseBody = nodeResponse.pipe(createBrotliDecompress());
105
- break;
106
- }
107
- if (nodeResponse.headers.location) {
108
- if (fetchRequest.redirect === 'error') {
109
- const redirectError = new Error('Redirects are not allowed');
110
- reject(redirectError);
111
- nodeResponse.resume();
112
- return;
113
- }
114
- if (fetchRequest.redirect === 'follow') {
115
- const redirectedUrl = new PonyfillURL(nodeResponse.headers.location, url);
116
- const redirectResponse$ = fetchPonyfill(redirectedUrl, info);
117
- resolve(redirectResponse$.then(redirectResponse => {
118
- redirectResponse.redirected = true;
119
- return redirectResponse;
120
- }));
121
- nodeResponse.resume();
122
- return;
123
- }
124
- }
125
- const responseHeaders = nodeResponse.headers;
126
- const ponyfillResponse = new PonyfillResponse(responseBody, {
127
- status: nodeResponse.statusCode,
128
- statusText: nodeResponse.statusMessage,
129
- headers: responseHeaders,
130
- url: info.url,
131
- });
132
- resolve(ponyfillResponse);
133
- });
134
- nodeRequest.once('error', reject);
135
- if (nodeReadable) {
136
- nodeReadable.pipe(nodeRequest);
137
- }
138
- else {
139
- nodeRequest.end();
140
- }
141
- }
142
- catch (e) {
143
- reject(e);
144
- }
43
+ const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
44
+ if (url.protocol === 'data:') {
45
+ const response = getResponseForDataUri(url);
46
+ return response;
47
+ }
48
+ if (url.protocol === 'file:') {
49
+ const response = getResponseForFile(fetchRequest.url);
50
+ return response;
51
+ }
52
+ const requestBody = (fetchRequest['bodyInit'] != null
53
+ ? fetchRequest['bodyInit'] :
54
+ fetchRequest.body != null
55
+ ? 'pipe' in fetchRequest.body
56
+ ? fetchRequest.body
57
+ : Readable.from(fetchRequest.body)
58
+ : null);
59
+ const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
60
+ const nodeHeaders = headersSerializer(fetchRequest.headers);
61
+ const undiciData = await request(fetchRequest.url, {
62
+ method: fetchRequest.method,
63
+ headers: nodeHeaders,
64
+ body: requestBody,
65
+ signal: fetchRequest.signal,
66
+ maxRedirections: fetchRequest.redirect === 'follow' ? 20 : 0,
67
+ });
68
+ if (fetchRequest.redirect === 'error' && undiciData.headers.location) {
69
+ const redirectError = new Error('Redirects are not allowed');
70
+ throw redirectError;
71
+ }
72
+ let responseBody = undiciData.body;
73
+ const contentEncoding = undiciData.headers['content-encoding'];
74
+ switch (contentEncoding) {
75
+ case 'x-gzip':
76
+ case 'gzip':
77
+ responseBody = responseBody.pipe(createGunzip());
78
+ break;
79
+ case 'x-deflate':
80
+ case 'deflate':
81
+ responseBody = responseBody.pipe(createInflate());
82
+ break;
83
+ case 'br':
84
+ responseBody = responseBody.pipe(createBrotliDecompress());
85
+ break;
86
+ }
87
+ const ponyfillResponse = new PonyfillResponse(responseBody, {
88
+ status: undiciData.statusCode,
89
+ headers: undiciData.headers,
90
+ url: fetchRequest.url,
145
91
  });
92
+ return ponyfillResponse;
146
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.4.0",
3
+ "version": "0.5.0-alpha-20230522130438-430d828",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -22,6 +22,7 @@ export declare class PonyfillBlob implements Blob {
22
22
  get size(): number;
23
23
  stream(): any;
24
24
  slice(): any;
25
+ [Symbol.toStringTag]: string;
25
26
  }
26
27
  export interface PonyfillBlob {
27
28
  prototype: Blob;
package/typings/Blob.d.ts CHANGED
@@ -22,6 +22,7 @@ export declare class PonyfillBlob implements Blob {
22
22
  get size(): number;
23
23
  stream(): any;
24
24
  slice(): any;
25
+ [Symbol.toStringTag]: string;
25
26
  }
26
27
  export interface PonyfillBlob {
27
28
  prototype: Blob;
@@ -2,6 +2,7 @@ import { PonyfillBlob } from './Blob.cjs';
2
2
  import { PonyfillReadableStream } from './ReadableStream.cjs';
3
3
  export declare class PonyfillFormData implements FormData {
4
4
  private map;
5
+ constructor();
5
6
  append(name: string, value: PonyfillBlob | string, fileName?: string): void;
6
7
  delete(name: string): void;
7
8
  get(name: string): FormDataEntryValue | null;
@@ -13,5 +14,6 @@ export declare class PonyfillFormData implements FormData {
13
14
  keys(): IterableIterator<string>;
14
15
  values(): IterableIterator<FormDataEntryValue>;
15
16
  forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void;
17
+ stream(): PonyfillReadableStream<Uint8Array>;
16
18
  }
17
19
  export declare function getStreamFromFormData(formData: FormData, boundary?: string): PonyfillReadableStream<Uint8Array>;
@@ -2,6 +2,7 @@ import { PonyfillBlob } from './Blob.js';
2
2
  import { PonyfillReadableStream } from './ReadableStream.js';
3
3
  export declare class PonyfillFormData implements FormData {
4
4
  private map;
5
+ constructor();
5
6
  append(name: string, value: PonyfillBlob | string, fileName?: string): void;
6
7
  delete(name: string): void;
7
8
  get(name: string): FormDataEntryValue | null;
@@ -13,5 +14,6 @@ export declare class PonyfillFormData implements FormData {
13
14
  keys(): IterableIterator<string>;
14
15
  values(): IterableIterator<FormDataEntryValue>;
15
16
  forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void;
17
+ stream(): PonyfillReadableStream<Uint8Array>;
16
18
  }
17
19
  export declare function getStreamFromFormData(formData: FormData, boundary?: string): PonyfillReadableStream<Uint8Array>;