@sveltejs/kit 1.0.0-next.5 → 1.0.0-next.500

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.
Files changed (138) hide show
  1. package/README.md +12 -9
  2. package/package.json +94 -52
  3. package/postinstall.js +38 -0
  4. package/scripts/special-types/$env+dynamic+private.md +10 -0
  5. package/scripts/special-types/$env+dynamic+public.md +8 -0
  6. package/scripts/special-types/$env+static+private.md +19 -0
  7. package/scripts/special-types/$env+static+public.md +7 -0
  8. package/scripts/special-types/$lib.md +5 -0
  9. package/src/cli.js +108 -0
  10. package/src/constants.js +7 -0
  11. package/src/core/adapt/builder.js +206 -0
  12. package/src/core/adapt/index.js +31 -0
  13. package/src/core/config/default-error.html +56 -0
  14. package/src/core/config/index.js +110 -0
  15. package/src/core/config/options.js +504 -0
  16. package/src/core/config/types.d.ts +1 -0
  17. package/src/core/env.js +121 -0
  18. package/src/core/generate_manifest/index.js +93 -0
  19. package/src/core/prerender/crawl.js +198 -0
  20. package/src/core/prerender/entities.js +2252 -0
  21. package/src/core/prerender/prerender.js +431 -0
  22. package/src/core/prerender/queue.js +80 -0
  23. package/src/core/sync/create_manifest_data/index.js +488 -0
  24. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  25. package/src/core/sync/sync.js +70 -0
  26. package/src/core/sync/utils.js +33 -0
  27. package/src/core/sync/write_ambient.js +53 -0
  28. package/src/core/sync/write_client_manifest.js +106 -0
  29. package/src/core/sync/write_matchers.js +25 -0
  30. package/src/core/sync/write_root.js +91 -0
  31. package/src/core/sync/write_tsconfig.js +195 -0
  32. package/src/core/sync/write_types/index.js +678 -0
  33. package/src/core/utils.js +70 -0
  34. package/src/exports/hooks/index.js +1 -0
  35. package/src/exports/hooks/sequence.js +44 -0
  36. package/src/exports/index.js +45 -0
  37. package/src/exports/node/index.js +173 -0
  38. package/src/exports/node/polyfills.js +41 -0
  39. package/src/exports/vite/build/build_server.js +378 -0
  40. package/src/exports/vite/build/build_service_worker.js +90 -0
  41. package/src/exports/vite/build/utils.js +180 -0
  42. package/src/exports/vite/dev/index.js +577 -0
  43. package/src/exports/vite/graph_analysis/index.js +277 -0
  44. package/src/exports/vite/graph_analysis/types.d.ts +5 -0
  45. package/src/exports/vite/graph_analysis/utils.js +30 -0
  46. package/src/exports/vite/index.js +598 -0
  47. package/src/exports/vite/preview/index.js +189 -0
  48. package/src/exports/vite/types.d.ts +3 -0
  49. package/src/exports/vite/utils.js +157 -0
  50. package/src/runtime/app/env.js +1 -0
  51. package/src/runtime/app/environment.js +11 -0
  52. package/src/runtime/app/forms.js +114 -0
  53. package/src/runtime/app/navigation.js +23 -0
  54. package/src/runtime/app/paths.js +1 -0
  55. package/src/runtime/app/stores.js +102 -0
  56. package/src/runtime/client/ambient.d.ts +26 -0
  57. package/src/runtime/client/client.js +1583 -0
  58. package/src/runtime/client/fetcher.js +107 -0
  59. package/src/runtime/client/parse.js +60 -0
  60. package/src/runtime/client/singletons.js +21 -0
  61. package/src/runtime/client/start.js +37 -0
  62. package/src/runtime/client/types.d.ts +84 -0
  63. package/src/runtime/client/utils.js +159 -0
  64. package/src/runtime/components/error.svelte +16 -0
  65. package/{assets → src/runtime}/components/layout.svelte +0 -0
  66. package/src/runtime/control.js +98 -0
  67. package/src/runtime/env/dynamic/private.js +1 -0
  68. package/src/runtime/env/dynamic/public.js +1 -0
  69. package/src/runtime/env-private.js +6 -0
  70. package/src/runtime/env-public.js +6 -0
  71. package/src/runtime/env.js +6 -0
  72. package/src/runtime/hash.js +16 -0
  73. package/src/runtime/paths.js +11 -0
  74. package/src/runtime/server/cookie.js +127 -0
  75. package/src/runtime/server/data/index.js +136 -0
  76. package/src/runtime/server/endpoint.js +90 -0
  77. package/src/runtime/server/index.js +340 -0
  78. package/src/runtime/server/page/actions.js +243 -0
  79. package/src/runtime/server/page/crypto.js +239 -0
  80. package/src/runtime/server/page/csp.js +250 -0
  81. package/src/runtime/server/page/fetch.js +301 -0
  82. package/src/runtime/server/page/index.js +304 -0
  83. package/src/runtime/server/page/load_data.js +124 -0
  84. package/src/runtime/server/page/render.js +342 -0
  85. package/src/runtime/server/page/respond_with_error.js +104 -0
  86. package/src/runtime/server/page/serialize_data.js +87 -0
  87. package/src/runtime/server/page/types.d.ts +41 -0
  88. package/src/runtime/server/utils.js +179 -0
  89. package/src/utils/array.js +9 -0
  90. package/src/utils/error.js +22 -0
  91. package/src/utils/escape.js +46 -0
  92. package/src/utils/filesystem.js +137 -0
  93. package/src/utils/functions.js +16 -0
  94. package/src/utils/http.js +55 -0
  95. package/src/utils/misc.js +1 -0
  96. package/src/utils/routing.js +117 -0
  97. package/src/utils/unit_test.js +11 -0
  98. package/src/utils/url.js +142 -0
  99. package/svelte-kit.js +2 -0
  100. package/types/ambient.d.ts +426 -0
  101. package/types/index.d.ts +444 -0
  102. package/types/internal.d.ts +378 -0
  103. package/types/private.d.ts +224 -0
  104. package/CHANGELOG.md +0 -177
  105. package/assets/components/error.svelte +0 -13
  106. package/assets/runtime/app/navigation.js +0 -47
  107. package/assets/runtime/app/navigation.js.map +0 -1
  108. package/assets/runtime/app/stores.js +0 -78
  109. package/assets/runtime/app/stores.js.map +0 -1
  110. package/assets/runtime/internal/singletons.js +0 -10
  111. package/assets/runtime/internal/singletons.js.map +0 -1
  112. package/assets/runtime/internal/start.js +0 -517
  113. package/assets/runtime/internal/start.js.map +0 -1
  114. package/dist/api.js +0 -40
  115. package/dist/api.js.map +0 -1
  116. package/dist/cli.js +0 -128
  117. package/dist/cli.js.map +0 -1
  118. package/dist/create_app.js +0 -550
  119. package/dist/create_app.js.map +0 -1
  120. package/dist/index.js +0 -8331
  121. package/dist/index.js.map +0 -1
  122. package/dist/index2.js +0 -509
  123. package/dist/index2.js.map +0 -1
  124. package/dist/index3.js +0 -63
  125. package/dist/index3.js.map +0 -1
  126. package/dist/index4.js +0 -466
  127. package/dist/index4.js.map +0 -1
  128. package/dist/index5.js +0 -276
  129. package/dist/index5.js.map +0 -1
  130. package/dist/package.js +0 -235
  131. package/dist/package.js.map +0 -1
  132. package/dist/renderer.js +0 -2398
  133. package/dist/renderer.js.map +0 -1
  134. package/dist/standard.js +0 -101
  135. package/dist/standard.js.map +0 -1
  136. package/dist/utils.js +0 -58
  137. package/dist/utils.js.map +0 -1
  138. package/svelte-kit +0 -3
