@whatwg-node/node-fetch 0.4.0-alpha-20230515124654-683122c → 0.4.0-alpha-20230515131253-e1eec20

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.
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillAbortError = void 0;
4
+ class PonyfillAbortError extends Error {
5
+ constructor(reason) {
6
+ let message = 'The operation was aborted';
7
+ if (reason) {
8
+ message += ` reason: ${reason}`;
9
+ }
10
+ super(message, {
11
+ cause: reason,
12
+ });
13
+ this.name = 'AbortError';
14
+ }
15
+ get reason() {
16
+ return this.cause;
17
+ }
18
+ }
19
+ exports.PonyfillAbortError = PonyfillAbortError;
package/cjs/fetch.js CHANGED
@@ -7,6 +7,7 @@ const https_1 = require("https");
7
7
  const stream_1 = require("stream");
8
8
  const url_1 = require("url");
9
9
  const zlib_1 = require("zlib");
10
+ const AbortError_js_1 = require("./AbortError.js");
10
11
  const Blob_js_1 = require("./Blob.js");
11
12
  const Request_js_1 = require("./Request.js");
12
13
  const Response_js_1 = require("./Response.js");
@@ -17,6 +18,26 @@ function getResponseForFile(url) {
17
18
  const readable = (0, fs_1.createReadStream)(path);
18
19
  return new Response_js_1.PonyfillResponse(readable);
19
20
  }
