@sveltejs/kit 1.0.0-next.251 → 1.0.0-next.252
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/dist/chunks/index.js +8 -2239
- package/dist/chunks/index2.js +1 -1
- package/dist/chunks/index3.js +1 -1
- package/dist/chunks/index5.js +100 -7
- package/dist/chunks/index6.js +1 -1
- package/dist/cli.js +3 -3
- package/package.json +2 -2
- package/types/ambient-modules.d.ts +2 -8
- package/dist/chunks/url.js +0 -119
package/dist/chunks/index.js
CHANGED
|
@@ -2,15 +2,14 @@ import path__default from 'path';
|
|
|
2
2
|
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
3
3
|
import vite from 'vite';
|
|
4
4
|
import { c as create_manifest_data, a as create_app, d as deep_merge } from './index2.js';
|
|
5
|
-
import {
|
|
5
|
+
import { r as runtime, S as SVELTE_KIT_ASSETS, a as resolve_entry, $, b as SVELTE_KIT, l as load_template, c as coalesce_to_error, g as get_mime_lookup, d as copy_assets, e as get_aliases, p as print_config_conflicts } from '../cli.js';
|
|
6
6
|
import fs__default from 'fs';
|
|
7
|
-
import { URL
|
|
7
|
+
import { URL } from 'url';
|
|
8
8
|
import { s as sirv } from './build.js';
|
|
9
|
-
import { e as escape_html_attr, r as resolve, i as is_root_relative, a as escape_json_string_in_html } from './url.js';
|
|
10
|
-
import { s } from './misc.js';
|
|
11
9
|
import { __fetch_polyfill } from '../install-fetch.js';
|
|
12
10
|
import { getRequest, setResponse } from '../node.js';
|
|
13
11
|
import { sequence } from '../hooks.js';
|
|
12
|
+
import './misc.js';
|
|
14
13
|
import 'sade';
|
|
15
14
|
import 'child_process';
|
|
16
15
|
import 'net';
|
|
@@ -24,2239 +23,6 @@ import 'node:util';
|
|
|
24
23
|
import 'node:url';
|
|
25
24
|
import 'stream';
|
|
26
25
|
|
|
27
|
-
/** @param {Partial<import('types/helper').ResponseHeaders> | undefined} object */
|
|
28
|
-
function to_headers(object) {
|
|
29
|
-
const headers = new Headers();
|
|
30
|
-
|
|
31
|
-
if (object) {
|
|
32
|
-
for (const key in object) {
|
|
33
|
-
const value = object[key];
|
|
34
|
-
if (!value) continue;
|
|
35
|
-
|
|
36
|
-
if (typeof value === 'string') {
|
|
37
|
-
headers.set(key, value);
|
|
38
|
-
} else {
|
|
39
|
-
value.forEach((value) => {
|
|
40
|
-
headers.append(key, value);
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return headers;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Hash using djb2
|
|
51
|
-
* @param {import('types/hooks').StrictBody} value
|
|
52
|
-
*/
|
|
53
|
-
function hash(value) {
|
|
54
|
-
let hash = 5381;
|
|
55
|
-
let i = value.length;
|
|
56
|
-
|
|
57
|
-
if (typeof value === 'string') {
|
|
58
|
-
while (i) hash = (hash * 33) ^ value.charCodeAt(--i);
|
|
59
|
-
} else {
|
|
60
|
-
while (i) hash = (hash * 33) ^ value[--i];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return (hash >>> 0).toString(36);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** @param {Record<string, any>} obj */
|
|
67
|
-
|
|
68
|
-
/** @param {Record<string, string>} params */
|
|
69
|
-
function decode_params(params) {
|
|
70
|
-
for (const key in params) {
|
|
71
|
-
// input has already been decoded by decodeURI
|
|
72
|
-
// now handle the rest that decodeURIComponent would do
|
|
73
|
-
params[key] = params[key]
|
|
74
|
-
.replace(/%23/g, '#')
|
|
75
|
-
.replace(/%3[Bb]/g, ';')
|
|
76
|
-
.replace(/%2[Cc]/g, ',')
|
|
77
|
-
.replace(/%2[Ff]/g, '/')
|
|
78
|
-
.replace(/%3[Ff]/g, '?')
|
|
79
|
-
.replace(/%3[Aa]/g, ':')
|
|
80
|
-
.replace(/%40/g, '@')
|
|
81
|
-
.replace(/%26/g, '&')
|
|
82
|
-
.replace(/%3[Dd]/g, '=')
|
|
83
|
-
.replace(/%2[Bb]/g, '+')
|
|
84
|
-
.replace(/%24/g, '$');
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return params;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** @param {string} body */
|
|
91
|
-
function error(body) {
|
|
92
|
-
return new Response(body, {
|
|
93
|
-
status: 500
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** @param {unknown} s */
|
|
98
|
-
function is_string(s) {
|
|
99
|
-
return typeof s === 'string' || s instanceof String;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const text_types = new Set([
|
|
103
|
-
'application/xml',
|
|
104
|
-
'application/json',
|
|
105
|
-
'application/x-www-form-urlencoded',
|
|
106
|
-
'multipart/form-data'
|
|
107
|
-
]);
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Decides how the body should be parsed based on its mime type. Should match what's in parse_body
|
|
111
|
-
*
|
|
112
|
-
* @param {string | undefined | null} content_type The `content-type` header of a request/response.
|
|
113
|
-
* @returns {boolean}
|
|
114
|
-
*/
|
|
115
|
-
function is_text(content_type) {
|
|
116
|
-
if (!content_type) return true; // defaults to json
|
|
117
|
-
const type = content_type.split(';')[0].toLowerCase(); // get the mime type
|
|
118
|
-
|
|
119
|
-
return type.startsWith('text/') || type.endsWith('+xml') || text_types.has(type);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* @param {import('types/hooks').RequestEvent} event
|
|
124
|
-
* @param {import('types/internal').SSREndpoint} route
|
|
125
|
-
* @param {RegExpExecArray} match
|
|
126
|
-
* @returns {Promise<Response | undefined>}
|
|
127
|
-
*/
|
|
128
|
-
async function render_endpoint(event, route, match) {
|
|
129
|
-
const mod = await route.load();
|
|
130
|
-
|
|
131
|
-
/** @type {import('types/endpoint').RequestHandler} */
|
|
132
|
-
const handler = mod[event.request.method.toLowerCase().replace('delete', 'del')]; // 'delete' is a reserved word
|
|
133
|
-
|
|
134
|
-
if (!handler) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// we're mutating `request` so that we don't have to do { ...request, params }
|
|
139
|
-
// on the next line, since that breaks the getters that replace path, query and
|
|
140
|
-
// origin. We could revert that once we remove the getters
|
|
141
|
-
event.params = route.params ? decode_params(route.params(match)) : {};
|
|
142
|
-
|
|
143
|
-
const response = await handler(event);
|
|
144
|
-
const preface = `Invalid response from route ${event.url.pathname}`;
|
|
145
|
-
|
|
146
|
-
if (typeof response !== 'object') {
|
|
147
|
-
return error(`${preface}: expected an object, got ${typeof response}`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (response.fallthrough) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const { status = 200, body = {} } = response;
|
|
155
|
-
const headers =
|
|
156
|
-
response.headers instanceof Headers ? response.headers : to_headers(response.headers);
|
|
157
|
-
|
|
158
|
-
const type = headers.get('content-type');
|
|
159
|
-
|
|
160
|
-
if (!is_text(type) && !(body instanceof Uint8Array || is_string(body))) {
|
|
161
|
-
return error(
|
|
162
|
-
`${preface}: body must be an instance of string or Uint8Array if content-type is not a supported textual content-type`
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/** @type {import('types/hooks').StrictBody} */
|
|
167
|
-
let normalized_body;
|
|
168
|
-
|
|
169
|
-
if (is_pojo(body) && (!type || type.startsWith('application/json'))) {
|
|
170
|
-
headers.set('content-type', 'application/json; charset=utf-8');
|
|
171
|
-
normalized_body = JSON.stringify(body);
|
|
172
|
-
} else {
|
|
173
|
-
normalized_body = /** @type {import('types/hooks').StrictBody} */ (body);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (
|
|
177
|
-
(typeof normalized_body === 'string' || normalized_body instanceof Uint8Array) &&
|
|
178
|
-
!headers.has('etag')
|
|
179
|
-
) {
|
|
180
|
-
const cache_control = headers.get('cache-control');
|
|
181
|
-
if (!cache_control || !/(no-store|immutable)/.test(cache_control)) {
|
|
182
|
-
headers.set('etag', `"${hash(normalized_body)}"`);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return new Response(normalized_body, {
|
|
187
|
-
status,
|
|
188
|
-
headers
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/** @param {any} body */
|
|
193
|
-
function is_pojo(body) {
|
|
194
|
-
if (typeof body !== 'object') return false;
|
|
195
|
-
|
|
196
|
-
if (body) {
|
|
197
|
-
if (body instanceof Uint8Array) return false;
|
|
198
|
-
|
|
199
|
-
// body could be a node Readable, but we don't want to import
|
|
200
|
-
// node built-ins, so we use duck typing
|
|
201
|
-
if (body._readableState && body._writableState && body._events) return false;
|
|
202
|
-
|
|
203
|
-
// similarly, it could be a web ReadableStream
|
|
204
|
-
if (typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) return false;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return true;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
var chars$1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
|
|
211
|
-
var unsafeChars = /[<>\b\f\n\r\t\0\u2028\u2029]/g;
|
|
212
|
-
var reserved = /^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|goto|long|this|void|with|await|break|catch|class|const|final|float|short|super|throw|while|yield|delete|double|export|import|native|return|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/;
|
|
213
|
-
var escaped = {
|
|
214
|
-
'<': '\\u003C',
|
|
215
|
-
'>': '\\u003E',
|
|
216
|
-
'/': '\\u002F',
|
|
217
|
-
'\\': '\\\\',
|
|
218
|
-
'\b': '\\b',
|
|
219
|
-
'\f': '\\f',
|
|
220
|
-
'\n': '\\n',
|
|
221
|
-
'\r': '\\r',
|
|
222
|
-
'\t': '\\t',
|
|
223
|
-
'\0': '\\0',
|
|
224
|
-
'\u2028': '\\u2028',
|
|
225
|
-
'\u2029': '\\u2029'
|
|
226
|
-
};
|
|
227
|
-
var objectProtoOwnPropertyNames = Object.getOwnPropertyNames(Object.prototype).sort().join('\0');
|
|
228
|
-
function devalue(value) {
|
|
229
|
-
var counts = new Map();
|
|
230
|
-
function walk(thing) {
|
|
231
|
-
if (typeof thing === 'function') {
|
|
232
|
-
throw new Error("Cannot stringify a function");
|
|
233
|
-
}
|
|
234
|
-
if (counts.has(thing)) {
|
|
235
|
-
counts.set(thing, counts.get(thing) + 1);
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
counts.set(thing, 1);
|
|
239
|
-
if (!isPrimitive(thing)) {
|
|
240
|
-
var type = getType(thing);
|
|
241
|
-
switch (type) {
|
|
242
|
-
case 'Number':
|
|
243
|
-
case 'String':
|
|
244
|
-
case 'Boolean':
|
|
245
|
-
case 'Date':
|
|
246
|
-
case 'RegExp':
|
|
247
|
-
return;
|
|
248
|
-
case 'Array':
|
|
249
|
-
thing.forEach(walk);
|
|
250
|
-
break;
|
|
251
|
-
case 'Set':
|
|
252
|
-
case 'Map':
|
|
253
|
-
Array.from(thing).forEach(walk);
|
|
254
|
-
break;
|
|
255
|
-
default:
|
|
256
|
-
var proto = Object.getPrototypeOf(thing);
|
|
257
|
-
if (proto !== Object.prototype &&
|
|
258
|
-
proto !== null &&
|
|
259
|
-
Object.getOwnPropertyNames(proto).sort().join('\0') !== objectProtoOwnPropertyNames) {
|
|
260
|
-
throw new Error("Cannot stringify arbitrary non-POJOs");
|
|
261
|
-
}
|
|
262
|
-
if (Object.getOwnPropertySymbols(thing).length > 0) {
|
|
263
|
-
throw new Error("Cannot stringify POJOs with symbolic keys");
|
|
264
|
-
}
|
|
265
|
-
Object.keys(thing).forEach(function (key) { return walk(thing[key]); });
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
walk(value);
|
|
270
|
-
var names = new Map();
|
|
271
|
-
Array.from(counts)
|
|
272
|
-
.filter(function (entry) { return entry[1] > 1; })
|
|
273
|
-
.sort(function (a, b) { return b[1] - a[1]; })
|
|
274
|
-
.forEach(function (entry, i) {
|
|
275
|
-
names.set(entry[0], getName(i));
|
|
276
|
-
});
|
|
277
|
-
function stringify(thing) {
|
|
278
|
-
if (names.has(thing)) {
|
|
279
|
-
return names.get(thing);
|
|
280
|
-
}
|
|
281
|
-
if (isPrimitive(thing)) {
|
|
282
|
-
return stringifyPrimitive(thing);
|
|
283
|
-
}
|
|
284
|
-
var type = getType(thing);
|
|
285
|
-
switch (type) {
|
|
286
|
-
case 'Number':
|
|
287
|
-
case 'String':
|
|
288
|
-
case 'Boolean':
|
|
289
|
-
return "Object(" + stringify(thing.valueOf()) + ")";
|
|
290
|
-
case 'RegExp':
|
|
291
|
-
return "new RegExp(" + stringifyString(thing.source) + ", \"" + thing.flags + "\")";
|
|
292
|
-
case 'Date':
|
|
293
|
-
return "new Date(" + thing.getTime() + ")";
|
|
294
|
-
case 'Array':
|
|
295
|
-
var members = thing.map(function (v, i) { return i in thing ? stringify(v) : ''; });
|
|
296
|
-
var tail = thing.length === 0 || (thing.length - 1 in thing) ? '' : ',';
|
|
297
|
-
return "[" + members.join(',') + tail + "]";
|
|
298
|
-
case 'Set':
|
|
299
|
-
case 'Map':
|
|
300
|
-
return "new " + type + "([" + Array.from(thing).map(stringify).join(',') + "])";
|
|
301
|
-
default:
|
|
302
|
-
var obj = "{" + Object.keys(thing).map(function (key) { return safeKey(key) + ":" + stringify(thing[key]); }).join(',') + "}";
|
|
303
|
-
var proto = Object.getPrototypeOf(thing);
|
|
304
|
-
if (proto === null) {
|
|
305
|
-
return Object.keys(thing).length > 0
|
|
306
|
-
? "Object.assign(Object.create(null)," + obj + ")"
|
|
307
|
-
: "Object.create(null)";
|
|
308
|
-
}
|
|
309
|
-
return obj;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
var str = stringify(value);
|
|
313
|
-
if (names.size) {
|
|
314
|
-
var params_1 = [];
|
|
315
|
-
var statements_1 = [];
|
|
316
|
-
var values_1 = [];
|
|
317
|
-
names.forEach(function (name, thing) {
|
|
318
|
-
params_1.push(name);
|
|
319
|
-
if (isPrimitive(thing)) {
|
|
320
|
-
values_1.push(stringifyPrimitive(thing));
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
var type = getType(thing);
|
|
324
|
-
switch (type) {
|
|
325
|
-
case 'Number':
|
|
326
|
-
case 'String':
|
|
327
|
-
case 'Boolean':
|
|
328
|
-
values_1.push("Object(" + stringify(thing.valueOf()) + ")");
|
|
329
|
-
break;
|
|
330
|
-
case 'RegExp':
|
|
331
|
-
values_1.push(thing.toString());
|
|
332
|
-
break;
|
|
333
|
-
case 'Date':
|
|
334
|
-
values_1.push("new Date(" + thing.getTime() + ")");
|
|
335
|
-
break;
|
|
336
|
-
case 'Array':
|
|
337
|
-
values_1.push("Array(" + thing.length + ")");
|
|
338
|
-
thing.forEach(function (v, i) {
|
|
339
|
-
statements_1.push(name + "[" + i + "]=" + stringify(v));
|
|
340
|
-
});
|
|
341
|
-
break;
|
|
342
|
-
case 'Set':
|
|
343
|
-
values_1.push("new Set");
|
|
344
|
-
statements_1.push(name + "." + Array.from(thing).map(function (v) { return "add(" + stringify(v) + ")"; }).join('.'));
|
|
345
|
-
break;
|
|
346
|
-
case 'Map':
|
|
347
|
-
values_1.push("new Map");
|
|
348
|
-
statements_1.push(name + "." + Array.from(thing).map(function (_a) {
|
|
349
|
-
var k = _a[0], v = _a[1];
|
|
350
|
-
return "set(" + stringify(k) + ", " + stringify(v) + ")";
|
|
351
|
-
}).join('.'));
|
|
352
|
-
break;
|
|
353
|
-
default:
|
|
354
|
-
values_1.push(Object.getPrototypeOf(thing) === null ? 'Object.create(null)' : '{}');
|
|
355
|
-
Object.keys(thing).forEach(function (key) {
|
|
356
|
-
statements_1.push("" + name + safeProp(key) + "=" + stringify(thing[key]));
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
statements_1.push("return " + str);
|
|
361
|
-
return "(function(" + params_1.join(',') + "){" + statements_1.join(';') + "}(" + values_1.join(',') + "))";
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
return str;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
function getName(num) {
|
|
368
|
-
var name = '';
|
|
369
|
-
do {
|
|
370
|
-
name = chars$1[num % chars$1.length] + name;
|
|
371
|
-
num = ~~(num / chars$1.length) - 1;
|
|
372
|
-
} while (num >= 0);
|
|
373
|
-
return reserved.test(name) ? name + "_" : name;
|
|
374
|
-
}
|
|
375
|
-
function isPrimitive(thing) {
|
|
376
|
-
return Object(thing) !== thing;
|
|
377
|
-
}
|
|
378
|
-
function stringifyPrimitive(thing) {
|
|
379
|
-
if (typeof thing === 'string')
|
|
380
|
-
return stringifyString(thing);
|
|
381
|
-
if (thing === void 0)
|
|
382
|
-
return 'void 0';
|
|
383
|
-
if (thing === 0 && 1 / thing < 0)
|
|
384
|
-
return '-0';
|
|
385
|
-
var str = String(thing);
|
|
386
|
-
if (typeof thing === 'number')
|
|
387
|
-
return str.replace(/^(-)?0\./, '$1.');
|
|
388
|
-
return str;
|
|
389
|
-
}
|
|
390
|
-
function getType(thing) {
|
|
391
|
-
return Object.prototype.toString.call(thing).slice(8, -1);
|
|
392
|
-
}
|
|
393
|
-
function escapeUnsafeChar(c) {
|
|
394
|
-
return escaped[c] || c;
|
|
395
|
-
}
|
|
396
|
-
function escapeUnsafeChars(str) {
|
|
397
|
-
return str.replace(unsafeChars, escapeUnsafeChar);
|
|
398
|
-
}
|
|
399
|
-
function safeKey(key) {
|
|
400
|
-
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : escapeUnsafeChars(JSON.stringify(key));
|
|
401
|
-
}
|
|
402
|
-
function safeProp(key) {
|
|
403
|
-
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? "." + key : "[" + escapeUnsafeChars(JSON.stringify(key)) + "]";
|
|
404
|
-
}
|
|
405
|
-
function stringifyString(str) {
|
|
406
|
-
var result = '"';
|
|
407
|
-
for (var i = 0; i < str.length; i += 1) {
|
|
408
|
-
var char = str.charAt(i);
|
|
409
|
-
var code = char.charCodeAt(0);
|
|
410
|
-
if (char === '"') {
|
|
411
|
-
result += '\\"';
|
|
412
|
-
}
|
|
413
|
-
else if (char in escaped) {
|
|
414
|
-
result += escaped[char];
|
|
415
|
-
}
|
|
416
|
-
else if (code >= 0xd800 && code <= 0xdfff) {
|
|
417
|
-
var next = str.charCodeAt(i + 1);
|
|
418
|
-
// If this is the beginning of a [high, low] surrogate pair,
|
|
419
|
-
// add the next two characters, otherwise escape
|
|
420
|
-
if (code <= 0xdbff && (next >= 0xdc00 && next <= 0xdfff)) {
|
|
421
|
-
result += char + str[++i];
|
|
422
|
-
}
|
|
423
|
-
else {
|
|
424
|
-
result += "\\u" + code.toString(16).toUpperCase();
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
else {
|
|
428
|
-
result += char;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
result += '"';
|
|
432
|
-
return result;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
function noop() { }
|
|
436
|
-
function safe_not_equal(a, b) {
|
|
437
|
-
return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function');
|
|
438
|
-
}
|
|
439
|
-
Promise.resolve();
|
|
440
|
-
|
|
441
|
-
const subscriber_queue = [];
|
|
442
|
-
/**
|
|
443
|
-
* Create a `Writable` store that allows both updating and reading by subscription.
|
|
444
|
-
* @param {*=}value initial value
|
|
445
|
-
* @param {StartStopNotifier=}start start and stop notifications for subscriptions
|
|
446
|
-
*/
|
|
447
|
-
function writable(value, start = noop) {
|
|
448
|
-
let stop;
|
|
449
|
-
const subscribers = new Set();
|
|
450
|
-
function set(new_value) {
|
|
451
|
-
if (safe_not_equal(value, new_value)) {
|
|
452
|
-
value = new_value;
|
|
453
|
-
if (stop) { // store is ready
|
|
454
|
-
const run_queue = !subscriber_queue.length;
|
|
455
|
-
for (const subscriber of subscribers) {
|
|
456
|
-
subscriber[1]();
|
|
457
|
-
subscriber_queue.push(subscriber, value);
|
|
458
|
-
}
|
|
459
|
-
if (run_queue) {
|
|
460
|
-
for (let i = 0; i < subscriber_queue.length; i += 2) {
|
|
461
|
-
subscriber_queue[i][0](subscriber_queue[i + 1]);
|
|
462
|
-
}
|
|
463
|
-
subscriber_queue.length = 0;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
function update(fn) {
|
|
469
|
-
set(fn(value));
|
|
470
|
-
}
|
|
471
|
-
function subscribe(run, invalidate = noop) {
|
|
472
|
-
const subscriber = [run, invalidate];
|
|
473
|
-
subscribers.add(subscriber);
|
|
474
|
-
if (subscribers.size === 1) {
|
|
475
|
-
stop = start(set) || noop;
|
|
476
|
-
}
|
|
477
|
-
run(value);
|
|
478
|
-
return () => {
|
|
479
|
-
subscribers.delete(subscriber);
|
|
480
|
-
if (subscribers.size === 0) {
|
|
481
|
-
stop();
|
|
482
|
-
stop = null;
|
|
483
|
-
}
|
|
484
|
-
};
|
|
485
|
-
}
|
|
486
|
-
return { set, update, subscribe };
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
/** @param {URL} url */
|
|
490
|
-
function create_prerendering_url_proxy(url) {
|
|
491
|
-
return new Proxy(url, {
|
|
492
|
-
get: (target, prop, receiver) => {
|
|
493
|
-
if (prop === 'search' || prop === 'searchParams') {
|
|
494
|
-
throw new Error(`Cannot access url.${prop} on a page with prerendering enabled`);
|
|
495
|
-
}
|
|
496
|
-
return Reflect.get(target, prop, receiver);
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
const encoder = new TextEncoder();
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* SHA-256 hashing function adapted from https://bitwiseshiftleft.github.io/sjcl
|
|
505
|
-
* modified and redistributed under BSD license
|
|
506
|
-
* @param {string} data
|
|
507
|
-
*/
|
|
508
|
-
function sha256(data) {
|
|
509
|
-
if (!key[0]) precompute();
|
|
510
|
-
|
|
511
|
-
const out = init.slice(0);
|
|
512
|
-
const array = encode(data);
|
|
513
|
-
|
|
514
|
-
for (let i = 0; i < array.length; i += 16) {
|
|
515
|
-
const w = array.subarray(i, i + 16);
|
|
516
|
-
|
|
517
|
-
let tmp;
|
|
518
|
-
let a;
|
|
519
|
-
let b;
|
|
520
|
-
|
|
521
|
-
let out0 = out[0];
|
|
522
|
-
let out1 = out[1];
|
|
523
|
-
let out2 = out[2];
|
|
524
|
-
let out3 = out[3];
|
|
525
|
-
let out4 = out[4];
|
|
526
|
-
let out5 = out[5];
|
|
527
|
-
let out6 = out[6];
|
|
528
|
-
let out7 = out[7];
|
|
529
|
-
|
|
530
|
-
/* Rationale for placement of |0 :
|
|
531
|
-
* If a value can overflow is original 32 bits by a factor of more than a few
|
|
532
|
-
* million (2^23 ish), there is a possibility that it might overflow the
|
|
533
|
-
* 53-bit mantissa and lose precision.
|
|
534
|
-
*
|
|
535
|
-
* To avoid this, we clamp back to 32 bits by |'ing with 0 on any value that
|
|
536
|
-
* propagates around the loop, and on the hash state out[]. I don't believe
|
|
537
|
-
* that the clamps on out4 and on out0 are strictly necessary, but it's close
|
|
538
|
-
* (for out4 anyway), and better safe than sorry.
|
|
539
|
-
*
|
|
540
|
-
* The clamps on out[] are necessary for the output to be correct even in the
|
|
541
|
-
* common case and for short inputs.
|
|
542
|
-
*/
|
|
543
|
-
|
|
544
|
-
for (let i = 0; i < 64; i++) {
|
|
545
|
-
// load up the input word for this round
|
|
546
|
-
|
|
547
|
-
if (i < 16) {
|
|
548
|
-
tmp = w[i];
|
|
549
|
-
} else {
|
|
550
|
-
a = w[(i + 1) & 15];
|
|
551
|
-
|
|
552
|
-
b = w[(i + 14) & 15];
|
|
553
|
-
|
|
554
|
-
tmp = w[i & 15] =
|
|
555
|
-
(((a >>> 7) ^ (a >>> 18) ^ (a >>> 3) ^ (a << 25) ^ (a << 14)) +
|
|
556
|
-
((b >>> 17) ^ (b >>> 19) ^ (b >>> 10) ^ (b << 15) ^ (b << 13)) +
|
|
557
|
-
w[i & 15] +
|
|
558
|
-
w[(i + 9) & 15]) |
|
|
559
|
-
0;
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
tmp =
|
|
563
|
-
tmp +
|
|
564
|
-
out7 +
|
|
565
|
-
((out4 >>> 6) ^ (out4 >>> 11) ^ (out4 >>> 25) ^ (out4 << 26) ^ (out4 << 21) ^ (out4 << 7)) +
|
|
566
|
-
(out6 ^ (out4 & (out5 ^ out6))) +
|
|
567
|
-
key[i]; // | 0;
|
|
568
|
-
|
|
569
|
-
// shift register
|
|
570
|
-
out7 = out6;
|
|
571
|
-
out6 = out5;
|
|
572
|
-
out5 = out4;
|
|
573
|
-
|
|
574
|
-
out4 = (out3 + tmp) | 0;
|
|
575
|
-
|
|
576
|
-
out3 = out2;
|
|
577
|
-
out2 = out1;
|
|
578
|
-
out1 = out0;
|
|
579
|
-
|
|
580
|
-
out0 =
|
|
581
|
-
(tmp +
|
|
582
|
-
((out1 & out2) ^ (out3 & (out1 ^ out2))) +
|
|
583
|
-
((out1 >>> 2) ^
|
|
584
|
-
(out1 >>> 13) ^
|
|
585
|
-
(out1 >>> 22) ^
|
|
586
|
-
(out1 << 30) ^
|
|
587
|
-
(out1 << 19) ^
|
|
588
|
-
(out1 << 10))) |
|
|
589
|
-
0;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
out[0] = (out[0] + out0) | 0;
|
|
593
|
-
out[1] = (out[1] + out1) | 0;
|
|
594
|
-
out[2] = (out[2] + out2) | 0;
|
|
595
|
-
out[3] = (out[3] + out3) | 0;
|
|
596
|
-
out[4] = (out[4] + out4) | 0;
|
|
597
|
-
out[5] = (out[5] + out5) | 0;
|
|
598
|
-
out[6] = (out[6] + out6) | 0;
|
|
599
|
-
out[7] = (out[7] + out7) | 0;
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
const bytes = new Uint8Array(out.buffer);
|
|
603
|
-
reverse_endianness(bytes);
|
|
604
|
-
|
|
605
|
-
return base64(bytes);
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
/** The SHA-256 initialization vector */
|
|
609
|
-
const init = new Uint32Array(8);
|
|
610
|
-
|
|
611
|
-
/** The SHA-256 hash key */
|
|
612
|
-
const key = new Uint32Array(64);
|
|
613
|
-
|
|
614
|
-
/** Function to precompute init and key. */
|
|
615
|
-
function precompute() {
|
|
616
|
-
/** @param {number} x */
|
|
617
|
-
function frac(x) {
|
|
618
|
-
return (x - Math.floor(x)) * 0x100000000;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
let prime = 2;
|
|
622
|
-
|
|
623
|
-
for (let i = 0; i < 64; prime++) {
|
|
624
|
-
let is_prime = true;
|
|
625
|
-
|
|
626
|
-
for (let factor = 2; factor * factor <= prime; factor++) {
|
|
627
|
-
if (prime % factor === 0) {
|
|
628
|
-
is_prime = false;
|
|
629
|
-
|
|
630
|
-
break;
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
if (is_prime) {
|
|
635
|
-
if (i < 8) {
|
|
636
|
-
init[i] = frac(prime ** (1 / 2));
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
key[i] = frac(prime ** (1 / 3));
|
|
640
|
-
|
|
641
|
-
i++;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
/** @param {Uint8Array} bytes */
|
|
647
|
-
function reverse_endianness(bytes) {
|
|
648
|
-
for (let i = 0; i < bytes.length; i += 4) {
|
|
649
|
-
const a = bytes[i + 0];
|
|
650
|
-
const b = bytes[i + 1];
|
|
651
|
-
const c = bytes[i + 2];
|
|
652
|
-
const d = bytes[i + 3];
|
|
653
|
-
|
|
654
|
-
bytes[i + 0] = d;
|
|
655
|
-
bytes[i + 1] = c;
|
|
656
|
-
bytes[i + 2] = b;
|
|
657
|
-
bytes[i + 3] = a;
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
/** @param {string} str */
|
|
662
|
-
function encode(str) {
|
|
663
|
-
const encoded = encoder.encode(str);
|
|
664
|
-
const length = encoded.length * 8;
|
|
665
|
-
|
|
666
|
-
// result should be a multiple of 512 bits in length,
|
|
667
|
-
// with room for a 1 (after the data) and two 32-bit
|
|
668
|
-
// words containing the original input bit length
|
|
669
|
-
const size = 512 * Math.ceil((length + 65) / 512);
|
|
670
|
-
const bytes = new Uint8Array(size / 8);
|
|
671
|
-
bytes.set(encoded);
|
|
672
|
-
|
|
673
|
-
// append a 1
|
|
674
|
-
bytes[encoded.length] = 0b10000000;
|
|
675
|
-
|
|
676
|
-
reverse_endianness(bytes);
|
|
677
|
-
|
|
678
|
-
// add the input bit length
|
|
679
|
-
const words = new Uint32Array(bytes.buffer);
|
|
680
|
-
words[words.length - 2] = Math.floor(length / 0x100000000); // this will always be zero for us
|
|
681
|
-
words[words.length - 1] = length;
|
|
682
|
-
|
|
683
|
-
return words;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
/*
|
|
687
|
-
Based on https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
|
|
688
|
-
|
|
689
|
-
MIT License
|
|
690
|
-
Copyright (c) 2020 Egor Nepomnyaschih
|
|
691
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
692
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
693
|
-
in the Software without restriction, including without limitation the rights
|
|
694
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
695
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
696
|
-
furnished to do so, subject to the following conditions:
|
|
697
|
-
The above copyright notice and this permission notice shall be included in all
|
|
698
|
-
copies or substantial portions of the Software.
|
|
699
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
700
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
701
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
702
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
703
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
704
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
705
|
-
SOFTWARE.
|
|
706
|
-
*/
|
|
707
|
-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
|
|
708
|
-
|
|
709
|
-
/** @param {Uint8Array} bytes */
|
|
710
|
-
function base64(bytes) {
|
|
711
|
-
const l = bytes.length;
|
|
712
|
-
|
|
713
|
-
let result = '';
|
|
714
|
-
let i;
|
|
715
|
-
|
|
716
|
-
for (i = 2; i < l; i += 3) {
|
|
717
|
-
result += chars[bytes[i - 2] >> 2];
|
|
718
|
-
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
719
|
-
result += chars[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)];
|
|
720
|
-
result += chars[bytes[i] & 0x3f];
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
if (i === l + 1) {
|
|
724
|
-
// 1 octet yet to write
|
|
725
|
-
result += chars[bytes[i - 2] >> 2];
|
|
726
|
-
result += chars[(bytes[i - 2] & 0x03) << 4];
|
|
727
|
-
result += '==';
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
if (i === l) {
|
|
731
|
-
// 2 octets yet to write
|
|
732
|
-
result += chars[bytes[i - 2] >> 2];
|
|
733
|
-
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
734
|
-
result += chars[(bytes[i - 1] & 0x0f) << 2];
|
|
735
|
-
result += '=';
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
return result;
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
/** @type {Promise<void>} */
|
|
742
|
-
let csp_ready;
|
|
743
|
-
|
|
744
|
-
/** @type {() => string} */
|
|
745
|
-
let generate_nonce;
|
|
746
|
-
|
|
747
|
-
/** @type {(input: string) => string} */
|
|
748
|
-
let generate_hash;
|
|
749
|
-
|
|
750
|
-
if (typeof crypto !== 'undefined') {
|
|
751
|
-
const array = new Uint8Array(16);
|
|
752
|
-
|
|
753
|
-
generate_nonce = () => {
|
|
754
|
-
crypto.getRandomValues(array);
|
|
755
|
-
return base64(array);
|
|
756
|
-
};
|
|
757
|
-
|
|
758
|
-
generate_hash = sha256;
|
|
759
|
-
} else {
|
|
760
|
-
// TODO: remove this in favor of web crypto API once we no longer support Node 14
|
|
761
|
-
const name = 'crypto'; // store in a variable to fool esbuild when adapters bundle kit
|
|
762
|
-
csp_ready = import(name).then((crypto) => {
|
|
763
|
-
generate_nonce = () => {
|
|
764
|
-
return crypto.randomBytes(16).toString('base64');
|
|
765
|
-
};
|
|
766
|
-
|
|
767
|
-
generate_hash = (input) => {
|
|
768
|
-
return crypto.createHash('sha256').update(input, 'utf-8').digest().toString('base64');
|
|
769
|
-
};
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
const quoted = new Set([
|
|
774
|
-
'self',
|
|
775
|
-
'unsafe-eval',
|
|
776
|
-
'unsafe-hashes',
|
|
777
|
-
'unsafe-inline',
|
|
778
|
-
'none',
|
|
779
|
-
'strict-dynamic',
|
|
780
|
-
'report-sample'
|
|
781
|
-
]);
|
|
782
|
-
|
|
783
|
-
const crypto_pattern = /^(nonce|sha\d\d\d)-/;
|
|
784
|
-
|
|
785
|
-
class Csp {
|
|
786
|
-
/** @type {boolean} */
|
|
787
|
-
#use_hashes;
|
|
788
|
-
|
|
789
|
-
/** @type {boolean} */
|
|
790
|
-
#dev;
|
|
791
|
-
|
|
792
|
-
/** @type {boolean} */
|
|
793
|
-
#script_needs_csp;
|
|
794
|
-
|
|
795
|
-
/** @type {boolean} */
|
|
796
|
-
#style_needs_csp;
|
|
797
|
-
|
|
798
|
-
/** @type {import('types/csp').CspDirectives} */
|
|
799
|
-
#directives;
|
|
800
|
-
|
|
801
|
-
/** @type {import('types/csp').Source[]} */
|
|
802
|
-
#script_src;
|
|
803
|
-
|
|
804
|
-
/** @type {import('types/csp').Source[]} */
|
|
805
|
-
#style_src;
|
|
806
|
-
|
|
807
|
-
/**
|
|
808
|
-
* @param {{
|
|
809
|
-
* mode: string,
|
|
810
|
-
* directives: import('types/csp').CspDirectives
|
|
811
|
-
* }} config
|
|
812
|
-
* @param {{
|
|
813
|
-
* dev: boolean;
|
|
814
|
-
* prerender: boolean;
|
|
815
|
-
* needs_nonce: boolean;
|
|
816
|
-
* }} opts
|
|
817
|
-
*/
|
|
818
|
-
constructor({ mode, directives }, { dev, prerender, needs_nonce }) {
|
|
819
|
-
this.#use_hashes = mode === 'hash' || (mode === 'auto' && prerender);
|
|
820
|
-
this.#directives = dev ? { ...directives } : directives; // clone in dev so we can safely mutate
|
|
821
|
-
this.#dev = dev;
|
|
822
|
-
|
|
823
|
-
const d = this.#directives;
|
|
824
|
-
|
|
825
|
-
if (dev) {
|
|
826
|
-
// remove strict-dynamic in dev...
|
|
827
|
-
// TODO reinstate this if we can figure out how to make strict-dynamic work
|
|
828
|
-
// if (d['default-src']) {
|
|
829
|
-
// d['default-src'] = d['default-src'].filter((name) => name !== 'strict-dynamic');
|
|
830
|
-
// if (d['default-src'].length === 0) delete d['default-src'];
|
|
831
|
-
// }
|
|
832
|
-
|
|
833
|
-
// if (d['script-src']) {
|
|
834
|
-
// d['script-src'] = d['script-src'].filter((name) => name !== 'strict-dynamic');
|
|
835
|
-
// if (d['script-src'].length === 0) delete d['script-src'];
|
|
836
|
-
// }
|
|
837
|
-
|
|
838
|
-
const effective_style_src = d['style-src'] || d['default-src'];
|
|
839
|
-
|
|
840
|
-
// ...and add unsafe-inline so we can inject <style> elements
|
|
841
|
-
if (effective_style_src && !effective_style_src.includes('unsafe-inline')) {
|
|
842
|
-
d['style-src'] = [...effective_style_src, 'unsafe-inline'];
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
this.#script_src = [];
|
|
847
|
-
this.#style_src = [];
|
|
848
|
-
|
|
849
|
-
const effective_script_src = d['script-src'] || d['default-src'];
|
|
850
|
-
const effective_style_src = d['style-src'] || d['default-src'];
|
|
851
|
-
|
|
852
|
-
this.#script_needs_csp =
|
|
853
|
-
!!effective_script_src &&
|
|
854
|
-
effective_script_src.filter((value) => value !== 'unsafe-inline').length > 0;
|
|
855
|
-
|
|
856
|
-
this.#style_needs_csp =
|
|
857
|
-
!dev &&
|
|
858
|
-
!!effective_style_src &&
|
|
859
|
-
effective_style_src.filter((value) => value !== 'unsafe-inline').length > 0;
|
|
860
|
-
|
|
861
|
-
this.script_needs_nonce = this.#script_needs_csp && !this.#use_hashes;
|
|
862
|
-
this.style_needs_nonce = this.#style_needs_csp && !this.#use_hashes;
|
|
863
|
-
|
|
864
|
-
if (this.script_needs_nonce || this.style_needs_nonce || needs_nonce) {
|
|
865
|
-
this.nonce = generate_nonce();
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
/** @param {string} content */
|
|
870
|
-
add_script(content) {
|
|
871
|
-
if (this.#script_needs_csp) {
|
|
872
|
-
if (this.#use_hashes) {
|
|
873
|
-
this.#script_src.push(`sha256-${generate_hash(content)}`);
|
|
874
|
-
} else if (this.#script_src.length === 0) {
|
|
875
|
-
this.#script_src.push(`nonce-${this.nonce}`);
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
/** @param {string} content */
|
|
881
|
-
add_style(content) {
|
|
882
|
-
if (this.#style_needs_csp) {
|
|
883
|
-
if (this.#use_hashes) {
|
|
884
|
-
this.#style_src.push(`sha256-${generate_hash(content)}`);
|
|
885
|
-
} else if (this.#style_src.length === 0) {
|
|
886
|
-
this.#style_src.push(`nonce-${this.nonce}`);
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
/** @param {boolean} [is_meta] */
|
|
892
|
-
get_header(is_meta = false) {
|
|
893
|
-
const header = [];
|
|
894
|
-
|
|
895
|
-
// due to browser inconsistencies, we can't append sources to default-src
|
|
896
|
-
// (specifically, Firefox appears to not ignore nonce-{nonce} directives
|
|
897
|
-
// on default-src), so we ensure that script-src and style-src exist
|
|
898
|
-
|
|
899
|
-
const directives = { ...this.#directives };
|
|
900
|
-
|
|
901
|
-
if (this.#style_src.length > 0) {
|
|
902
|
-
directives['style-src'] = [
|
|
903
|
-
...(directives['style-src'] || directives['default-src'] || []),
|
|
904
|
-
...this.#style_src
|
|
905
|
-
];
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
if (this.#script_src.length > 0) {
|
|
909
|
-
directives['script-src'] = [
|
|
910
|
-
...(directives['script-src'] || directives['default-src'] || []),
|
|
911
|
-
...this.#script_src
|
|
912
|
-
];
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
for (const key in directives) {
|
|
916
|
-
if (is_meta && (key === 'frame-ancestors' || key === 'report-uri' || key === 'sandbox')) {
|
|
917
|
-
// these values cannot be used with a <meta> tag
|
|
918
|
-
// TODO warn?
|
|
919
|
-
continue;
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
// @ts-expect-error gimme a break typescript, `key` is obviously a member of directives
|
|
923
|
-
const value = /** @type {string[] | true} */ (directives[key]);
|
|
924
|
-
|
|
925
|
-
if (!value) continue;
|
|
926
|
-
|
|
927
|
-
const directive = [key];
|
|
928
|
-
if (Array.isArray(value)) {
|
|
929
|
-
value.forEach((value) => {
|
|
930
|
-
if (quoted.has(value) || crypto_pattern.test(value)) {
|
|
931
|
-
directive.push(`'${value}'`);
|
|
932
|
-
} else {
|
|
933
|
-
directive.push(value);
|
|
934
|
-
}
|
|
935
|
-
});
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
header.push(directive.join(' '));
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
return header.join('; ');
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
get_meta() {
|
|
945
|
-
const content = escape_html_attr(this.get_header(true));
|
|
946
|
-
return `<meta http-equiv="content-security-policy" content=${content}>`;
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
// TODO rename this function/module
|
|
951
|
-
|
|
952
|
-
/**
|
|
953
|
-
* @param {{
|
|
954
|
-
* branch: Array<import('./types').Loaded>;
|
|
955
|
-
* options: import('types/internal').SSRRenderOptions;
|
|
956
|
-
* state: import('types/internal').SSRRenderState;
|
|
957
|
-
* $session: any;
|
|
958
|
-
* page_config: { hydrate: boolean, router: boolean };
|
|
959
|
-
* status: number;
|
|
960
|
-
* error?: Error;
|
|
961
|
-
* url: URL;
|
|
962
|
-
* params: Record<string, string>;
|
|
963
|
-
* ssr: boolean;
|
|
964
|
-
* stuff: Record<string, any>;
|
|
965
|
-
* }} opts
|
|
966
|
-
*/
|
|
967
|
-
async function render_response({
|
|
968
|
-
branch,
|
|
969
|
-
options,
|
|
970
|
-
state,
|
|
971
|
-
$session,
|
|
972
|
-
page_config,
|
|
973
|
-
status,
|
|
974
|
-
error,
|
|
975
|
-
url,
|
|
976
|
-
params,
|
|
977
|
-
ssr,
|
|
978
|
-
stuff
|
|
979
|
-
}) {
|
|
980
|
-
if (state.prerender) {
|
|
981
|
-
if (options.csp.mode === 'nonce') {
|
|
982
|
-
throw new Error('Cannot use prerendering if config.kit.csp.mode === "nonce"');
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
if (options.template_contains_nonce) {
|
|
986
|
-
throw new Error('Cannot use prerendering if page template contains %svelte.nonce%');
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
|
|
990
|
-
const stylesheets = new Set(options.manifest._.entry.css);
|
|
991
|
-
const modulepreloads = new Set(options.manifest._.entry.js);
|
|
992
|
-
/** @type {Map<string, string>} */
|
|
993
|
-
const styles = new Map();
|
|
994
|
-
|
|
995
|
-
/** @type {Array<{ url: string, body: string, json: string }>} */
|
|
996
|
-
const serialized_data = [];
|
|
997
|
-
|
|
998
|
-
let rendered;
|
|
999
|
-
|
|
1000
|
-
let is_private = false;
|
|
1001
|
-
let maxage;
|
|
1002
|
-
|
|
1003
|
-
if (error) {
|
|
1004
|
-
error.stack = options.get_stack(error);
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
if (ssr) {
|
|
1008
|
-
branch.forEach(({ node, loaded, fetched, uses_credentials }) => {
|
|
1009
|
-
if (node.css) node.css.forEach((url) => stylesheets.add(url));
|
|
1010
|
-
if (node.js) node.js.forEach((url) => modulepreloads.add(url));
|
|
1011
|
-
if (node.styles) Object.entries(node.styles).forEach(([k, v]) => styles.set(k, v));
|
|
1012
|
-
|
|
1013
|
-
// TODO probably better if `fetched` wasn't populated unless `hydrate`
|
|
1014
|
-
if (fetched && page_config.hydrate) serialized_data.push(...fetched);
|
|
1015
|
-
|
|
1016
|
-
if (uses_credentials) is_private = true;
|
|
1017
|
-
|
|
1018
|
-
maxage = loaded.maxage;
|
|
1019
|
-
});
|
|
1020
|
-
|
|
1021
|
-
const session = writable($session);
|
|
1022
|
-
|
|
1023
|
-
/** @type {Record<string, any>} */
|
|
1024
|
-
const props = {
|
|
1025
|
-
stores: {
|
|
1026
|
-
page: writable(null),
|
|
1027
|
-
navigating: writable(null),
|
|
1028
|
-
session
|
|
1029
|
-
},
|
|
1030
|
-
page: {
|
|
1031
|
-
url: state.prerender ? create_prerendering_url_proxy(url) : url,
|
|
1032
|
-
params,
|
|
1033
|
-
status,
|
|
1034
|
-
error,
|
|
1035
|
-
stuff
|
|
1036
|
-
},
|
|
1037
|
-
components: branch.map(({ node }) => node.module.default)
|
|
1038
|
-
};
|
|
1039
|
-
|
|
1040
|
-
// TODO remove this for 1.0
|
|
1041
|
-
/**
|
|
1042
|
-
* @param {string} property
|
|
1043
|
-
* @param {string} replacement
|
|
1044
|
-
*/
|
|
1045
|
-
const print_error = (property, replacement) => {
|
|
1046
|
-
Object.defineProperty(props.page, property, {
|
|
1047
|
-
get: () => {
|
|
1048
|
-
throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
|
|
1049
|
-
}
|
|
1050
|
-
});
|
|
1051
|
-
};
|
|
1052
|
-
|
|
1053
|
-
print_error('origin', 'origin');
|
|
1054
|
-
print_error('path', 'pathname');
|
|
1055
|
-
print_error('query', 'searchParams');
|
|
1056
|
-
|
|
1057
|
-
// props_n (instead of props[n]) makes it easy to avoid
|
|
1058
|
-
// unnecessary updates for layout components
|
|
1059
|
-
for (let i = 0; i < branch.length; i += 1) {
|
|
1060
|
-
props[`props_${i}`] = await branch[i].loaded.props;
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
let session_tracking_active = false;
|
|
1064
|
-
const unsubscribe = session.subscribe(() => {
|
|
1065
|
-
if (session_tracking_active) is_private = true;
|
|
1066
|
-
});
|
|
1067
|
-
session_tracking_active = true;
|
|
1068
|
-
|
|
1069
|
-
try {
|
|
1070
|
-
rendered = options.root.render(props);
|
|
1071
|
-
} finally {
|
|
1072
|
-
unsubscribe();
|
|
1073
|
-
}
|
|
1074
|
-
} else {
|
|
1075
|
-
rendered = { head: '', html: '', css: { code: '', map: null } };
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
|
-
let { head, html: body } = rendered;
|
|
1079
|
-
|
|
1080
|
-
const inlined_style = Array.from(styles.values()).join('\n');
|
|
1081
|
-
|
|
1082
|
-
await csp_ready;
|
|
1083
|
-
const csp = new Csp(options.csp, {
|
|
1084
|
-
dev: options.dev,
|
|
1085
|
-
prerender: !!state.prerender,
|
|
1086
|
-
needs_nonce: options.template_contains_nonce
|
|
1087
|
-
});
|
|
1088
|
-
|
|
1089
|
-
// prettier-ignore
|
|
1090
|
-
const init_app = `
|
|
1091
|
-
import { start } from ${s(options.prefix + options.manifest._.entry.file)};
|
|
1092
|
-
start({
|
|
1093
|
-
target: ${options.target ? `document.querySelector(${s(options.target)})` : 'document.body'},
|
|
1094
|
-
paths: ${s(options.paths)},
|
|
1095
|
-
session: ${try_serialize($session, (error) => {
|
|
1096
|
-
throw new Error(`Failed to serialize session data: ${error.message}`);
|
|
1097
|
-
})},
|
|
1098
|
-
route: ${!!page_config.router},
|
|
1099
|
-
spa: ${!ssr},
|
|
1100
|
-
trailing_slash: ${s(options.trailing_slash)},
|
|
1101
|
-
hydrate: ${ssr && page_config.hydrate ? `{
|
|
1102
|
-
status: ${status},
|
|
1103
|
-
error: ${serialize_error(error)},
|
|
1104
|
-
nodes: [
|
|
1105
|
-
${(branch || [])
|
|
1106
|
-
.map(({ node }) => `import(${s(options.prefix + node.entry)})`)
|
|
1107
|
-
.join(',\n\t\t\t\t\t\t')}
|
|
1108
|
-
],
|
|
1109
|
-
url: new URL(${s(url.href)}),
|
|
1110
|
-
params: ${devalue(params)}
|
|
1111
|
-
}` : 'null'}
|
|
1112
|
-
});
|
|
1113
|
-
`;
|
|
1114
|
-
|
|
1115
|
-
const init_service_worker = `
|
|
1116
|
-
if ('serviceWorker' in navigator) {
|
|
1117
|
-
navigator.serviceWorker.register('${options.service_worker}');
|
|
1118
|
-
}
|
|
1119
|
-
`;
|
|
1120
|
-
|
|
1121
|
-
if (options.amp) {
|
|
1122
|
-
head += `
|
|
1123
|
-
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>
|
|
1124
|
-
<noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
|
|
1125
|
-
<script async src="https://cdn.ampproject.org/v0.js"></script>
|
|
1126
|
-
|
|
1127
|
-
<style amp-custom>${inlined_style}\n${rendered.css.code}</style>`;
|
|
1128
|
-
|
|
1129
|
-
if (options.service_worker) {
|
|
1130
|
-
head +=
|
|
1131
|
-
'<script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>';
|
|
1132
|
-
|
|
1133
|
-
body += `<amp-install-serviceworker src="${options.service_worker}" layout="nodisplay"></amp-install-serviceworker>`;
|
|
1134
|
-
}
|
|
1135
|
-
} else {
|
|
1136
|
-
if (inlined_style) {
|
|
1137
|
-
const attributes = [];
|
|
1138
|
-
if (options.dev) attributes.push(' data-svelte');
|
|
1139
|
-
if (csp.style_needs_nonce) attributes.push(` nonce="${csp.nonce}"`);
|
|
1140
|
-
|
|
1141
|
-
csp.add_style(inlined_style);
|
|
1142
|
-
|
|
1143
|
-
head += `\n\t<style${attributes.join('')}>${inlined_style}</style>`;
|
|
1144
|
-
}
|
|
1145
|
-
|
|
1146
|
-
// prettier-ignore
|
|
1147
|
-
head += Array.from(stylesheets)
|
|
1148
|
-
.map((dep) => {
|
|
1149
|
-
const attributes = [
|
|
1150
|
-
'rel="stylesheet"',
|
|
1151
|
-
`href="${options.prefix + dep}"`
|
|
1152
|
-
];
|
|
1153
|
-
|
|
1154
|
-
if (csp.style_needs_nonce) {
|
|
1155
|
-
attributes.push(`nonce="${csp.nonce}"`);
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
if (styles.has(dep)) {
|
|
1159
|
-
attributes.push('disabled', 'media="(max-width: 0)"');
|
|
1160
|
-
}
|
|
1161
|
-
|
|
1162
|
-
return `\n\t<link ${attributes.join(' ')}>`;
|
|
1163
|
-
})
|
|
1164
|
-
.join('');
|
|
1165
|
-
|
|
1166
|
-
if (page_config.router || page_config.hydrate) {
|
|
1167
|
-
head += Array.from(modulepreloads)
|
|
1168
|
-
.map((dep) => `\n\t<link rel="modulepreload" href="${options.prefix + dep}">`)
|
|
1169
|
-
.join('');
|
|
1170
|
-
|
|
1171
|
-
const attributes = ['type="module"'];
|
|
1172
|
-
|
|
1173
|
-
csp.add_script(init_app);
|
|
1174
|
-
|
|
1175
|
-
if (csp.script_needs_nonce) {
|
|
1176
|
-
attributes.push(`nonce="${csp.nonce}"`);
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
head += `<script ${attributes.join(' ')}>${init_app}</script>`;
|
|
1180
|
-
|
|
1181
|
-
// prettier-ignore
|
|
1182
|
-
body += serialized_data
|
|
1183
|
-
.map(({ url, body, json }) => {
|
|
1184
|
-
let attributes = `type="application/json" data-type="svelte-data" data-url=${escape_html_attr(url)}`;
|
|
1185
|
-
if (body) attributes += ` data-body="${hash(body)}"`;
|
|
1186
|
-
|
|
1187
|
-
return `<script ${attributes}>${json}</script>`;
|
|
1188
|
-
})
|
|
1189
|
-
.join('\n\n\t');
|
|
1190
|
-
}
|
|
1191
|
-
|
|
1192
|
-
if (options.service_worker) {
|
|
1193
|
-
// always include service worker unless it's turned off explicitly
|
|
1194
|
-
csp.add_script(init_service_worker);
|
|
1195
|
-
|
|
1196
|
-
head += `
|
|
1197
|
-
<script${csp.script_needs_nonce ? ` nonce="${csp.nonce}"` : ''}>${init_service_worker}</script>`;
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
|
|
1201
|
-
if (state.prerender) {
|
|
1202
|
-
const http_equiv = [];
|
|
1203
|
-
|
|
1204
|
-
const csp_headers = csp.get_meta();
|
|
1205
|
-
if (csp_headers) {
|
|
1206
|
-
http_equiv.push(csp_headers);
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
if (maxage) {
|
|
1210
|
-
http_equiv.push(`<meta http-equiv="cache-control" content="max-age=${maxage}">`);
|
|
1211
|
-
}
|
|
1212
|
-
|
|
1213
|
-
if (http_equiv.length > 0) {
|
|
1214
|
-
head = http_equiv.join('\n') + head;
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
const segments = url.pathname.slice(options.paths.base.length).split('/').slice(2);
|
|
1219
|
-
const assets =
|
|
1220
|
-
options.paths.assets || (segments.length > 0 ? segments.map(() => '..').join('/') : '.');
|
|
1221
|
-
|
|
1222
|
-
const html = options.template({ head, body, assets, nonce: /** @type {string} */ (csp.nonce) });
|
|
1223
|
-
|
|
1224
|
-
const headers = new Headers({
|
|
1225
|
-
'content-type': 'text/html',
|
|
1226
|
-
etag: `"${hash(html)}"`
|
|
1227
|
-
});
|
|
1228
|
-
|
|
1229
|
-
if (maxage) {
|
|
1230
|
-
headers.set('cache-control', `${is_private ? 'private' : 'public'}, max-age=${maxage}`);
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
if (!options.floc) {
|
|
1234
|
-
headers.set('permissions-policy', 'interest-cohort=()');
|
|
1235
|
-
}
|
|
1236
|
-
|
|
1237
|
-
if (!state.prerender) {
|
|
1238
|
-
const csp_header = csp.get_header();
|
|
1239
|
-
if (csp_header) {
|
|
1240
|
-
headers.set('content-security-policy', csp_header);
|
|
1241
|
-
}
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
return new Response(html, {
|
|
1245
|
-
status,
|
|
1246
|
-
headers
|
|
1247
|
-
});
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
/**
|
|
1251
|
-
* @param {any} data
|
|
1252
|
-
* @param {(error: Error) => void} [fail]
|
|
1253
|
-
*/
|
|
1254
|
-
function try_serialize(data, fail) {
|
|
1255
|
-
try {
|
|
1256
|
-
return devalue(data);
|
|
1257
|
-
} catch (err) {
|
|
1258
|
-
if (fail) fail(coalesce_to_error(err));
|
|
1259
|
-
return null;
|
|
1260
|
-
}
|
|
1261
|
-
}
|
|
1262
|
-
|
|
1263
|
-
// Ensure we return something truthy so the client will not re-render the page over the error
|
|
1264
|
-
|
|
1265
|
-
/** @param {(Error & {frame?: string} & {loc?: object}) | undefined | null} error */
|
|
1266
|
-
function serialize_error(error) {
|
|
1267
|
-
if (!error) return null;
|
|
1268
|
-
let serialized = try_serialize(error);
|
|
1269
|
-
if (!serialized) {
|
|
1270
|
-
const { name, message, stack } = error;
|
|
1271
|
-
serialized = try_serialize({ ...error, name, message, stack });
|
|
1272
|
-
}
|
|
1273
|
-
if (!serialized) {
|
|
1274
|
-
serialized = '{}';
|
|
1275
|
-
}
|
|
1276
|
-
return serialized;
|
|
1277
|
-
}
|
|
1278
|
-
|
|
1279
|
-
/**
|
|
1280
|
-
* @param {import('types/page').LoadOutput} loaded
|
|
1281
|
-
* @returns {import('types/internal').NormalizedLoadOutput}
|
|
1282
|
-
*/
|
|
1283
|
-
function normalize(loaded) {
|
|
1284
|
-
const has_error_status =
|
|
1285
|
-
loaded.status && loaded.status >= 400 && loaded.status <= 599 && !loaded.redirect;
|
|
1286
|
-
if (loaded.error || has_error_status) {
|
|
1287
|
-
const status = loaded.status;
|
|
1288
|
-
|
|
1289
|
-
if (!loaded.error && has_error_status) {
|
|
1290
|
-
return {
|
|
1291
|
-
status: status || 500,
|
|
1292
|
-
error: new Error()
|
|
1293
|
-
};
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
const error = typeof loaded.error === 'string' ? new Error(loaded.error) : loaded.error;
|
|
1297
|
-
|
|
1298
|
-
if (!(error instanceof Error)) {
|
|
1299
|
-
return {
|
|
1300
|
-
status: 500,
|
|
1301
|
-
error: new Error(
|
|
1302
|
-
`"error" property returned from load() must be a string or instance of Error, received type "${typeof error}"`
|
|
1303
|
-
)
|
|
1304
|
-
};
|
|
1305
|
-
}
|
|
1306
|
-
|
|
1307
|
-
if (!status || status < 400 || status > 599) {
|
|
1308
|
-
console.warn('"error" returned from load() without a valid status code — defaulting to 500');
|
|
1309
|
-
return { status: 500, error };
|
|
1310
|
-
}
|
|
1311
|
-
|
|
1312
|
-
return { status, error };
|
|
1313
|
-
}
|
|
1314
|
-
|
|
1315
|
-
if (loaded.redirect) {
|
|
1316
|
-
if (!loaded.status || Math.floor(loaded.status / 100) !== 3) {
|
|
1317
|
-
return {
|
|
1318
|
-
status: 500,
|
|
1319
|
-
error: new Error(
|
|
1320
|
-
'"redirect" property returned from load() must be accompanied by a 3xx status code'
|
|
1321
|
-
)
|
|
1322
|
-
};
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
|
-
if (typeof loaded.redirect !== 'string') {
|
|
1326
|
-
return {
|
|
1327
|
-
status: 500,
|
|
1328
|
-
error: new Error('"redirect" property returned from load() must be a string')
|
|
1329
|
-
};
|
|
1330
|
-
}
|
|
1331
|
-
}
|
|
1332
|
-
|
|
1333
|
-
// TODO remove before 1.0
|
|
1334
|
-
if (/** @type {any} */ (loaded).context) {
|
|
1335
|
-
throw new Error(
|
|
1336
|
-
'You are returning "context" from a load function. ' +
|
|
1337
|
-
'"context" was renamed to "stuff", please adjust your code accordingly.'
|
|
1338
|
-
);
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
return /** @type {import('types/internal').NormalizedLoadOutput} */ (loaded);
|
|
1342
|
-
}
|
|
1343
|
-
|
|
1344
|
-
/**
|
|
1345
|
-
* @param {{
|
|
1346
|
-
* event: import('types/hooks').RequestEvent;
|
|
1347
|
-
* options: import('types/internal').SSRRenderOptions;
|
|
1348
|
-
* state: import('types/internal').SSRRenderState;
|
|
1349
|
-
* route: import('types/internal').SSRPage | null;
|
|
1350
|
-
* url: URL;
|
|
1351
|
-
* params: Record<string, string>;
|
|
1352
|
-
* node: import('types/internal').SSRNode;
|
|
1353
|
-
* $session: any;
|
|
1354
|
-
* stuff: Record<string, any>;
|
|
1355
|
-
* is_error: boolean;
|
|
1356
|
-
* status?: number;
|
|
1357
|
-
* error?: Error;
|
|
1358
|
-
* }} opts
|
|
1359
|
-
* @returns {Promise<import('./types').Loaded | undefined>} undefined for fallthrough
|
|
1360
|
-
*/
|
|
1361
|
-
async function load_node({
|
|
1362
|
-
event,
|
|
1363
|
-
options,
|
|
1364
|
-
state,
|
|
1365
|
-
route,
|
|
1366
|
-
url,
|
|
1367
|
-
params,
|
|
1368
|
-
node,
|
|
1369
|
-
$session,
|
|
1370
|
-
stuff,
|
|
1371
|
-
is_error,
|
|
1372
|
-
status,
|
|
1373
|
-
error
|
|
1374
|
-
}) {
|
|
1375
|
-
const { module } = node;
|
|
1376
|
-
|
|
1377
|
-
let uses_credentials = false;
|
|
1378
|
-
|
|
1379
|
-
/**
|
|
1380
|
-
* @type {Array<{
|
|
1381
|
-
* url: string;
|
|
1382
|
-
* body: string;
|
|
1383
|
-
* json: string;
|
|
1384
|
-
* }>}
|
|
1385
|
-
*/
|
|
1386
|
-
const fetched = [];
|
|
1387
|
-
|
|
1388
|
-
/**
|
|
1389
|
-
* @type {string[]}
|
|
1390
|
-
*/
|
|
1391
|
-
let set_cookie_headers = [];
|
|
1392
|
-
|
|
1393
|
-
let loaded;
|
|
1394
|
-
|
|
1395
|
-
if (module.load) {
|
|
1396
|
-
/** @type {import('types/page').LoadInput | import('types/page').ErrorLoadInput} */
|
|
1397
|
-
const load_input = {
|
|
1398
|
-
url: state.prerender ? create_prerendering_url_proxy(url) : url,
|
|
1399
|
-
params,
|
|
1400
|
-
get session() {
|
|
1401
|
-
uses_credentials = true;
|
|
1402
|
-
return $session;
|
|
1403
|
-
},
|
|
1404
|
-
/**
|
|
1405
|
-
* @param {RequestInfo} resource
|
|
1406
|
-
* @param {RequestInit} opts
|
|
1407
|
-
*/
|
|
1408
|
-
fetch: async (resource, opts = {}) => {
|
|
1409
|
-
/** @type {string} */
|
|
1410
|
-
let requested;
|
|
1411
|
-
|
|
1412
|
-
if (typeof resource === 'string') {
|
|
1413
|
-
requested = resource;
|
|
1414
|
-
} else {
|
|
1415
|
-
requested = resource.url;
|
|
1416
|
-
|
|
1417
|
-
opts = {
|
|
1418
|
-
method: resource.method,
|
|
1419
|
-
headers: resource.headers,
|
|
1420
|
-
body: resource.body,
|
|
1421
|
-
mode: resource.mode,
|
|
1422
|
-
credentials: resource.credentials,
|
|
1423
|
-
cache: resource.cache,
|
|
1424
|
-
redirect: resource.redirect,
|
|
1425
|
-
referrer: resource.referrer,
|
|
1426
|
-
integrity: resource.integrity,
|
|
1427
|
-
...opts
|
|
1428
|
-
};
|
|
1429
|
-
}
|
|
1430
|
-
|
|
1431
|
-
opts.headers = new Headers(opts.headers);
|
|
1432
|
-
|
|
1433
|
-
const resolved = resolve(event.url.pathname, requested.split('?')[0]);
|
|
1434
|
-
|
|
1435
|
-
/** @type {Response} */
|
|
1436
|
-
let response;
|
|
1437
|
-
|
|
1438
|
-
/** @type {import('types/internal').PrerenderDependency} */
|
|
1439
|
-
let dependency;
|
|
1440
|
-
|
|
1441
|
-
// handle fetch requests for static assets. e.g. prebaked data, etc.
|
|
1442
|
-
// we need to support everything the browser's fetch supports
|
|
1443
|
-
const prefix = options.paths.assets || options.paths.base;
|
|
1444
|
-
const filename = decodeURIComponent(
|
|
1445
|
-
resolved.startsWith(prefix) ? resolved.slice(prefix.length) : resolved
|
|
1446
|
-
).slice(1);
|
|
1447
|
-
const filename_html = `${filename}/index.html`; // path may also match path/index.html
|
|
1448
|
-
|
|
1449
|
-
const is_asset = options.manifest.assets.has(filename);
|
|
1450
|
-
const is_asset_html = options.manifest.assets.has(filename_html);
|
|
1451
|
-
|
|
1452
|
-
if (is_asset || is_asset_html) {
|
|
1453
|
-
const file = is_asset ? filename : filename_html;
|
|
1454
|
-
|
|
1455
|
-
if (options.read) {
|
|
1456
|
-
const type = is_asset
|
|
1457
|
-
? options.manifest._.mime[filename.slice(filename.lastIndexOf('.'))]
|
|
1458
|
-
: 'text/html';
|
|
1459
|
-
|
|
1460
|
-
response = new Response(options.read(file), {
|
|
1461
|
-
headers: type ? { 'content-type': type } : {}
|
|
1462
|
-
});
|
|
1463
|
-
} else {
|
|
1464
|
-
response = await fetch(`${url.origin}/${file}`, /** @type {RequestInit} */ (opts));
|
|
1465
|
-
}
|
|
1466
|
-
} else if (is_root_relative(resolved)) {
|
|
1467
|
-
if (opts.credentials !== 'omit') {
|
|
1468
|
-
uses_credentials = true;
|
|
1469
|
-
|
|
1470
|
-
const cookie = event.request.headers.get('cookie');
|
|
1471
|
-
const authorization = event.request.headers.get('authorization');
|
|
1472
|
-
|
|
1473
|
-
if (cookie) {
|
|
1474
|
-
opts.headers.set('cookie', cookie);
|
|
1475
|
-
}
|
|
1476
|
-
|
|
1477
|
-
if (authorization && !opts.headers.has('authorization')) {
|
|
1478
|
-
opts.headers.set('authorization', authorization);
|
|
1479
|
-
}
|
|
1480
|
-
}
|
|
1481
|
-
|
|
1482
|
-
if (opts.body && typeof opts.body !== 'string') {
|
|
1483
|
-
// per https://developer.mozilla.org/en-US/docs/Web/API/Request/Request, this can be a
|
|
1484
|
-
// Blob, BufferSource, FormData, URLSearchParams, USVString, or ReadableStream object.
|
|
1485
|
-
// non-string bodies are irksome to deal with, but luckily aren't particularly useful
|
|
1486
|
-
// in this context anyway, so we take the easy route and ban them
|
|
1487
|
-
throw new Error('Request body must be a string');
|
|
1488
|
-
}
|
|
1489
|
-
|
|
1490
|
-
response = await respond(new Request(new URL(requested, event.url).href, opts), options, {
|
|
1491
|
-
fetched: requested,
|
|
1492
|
-
initiator: route
|
|
1493
|
-
});
|
|
1494
|
-
|
|
1495
|
-
if (state.prerender) {
|
|
1496
|
-
dependency = { response, body: null };
|
|
1497
|
-
state.prerender.dependencies.set(resolved, dependency);
|
|
1498
|
-
}
|
|
1499
|
-
} else {
|
|
1500
|
-
// external
|
|
1501
|
-
if (resolved.startsWith('//')) {
|
|
1502
|
-
throw new Error(
|
|
1503
|
-
`Cannot request protocol-relative URL (${requested}) in server-side fetch`
|
|
1504
|
-
);
|
|
1505
|
-
}
|
|
1506
|
-
|
|
1507
|
-
// external fetch
|
|
1508
|
-
// allow cookie passthrough for "same-origin"
|
|
1509
|
-
// if SvelteKit is serving my.domain.com:
|
|
1510
|
-
// - domain.com WILL NOT receive cookies
|
|
1511
|
-
// - my.domain.com WILL receive cookies
|
|
1512
|
-
// - api.domain.dom WILL NOT receive cookies
|
|
1513
|
-
// - sub.my.domain.com WILL receive cookies
|
|
1514
|
-
// ports do not affect the resolution
|
|
1515
|
-
// leading dot prevents mydomain.com matching domain.com
|
|
1516
|
-
if (
|
|
1517
|
-
`.${new URL(requested).hostname}`.endsWith(`.${event.url.hostname}`) &&
|
|
1518
|
-
opts.credentials !== 'omit'
|
|
1519
|
-
) {
|
|
1520
|
-
uses_credentials = true;
|
|
1521
|
-
|
|
1522
|
-
const cookie = event.request.headers.get('cookie');
|
|
1523
|
-
if (cookie) opts.headers.set('cookie', cookie);
|
|
1524
|
-
}
|
|
1525
|
-
|
|
1526
|
-
const external_request = new Request(requested, /** @type {RequestInit} */ (opts));
|
|
1527
|
-
response = await options.hooks.externalFetch.call(null, external_request);
|
|
1528
|
-
}
|
|
1529
|
-
|
|
1530
|
-
const proxy = new Proxy(response, {
|
|
1531
|
-
get(response, key, _receiver) {
|
|
1532
|
-
async function text() {
|
|
1533
|
-
const body = await response.text();
|
|
1534
|
-
|
|
1535
|
-
/** @type {import('types/helper').ResponseHeaders} */
|
|
1536
|
-
const headers = {};
|
|
1537
|
-
for (const [key, value] of response.headers) {
|
|
1538
|
-
if (key === 'set-cookie') {
|
|
1539
|
-
set_cookie_headers = set_cookie_headers.concat(value);
|
|
1540
|
-
} else if (key !== 'etag') {
|
|
1541
|
-
headers[key] = value;
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
if (!opts.body || typeof opts.body === 'string') {
|
|
1546
|
-
// prettier-ignore
|
|
1547
|
-
fetched.push({
|
|
1548
|
-
url: requested,
|
|
1549
|
-
body: /** @type {string} */ (opts.body),
|
|
1550
|
-
json: `{"status":${response.status},"statusText":${s(response.statusText)},"headers":${s(headers)},"body":"${escape_json_string_in_html(body)}"}`
|
|
1551
|
-
});
|
|
1552
|
-
}
|
|
1553
|
-
|
|
1554
|
-
if (dependency) {
|
|
1555
|
-
dependency.body = body;
|
|
1556
|
-
}
|
|
1557
|
-
|
|
1558
|
-
return body;
|
|
1559
|
-
}
|
|
1560
|
-
|
|
1561
|
-
if (key === 'arrayBuffer') {
|
|
1562
|
-
return async () => {
|
|
1563
|
-
const buffer = await response.arrayBuffer();
|
|
1564
|
-
|
|
1565
|
-
if (dependency) {
|
|
1566
|
-
dependency.body = new Uint8Array(buffer);
|
|
1567
|
-
}
|
|
1568
|
-
|
|
1569
|
-
// TODO should buffer be inlined into the page (albeit base64'd)?
|
|
1570
|
-
// any conditions in which it shouldn't be?
|
|
1571
|
-
|
|
1572
|
-
return buffer;
|
|
1573
|
-
};
|
|
1574
|
-
}
|
|
1575
|
-
|
|
1576
|
-
if (key === 'text') {
|
|
1577
|
-
return text;
|
|
1578
|
-
}
|
|
1579
|
-
|
|
1580
|
-
if (key === 'json') {
|
|
1581
|
-
return async () => {
|
|
1582
|
-
return JSON.parse(await text());
|
|
1583
|
-
};
|
|
1584
|
-
}
|
|
1585
|
-
|
|
1586
|
-
// TODO arrayBuffer?
|
|
1587
|
-
|
|
1588
|
-
return Reflect.get(response, key, response);
|
|
1589
|
-
}
|
|
1590
|
-
});
|
|
1591
|
-
|
|
1592
|
-
return proxy;
|
|
1593
|
-
},
|
|
1594
|
-
stuff: { ...stuff }
|
|
1595
|
-
};
|
|
1596
|
-
|
|
1597
|
-
if (options.dev) {
|
|
1598
|
-
// TODO remove this for 1.0
|
|
1599
|
-
Object.defineProperty(load_input, 'page', {
|
|
1600
|
-
get: () => {
|
|
1601
|
-
throw new Error('`page` in `load` functions has been replaced by `url` and `params`');
|
|
1602
|
-
}
|
|
1603
|
-
});
|
|
1604
|
-
}
|
|
1605
|
-
|
|
1606
|
-
if (is_error) {
|
|
1607
|
-
/** @type {import('types/page').ErrorLoadInput} */ (load_input).status = status;
|
|
1608
|
-
/** @type {import('types/page').ErrorLoadInput} */ (load_input).error = error;
|
|
1609
|
-
}
|
|
1610
|
-
|
|
1611
|
-
loaded = await module.load.call(null, load_input);
|
|
1612
|
-
|
|
1613
|
-
if (!loaded) {
|
|
1614
|
-
throw new Error(`load function must return a value${options.dev ? ` (${node.entry})` : ''}`);
|
|
1615
|
-
}
|
|
1616
|
-
} else {
|
|
1617
|
-
loaded = {};
|
|
1618
|
-
}
|
|
1619
|
-
|
|
1620
|
-
if (loaded.fallthrough && !is_error) {
|
|
1621
|
-
return;
|
|
1622
|
-
}
|
|
1623
|
-
|
|
1624
|
-
return {
|
|
1625
|
-
node,
|
|
1626
|
-
loaded: normalize(loaded),
|
|
1627
|
-
stuff: loaded.stuff || stuff,
|
|
1628
|
-
fetched,
|
|
1629
|
-
set_cookie_headers,
|
|
1630
|
-
uses_credentials
|
|
1631
|
-
};
|
|
1632
|
-
}
|
|
1633
|
-
|
|
1634
|
-
/**
|
|
1635
|
-
* @typedef {import('./types.js').Loaded} Loaded
|
|
1636
|
-
* @typedef {import('types/internal').SSRRenderOptions} SSRRenderOptions
|
|
1637
|
-
* @typedef {import('types/internal').SSRRenderState} SSRRenderState
|
|
1638
|
-
*/
|
|
1639
|
-
|
|
1640
|
-
/**
|
|
1641
|
-
* @param {{
|
|
1642
|
-
* event: import('types/hooks').RequestEvent;
|
|
1643
|
-
* options: SSRRenderOptions;
|
|
1644
|
-
* state: SSRRenderState;
|
|
1645
|
-
* $session: any;
|
|
1646
|
-
* status: number;
|
|
1647
|
-
* error: Error;
|
|
1648
|
-
* ssr: boolean;
|
|
1649
|
-
* }} opts
|
|
1650
|
-
*/
|
|
1651
|
-
async function respond_with_error({ event, options, state, $session, status, error, ssr }) {
|
|
1652
|
-
try {
|
|
1653
|
-
const default_layout = await options.manifest._.nodes[0](); // 0 is always the root layout
|
|
1654
|
-
const default_error = await options.manifest._.nodes[1](); // 1 is always the root error
|
|
1655
|
-
|
|
1656
|
-
/** @type {Record<string, string>} */
|
|
1657
|
-
const params = {}; // error page has no params
|
|
1658
|
-
|
|
1659
|
-
const layout_loaded = /** @type {Loaded} */ (
|
|
1660
|
-
await load_node({
|
|
1661
|
-
event,
|
|
1662
|
-
options,
|
|
1663
|
-
state,
|
|
1664
|
-
route: null,
|
|
1665
|
-
url: event.url, // TODO this is redundant, no?
|
|
1666
|
-
params,
|
|
1667
|
-
node: default_layout,
|
|
1668
|
-
$session,
|
|
1669
|
-
stuff: {},
|
|
1670
|
-
is_error: false
|
|
1671
|
-
})
|
|
1672
|
-
);
|
|
1673
|
-
|
|
1674
|
-
const error_loaded = /** @type {Loaded} */ (
|
|
1675
|
-
await load_node({
|
|
1676
|
-
event,
|
|
1677
|
-
options,
|
|
1678
|
-
state,
|
|
1679
|
-
route: null,
|
|
1680
|
-
url: event.url,
|
|
1681
|
-
params,
|
|
1682
|
-
node: default_error,
|
|
1683
|
-
$session,
|
|
1684
|
-
stuff: layout_loaded ? layout_loaded.stuff : {},
|
|
1685
|
-
is_error: true,
|
|
1686
|
-
status,
|
|
1687
|
-
error
|
|
1688
|
-
})
|
|
1689
|
-
);
|
|
1690
|
-
|
|
1691
|
-
return await render_response({
|
|
1692
|
-
options,
|
|
1693
|
-
state,
|
|
1694
|
-
$session,
|
|
1695
|
-
page_config: {
|
|
1696
|
-
hydrate: options.hydrate,
|
|
1697
|
-
router: options.router
|
|
1698
|
-
},
|
|
1699
|
-
stuff: error_loaded.stuff,
|
|
1700
|
-
status,
|
|
1701
|
-
error,
|
|
1702
|
-
branch: [layout_loaded, error_loaded],
|
|
1703
|
-
url: event.url,
|
|
1704
|
-
params,
|
|
1705
|
-
ssr
|
|
1706
|
-
});
|
|
1707
|
-
} catch (err) {
|
|
1708
|
-
const error = coalesce_to_error(err);
|
|
1709
|
-
|
|
1710
|
-
options.handle_error(error, event);
|
|
1711
|
-
|
|
1712
|
-
return new Response(error.stack, {
|
|
1713
|
-
status: 500
|
|
1714
|
-
});
|
|
1715
|
-
}
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
|
-
/**
|
|
1719
|
-
* @typedef {import('./types.js').Loaded} Loaded
|
|
1720
|
-
* @typedef {import('types/internal').SSRNode} SSRNode
|
|
1721
|
-
* @typedef {import('types/internal').SSRRenderOptions} SSRRenderOptions
|
|
1722
|
-
* @typedef {import('types/internal').SSRRenderState} SSRRenderState
|
|
1723
|
-
*/
|
|
1724
|
-
|
|
1725
|
-
/**
|
|
1726
|
-
* @param {{
|
|
1727
|
-
* event: import('types/hooks').RequestEvent;
|
|
1728
|
-
* options: SSRRenderOptions;
|
|
1729
|
-
* state: SSRRenderState;
|
|
1730
|
-
* $session: any;
|
|
1731
|
-
* route: import('types/internal').SSRPage;
|
|
1732
|
-
* params: Record<string, string>;
|
|
1733
|
-
* ssr: boolean;
|
|
1734
|
-
* }} opts
|
|
1735
|
-
* @returns {Promise<Response | undefined>}
|
|
1736
|
-
*/
|
|
1737
|
-
async function respond$1(opts) {
|
|
1738
|
-
const { event, options, state, $session, route, ssr } = opts;
|
|
1739
|
-
|
|
1740
|
-
/** @type {Array<SSRNode | undefined>} */
|
|
1741
|
-
let nodes;
|
|
1742
|
-
|
|
1743
|
-
if (!ssr) {
|
|
1744
|
-
return await render_response({
|
|
1745
|
-
...opts,
|
|
1746
|
-
branch: [],
|
|
1747
|
-
page_config: {
|
|
1748
|
-
hydrate: true,
|
|
1749
|
-
router: true
|
|
1750
|
-
},
|
|
1751
|
-
status: 200,
|
|
1752
|
-
url: event.url,
|
|
1753
|
-
stuff: {}
|
|
1754
|
-
});
|
|
1755
|
-
}
|
|
1756
|
-
|
|
1757
|
-
try {
|
|
1758
|
-
nodes = await Promise.all(
|
|
1759
|
-
route.a.map((n) => options.manifest._.nodes[n] && options.manifest._.nodes[n]())
|
|
1760
|
-
);
|
|
1761
|
-
} catch (err) {
|
|
1762
|
-
const error = coalesce_to_error(err);
|
|
1763
|
-
|
|
1764
|
-
options.handle_error(error, event);
|
|
1765
|
-
|
|
1766
|
-
return await respond_with_error({
|
|
1767
|
-
event,
|
|
1768
|
-
options,
|
|
1769
|
-
state,
|
|
1770
|
-
$session,
|
|
1771
|
-
status: 500,
|
|
1772
|
-
error,
|
|
1773
|
-
ssr
|
|
1774
|
-
});
|
|
1775
|
-
}
|
|
1776
|
-
|
|
1777
|
-
// the leaf node will be present. only layouts may be undefined
|
|
1778
|
-
const leaf = /** @type {SSRNode} */ (nodes[nodes.length - 1]).module;
|
|
1779
|
-
|
|
1780
|
-
let page_config = get_page_config(leaf, options);
|
|
1781
|
-
|
|
1782
|
-
if (!leaf.prerender && state.prerender && !state.prerender.all) {
|
|
1783
|
-
// if the page has `export const prerender = true`, continue,
|
|
1784
|
-
// otherwise bail out at this point
|
|
1785
|
-
return new Response(undefined, {
|
|
1786
|
-
status: 204
|
|
1787
|
-
});
|
|
1788
|
-
}
|
|
1789
|
-
|
|
1790
|
-
/** @type {Array<Loaded>} */
|
|
1791
|
-
let branch = [];
|
|
1792
|
-
|
|
1793
|
-
/** @type {number} */
|
|
1794
|
-
let status = 200;
|
|
1795
|
-
|
|
1796
|
-
/** @type {Error|undefined} */
|
|
1797
|
-
let error;
|
|
1798
|
-
|
|
1799
|
-
/** @type {string[]} */
|
|
1800
|
-
let set_cookie_headers = [];
|
|
1801
|
-
|
|
1802
|
-
let stuff = {};
|
|
1803
|
-
|
|
1804
|
-
ssr: if (ssr) {
|
|
1805
|
-
for (let i = 0; i < nodes.length; i += 1) {
|
|
1806
|
-
const node = nodes[i];
|
|
1807
|
-
|
|
1808
|
-
/** @type {Loaded | undefined} */
|
|
1809
|
-
let loaded;
|
|
1810
|
-
|
|
1811
|
-
if (node) {
|
|
1812
|
-
try {
|
|
1813
|
-
loaded = await load_node({
|
|
1814
|
-
...opts,
|
|
1815
|
-
url: event.url,
|
|
1816
|
-
node,
|
|
1817
|
-
stuff,
|
|
1818
|
-
is_error: false
|
|
1819
|
-
});
|
|
1820
|
-
|
|
1821
|
-
if (!loaded) return;
|
|
1822
|
-
|
|
1823
|
-
set_cookie_headers = set_cookie_headers.concat(loaded.set_cookie_headers);
|
|
1824
|
-
|
|
1825
|
-
if (loaded.loaded.redirect) {
|
|
1826
|
-
return with_cookies(
|
|
1827
|
-
new Response(undefined, {
|
|
1828
|
-
status: loaded.loaded.status,
|
|
1829
|
-
headers: {
|
|
1830
|
-
location: loaded.loaded.redirect
|
|
1831
|
-
}
|
|
1832
|
-
}),
|
|
1833
|
-
set_cookie_headers
|
|
1834
|
-
);
|
|
1835
|
-
}
|
|
1836
|
-
|
|
1837
|
-
if (loaded.loaded.error) {
|
|
1838
|
-
({ status, error } = loaded.loaded);
|
|
1839
|
-
}
|
|
1840
|
-
} catch (err) {
|
|
1841
|
-
const e = coalesce_to_error(err);
|
|
1842
|
-
|
|
1843
|
-
options.handle_error(e, event);
|
|
1844
|
-
|
|
1845
|
-
status = 500;
|
|
1846
|
-
error = e;
|
|
1847
|
-
}
|
|
1848
|
-
|
|
1849
|
-
if (loaded && !error) {
|
|
1850
|
-
branch.push(loaded);
|
|
1851
|
-
}
|
|
1852
|
-
|
|
1853
|
-
if (error) {
|
|
1854
|
-
while (i--) {
|
|
1855
|
-
if (route.b[i]) {
|
|
1856
|
-
const error_node = await options.manifest._.nodes[route.b[i]]();
|
|
1857
|
-
|
|
1858
|
-
/** @type {Loaded} */
|
|
1859
|
-
let node_loaded;
|
|
1860
|
-
let j = i;
|
|
1861
|
-
while (!(node_loaded = branch[j])) {
|
|
1862
|
-
j -= 1;
|
|
1863
|
-
}
|
|
1864
|
-
|
|
1865
|
-
try {
|
|
1866
|
-
const error_loaded = /** @type {import('./types').Loaded} */ (
|
|
1867
|
-
await load_node({
|
|
1868
|
-
...opts,
|
|
1869
|
-
url: event.url,
|
|
1870
|
-
node: error_node,
|
|
1871
|
-
stuff: node_loaded.stuff,
|
|
1872
|
-
is_error: true,
|
|
1873
|
-
status,
|
|
1874
|
-
error
|
|
1875
|
-
})
|
|
1876
|
-
);
|
|
1877
|
-
|
|
1878
|
-
if (error_loaded.loaded.error) {
|
|
1879
|
-
continue;
|
|
1880
|
-
}
|
|
1881
|
-
|
|
1882
|
-
page_config = get_page_config(error_node.module, options);
|
|
1883
|
-
branch = branch.slice(0, j + 1).concat(error_loaded);
|
|
1884
|
-
stuff = { ...node_loaded.stuff, ...error_loaded.stuff };
|
|
1885
|
-
break ssr;
|
|
1886
|
-
} catch (err) {
|
|
1887
|
-
const e = coalesce_to_error(err);
|
|
1888
|
-
|
|
1889
|
-
options.handle_error(e, event);
|
|
1890
|
-
|
|
1891
|
-
continue;
|
|
1892
|
-
}
|
|
1893
|
-
}
|
|
1894
|
-
}
|
|
1895
|
-
|
|
1896
|
-
// TODO backtrack until we find an __error.svelte component
|
|
1897
|
-
// that we can use as the leaf node
|
|
1898
|
-
// for now just return regular error page
|
|
1899
|
-
return with_cookies(
|
|
1900
|
-
await respond_with_error({
|
|
1901
|
-
event,
|
|
1902
|
-
options,
|
|
1903
|
-
state,
|
|
1904
|
-
$session,
|
|
1905
|
-
status,
|
|
1906
|
-
error,
|
|
1907
|
-
ssr
|
|
1908
|
-
}),
|
|
1909
|
-
set_cookie_headers
|
|
1910
|
-
);
|
|
1911
|
-
}
|
|
1912
|
-
}
|
|
1913
|
-
|
|
1914
|
-
if (loaded && loaded.loaded.stuff) {
|
|
1915
|
-
stuff = {
|
|
1916
|
-
...stuff,
|
|
1917
|
-
...loaded.loaded.stuff
|
|
1918
|
-
};
|
|
1919
|
-
}
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
|
|
1923
|
-
try {
|
|
1924
|
-
return with_cookies(
|
|
1925
|
-
await render_response({
|
|
1926
|
-
...opts,
|
|
1927
|
-
stuff,
|
|
1928
|
-
url: event.url,
|
|
1929
|
-
page_config,
|
|
1930
|
-
status,
|
|
1931
|
-
error,
|
|
1932
|
-
branch: branch.filter(Boolean)
|
|
1933
|
-
}),
|
|
1934
|
-
set_cookie_headers
|
|
1935
|
-
);
|
|
1936
|
-
} catch (err) {
|
|
1937
|
-
const error = coalesce_to_error(err);
|
|
1938
|
-
|
|
1939
|
-
options.handle_error(error, event);
|
|
1940
|
-
|
|
1941
|
-
return with_cookies(
|
|
1942
|
-
await respond_with_error({
|
|
1943
|
-
...opts,
|
|
1944
|
-
status: 500,
|
|
1945
|
-
error
|
|
1946
|
-
}),
|
|
1947
|
-
set_cookie_headers
|
|
1948
|
-
);
|
|
1949
|
-
}
|
|
1950
|
-
}
|
|
1951
|
-
|
|
1952
|
-
/**
|
|
1953
|
-
* @param {import('types/internal').SSRComponent} leaf
|
|
1954
|
-
* @param {SSRRenderOptions} options
|
|
1955
|
-
*/
|
|
1956
|
-
function get_page_config(leaf, options) {
|
|
1957
|
-
// TODO remove for 1.0
|
|
1958
|
-
if ('ssr' in leaf) {
|
|
1959
|
-
throw new Error(
|
|
1960
|
-
'`export const ssr` has been removed — use the handle hook instead: https://kit.svelte.dev/docs#hooks-handle'
|
|
1961
|
-
);
|
|
1962
|
-
}
|
|
1963
|
-
|
|
1964
|
-
return {
|
|
1965
|
-
router: 'router' in leaf ? !!leaf.router : options.router,
|
|
1966
|
-
hydrate: 'hydrate' in leaf ? !!leaf.hydrate : options.hydrate
|
|
1967
|
-
};
|
|
1968
|
-
}
|
|
1969
|
-
|
|
1970
|
-
/**
|
|
1971
|
-
* @param {Response} response
|
|
1972
|
-
* @param {string[]} set_cookie_headers
|
|
1973
|
-
*/
|
|
1974
|
-
function with_cookies(response, set_cookie_headers) {
|
|
1975
|
-
if (set_cookie_headers.length) {
|
|
1976
|
-
set_cookie_headers.forEach((value) => {
|
|
1977
|
-
response.headers.append('set-cookie', value);
|
|
1978
|
-
});
|
|
1979
|
-
}
|
|
1980
|
-
return response;
|
|
1981
|
-
}
|
|
1982
|
-
|
|
1983
|
-
/**
|
|
1984
|
-
* @param {import('types/hooks').RequestEvent} event
|
|
1985
|
-
* @param {import('types/internal').SSRPage} route
|
|
1986
|
-
* @param {RegExpExecArray} match
|
|
1987
|
-
* @param {import('types/internal').SSRRenderOptions} options
|
|
1988
|
-
* @param {import('types/internal').SSRRenderState} state
|
|
1989
|
-
* @param {boolean} ssr
|
|
1990
|
-
* @returns {Promise<Response | undefined>}
|
|
1991
|
-
*/
|
|
1992
|
-
async function render_page(event, route, match, options, state, ssr) {
|
|
1993
|
-
if (state.initiator === route) {
|
|
1994
|
-
// infinite request cycle detected
|
|
1995
|
-
return new Response(`Not found: ${event.url.pathname}`, {
|
|
1996
|
-
status: 404
|
|
1997
|
-
});
|
|
1998
|
-
}
|
|
1999
|
-
|
|
2000
|
-
const params = route.params ? decode_params(route.params(match)) : {};
|
|
2001
|
-
|
|
2002
|
-
const $session = await options.hooks.getSession(event);
|
|
2003
|
-
|
|
2004
|
-
const response = await respond$1({
|
|
2005
|
-
event,
|
|
2006
|
-
options,
|
|
2007
|
-
state,
|
|
2008
|
-
$session,
|
|
2009
|
-
route,
|
|
2010
|
-
params,
|
|
2011
|
-
ssr
|
|
2012
|
-
});
|
|
2013
|
-
|
|
2014
|
-
if (response) {
|
|
2015
|
-
return response;
|
|
2016
|
-
}
|
|
2017
|
-
|
|
2018
|
-
if (state.fetched) {
|
|
2019
|
-
// we came here because of a bad request in a `load` function.
|
|
2020
|
-
// rather than render the error page — which could lead to an
|
|
2021
|
-
// infinite loop, if the `load` belonged to the root layout,
|
|
2022
|
-
// we respond with a bare-bones 500
|
|
2023
|
-
return new Response(`Bad request in load function: failed to fetch ${state.fetched}`, {
|
|
2024
|
-
status: 500
|
|
2025
|
-
});
|
|
2026
|
-
}
|
|
2027
|
-
}
|
|
2028
|
-
|
|
2029
|
-
/** @type {import('types/internal').Respond} */
|
|
2030
|
-
async function respond(request, options, state = {}) {
|
|
2031
|
-
const url = new URL(request.url);
|
|
2032
|
-
|
|
2033
|
-
if (url.pathname !== '/' && options.trailing_slash !== 'ignore') {
|
|
2034
|
-
const has_trailing_slash = url.pathname.endsWith('/');
|
|
2035
|
-
|
|
2036
|
-
if (
|
|
2037
|
-
(has_trailing_slash && options.trailing_slash === 'never') ||
|
|
2038
|
-
(!has_trailing_slash &&
|
|
2039
|
-
options.trailing_slash === 'always' &&
|
|
2040
|
-
!(url.pathname.split('/').pop() || '').includes('.'))
|
|
2041
|
-
) {
|
|
2042
|
-
url.pathname = has_trailing_slash ? url.pathname.slice(0, -1) : url.pathname + '/';
|
|
2043
|
-
|
|
2044
|
-
if (url.search === '?') url.search = '';
|
|
2045
|
-
|
|
2046
|
-
return new Response(undefined, {
|
|
2047
|
-
status: 301,
|
|
2048
|
-
headers: {
|
|
2049
|
-
location: url.pathname + url.search
|
|
2050
|
-
}
|
|
2051
|
-
});
|
|
2052
|
-
}
|
|
2053
|
-
}
|
|
2054
|
-
|
|
2055
|
-
const { parameter, allowed } = options.method_override;
|
|
2056
|
-
const method_override = url.searchParams.get(parameter)?.toUpperCase();
|
|
2057
|
-
|
|
2058
|
-
if (method_override) {
|
|
2059
|
-
if (request.method === 'POST') {
|
|
2060
|
-
if (allowed.includes(method_override)) {
|
|
2061
|
-
request = new Proxy(request, {
|
|
2062
|
-
get: (target, property, _receiver) => {
|
|
2063
|
-
if (property === 'method') return method_override;
|
|
2064
|
-
return Reflect.get(target, property, target);
|
|
2065
|
-
}
|
|
2066
|
-
});
|
|
2067
|
-
} else {
|
|
2068
|
-
const verb = allowed.length === 0 ? 'enabled' : 'allowed';
|
|
2069
|
-
const body = `${parameter}=${method_override} is not ${verb}. See https://kit.svelte.dev/docs#configuration-methodoverride`;
|
|
2070
|
-
|
|
2071
|
-
return new Response(body, {
|
|
2072
|
-
status: 400
|
|
2073
|
-
});
|
|
2074
|
-
}
|
|
2075
|
-
} else {
|
|
2076
|
-
throw new Error(`${parameter}=${method_override} is only allowed with POST requests`);
|
|
2077
|
-
}
|
|
2078
|
-
}
|
|
2079
|
-
|
|
2080
|
-
/** @type {import('types/hooks').RequestEvent} */
|
|
2081
|
-
const event = {
|
|
2082
|
-
request,
|
|
2083
|
-
url,
|
|
2084
|
-
params: {},
|
|
2085
|
-
locals: {},
|
|
2086
|
-
platform: state.platform
|
|
2087
|
-
};
|
|
2088
|
-
|
|
2089
|
-
// TODO remove this for 1.0
|
|
2090
|
-
/**
|
|
2091
|
-
* @param {string} property
|
|
2092
|
-
* @param {string} replacement
|
|
2093
|
-
* @param {string} suffix
|
|
2094
|
-
*/
|
|
2095
|
-
const removed = (property, replacement, suffix = '') => ({
|
|
2096
|
-
get: () => {
|
|
2097
|
-
throw new Error(`event.${property} has been replaced by event.${replacement}` + suffix);
|
|
2098
|
-
}
|
|
2099
|
-
});
|
|
2100
|
-
|
|
2101
|
-
const details = '. See https://github.com/sveltejs/kit/pull/3384 for details';
|
|
2102
|
-
|
|
2103
|
-
const body_getter = {
|
|
2104
|
-
get: () => {
|
|
2105
|
-
throw new Error(
|
|
2106
|
-
'To access the request body use the text/json/arrayBuffer/formData methods, e.g. `body = await request.json()`' +
|
|
2107
|
-
details
|
|
2108
|
-
);
|
|
2109
|
-
}
|
|
2110
|
-
};
|
|
2111
|
-
|
|
2112
|
-
Object.defineProperties(event, {
|
|
2113
|
-
method: removed('method', 'request.method', details),
|
|
2114
|
-
headers: removed('headers', 'request.headers', details),
|
|
2115
|
-
origin: removed('origin', 'url.origin'),
|
|
2116
|
-
path: removed('path', 'url.pathname'),
|
|
2117
|
-
query: removed('query', 'url.searchParams'),
|
|
2118
|
-
body: body_getter,
|
|
2119
|
-
rawBody: body_getter
|
|
2120
|
-
});
|
|
2121
|
-
|
|
2122
|
-
let ssr = true;
|
|
2123
|
-
|
|
2124
|
-
try {
|
|
2125
|
-
const response = await options.hooks.handle({
|
|
2126
|
-
event,
|
|
2127
|
-
resolve: async (event, opts) => {
|
|
2128
|
-
if (opts && 'ssr' in opts) ssr = /** @type {boolean} */ (opts.ssr);
|
|
2129
|
-
|
|
2130
|
-
if (state.prerender && state.prerender.fallback) {
|
|
2131
|
-
return await render_response({
|
|
2132
|
-
url: event.url,
|
|
2133
|
-
params: event.params,
|
|
2134
|
-
options,
|
|
2135
|
-
state,
|
|
2136
|
-
$session: await options.hooks.getSession(event),
|
|
2137
|
-
page_config: { router: true, hydrate: true },
|
|
2138
|
-
stuff: {},
|
|
2139
|
-
status: 200,
|
|
2140
|
-
branch: [],
|
|
2141
|
-
ssr: false
|
|
2142
|
-
});
|
|
2143
|
-
}
|
|
2144
|
-
|
|
2145
|
-
let decoded = decodeURI(event.url.pathname);
|
|
2146
|
-
|
|
2147
|
-
if (options.paths.base) {
|
|
2148
|
-
if (!decoded.startsWith(options.paths.base)) {
|
|
2149
|
-
return new Response(undefined, { status: 404 });
|
|
2150
|
-
}
|
|
2151
|
-
decoded = decoded.slice(options.paths.base.length) || '/';
|
|
2152
|
-
}
|
|
2153
|
-
|
|
2154
|
-
for (const route of options.manifest._.routes) {
|
|
2155
|
-
const match = route.pattern.exec(decoded);
|
|
2156
|
-
if (!match) continue;
|
|
2157
|
-
|
|
2158
|
-
const response =
|
|
2159
|
-
route.type === 'endpoint'
|
|
2160
|
-
? await render_endpoint(event, route, match)
|
|
2161
|
-
: await render_page(event, route, match, options, state, ssr);
|
|
2162
|
-
|
|
2163
|
-
if (response) {
|
|
2164
|
-
// respond with 304 if etag matches
|
|
2165
|
-
if (response.status === 200 && response.headers.has('etag')) {
|
|
2166
|
-
let if_none_match_value = request.headers.get('if-none-match');
|
|
2167
|
-
|
|
2168
|
-
// ignore W/ prefix https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match#directives
|
|
2169
|
-
if (if_none_match_value?.startsWith('W/"')) {
|
|
2170
|
-
if_none_match_value = if_none_match_value.substring(2);
|
|
2171
|
-
}
|
|
2172
|
-
|
|
2173
|
-
const etag = /** @type {string} */ (response.headers.get('etag'));
|
|
2174
|
-
|
|
2175
|
-
if (if_none_match_value === etag) {
|
|
2176
|
-
const headers = new Headers({ etag });
|
|
2177
|
-
|
|
2178
|
-
// https://datatracker.ietf.org/doc/html/rfc7232#section-4.1
|
|
2179
|
-
for (const key of [
|
|
2180
|
-
'cache-control',
|
|
2181
|
-
'content-location',
|
|
2182
|
-
'date',
|
|
2183
|
-
'expires',
|
|
2184
|
-
'vary'
|
|
2185
|
-
]) {
|
|
2186
|
-
const value = response.headers.get(key);
|
|
2187
|
-
if (value) headers.set(key, value);
|
|
2188
|
-
}
|
|
2189
|
-
|
|
2190
|
-
return new Response(undefined, {
|
|
2191
|
-
status: 304,
|
|
2192
|
-
headers
|
|
2193
|
-
});
|
|
2194
|
-
}
|
|
2195
|
-
}
|
|
2196
|
-
|
|
2197
|
-
return response;
|
|
2198
|
-
}
|
|
2199
|
-
}
|
|
2200
|
-
|
|
2201
|
-
// if this request came direct from the user, rather than
|
|
2202
|
-
// via a `fetch` in a `load`, render a 404 page
|
|
2203
|
-
if (!state.initiator) {
|
|
2204
|
-
const $session = await options.hooks.getSession(event);
|
|
2205
|
-
return await respond_with_error({
|
|
2206
|
-
event,
|
|
2207
|
-
options,
|
|
2208
|
-
state,
|
|
2209
|
-
$session,
|
|
2210
|
-
status: 404,
|
|
2211
|
-
error: new Error(`Not found: ${event.url.pathname}`),
|
|
2212
|
-
ssr
|
|
2213
|
-
});
|
|
2214
|
-
}
|
|
2215
|
-
|
|
2216
|
-
// we can't load the endpoint from our own manifest,
|
|
2217
|
-
// so we need to make an actual HTTP request
|
|
2218
|
-
return await fetch(request);
|
|
2219
|
-
},
|
|
2220
|
-
|
|
2221
|
-
// TODO remove for 1.0
|
|
2222
|
-
// @ts-expect-error
|
|
2223
|
-
get request() {
|
|
2224
|
-
throw new Error('request in handle has been replaced with event' + details);
|
|
2225
|
-
}
|
|
2226
|
-
});
|
|
2227
|
-
|
|
2228
|
-
// TODO for 1.0, change the error message to point to docs rather than PR
|
|
2229
|
-
if (response && !(response instanceof Response)) {
|
|
2230
|
-
throw new Error('handle must return a Response object' + details);
|
|
2231
|
-
}
|
|
2232
|
-
|
|
2233
|
-
return response;
|
|
2234
|
-
} catch (/** @type {unknown} */ e) {
|
|
2235
|
-
const error = coalesce_to_error(e);
|
|
2236
|
-
|
|
2237
|
-
options.handle_error(error, event);
|
|
2238
|
-
|
|
2239
|
-
try {
|
|
2240
|
-
const $session = await options.hooks.getSession(event);
|
|
2241
|
-
return await respond_with_error({
|
|
2242
|
-
event,
|
|
2243
|
-
options,
|
|
2244
|
-
state,
|
|
2245
|
-
$session,
|
|
2246
|
-
status: 500,
|
|
2247
|
-
error,
|
|
2248
|
-
ssr
|
|
2249
|
-
});
|
|
2250
|
-
} catch (/** @type {unknown} */ e) {
|
|
2251
|
-
const error = coalesce_to_error(e);
|
|
2252
|
-
|
|
2253
|
-
return new Response(options.dev ? error.stack : error.message, {
|
|
2254
|
-
status: 500
|
|
2255
|
-
});
|
|
2256
|
-
}
|
|
2257
|
-
}
|
|
2258
|
-
}
|
|
2259
|
-
|
|
2260
26
|
/**
|
|
2261
27
|
* @param {import('types/config').ValidatedConfig} config
|
|
2262
28
|
* @param {string} cwd
|
|
@@ -2271,6 +37,9 @@ async function create_plugin(config, cwd) {
|
|
|
2271
37
|
amp = (await import('./amp_hook.js')).handle;
|
|
2272
38
|
}
|
|
2273
39
|
|
|
40
|
+
/** @type {import('types/internal').Respond} */
|
|
41
|
+
const respond = (await import(`${runtime}/server/index.js`)).respond;
|
|
42
|
+
|
|
2274
43
|
return {
|
|
2275
44
|
name: 'vite-plugin-svelte-kit',
|
|
2276
45
|
|
|
@@ -2313,7 +82,7 @@ async function create_plugin(config, cwd) {
|
|
|
2313
82
|
const styles = {};
|
|
2314
83
|
|
|
2315
84
|
for (const dep of deps) {
|
|
2316
|
-
const parsed = new URL
|
|
85
|
+
const parsed = new URL(dep.url, 'http://localhost/');
|
|
2317
86
|
const query = parsed.searchParams;
|
|
2318
87
|
|
|
2319
88
|
// TODO what about .scss files, etc?
|
|
@@ -2388,7 +157,7 @@ async function create_plugin(config, cwd) {
|
|
|
2388
157
|
|
|
2389
158
|
const base = `${vite.config.server.https ? 'https' : 'http'}://${req.headers.host}`;
|
|
2390
159
|
|
|
2391
|
-
const decoded = decodeURI(new URL
|
|
160
|
+
const decoded = decodeURI(new URL(base + req.url).pathname);
|
|
2392
161
|
|
|
2393
162
|
if (decoded.startsWith(assets)) {
|
|
2394
163
|
const pathname = decoded.slice(assets.length);
|