@gjsify/fetch 0.3.13 → 0.3.15

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.
@@ -1,22 +1,24 @@
1
1
  import { File } from "./blob-from.js";
2
2
  import { FormData } from "@gjsify/formdata";
3
- var S = /* @__PURE__ */ ((S2) => {
4
- S2[S2["START_BOUNDARY"] = 0] = "START_BOUNDARY";
5
- S2[S2["HEADER_FIELD_START"] = 1] = "HEADER_FIELD_START";
6
- S2[S2["HEADER_FIELD"] = 2] = "HEADER_FIELD";
7
- S2[S2["HEADER_VALUE_START"] = 3] = "HEADER_VALUE_START";
8
- S2[S2["HEADER_VALUE"] = 4] = "HEADER_VALUE";
9
- S2[S2["HEADER_VALUE_ALMOST_DONE"] = 5] = "HEADER_VALUE_ALMOST_DONE";
10
- S2[S2["HEADERS_ALMOST_DONE"] = 6] = "HEADERS_ALMOST_DONE";
11
- S2[S2["PART_DATA_START"] = 7] = "PART_DATA_START";
12
- S2[S2["PART_DATA"] = 8] = "PART_DATA";
13
- S2[S2["END"] = 9] = "END";
14
- return S2;
15
- })(S || {});
3
+
4
+ //#region src/utils/multipart-parser.ts
5
+ var S = /* @__PURE__ */ function(S) {
6
+ S[S["START_BOUNDARY"] = 0] = "START_BOUNDARY";
7
+ S[S["HEADER_FIELD_START"] = 1] = "HEADER_FIELD_START";
8
+ S[S["HEADER_FIELD"] = 2] = "HEADER_FIELD";
9
+ S[S["HEADER_VALUE_START"] = 3] = "HEADER_VALUE_START";
10
+ S[S["HEADER_VALUE"] = 4] = "HEADER_VALUE";
11
+ S[S["HEADER_VALUE_ALMOST_DONE"] = 5] = "HEADER_VALUE_ALMOST_DONE";
12
+ S[S["HEADERS_ALMOST_DONE"] = 6] = "HEADERS_ALMOST_DONE";
13
+ S[S["PART_DATA_START"] = 7] = "PART_DATA_START";
14
+ S[S["PART_DATA"] = 8] = "PART_DATA";
15
+ S[S["END"] = 9] = "END";
16
+ return S;
17
+ }(S || {});
16
18
  let f = 1;
17
19
  const F = {
18
- PART_BOUNDARY: f,
19
- LAST_BOUNDARY: f *= 2
20
+ PART_BOUNDARY: f,
21
+ LAST_BOUNDARY: f *= 2
20
22
  };
21
23
  const LF = 10;
22
24
  const CR = 13;
@@ -26,331 +28,325 @@ const COLON = 58;
26
28
  const A = 97;
27
29
  const Z = 122;
28
30
  const lower = (c) => c | 32;
