@whatwg-node/node-fetch 0.4.0-alpha-20230515125426-a472464 → 0.4.0-alpha-20230515132257-4514d91
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/AbortError.js +19 -0
- package/cjs/fetch.js +33 -21
- package/esm/AbortError.js +15 -0
- package/esm/fetch.js +33 -21
- package/package.json +2 -2
- package/typings/AbortError.d.cts +4 -0
- package/typings/AbortError.d.ts +4 -0
@@ -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:':
|
@@ -34,29 +55,11 @@ function fetchPonyfill(info, init) {
|
|
34
55
|
}
|
35
56
|
const fetchRequest = info;
|
36
57
|
return new Promise((resolve, reject) => {
|
58
|
+
var _a;
|
37
59
|
try {
|
38
60
|
const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
|
39
61
|
if (url.protocol === 'data:') {
|
40
|
-
const
|
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
|
-
});
|
62
|
+
const response = getResponseForDataUri(url);
|
60
63
|
resolve(response);
|
61
64
|
return;
|
62
65
|
}
|
@@ -74,11 +77,20 @@ function fetchPonyfill(info, init) {
|
|
74
77
|
const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
|
75
78
|
const nodeHeaders = headersSerializer(fetchRequest.headers);
|
76
79
|
const nodeRequest = requestFn(fetchRequest.url, {
|
77
|
-
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
78
80
|
method: fetchRequest.method,
|
79
81
|
headers: nodeHeaders,
|
80
82
|
signal: fetchRequest.signal,
|
81
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
|
+
});
|
82
94
|
nodeRequest.once('response', nodeResponse => {
|
83
95
|
let responseBody = nodeResponse;
|
84
96
|
const contentEncoding = nodeResponse.headers['content-encoding'];
|
@@ -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:':
|
@@ -31,29 +52,11 @@ export function fetchPonyfill(info, init) {
|
|
31
52
|
}
|
32
53
|
const fetchRequest = info;
|
33
54
|
return new Promise((resolve, reject) => {
|
55
|
+
var _a;
|
34
56
|
try {
|
35
57
|
const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
|
36
58
|
if (url.protocol === 'data:') {
|
37
|
-
const
|
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
|
-
});
|
59
|
+
const response = getResponseForDataUri(url);
|
57
60
|
resolve(response);
|
58
61
|
return;
|
59
62
|
}
|
@@ -71,11 +74,20 @@ export function fetchPonyfill(info, init) {
|
|
71
74
|
const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
|
72
75
|
const nodeHeaders = headersSerializer(fetchRequest.headers);
|
73
76
|
const nodeRequest = requestFn(fetchRequest.url, {
|
74
|
-
// signal: fetchRequest.signal will be added when v14 reaches EOL
|
75
77
|
method: fetchRequest.method,
|
76
78
|
headers: nodeHeaders,
|
77
79
|
signal: fetchRequest.signal,
|
78
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
|
+
});
|
79
91
|
nodeRequest.once('response', nodeResponse => {
|
80
92
|
let responseBody = nodeResponse;
|
81
93
|
const contentEncoding = nodeResponse.headers['content-encoding'];
|
package/package.json
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "@whatwg-node/node-fetch",
|
3
|
-
"version": "0.4.0-alpha-
|
3
|
+
"version": "0.4.0-alpha-20230515132257-4514d91",
|
4
4
|
"description": "Fetch API implementation for Node",
|
5
5
|
"sideEffects": false,
|
6
6
|
"dependencies": {
|
7
|
-
"@whatwg-node/events": "0.1.0-alpha-
|
7
|
+
"@whatwg-node/events": "0.1.0-alpha-20230515132257-4514d91",
|
8
8
|
"busboy": "^1.6.0",
|
9
9
|
"fast-querystring": "^1.1.1",
|
10
10
|
"fast-url-parser": "^1.1.3",
|