@sveltejs/adapter-netlify 1.0.0-next.6 → 1.0.0-next.60

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.
package/README.md CHANGED
@@ -1,18 +1,87 @@
1
1
  # adapter-netlify
2
2
 
3
- Adapter for Svelte apps that creates a Netlify app, using a function for dynamic server rendering. A future version might use a function per route, though it's unclear if that has any real advantages.
3
+ A SvelteKit adapter that creates a Netlify app.
4
4
 
5
- This is very experimental; the adapter API isn't at all fleshed out, and things will definitely change.
5
+ If you're using [adapter-auto](../adapter-auto), you don't need to install this unless you need to specify Netlify-specific options, since it's already included.
6
6
 
7
- ## Configuration
7
+ ## Installation
8
8
 
9
- This adapter expects to find a [netlify.toml](https://docs.netlify.com/configure-builds/file-based-configuration) file in the project root. It will determine where to write static assets and functions to based on the `build.publish` and `build.functions` settings, as per this sample configuration:
9
+ ```bash
10
+ npm i -D @sveltejs/adapter-netlify
11
+ ```
12
+
13
+ You can then configure it inside of `svelte.config.js`:
14
+
15
+ ```js
16
+ import adapter from '@sveltejs/adapter-netlify';
17
+
18
+ export default {
19
+ kit: {
20
+ // default options are shown
21
+ adapter: adapter({
22
+ // if true, will create a Netlify Edge Function rather
23
+ // than using standard Node-based functions
24
+ edge: false,
25
+
26
+ // if true, will split your app into multiple functions
27
+ // instead of creating a single one for the entire app.
28
+ // if `edge` is true, this option cannot be used
29
+ split: false
30
+ })
31
+ }
32
+ };
33
+ ```
34
+
35
+ Then, make sure you have a [netlify.toml](https://docs.netlify.com/configure-builds/file-based-configuration) file in the project root. This will determine where to write static assets based on the `build.publish` settings, as per this sample configuration:
10
36
 
11
37
  ```toml
12
38
  [build]
13
39
  command = "npm run build"
14
- publish = "build/"
15
- functions = "functions/"
40
+ publish = "build"
16
41
  ```
17
42
 
18
- It's recommended that you add the `build` and `functions` folders (or whichever other folders you specify) to your `.gitignore`.
43
+ If the `netlify.toml` file or the `build.publish` value is missing, a default value of `"build"` will be used. Note that if you have set the publish directory in the Netlify UI to something else then you will need to set it in `netlify.toml` too, or use the default value of `"build"`.
44
+
45
+ ### Node version
46
+
47
+ New projects will use Node 16 by default. However, if you're upgrading a project you created a while ago it may be stuck on an older version. See [the Netlify docs](https://docs.netlify.com/configure-builds/manage-dependencies/#node-js-and-javascript) for details on manually specifying Node 16 or newer.
48
+
49
+ ## Netlify Edge Functions (beta)
50
+
51
+ SvelteKit supports the beta release of Netlify Edge Functions. If you pass the option `edge: true` to the `adapter` function, server-side rendering will happen in a Deno-based edge function that's deployed close to the site visitor. If set to `false` (the default), the site will deploy to standard Node-based Netlify Functions.
52
+
53
+ ## Netlify alternatives to SvelteKit functionality
54
+
55
+ You may build your app using functionality provided directly by SvelteKit without relying on any Netlify functionality. Using the SvelteKit versions of these features will allow them to be used in dev mode, tested with integration tests, and to work with other adapters should you ever decide to switch away from Netlify. However, in some scenarios you may find it beneficial to use the Netlify versions of these features. One example would be if you're migrating an app that's already hosted on Netlify to SvelteKit.
56
+
57
+ ### Using Netlify Redirect Rules
58
+
59
+ During compilation, redirect rules are automatically appended to your `_redirects` file. (If it doesn't exist yet, it will be created.) That means:
60
+
61
+ - `[[redirects]]` in `netlify.toml` will never match as `_redirects` has a [higher priority](https://docs.netlify.com/routing/redirects/#rule-processing-order). So always put your rules in the [`_redirects` file](https://docs.netlify.com/routing/redirects/#syntax-for-the-redirects-file).
62
+ - `_redirects` shouldn't have any custom "catch all" rules such as `/* /foobar/:splat`. Otherwise the automatically appended rule will never be applied as Netlify is only processing [the first matching rule](https://docs.netlify.com/routing/redirects/#rule-processing-order).
63
+
64
+ ### Using Netlify Forms
65
+
66
+ 1. Create your Netlify HTML form as described [here](https://docs.netlify.com/forms/setup/#html-forms), e.g. as `/routes/contact.svelte`. (Don't forget to add the hidden `form-name` input element!)
67
+ 2. Netlify's build bot parses your HTML files at deploy time, which means your form must be [prerendered](https://kit.svelte.dev/docs/page-options#prerender) as HTML. You can either add `export const prerender = true` to your `contact.svelte` to prerender just that page or set the `kit.prerender.force: true` option to prerender all pages.
68
+ 3. If your Netlify form has a [custom success message](https://docs.netlify.com/forms/setup/#success-messages) like `<form netlify ... action="/success">` then ensure the corresponding `/routes/success.svelte` exists and is prerendered.
69
+
70
+ ### Using Netlify Functions
71
+
72
+ With this adapter, SvelteKit endpoints are hosted as [Netlify Functions](https://docs.netlify.com/functions/overview/). Netlify function handlers have additional context, including [Netlify Identity](https://docs.netlify.com/visitor-access/identity/) information. You can access this context via the `event.platform.context` field inside your hooks and endpoints.
73
+
74
+ Additionally, you can add your own Netlify functions by creating a directory for them and adding the configuration to your `netlify.toml` file. For example:
75
+
76
+ ```toml
77
+ [build]
78
+ command = "npm run build"
79
+ publish = "build"
80
+
81
+ [functions]
82
+ directory = "functions"
83
+ ```
84
+
85
+ ## Changelog
86
+
87
+ [The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-netlify/CHANGELOG.md).
@@ -0,0 +1,452 @@
1
+ 'use strict';
2
+
3
+ require('node:fs');
4
+ require('node:path');
5
+ var node_worker_threads = require('node:worker_threads');
6
+ var shims = require('./shims.js');
7
+ require('node:http');
8
+ require('node:https');
9
+ require('node:zlib');
10
+ require('node:stream');
11
+ require('node:util');
12
+ require('node:url');
13
+ require('net');
14
+ require('crypto');
15
+
16
+ globalThis.DOMException || (() => {
17
+ const port = new node_worker_threads.MessageChannel().port1;
18
+ const ab = new ArrayBuffer(0);
19
+ try { port.postMessage(ab, [ab, ab]); } catch (err) { return err.constructor }
20
+ })();
21
+
22
+ let s = 0;
23
+ const S = {
24
+ START_BOUNDARY: s++,
25
+ HEADER_FIELD_START: s++,
26
+ HEADER_FIELD: s++,
27
+ HEADER_VALUE_START: s++,
28
+ HEADER_VALUE: s++,
29
+ HEADER_VALUE_ALMOST_DONE: s++,
30
+ HEADERS_ALMOST_DONE: s++,
31
+ PART_DATA_START: s++,
32
+ PART_DATA: s++,
33
+ END: s++
34
+ };
35
+
36
+ let f = 1;
37
+ const F = {
38
+ PART_BOUNDARY: f,
39
+ LAST_BOUNDARY: f *= 2
40
+ };
41
+
42
+ const LF = 10;
43
+ const CR = 13;
44
+ const SPACE = 32;
45
+ const HYPHEN = 45;
46
+ const COLON = 58;
47
+ const A = 97;
48
+ const Z = 122;
49
+
50
+ const lower = c => c | 0x20;
51
+
52
+ const noop = () => {};
53
+
54
+ class MultipartParser {
55
+ /**
56
+ * @param {string} boundary
57
+ */
58
+ constructor(boundary) {
59
+ this.index = 0;
60
+ this.flags = 0;
61
+
62
+ this.onHeaderEnd = noop;
63
+ this.onHeaderField = noop;
64
+ this.onHeadersEnd = noop;
65
+ this.onHeaderValue = noop;
66
+ this.onPartBegin = noop;
67
+ this.onPartData = noop;
68
+ this.onPartEnd = noop;
69
+
70
+ this.boundaryChars = {};
71
+
72
+ boundary = '\r\n--' + boundary;
73
+ const ui8a = new Uint8Array(boundary.length);
74
+ for (let i = 0; i < boundary.length; i++) {
75
+ ui8a[i] = boundary.charCodeAt(i);
76
+ this.boundaryChars[ui8a[i]] = true;
77
+ }
78
+
79
+ this.boundary = ui8a;
80
+ this.lookbehind = new Uint8Array(this.boundary.length + 8);
81
+ this.state = S.START_BOUNDARY;
82
+ }
83
+
84
+ /**
85
+ * @param {Uint8Array} data
86
+ */
87
+ write(data) {
88
+ let i = 0;
89
+ const length_ = data.length;
90
+ let previousIndex = this.index;
91
+ let {lookbehind, boundary, boundaryChars, index, state, flags} = this;
92
+ const boundaryLength = this.boundary.length;
93
+ const boundaryEnd = boundaryLength - 1;
94
+ const bufferLength = data.length;
95
+ let c;
96
+ let cl;
97
+
98
+ const mark = name => {
99
+ this[name + 'Mark'] = i;
100
+ };
101
+
102
+ const clear = name => {
103
+ delete this[name + 'Mark'];
104
+ };
105
+
106
+ const callback = (callbackSymbol, start, end, ui8a) => {
107
+ if (start === undefined || start !== end) {
108
+ this[callbackSymbol](ui8a && ui8a.subarray(start, end));
109
+ }
110
+ };
111
+
112
+ const dataCallback = (name, clear) => {
113
+ const markSymbol = name + 'Mark';
114
+ if (!(markSymbol in this)) {
115
+ return;
116
+ }
117
+
118
+ if (clear) {
119
+ callback(name, this[markSymbol], i, data);
120
+ delete this[markSymbol];
121
+ } else {
122
+ callback(name, this[markSymbol], data.length, data);
123
+ this[markSymbol] = 0;
124
+ }
125
+ };
126
+
127
+ for (i = 0; i < length_; i++) {
128
+ c = data[i];
129
+
130
+ switch (state) {
131
+ case S.START_BOUNDARY:
132
+ if (index === boundary.length - 2) {
133
+ if (c === HYPHEN) {
134
+ flags |= F.LAST_BOUNDARY;
135
+ } else if (c !== CR) {
136
+ return;
137
+ }
138
+
139
+ index++;
140
+ break;
141
+ } else if (index - 1 === boundary.length - 2) {
142
+ if (flags & F.LAST_BOUNDARY && c === HYPHEN) {
143
+ state = S.END;
144
+ flags = 0;
145
+ } else if (!(flags & F.LAST_BOUNDARY) && c === LF) {
146
+ index = 0;
147
+ callback('onPartBegin');
148
+ state = S.HEADER_FIELD_START;
149
+ } else {
150
+ return;
151
+ }
152
+
153
+ break;
154
+ }
155
+
156
+ if (c !== boundary[index + 2]) {
157
+ index = -2;
158
+ }
159
+
160
+ if (c === boundary[index + 2]) {
161
+ index++;
162
+ }
163
+
164
+ break;
165
+ case S.HEADER_FIELD_START:
166
+ state = S.HEADER_FIELD;
167
+ mark('onHeaderField');
168
+ index = 0;
169
+ // falls through
170
+ case S.HEADER_FIELD:
171
+ if (c === CR) {
172
+ clear('onHeaderField');
173
+ state = S.HEADERS_ALMOST_DONE;
174
+ break;
175
+ }
176
+
177
+ index++;
178
+ if (c === HYPHEN) {
179
+ break;
180
+ }
181
+
182
+ if (c === COLON) {
183
+ if (index === 1) {
184
+ // empty header field
185
+ return;
186
+ }
187
+
188
+ dataCallback('onHeaderField', true);
189
+ state = S.HEADER_VALUE_START;
190
+ break;
191
+ }
192
+
193
+ cl = lower(c);
194
+ if (cl < A || cl > Z) {
195
+ return;
196
+ }
197
+
198
+ break;
199
+ case S.HEADER_VALUE_START:
200
+ if (c === SPACE) {
201
+ break;
202
+ }
203
+
204
+ mark('onHeaderValue');
205
+ state = S.HEADER_VALUE;
206
+ // falls through
207
+ case S.HEADER_VALUE:
208
+ if (c === CR) {
209
+ dataCallback('onHeaderValue', true);
210
+ callback('onHeaderEnd');
211
+ state = S.HEADER_VALUE_ALMOST_DONE;
212
+ }
213
+
214
+ break;
215
+ case S.HEADER_VALUE_ALMOST_DONE:
216
+ if (c !== LF) {
217
+ return;
218
+ }
219
+
220
+ state = S.HEADER_FIELD_START;
221
+ break;
222
+ case S.HEADERS_ALMOST_DONE:
223
+ if (c !== LF) {
224
+ return;
225
+ }
226
+
227
+ callback('onHeadersEnd');
228
+ state = S.PART_DATA_START;
229
+ break;
230
+ case S.PART_DATA_START:
231
+ state = S.PART_DATA;
232
+ mark('onPartData');
233
+ // falls through
234
+ case S.PART_DATA:
235
+ previousIndex = index;
236
+
237
+ if (index === 0) {
238
+ // boyer-moore derrived algorithm to safely skip non-boundary data
239
+ i += boundaryEnd;
240
+ while (i < bufferLength && !(data[i] in boundaryChars)) {
241
+ i += boundaryLength;
242
+ }
243
+
244
+ i -= boundaryEnd;
245
+ c = data[i];
246
+ }
247
+
248
+ if (index < boundary.length) {
249
+ if (boundary[index] === c) {
250
+ if (index === 0) {
251
+ dataCallback('onPartData', true);
252
+ }
253
+
254
+ index++;
255
+ } else {
256
+ index = 0;
257
+ }
258
+ } else if (index === boundary.length) {
259
+ index++;
260
+ if (c === CR) {
261
+ // CR = part boundary
262
+ flags |= F.PART_BOUNDARY;
263
+ } else if (c === HYPHEN) {
264
+ // HYPHEN = end boundary
265
+ flags |= F.LAST_BOUNDARY;
266
+ } else {
267
+ index = 0;
268
+ }
269
+ } else if (index - 1 === boundary.length) {
270
+ if (flags & F.PART_BOUNDARY) {
271
+ index = 0;
272
+ if (c === LF) {
273
+ // unset the PART_BOUNDARY flag
274
+ flags &= ~F.PART_BOUNDARY;
275
+ callback('onPartEnd');
276
+ callback('onPartBegin');
277
+ state = S.HEADER_FIELD_START;
278
+ break;
279
+ }
280
+ } else if (flags & F.LAST_BOUNDARY) {
281
+ if (c === HYPHEN) {
282
+ callback('onPartEnd');
283
+ state = S.END;
284
+ flags = 0;
285
+ } else {
286
+ index = 0;
287
+ }
288
+ } else {
289
+ index = 0;
290
+ }
291
+ }
292
+
293
+ if (index > 0) {
294
+ // when matching a possible boundary, keep a lookbehind reference
295
+ // in case it turns out to be a false lead
296
+ lookbehind[index - 1] = c;
297
+ } else if (previousIndex > 0) {
298
+ // if our boundary turned out to be rubbish, the captured lookbehind
299
+ // belongs to partData
300
+ const _lookbehind = new Uint8Array(lookbehind.buffer, lookbehind.byteOffset, lookbehind.byteLength);
301
+ callback('onPartData', 0, previousIndex, _lookbehind);
302
+ previousIndex = 0;
303
+ mark('onPartData');
304
+
305
+ // reconsider the current character even so it interrupted the sequence
306
+ // it could be the beginning of a new sequence
307
+ i--;
308
+ }
309
+
310
+ break;
311
+ case S.END:
312
+ break;
313
+ default:
314
+ throw new Error(`Unexpected state entered: ${state}`);
315
+ }
316
+ }
317
+
318
+ dataCallback('onHeaderField');
319
+ dataCallback('onHeaderValue');
320
+ dataCallback('onPartData');
321
+
322
+ // Update properties for the next call
323
+ this.index = index;
324
+ this.state = state;
325
+ this.flags = flags;
326
+ }
327
+
328
+ end() {
329
+ if ((this.state === S.HEADER_FIELD_START && this.index === 0) ||
330
+ (this.state === S.PART_DATA && this.index === this.boundary.length)) {
331
+ this.onPartEnd();
332
+ } else if (this.state !== S.END) {
333
+ throw new Error('MultipartParser.end(): stream ended unexpectedly');
334
+ }
335
+ }
336
+ }
337
+
338
+ function _fileName(headerValue) {
339
+ // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
340
+ const m = headerValue.match(/\bfilename=("(.*?)"|([^()<>@,;:\\"/[\]?={}\s\t]+))($|;\s)/i);
341
+ if (!m) {
342
+ return;
343
+ }
344
+
345
+ const match = m[2] || m[3] || '';
346
+ let filename = match.slice(match.lastIndexOf('\\') + 1);
347
+ filename = filename.replace(/%22/g, '"');
348
+ filename = filename.replace(/&#(\d{4});/g, (m, code) => {
349
+ return String.fromCharCode(code);
350
+ });
351
+ return filename;
352
+ }
353
+
354
+ async function toFormData(Body, ct) {
355
+ if (!/multipart/i.test(ct)) {
356
+ throw new TypeError('Failed to fetch');
357
+ }
358
+
359
+ const m = ct.match(/boundary=(?:"([^"]+)"|([^;]+))/i);
360
+
361
+ if (!m) {
362
+ throw new TypeError('no or bad content-type header, no multipart boundary');
363
+ }
364
+
365
+ const parser = new MultipartParser(m[1] || m[2]);
366
+
367
+ let headerField;
368
+ let headerValue;
369
+ let entryValue;
370
+ let entryName;
371
+ let contentType;
372
+ let filename;
373
+ const entryChunks = [];
374
+ const formData = new shims.FormData();
375
+
376
+ const onPartData = ui8a => {
377
+ entryValue += decoder.decode(ui8a, {stream: true});
378
+ };
379
+
380
+ const appendToFile = ui8a => {
381
+ entryChunks.push(ui8a);
382
+ };
383
+
384
+ const appendFileToFormData = () => {
385
+ const file = new shims.File(entryChunks, filename, {type: contentType});
386
+ formData.append(entryName, file);
387
+ };
388
+
389
+ const appendEntryToFormData = () => {
390
+ formData.append(entryName, entryValue);
391
+ };
392
+
393
+ const decoder = new TextDecoder('utf-8');
394
+ decoder.decode();
395
+
396
+ parser.onPartBegin = function () {
397
+ parser.onPartData = onPartData;
398
+ parser.onPartEnd = appendEntryToFormData;
399
+
400
+ headerField = '';
401
+ headerValue = '';
402
+ entryValue = '';
403
+ entryName = '';
404
+ contentType = '';
405
+ filename = null;
406
+ entryChunks.length = 0;
407
+ };
408
+
409
+ parser.onHeaderField = function (ui8a) {
410
+ headerField += decoder.decode(ui8a, {stream: true});
411
+ };
412
+
413
+ parser.onHeaderValue = function (ui8a) {
414
+ headerValue += decoder.decode(ui8a, {stream: true});
415
+ };
416
+
417
+ parser.onHeaderEnd = function () {
418
+ headerValue += decoder.decode();
419
+ headerField = headerField.toLowerCase();
420
+
421
+ if (headerField === 'content-disposition') {
422
+ // matches either a quoted-string or a token (RFC 2616 section 19.5.1)
423
+ const m = headerValue.match(/\bname=("([^"]*)"|([^()<>@,;:\\"/[\]?={}\s\t]+))/i);
424
+
425
+ if (m) {
426
+ entryName = m[2] || m[3] || '';
427
+ }
428
+
429
+ filename = _fileName(headerValue);
430
+
431
+ if (filename) {
432
+ parser.onPartData = appendToFile;
433
+ parser.onPartEnd = appendFileToFormData;
434
+ }
435
+ } else if (headerField === 'content-type') {
436
+ contentType = headerValue;
437
+ }
438
+
439
+ headerValue = '';
440
+ headerField = '';
441
+ };
442
+
443
+ for await (const chunk of Body) {
444
+ parser.write(chunk);
445
+ }
446
+
447
+ parser.end();
448
+
449
+ return formData;
450
+ }
451
+
452
+ exports.toFormData = toFormData;