29
- const noop = (..._args) => {
31
+ const noop = (..._args) => {};
32
+ var MultipartParser = class {
33
+ index = 0;
34
+ flags = 0;
35
+ boundary;
36
+ lookbehind;
37
+ state = 0;
38
+ onHeaderEnd = noop;
39
+ onHeaderField = noop;
40
+ onHeadersEnd = noop;
41
+ onHeaderValue = noop;
42
+ onPartBegin = noop;
43
+ onPartData = noop;
44
+ onPartEnd = noop;
45
+ boundaryChars = {};
46
+ /**
47
+ * @param boundary
48
+ */
49
+ constructor(boundary) {
50
+ boundary = "\r\n--" + boundary;
51
+ const ui8a = new Uint8Array(boundary.length);
52
+ for (let i = 0; i < boundary.length; i++) {
53
+ ui8a[i] = boundary.charCodeAt(i);
54
+ this.boundaryChars[ui8a[i]] = true;
55
+ }
56
+ this.boundary = ui8a;
57
+ this.lookbehind = new Uint8Array(this.boundary.length + 8);
58
+ }
59
+ /**
60
+ * @param data
61
+ */
62
+ write(data) {
63
+ let i = 0;
64
+ const length_ = data.length;
65
+ let previousIndex = this.index;
66
+ let { lookbehind, boundary, boundaryChars, index, state, flags } = this;
67
+ const boundaryLength = this.boundary.length;
68
+ const boundaryEnd = boundaryLength - 1;
69
+ const bufferLength = data.length;
70
+ let c;
71
+ let cl;
72
+ const mark = (name) => {
73
+ this[name + "Mark"] = i;
74
+ };
75
+ const clear = (name) => {
76
+ delete this[name + "Mark"];
77
+ };
78
+ const callback = (callbackSymbol, start, end, ui8a) => {
79
+ if (start === undefined || start !== end) {
80
+ this[callbackSymbol](ui8a && ui8a.subarray(start, end));
81
+ }
82
+ };
83
+ const dataCallback = (name, clear = false) => {
84
+ const markSymbol = name + "Mark";
85
+ if (!(markSymbol in this)) {
86
+ return;
87
+ }
88
+ if (clear) {
89
+ callback(name, this[markSymbol], i, data);
90
+ delete this[markSymbol];
91
+ } else {
92
+ callback(name, this[markSymbol], data.length, data);
93
+ this[markSymbol] = 0;
94
+ }
95
+ };
96
+ for (i = 0; i < length_; i++) {
97
+ c = data[i];
98
+ switch (state) {
99
+ case 0:
100
+ if (index === boundary.length - 2) {
101
+ if (c === HYPHEN) {
102
+ flags |= F.LAST_BOUNDARY;
103
+ } else if (c !== CR) {
104
+ return;
105
+ }
106
+ index++;
107
+ break;
108
+ } else if (index - 1 === boundary.length - 2) {
109
+ if (flags & F.LAST_BOUNDARY && c === HYPHEN) {
110
+ state = 9;
111
+ flags = 0;
112
+ } else if (!(flags & F.LAST_BOUNDARY) && c === LF) {
113
+ index = 0;
114
+ callback("onPartBegin");
115
+ state = 1;
116
+ } else {
117
+ return;
118
+ }
119
+ break;
120
+ }
121
+ if (c !== boundary[index + 2]) {
122
+ index = -2;
123
+ }
124
+ if (c === boundary[index + 2]) {
125
+ index++;
126
+ }
127
+ break;
128
+ case 1:
129
+ state = 2;
130
+ mark("onHeaderField");
131
+ index = 0;
132
+ case 2:
133
+ if (c === CR) {
134
+ clear("onHeaderField");
135
+ state = 6;
136
+ break;
137
+ }
138
+ index++;
139
+ if (c === HYPHEN) {
140
+ break;
141
+ }
142
+ if (c === COLON) {
143
+ if (index === 1) {
144
+ return;
145
+ }
146
+ dataCallback("onHeaderField", true);
147
+ state = 3;
148
+ break;
149
+ }
150
+ cl = lower(c);
151
+ if (cl < A || cl > Z) {
152
+ return;
153
+ }
154
+ break;
155
+ case 3:
156
+ if (c === SPACE) {
157
+ break;
158
+ }
159
+ mark("onHeaderValue");
160
+ state = 4;
161
+ case 4:
162
+ if (c === CR) {
163
+ dataCallback("onHeaderValue", true);
164
+ callback("onHeaderEnd");
165
+ state = 5;
166
+ }
167
+ break;
168
+ case 5:
169
+ if (c !== LF) {
170
+ return;
171
+ }
172
+ state = 1;
173
+ break;
174
+ case 6:
175
+ if (c !== LF) {
176
+ return;
177
+ }
178
+ callback("onHeadersEnd");
179
+ state = 7;
180
+ break;
181
+ case 7:
182
+ state = 8;
183
+ mark("onPartData");
184
+ case 8:
185
+ previousIndex = index;
186
+ if (index === 0) {
187
+ i += boundaryEnd;
188
+ while (i < bufferLength && !(data[i] in boundaryChars)) {
189
+ i += boundaryLength;
190
+ }
191
+ i -= boundaryEnd;
192
+ c = data[i];
193
+ }
194
+ if (index < boundary.length) {
195
+ if (boundary[index] === c) {
196
+ if (index === 0) {
197
+ dataCallback("onPartData", true);
198
+ }
199
+ index++;
200
+ } else {
201
+ index = 0;
202
+ }
203
+ } else if (index === boundary.length) {
204
+ index++;
205
+ if (c === CR) {
206
+ flags |= F.PART_BOUNDARY;
207
+ } else if (c === HYPHEN) {
208
+ flags |= F.LAST_BOUNDARY;
209
+ } else {
210
+ index = 0;
211
+ }
212
+ } else if (index - 1 === boundary.length) {
213
+ if (flags & F.PART_BOUNDARY) {
214
+ index = 0;
215
+ if (c === LF) {
216
+ flags &= ~F.PART_BOUNDARY;
217
+ callback("onPartEnd");
218
+ callback("onPartBegin");
219
+ state = 1;
220
+ break;
221
+ }
222
+ } else if (flags & F.LAST_BOUNDARY) {
223
+ if (c === HYPHEN) {
224
+ callback("onPartEnd");
225
+ state = 9;
226
+ flags = 0;
227
+ } else {
228
+ index = 0;
229
+ }
230
+ } else {
231
+ index = 0;
232
+ }
233
+ }
234
+ if (index > 0) {
235
+ lookbehind[index - 1] = c;
236
+ } else if (previousIndex > 0) {
237
+ const _lookbehind = new Uint8Array(lookbehind.buffer, lookbehind.byteOffset, lookbehind.byteLength);
238
+ callback("onPartData", 0, previousIndex, _lookbehind);
239
+ previousIndex = 0;
240
+ mark("onPartData");
241
+ i--;
242
+ }
243
+ break;
244
+ case 9: break;
245
+ default: throw new Error(`Unexpected state entered: ${state}`);
246
+ }
247
+ }
248
+ dataCallback("onHeaderField");
249
+ dataCallback("onHeaderValue");
250
+ dataCallback("onPartData");
251
+ this.index = index;
252
+ this.state = state;
253
+ this.flags = flags;
254
+ }
255
+ end() {
256
+ if (this.state === 1 && this.index === 0 || this.state === 8 && this.index === this.boundary.length) {
257
+ this.onPartEnd();
258
+ } else if (this.state !== 9) {
259
+ throw new Error("MultipartParser.end(): stream ended unexpectedly");
260
+ }
261
+ }
30
262
  };
31
- class MultipartParser {
32
- index = 0;
33
- flags = 0;
34
- boundary;
35
- lookbehind;
36
- state = 0 /* START_BOUNDARY */;
37
- onHeaderEnd = noop;
38
- onHeaderField = noop;
39
- onHeadersEnd = noop;
40
- onHeaderValue = noop;
41
- onPartBegin = noop;
42
- onPartData = noop;
43
- onPartEnd = noop;
44
- boundaryChars = {};
45
- /**
46
- * @param boundary
47
- */
48
- constructor(boundary) {
49
- boundary = "\r\n--" + boundary;
50
- const ui8a = new Uint8Array(boundary.length);
51
- for (let i = 0; i < boundary.length; i++) {
52
- ui8a[i] = boundary.charCodeAt(i);
53
- this.boundaryChars[ui8a[i]] = true;
54
- }
55
- this.boundary = ui8a;
56
- this.lookbehind = new Uint8Array(this.boundary.length + 8);
57
- }
58
- /**
59
- * @param data
60
- */
61
- write(data) {
62
- let i = 0;
63
- const length_ = data.length;
64
- let previousIndex = this.index;
65
- let { lookbehind, boundary, boundaryChars, index, state, flags } = this;
66
- const boundaryLength = this.boundary.length;
67
- const boundaryEnd = boundaryLength - 1;
68
- const bufferLength = data.length;
69
- let c;
70
- let cl;
71
- const mark = (name) => {
72
- this[name + "Mark"] = i;
73
- };
74
- const clear = (name) => {
75
- delete this[name + "Mark"];
76
- };
77
- const callback = (callbackSymbol, start, end, ui8a) => {
78
- if (start === void 0 || start !== end) {
79
- this[callbackSymbol](ui8a && ui8a.subarray(start, end));
80
- }
81
- };
82
- const dataCallback = (name, clear2 = false) => {
83
- const markSymbol = name + "Mark";
84
- if (!(markSymbol in this)) {
85
- return;
86
- }
87
- if (clear2) {
88
- callback(name, this[markSymbol], i, data);
89
- delete this[markSymbol];
90
- } else {
91
- callback(name, this[markSymbol], data.length, data);
92
- this[markSymbol] = 0;
93
- }
94
- };
95
- for (i = 0; i < length_; i++) {
96
- c = data[i];
97
- switch (state) {
98
- case 0 /* START_BOUNDARY */:
99
- if (index === boundary.length - 2) {
100
- if (c === HYPHEN) {
101
- flags |= F.LAST_BOUNDARY;
102
- } else if (c !== CR) {
103
- return;
104
- }
105
- index++;
106
- break;
107
- } else if (index - 1 === boundary.length - 2) {
108
- if (flags & F.LAST_BOUNDARY && c === HYPHEN) {
109
- state = 9 /* END */;
110
- flags = 0;
111
- } else if (!(flags & F.LAST_BOUNDARY) && c === LF) {
112
- index = 0;
113
- callback("onPartBegin");
114
- state = 1 /* HEADER_FIELD_START */;
115
- } else {
116
- return;
117
- }
118
- break;
119
- }
120
- if (c !== boundary[index + 2]) {
121
- index = -2;
122
- }
123
- if (c === boundary[index + 2]) {
124
- index++;
125
- }
126
- break;
127
- case 1 /* HEADER_FIELD_START */:
128
- state = 2 /* HEADER_FIELD */;
129
- mark("onHeaderField");
130
- index = 0;
131
- // falls through
132
- case 2 /* HEADER_FIELD */:
133
- if (c === CR) {
134
- clear("onHeaderField");
135
- state = 6 /* HEADERS_ALMOST_DONE */;
136
- break;
137
- }
138
- index++;
139
- if (c === HYPHEN) {
140
- break;
141
- }
142
- if (c === COLON) {
143
- if (index === 1) {
144
- return;
145
- }
146
- dataCallback("onHeaderField", true);
147
- state = 3 /* HEADER_VALUE_START */;
148
- break;
149
- }
150
- cl = lower(c);
151
- if (cl < A || cl > Z) {
152
- return;
153
- }
154
- break;
155
- case 3 /* HEADER_VALUE_START */:
156
- if (c === SPACE) {
157
- break;
158
- }
159
- mark("onHeaderValue");
160
- state = 4 /* HEADER_VALUE */;
161
- // falls through
162
- case 4 /* HEADER_VALUE */:
163
- if (c === CR) {
164
- dataCallback("onHeaderValue", true);
165
- callback("onHeaderEnd");
166
- state = 5 /* HEADER_VALUE_ALMOST_DONE */;
167
- }
168
- break;
169
- case 5 /* HEADER_VALUE_ALMOST_DONE */:
170
- if (c !== LF) {
171
- return;
172
- }
173
- state = 1 /* HEADER_FIELD_START */;
174
- break;
175
- case 6 /* HEADERS_ALMOST_DONE */:
176
- if (c !== LF) {
177
- return;
178
- }
179
- callback("onHeadersEnd");
180
- state = 7 /* PART_DATA_START */;
181
- break;
182
- case 7 /* PART_DATA_START */:
183
- state = 8 /* PART_DATA */;
184
- mark("onPartData");
185
- // falls through
186
- case 8 /* PART_DATA */:
187
- previousIndex = index;
188
- if (index === 0) {
189
- i += boundaryEnd;
190
- while (i < bufferLength && !(data[i] in boundaryChars)) {
191
- i += boundaryLength;
192
- }
193
- i -= boundaryEnd;
194
- c = data[i];
195
- }
196
- if (index < boundary.length) {
197
- if (boundary[index] === c) {
198
- if (index === 0) {
199
- dataCallback("onPartData", true);
200
- }
201
- index++;
202
- } else {
203
- index = 0;
204
- }
205
- } else if (index === boundary.length) {
206
- index++;
207
- if (c === CR) {
208
- flags |= F.PART_BOUNDARY;
209
- } else if (c === HYPHEN) {
210
- flags |= F.LAST_BOUNDARY;
211
- } else {
212
- index = 0;
213
- }
214
- } else if (index - 1 === boundary.length) {
215
- if (flags & F.PART_BOUNDARY) {
216
- index = 0;
217
- if (c === LF) {
218
- flags &= ~F.PART_BOUNDARY;
219
- callback("onPartEnd");
220
- callback("onPartBegin");
221
- state = 1 /* HEADER_FIELD_START */;
222
- break;
223
- }
224
- } else if (flags & F.LAST_BOUNDARY) {
225
- if (c === HYPHEN) {
226
- callback("onPartEnd");
227
- state = 9 /* END */;
228
- flags = 0;
229
- } else {
230
- index = 0;
231
- }
232
- } else {
233
- index = 0;
234
- }
235
- }
236
- if (index > 0) {
237
- lookbehind[index - 1] = c;
238
- } else if (previousIndex > 0) {
239
- const _lookbehind = new Uint8Array(lookbehind.buffer, lookbehind.byteOffset, lookbehind.byteLength);
240
- callback("onPartData", 0, previousIndex, _lookbehind);
241
- previousIndex = 0;
242
- mark("onPartData");
243
- i--;
244
- }
245
- break;
246
- case 9 /* END */:
247
- break;
248
- default:
249
- throw new Error(`Unexpected state entered: ${state}`);
250
- }
251
- }
252
- dataCallback("onHeaderField");
253
- dataCallback("onHeaderValue");
254
- dataCallback("onPartData");
255
- this.index = index;
256
- this.state = state;
257
- this.flags = flags;
258
- }
259
- end() {
260
- if (this.state === 1 /* HEADER_FIELD_START */ && this.index === 0 || this.state === 8 /* PART_DATA */ && this.index === this.boundary.length) {
261
- this.onPartEnd();
262
- } else if (this.state !== 9 /* END */) {
263
- throw new Error("MultipartParser.end(): stream ended unexpectedly");
264
- }
265
- }
266
- }
267
263
  function _fileName(headerValue) {
268
- const m = headerValue.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);
269
- if (!m) {
270
- return;
271
- }
272
- const match = m[2] || m[3] || "";
273
- let filename = match.slice(match.lastIndexOf("\\") + 1);
274
- filename = filename.replace(/%22/g, '"');
275
- filename = filename.replace(/&#(\d{4});/g, (m2, code) => {
276
- return String.fromCharCode(code);
277
- });
278
- return filename;
264
+ const m = headerValue.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);
265
+ if (!m) {
266
+ return;
267
+ }
268
+ const match = m[2] || m[3] || "";
269
+ let filename = match.slice(match.lastIndexOf("\\") + 1);
270
+ filename = filename.replace(/%22/g, "\"");
271
+ filename = filename.replace(/&#(\d{4});/g, (m, code) => {
272
+ return String.fromCharCode(code);
273
+ });
274
+ return filename;
279
275
  }
