@sveltejs/kit 1.0.0-next.42 → 1.0.0-next.422

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 (126) hide show
  1. package/README.md +12 -9
  2. package/package.json +95 -63
  3. package/src/cli.js +112 -0
  4. package/src/core/adapt/builder.js +207 -0
  5. package/src/core/adapt/index.js +19 -0
  6. package/src/core/config/index.js +86 -0
  7. package/src/core/config/options.js +482 -0
  8. package/src/core/config/types.d.ts +1 -0
  9. package/src/core/constants.js +5 -0
  10. package/src/core/env.js +91 -0
  11. package/src/core/generate_manifest/index.js +99 -0
  12. package/src/core/prerender/crawl.js +194 -0
  13. package/src/core/prerender/prerender.js +378 -0
  14. package/src/core/prerender/queue.js +80 -0
  15. package/src/core/sync/create_manifest_data/index.js +506 -0
  16. package/src/core/sync/create_manifest_data/types.d.ts +40 -0
  17. package/src/core/sync/sync.js +59 -0
  18. package/src/core/sync/utils.js +44 -0
  19. package/src/core/sync/write_ambient.js +27 -0
  20. package/src/core/sync/write_client_manifest.js +82 -0
  21. package/src/core/sync/write_matchers.js +25 -0
  22. package/src/core/sync/write_root.js +88 -0
  23. package/src/core/sync/write_tsconfig.js +189 -0
  24. package/src/core/sync/write_types.js +769 -0
  25. package/src/core/utils.js +58 -0
  26. package/src/hooks.js +26 -0
  27. package/src/index/index.js +45 -0
  28. package/src/index/private.js +33 -0
  29. package/src/node/index.js +145 -0
  30. package/src/node/polyfills.js +40 -0
  31. package/src/runtime/app/env.js +11 -0
  32. package/src/runtime/app/navigation.js +22 -0
  33. package/src/runtime/app/paths.js +1 -0
  34. package/src/runtime/app/stores.js +91 -0
  35. package/src/runtime/client/ambient.d.ts +17 -0
  36. package/src/runtime/client/client.js +1258 -0
  37. package/src/runtime/client/fetcher.js +60 -0
  38. package/src/runtime/client/parse.js +36 -0
  39. package/src/runtime/client/singletons.js +11 -0
  40. package/src/runtime/client/start.js +46 -0
  41. package/src/runtime/client/types.d.ts +105 -0
  42. package/src/runtime/client/utils.js +113 -0
  43. package/src/runtime/components/error.svelte +16 -0
  44. package/{assets → src/runtime}/components/layout.svelte +0 -0
  45. package/src/runtime/env/dynamic/private.js +1 -0
  46. package/src/runtime/env/dynamic/public.js +1 -0
  47. package/src/runtime/env-private.js +7 -0
  48. package/src/runtime/env-public.js +7 -0
  49. package/src/runtime/env.js +6 -0
  50. package/src/runtime/hash.js +16 -0
  51. package/src/runtime/paths.js +11 -0
  52. package/src/runtime/server/endpoint.js +58 -0
  53. package/src/runtime/server/index.js +448 -0
  54. package/src/runtime/server/page/cookie.js +25 -0
  55. package/src/runtime/server/page/crypto.js +239 -0
  56. package/src/runtime/server/page/csp.js +249 -0
  57. package/src/runtime/server/page/fetch.js +266 -0
  58. package/src/runtime/server/page/index.js +416 -0
  59. package/src/runtime/server/page/load_data.js +127 -0
  60. package/src/runtime/server/page/render.js +359 -0
  61. package/src/runtime/server/page/respond_with_error.js +94 -0
  62. package/src/runtime/server/page/types.d.ts +44 -0
  63. package/src/runtime/server/utils.js +116 -0
  64. package/src/utils/error.js +22 -0
  65. package/src/utils/escape.js +104 -0
  66. package/src/utils/filesystem.js +108 -0
  67. package/src/utils/http.js +55 -0
  68. package/src/utils/misc.js +1 -0
  69. package/src/utils/routing.js +107 -0
  70. package/src/utils/url.js +97 -0
  71. package/src/vite/build/build_server.js +337 -0
  72. package/src/vite/build/build_service_worker.js +90 -0
  73. package/src/vite/build/utils.js +157 -0
  74. package/src/vite/dev/index.js +568 -0
  75. package/src/vite/index.js +566 -0
  76. package/src/vite/preview/index.js +186 -0
  77. package/src/vite/types.d.ts +3 -0
  78. package/src/vite/utils.js +334 -0
  79. package/svelte-kit.js +1 -1
  80. package/types/ambient.d.ts +355 -0
  81. package/types/index.d.ts +342 -0
  82. package/types/internal.d.ts +308 -0
  83. package/types/private.d.ts +209 -0
  84. package/CHANGELOG.md +0 -425
  85. package/assets/components/error.svelte +0 -13
  86. package/assets/runtime/app/env.js +0 -5
  87. package/assets/runtime/app/navigation.js +0 -41
  88. package/assets/runtime/app/paths.js +0 -1
  89. package/assets/runtime/app/stores.js +0 -93
  90. package/assets/runtime/chunks/utils.js +0 -19
  91. package/assets/runtime/internal/singletons.js +0 -23
  92. package/assets/runtime/internal/start.js +0 -770
  93. package/assets/runtime/paths.js +0 -12
  94. package/dist/api.js +0 -28
  95. package/dist/api.js.map +0 -1
  96. package/dist/chunks/index.js +0 -3521
  97. package/dist/chunks/index2.js +0 -587
  98. package/dist/chunks/index3.js +0 -246
  99. package/dist/chunks/index4.js +0 -530
  100. package/dist/chunks/index5.js +0 -761
  101. package/dist/chunks/index6.js +0 -322
  102. package/dist/chunks/standard.js +0 -99
  103. package/dist/chunks/utils.js +0 -83
  104. package/dist/cli.js +0 -546
  105. package/dist/cli.js.map +0 -1
  106. package/dist/create_app.js +0 -592
  107. package/dist/create_app.js.map +0 -1
  108. package/dist/index.js +0 -392
  109. package/dist/index.js.map +0 -1
  110. package/dist/index2.js +0 -3519
  111. package/dist/index2.js.map +0 -1
  112. package/dist/index3.js +0 -320
  113. package/dist/index3.js.map +0 -1
  114. package/dist/index4.js +0 -323
  115. package/dist/index4.js.map +0 -1
  116. package/dist/index5.js +0 -247
  117. package/dist/index5.js.map +0 -1
  118. package/dist/index6.js +0 -761
  119. package/dist/index6.js.map +0 -1
  120. package/dist/renderer.js +0 -2499
  121. package/dist/renderer.js.map +0 -1
  122. package/dist/ssr.js +0 -2581
  123. package/dist/standard.js +0 -100
  124. package/dist/standard.js.map +0 -1
  125. package/dist/utils.js +0 -84
  126. package/dist/utils.js.map +0 -1
