@sveltejs/kit 1.0.0-next.201 → 1.0.0-next.202

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