@whatwg-node/node-fetch 0.3.3-rc-20230320121450-923e939 → 0.3.3-rc-20230320124940-870b034

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.
Files changed (73) hide show
  1. package/cjs/AbortController.js +14 -0
  2. package/cjs/AbortError.js +20 -0
  3. package/cjs/AbortSignal.js +37 -0
  4. package/cjs/Blob.js +119 -0
  5. package/cjs/Body.js +372 -0
  6. package/cjs/File.js +13 -0
  7. package/cjs/FormData.js +134 -0
  8. package/cjs/Headers.js +127 -0
  9. package/cjs/ReadableStream.js +172 -0
  10. package/cjs/Request.js +69 -0
  11. package/cjs/Response.js +76 -0
  12. package/cjs/TextEncoderDecoder.js +39 -0
  13. package/cjs/URL.js +55 -0
  14. package/cjs/URLSearchParams.js +103 -0
  15. package/cjs/fetch.js +143 -0
  16. package/cjs/index.js +35 -0
  17. package/cjs/package.json +1 -0
  18. package/cjs/utils.js +18 -0
  19. package/esm/AbortController.js +10 -0
  20. package/esm/AbortError.js +16 -0
  21. package/esm/AbortSignal.js +33 -0
  22. package/esm/Blob.js +115 -0
  23. package/esm/Body.js +367 -0
  24. package/esm/File.js +9 -0
  25. package/esm/FormData.js +129 -0
  26. package/esm/Headers.js +123 -0
  27. package/esm/ReadableStream.js +168 -0
  28. package/esm/Request.js +65 -0
  29. package/esm/Response.js +72 -0
  30. package/esm/TextEncoderDecoder.js +33 -0
  31. package/esm/URL.js +50 -0
  32. package/esm/URLSearchParams.js +98 -0
  33. package/esm/fetch.js +139 -0
  34. package/esm/index.js +15 -0
  35. package/esm/utils.js +13 -0
  36. package/package.json +19 -12
  37. package/typings/AbortController.d.cts +5 -0
  38. package/{AbortController.d.ts → typings/AbortController.d.ts} +1 -1
  39. package/typings/AbortError.d.ts +4 -0
  40. package/typings/AbortSignal.d.ts +11 -0
  41. package/typings/Blob.d.ts +18 -0
  42. package/typings/Body.d.cts +41 -0
  43. package/{Body.d.ts → typings/Body.d.ts} +3 -3
  44. package/typings/File.d.cts +7 -0
  45. package/{File.d.ts → typings/File.d.ts} +1 -1
  46. package/typings/FormData.d.cts +17 -0
  47. package/{FormData.d.ts → typings/FormData.d.ts} +2 -2
  48. package/typings/Headers.d.ts +21 -0
  49. package/typings/ReadableStream.d.ts +20 -0
  50. package/typings/Request.d.cts +24 -0
  51. package/{Request.d.ts → typings/Request.d.ts} +2 -2
  52. package/typings/Response.d.cts +22 -0
  53. package/{Response.d.ts → typings/Response.d.ts} +2 -2
  54. package/typings/TextEncoderDecoder.d.ts +15 -0
  55. package/typings/URL.d.cts +14 -0
  56. package/{URL.d.ts → typings/URL.d.ts} +1 -1
  57. package/typings/URLSearchParams.d.ts +17 -0
  58. package/typings/fetch.d.cts +3 -0
  59. package/{fetch.d.ts → typings/fetch.d.ts} +2 -2
  60. package/typings/index.d.cts +15 -0
  61. package/typings/index.d.ts +15 -0
  62. package/typings/utils.d.ts +2 -0
  63. package/index.d.ts +0 -15
  64. package/index.js +0 -1452
  65. package/index.mjs +0 -1430
  66. /package/{AbortError.d.ts → typings/AbortError.d.cts} +0 -0
  67. /package/{AbortSignal.d.ts → typings/AbortSignal.d.cts} +0 -0
  68. /package/{Blob.d.ts → typings/Blob.d.cts} +0 -0
  69. /package/{Headers.d.ts → typings/Headers.d.cts} +0 -0
  70. /package/{ReadableStream.d.ts → typings/ReadableStream.d.cts} +0 -0
  71. /package/{TextEncoderDecoder.d.ts → typings/TextEncoderDecoder.d.cts} +0 -0
  72. /package/{URLSearchParams.d.ts → typings/URLSearchParams.d.cts} +0 -0
  73. /package/{utils.d.ts → typings/utils.d.cts} +0 -0
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillAbortController = void 0;
4
+ const AbortSignal_js_1 = require("./AbortSignal.js");
5
+ // Will be removed after v14 reaches EOL
6
+ class PonyfillAbortController {
7
+ constructor() {
8
+ this.signal = new AbortSignal_js_1.PonyfillAbortSignal();
9
+ }
10
+ abort(reason) {
11
+ this.signal.abort(reason);
12
+ }
13
+ }
14
+ exports.PonyfillAbortController = PonyfillAbortController;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillAbortError = void 0;
4
+ // Will be removed after v14 reaches EOL
5
+ class PonyfillAbortError extends Error {
6
+ constructor(reason) {
7
+ let message = 'The operation was aborted.';
8
+ if (reason) {
9
+ message += ` reason: ${reason}`;
10
+ }
11
+ super(message, {
12
+ cause: reason,
13
+ });
14
+ this.name = 'AbortError';
15
+ }
16
+ get reason() {
17
+ return this.cause;
18
+ }
19
+ }
20
+ exports.PonyfillAbortError = PonyfillAbortError;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillAbortSignal = void 0;
4
+ // Will be removed after v14 reaches EOL
5
+ const events_1 = require("@whatwg-node/events");
6
+ const AbortError_js_1 = require("./AbortError.js");
7
+ class PonyfillAbortSignal extends events_1.EventTarget {
8
+ constructor() {
9
+ super(...arguments);
10
+ this.aborted = false;
11
+ this._onabort = null;
12
+ }
13
+ throwIfAborted() {
14
+ if (this.aborted) {
15
+ throw new AbortError_js_1.PonyfillAbortError();
16
+ }
17
+ }
18
+ get onabort() {
19
+ return this._onabort;
20
+ }
21
+ set onabort(value) {
22
+ if (this._onabort) {
23
+ this.removeEventListener('abort', this._onabort);
24
+ }
25
+ this.addEventListener('abort', value);
26
+ }
27
+ abort(reason) {
28
+ const abortEvent = new events_1.CustomEvent('abort', { detail: reason });
29
+ this.dispatchEvent(abortEvent);
30
+ }
31
+ static timeout(milliseconds) {
32
+ const signal = new PonyfillAbortSignal();
33
+ setTimeout(() => signal.abort(`timeout in ${milliseconds} ms`), milliseconds);
34
+ return signal;
35
+ }
36
+ }
37
+ exports.PonyfillAbortSignal = PonyfillAbortSignal;
package/cjs/Blob.js ADDED
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillBlob = void 0;
4
+ const ReadableStream_js_1 = require("./ReadableStream.js");
5
+ const utils_js_1 = require("./utils.js");
6
+ function getBlobPartAsBuffer(blobPart) {
7
+ if (typeof blobPart === 'string') {
8
+ return Buffer.from(blobPart);
9
+ }
10
+ else if (Buffer.isBuffer(blobPart)) {
11
+ return blobPart;
12
+ }
13
+ else if (blobPart instanceof Uint8Array) {
14
+ return Buffer.from(blobPart);
15
+ }
16
+ else if ('buffer' in blobPart) {
17
+ return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
18
+ }
19
+ else {
20
+ return Buffer.from(blobPart);
21
+ }
22
+ }
23
+ function isBlob(obj) {
24
+ return obj != null && typeof obj === 'object' && obj.arrayBuffer != null;
25
+ }
26
+ // Will be removed after v14 reaches EOL
27
+ // Needed because v14 doesn't have .stream() implemented
28
+ class PonyfillBlob {
29
+ constructor(blobParts, options) {
30
+ this.blobParts = blobParts;
31
+ this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream';
32
+ this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8';
33
+ }
34
+ async buffer() {
35
+ const bufferChunks = [];
36
+ for (const blobPart of this.blobParts) {
37
+ if (isBlob(blobPart)) {
38
+ const arrayBuf = await blobPart.arrayBuffer();
39
+ const buf = Buffer.from(arrayBuf, undefined, blobPart.size);
40
+ bufferChunks.push(buf);
41
+ }
42
+ else {
43
+ const buf = getBlobPartAsBuffer(blobPart);
44
+ bufferChunks.push(buf);
45
+ }
46
+ }
47
+ return Buffer.concat(bufferChunks);
48
+ }
49
+ async arrayBuffer() {
50
+ const buffer = await this.buffer();
51
+ return (0, utils_js_1.uint8ArrayToArrayBuffer)(buffer);
52
+ }
53
+ async text() {
54
+ let text = '';
55
+ for (const blobPart of this.blobParts) {
56
+ if (typeof blobPart === 'string') {
57
+ text += blobPart;
58
+ }
59
+ else if ('text' in blobPart) {
60
+ text += await blobPart.text();
61
+ }
62
+ else {
63
+ const buf = getBlobPartAsBuffer(blobPart);
64
+ text += buf.toString(this.encoding);
65
+ }
66
+ }
67
+ return text;
68
+ }
69
+ get size() {
70
+ let size = 0;
71
+ for (const blobPart of this.blobParts) {
72
+ if (typeof blobPart === 'string') {
73
+ size += Buffer.byteLength(blobPart);
74
+ }
75
+ else if (isBlob(blobPart)) {
76
+ size += blobPart.size;
77
+ }
78
+ else if ('length' in blobPart) {
79
+ size += blobPart.length;
80
+ }
81
+ else if ('byteLength' in blobPart) {
82
+ size += blobPart.byteLength;
83
+ }
84
+ }
85
+ return size;
86
+ }
87
+ stream() {
88
+ let partQueue = [];
89
+ return new ReadableStream_js_1.PonyfillReadableStream({
90
+ start: controller => {
91
+ partQueue = [...this.blobParts];
92
+ if (partQueue.length === 0) {
93
+ controller.close();
94
+ }
95
+ },
96
+ pull: async (controller) => {
97
+ const blobPart = partQueue.pop();
98
+ if (blobPart) {
99
+ if (isBlob(blobPart)) {
100
+ const arrayBuffer = await blobPart.arrayBuffer();
101
+ const buf = Buffer.from(arrayBuffer, undefined, blobPart.size);
102
+ controller.enqueue(buf);
103
+ }
104
+ else {
105
+ const buf = getBlobPartAsBuffer(blobPart);
106
+ controller.enqueue(buf);
107
+ }
108
+ }
109
+ else {
110
+ controller.close();
111
+ }
112
+ },
113
+ });
114
+ }
115
+ slice() {
116
+ throw new Error('Not implemented');
117
+ }
118
+ }
119
+ exports.PonyfillBlob = PonyfillBlob;
package/cjs/Body.js ADDED
@@ -0,0 +1,372 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillBody = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const stream_1 = require("stream");
6
+ const busboy_1 = tslib_1.__importDefault(require("busboy"));
7
+ const Blob_js_1 = require("./Blob.js");
8
+ const File_js_1 = require("./File.js");
9
+ const FormData_js_1 = require("./FormData.js");
10
+ const ReadableStream_js_1 = require("./ReadableStream.js");
11
+ const utils_js_1 = require("./utils.js");
12
+ var BodyInitType;
13
+ (function (BodyInitType) {
14
+ BodyInitType["ReadableStream"] = "ReadableStream";
15
+ BodyInitType["Blob"] = "Blob";
16
+ BodyInitType["FormData"] = "FormData";
17
+ BodyInitType["ArrayBuffer"] = "ArrayBuffer";
18
+ BodyInitType["String"] = "String";
19
+ BodyInitType["Readable"] = "Readable";
20
+ BodyInitType["Buffer"] = "Buffer";
21
+ BodyInitType["Uint8Array"] = "Uint8Array";
22
+ })(BodyInitType || (BodyInitType = {}));
23
+ class PonyfillBody {
24
+ constructor(bodyInit, options = {}) {
25
+ this.bodyInit = bodyInit;
26
+ this.options = options;
27
+ this.bodyUsed = false;
28
+ this.contentType = null;
29
+ this.contentLength = null;
30
+ this._bodyFactory = () => null;
31
+ this._generatedBody = null;
32
+ const { bodyFactory, contentType, contentLength, bodyType } = processBodyInit(bodyInit);
33
+ this._bodyFactory = bodyFactory;
34
+ this.contentType = contentType;
35
+ this.contentLength = contentLength;
36
+ this.bodyType = bodyType;
37
+ }
38
+ generateBody() {
39
+ if (this._generatedBody) {
40
+ return this._generatedBody;
41
+ }
42
+ const body = this._bodyFactory();
43
+ this._generatedBody = body;
44
+ return body;
45
+ }
46
+ get body() {
47
+ const _body = this.generateBody();
48
+ if (_body != null) {
49
+ const ponyfillReadableStream = _body;
50
+ const readable = _body.readable;
51
+ return new Proxy(_body.readable, {
52
+ get(_, prop) {
53
+ if (prop in ponyfillReadableStream) {
54
+ const ponyfillReadableStreamProp = ponyfillReadableStream[prop];
55
+ if (typeof ponyfillReadableStreamProp === 'function') {
56
+ return ponyfillReadableStreamProp.bind(ponyfillReadableStream);
57
+ }
58
+ return ponyfillReadableStreamProp;
59
+ }
60
+ if (prop in readable) {
61
+ const readableProp = readable[prop];
62
+ if (typeof readableProp === 'function') {
63
+ return readableProp.bind(readable);
64
+ }
65
+ return readableProp;
66
+ }
67
+ },
68
+ });
69
+ }
70
+ return null;
71
+ }
72
+ async arrayBuffer() {
73
+ if (this.bodyType === BodyInitType.ArrayBuffer) {
74
+ return this.bodyInit;
75
+ }
76
+ if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
77
+ const typedBodyInit = this.bodyInit;
78
+ return (0, utils_js_1.uint8ArrayToArrayBuffer)(typedBodyInit);
79
+ }
80
+ if (this.bodyType === BodyInitType.String) {
81
+ const buffer = Buffer.from(this.bodyInit);
82
+ return (0, utils_js_1.uint8ArrayToArrayBuffer)(buffer);
83
+ }
84
+ if (this.bodyType === BodyInitType.Blob) {
85
+ const blob = this.bodyInit;
86
+ const arrayBuffer = await blob.arrayBuffer();
87
+ return arrayBuffer;
88
+ }
89
+ const blob = await this.blob();
90
+ return blob.arrayBuffer();
91
+ }
92
+ async _collectChunksFromReadable() {
93
+ const chunks = [];
94
+ const _body = this.generateBody();
95
+ if (_body) {
96
+ for await (const chunk of _body.readable) {
97
+ chunks.push(chunk);
98
+ }
99
+ }
100
+ return chunks;
101
+ }
102
+ async blob() {
103
+ if (this.bodyType === BodyInitType.Blob) {
104
+ return this.bodyInit;
105
+ }
106
+ if (this.bodyType === BodyInitType.String ||
107
+ this.bodyType === BodyInitType.Buffer ||
108
+ this.bodyType === BodyInitType.Uint8Array) {
109
+ const bodyInitTyped = this.bodyInit;
110
+ return new Blob_js_1.PonyfillBlob([bodyInitTyped], {
111
+ type: this.contentType || '',
112
+ });
113
+ }
114
+ if (this.bodyType === BodyInitType.ArrayBuffer) {
115
+ const bodyInitTyped = this.bodyInit;
116
+ const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength);
117
+ return new Blob_js_1.PonyfillBlob([buf], {
118
+ type: this.contentType || '',
119
+ });
120
+ }
121
+ const chunks = await this._collectChunksFromReadable();
122
+ return new Blob_js_1.PonyfillBlob(chunks, {
123
+ type: this.contentType || '',
124
+ });
125
+ }
126
+ formData(opts) {
127
+ if (this.bodyType === BodyInitType.FormData) {
128
+ return Promise.resolve(this.bodyInit);
129
+ }
130
+ const formData = new FormData_js_1.PonyfillFormData();
131
+ const _body = this.generateBody();
132
+ if (_body == null) {
133
+ return Promise.resolve(formData);
134
+ }
135
+ const formDataLimits = {
136
+ ...this.options.formDataLimits,
137
+ ...opts === null || opts === void 0 ? void 0 : opts.formDataLimits,
138
+ };
139
+ return new Promise((resolve, reject) => {
140
+ const bb = (0, busboy_1.default)({
141
+ headers: {
142
+ 'content-type': this.contentType || '',
143
+ },
144
+ limits: formDataLimits,
145
+ defParamCharset: 'utf-8',
146
+ });
147
+ bb.on('field', (name, value, { nameTruncated, valueTruncated }) => {
148
+ if (nameTruncated) {
149
+ reject(new Error(`Field name size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldNameSize} bytes`));
150
+ }
151
+ if (valueTruncated) {
152
+ reject(new Error(`Field value size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldSize} bytes`));
153
+ }
154
+ formData.set(name, value);
155
+ });
156
+ bb.on('fieldsLimit', () => {
157
+ reject(new Error(`Fields limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fields}`));
158
+ });
159
+ bb.on('file', (name, fileStream, { filename, mimeType }) => {
160
+ const chunks = [];
161
+ fileStream.on('limit', () => {
162
+ reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`));
163
+ });
164
+ fileStream.on('data', chunk => {
165
+ chunks.push(Buffer.from(chunk));
166
+ });
167
+ fileStream.on('close', () => {
168
+ if (fileStream.truncated) {
169
+ reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`));
170
+ }
171
+ const file = new File_js_1.PonyfillFile(chunks, filename, { type: mimeType });
172
+ formData.set(name, file);
173
+ });
174
+ });
175
+ bb.on('filesLimit', () => {
176
+ reject(new Error(`Files limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.files}`));
177
+ });
178
+ bb.on('partsLimit', () => {
179
+ reject(new Error(`Parts limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.parts}`));
180
+ });
181
+ bb.on('close', () => {
182
+ resolve(formData);
183
+ });
184
+ bb.on('error', err => {
185
+ reject(err);
186
+ });
187
+ _body === null || _body === void 0 ? void 0 : _body.readable.pipe(bb);
188
+ });
189
+ }
190
+ async buffer() {
191
+ if (this.bodyType === BodyInitType.Buffer) {
192
+ return this.bodyInit;
193
+ }
194
+ if (this.bodyType === BodyInitType.String) {
195
+ return Buffer.from(this.bodyInit);
196
+ }
197
+ if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) {
198
+ const bodyInitTyped = this.bodyInit;
199
+ const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
200
+ return buffer;
201
+ }
202
+ if (this.bodyType === BodyInitType.Blob) {
203
+ if (this.bodyInit instanceof Blob_js_1.PonyfillBlob) {
204
+ return this.bodyInit.buffer();
205
+ }
206
+ const bodyInitTyped = this.bodyInit;
207
+ const buffer = Buffer.from(await bodyInitTyped.arrayBuffer(), undefined, bodyInitTyped.size);
208
+ return buffer;
209
+ }
210
+ const chunks = await this._collectChunksFromReadable();
211
+ return Buffer.concat(chunks);
212
+ }
213
+ async json() {
214
+ const text = await this.text();
215
+ return JSON.parse(text);
216
+ }
217
+ async text() {
218
+ if (this.bodyType === BodyInitType.String) {
219
+ return this.bodyInit;
220
+ }
221
+ const buffer = await this.buffer();
222
+ return buffer.toString('utf-8');
223
+ }
224
+ }
225
+ exports.PonyfillBody = PonyfillBody;
226
+ function processBodyInit(bodyInit) {
227
+ if (bodyInit == null) {
228
+ return {
229
+ bodyFactory: () => null,
230
+ contentType: null,
231
+ contentLength: null,
232
+ };
233
+ }
234
+ if (typeof bodyInit === 'string') {
235
+ return {
236
+ bodyType: BodyInitType.String,
237
+ contentType: 'text/plain;charset=UTF-8',
238
+ contentLength: Buffer.byteLength(bodyInit),
239
+ bodyFactory() {
240
+ const readable = stream_1.Readable.from(bodyInit);
241
+ return new ReadableStream_js_1.PonyfillReadableStream(readable);
242
+ },
243
+ };
244
+ }
245
+ if (bodyInit instanceof ReadableStream_js_1.PonyfillReadableStream) {
246
+ return {
247
+ bodyType: BodyInitType.ReadableStream,
248
+ bodyFactory: () => bodyInit,
249
+ contentType: null,
250
+ contentLength: null,
251
+ };
252
+ }
253
+ if (bodyInit instanceof Blob_js_1.PonyfillBlob) {
254
+ return {
255
+ bodyType: BodyInitType.Blob,
256
+ contentType: bodyInit.type,
257
+ contentLength: bodyInit.size,
258
+ bodyFactory() {
259
+ return bodyInit.stream();
260
+ },
261
+ };
262
+ }
263
+ if (bodyInit instanceof Buffer) {
264
+ const contentLength = bodyInit.length;
265
+ return {
266
+ bodyType: BodyInitType.Buffer,
267
+ contentLength,
268
+ contentType: null,
269
+ bodyFactory() {
270
+ const readable = stream_1.Readable.from(bodyInit);
271
+ const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
272
+ return body;
273
+ },
274
+ };
275
+ }
276
+ if (bodyInit instanceof Uint8Array) {
277
+ const contentLength = bodyInit.byteLength;
278
+ return {
279
+ bodyType: BodyInitType.Uint8Array,
280
+ contentLength,
281
+ contentType: null,
282
+ bodyFactory() {
283
+ const readable = stream_1.Readable.from(bodyInit);
284
+ const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
285
+ return body;
286
+ },
287
+ };
288
+ }
289
+ if ('buffer' in bodyInit) {
290
+ const contentLength = bodyInit.byteLength;
291
+ return {
292
+ contentLength,
293
+ contentType: null,
294
+ bodyFactory() {
295
+ const buffer = Buffer.from(bodyInit);
296
+ const readable = stream_1.Readable.from(buffer);
297
+ const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
298
+ return body;
299
+ },
300
+ };
301
+ }
302
+ if (bodyInit instanceof ArrayBuffer) {
303
+ const contentLength = bodyInit.byteLength;
304
+ return {
305
+ bodyType: BodyInitType.ArrayBuffer,
306
+ contentType: null,
307
+ contentLength,
308
+ bodyFactory() {
309
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
310
+ const readable = stream_1.Readable.from(buffer);
311
+ const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
312
+ return body;
313
+ },
314
+ };
315
+ }
316
+ if (bodyInit instanceof stream_1.Readable) {
317
+ return {
318
+ bodyType: BodyInitType.Readable,
319
+ contentType: null,
320
+ contentLength: null,
321
+ bodyFactory() {
322
+ const body = new ReadableStream_js_1.PonyfillReadableStream(bodyInit);
323
+ return body;
324
+ },
325
+ };
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
+ if ('sort' in bodyInit) {
339
+ const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
340
+ return {
341
+ bodyType: BodyInitType.String,
342
+ contentType,
343
+ contentLength: null,
344
+ bodyFactory() {
345
+ const body = new ReadableStream_js_1.PonyfillReadableStream(stream_1.Readable.from(bodyInit.toString()));
346
+ return body;
347
+ },
348
+ };
349
+ }
350
+ if ('forEach' in bodyInit) {
351
+ const boundary = Math.random().toString(36).substr(2);
352
+ const contentType = `multipart/form-data; boundary=${boundary}`;
353
+ return {
354
+ contentType,
355
+ contentLength: null,
356
+ bodyFactory() {
357
+ return (0, FormData_js_1.getStreamFromFormData)(bodyInit, boundary);
358
+ },
359
+ };
360
+ }
361
+ if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
362
+ return {
363
+ contentType: null,
364
+ contentLength: null,
365
+ bodyFactory() {
366
+ const readable = stream_1.Readable.from(bodyInit);
367
+ return new ReadableStream_js_1.PonyfillReadableStream(readable);
368
+ },
369
+ };
370
+ }
371
+ throw new Error('Unknown body type');
372
+ }
package/cjs/File.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PonyfillFile = void 0;
4
+ const Blob_js_1 = require("./Blob.js");
5
+ class PonyfillFile extends Blob_js_1.PonyfillBlob {
6
+ constructor(fileBits, name, options) {
7
+ super(fileBits, options);
8
+ this.name = name;
9
+ this.webkitRelativePath = '';
10
+ this.lastModified = (options === null || options === void 0 ? void 0 : options.lastModified) || Date.now();
11
+ }
12
+ }
13
+ exports.PonyfillFile = PonyfillFile;