@sveltejs/kit 1.0.0-next.43 → 1.0.0-next.430
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 +12 -9
- package/package.json +95 -63
- package/src/cli.js +112 -0
- package/src/core/adapt/builder.js +207 -0
- package/src/core/adapt/index.js +19 -0
- package/src/core/config/index.js +86 -0
- package/src/core/config/options.js +488 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/constants.js +5 -0
- package/src/core/env.js +97 -0
- package/src/core/generate_manifest/index.js +99 -0
- package/src/core/prerender/crawl.js +194 -0
- package/src/core/prerender/prerender.js +378 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +506 -0
- package/src/core/sync/create_manifest_data/types.d.ts +40 -0
- package/src/core/sync/sync.js +59 -0
- package/src/core/sync/utils.js +44 -0
- package/src/core/sync/write_ambient.js +27 -0
- package/src/core/sync/write_client_manifest.js +82 -0
- package/src/core/sync/write_matchers.js +25 -0
- package/src/core/sync/write_root.js +91 -0
- package/src/core/sync/write_tsconfig.js +195 -0
- package/src/core/sync/write_types.js +775 -0
- package/src/core/utils.js +70 -0
- package/src/hooks.js +26 -0
- package/src/index/index.js +45 -0
- package/src/index/private.js +33 -0
- package/src/node/index.js +145 -0
- package/src/node/polyfills.js +40 -0
- package/src/runtime/app/env.js +11 -0
- package/src/runtime/app/navigation.js +22 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +102 -0
- package/src/runtime/client/ambient.d.ts +17 -0
- package/src/runtime/client/client.js +1289 -0
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +36 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +46 -0
- package/src/runtime/client/types.d.ts +105 -0
- package/src/runtime/client/utils.js +113 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/env/dynamic/private.js +1 -0
- package/src/runtime/env/dynamic/public.js +1 -0
- package/src/runtime/env-private.js +7 -0
- package/src/runtime/env-public.js +7 -0
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +16 -0
- package/src/runtime/paths.js +11 -0
- package/src/runtime/server/endpoint.js +58 -0
- package/src/runtime/server/index.js +448 -0
- package/src/runtime/server/page/cookie.js +25 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +249 -0
- package/src/runtime/server/page/fetch.js +266 -0
- package/src/runtime/server/page/index.js +416 -0
- package/src/runtime/server/page/load_data.js +135 -0
- package/src/runtime/server/page/render.js +362 -0
- package/src/runtime/server/page/respond_with_error.js +94 -0
- package/src/runtime/server/page/types.d.ts +44 -0
- package/src/runtime/server/utils.js +116 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +104 -0
- package/src/utils/filesystem.js +108 -0
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +108 -0
- package/src/utils/url.js +97 -0
- package/src/vite/build/build_server.js +337 -0
- package/src/vite/build/build_service_worker.js +90 -0
- package/src/vite/build/utils.js +160 -0
- package/src/vite/dev/index.js +551 -0
- package/src/vite/index.js +574 -0
- package/src/vite/preview/index.js +186 -0
- package/src/vite/types.d.ts +3 -0
- package/src/vite/utils.js +345 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +357 -0
- package/types/index.d.ts +343 -0
- package/types/internal.d.ts +308 -0
- package/types/private.d.ts +209 -0
- package/CHANGELOG.md +0 -431
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/env.js +0 -5
- package/assets/runtime/app/navigation.js +0 -41
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -93
- package/assets/runtime/chunks/utils.js +0 -19
- package/assets/runtime/internal/singletons.js +0 -23
- package/assets/runtime/internal/start.js +0 -770
- package/assets/runtime/paths.js +0 -12
- package/dist/.DS_Store +0 -0
- package/dist/chunks/index.js +0 -3521
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -538
- package/dist/chunks/index5.js +0 -761
- package/dist/chunks/index6.js +0 -322
- package/dist/chunks/standard.js +0 -99
- package/dist/chunks/utils.js +0 -83
- package/dist/cli.js +0 -546
- package/dist/ssr.js +0 -2581
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import { render_endpoint } from './endpoint.js';
|
|
2
|
+
import { render_page } from './page/index.js';
|
|
3
|
+
import { render_response } from './page/render.js';
|
|
4
|
+
import { respond_with_error } from './page/respond_with_error.js';
|
|
5
|
+
import { coalesce_to_error, normalize_error } from '../../utils/error.js';
|
|
6
|
+
import { serialize_error, GENERIC_ERROR, error_to_pojo } from './utils.js';
|
|
7
|
+
import { decode_params, normalize_path } from '../../utils/url.js';
|
|
8
|
+
import { exec } from '../../utils/routing.js';
|
|
9
|
+
import { negotiate } from '../../utils/http.js';
|
|
10
|
+
import { HttpError, Redirect } from '../../index/private.js';
|
|
11
|
+
import { load_server_data } from './page/load_data.js';
|
|
12
|
+
import { json } from '../../index/index.js';
|
|
13
|
+
|
|
14
|
+
/* global __SVELTEKIT_ADAPTER_NAME__ */
|
|
15
|
+
|
|
16
|
+
const DATA_SUFFIX = '/__data.json';
|
|
17
|
+
|
|
18
|
+
/** @param {{ html: string }} opts */
|
|
19
|
+
const default_transform = ({ html }) => html;
|
|
20
|
+
|
|
21
|
+
/** @type {import('types').Respond} */
|
|
22
|
+
export async function respond(request, options, state) {
|
|
23
|
+
let url = new URL(request.url);
|
|
24
|
+
|
|
25
|
+
const { parameter, allowed } = options.method_override;
|
|
26
|
+
const method_override = url.searchParams.get(parameter)?.toUpperCase();
|
|
27
|
+
|
|
28
|
+
if (method_override) {
|
|
29
|
+
if (request.method === 'POST') {
|
|
30
|
+
if (allowed.includes(method_override)) {
|
|
31
|
+
request = new Proxy(request, {
|
|
32
|
+
get: (target, property, _receiver) => {
|
|
33
|
+
if (property === 'method') return method_override;
|
|
34
|
+
return Reflect.get(target, property, target);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
} else {
|
|
38
|
+
const verb = allowed.length === 0 ? 'enabled' : 'allowed';
|
|
39
|
+
const body = `${parameter}=${method_override} is not ${verb}. See https://kit.svelte.dev/docs/configuration#methodoverride`;
|
|
40
|
+
|
|
41
|
+
return new Response(body, {
|
|
42
|
+
status: 400
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error(`${parameter}=${method_override} is only allowed with POST requests`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let decoded;
|
|
51
|
+
try {
|
|
52
|
+
decoded = decodeURI(url.pathname);
|
|
53
|
+
} catch {
|
|
54
|
+
return new Response('Malformed URI', { status: 400 });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** @type {import('types').SSRRoute | null} */
|
|
58
|
+
let route = null;
|
|
59
|
+
|
|
60
|
+
/** @type {Record<string, string>} */
|
|
61
|
+
let params = {};
|
|
62
|
+
|
|
63
|
+
if (options.paths.base && !state.prerendering?.fallback) {
|
|
64
|
+
if (!decoded.startsWith(options.paths.base)) {
|
|
65
|
+
return new Response('Not found', { status: 404 });
|
|
66
|
+
}
|
|
67
|
+
decoded = decoded.slice(options.paths.base.length) || '/';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const is_data_request = decoded.endsWith(DATA_SUFFIX);
|
|
71
|
+
|
|
72
|
+
if (is_data_request) {
|
|
73
|
+
const data_suffix_length = DATA_SUFFIX.length - (options.trailing_slash === 'always' ? 1 : 0);
|
|
74
|
+
decoded = decoded.slice(0, -data_suffix_length) || '/';
|
|
75
|
+
url = new URL(url.origin + url.pathname.slice(0, -data_suffix_length) + url.search);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!state.prerendering?.fallback) {
|
|
79
|
+
const matchers = await options.manifest._.matchers();
|
|
80
|
+
|
|
81
|
+
for (const candidate of options.manifest._.routes) {
|
|
82
|
+
const match = candidate.pattern.exec(decoded);
|
|
83
|
+
if (!match) continue;
|
|
84
|
+
|
|
85
|
+
const matched = exec(match, candidate.names, candidate.types, matchers);
|
|
86
|
+
if (matched) {
|
|
87
|
+
route = candidate;
|
|
88
|
+
params = decode_params(matched);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (route) {
|
|
95
|
+
if (route.type === 'page') {
|
|
96
|
+
const normalized = normalize_path(url.pathname, options.trailing_slash);
|
|
97
|
+
|
|
98
|
+
if (normalized !== url.pathname && !state.prerendering?.fallback) {
|
|
99
|
+
return new Response(undefined, {
|
|
100
|
+
status: 301,
|
|
101
|
+
headers: {
|
|
102
|
+
'x-sveltekit-normalize': '1',
|
|
103
|
+
location:
|
|
104
|
+
// ensure paths starting with '//' are not treated as protocol-relative
|
|
105
|
+
(normalized.startsWith('//') ? url.origin + normalized : normalized) +
|
|
106
|
+
(url.search === '?' ? '' : url.search)
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} else if (is_data_request) {
|
|
111
|
+
// requesting /__data.json should fail for a standalone endpoint
|
|
112
|
+
return new Response(undefined, {
|
|
113
|
+
status: 404
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** @type {import('types').ResponseHeaders} */
|
|
119
|
+
const headers = {};
|
|
120
|
+
|
|
121
|
+
/** @type {string[]} */
|
|
122
|
+
const cookies = [];
|
|
123
|
+
|
|
124
|
+
/** @type {import('types').RequestEvent} */
|
|
125
|
+
const event = {
|
|
126
|
+
get clientAddress() {
|
|
127
|
+
if (!state.getClientAddress) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`${__SVELTEKIT_ADAPTER_NAME__} does not specify getClientAddress. Please raise an issue`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
Object.defineProperty(event, 'clientAddress', {
|
|
134
|
+
value: state.getClientAddress()
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return event.clientAddress;
|
|
138
|
+
},
|
|
139
|
+
locals: {},
|
|
140
|
+
params,
|
|
141
|
+
platform: state.platform,
|
|
142
|
+
request,
|
|
143
|
+
routeId: route && route.id,
|
|
144
|
+
setHeaders: (new_headers) => {
|
|
145
|
+
for (const key in new_headers) {
|
|
146
|
+
const lower = key.toLowerCase();
|
|
147
|
+
const value = new_headers[key];
|
|
148
|
+
|
|
149
|
+
if (lower === 'set-cookie') {
|
|
150
|
+
const new_cookies = /** @type {string[]} */ (Array.isArray(value) ? value : [value]);
|
|
151
|
+
|
|
152
|
+
for (const cookie of new_cookies) {
|
|
153
|
+
if (cookies.includes(cookie)) {
|
|
154
|
+
throw new Error(`"${key}" header already has cookie with same value`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
cookies.push(cookie);
|
|
158
|
+
}
|
|
159
|
+
} else if (lower in headers) {
|
|
160
|
+
throw new Error(`"${key}" header is already set`);
|
|
161
|
+
} else {
|
|
162
|
+
headers[lower] = value;
|
|
163
|
+
|
|
164
|
+
if (state.prerendering && lower === 'cache-control') {
|
|
165
|
+
state.prerendering.cache = /** @type {string} */ (value);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
url
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// TODO remove this for 1.0
|
|
174
|
+
/**
|
|
175
|
+
* @param {string} property
|
|
176
|
+
* @param {string} replacement
|
|
177
|
+
* @param {string} suffix
|
|
178
|
+
*/
|
|
179
|
+
const removed = (property, replacement, suffix = '') => ({
|
|
180
|
+
get: () => {
|
|
181
|
+
throw new Error(`event.${property} has been replaced by event.${replacement}` + suffix);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const details = '. See https://github.com/sveltejs/kit/pull/3384 for details';
|
|
186
|
+
|
|
187
|
+
const body_getter = {
|
|
188
|
+
get: () => {
|
|
189
|
+
throw new Error(
|
|
190
|
+
'To access the request body use the text/json/arrayBuffer/formData methods, e.g. `body = await request.json()`' +
|
|
191
|
+
details
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
Object.defineProperties(event, {
|
|
197
|
+
method: removed('method', 'request.method', details),
|
|
198
|
+
headers: removed('headers', 'request.headers', details),
|
|
199
|
+
origin: removed('origin', 'url.origin'),
|
|
200
|
+
path: removed('path', 'url.pathname'),
|
|
201
|
+
query: removed('query', 'url.searchParams'),
|
|
202
|
+
body: body_getter,
|
|
203
|
+
rawBody: body_getter
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
/** @type {import('types').RequiredResolveOptions} */
|
|
207
|
+
let resolve_opts = {
|
|
208
|
+
ssr: true,
|
|
209
|
+
transformPageChunk: default_transform
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
// TODO match route before calling handle?
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
const response = await options.hooks.handle({
|
|
216
|
+
event,
|
|
217
|
+
resolve: async (event, opts) => {
|
|
218
|
+
if (opts) {
|
|
219
|
+
// TODO remove for 1.0
|
|
220
|
+
// @ts-expect-error
|
|
221
|
+
if (opts.transformPage) {
|
|
222
|
+
throw new Error(
|
|
223
|
+
'transformPage has been replaced by transformPageChunk — see https://github.com/sveltejs/kit/pull/5657 for more information'
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
resolve_opts = {
|
|
228
|
+
ssr: opts.ssr !== false,
|
|
229
|
+
transformPageChunk: opts.transformPageChunk || default_transform
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (state.prerendering?.fallback) {
|
|
234
|
+
return await render_response({
|
|
235
|
+
event,
|
|
236
|
+
options,
|
|
237
|
+
state,
|
|
238
|
+
page_config: { router: true, hydrate: true },
|
|
239
|
+
status: 200,
|
|
240
|
+
error: null,
|
|
241
|
+
branch: [],
|
|
242
|
+
fetched: [],
|
|
243
|
+
validation_errors: undefined,
|
|
244
|
+
cookies: [],
|
|
245
|
+
resolve_opts: {
|
|
246
|
+
...resolve_opts,
|
|
247
|
+
ssr: false
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (route) {
|
|
253
|
+
/** @type {Response} */
|
|
254
|
+
let response;
|
|
255
|
+
if (is_data_request && route.type === 'page') {
|
|
256
|
+
try {
|
|
257
|
+
/** @type {Redirect | HttpError | Error} */
|
|
258
|
+
let error;
|
|
259
|
+
|
|
260
|
+
// TODO only get the data we need for the navigation
|
|
261
|
+
const promises = [...route.layouts, route.leaf].map(async (n, i) => {
|
|
262
|
+
try {
|
|
263
|
+
if (error) return;
|
|
264
|
+
|
|
265
|
+
// == because it could be undefined (in dev) or null (in build, because of JSON.stringify)
|
|
266
|
+
const node = n == undefined ? n : await options.manifest._.nodes[n]();
|
|
267
|
+
return {
|
|
268
|
+
// TODO return `uses`, so we can reuse server data effectively
|
|
269
|
+
data: await load_server_data({
|
|
270
|
+
dev: options.dev,
|
|
271
|
+
event,
|
|
272
|
+
node,
|
|
273
|
+
parent: async () => {
|
|
274
|
+
/** @type {Record<string, any>} */
|
|
275
|
+
const data = {};
|
|
276
|
+
for (let j = 0; j < i; j += 1) {
|
|
277
|
+
const parent = await promises[j];
|
|
278
|
+
if (!parent || parent instanceof HttpError || 'error' in parent) {
|
|
279
|
+
return data;
|
|
280
|
+
}
|
|
281
|
+
Object.assign(data, parent.data);
|
|
282
|
+
}
|
|
283
|
+
return data;
|
|
284
|
+
}
|
|
285
|
+
})
|
|
286
|
+
};
|
|
287
|
+
} catch (e) {
|
|
288
|
+
error = normalize_error(e);
|
|
289
|
+
|
|
290
|
+
if (error instanceof Redirect) {
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (error instanceof HttpError) {
|
|
295
|
+
return error; // { status, message }
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
options.handle_error(error, event);
|
|
299
|
+
|
|
300
|
+
return {
|
|
301
|
+
error: error_to_pojo(error, options.get_stack)
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
response = json({
|
|
307
|
+
type: 'data',
|
|
308
|
+
nodes: await Promise.all(promises)
|
|
309
|
+
});
|
|
310
|
+
} catch (e) {
|
|
311
|
+
const error = normalize_error(e);
|
|
312
|
+
|
|
313
|
+
if (error instanceof Redirect) {
|
|
314
|
+
response = json({
|
|
315
|
+
type: 'redirect',
|
|
316
|
+
location: error.location
|
|
317
|
+
});
|
|
318
|
+
} else {
|
|
319
|
+
response = json(error_to_pojo(error, options.get_stack), { status: 500 });
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
response =
|
|
324
|
+
route.type === 'endpoint'
|
|
325
|
+
? await render_endpoint(event, route)
|
|
326
|
+
: await render_page(event, route, options, state, resolve_opts);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (!is_data_request) {
|
|
330
|
+
// we only want to set cookies on __data.json requests, we don't
|
|
331
|
+
// want to cache stuff erroneously etc
|
|
332
|
+
for (const key in headers) {
|
|
333
|
+
const value = headers[key];
|
|
334
|
+
response.headers.set(key, /** @type {string} */ (value));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
for (const cookie of cookies) {
|
|
339
|
+
response.headers.append('set-cookie', cookie);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// respond with 304 if etag matches
|
|
343
|
+
if (response.status === 200 && response.headers.has('etag')) {
|
|
344
|
+
let if_none_match_value = request.headers.get('if-none-match');
|
|
345
|
+
|
|
346
|
+
// ignore W/ prefix https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match#directives
|
|
347
|
+
if (if_none_match_value?.startsWith('W/"')) {
|
|
348
|
+
if_none_match_value = if_none_match_value.substring(2);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const etag = /** @type {string} */ (response.headers.get('etag'));
|
|
352
|
+
|
|
353
|
+
if (if_none_match_value === etag) {
|
|
354
|
+
const headers = new Headers({ etag });
|
|
355
|
+
|
|
356
|
+
// https://datatracker.ietf.org/doc/html/rfc7232#section-4.1
|
|
357
|
+
for (const key of ['cache-control', 'content-location', 'date', 'expires', 'vary']) {
|
|
358
|
+
const value = response.headers.get(key);
|
|
359
|
+
if (value) headers.set(key, value);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return new Response(undefined, {
|
|
363
|
+
status: 304,
|
|
364
|
+
headers
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return response;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (state.initiator === GENERIC_ERROR) {
|
|
373
|
+
return new Response('Internal Server Error', {
|
|
374
|
+
status: 500
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// if this request came direct from the user, rather than
|
|
379
|
+
// via a `fetch` in a `load`, render a 404 page
|
|
380
|
+
if (!state.initiator) {
|
|
381
|
+
return await respond_with_error({
|
|
382
|
+
event,
|
|
383
|
+
options,
|
|
384
|
+
state,
|
|
385
|
+
status: 404,
|
|
386
|
+
error: new Error(`Not found: ${event.url.pathname}`),
|
|
387
|
+
resolve_opts
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (state.prerendering) {
|
|
392
|
+
return new Response('not found', { status: 404 });
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// we can't load the endpoint from our own manifest,
|
|
396
|
+
// so we need to make an actual HTTP request
|
|
397
|
+
return await fetch(request);
|
|
398
|
+
},
|
|
399
|
+
|
|
400
|
+
// TODO remove for 1.0
|
|
401
|
+
// @ts-expect-error
|
|
402
|
+
get request() {
|
|
403
|
+
throw new Error('request in handle has been replaced with event' + details);
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// TODO for 1.0, change the error message to point to docs rather than PR
|
|
408
|
+
if (response && !(response instanceof Response)) {
|
|
409
|
+
throw new Error('handle must return a Response object' + details);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return response;
|
|
413
|
+
} catch (/** @type {unknown} */ e) {
|
|
414
|
+
const error = coalesce_to_error(e);
|
|
415
|
+
|
|
416
|
+
options.handle_error(error, event);
|
|
417
|
+
|
|
418
|
+
const type = negotiate(event.request.headers.get('accept') || 'text/html', [
|
|
419
|
+
'text/html',
|
|
420
|
+
'application/json'
|
|
421
|
+
]);
|
|
422
|
+
|
|
423
|
+
if (is_data_request || type === 'application/json') {
|
|
424
|
+
return new Response(serialize_error(error, options.get_stack), {
|
|
425
|
+
status: 500,
|
|
426
|
+
headers: { 'content-type': 'application/json; charset=utf-8' }
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// TODO is this necessary? should we just return a plain 500 at this point?
|
|
431
|
+
try {
|
|
432
|
+
return await respond_with_error({
|
|
433
|
+
event,
|
|
434
|
+
options,
|
|
435
|
+
state,
|
|
436
|
+
status: 500,
|
|
437
|
+
error,
|
|
438
|
+
resolve_opts
|
|
439
|
+
});
|
|
440
|
+
} catch (/** @type {unknown} */ e) {
|
|
441
|
+
const error = coalesce_to_error(e);
|
|
442
|
+
|
|
443
|
+
return new Response(options.dev ? error.stack : error.message, {
|
|
444
|
+
status: 500
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string} hostname
|
|
3
|
+
* @param {string} [constraint]
|
|
4
|
+
*/
|
|
5
|
+
export function domain_matches(hostname, constraint) {
|
|
6
|
+
if (!constraint) return true;
|
|
7
|
+
|
|
8
|
+
const normalized = constraint[0] === '.' ? constraint.slice(1) : constraint;
|
|
9
|
+
|
|
10
|
+
if (hostname === normalized) return true;
|
|
11
|
+
return hostname.endsWith('.' + normalized);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} path
|
|
16
|
+
* @param {string} [constraint]
|
|
17
|
+
*/
|
|
18
|
+
export function path_matches(path, constraint) {
|
|
19
|
+
if (!constraint) return true;
|
|
20
|
+
|
|
21
|
+
const normalized = constraint.endsWith('/') ? constraint.slice(0, -1) : constraint;
|
|
22
|
+
|
|
23
|
+
if (path === normalized) return true;
|
|
24
|
+
return path.startsWith(normalized + '/');
|
|
25
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SHA-256 hashing function adapted from https://bitwiseshiftleft.github.io/sjcl
|
|
5
|
+
* modified and redistributed under BSD license
|
|
6
|
+
* @param {string} data
|
|
7
|
+
*/
|
|
8
|
+
export function sha256(data) {
|
|
9
|
+
if (!key[0]) precompute();
|
|
10
|
+
|
|
11
|
+
const out = init.slice(0);
|
|
12
|
+
const array = encode(data);
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < array.length; i += 16) {
|
|
15
|
+
const w = array.subarray(i, i + 16);
|
|
16
|
+
|
|
17
|
+
let tmp;
|
|
18
|
+
let a;
|
|
19
|
+
let b;
|
|
20
|
+
|
|
21
|
+
let out0 = out[0];
|
|
22
|
+
let out1 = out[1];
|
|
23
|
+
let out2 = out[2];
|
|
24
|
+
let out3 = out[3];
|
|
25
|
+
let out4 = out[4];
|
|
26
|
+
let out5 = out[5];
|
|
27
|
+
let out6 = out[6];
|
|
28
|
+
let out7 = out[7];
|
|
29
|
+
|
|
30
|
+
/* Rationale for placement of |0 :
|
|
31
|
+
* If a value can overflow is original 32 bits by a factor of more than a few
|
|
32
|
+
* million (2^23 ish), there is a possibility that it might overflow the
|
|
33
|
+
* 53-bit mantissa and lose precision.
|
|
34
|
+
*
|
|
35
|
+
* To avoid this, we clamp back to 32 bits by |'ing with 0 on any value that
|
|
36
|
+
* propagates around the loop, and on the hash state out[]. I don't believe
|
|
37
|
+
* that the clamps on out4 and on out0 are strictly necessary, but it's close
|
|
38
|
+
* (for out4 anyway), and better safe than sorry.
|
|
39
|
+
*
|
|
40
|
+
* The clamps on out[] are necessary for the output to be correct even in the
|
|
41
|
+
* common case and for short inputs.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < 64; i++) {
|
|
45
|
+
// load up the input word for this round
|
|
46
|
+
|
|
47
|
+
if (i < 16) {
|
|
48
|
+
tmp = w[i];
|
|
49
|
+
} else {
|
|
50
|
+
a = w[(i + 1) & 15];
|
|
51
|
+
|
|
52
|
+
b = w[(i + 14) & 15];
|
|
53
|
+
|
|
54
|
+
tmp = w[i & 15] =
|
|
55
|
+
(((a >>> 7) ^ (a >>> 18) ^ (a >>> 3) ^ (a << 25) ^ (a << 14)) +
|
|
56
|
+
((b >>> 17) ^ (b >>> 19) ^ (b >>> 10) ^ (b << 15) ^ (b << 13)) +
|
|
57
|
+
w[i & 15] +
|
|
58
|
+
w[(i + 9) & 15]) |
|
|
59
|
+
0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
tmp =
|
|
63
|
+
tmp +
|
|
64
|
+
out7 +
|
|
65
|
+
((out4 >>> 6) ^ (out4 >>> 11) ^ (out4 >>> 25) ^ (out4 << 26) ^ (out4 << 21) ^ (out4 << 7)) +
|
|
66
|
+
(out6 ^ (out4 & (out5 ^ out6))) +
|
|
67
|
+
key[i]; // | 0;
|
|
68
|
+
|
|
69
|
+
// shift register
|
|
70
|
+
out7 = out6;
|
|
71
|
+
out6 = out5;
|
|
72
|
+
out5 = out4;
|
|
73
|
+
|
|
74
|
+
out4 = (out3 + tmp) | 0;
|
|
75
|
+
|
|
76
|
+
out3 = out2;
|
|
77
|
+
out2 = out1;
|
|
78
|
+
out1 = out0;
|
|
79
|
+
|
|
80
|
+
out0 =
|
|
81
|
+
(tmp +
|
|
82
|
+
((out1 & out2) ^ (out3 & (out1 ^ out2))) +
|
|
83
|
+
((out1 >>> 2) ^
|
|
84
|
+
(out1 >>> 13) ^
|
|
85
|
+
(out1 >>> 22) ^
|
|
86
|
+
(out1 << 30) ^
|
|
87
|
+
(out1 << 19) ^
|
|
88
|
+
(out1 << 10))) |
|
|
89
|
+
0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
out[0] = (out[0] + out0) | 0;
|
|
93
|
+
out[1] = (out[1] + out1) | 0;
|
|
94
|
+
out[2] = (out[2] + out2) | 0;
|
|
95
|
+
out[3] = (out[3] + out3) | 0;
|
|
96
|
+
out[4] = (out[4] + out4) | 0;
|
|
97
|
+
out[5] = (out[5] + out5) | 0;
|
|
98
|
+
out[6] = (out[6] + out6) | 0;
|
|
99
|
+
out[7] = (out[7] + out7) | 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const bytes = new Uint8Array(out.buffer);
|
|
103
|
+
reverse_endianness(bytes);
|
|
104
|
+
|
|
105
|
+
return base64(bytes);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The SHA-256 initialization vector */
|
|
109
|
+
const init = new Uint32Array(8);
|
|
110
|
+
|
|
111
|
+
/** The SHA-256 hash key */
|
|
112
|
+
const key = new Uint32Array(64);
|
|
113
|
+
|
|
114
|
+
/** Function to precompute init and key. */
|
|
115
|
+
function precompute() {
|
|
116
|
+
/** @param {number} x */
|
|
117
|
+
function frac(x) {
|
|
118
|
+
return (x - Math.floor(x)) * 0x100000000;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let prime = 2;
|
|
122
|
+
|
|
123
|
+
for (let i = 0; i < 64; prime++) {
|
|
124
|
+
let is_prime = true;
|
|
125
|
+
|
|
126
|
+
for (let factor = 2; factor * factor <= prime; factor++) {
|
|
127
|
+
if (prime % factor === 0) {
|
|
128
|
+
is_prime = false;
|
|
129
|
+
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (is_prime) {
|
|
135
|
+
if (i < 8) {
|
|
136
|
+
init[i] = frac(prime ** (1 / 2));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
key[i] = frac(prime ** (1 / 3));
|
|
140
|
+
|
|
141
|
+
i++;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** @param {Uint8Array} bytes */
|
|
147
|
+
function reverse_endianness(bytes) {
|
|
148
|
+
for (let i = 0; i < bytes.length; i += 4) {
|
|
149
|
+
const a = bytes[i + 0];
|
|
150
|
+
const b = bytes[i + 1];
|
|
151
|
+
const c = bytes[i + 2];
|
|
152
|
+
const d = bytes[i + 3];
|
|
153
|
+
|
|
154
|
+
bytes[i + 0] = d;
|
|
155
|
+
bytes[i + 1] = c;
|
|
156
|
+
bytes[i + 2] = b;
|
|
157
|
+
bytes[i + 3] = a;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** @param {string} str */
|
|
162
|
+
function encode(str) {
|
|
163
|
+
const encoded = encoder.encode(str);
|
|
164
|
+
const length = encoded.length * 8;
|
|
165
|
+
|
|
166
|
+
// result should be a multiple of 512 bits in length,
|
|
167
|
+
// with room for a 1 (after the data) and two 32-bit
|
|
168
|
+
// words containing the original input bit length
|
|
169
|
+
const size = 512 * Math.ceil((length + 65) / 512);
|
|
170
|
+
const bytes = new Uint8Array(size / 8);
|
|
171
|
+
bytes.set(encoded);
|
|
172
|
+
|
|
173
|
+
// append a 1
|
|
174
|
+
bytes[encoded.length] = 0b10000000;
|
|
175
|
+
|
|
176
|
+
reverse_endianness(bytes);
|
|
177
|
+
|
|
178
|
+
// add the input bit length
|
|
179
|
+
const words = new Uint32Array(bytes.buffer);
|
|
180
|
+
words[words.length - 2] = Math.floor(length / 0x100000000); // this will always be zero for us
|
|
181
|
+
words[words.length - 1] = length;
|
|
182
|
+
|
|
183
|
+
return words;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
Based on https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
|
|
188
|
+
|
|
189
|
+
MIT License
|
|
190
|
+
Copyright (c) 2020 Egor Nepomnyaschih
|
|
191
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
192
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
193
|
+
in the Software without restriction, including without limitation the rights
|
|
194
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
195
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
196
|
+
furnished to do so, subject to the following conditions:
|
|
197
|
+
The above copyright notice and this permission notice shall be included in all
|
|
198
|
+
copies or substantial portions of the Software.
|
|
199
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
200
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
201
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
202
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
203
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
204
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
205
|
+
SOFTWARE.
|
|
206
|
+
*/
|
|
207
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
|
|
208
|
+
|
|
209
|
+
/** @param {Uint8Array} bytes */
|
|
210
|
+
export function base64(bytes) {
|
|
211
|
+
const l = bytes.length;
|
|
212
|
+
|
|
213
|
+
let result = '';
|
|
214
|
+
let i;
|
|
215
|
+
|
|
216
|
+
for (i = 2; i < l; i += 3) {
|
|
217
|
+
result += chars[bytes[i - 2] >> 2];
|
|
218
|
+
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
219
|
+
result += chars[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)];
|
|
220
|
+
result += chars[bytes[i] & 0x3f];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (i === l + 1) {
|
|
224
|
+
// 1 octet yet to write
|
|
225
|
+
result += chars[bytes[i - 2] >> 2];
|
|
226
|
+
result += chars[(bytes[i - 2] & 0x03) << 4];
|
|
227
|
+
result += '==';
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (i === l) {
|
|
231
|
+
// 2 octets yet to write
|
|
232
|
+
result += chars[bytes[i - 2] >> 2];
|
|
233
|
+
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
234
|
+
result += chars[(bytes[i - 1] & 0x0f) << 2];
|
|
235
|
+
result += '=';
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return result;
|
|
239
|
+
}
|