@@ -0,0 +1,359 @@
1
+ import devalue from 'devalue';
2
+ import { readable, writable } from 'svelte/store';
3
+ import * as cookie from 'cookie';
4
+ import { hash } from '../../hash.js';
5
+ import { render_json_payload_script } from '../../../utils/escape.js';
6
+ import { s } from '../../../utils/misc.js';
7
+ import { Csp } from './csp.js';
8
+ import { PrerenderingURL } from '../../../utils/url.js';
9
+ import { serialize_error } from '../utils.js';
10
+ import { HttpError } from '../../../index/private.js';
11
+
12
+ // TODO rename this function/module
13
+
14
+ const updated = {
15
+ ...readable(false),
16
+ check: () => false
17
+ };
18
+
19
+ /**
20
+ * Creates the HTML response.
21
+ * @param {{
22
+ * branch: Array<import('./types').Loaded>;
23
+ * fetched: Array<import('./types').Fetched>;
24
+ * cookies: import('set-cookie-parser').Cookie[];
25
+ * options: import('types').SSROptions;
26
+ * state: import('types').SSRState;
27
+ * page_config: { hydrate: boolean, router: boolean };
28
+ * status: number;
29
+ * error: HttpError | Error | null;
30
+ * event: import('types').RequestEvent;
31
+ * resolve_opts: import('types').RequiredResolveOptions;
32
+ * validation_errors: Record<string, string> | undefined;
33
+ * }} opts
34
+ */
35
+ export async function render_response({
36
+ branch,
37
+ fetched,
38
+ cookies,
39
+ options,
40
+ state,
41
+ page_config,
42
+ status,
43
+ error = null,
44
+ event,
45
+ resolve_opts,
46
+ validation_errors
47
+ }) {
48
+ if (state.prerendering) {
49
+ if (options.csp.mode === 'nonce') {
50
+ throw new Error('Cannot use prerendering if config.kit.csp.mode === "nonce"');
51
+ }
52
+
53
+ if (options.template_contains_nonce) {
54
+ throw new Error('Cannot use prerendering if page template contains %sveltekit.nonce%');
55
+ }
56
+ }
57
+
58
+ const { entry } = options.manifest._;
59
+
60
+ const stylesheets = new Set(entry.stylesheets);
61
+ const modulepreloads = new Set(entry.imports);
62
+
63
+ /** @type {Set<string>} */
64
+ const link_header_preloads = new Set();
65
+
66
+ /** @type {Map<string, string>} */
67
+ // TODO if we add a client entry point one day, we will need to include inline_styles with the entry, otherwise stylesheets will be linked even if they are below inlineStyleThreshold
68
+ const inline_styles = new Map();
69
+
70
+ let rendered;
71
+
72
+ const stack = error instanceof HttpError ? undefined : error?.stack;
73
+
74
+ if (error && options.dev && !(error instanceof HttpError)) {
75
+ error.stack = options.get_stack(error);
76
+ }
77
+
78
+ if (resolve_opts.ssr) {
79
+ /** @type {Record<string, any>} */
80
+ const props = {
81
+ stores: {
82
+ page: writable(null),
83
+ navigating: writable(null),
84
+ updated
85
+ },
86
+ /** @type {import('types').Page} */
87
+ page: {
88
+ error,
89
+ params: /** @type {Record<string, any>} */ (event.params),
90
+ routeId: event.routeId,
91
+ status,
92
+ url: state.prerendering ? new PrerenderingURL(event.url) : event.url,
93
+ data: branch.reduce((acc, { data }) => (Object.assign(acc, data), acc), {})
94
+ },
95
+ components: await Promise.all(branch.map(({ node }) => node.component()))
96
+ };
97
+
98
+ // TODO remove this for 1.0
99
+ /**
100
+ * @param {string} property
101
+ * @param {string} replacement
102
+ */
103
+ const print_error = (property, replacement) => {
104
+ Object.defineProperty(props.page, property, {
105
+ get: () => {
106
+ throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
107
+ }
108
+ });
109
+ };
110
+
111
+ print_error('origin', 'origin');
112
+ print_error('path', 'pathname');
113
+ print_error('query', 'searchParams');
114
+
115
+ // props_n (instead of props[n]) makes it easy to avoid
116
+ // unnecessary updates for layout components
117
+ for (let i = 0; i < branch.length; i += 1) {
118
+ props[`data_${i}`] = branch[i].data;
119
+ }
120
+
121
+ if (validation_errors) {
122
+ props.errors = validation_errors;
123
+ }
124
+
125
+ rendered = options.root.render(props);
126
+
127
+ for (const { node } of branch) {
128
+ if (node.imports) {
129
+ node.imports.forEach((url) => modulepreloads.add(url));
130
+ }
131
+
132
+ if (node.stylesheets) {
133
+ node.stylesheets.forEach((url) => stylesheets.add(url));
134
+ }
135
+
136
+ if (node.inline_styles) {
137
+ Object.entries(await node.inline_styles()).forEach(([k, v]) => inline_styles.set(k, v));
138
+ }
139
+ }
140
+ } else {
141
+ rendered = { head: '', html: '', css: { code: '', map: null } };
142
+ }
143
+
144
+ let { head, html: body } = rendered;
145
+
146
+ const csp = new Csp(options.csp, {
147
+ dev: options.dev,
148
+ prerender: !!state.prerendering
149
+ });
150
+
151
+ const target = hash(body);
152
+
153
+ /**
154
+ * The prefix to use for static assets. Replaces `%sveltekit.assets%` in the template
155
+ * @type {string}
156
+ */
157
+ let assets;
158
+
159
+ if (options.paths.assets) {
160
+ // if an asset path is specified, use it
161
+ assets = options.paths.assets;
162
+ } else if (state.prerendering?.fallback) {
163
+ // if we're creating a fallback page, asset paths need to be root-relative
164
+ assets = options.paths.base;
165
+ } else {
166
+ // otherwise we want asset paths to be relative to the page, so that they
167
+ // will work in odd contexts like IPFS, the internet archive, and so on
168
+ const segments = event.url.pathname.slice(options.paths.base.length).split('/').slice(2);
169
+ assets = segments.length > 0 ? segments.map(() => '..').join('/') : '.';
170
+ }
171
+
172
+ /** @param {string} path */
173
+ const prefixed = (path) => (path.startsWith('/') ? path : `${assets}/${path}`);
174
+
175
+ // prettier-ignore
176
+ const init_app = `
177
+ import { set_public_env, start } from ${s(prefixed(entry.file))};
178
+
179
+ set_public_env(${s(options.public_env)});
180
+
181
+ start({
182
+ target: document.querySelector('[data-sveltekit-hydrate="${target}"]').parentNode,
183
+ paths: ${s(options.paths)},
184
+ route: ${!!page_config.router},
185
+ spa: ${!resolve_opts.ssr},
186
+ trailing_slash: ${s(options.trailing_slash)},
187
+ hydrate: ${resolve_opts.ssr && page_config.hydrate ? `{
188
+ status: ${status},
189
+ error: ${error && serialize_error(error, e => e.stack)},
190
+ node_ids: [${branch.map(({ node }) => node.index).join(', ')}],
191
+ params: ${devalue(event.params)},
192
+ routeId: ${s(event.routeId)}
193
+ }` : 'null'}
194
+ });
195
+ `;
196
+
197
+ // we use an anonymous function instead of an arrow function to support
198
+ // older browsers (https://github.com/sveltejs/kit/pull/5417)
199
+ const init_service_worker = `
200
+ if ('serviceWorker' in navigator) {
201
+ addEventListener('load', function () {
202
+ navigator.serviceWorker.register('${options.service_worker}');
203
+ });
204
+ }
205
+ `;
206
+
207
+ if (inline_styles.size > 0) {
208
+ const content = Array.from(inline_styles.values()).join('\n');
209
+
210
+ const attributes = [];
211
+ if (options.dev) attributes.push(' data-sveltekit');
212
+ if (csp.style_needs_nonce) attributes.push(` nonce="${csp.nonce}"`);
213
+
214
+ csp.add_style(content);
215
+
216
+ head += `\n\t<style${attributes.join('')}>${content}</style>`;
217
+ }
218
+
219
+ for (const dep of stylesheets) {
220
+ const path = prefixed(dep);
221
+ const attributes = [];
222
+
223
+ if (csp.style_needs_nonce) {
224
+ attributes.push(`nonce="${csp.nonce}"`);
225
+ }
226
+
227
+ if (inline_styles.has(dep)) {
228
+ // don't load stylesheets that are already inlined
229
+ // include them in disabled state so that Vite can detect them and doesn't try to add them
230
+ attributes.push('disabled', 'media="(max-width: 0)"');
231
+ } else {
232
+ const preload_atts = ['rel="preload"', 'as="style"'].concat(attributes);
233
+ link_header_preloads.add(`<${encodeURI(path)}>; ${preload_atts.join(';')}; nopush`);
234
+ }
235
+
236
+ attributes.unshift('rel="stylesheet"');
237
+ head += `\n\t<link href="${path}" ${attributes.join(' ')}>`;
238
+ }
239
+
240
+ if (page_config.router || page_config.hydrate) {
241
+ for (const dep of modulepreloads) {
242
+ const path = prefixed(dep);
243
+ link_header_preloads.add(`<${encodeURI(path)}>; rel="modulepreload"; nopush`);
244
+ if (state.prerendering) {
245
+ head += `\n\t<link rel="modulepreload" href="${path}">`;
246
+ }
247
+ }
248
+
249
+ const attributes = ['type="module"', `data-sveltekit-hydrate="${target}"`];
250
+
251
+ csp.add_script(init_app);
252
+
253
+ if (csp.script_needs_nonce) {
254
+ attributes.push(`nonce="${csp.nonce}"`);
255
+ }
256
+
257
+ body += `\n\t\t<script ${attributes.join(' ')}>${init_app}</script>`;
258
+ }
259
+
260
+ if (resolve_opts.ssr && page_config.hydrate) {
261
+ /** @type {string[]} */
262
+ const serialized_data = [];
263
+
264
+ for (const { url, body, response } of fetched) {
265
+ serialized_data.push(
266
+ render_json_payload_script(
267
+ { type: 'data', url, body: typeof body === 'string' ? hash(body) : undefined },
268
+ response
269
+ )
270
+ );
271
+ }
272
+
273
+ if (branch.some((node) => node.server_data)) {
274
+ serialized_data.push(
275
+ render_json_payload_script(
276
+ { type: 'server_data' },
277
+ branch.map(({ server_data }) => server_data)
278
+ )
279
+ );
280
+ }
281
+
282
+ if (validation_errors) {
283
+ serialized_data.push(
284
+ render_json_payload_script({ type: 'validation_errors' }, validation_errors)
285
+ );
286
+ }
287
+
288
+ body += `\n\t${serialized_data.join('\n\t')}`;
289
+ }
290
+
291
+ if (options.service_worker) {
292
+ // always include service worker unless it's turned off explicitly
293
+ csp.add_script(init_service_worker);
294
+
295
+ head += `
296
+ <script${csp.script_needs_nonce ? ` nonce="${csp.nonce}"` : ''}>${init_service_worker}</script>`;
297
+ }
298
+
299
+ if (state.prerendering) {
300
+ // TODO read headers set with setHeaders and convert into http-equiv where possible
301
+ const http_equiv = [];
302
+
303
+ const csp_headers = csp.csp_provider.get_meta();
304
+ if (csp_headers) {
305
+ http_equiv.push(csp_headers);
306
+ }
307
+
308
+ if (state.prerendering.cache) {
309
+ http_equiv.push(`<meta http-equiv="cache-control" content="${state.prerendering.cache}">`);
310
+ }
311
+
312
+ if (http_equiv.length > 0) {
313
+ head = http_equiv.join('\n') + head;
314
+ }
315
+ }
316
+
317
+ // TODO flush chunks as early as we can
318
+ const html =
319
+ (await resolve_opts.transformPageChunk({
320
+ html: options.template({ head, body, assets, nonce: /** @type {string} */ (csp.nonce) }),
321
+ done: true
322
+ })) || '';
323
+
324
+ const headers = new Headers({
325
+ 'content-type': 'text/html',
326
+ etag: `"${hash(html)}"`
327
+ });
328
+
329
+ if (!state.prerendering) {
330
+ const csp_header = csp.csp_provider.get_header();
331
+ if (csp_header) {
332
+ headers.set('content-security-policy', csp_header);
333
+ }
334
+ const report_only_header = csp.report_only_provider.get_header();
335
+ if (report_only_header) {
336
+ headers.set('content-security-policy-report-only', report_only_header);
337
+ }
338
+
339
+ for (const new_cookie of cookies) {
340
+ const { name, value, ...options } = new_cookie;
341
+ // @ts-expect-error
342
+ headers.append('set-cookie', cookie.serialize(name, value, options));
343
+ }
344
+
345
+ if (link_header_preloads.size) {
346
+ headers.set('link', Array.from(link_header_preloads).join(', '));
347
+ }
348
+ }
349
+
350
+ if (error && options.dev && !(error instanceof HttpError)) {
351
+ // reset stack, otherwise it may be 'fixed' a second time
352
+ error.stack = stack;
353
+ }
354
+
355
+ return new Response(html, {
356
+ status,
357
+ headers
358
+ });
359
+ }
@@ -0,0 +1,94 @@
1
+ import { render_response } from './render.js';
2
+ import { load_data, load_server_data } from './load_data.js';
3
+ import { coalesce_to_error } from '../../../utils/error.js';
4
+ import { GENERIC_ERROR } from '../utils.js';
5
+ import { create_fetch } from './fetch.js';
6
+
7
+ /**
8
+ * @typedef {import('./types.js').Loaded} Loaded
9
+ * @typedef {import('types').SSROptions} SSROptions
10
+ * @typedef {import('types').SSRState} SSRState
11
+ */
12
+
13
+ /**
14
+ * @param {{
15
+ * event: import('types').RequestEvent;
16
+ * options: SSROptions;
17
+ * state: SSRState;
18
+ * status: number;
19
+ * error: Error;
20
+ * resolve_opts: import('types').RequiredResolveOptions;
21
+ * }} opts
22
+ */
23
+ export async function respond_with_error({ event, options, state, status, error, resolve_opts }) {
24
+ const { fetcher, fetched, cookies } = create_fetch({
25
+ event,
26
+ options,
27
+ state,
28
+ route: GENERIC_ERROR
29
+ });
30
+
31
+ try {
32
+ const branch = [];
33
+
34
+ if (resolve_opts.ssr) {
35
+ const default_layout = await options.manifest._.nodes[0](); // 0 is always the root layout
36
+
37
+ const server_data_promise = load_server_data({
38
+ dev: options.dev,
39
+ event,
40
+ node: default_layout,
41
+ parent: async () => ({})
42
+ });
43
+
44
+ const server_data = await server_data_promise;
45
+
46
+ const data = await load_data({
47
+ event,
48
+ fetcher,
49
+ node: default_layout,
50
+ parent: async () => ({}),
51
+ server_data_promise,
52
+ state
53
+ });
54
+
55
+ branch.push(
56
+ {
57
+ node: default_layout,
58
+ server_data,
59
+ data
60
+ },
61
+ {
62
+ node: await options.manifest._.nodes[1](), // 1 is always the root error
63
+ data: null,
64
+ server_data: null
65
+ }
66
+ );
67
+ }
68
+
69
+ return await render_response({
70
+ options,
71
+ state,
72
+ page_config: {
73
+ hydrate: options.hydrate,
74
+ router: options.router
75
+ },
76
+ status,
77
+ error,
78
+ branch,
79
+ fetched,
80
+ cookies,
81
+ event,
82
+ resolve_opts,
83
+ validation_errors: undefined
84
+ });
85
+ } catch (err) {
86
+ const error = coalesce_to_error(err);
87
+
88
+ options.handle_error(error, event);
89
+
90
+ return new Response(error.stack, {
91
+ status: 500
92
+ });
93
+ }
94
+ }
@@ -0,0 +1,44 @@
1
+ import { ResponseHeaders, SSRNode, CspDirectives } from 'types';
2
+ import { HttpError } from '../../../index/private';
3
+
4
+ export interface Fetched {
5
+ url: string;
6
+ body?: string | null;
7
+ response: {
8
+ status: number;
9
+ statusText: string;
10
+ headers: ResponseHeaders;
11
+ body: string;
12
+ };
13
+ }
14
+
15
+ export interface FetchState {
16
+ fetched: Fetched[];
17
+ cookies: string[];
18
+ new_cookies: string[];
19
+ }
20
+
21
+ export type Loaded = {
22
+ node: SSRNode;
23
+ data: Record<string, any> | null;
24
+ server_data: any;
25
+ };
26
+
27
+ type CspMode = 'hash' | 'nonce' | 'auto';
28
+
29
+ export interface CspConfig {
30
+ mode: CspMode;
31
+ directives: CspDirectives;
32
+ reportOnly: CspDirectives;
33
+ }
34
+
35
+ export interface CspOpts {
36
+ dev: boolean;
37
+ prerender: boolean;
38
+ }
39
+
40
+ export interface SerializedHttpError extends Pick<HttpError, 'message' | 'status'> {
41
+ name: 'HttpError';
42
+ stack: '';
43
+ __is_http_error: true;
44
+ }
@@ -0,0 +1,116 @@
1
+ import { HttpError } from '../../index/private.js';
2
+
3
+ /** @param {any} body */
4
+ export function is_pojo(body) {
5
+ if (typeof body !== 'object') return false;
6
+
7
+ if (body) {
8
+ if (body instanceof Uint8Array) return false;
9
+ if (body instanceof ReadableStream) return false;
10
+
11
+ // if body is a node Readable, throw an error
12
+ // TODO remove this for 1.0
13
+ if (body._readableState && typeof body.pipe === 'function') {
14
+ throw new Error('Node streams are no longer supported — use a ReadableStream instead');
15
+ }
16
+ }
17
+
18
+ return true;
19
+ }
20
+
21
+ /**
22
+ * Serialize an error into a JSON string through `error_to_pojo`.
23
+ * This is necessary because `JSON.stringify(error) === '{}'`
24
+ *
25
+ * @param {Error | HttpError} error
26
+ * @param {(error: Error) => string | undefined} get_stack
27
+ */
28
+ export function serialize_error(error, get_stack) {
29
+ return JSON.stringify(error_to_pojo(error, get_stack));
30
+ }
31
+
32
+ /**
33
+ * Transform an error into a POJO, by copying its `name`, `message`
34
+ * and (in dev) `stack`, plus any custom properties, plus recursively
35
+ * serialized `cause` properties.
36
+ * Our own HttpError gets a meta property attached so we can identify it on the client.
37
+ *
38
+ * @param {HttpError | Error } error
39
+ * @param {(error: Error) => string | undefined} get_stack
40
+ */
41
+ export function error_to_pojo(error, get_stack) {
42
+ if (error instanceof HttpError) {
43
+ return /** @type {import('./page/types').SerializedHttpError} */ ({
44
+ message: error.message,
45
+ status: error.status,
46
+ __is_http_error: true // TODO we should probably make this unnecessary
47
+ });
48
+ }
49
+
50
+ const {
51
+ name,
52
+ message,
53
+ stack,
54
+ // @ts-expect-error i guess typescript doesn't know about error.cause yet
55
+ cause,
56
+ ...custom
57
+ } = error;
58
+
59
+ /** @type {Record<string, any>} */
60
+ const object = { name, message, stack: get_stack(error) };
61
+
62
+ if (cause) object.cause = error_to_pojo(cause, get_stack);
63
+
64
+ for (const key in custom) {
65
+ // @ts-expect-error
66
+ object[key] = custom[key];
67
+ }
68
+
69
+ return object;
70
+ }
71
+
72
+ // TODO: Remove for 1.0
73
+ /** @param {Record<string, any>} mod */
74
+ export function check_method_names(mod) {
75
+ ['get', 'post', 'put', 'patch', 'del'].forEach((m) => {
76
+ if (m in mod) {
77
+ const replacement = m === 'del' ? 'DELETE' : m.toUpperCase();
78
+ throw Error(
79
+ `Endpoint method "${m}" has changed to "${replacement}". See https://github.com/sveltejs/kit/discussions/5359 for more information.`
80
+ );
81
+ }
82
+ });
83
+ }
84
+
85
+ /** @type {import('types').SSRErrorPage} */
86
+ export const GENERIC_ERROR = {
87
+ id: '__error'
88
+ };
89
+
90
+ /**
91
+ * @param {Partial<Record<import('types').HttpMethod, any>>} mod
92
+ * @param {import('types').HttpMethod} method
93
+ */
94
+ export function method_not_allowed(mod, method) {
95
+ return new Response(`${method} method not allowed`, {
96
+ status: 405,
97
+ headers: {
98
+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
99
+ // "The server must generate an Allow header field in a 405 status code response"
100
+ allow: allowed_methods(mod).join(', ')
101
+ }
102
+ });
103
+ }
104
+
105
+ /** @param {Partial<Record<import('types').HttpMethod, any>>} mod */
106
+ export function allowed_methods(mod) {
107
+ const allowed = [];
108
+
109
+ for (const method in ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) {
110
+ if (method in mod) allowed.push(method);
111
+ }
112
+
113
+ if (mod.GET || mod.HEAD) allowed.push('HEAD');
114
+
115
+ return allowed;
116
+ }
@@ -0,0 +1,22 @@
1
+ import { HttpError, Redirect } from '../index/private.js';
2
+
3
+ /**
4
+ * @param {unknown} err
5
+ * @return {Error}
6
+ */
7
+ export function coalesce_to_error(err) {
8
+ return err instanceof Error ||
9
+ (err && /** @type {any} */ (err).name && /** @type {any} */ (err).message)
10
+ ? /** @type {Error} */ (err)
11
+ : new Error(JSON.stringify(err));
12
+ }
13
+
14
+ /**
15
+ * This is an identity function that exists to make TypeScript less
16
+ * paranoid about people throwing things that aren't errors, which
17
+ * frankly is not something we should care about
18
+ * @param {unknown} error
19
+ */
20
+ export function normalize_error(error) {
21
+ return /** @type {Redirect | HttpError | Error} */ (error);
22
+ }