@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,60 @@
|
|
|
1
|
+
import { hash } from '../hash.js';
|
|
2
|
+
|
|
3
|
+
let loading = 0;
|
|
4
|
+
|
|
5
|
+
export const native_fetch = window.fetch;
|
|
6
|
+
|
|
7
|
+
export function lock_fetch() {
|
|
8
|
+
loading += 1;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function unlock_fetch() {
|
|
12
|
+
loading -= 1;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (import.meta.env.DEV) {
|
|
16
|
+
let can_inspect_stack_trace = false;
|
|
17
|
+
|
|
18
|
+
const check_stack_trace = async () => {
|
|
19
|
+
const stack = /** @type {string} */ (new Error().stack);
|
|
20
|
+
can_inspect_stack_trace = stack.includes('check_stack_trace');
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
check_stack_trace();
|
|
24
|
+
|
|
25
|
+
window.fetch = (input, init) => {
|
|
26
|
+
const url = input instanceof Request ? input.url : input.toString();
|
|
27
|
+
const stack = /** @type {string} */ (new Error().stack);
|
|
28
|
+
|
|
29
|
+
const heuristic = can_inspect_stack_trace ? stack.includes('load_node') : loading;
|
|
30
|
+
if (heuristic) {
|
|
31
|
+
console.warn(
|
|
32
|
+
`Loading ${url} using \`window.fetch\`. For best results, use the \`fetch\` that is passed to your \`load\` function: https://kit.svelte.dev/docs/load#input-fetch`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return native_fetch(input, init);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {RequestInfo} resource
|
|
42
|
+
* @param {RequestInit} [opts]
|
|
43
|
+
*/
|
|
44
|
+
export function initial_fetch(resource, opts) {
|
|
45
|
+
const url = JSON.stringify(typeof resource === 'string' ? resource : resource.url);
|
|
46
|
+
|
|
47
|
+
let selector = `script[sveltekit\\:data-type="data"][sveltekit\\:data-url=${url}]`;
|
|
48
|
+
|
|
49
|
+
if (opts && typeof opts.body === 'string') {
|
|
50
|
+
selector += `[sveltekit\\:data-body="${hash(opts.body)}"]`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const script = document.querySelector(selector);
|
|
54
|
+
if (script && script.textContent) {
|
|
55
|
+
const { body, ...init } = JSON.parse(script.textContent);
|
|
56
|
+
return Promise.resolve(new Response(body, init));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return native_fetch(resource, opts);
|
|
60
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { exec, parse_route_id } from '../../utils/routing.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {import('types').CSRPageNodeLoader[]} nodes
|
|
5
|
+
* @param {Record<string, [number[], number[], number, 1?]>} dictionary
|
|
6
|
+
* @param {Record<string, (param: string) => boolean>} matchers
|
|
7
|
+
* @returns {import('types').CSRRoute[]}
|
|
8
|
+
*/
|
|
9
|
+
export function parse(nodes, dictionary, matchers) {
|
|
10
|
+
return Object.entries(dictionary).map(([id, [errors, layouts, leaf, uses_server_data]]) => {
|
|
11
|
+
const { pattern, names, types } = parse_route_id(id);
|
|
12
|
+
|
|
13
|
+
const route = {
|
|
14
|
+
id,
|
|
15
|
+
/** @param {string} path */
|
|
16
|
+
exec: (path) => {
|
|
17
|
+
const match = pattern.exec(path);
|
|
18
|
+
if (match) return exec(match, names, types, matchers);
|
|
19
|
+
},
|
|
20
|
+
errors: errors.map((n) => nodes[n]),
|
|
21
|
+
layouts: layouts.map((n) => nodes[n]),
|
|
22
|
+
leaf: nodes[leaf],
|
|
23
|
+
uses_server_data: !!uses_server_data
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// bit of a hack, but ensures that layout/error node lists are the same
|
|
27
|
+
// length, without which the wrong data will be applied if the route
|
|
28
|
+
// manifest looks like `[[a, b], [c,], d]`
|
|
29
|
+
route.errors.length = route.layouts.length = Math.max(
|
|
30
|
+
route.errors.length,
|
|
31
|
+
route.layouts.length
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return route;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { writable } from 'svelte/store';
|
|
2
|
+
import { create_updated_store, notifiable_store } from './utils.js';
|
|
3
|
+
|
|
4
|
+
/** @type {import('./types').Client} */
|
|
5
|
+
export let client;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {{
|
|
9
|
+
* client: import('./types').Client;
|
|
10
|
+
* }} opts
|
|
11
|
+
*/
|
|
12
|
+
export function init(opts) {
|
|
13
|
+
client = opts.client;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const stores = {
|
|
17
|
+
url: notifiable_store({}),
|
|
18
|
+
page: notifiable_store({}),
|
|
19
|
+
navigating: writable(/** @type {import('types').Navigation | null} */ (null)),
|
|
20
|
+
updated: create_updated_store()
|
|
21
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { create_client } from './client.js';
|
|
2
|
+
import { init } from './singletons.js';
|
|
3
|
+
import { set_paths } from '../paths.js';
|
|
4
|
+
|
|
5
|
+
export { set_public_env } from '../env-public.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {{
|
|
9
|
+
* paths: {
|
|
10
|
+
* assets: string;
|
|
11
|
+
* base: string;
|
|
12
|
+
* },
|
|
13
|
+
* target: Element;
|
|
14
|
+
* route: boolean;
|
|
15
|
+
* spa: boolean;
|
|
16
|
+
* trailing_slash: import('types').TrailingSlash;
|
|
17
|
+
* hydrate: {
|
|
18
|
+
* status: number;
|
|
19
|
+
* error: Error | (import('../server/page/types').SerializedHttpError);
|
|
20
|
+
* node_ids: number[];
|
|
21
|
+
* params: Record<string, string>;
|
|
22
|
+
* routeId: string | null;
|
|
23
|
+
* };
|
|
24
|
+
* }} opts
|
|
25
|
+
*/
|
|
26
|
+
export async function start({ paths, target, route, spa, trailing_slash, hydrate }) {
|
|
27
|
+
const client = create_client({
|
|
28
|
+
target,
|
|
29
|
+
base: paths.base,
|
|
30
|
+
trailing_slash
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
init({ client });
|
|
34
|
+
set_paths(paths);
|
|
35
|
+
|
|
36
|
+
if (hydrate) {
|
|
37
|
+
await client._hydrate(hydrate);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (route) {
|
|
41
|
+
if (spa) client.goto(location.href, { replaceState: true });
|
|
42
|
+
client._start_router();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
dispatchEvent(new CustomEvent('sveltekit:start'));
|
|
46
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
afterNavigate,
|
|
3
|
+
beforeNavigate,
|
|
4
|
+
goto,
|
|
5
|
+
invalidate,
|
|
6
|
+
prefetch,
|
|
7
|
+
prefetchRoutes
|
|
8
|
+
} from '$app/navigation';
|
|
9
|
+
import { CSRPageNode, CSRRoute } from 'types';
|
|
10
|
+
import { HttpError } from '../../index/private.js';
|
|
11
|
+
import { SerializedHttpError } from '../server/page/types.js';
|
|
12
|
+
|
|
13
|
+
export interface Client {
|
|
14
|
+
// public API, exposed via $app/navigation
|
|
15
|
+
after_navigate: typeof afterNavigate;
|
|
16
|
+
before_navigate: typeof beforeNavigate;
|
|
17
|
+
disable_scroll_handling: () => void;
|
|
18
|
+
goto: typeof goto;
|
|
19
|
+
invalidate: typeof invalidate;
|
|
20
|
+
prefetch: typeof prefetch;
|
|
21
|
+
prefetch_routes: typeof prefetchRoutes;
|
|
22
|
+
|
|
23
|
+
// private API
|
|
24
|
+
_hydrate: (opts: {
|
|
25
|
+
status: number;
|
|
26
|
+
error: Error | SerializedHttpError;
|
|
27
|
+
node_ids: number[];
|
|
28
|
+
params: Record<string, string>;
|
|
29
|
+
routeId: string | null;
|
|
30
|
+
}) => Promise<void>;
|
|
31
|
+
_start_router: () => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type NavigationIntent = {
|
|
35
|
+
/**
|
|
36
|
+
* `url.pathname + url.search`
|
|
37
|
+
*/
|
|
38
|
+
id: string;
|
|
39
|
+
/**
|
|
40
|
+
* The route parameters
|
|
41
|
+
*/
|
|
42
|
+
params: Record<string, string>;
|
|
43
|
+
/**
|
|
44
|
+
* The route that matches `path`
|
|
45
|
+
*/
|
|
46
|
+
route: CSRRoute;
|
|
47
|
+
/**
|
|
48
|
+
* The destination URL
|
|
49
|
+
*/
|
|
50
|
+
url: URL;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type NavigationResult = NavigationRedirect | NavigationFinished;
|
|
54
|
+
|
|
55
|
+
export type NavigationRedirect = {
|
|
56
|
+
type: 'redirect';
|
|
57
|
+
location: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type NavigationFinished = {
|
|
61
|
+
type: 'loaded';
|
|
62
|
+
state: NavigationState;
|
|
63
|
+
props: Record<string, any>;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type BranchNode = {
|
|
67
|
+
node: CSRPageNode;
|
|
68
|
+
data: Record<string, any> | null;
|
|
69
|
+
uses: {
|
|
70
|
+
params: Set<string>;
|
|
71
|
+
url: boolean; // TODO make more granular?
|
|
72
|
+
dependencies: Set<string>;
|
|
73
|
+
parent: boolean;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type NavigationState = {
|
|
78
|
+
branch: Array<BranchNode | undefined>;
|
|
79
|
+
error: HttpError | Error | null;
|
|
80
|
+
params: Record<string, string>;
|
|
81
|
+
session_id: number;
|
|
82
|
+
url: URL;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export type ServerDataPayload = ServerDataRedirected | ServerDataLoaded;
|
|
86
|
+
|
|
87
|
+
export interface ServerDataRedirected {
|
|
88
|
+
type: 'redirect';
|
|
89
|
+
location: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface ServerDataLoaded {
|
|
93
|
+
type: 'data';
|
|
94
|
+
nodes: Array<{
|
|
95
|
+
data?: Record<string, any> | null; // TODO or `-1` to indicate 'reuse cached data'?
|
|
96
|
+
status?: number;
|
|
97
|
+
message?: string;
|
|
98
|
+
error?: {
|
|
99
|
+
name: string;
|
|
100
|
+
message: string;
|
|
101
|
+
stack: string;
|
|
102
|
+
[key: string]: any;
|
|
103
|
+
};
|
|
104
|
+
}>;
|
|
105
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { writable } from 'svelte/store';
|
|
2
|
+
import { assets } from '../paths.js';
|
|
3
|
+
|
|
4
|
+
/* global __SVELTEKIT_APP_VERSION__, __SVELTEKIT_APP_VERSION_FILE__, __SVELTEKIT_APP_VERSION_POLL_INTERVAL__ */
|
|
5
|
+
|
|
6
|
+
/** @param {HTMLDocument} doc */
|
|
7
|
+
export function get_base_uri(doc) {
|
|
8
|
+
let baseURI = doc.baseURI;
|
|
9
|
+
|
|
10
|
+
if (!baseURI) {
|
|
11
|
+
const baseTags = doc.getElementsByTagName('base');
|
|
12
|
+
baseURI = baseTags.length ? baseTags[0].href : doc.URL;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return baseURI;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function scroll_state() {
|
|
19
|
+
return {
|
|
20
|
+
x: pageXOffset,
|
|
21
|
+
y: pageYOffset
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @param {Event} event */
|
|
26
|
+
export function find_anchor(event) {
|
|
27
|
+
const node = event
|
|
28
|
+
.composedPath()
|
|
29
|
+
.find((e) => e instanceof Node && e.nodeName.toUpperCase() === 'A'); // SVG <a> elements have a lowercase name
|
|
30
|
+
return /** @type {HTMLAnchorElement | SVGAElement | undefined} */ (node);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @param {HTMLAnchorElement | SVGAElement} node */
|
|
34
|
+
export function get_href(node) {
|
|
35
|
+
return node instanceof SVGAElement
|
|
36
|
+
? new URL(node.href.baseVal, document.baseURI)
|
|
37
|
+
: new URL(node.href);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @param {any} value */
|
|
41
|
+
export function notifiable_store(value) {
|
|
42
|
+
const store = writable(value);
|
|
43
|
+
let ready = true;
|
|
44
|
+
|
|
45
|
+
function notify() {
|
|
46
|
+
ready = true;
|
|
47
|
+
store.update((val) => val);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @param {any} new_value */
|
|
51
|
+
function set(new_value) {
|
|
52
|
+
ready = false;
|
|
53
|
+
store.set(new_value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @param {(value: any) => void} run */
|
|
57
|
+
function subscribe(run) {
|
|
58
|
+
/** @type {any} */
|
|
59
|
+
let old_value;
|
|
60
|
+
return store.subscribe((new_value) => {
|
|
61
|
+
if (old_value === undefined || (ready && new_value !== old_value)) {
|
|
62
|
+
run((old_value = new_value));
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { notify, set, subscribe };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function create_updated_store() {
|
|
71
|
+
const { set, subscribe } = writable(false);
|
|
72
|
+
|
|
73
|
+
const interval = __SVELTEKIT_APP_VERSION_POLL_INTERVAL__;
|
|
74
|
+
|
|
75
|
+
/** @type {NodeJS.Timeout} */
|
|
76
|
+
let timeout;
|
|
77
|
+
|
|
78
|
+
async function check() {
|
|
79
|
+
if (import.meta.env.DEV || import.meta.env.SSR) return false;
|
|
80
|
+
|
|
81
|
+
clearTimeout(timeout);
|
|
82
|
+
|
|
83
|
+
if (interval) timeout = setTimeout(check, interval);
|
|
84
|
+
|
|
85
|
+
const res = await fetch(`${assets}/${__SVELTEKIT_APP_VERSION_FILE__}`, {
|
|
86
|
+
headers: {
|
|
87
|
+
pragma: 'no-cache',
|
|
88
|
+
'cache-control': 'no-cache'
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (res.ok) {
|
|
93
|
+
const { version } = await res.json();
|
|
94
|
+
const updated = version !== __SVELTEKIT_APP_VERSION__;
|
|
95
|
+
|
|
96
|
+
if (updated) {
|
|
97
|
+
set(true);
|
|
98
|
+
clearTimeout(timeout);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return updated;
|
|
102
|
+
} else {
|
|
103
|
+
throw new Error(`Version check failed: ${res.status}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (interval) timeout = setTimeout(check, interval);
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
subscribe,
|
|
111
|
+
check
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { page } from '$app/stores';
|
|
3
|
+
</script>
|
|
4
|
+
|
|
5
|
+
<h1>{$page.status}</h1>
|
|
6
|
+
|
|
7
|
+
<pre>{$page.error.message}</pre>
|
|
8
|
+
|
|
9
|
+
<!-- TODO figure out what to do with frames/stacktraces in prod -->
|
|
10
|
+
<!-- frame is populated by Svelte in its CompileError and is a Rollup/Vite convention -->
|
|
11
|
+
{#if $page.error.frame}
|
|
12
|
+
<pre>{$page.error.frame}</pre>
|
|
13
|
+
{/if}
|
|
14
|
+
{#if $page.error.stack}
|
|
15
|
+
<pre>{$page.error.stack}</pre>
|
|
16
|
+
{/if}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { env } from '../../env-private.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { env } from '../../env-public.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hash using djb2
|
|
3
|
+
* @param {import('types').StrictBody} value
|
|
4
|
+
*/
|
|
5
|
+
export function hash(value) {
|
|
6
|
+
let hash = 5381;
|
|
7
|
+
let i = value.length;
|
|
8
|
+
|
|
9
|
+
if (typeof value === 'string') {
|
|
10
|
+
while (i) hash = (hash * 33) ^ value.charCodeAt(--i);
|
|
11
|
+
} else {
|
|
12
|
+
while (i) hash = (hash * 33) ^ value[--i];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return (hash >>> 0).toString(36);
|
|
16
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { HttpError, Redirect } from '../../index/private.js';
|
|
2
|
+
import { check_method_names, method_not_allowed } from './utils.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {import('types').RequestEvent} event
|
|
6
|
+
* @param {import('types').SSREndpoint} route
|
|
7
|
+
* @returns {Promise<Response>}
|
|
8
|
+
*/
|
|
9
|
+
export async function render_endpoint(event, route) {
|
|
10
|
+
const method = /** @type {import('types').HttpMethod} */ (event.request.method);
|
|
11
|
+
|
|
12
|
+
const mod = await route.load();
|
|
13
|
+
|
|
14
|
+
// TODO: Remove for 1.0
|
|
15
|
+
check_method_names(mod);
|
|
16
|
+
|
|
17
|
+
let handler = mod[method];
|
|
18
|
+
|
|
19
|
+
if (!handler && method === 'HEAD') {
|
|
20
|
+
handler = mod.GET;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!handler) {
|
|
24
|
+
if (event.request.headers.get('x-sveltekit-load')) {
|
|
25
|
+
// TODO would be nice to avoid these requests altogether,
|
|
26
|
+
// by noting whether or not page endpoints export `get`
|
|
27
|
+
return new Response(undefined, { status: 204 });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return method_not_allowed(mod, method);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const response = await handler(
|
|
35
|
+
/** @type {import('types').RequestEvent<Record<string, any>>} */ (event)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
if (!(response instanceof Response)) {
|
|
39
|
+
return new Response(
|
|
40
|
+
`Invalid response from route ${event.url.pathname}: handler should return a Response object`,
|
|
41
|
+
{ status: 500 }
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return response;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error instanceof HttpError) {
|
|
48
|
+
return new Response(error.message, { status: error.status });
|
|
49
|
+
} else if (error instanceof Redirect) {
|
|
50
|
+
return new Response(undefined, {
|
|
51
|
+
status: error.status,
|
|
52
|
+
headers: { Location: error.location }
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|