@depup/node-fetch 3.3.2-depup.0

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,432 @@
1
+ import {File} from 'fetch-blob/from.js';
2
+ import {FormData} from 'formdata-polyfill/esm.min.js';
3
+
4
+ let s = 0;
5
+ const S = {
6
+ START_BOUNDARY: s++,
7
+ HEADER_FIELD_START: s++,
8
+ HEADER_FIELD: s++,
9
+ HEADER_VALUE_START: s++,
10
+ HEADER_VALUE: s++,
11
+ HEADER_VALUE_ALMOST_DONE: s++,
12
+ HEADERS_ALMOST_DONE: s++,
13
+ PART_DATA_START: s++,
14
+ PART_DATA: s++,
15
+ END: s++
16
+ };
17
+
18
+ let f = 1;
19
+ const F = {
20
+ PART_BOUNDARY: f,
21
+ LAST_BOUNDARY: f *= 2
22
+ };
23
+
24
+ const LF = 10;
25
+ const CR = 13;
26
+ const SPACE = 32;
27
+ const HYPHEN = 45;
28
+ const COLON = 58;
29
+ const A = 97;
30
+ const Z = 122;
31
+
32
+ const lower = c => c | 0x20;
33
+
34
+ const noop = () => {};
35
+
36
+ class MultipartParser {
37
+ /**
38
+ * @param {string} boundary
39
+ */
40
+ constructor(boundary) {
41
+ this.index = 0;
42
+ this.flags = 0;
43
+
44
+ this.onHeaderEnd = noop;
45
+ this.onHeaderField = noop;
46
+ this.onHeadersEnd = noop;
47
+ this.onHeaderValue = noop;
48
+ this.onPartBegin = noop;
49
+ this.onPartData = noop;
50
+ this.onPartEnd = noop;
51
+
52
+ this.boundaryChars = {};
53
+
54
+ boundary = '\r\n--' + boundary;
55
+ const ui8a = new Uint8Array(boundary.length);
56
+ for (let i = 0; i < boundary.length; i++) {
57
+ ui8a[i] = boundary.charCodeAt(i);
58
+ this.boundaryChars[ui8a[i]] = true;
59
+ }
60
+
61
+ this.boundary = ui8a;
62
+ this.lookbehind = new Uint8Array(this.boundary.length + 8);
63
+ this.state = S.START_BOUNDARY;
64
+ }
65
+
66
+ /**
67
+ * @param {Uint8Array} data
68
+ */
69
+ write(data) {
70
+ let i = 0;
71
+ const length_ = data.length;
72
+ let previousIndex = this.index;
73
+ let {lookbehind, boundary, boundaryChars, index, state, flags} = this;
74
+ const boundaryLength = this.boundary.length;
75
+ const boundaryEnd = boundaryLength - 1;
76
+ const bufferLength = data.length;
77
+ let c;
78
+ let cl;
79
+
80
+ const mark = name => {
81
+ this[name + 'Mark'] = i;
82
+ };
83
+
84
+ const clear = name => {
85
+ delete this[name + 'Mark'];
86
+ };
87
+
88
+ const callback = (callbackSymbol, start, end, ui8a) => {
89
+ if (start === undefined || start !== end) {
90
+ this[callbackSymbol](ui8a && ui8a.subarray(start, end));
91
+ }
92
+ };
93
+
94
+ const dataCallback = (name, clear) => {
95
+ const markSymbol = name + 'Mark';
96
+ if (!(markSymbol in this)) {
97
+ return;
98
+ }
99
+
100
+ if (clear) {
101
+ callback(name, this[markSymbol], i, data);
102
+ delete this[markSymbol];
103
+ } else {
104
+ callback(name, this[markSymbol], data.length, data);
105
+ this[markSymbol] = 0;
106
+ }
107
+ };
108
+
109
+ for (i = 0; i < length_; i++) {
110
+ c = data[i];
111
+
112
+ switch (state) {
113
+ case S.START_BOUNDARY:
114
+ if (index === boundary.length - 2) {
115
+ if (c === HYPHEN) {
116
+ flags |= F.LAST_BOUNDARY;
117
+ } else if (c !== CR) {
118
+ return;
119
+ }
120
+
121
+ index++;
122
+ break;
123
+ } else if (index - 1 === boundary.length - 2) {
124
+ if (flags & F.LAST_BOUNDARY && c === HYPHEN) {
125
+ state = S.END;
126
+ flags = 0;
127
+ } else if (!(flags & F.LAST_BOUNDARY) && c === LF) {
128
+ index = 0;
129
+ callback('onPartBegin');
130
+ state = S.HEADER_FIELD_START;
131
+ } else {
132
+ return;
133
+ }
134
+
135
+ break;
136
+ }
137
+
138
+ if (c !== boundary[index + 2]) {
139
+ index = -2;
140
+ }
141
+
142
+ if (c === boundary[index + 2]) {
143
+ index++;
144
+ }
145
+
146
+ break;
147
+ case S.HEADER_FIELD_START:
148
+ state = S.HEADER_FIELD;
149
+ mark('onHeaderField');
150
+ index = 0;
151
+ // falls through
152
+ case S.HEADER_FIELD:
153
+ if (c === CR) {
154
+ clear('onHeaderField');
155
+ state = S.HEADERS_ALMOST_DONE;
156
+ break;
157
+ }
158
+
159
+ index++;
160
+ if (c === HYPHEN) {
161
+ break;
162
+ }
163
+
164
+ if (c === COLON) {
165
+ if (index === 1) {
166
+ // empty header field
167
+ return;
168
+ }
169
+
170
+ dataCallback('onHeaderField', true);
171
+ state = S.HEADER_VALUE_START;
172
+ break;
173
+ }
174
+
175
+ cl = lower(c);
176
+ if (cl < A || cl > Z) {
177
+ return;
178
+ }
179
+
180
+ break;
181
+ case S.HEADER_VALUE_START:
182
+ if (c === SPACE) {
183
+ break;
184
+ }
185
+
186
+ mark('onHeaderValue');
187
+ state = S.HEADER_VALUE;
188
+ // falls through
189
+ case S.HEADER_VALUE:
190
+ if (c === CR) {
191
+ dataCallback('onHeaderValue', true);
192
+ callback('onHeaderEnd');
193
+ state = S.HEADER_VALUE_ALMOST_DONE;
194
+ }
195
+
196
+ break;
197
+ case S.HEADER_VALUE_ALMOST_DONE:
198
+ if (c !== LF) {
199
+ return;
200
+ }
201
+
202
+ state = S.HEADER_FIELD_START;
203
+ break;
204
+ case S.HEADERS_ALMOST_DONE:
205
+ if (c !== LF) {
206
+ return;
207
+ }
208
+
209
+ callback('onHeadersEnd');
210
+ state = S.PART_DATA_START;
211
+ break;
212
+ case S.PART_DATA_START:
213
+ state = S.PART_DATA;
214
+ mark('onPartData');
215
+ // falls through
216
+ case S.PART_DATA:
217
+ previousIndex = index;
218
+
219
+ if (index === 0) {
220
+ // boyer-moore derrived algorithm to safely skip non-boundary data
221
+ i += boundaryEnd;
222
+ while (i < bufferLength && !(data[i] in boundaryChars)) {
223
+ i += boundaryLength;
224
+ }
225
+
226
+ i -= boundaryEnd;
227
+ c = data[i];
228
+ }
229
+
230
+ if (index < boundary.length) {
231
+ if (boundary[index] === c) {
232
+ if (index === 0) {
233
+ dataCallback('onPartData', true);
234
+ }
235
+
236
+ index++;
237
+ } else {
238
+ index = 0;
239
+ }
240
+ } else if (index === boundary.length) {
241
+ index++;
242
+ if (c === CR) {
243
+ // CR = part boundary
244
+ flags |= F.PART_BOUNDARY;
245
+ } else if (c === HYPHEN) {
246
+ // HYPHEN = end boundary
247
+ flags |= F.LAST_BOUNDARY;
248
+ } else {
249
+ index = 0;
250
+ }
251
+ } else if (index - 1 === boundary.length) {
252
+ if (flags & F.PART_BOUNDARY) {
253
+ index = 0;
254
+ if (c === LF) {
255
+ // unset the PART_BOUNDARY flag
256
+ flags &= ~F.PART_BOUNDARY;
257
+ callback('onPartEnd');
258
+ callback('onPartBegin');
259
+ state = S.HEADER_FIELD_START;
260
+ break;
261
+ }
262
+ } else if (flags & F.LAST_BOUNDARY) {
263
+ if (c === HYPHEN) {
264
+ callback('onPartEnd');
265
+ state = S.END;
266
+ flags = 0;
267
+ } else {
268
+ index = 0;
269
+ }
270
+ } else {
271
+ index = 0;
272
+ }
273
+ }
274
+
275
+ if (index > 0) {
276
+ // when matching a possible boundary, keep a lookbehind reference
277
+ // in case it turns out to be a false lead
278
+ lookbehind[index - 1] = c;
279
+ } else if (previousIndex > 0) {
280
+ // if our boundary turned out to be rubbish, the captured lookbehind
281
+ // belongs to partData
282
+ const _lookbehind = new Uint8Array(lookbehind.buffer, lookbehind.byteOffset, lookbehind.byteLength);
283
+ callback('onPartData', 0, previousIndex, _lookbehind);
284
+ previousIndex = 0;
285
+ mark('onPartData');
286
+
287
+ // reconsider the current character even so it interrupted the sequence
288
+ // it could be the beginning of a new sequence
289
+ i--;
290
+ }
291
+
292
+ break;
293
+ case S.END:
294
+ break;
295
+ default:
296
+ throw new Error(`Unexpected state entered: ${state}`);
297
+ }
298
+ }
299
+
300
+ dataCallback('onHeaderField');
301
+ dataCallback('onHeaderValue');
302
+ dataCallback('onPartData');
303
+
304
+ // Update properties for the next call
305
+ this.index = index;
306
+ this.state = state;
307
+ this.flags = flags;
308
+ }
309
+
310
+ end() {
311
+ if ((this.state === S.HEADER_FIELD_START && this.index === 0) ||
312
+ (this.state === S.PART_DATA && this.index === this.boundary.length)) {
313
+ this.onPartEnd();
314
+ } else if (this.state !== S.END) {
315
+ throw new Error('MultipartParser.end(): stream ended unexpectedly');
316
+ }
317
+ }
318
+ }
319
+
320
+ function _fileName(headerValue) {
321
+ // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
322
+ const m = headerValue.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);
323
+ if (!m) {
324
+ return;
325
+ }
326
+
327
+ const match = m[2] || m[3] || '';
328
+ let filename = match.slice(match.lastIndexOf('\\') + 1);
329
+ filename = filename.replace(/%22/g, '"');
330
+ filename = filename.replace(/&#(\d{4});/g, (m, code) => {
331
+ return String.fromCharCode(code);
332
+ });
333
+ return filename;
334
+ }
335
+
336
+ export async function toFormData(Body, ct) {
337
+ if (!/multipart/i.test(ct)) {
338
+ throw new TypeError('Failed to fetch');
339
+ }
340
+
341
+ const m = ct.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
342
+
343
+ if (!m) {
344
+ throw new TypeError('no or bad content-type header, no multipart boundary');
345
+ }
346
+
347
+ const parser = new MultipartParser(m[1] || m[2]);
348
+
349
+ let headerField;
350
+ let headerValue;
351
+ let entryValue;
352
+ let entryName;
353
+ let contentType;
354
+ let filename;
355
+ const entryChunks = [];
356
+ const formData = new FormData();
357
+
358
+ const onPartData = ui8a => {
359
+ entryValue += decoder.decode(ui8a, {stream: true});
360
+ };
361
+
362
+ const appendToFile = ui8a => {
363
+ entryChunks.push(ui8a);
364
+ };
365
+
366
+ const appendFileToFormData = () => {
367
+ const file = new File(entryChunks, filename, {type: contentType});
368
+ formData.append(entryName, file);
369
+ };
370
+
371
+ const appendEntryToFormData = () => {
372
+ formData.append(entryName, entryValue);
373
+ };
374
+
375
+ const decoder = new TextDecoder('utf-8');
376
+ decoder.decode();
377
+
378
+ parser.onPartBegin = function () {
379
+ parser.onPartData = onPartData;
380
+ parser.onPartEnd = appendEntryToFormData;
381
+
382
+ headerField = '';
383
+ headerValue = '';
384
+ entryValue = '';
385
+ entryName = '';
386
+ contentType = '';
387
+ filename = null;
388
+ entryChunks.length = 0;
389
+ };
390
+
391
+ parser.onHeaderField = function (ui8a) {
392
+ headerField += decoder.decode(ui8a, {stream: true});
393
+ };
394
+
395
+ parser.onHeaderValue = function (ui8a) {
396
+ headerValue += decoder.decode(ui8a, {stream: true});
397
+ };
398
+
399
+ parser.onHeaderEnd = function () {
400
+ headerValue += decoder.decode();
401
+ headerField = headerField.toLowerCase();
402
+
403
+ if (headerField === 'content-disposition') {
404
+ // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
405
+ const m = headerValue.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);
406
+
407
+ if (m) {
408
+ entryName = m[2] || m[3] || '';
409
+ }
410
+
411
+ filename = _fileName(headerValue);
412
+
413
+ if (filename) {
414
+ parser.onPartData = appendToFile;
415
+ parser.onPartEnd = appendFileToFormData;
416
+ }
417
+ } else if (headerField === 'content-type') {
418
+ contentType = headerValue;
419
+ }
420
+
421
+ headerValue = '';
422
+ headerField = '';
423
+ };
424
+
425
+ for await (const chunk of Body) {
426
+ parser.write(chunk);
427
+ }
428
+
429
+ parser.end();
430
+
431
+ return formData;
432
+ }