280
276
  async function toFormData(Body, ct) {
281
- if (!/multipart/i.test(ct)) {
282
- throw new TypeError("Failed to fetch");
283
- }
284
- const m = ct.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
285
- if (!m) {
286
- throw new TypeError("no or bad content-type header, no multipart boundary");
287
- }
288
- const parser = new MultipartParser(m[1] || m[2]);
289
- let headerField;
290
- let headerValue;
291
- let entryValue;
292
- let entryName;
293
- let contentType;
294
- let filename;
295
- const entryChunks = [];
296
- const formData = new FormData();
297
- const onPartData = (ui8a) => {
298
- entryValue += decoder.decode(ui8a, { stream: true });
299
- };
300
- const appendToFile = (ui8a) => {
301
- entryChunks.push(ui8a);
302
- };
303
- const appendFileToFormData = () => {
304
- const file = new File(entryChunks, filename, { type: contentType });
305
- formData.append(entryName, file);
306
- };
307
- const appendEntryToFormData = () => {
308
- formData.append(entryName, entryValue);
309
- };
310
- const decoder = new TextDecoder("utf-8");
311
- decoder.decode();
312
- parser.onPartBegin = function() {
313
- parser.onPartData = onPartData;
314
- parser.onPartEnd = appendEntryToFormData;
315
- headerField = "";
316
- headerValue = "";
317
- entryValue = "";
318
- entryName = "";
319
- contentType = "";
320
- filename = null;
321
- entryChunks.length = 0;
322
- };
323
- parser.onHeaderField = function(ui8a) {
324
- headerField += decoder.decode(ui8a, { stream: true });
325
- };
326
- parser.onHeaderValue = function(ui8a) {
327
- headerValue += decoder.decode(ui8a, { stream: true });
328
- };
329
- parser.onHeaderEnd = function() {
330
- headerValue += decoder.decode();
331
- headerField = headerField.toLowerCase();
332
- if (headerField === "content-disposition") {
333
- const m2 = headerValue.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);
334
- if (m2) {
335
- entryName = m2[2] || m2[3] || "";
336
- }
337
- filename = _fileName(headerValue);
338
- if (filename) {
339
- parser.onPartData = appendToFile;
340
- parser.onPartEnd = appendFileToFormData;
341
- }
342
- } else if (headerField === "content-type") {
343
- contentType = headerValue;
344
- }
345
- headerValue = "";
346
- headerField = "";
347
- };
348
- for await (const chunk of Body) {
349
- parser.write(chunk);
350
- }
351
- parser.end();
352
- return formData;
277
+ if (!/multipart/i.test(ct)) {
278
+ throw new TypeError("Failed to fetch");
279
+ }
280
+ const m = ct.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
281
+ if (!m) {
282
+ throw new TypeError("no or bad content-type header, no multipart boundary");
283
+ }
284
+ const parser = new MultipartParser(m[1] || m[2]);
285
+ let headerField;
286
+ let headerValue;
287
+ let entryValue;
288
+ let entryName;
289
+ let contentType;
290
+ let filename;
291
+ const entryChunks = [];
292
+ const formData = new FormData();
293
+ const onPartData = (ui8a) => {
294
+ entryValue += decoder.decode(ui8a, { stream: true });
295
+ };
296
+ const appendToFile = (ui8a) => {
297
+ entryChunks.push(ui8a);
298
+ };
299
+ const appendFileToFormData = () => {
300
+ const file = new File(entryChunks, filename, { type: contentType });
301
+ formData.append(entryName, file);
302
+ };
303
+ const appendEntryToFormData = () => {
304
+ formData.append(entryName, entryValue);
305
+ };
306
+ const decoder = new TextDecoder("utf-8");
307
+ decoder.decode();
308
+ parser.onPartBegin = function() {
309
+ parser.onPartData = onPartData;
310
+ parser.onPartEnd = appendEntryToFormData;
311
+ headerField = "";
312
+ headerValue = "";
313
+ entryValue = "";
314
+ entryName = "";
315
+ contentType = "";
316
+ filename = null;
317
+ entryChunks.length = 0;
318
+ };
319
+ parser.onHeaderField = function(ui8a) {
320
+ headerField += decoder.decode(ui8a, { stream: true });
321
+ };
322
+ parser.onHeaderValue = function(ui8a) {
323
+ headerValue += decoder.decode(ui8a, { stream: true });
324
+ };
325
+ parser.onHeaderEnd = function() {
326
+ headerValue += decoder.decode();
327
+ headerField = headerField.toLowerCase();
328
+ if (headerField === "content-disposition") {
329
+ const m = headerValue.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);
330
+ if (m) {
331
+ entryName = m[2] || m[3] || "";
332
+ }
333
+ filename = _fileName(headerValue);
334
+ if (filename) {
335
+ parser.onPartData = appendToFile;
336
+ parser.onPartEnd = appendFileToFormData;
337
+ }
338
+ } else if (headerField === "content-type") {
339
+ contentType = headerValue;
340
+ }
341
+ headerValue = "";
342
+ headerField = "";
343
+ };
344
+ for await (const chunk of Body) {
345
+ parser.write(chunk);
346
+ }
347
+ parser.end();
348
+ return formData;
353
349
  }
354
- export {
355
- toFormData
356
- };
350
+
351
+ //#endregion
352
+ export { toFormData };