21
+ function getResponseForDataUri(url) {
22
+ const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
23
+ const data = decodeURIComponent(datas.join(','));
24
+ if (mimeType.endsWith(BASE64_SUFFIX)) {
25
+ const buffer = Buffer.from(data, 'base64url');
26
+ const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
27
+ const file = new Blob_js_1.PonyfillBlob([buffer], { type: realMimeType });
28
+ return new Response_js_1.PonyfillResponse(file, {
29
+ status: 200,
30
+ statusText: 'OK',
31
+ });
32
+ }
33
+ return new Response_js_1.PonyfillResponse(data, {
34
+ status: 200,
35
+ statusText: 'OK',
36
+ headers: {
37
+ 'content-type': mimeType,
38
+ },
39
+ });
40
+ }
20
41
  function getRequestFnForProtocol(protocol) {
21
42
  switch (protocol) {
22
43
  case 'http:':
@@ -37,26 +58,7 @@ function fetchPonyfill(info, init) {
37
58
  try {
38
59
  const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
39
60
  if (url.protocol === 'data:') {
40
- const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
41
- const data = decodeURIComponent(datas.join(','));
42
- if (mimeType.endsWith(BASE64_SUFFIX)) {
43
- const buffer = Buffer.from(data, 'base64url');
44
- const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
45
- const file = new Blob_js_1.PonyfillBlob([buffer], { type: realMimeType });
46
- const response = new Response_js_1.PonyfillResponse(file, {
47
- status: 200,
48
- statusText: 'OK',
49
- });
50
- resolve(response);
51
- return;
52
- }
53
- const response = new Response_js_1.PonyfillResponse(data, {
54
- status: 200,
55
- statusText: 'OK',
56
- headers: {
57
- 'content-type': mimeType,
58
- },
59
- });
61
+ const response = getResponseForDataUri(url);
60
62
  resolve(response);
61
63
  return;
62
64
  }
@@ -74,7 +76,6 @@ function fetchPonyfill(info, init) {
74
76
  const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
75
77
  const nodeHeaders = headersSerializer(fetchRequest.headers);
76
78
  const nodeRequest = requestFn(fetchRequest.url, {
77
- // signal: fetchRequest.signal will be added when v14 reaches EOL
78
79
  method: fetchRequest.method,
79
80
  headers: nodeHeaders,
80
81
  signal: fetchRequest.signal,
@@ -122,6 +123,10 @@ function fetchPonyfill(info, init) {
122
123
  });
123
124
  resolve(ponyfillResponse);
124
125
  });
126
+ // TODO: will be removed after v16 reaches EOL
127
+ nodeRequest.once('abort', (reason) => {
128
+ reject(new AbortError_js_1.PonyfillAbortError(reason));
129
+ });
125
130
  nodeRequest.once('error', reject);
126
131
  if (nodeReadable) {
127
132
  nodeReadable.pipe(nodeRequest);
@@ -0,0 +1,15 @@
1
+ export class PonyfillAbortError extends Error {
2
+ constructor(reason) {
3
+ let message = 'The operation was aborted';
4
+ if (reason) {
5
+ message += ` reason: ${reason}`;
6
+ }
7
+ super(message, {
8
+ cause: reason,
9
+ });
10
+ this.name = 'AbortError';
11
+ }
12
+ get reason() {
13
+ return this.cause;
14
+ }
15
+ }
package/esm/fetch.js CHANGED
@@ -4,6 +4,7 @@ import { request as httpsRequest } from 'https';
4
4
  import { Readable } from 'stream';
5
5
  import { fileURLToPath } from 'url';
6
6
  import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
7
+ import { PonyfillAbortError } from './AbortError.js';
7
8
  import { PonyfillBlob } from './Blob.js';
8
9
  import { PonyfillRequest } from './Request.js';
9
10
  import { PonyfillResponse } from './Response.js';
@@ -14,6 +15,26 @@ function getResponseForFile(url) {
14
15
  const readable = createReadStream(path);
15
16
  return new PonyfillResponse(readable);
16
17
  }
18
+ function getResponseForDataUri(url) {
19
+ const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
20
+ const data = decodeURIComponent(datas.join(','));
21
+ if (mimeType.endsWith(BASE64_SUFFIX)) {
22
+ const buffer = Buffer.from(data, 'base64url');
23
+ const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
24
+ const file = new PonyfillBlob([buffer], { type: realMimeType });
25
+ return new PonyfillResponse(file, {
26
+ status: 200,
27
+ statusText: 'OK',
28
+ });
29
+ }
30
+ return new PonyfillResponse(data, {
31
+ status: 200,
32
+ statusText: 'OK',
33
+ headers: {
34
+ 'content-type': mimeType,
35
+ },
36
+ });
37
+ }
17
38
  function getRequestFnForProtocol(protocol) {
18
39
  switch (protocol) {
19
40
  case 'http:':
@@ -34,26 +55,7 @@ export function fetchPonyfill(info, init) {
34
55
  try {
35
56
  const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
36
57
  if (url.protocol === 'data:') {
37
- const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
38
- const data = decodeURIComponent(datas.join(','));
39
- if (mimeType.endsWith(BASE64_SUFFIX)) {
40
- const buffer = Buffer.from(data, 'base64url');
41
- const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
42
- const file = new PonyfillBlob([buffer], { type: realMimeType });
43
- const response = new PonyfillResponse(file, {
44
- status: 200,
45
- statusText: 'OK',
46
- });
47
- resolve(response);
48
- return;
49
- }
50
- const response = new PonyfillResponse(data, {
51
- status: 200,
52
- statusText: 'OK',
53
- headers: {
54
- 'content-type': mimeType,
55
- },
56
- });
58
+ const response = getResponseForDataUri(url);
57
59
  resolve(response);
58
60
  return;
59
61
  }
@@ -71,7 +73,6 @@ export function fetchPonyfill(info, init) {
71
73
  const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
72
74
  const nodeHeaders = headersSerializer(fetchRequest.headers);
73
75
  const nodeRequest = requestFn(fetchRequest.url, {
74
- // signal: fetchRequest.signal will be added when v14 reaches EOL
75
76
  method: fetchRequest.method,
76
77
  headers: nodeHeaders,
77
78
  signal: fetchRequest.signal,
@@ -119,6 +120,10 @@ export function fetchPonyfill(info, init) {
119
120
  });
120
121
  resolve(ponyfillResponse);
121
122
  });
123
+ // TODO: will be removed after v16 reaches EOL
124
+ nodeRequest.once('abort', (reason) => {
125
+ reject(new PonyfillAbortError(reason));
126
+ });
122
127
  nodeRequest.once('error', reject);
123
128
  if (nodeReadable) {
124
129
  nodeReadable.pipe(nodeRequest);
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.4.0-alpha-20230515124654-683122c",
3
+ "version": "0.4.0-alpha-20230515131253-e1eec20",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
7
- "@whatwg-node/events": "0.1.0-alpha-20230515124654-683122c",
7
+ "@whatwg-node/events": "0.1.0-alpha-20230515131253-e1eec20",
8
8
  "busboy": "^1.6.0",
9
9
  "fast-querystring": "^1.1.1",
10
10
  "fast-url-parser": "^1.1.3",
@@ -0,0 +1,4 @@
1
+ export declare class PonyfillAbortError extends Error {
2
+ constructor(reason?: any);
3
+ get reason(): unknown;
4
+ }
@@ -0,0 +1,4 @@
1
+ export declare class PonyfillAbortError extends Error {
2
+ constructor(reason?: any);
3
+ get reason(): unknown;
4
+ }