@whatwg-node/node-fetch 0.3.3-rc-20230320123406-da3a60b → 0.3.3
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/AbortController.js +14 -0
- package/cjs/AbortError.js +20 -0
- package/cjs/AbortSignal.js +37 -0
- package/cjs/Blob.js +119 -0
- package/cjs/Body.js +372 -0
- package/cjs/File.js +13 -0
- package/cjs/FormData.js +134 -0
- package/cjs/Headers.js +127 -0
- package/cjs/ReadableStream.js +172 -0
- package/cjs/Request.js +69 -0
- package/cjs/Response.js +76 -0
- package/cjs/TextEncoderDecoder.js +39 -0
- package/cjs/URL.js +55 -0
- package/cjs/URLSearchParams.js +103 -0
- package/cjs/fetch.js +143 -0
- package/cjs/index.js +35 -0
- package/cjs/package.json +1 -0
- package/cjs/utils.js +18 -0
- package/esm/AbortController.js +10 -0
- package/esm/AbortError.js +16 -0
- package/esm/AbortSignal.js +33 -0
- package/esm/Blob.js +115 -0
- package/esm/Body.js +367 -0
- package/esm/File.js +9 -0
- package/esm/FormData.js +129 -0
- package/esm/Headers.js +123 -0
- package/esm/ReadableStream.js +168 -0
- package/esm/Request.js +65 -0
- package/esm/Response.js +72 -0
- package/esm/TextEncoderDecoder.js +33 -0
- package/esm/URL.js +50 -0
- package/esm/URLSearchParams.js +98 -0
- package/esm/fetch.js +139 -0
- package/esm/index.js +15 -0
- package/esm/utils.js +13 -0
- package/package.json +19 -12
- package/typings/AbortController.d.cts +5 -0
- package/{AbortController.d.ts → typings/AbortController.d.ts} +1 -1
- package/typings/AbortError.d.ts +4 -0
- package/typings/AbortSignal.d.ts +11 -0
- package/typings/Blob.d.ts +18 -0
- package/typings/Body.d.cts +41 -0
- package/{Body.d.ts → typings/Body.d.ts} +3 -3
- package/typings/File.d.cts +7 -0
- package/{File.d.ts → typings/File.d.ts} +1 -1
- package/typings/FormData.d.cts +17 -0
- package/{FormData.d.ts → typings/FormData.d.ts} +2 -2
- package/typings/Headers.d.ts +21 -0
- package/typings/ReadableStream.d.ts +20 -0
- package/typings/Request.d.cts +24 -0
- package/{Request.d.ts → typings/Request.d.ts} +2 -2
- package/typings/Response.d.cts +22 -0
- package/{Response.d.ts → typings/Response.d.ts} +2 -2
- package/typings/TextEncoderDecoder.d.ts +15 -0
- package/typings/URL.d.cts +14 -0
- package/{URL.d.ts → typings/URL.d.ts} +1 -1
- package/typings/URLSearchParams.d.ts +17 -0
- package/typings/fetch.d.cts +3 -0
- package/{fetch.d.ts → typings/fetch.d.ts} +2 -2
- package/typings/index.d.cts +15 -0
- package/typings/index.d.ts +15 -0
- package/typings/utils.d.ts +2 -0
- package/index.d.ts +0 -15
- package/index.js +0 -1452
- package/index.mjs +0 -1430
- /package/{AbortError.d.ts → typings/AbortError.d.cts} +0 -0
- /package/{AbortSignal.d.ts → typings/AbortSignal.d.cts} +0 -0
- /package/{Blob.d.ts → typings/Blob.d.cts} +0 -0
- /package/{Headers.d.ts → typings/Headers.d.cts} +0 -0
- /package/{ReadableStream.d.ts → typings/ReadableStream.d.cts} +0 -0
- /package/{TextEncoderDecoder.d.ts → typings/TextEncoderDecoder.d.cts} +0 -0
- /package/{URLSearchParams.d.ts → typings/URLSearchParams.d.cts} +0 -0
- /package/{utils.d.ts → typings/utils.d.cts} +0 -0
package/cjs/FormData.js
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getStreamFromFormData = exports.PonyfillFormData = void 0;
|
4
|
+
const File_js_1 = require("./File.js");
|
5
|
+
const ReadableStream_js_1 = require("./ReadableStream.js");
|
6
|
+
class PonyfillFormData {
|
7
|
+
constructor() {
|
8
|
+
this.map = new Map();
|
9
|
+
}
|
10
|
+
append(name, value, fileName) {
|
11
|
+
let values = this.map.get(name);
|
12
|
+
if (!values) {
|
13
|
+
values = [];
|
14
|
+
this.map.set(name, values);
|
15
|
+
}
|
16
|
+
const entry = isBlob(value)
|
17
|
+
? getNormalizedFile(name, value, fileName)
|
18
|
+
: value;
|
19
|
+
values.push(entry);
|
20
|
+
}
|
21
|
+
delete(name) {
|
22
|
+
this.map.delete(name);
|
23
|
+
}
|
24
|
+
get(name) {
|
25
|
+
const values = this.map.get(name);
|
26
|
+
return values ? values[0] : null;
|
27
|
+
}
|
28
|
+
getAll(name) {
|
29
|
+
return this.map.get(name) || [];
|
30
|
+
}
|
31
|
+
has(name) {
|
32
|
+
return this.map.has(name);
|
33
|
+
}
|
34
|
+
set(name, value, fileName) {
|
35
|
+
const entry = isBlob(value)
|
36
|
+
? getNormalizedFile(name, value, fileName)
|
37
|
+
: value;
|
38
|
+
this.map.set(name, [entry]);
|
39
|
+
}
|
40
|
+
*[Symbol.iterator]() {
|
41
|
+
for (const [key, values] of this.map) {
|
42
|
+
for (const value of values) {
|
43
|
+
yield [key, value];
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
entries() {
|
48
|
+
return this[Symbol.iterator]();
|
49
|
+
}
|
50
|
+
keys() {
|
51
|
+
return this.map.keys();
|
52
|
+
}
|
53
|
+
*values() {
|
54
|
+
for (const values of this.map.values()) {
|
55
|
+
for (const value of values) {
|
56
|
+
yield value;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
forEach(callback) {
|
61
|
+
for (const [key, value] of this) {
|
62
|
+
callback(value, key, this);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
exports.PonyfillFormData = PonyfillFormData;
|
67
|
+
function getStreamFromFormData(formData, boundary = '---') {
|
68
|
+
const entries = [];
|
69
|
+
let sentInitialHeader = false;
|
70
|
+
return new ReadableStream_js_1.PonyfillReadableStream({
|
71
|
+
start: controller => {
|
72
|
+
formData.forEach((value, key) => {
|
73
|
+
if (!sentInitialHeader) {
|
74
|
+
controller.enqueue(Buffer.from(`--${boundary}\r\n`));
|
75
|
+
sentInitialHeader = true;
|
76
|
+
}
|
77
|
+
entries.push([key, value]);
|
78
|
+
});
|
79
|
+
if (!sentInitialHeader) {
|
80
|
+
controller.enqueue(Buffer.from(`--${boundary}--\r\n`));
|
81
|
+
controller.close();
|
82
|
+
}
|
83
|
+
},
|
84
|
+
pull: async (controller) => {
|
85
|
+
const entry = entries.shift();
|
86
|
+
if (entry) {
|
87
|
+
const [key, value] = entry;
|
88
|
+
if (typeof value === 'string') {
|
89
|
+
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
|
90
|
+
controller.enqueue(Buffer.from(value));
|
91
|
+
}
|
92
|
+
else {
|
93
|
+
let filenamePart = '';
|
94
|
+
if (value.name) {
|
95
|
+
filenamePart = `; filename="${value.name}"`;
|
96
|
+
}
|
97
|
+
controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
|
98
|
+
controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
|
99
|
+
const entryStream = value.stream();
|
100
|
+
for await (const chunk of entryStream) {
|
101
|
+
controller.enqueue(chunk);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
if (entries.length === 0) {
|
105
|
+
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
106
|
+
controller.close();
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
|
110
|
+
}
|
111
|
+
}
|
112
|
+
else {
|
113
|
+
controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
|
114
|
+
controller.close();
|
115
|
+
}
|
116
|
+
},
|
117
|
+
});
|
118
|
+
}
|
119
|
+
exports.getStreamFromFormData = getStreamFromFormData;
|
120
|
+
function getNormalizedFile(name, blob, fileName) {
|
121
|
+
if (blob instanceof File_js_1.PonyfillFile) {
|
122
|
+
if (fileName != null) {
|
123
|
+
return new File_js_1.PonyfillFile([blob], fileName, {
|
124
|
+
type: blob.type,
|
125
|
+
lastModified: blob.lastModified,
|
126
|
+
});
|
127
|
+
}
|
128
|
+
return blob;
|
129
|
+
}
|
130
|
+
return new File_js_1.PonyfillFile([blob], fileName || name, { type: blob.type });
|
131
|
+
}
|
132
|
+
function isBlob(value) {
|
133
|
+
return value != null && typeof value === 'object' && typeof value.arrayBuffer === 'function';
|
134
|
+
}
|
package/cjs/Headers.js
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.PonyfillHeaders = void 0;
|
4
|
+
function isHeadersLike(headers) {
|
5
|
+
return headers && typeof headers.get === 'function';
|
6
|
+
}
|
7
|
+
class PonyfillHeaders {
|
8
|
+
constructor(headersInit) {
|
9
|
+
this.headersInit = headersInit;
|
10
|
+
this.map = new Map();
|
11
|
+
this.mapIsBuilt = false;
|
12
|
+
this.objectNormalizedKeysOfHeadersInit = [];
|
13
|
+
this.objectOriginalKeysOfHeadersInit = [];
|
14
|
+
}
|
15
|
+
// perf: we don't need to build `this.map` for Requests, as we can access the headers directly
|
16
|
+
_get(key) {
|
17
|
+
// If the map is built, reuse it
|
18
|
+
if (this.mapIsBuilt) {
|
19
|
+
return this.map.get(key.toLowerCase()) || null;
|
20
|
+
}
|
21
|
+
// If the map is not built, try to get the value from the this.headersInit
|
22
|
+
if (this.headersInit == null) {
|
23
|
+
return null;
|
24
|
+
}
|
25
|
+
const normalized = key.toLowerCase();
|
26
|
+
if (Array.isArray(this.headersInit)) {
|
27
|
+
return this.headersInit.find(header => header[0] === normalized);
|
28
|
+
}
|
29
|
+
else if (isHeadersLike(this.headersInit)) {
|
30
|
+
return this.headersInit.get(normalized);
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
const initValue = this.headersInit[key] || this.headersInit[normalized];
|
34
|
+
if (initValue != null) {
|
35
|
+
return initValue;
|
36
|
+
}
|
37
|
+
if (!this.objectNormalizedKeysOfHeadersInit.length) {
|
38
|
+
Object.keys(this.headersInit).forEach(k => {
|
39
|
+
this.objectOriginalKeysOfHeadersInit.push(k);
|
40
|
+
this.objectNormalizedKeysOfHeadersInit.push(k.toLowerCase());
|
41
|
+
});
|
42
|
+
}
|
43
|
+
const index = this.objectNormalizedKeysOfHeadersInit.indexOf(normalized);
|
44
|
+
if (index === -1) {
|
45
|
+
return null;
|
46
|
+
}
|
47
|
+
const originalKey = this.objectOriginalKeysOfHeadersInit[index];
|
48
|
+
return this.headersInit[originalKey];
|
49
|
+
}
|
50
|
+
}
|
51
|
+
// perf: Build the map of headers lazily, only when we need to access all headers or write to it.
|
52
|
+
// I could do a getter here, but I'm too lazy to type `getter`.
|
53
|
+
getMap() {
|
54
|
+
if (this.mapIsBuilt) {
|
55
|
+
return this.map;
|
56
|
+
}
|
57
|
+
if (this.headersInit != null) {
|
58
|
+
if (Array.isArray(this.headersInit)) {
|
59
|
+
this.map = new Map(this.headersInit);
|
60
|
+
}
|
61
|
+
else if (isHeadersLike(this.headersInit)) {
|
62
|
+
this.headersInit.forEach((value, key) => {
|
63
|
+
this.map.set(key, value);
|
64
|
+
});
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
for (const initKey in this.headersInit) {
|
68
|
+
const initValue = this.headersInit[initKey];
|
69
|
+
if (initValue != null) {
|
70
|
+
const normalizedValue = Array.isArray(initValue) ? initValue.join(', ') : initValue;
|
71
|
+
const normalizedKey = initKey.toLowerCase();
|
72
|
+
this.map.set(normalizedKey, normalizedValue);
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
}
|
77
|
+
this.mapIsBuilt = true;
|
78
|
+
return this.map;
|
79
|
+
}
|
80
|
+
append(name, value) {
|
81
|
+
const key = name.toLowerCase();
|
82
|
+
const existingValue = this.getMap().get(key);
|
83
|
+
const finalValue = existingValue ? `${existingValue}, ${value}` : value;
|
84
|
+
this.getMap().set(key, finalValue);
|
85
|
+
}
|
86
|
+
get(name) {
|
87
|
+
const key = name.toLowerCase();
|
88
|
+
const value = this._get(key);
|
89
|
+
if (value == null) {
|
90
|
+
return null;
|
91
|
+
}
|
92
|
+
if (Array.isArray(value)) {
|
93
|
+
return value.join(', ');
|
94
|
+
}
|
95
|
+
return value;
|
96
|
+
}
|
97
|
+
has(name) {
|
98
|
+
const key = name.toLowerCase();
|
99
|
+
return !!this._get(key); // we might need to check if header exists and not just check if it's not nullable
|
100
|
+
}
|
101
|
+
set(name, value) {
|
102
|
+
const key = name.toLowerCase();
|
103
|
+
this.getMap().set(key, value);
|
104
|
+
}
|
105
|
+
delete(name) {
|
106
|
+
const key = name.toLowerCase();
|
107
|
+
this.getMap().delete(key);
|
108
|
+
}
|
109
|
+
forEach(callback) {
|
110
|
+
this.getMap().forEach((value, key) => {
|
111
|
+
callback(value, key, this);
|
112
|
+
});
|
113
|
+
}
|
114
|
+
keys() {
|
115
|
+
return this.getMap().keys();
|
116
|
+
}
|
117
|
+
values() {
|
118
|
+
return this.getMap().values();
|
119
|
+
}
|
120
|
+
entries() {
|
121
|
+
return this.getMap().entries();
|
122
|
+
}
|
123
|
+
[Symbol.iterator]() {
|
124
|
+
return this.getMap().entries();
|
125
|
+
}
|
126
|
+
}
|
127
|
+
exports.PonyfillHeaders = PonyfillHeaders;
|
@@ -0,0 +1,172 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.PonyfillReadableStream = void 0;
|
4
|
+
const stream_1 = require("stream");
|
5
|
+
function createController(desiredSize, readable) {
|
6
|
+
let chunks = [];
|
7
|
+
let _closed = false;
|
8
|
+
let flushed = false;
|
9
|
+
return {
|
10
|
+
desiredSize,
|
11
|
+
enqueue(chunk) {
|
12
|
+
const buf = typeof chunk === 'string' ? Buffer.from(chunk) : chunk;
|
13
|
+
if (!flushed) {
|
14
|
+
chunks.push(buf);
|
15
|
+
}
|
16
|
+
else {
|
17
|
+
readable.push(buf);
|
18
|
+
}
|
19
|
+
},
|
20
|
+
close() {
|
21
|
+
if (chunks.length > 0) {
|
22
|
+
this._flush();
|
23
|
+
}
|
24
|
+
readable.push(null);
|
25
|
+
_closed = true;
|
26
|
+
},
|
27
|
+
error(error) {
|
28
|
+
if (chunks.length > 0) {
|
29
|
+
this._flush();
|
30
|
+
}
|
31
|
+
readable.destroy(error);
|
32
|
+
},
|
33
|
+
get _closed() {
|
34
|
+
return _closed;
|
35
|
+
},
|
36
|
+
_flush() {
|
37
|
+
flushed = true;
|
38
|
+
if (chunks.length > 0) {
|
39
|
+
const concatenated = Buffer.concat(chunks);
|
40
|
+
readable.push(concatenated);
|
41
|
+
chunks = [];
|
42
|
+
}
|
43
|
+
},
|
44
|
+
};
|
45
|
+
}
|
46
|
+
class PonyfillReadableStream {
|
47
|
+
constructor(underlyingSource) {
|
48
|
+
this.locked = false;
|
49
|
+
if (underlyingSource instanceof PonyfillReadableStream) {
|
50
|
+
this.readable = underlyingSource.readable;
|
51
|
+
}
|
52
|
+
else if (underlyingSource && 'read' in underlyingSource) {
|
53
|
+
this.readable = underlyingSource;
|
54
|
+
}
|
55
|
+
else if (underlyingSource && 'getReader' in underlyingSource) {
|
56
|
+
let reader;
|
57
|
+
let started = false;
|
58
|
+
this.readable = new stream_1.Readable({
|
59
|
+
read() {
|
60
|
+
if (!started) {
|
61
|
+
started = true;
|
62
|
+
reader = underlyingSource.getReader();
|
63
|
+
}
|
64
|
+
reader
|
65
|
+
.read()
|
66
|
+
.then(({ value, done }) => {
|
67
|
+
if (done) {
|
68
|
+
this.push(null);
|
69
|
+
}
|
70
|
+
else {
|
71
|
+
this.push(value);
|
72
|
+
}
|
73
|
+
})
|
74
|
+
.catch(err => {
|
75
|
+
this.destroy(err);
|
76
|
+
});
|
77
|
+
},
|
78
|
+
destroy(err, callback) {
|
79
|
+
reader.cancel(err).then(() => callback(err), callback);
|
80
|
+
},
|
81
|
+
});
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
let started = false;
|
85
|
+
let ongoing = false;
|
86
|
+
this.readable = new stream_1.Readable({
|
87
|
+
read(desiredSize) {
|
88
|
+
if (ongoing) {
|
89
|
+
return;
|
90
|
+
}
|
91
|
+
ongoing = true;
|
92
|
+
return Promise.resolve().then(async () => {
|
93
|
+
var _a, _b;
|
94
|
+
if (!started) {
|
95
|
+
const controller = createController(desiredSize, this);
|
96
|
+
started = true;
|
97
|
+
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
|
98
|
+
controller._flush();
|
99
|
+
if (controller._closed) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
const controller = createController(desiredSize, this);
|
104
|
+
await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
|
105
|
+
controller._flush();
|
106
|
+
ongoing = false;
|
107
|
+
});
|
108
|
+
},
|
109
|
+
async destroy(err, callback) {
|
110
|
+
var _a;
|
111
|
+
try {
|
112
|
+
await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.cancel) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, err));
|
113
|
+
callback(null);
|
114
|
+
}
|
115
|
+
catch (err) {
|
116
|
+
callback(err);
|
117
|
+
}
|
118
|
+
},
|
119
|
+
});
|
120
|
+
}
|
121
|
+
}
|
122
|
+
cancel(reason) {
|
123
|
+
this.readable.destroy(reason);
|
124
|
+
return Promise.resolve();
|
125
|
+
}
|
126
|
+
getReader(_options) {
|
127
|
+
const iterator = this.readable[Symbol.asyncIterator]();
|
128
|
+
this.locked = true;
|
129
|
+
return {
|
130
|
+
read() {
|
131
|
+
return iterator.next();
|
132
|
+
},
|
133
|
+
releaseLock: () => {
|
134
|
+
var _a;
|
135
|
+
(_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
|
136
|
+
this.locked = false;
|
137
|
+
},
|
138
|
+
cancel: async (reason) => {
|
139
|
+
var _a;
|
140
|
+
await ((_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator, reason));
|
141
|
+
this.locked = false;
|
142
|
+
},
|
143
|
+
closed: new Promise((resolve, reject) => {
|
144
|
+
this.readable.once('end', resolve);
|
145
|
+
this.readable.once('error', reject);
|
146
|
+
}),
|
147
|
+
};
|
148
|
+
}
|
149
|
+
[Symbol.asyncIterator]() {
|
150
|
+
return this.readable[Symbol.asyncIterator]();
|
151
|
+
}
|
152
|
+
tee() {
|
153
|
+
throw new Error('Not implemented');
|
154
|
+
}
|
155
|
+
async pipeTo(destination) {
|
156
|
+
const writer = destination.getWriter();
|
157
|
+
await writer.ready;
|
158
|
+
for await (const chunk of this.readable) {
|
159
|
+
await writer.write(chunk);
|
160
|
+
}
|
161
|
+
await writer.ready;
|
162
|
+
return writer.close();
|
163
|
+
}
|
164
|
+
pipeThrough({ writable, readable, }) {
|
165
|
+
this.pipeTo(writable);
|
166
|
+
return readable;
|
167
|
+
}
|
168
|
+
static [Symbol.hasInstance](instance) {
|
169
|
+
return instance != null && typeof instance === 'object' && 'getReader' in instance;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
exports.PonyfillReadableStream = PonyfillReadableStream;
|
package/cjs/Request.js
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.PonyfillRequest = void 0;
|
4
|
+
const AbortController_js_1 = require("./AbortController.js");
|
5
|
+
const Body_js_1 = require("./Body.js");
|
6
|
+
const Headers_js_1 = require("./Headers.js");
|
7
|
+
function isRequest(input) {
|
8
|
+
return input[Symbol.toStringTag] === 'Request';
|
9
|
+
}
|
10
|
+
class PonyfillRequest extends Body_js_1.PonyfillBody {
|
11
|
+
constructor(input, options) {
|
12
|
+
var _a;
|
13
|
+
let url;
|
14
|
+
let bodyInit = null;
|
15
|
+
let requestInit;
|
16
|
+
if (typeof input === 'string') {
|
17
|
+
url = input;
|
18
|
+
}
|
19
|
+
else if ('href' in input) {
|
20
|
+
url = input.toString();
|
21
|
+
}
|
22
|
+
else if (isRequest(input)) {
|
23
|
+
url = input.url;
|
24
|
+
bodyInit = input.body;
|
25
|
+
requestInit = input;
|
26
|
+
}
|
27
|
+
if (options != null) {
|
28
|
+
bodyInit = options.body || null;
|
29
|
+
requestInit = options;
|
30
|
+
}
|
31
|
+
super(bodyInit, options);
|
32
|
+
this.destination = '';
|
33
|
+
this.priority = 'auto';
|
34
|
+
this.cache = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.cache) || 'default';
|
35
|
+
this.credentials = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.credentials) || 'same-origin';
|
36
|
+
this.headers = new Headers_js_1.PonyfillHeaders(requestInit === null || requestInit === void 0 ? void 0 : requestInit.headers);
|
37
|
+
this.integrity = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.integrity) || '';
|
38
|
+
this.keepalive = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.keepalive) != null ? requestInit === null || requestInit === void 0 ? void 0 : requestInit.keepalive : false;
|
39
|
+
this.method = ((_a = requestInit === null || requestInit === void 0 ? void 0 : requestInit.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'GET';
|
40
|
+
this.mode = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.mode) || 'cors';
|
41
|
+
this.redirect = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.redirect) || 'follow';
|
42
|
+
this.referrer = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.referrer) || 'about:client';
|
43
|
+
this.referrerPolicy = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.referrerPolicy) || 'no-referrer';
|
44
|
+
this.signal = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.signal) || new AbortController_js_1.PonyfillAbortController().signal;
|
45
|
+
this.url = url || '';
|
46
|
+
const contentTypeInHeaders = this.headers.get('content-type');
|
47
|
+
if (!contentTypeInHeaders) {
|
48
|
+
if (this.contentType) {
|
49
|
+
this.headers.set('content-type', this.contentType);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
else {
|
53
|
+
this.contentType = contentTypeInHeaders;
|
54
|
+
}
|
55
|
+
const contentLengthInHeaders = this.headers.get('content-length');
|
56
|
+
if (!contentLengthInHeaders) {
|
57
|
+
if (this.contentLength) {
|
58
|
+
this.headers.set('content-length', this.contentLength.toString());
|
59
|
+
}
|
60
|
+
}
|
61
|
+
else {
|
62
|
+
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
clone() {
|
66
|
+
return new Request(this);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
exports.PonyfillRequest = PonyfillRequest;
|
package/cjs/Response.js
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.PonyfillResponse = void 0;
|
4
|
+
const http_1 = require("http");
|
5
|
+
const Body_js_1 = require("./Body.js");
|
6
|
+
const Headers_js_1 = require("./Headers.js");
|
7
|
+
class PonyfillResponse extends Body_js_1.PonyfillBody {
|
8
|
+
constructor(body, init) {
|
9
|
+
super(body || null, init);
|
10
|
+
this.headers = new Headers_js_1.PonyfillHeaders();
|
11
|
+
this.status = 200;
|
12
|
+
this.statusText = 'OK';
|
13
|
+
this.url = '';
|
14
|
+
this.redirected = false;
|
15
|
+
this.type = 'default';
|
16
|
+
if (init) {
|
17
|
+
this.headers = new Headers_js_1.PonyfillHeaders(init.headers);
|
18
|
+
this.status = init.status || 200;
|
19
|
+
this.statusText = init.statusText || http_1.STATUS_CODES[this.status] || 'OK';
|
20
|
+
this.url = init.url || '';
|
21
|
+
this.redirected = init.redirected || false;
|
22
|
+
this.type = init.type || 'default';
|
23
|
+
}
|
24
|
+
const contentTypeInHeaders = this.headers.get('content-type');
|
25
|
+
if (!contentTypeInHeaders) {
|
26
|
+
if (this.contentType) {
|
27
|
+
this.headers.set('content-type', this.contentType);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
this.contentType = contentTypeInHeaders;
|
32
|
+
}
|
33
|
+
const contentLengthInHeaders = this.headers.get('content-length');
|
34
|
+
if (!contentLengthInHeaders) {
|
35
|
+
if (this.contentLength) {
|
36
|
+
this.headers.set('content-length', this.contentLength.toString());
|
37
|
+
}
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
get ok() {
|
44
|
+
return this.status >= 200 && this.status < 300;
|
45
|
+
}
|
46
|
+
clone() {
|
47
|
+
return new PonyfillResponse(this.body, this);
|
48
|
+
}
|
49
|
+
static error() {
|
50
|
+
return new PonyfillResponse(null, {
|
51
|
+
status: 500,
|
52
|
+
statusText: 'Internal Server Error',
|
53
|
+
});
|
54
|
+
}
|
55
|
+
static redirect(url, status = 301) {
|
56
|
+
if (status < 300 || status > 399) {
|
57
|
+
throw new RangeError('Invalid status code');
|
58
|
+
}
|
59
|
+
return new PonyfillResponse(null, {
|
60
|
+
headers: {
|
61
|
+
location: url,
|
62
|
+
},
|
63
|
+
status,
|
64
|
+
});
|
65
|
+
}
|
66
|
+
static json(data, init = {}) {
|
67
|
+
return new PonyfillResponse(JSON.stringify(data), {
|
68
|
+
...init,
|
69
|
+
headers: {
|
70
|
+
'Content-Type': 'application/json',
|
71
|
+
...init === null || init === void 0 ? void 0 : init.headers,
|
72
|
+
},
|
73
|
+
});
|
74
|
+
}
|
75
|
+
}
|
76
|
+
exports.PonyfillResponse = PonyfillResponse;
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.PonyfillBtoa = exports.PonyfillTextDecoder = exports.PonyfillTextEncoder = void 0;
|
4
|
+
class PonyfillTextEncoder {
|
5
|
+
constructor(encoding = 'utf-8') {
|
6
|
+
this.encoding = encoding;
|
7
|
+
}
|
8
|
+
encode(input) {
|
9
|
+
return Buffer.from(input, this.encoding);
|
10
|
+
}
|
11
|
+
encodeInto(source, destination) {
|
12
|
+
const buffer = this.encode(source);
|
13
|
+
const copied = buffer.copy(destination);
|
14
|
+
return {
|
15
|
+
read: copied,
|
16
|
+
written: copied,
|
17
|
+
};
|
18
|
+
}
|
19
|
+
}
|
20
|
+
exports.PonyfillTextEncoder = PonyfillTextEncoder;
|
21
|
+
class PonyfillTextDecoder {
|
22
|
+
constructor(encoding = 'utf-8', options) {
|
23
|
+
this.encoding = encoding;
|
24
|
+
this.fatal = false;
|
25
|
+
this.ignoreBOM = false;
|
26
|
+
if (options) {
|
27
|
+
this.fatal = options.fatal || false;
|
28
|
+
this.ignoreBOM = options.ignoreBOM || false;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
decode(input) {
|
32
|
+
return Buffer.from(input).toString(this.encoding);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
exports.PonyfillTextDecoder = PonyfillTextDecoder;
|
36
|
+
function PonyfillBtoa(input) {
|
37
|
+
return Buffer.from(input, 'binary').toString('base64');
|
38
|
+
}
|
39
|
+
exports.PonyfillBtoa = PonyfillBtoa;
|
package/cjs/URL.js
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.PonyfillURL = void 0;
|
4
|
+
const tslib_1 = require("tslib");
|
5
|
+
const fast_querystring_1 = tslib_1.__importDefault(require("fast-querystring"));
|
6
|
+
const fast_url_parser_1 = tslib_1.__importDefault(require("fast-url-parser"));
|
7
|
+
const URLSearchParams_js_1 = require("./URLSearchParams.js");
|
8
|
+
fast_url_parser_1.default.queryString = fast_querystring_1.default;
|
9
|
+
class PonyfillURL extends fast_url_parser_1.default {
|
10
|
+
constructor(url, base) {
|
11
|
+
super();
|
12
|
+
if (url.startsWith('data:')) {
|
13
|
+
this.protocol = 'data:';
|
14
|
+
this.pathname = url.slice('data:'.length);
|
15
|
+
return;
|
16
|
+
}
|
17
|
+
this.parse(url, false);
|
18
|
+
if (base) {
|
19
|
+
const baseParsed = typeof base === 'string' ? new PonyfillURL(base) : base;
|
20
|
+
this.protocol = this.protocol || baseParsed.protocol;
|
21
|
+
this.host = this.host || baseParsed.host;
|
22
|
+
this.pathname = this.pathname || baseParsed.pathname;
|
23
|
+
}
|
24
|
+
}
|
25
|
+
get origin() {
|
26
|
+
return `${this.protocol}//${this.host}`;
|
27
|
+
}
|
28
|
+
get searchParams() {
|
29
|
+
if (!this._searchParams) {
|
30
|
+
this._searchParams = new URLSearchParams_js_1.PonyfillURLSearchParams(this.query);
|
31
|
+
}
|
32
|
+
return this._searchParams;
|
33
|
+
}
|
34
|
+
get username() {
|
35
|
+
var _a;
|
36
|
+
return ((_a = this.auth) === null || _a === void 0 ? void 0 : _a.split(':')[0]) || '';
|
37
|
+
}
|
38
|
+
set username(value) {
|
39
|
+
this.auth = `${value}:${this.password}`;
|
40
|
+
}
|
41
|
+
get password() {
|
42
|
+
var _a;
|
43
|
+
return ((_a = this.auth) === null || _a === void 0 ? void 0 : _a.split(':')[1]) || '';
|
44
|
+
}
|
45
|
+
set password(value) {
|
46
|
+
this.auth = `${this.username}:${value}`;
|
47
|
+
}
|
48
|
+
toString() {
|
49
|
+
return this.format();
|
50
|
+
}
|
51
|
+
toJSON() {
|
52
|
+
return this.toString();
|
53
|
+
}
|
54
|
+
}
|
55
|
+
exports.PonyfillURL = PonyfillURL;
|