@@ -0,0 +1,107 @@
1
+ import { hash } from '../hash.js';
2
+
3
+ let loading = 0;
4
+
5
+ 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
+ const method = input instanceof Request ? input.method : init?.method || 'GET';
37
+
38
+ if (method !== 'GET') {
39
+ const url = new URL(input instanceof Request ? input.url : input.toString(), document.baseURI)
40
+ .href;
41
+ cache.delete(url);
42
+ }
43
+
44
+ return native_fetch(input, init);
45
+ };
46
+ } else {
47
+ window.fetch = (input, init) => {
48
+ const method = input instanceof Request ? input.method : init?.method || 'GET';
49
+
50
+ if (method !== 'GET') {
51
+ const url = new URL(input instanceof Request ? input.url : input.toString(), document.baseURI)
52
+ .href;
53
+ cache.delete(url);
54
+ }
55
+
56
+ return native_fetch(input, init);
57
+ };
58
+ }
59
+
60
+ const cache = new Map();
61
+
62
+ /**
63
+ * Should be called on the initial run of load functions that hydrate the page.
64
+ * Saves any requests with cache-control max-age to the cache.
65
+ * @param {RequestInfo | URL} resource
66
+ * @param {string} resolved
67
+ * @param {RequestInit} [opts]
68
+ */
69
+ export function initial_fetch(resource, resolved, opts) {
70
+ const url = JSON.stringify(resource instanceof Request ? resource.url : resource);
71
+
72
+ let selector = `script[data-sveltekit-fetched][data-url=${url}]`;
73
+
74
+ if (opts && typeof opts.body === 'string') {
75
+ selector += `[data-hash="${hash(opts.body)}"]`;
76
+ }
77
+
78
+ const script = document.querySelector(selector);
79
+ if (script?.textContent) {
80
+ const { body, ...init } = JSON.parse(script.textContent);
81
+
82
+ const ttl = script.getAttribute('data-ttl');
83
+ if (ttl) cache.set(resolved, { body, init, ttl: 1000 * Number(ttl) });
84
+
85
+ return Promise.resolve(new Response(body, init));
86
+ }
87
+
88
+ return native_fetch(resource, opts);
89
+ }
90
+
91
+ /**
92
+ * Tries to get the response from the cache, if max-age allows it, else does a fetch.
93
+ * @param {string} resolved
94
+ * @param {RequestInit} [opts]
95
+ */
96
+ export function subsequent_fetch(resolved, opts) {
97
+ const cached = cache.get(resolved);
98
+ if (cached) {
99
+ if (performance.now() < cached.ttl) {
100
+ return new Response(cached.body, cached.init);
101
+ }
102
+
103
+ cache.delete(resolved);
104
+ }
105
+
106
+ return native_fetch(resolved, opts);
107
+ }
@@ -0,0 +1,60 @@
1
+ import { exec, parse_route_id } from '../../utils/routing.js';
2
+
3
+ /**
4
+ * @param {import('types').CSRPageNodeLoader[]} nodes
5
+ * @param {number[]} server_loads
6
+ * @param {typeof import('__GENERATED__/client-manifest.js').dictionary} dictionary
7
+ * @param {Record<string, (param: string) => boolean>} matchers
8
+ * @returns {import('types').CSRRoute[]}
9
+ */
10
+ export function parse(nodes, server_loads, dictionary, matchers) {
11
+ const layouts_with_server_load = new Set(server_loads);
12
+
13
+ return Object.entries(dictionary).map(([id, [leaf, layouts, errors]]) => {
14
+ const { pattern, names, types } = parse_route_id(id);
15
+
16
+ const route = {
17
+ id,
18
+ /** @param {string} path */
19
+ exec: (path) => {
20
+ const match = pattern.exec(path);
21
+ if (match) return exec(match, names, types, matchers);
22
+ },
23
+ errors: [1, ...(errors || [])].map((n) => nodes[n]),
24
+ layouts: [0, ...(layouts || [])].map(create_layout_loader),
25
+ leaf: create_leaf_loader(leaf)
26
+ };
27
+
28
+ // bit of a hack, but ensures that layout/error node lists are the same
29
+ // length, without which the wrong data will be applied if the route
30
+ // manifest looks like `[[a, b], [c,], d]`
31
+ route.errors.length = route.layouts.length = Math.max(
32
+ route.errors.length,
33
+ route.layouts.length
34
+ );
35
+
36
+ return route;
37
+ });
38
+
39
+ /**
40
+ * @param {number} id
41
+ * @returns {[boolean, import('types').CSRPageNodeLoader]}
42
+ */
43
+ function create_leaf_loader(id) {
44
+ // whether or not the route uses the server data is
45
+ // encoded using the ones' complement, to save space
46
+ const uses_server_data = id < 0;
47
+ if (uses_server_data) id = ~id;
48
+ return [uses_server_data, nodes[id]];
49
+ }
50
+
51
+ /**
52
+ * @param {number | undefined} id
53
+ * @returns {[boolean, import('types').CSRPageNodeLoader] | undefined}
54
+ */
55
+ function create_layout_loader(id) {
56
+ // whether or not the layout uses the server data is
57
+ // encoded in the layouts array, to save space
58
+ return id === undefined ? id : [layouts_with_server_load.has(id), nodes[id]];
59
+ }
60
+ }
@@ -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,37 @@
1
+ import { create_client } from './client.js';
2
+ import { init } from './singletons.js';
3
+ import { set_paths } from '../paths.js';
4
+ import { set_public_env } from '../env-public.js';
5
+
6
+ /**
7
+ * @param {{
8
+ * env: Record<string, string>;
9
+ * hydrate: Parameters<import('./types').Client['_hydrate']>[0];
10
+ * paths: {
11
+ * assets: string;
12
+ * base: string;
13
+ * },
14
+ * target: Element;
15
+ * trailing_slash: import('types').TrailingSlash;
16
+ * }} opts
17
+ */
18
+ export async function start({ env, hydrate, paths, target, trailing_slash }) {
19
+ set_public_env(env);
20
+ set_paths(paths);
21
+
22
+ const client = create_client({
23
+ target,
24
+ base: paths.base,
25
+ trailing_slash
26
+ });
27
+
28
+ init({ client });
29
+
30
+ if (hydrate) {
31
+ await client._hydrate(hydrate);
32
+ } else {
33
+ client.goto(location.href, { replaceState: true });
34
+ }
35
+
36
+ client._start_router();
37
+ }
@@ -0,0 +1,84 @@
1
+ import { applyAction } from '$app/forms';
2
+ import {
3
+ afterNavigate,
4
+ beforeNavigate,
5
+ goto,
6
+ invalidate,
7
+ invalidateAll,
8
+ prefetch,
9
+ prefetchRoutes
10
+ } from '$app/navigation';
11
+ import { CSRPageNode, CSRPageNodeLoader, CSRRoute, Uses } from 'types';
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
+ invalidateAll: typeof invalidateAll;
21
+ prefetch: typeof prefetch;
22
+ prefetch_routes: typeof prefetchRoutes;
23
+ apply_action: typeof applyAction;
24
+
25
+ // private API
26
+ _hydrate: (opts: {
27
+ status: number;
28
+ error: App.Error;
29
+ node_ids: number[];
30
+ params: Record<string, string>;
31
+ routeId: string | null;
32
+ data: Array<import('types').ServerDataNode | null>;
33
+ form: Record<string, any> | null;
34
+ }) => Promise<void>;
35
+ _start_router: () => void;
36
+ }
37
+
38
+ export type NavigationIntent = {
39
+ /** `url.pathname + url.search` */
40
+ id: string;
41
+ /** Whether we are invalidating or navigating */
42
+ invalidating: boolean;
43
+ /** The route parameters */
44
+ params: Record<string, string>;
45
+ /** The route that matches `path` */
46
+ route: CSRRoute;
47
+ /** The destination URL */
48
+ url: URL;
49
+ };
50
+
51
+ export type NavigationResult = NavigationRedirect | NavigationFinished;
52
+
53
+ export type NavigationRedirect = {
54
+ type: 'redirect';
55
+ location: string;
56
+ };
57
+
58
+ export type NavigationFinished = {
59
+ type: 'loaded';
60
+ state: NavigationState;
61
+ props: Record<string, any>;
62
+ };
63
+
64
+ export type BranchNode = {
65
+ node: CSRPageNode;
66
+ loader: CSRPageNodeLoader;
67
+ server: DataNode | null;
68
+ shared: DataNode | null;
69
+ data: Record<string, any> | null;
70
+ };
71
+
72
+ export interface DataNode {
73
+ type: 'data';
74
+ data: Record<string, any> | null;
75
+ uses: Uses;
76
+ }
77
+
78
+ export interface NavigationState {
79
+ branch: Array<BranchNode | undefined>;
80
+ error: App.Error | null;
81
+ params: Record<string, string>;
82
+ route: CSRRoute | null;
83
+ url: URL;
84
+ }
@@ -0,0 +1,159 @@
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
+ /** @type {HTMLAnchorElement | SVGAElement | undefined} */
28
+ let a;
29
+
30
+ /** @type {boolean | null} */
31
+ let noscroll = null;
32
+
33
+ /** @type {boolean | null} */
34
+ let prefetch = null;
35
+
36
+ /** @type {boolean | null} */
37
+ let reload = null;
38
+
39
+ for (const element of event.composedPath()) {
40
+ if (!(element instanceof Element)) continue;
41
+
42
+ if (!a && element.nodeName.toUpperCase() === 'A') {
43
+ // SVG <a> elements have a lowercase name
44
+ a = /** @type {HTMLAnchorElement | SVGAElement} */ (element);
45
+ }
46
+
47
+ if (noscroll === null) noscroll = get_link_option(element, 'data-sveltekit-noscroll');
48
+ if (prefetch === null) prefetch = get_link_option(element, 'data-sveltekit-prefetch');
49
+ if (reload === null) reload = get_link_option(element, 'data-sveltekit-reload');
50
+ }
51
+
52
+ const url = a && new URL(a instanceof SVGAElement ? a.href.baseVal : a.href, document.baseURI);
53
+
54
+ return {
55
+ a,
56
+ url,
57
+ options: {
58
+ noscroll,
59
+ prefetch,
60
+ reload
61
+ }
62
+ };
63
+ }
64
+
65
+ const warned = new WeakSet();
66
+
67
+ /**
68
+ * @param {Element} element
69
+ * @param {string} attribute
70
+ */
71
+ function get_link_option(element, attribute) {
72
+ const value = element.getAttribute(attribute);
73
+ if (value === null) return value;
74
+
75
+ if (value === '') return true;
76
+ if (value === 'off') return false;
77
+
78
+ if (__SVELTEKIT_DEV__ && !warned.has(element)) {
79
+ console.error(`Unexpected value for ${attribute} — should be "" or "off"`, element);
80
+ warned.add(element);
81
+ }
82
+
83
+ return false;
84
+ }
85
+
86
+ /** @param {any} value */
87
+ export function notifiable_store(value) {
88
+ const store = writable(value);
89
+ let ready = true;
90
+
91
+ function notify() {
92
+ ready = true;
93
+ store.update((val) => val);
94
+ }
95
+
96
+ /** @param {any} new_value */
97
+ function set(new_value) {
98
+ ready = false;
99
+ store.set(new_value);
100
+ }
101
+
102
+ /** @param {(value: any) => void} run */
103
+ function subscribe(run) {
104
+ /** @type {any} */
105
+ let old_value;
106
+ return store.subscribe((new_value) => {
107
+ if (old_value === undefined || (ready && new_value !== old_value)) {
108
+ run((old_value = new_value));
109
+ }
110
+ });
111
+ }
112
+
113
+ return { notify, set, subscribe };
114
+ }
115
+
116
+ export function create_updated_store() {
117
+ const { set, subscribe } = writable(false);
118
+
119
+ const interval = __SVELTEKIT_APP_VERSION_POLL_INTERVAL__;
120
+
121
+ /** @type {NodeJS.Timeout} */
122
+ let timeout;
123
+
124
+ async function check() {
125
+ if (import.meta.env.DEV || import.meta.env.SSR) return false;
126
+
127
+ clearTimeout(timeout);
128
+
129
+ if (interval) timeout = setTimeout(check, interval);
130
+
131
+ const res = await fetch(`${assets}/${__SVELTEKIT_APP_VERSION_FILE__}`, {
132
+ headers: {
133
+ pragma: 'no-cache',
134
+ 'cache-control': 'no-cache'
135
+ }
136
+ });
137
+
138
+ if (res.ok) {
139
+ const { version } = await res.json();
140
+ const updated = version !== __SVELTEKIT_APP_VERSION__;
141
+
142
+ if (updated) {
143
+ set(true);
144
+ clearTimeout(timeout);
145
+ }
146
+
147
+ return updated;
148
+ } else {
149
+ throw new Error(`Version check failed: ${res.status}`);
150
+ }
151
+ }
152
+
153
+ if (interval) timeout = setTimeout(check, interval);
154
+
155
+ return {
156
+ subscribe,
157
+ check
158
+ };
159
+ }
@@ -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,98 @@
1
+ export class HttpError {
2
+ /**
3
+ * @param {number} status
4
+ * @param {{message: string} extends App.Error ? (App.Error | string | undefined) : App.Error} body
5
+ */
6
+ constructor(status, body) {
7
+ this.status = status;
8
+ if (typeof body === 'string') {
9
+ this.body = { message: body };
10
+ } else if (body) {
11
+ this.body = body;
12
+ } else {
13
+ this.body = { message: `Error: ${status}` };
14
+ }
15
+ }
16
+
17
+ toString() {
18
+ return JSON.stringify(this.body);
19
+ }
20
+ }
21
+
22
+ export class Redirect {
23
+ /**
24
+ * @param {number} status
25
+ * @param {string} location
26
+ */
27
+ constructor(status, location) {
28
+ this.status = status;
29
+ this.location = location;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * @template {Record<string, unknown> | undefined} [T=undefined]
35
+ */
36
+ export class ValidationError {
37
+ /**
38
+ * @param {number} status
39
+ * @param {T} [data]
40
+ */
41
+ constructor(status, data) {
42
+ this.status = status;
43
+ this.data = data;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Creates an `HttpError` object with an HTTP status code and an optional message.
49
+ * This object, if thrown during request handling, will cause SvelteKit to
50
+ * return an error response without invoking `handleError`
51
+ * @param {number} status
52
+ * @param {string | undefined} [message]
53
+ */
54
+ export function error(status, message) {
55
+ return new HttpError(status, message);
56
+ }
57
+
58
+ /**
59
+ * Creates a `Redirect` object. If thrown during request handling, SvelteKit will
60
+ * return a redirect response.
61
+ * @param {number} status
62
+ * @param {string} location
63
+ */
64
+ export function redirect(status, location) {
65
+ if (isNaN(status) || status < 300 || status > 399) {
66
+ throw new Error('Invalid status code');
67
+ }
68
+
69
+ return new Redirect(status, location);
70
+ }
71
+
72
+ /**
73
+ * Generates a JSON `Response` object from the supplied data.
74
+ * @param {any} data
75
+ * @param {ResponseInit} [init]
76
+ */
77
+ export function json(data, init) {
78
+ // TODO deprecate this in favour of `Response.json` when it's
79
+ // more widely supported
80
+ const headers = new Headers(init?.headers);
81
+ if (!headers.has('content-type')) {
82
+ headers.set('content-type', 'application/json');
83
+ }
84
+
85
+ return new Response(JSON.stringify(data), {
86
+ ...init,
87
+ headers
88
+ });
89
+ }
90
+
91
+ /**
92
+ * Generates a `ValidationError` object.
93
+ * @param {number} status
94
+ * @param {Record<string, any> | undefined} [data]
95
+ */
96
+ export function invalid(status, data) {
97
+ return new ValidationError(status, data);
98
+ }
@@ -0,0 +1 @@
1
+ export { env } from '../../env-private.js';
@@ -0,0 +1 @@
1
+ export { env } from '../../env-public.js';
@@ -0,0 +1,6 @@
1
+ export let env = {};
2
+
3
+ /** @type {(environment: Record<string, string>) => void} */
4
+ export function set_private_env(environment) {
5
+ env = environment;
6
+ }
@@ -0,0 +1,6 @@
1
+ export let env = {};
2
+
3
+ /** @type {(environment: Record<string, string>) => void} */
4
+ export function set_public_env(environment) {
5
+ env = environment;
6
+ }
@@ -0,0 +1,6 @@
1
+ export let prerendering = false;
2
+
3
+ /** @param {boolean} value */
4
+ export function set_prerendering(value) {
5
+ prerendering = value;
6
+ }
@@ -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,11 @@
1
+ /** @type {string} */
2
+ export let base = '';
3
+
4
+ /** @type {string} */
5
+ export let assets = '';
6
+
7
+ /** @param {{ base: string, assets: string }} paths */
8
+ export function set_paths(paths) {
9
+ base = paths.base;
10
+ assets = paths.assets || base;
11
+ }