@whatwg-node/node-fetch 0.0.1-alpha-20230117095738-14df726 → 0.0.1-alpha-20230117102108-86636f9
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/index.js +26 -25
- package/index.mjs +27 -26
- package/package.json +1 -1
package/index.js
CHANGED
@@ -16,7 +16,11 @@ const fs = require('fs');
|
|
16
16
|
// Will be removed after v14 reaches EOL
|
17
17
|
class PonyfillAbortError extends Error {
|
18
18
|
constructor(reason) {
|
19
|
-
|
19
|
+
let message = 'The operation was aborted.';
|
20
|
+
if (reason) {
|
21
|
+
message += ` reason: ${reason}`;
|
22
|
+
}
|
23
|
+
super(message, {
|
20
24
|
cause: reason,
|
21
25
|
});
|
22
26
|
this.name = 'AbortError';
|
@@ -47,11 +51,12 @@ class PonyfillAbortSignal extends events.EventTarget {
|
|
47
51
|
this.addEventListener('abort', value);
|
48
52
|
}
|
49
53
|
abort(reason) {
|
50
|
-
|
54
|
+
const abortEvent = new events.CustomEvent('abort', { detail: reason });
|
55
|
+
this.dispatchEvent(abortEvent);
|
51
56
|
}
|
52
57
|
static timeout(milliseconds) {
|
53
58
|
const signal = new PonyfillAbortSignal();
|
54
|
-
setTimeout(() => signal.abort(`
|
59
|
+
setTimeout(() => signal.abort(`timeout in ${milliseconds} ms`), milliseconds);
|
55
60
|
return signal;
|
56
61
|
}
|
57
62
|
}
|
@@ -62,7 +67,7 @@ class PonyfillAbortController {
|
|
62
67
|
this.signal = new PonyfillAbortSignal();
|
63
68
|
}
|
64
69
|
abort(reason) {
|
65
|
-
this.signal.
|
70
|
+
this.signal.abort(reason);
|
66
71
|
}
|
67
72
|
}
|
68
73
|
|
@@ -805,6 +810,7 @@ function getRequestFnForProtocol(protocol) {
|
|
805
810
|
}
|
806
811
|
throw new Error(`Unsupported protocol: ${protocol}`);
|
807
812
|
}
|
813
|
+
const BASE64_SUFFIX = ';base64';
|
808
814
|
function fetchPonyfill(info, init) {
|
809
815
|
if (typeof info === 'string' || info instanceof URL) {
|
810
816
|
const ponyfillRequest = new PonyfillRequest(info, init);
|
@@ -815,30 +821,30 @@ function fetchPonyfill(info, init) {
|
|
815
821
|
try {
|
816
822
|
const url = new URL(fetchRequest.url, 'http://localhost');
|
817
823
|
if (url.protocol === 'data:') {
|
818
|
-
const [mimeType = 'text/plain',
|
819
|
-
|
820
|
-
|
824
|
+
const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
|
825
|
+
const data = decodeURIComponent(datas.join(','));
|
826
|
+
if (mimeType.endsWith(BASE64_SUFFIX)) {
|
827
|
+
const buffer = Buffer.from(data, 'base64');
|
828
|
+
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
|
821
829
|
const response = new PonyfillResponse(buffer, {
|
822
830
|
status: 200,
|
823
831
|
statusText: 'OK',
|
824
832
|
headers: {
|
825
|
-
'content-type':
|
826
|
-
},
|
827
|
-
});
|
828
|
-
resolve(response);
|
829
|
-
return;
|
830
|
-
}
|
831
|
-
if (base64FlagOrText) {
|
832
|
-
const response = new PonyfillResponse(base64FlagOrText, {
|
833
|
-
status: 200,
|
834
|
-
statusText: 'OK',
|
835
|
-
headers: {
|
836
|
-
'content-type': mimeType,
|
833
|
+
'content-type': realMimeType,
|
837
834
|
},
|
838
835
|
});
|
839
836
|
resolve(response);
|
840
837
|
return;
|
841
838
|
}
|
839
|
+
const response = new PonyfillResponse(data, {
|
840
|
+
status: 200,
|
841
|
+
statusText: 'OK',
|
842
|
+
headers: {
|
843
|
+
'content-type': mimeType,
|
844
|
+
},
|
845
|
+
});
|
846
|
+
resolve(response);
|
847
|
+
return;
|
842
848
|
}
|
843
849
|
if (url.protocol === 'file:') {
|
844
850
|
const response = getResponseForFile(url);
|
@@ -855,12 +861,7 @@ function fetchPonyfill(info, init) {
|
|
855
861
|
const abortListener = function abortListener(event) {
|
856
862
|
nodeRequest.destroy();
|
857
863
|
const reason = event.detail;
|
858
|
-
|
859
|
-
reject(reason);
|
860
|
-
}
|
861
|
-
else {
|
862
|
-
reject(new PonyfillAbortError(reason));
|
863
|
-
}
|
864
|
+
reject(new PonyfillAbortError(reason));
|
864
865
|
};
|
865
866
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
866
867
|
const nodeRequest = requestFn(url, {
|
package/index.mjs
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { request as request$1 } from 'http';
|
2
2
|
import { request } from 'https';
|
3
|
-
import { EventTarget, CustomEvent
|
3
|
+
import { EventTarget, CustomEvent } from '@whatwg-node/events';
|
4
4
|
import { Blob } from 'buffer';
|
5
5
|
import { Readable } from 'stream';
|
6
6
|
import busboy from 'busboy';
|
@@ -10,7 +10,11 @@ import { createReadStream } from 'fs';
|
|
10
10
|
// Will be removed after v14 reaches EOL
|
11
11
|
class PonyfillAbortError extends Error {
|
12
12
|
constructor(reason) {
|
13
|
-
|
13
|
+
let message = 'The operation was aborted.';
|
14
|
+
if (reason) {
|
15
|
+
message += ` reason: ${reason}`;
|
16
|
+
}
|
17
|
+
super(message, {
|
14
18
|
cause: reason,
|
15
19
|
});
|
16
20
|
this.name = 'AbortError';
|
@@ -41,11 +45,12 @@ class PonyfillAbortSignal extends EventTarget {
|
|
41
45
|
this.addEventListener('abort', value);
|
42
46
|
}
|
43
47
|
abort(reason) {
|
44
|
-
|
48
|
+
const abortEvent = new CustomEvent('abort', { detail: reason });
|
49
|
+
this.dispatchEvent(abortEvent);
|
45
50
|
}
|
46
51
|
static timeout(milliseconds) {
|
47
52
|
const signal = new PonyfillAbortSignal();
|
48
|
-
setTimeout(() => signal.abort(`
|
53
|
+
setTimeout(() => signal.abort(`timeout in ${milliseconds} ms`), milliseconds);
|
49
54
|
return signal;
|
50
55
|
}
|
51
56
|
}
|
@@ -56,7 +61,7 @@ class PonyfillAbortController {
|
|
56
61
|
this.signal = new PonyfillAbortSignal();
|
57
62
|
}
|
58
63
|
abort(reason) {
|
59
|
-
this.signal.
|
64
|
+
this.signal.abort(reason);
|
60
65
|
}
|
61
66
|
}
|
62
67
|
|
@@ -799,6 +804,7 @@ function getRequestFnForProtocol(protocol) {
|
|
799
804
|
}
|
800
805
|
throw new Error(`Unsupported protocol: ${protocol}`);
|
801
806
|
}
|
807
|
+
const BASE64_SUFFIX = ';base64';
|
802
808
|
function fetchPonyfill(info, init) {
|
803
809
|
if (typeof info === 'string' || info instanceof URL) {
|
804
810
|
const ponyfillRequest = new PonyfillRequest(info, init);
|
@@ -809,30 +815,30 @@ function fetchPonyfill(info, init) {
|
|
809
815
|
try {
|
810
816
|
const url = new URL(fetchRequest.url, 'http://localhost');
|
811
817
|
if (url.protocol === 'data:') {
|
812
|
-
const [mimeType = 'text/plain',
|
813
|
-
|
814
|
-
|
818
|
+
const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
|
819
|
+
const data = decodeURIComponent(datas.join(','));
|
820
|
+
if (mimeType.endsWith(BASE64_SUFFIX)) {
|
821
|
+
const buffer = Buffer.from(data, 'base64');
|
822
|
+
const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
|
815
823
|
const response = new PonyfillResponse(buffer, {
|
816
824
|
status: 200,
|
817
825
|
statusText: 'OK',
|
818
826
|
headers: {
|
819
|
-
'content-type':
|
820
|
-
},
|
821
|
-
});
|
822
|
-
resolve(response);
|
823
|
-
return;
|
824
|
-
}
|
825
|
-
if (base64FlagOrText) {
|
826
|
-
const response = new PonyfillResponse(base64FlagOrText, {
|
827
|
-
status: 200,
|
828
|
-
statusText: 'OK',
|
829
|
-
headers: {
|
830
|
-
'content-type': mimeType,
|
827
|
+
'content-type': realMimeType,
|
831
828
|
},
|
832
829
|
});
|
833
830
|
resolve(response);
|
834
831
|
return;
|
835
832
|
}
|
833
|
+
const response = new PonyfillResponse(data, {
|
834
|
+
status: 200,
|
835
|
+
statusText: 'OK',
|
836
|
+
headers: {
|
837
|
+
'content-type': mimeType,
|
838
|
+
},
|
839
|
+
});
|
840
|
+
resolve(response);
|
841
|
+
return;
|
836
842
|
}
|
837
843
|
if (url.protocol === 'file:') {
|
838
844
|
const response = getResponseForFile(url);
|
@@ -849,12 +855,7 @@ function fetchPonyfill(info, init) {
|
|
849
855
|
const abortListener = function abortListener(event) {
|
850
856
|
nodeRequest.destroy();
|
851
857
|
const reason = event.detail;
|
852
|
-
|
853
|
-
reject(reason);
|
854
|
-
}
|
855
|
-
else {
|
856
|
-
reject(new PonyfillAbortError(reason));
|
857
|
-
}
|
858
|
+
reject(new PonyfillAbortError(reason));
|
858
859
|
};
|
859
860
|
fetchRequest.signal.addEventListener('abort', abortListener);
|
860
861
|
const nodeRequest = requestFn(url, {
|
package/package.json
CHANGED