@gjsify/fetch 0.0.2

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