@sveltejs/kit 1.0.11 → 1.0.13
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/package.json +3 -3
- package/src/core/adapt/builder.js +1 -1
- package/src/core/{prerender → postbuild}/crawl.js +0 -0
- package/src/core/{prerender → postbuild}/entities.js +0 -0
- package/src/core/{prerender → postbuild}/fallback.js +10 -7
- package/src/core/postbuild/index.js +104 -0
- package/src/core/{prerender → postbuild}/prerender.js +32 -106
- package/src/core/{prerender → postbuild}/queue.js +0 -0
- package/src/core/sync/sync.js +10 -0
- package/src/core/sync/write_client_manifest.js +1 -1
- package/src/core/sync/write_server.js +89 -0
- package/src/core/utils.js +0 -9
- package/src/exports/vite/build/build_server.js +11 -163
- package/src/exports/vite/build/build_service_worker.js +3 -2
- package/src/exports/vite/build/utils.js +1 -0
- package/src/exports/vite/dev/index.js +72 -114
- package/src/exports/vite/index.js +26 -27
- package/src/exports/vite/preview/index.js +11 -12
- package/src/runtime/app/environment.js +1 -1
- package/src/runtime/app/paths.js +1 -1
- package/src/runtime/client/client.js +8 -2
- package/src/runtime/client/fetcher.js +12 -4
- package/src/runtime/client/start.js +1 -2
- package/src/runtime/client/types.d.ts +1 -1
- package/src/runtime/client/utils.js +1 -2
- package/src/runtime/control.js +23 -5
- package/src/runtime/server/ambient.d.ts +8 -0
- package/src/runtime/server/cookie.js +4 -5
- package/src/runtime/server/data/index.js +5 -2
- package/src/runtime/server/endpoint.js +5 -5
- package/src/runtime/server/fetch.js +11 -9
- package/src/runtime/server/index.js +54 -395
- package/src/runtime/server/page/csp.js +9 -11
- package/src/runtime/server/page/index.js +13 -8
- package/src/runtime/server/page/load_data.js +2 -3
- package/src/runtime/server/page/render.js +28 -25
- package/src/runtime/server/page/respond_with_error.js +20 -13
- package/src/runtime/server/page/types.d.ts +0 -1
- package/src/runtime/server/respond.js +419 -0
- package/src/runtime/server/utils.js +21 -4
- package/src/runtime/shared.js +28 -0
- package/types/index.d.ts +10 -5
- package/types/internal.d.ts +22 -39
- package/src/runtime/env.js +0 -12
- package/src/runtime/paths.js +0 -11
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { mergeConfig } from 'vite';
|
|
4
|
-
import { mkdirp, posixify
|
|
4
|
+
import { mkdirp, posixify } from '../../../utils/filesystem.js';
|
|
5
5
|
import { get_vite_config } from '../utils.js';
|
|
6
|
-
import { load_error_page, load_template } from '../../../core/config/index.js';
|
|
7
|
-
import { runtime_directory } from '../../../core/utils.js';
|
|
8
6
|
import {
|
|
9
7
|
create_build,
|
|
10
8
|
find_deps,
|
|
@@ -13,162 +11,31 @@ import {
|
|
|
13
11
|
resolve_symlinks
|
|
14
12
|
} from './utils.js';
|
|
15
13
|
import { s } from '../../../utils/misc.js';
|
|
14
|
+
import { runtime_directory } from '../../../core/utils.js';
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
17
|
* @param {{
|
|
19
|
-
* hooks: string;
|
|
20
|
-
* config: import('types').ValidatedConfig;
|
|
21
|
-
* has_service_worker: boolean;
|
|
22
|
-
* runtime: string;
|
|
23
|
-
* template: string;
|
|
24
|
-
* error_page: string;
|
|
25
|
-
* }} opts
|
|
26
|
-
*/
|
|
27
|
-
const server_template = ({ config, hooks, has_service_worker, runtime, template, error_page }) => `
|
|
28
|
-
import root from '__GENERATED__/root.svelte';
|
|
29
|
-
import { respond } from '${runtime}/server/index.js';
|
|
30
|
-
import { set_paths, assets, base } from '${runtime}/paths.js';
|
|
31
|
-
import { set_building, set_version } from '${runtime}/env.js';
|
|
32
|
-
import { set_private_env } from '${runtime}/env-private.js';
|
|
33
|
-
import { set_public_env } from '${runtime}/env-public.js';
|
|
34
|
-
|
|
35
|
-
const app_template = ({ head, body, assets, nonce }) => ${s(template)
|
|
36
|
-
.replace('%sveltekit.head%', '" + head + "')
|
|
37
|
-
.replace('%sveltekit.body%', '" + body + "')
|
|
38
|
-
.replace(/%sveltekit\.assets%/g, '" + assets + "')
|
|
39
|
-
.replace(/%sveltekit\.nonce%/g, '" + nonce + "')};
|
|
40
|
-
|
|
41
|
-
const error_template = ({ status, message }) => ${s(error_page)
|
|
42
|
-
.replace(/%sveltekit\.status%/g, '" + status + "')
|
|
43
|
-
.replace(/%sveltekit\.error\.message%/g, '" + message + "')};
|
|
44
|
-
|
|
45
|
-
let read = null;
|
|
46
|
-
|
|
47
|
-
set_paths(${s(config.kit.paths)});
|
|
48
|
-
set_version(${s(config.kit.version.name)});
|
|
49
|
-
|
|
50
|
-
let default_protocol = 'https';
|
|
51
|
-
|
|
52
|
-
// allow paths to be globally overridden
|
|
53
|
-
// in svelte-kit preview and in prerendering
|
|
54
|
-
export function override(settings) {
|
|
55
|
-
default_protocol = settings.protocol || default_protocol;
|
|
56
|
-
set_paths(settings.paths);
|
|
57
|
-
set_building(settings.building);
|
|
58
|
-
read = settings.read;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export class Server {
|
|
62
|
-
constructor(manifest) {
|
|
63
|
-
this.options = {
|
|
64
|
-
csp: ${s(config.kit.csp)},
|
|
65
|
-
csrf: {
|
|
66
|
-
check_origin: ${s(config.kit.csrf.checkOrigin)},
|
|
67
|
-
},
|
|
68
|
-
dev: false,
|
|
69
|
-
embedded: ${config.kit.embedded},
|
|
70
|
-
handle_error: (error, event) => {
|
|
71
|
-
return this.options.hooks.handleError({ error, event }) ?? {
|
|
72
|
-
message: event.route.id != null ? 'Internal Error' : 'Not Found'
|
|
73
|
-
};
|
|
74
|
-
},
|
|
75
|
-
hooks: null,
|
|
76
|
-
manifest,
|
|
77
|
-
paths: { base, assets },
|
|
78
|
-
public_env: {},
|
|
79
|
-
read,
|
|
80
|
-
root,
|
|
81
|
-
service_worker: ${has_service_worker},
|
|
82
|
-
app_template,
|
|
83
|
-
app_template_contains_nonce: ${template.includes('%sveltekit.nonce%')},
|
|
84
|
-
error_template,
|
|
85
|
-
version: ${s(config.kit.version.name)}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Take care: Some adapters may have to call \`Server.init\` per-request to set env vars,
|
|
91
|
-
* so anything that shouldn't be rerun should be wrapped in an \`if\` block to make sure it hasn't
|
|
92
|
-
* been done already.
|
|
93
|
-
*/
|
|
94
|
-
async init({ env }) {
|
|
95
|
-
const entries = Object.entries(env);
|
|
96
|
-
|
|
97
|
-
const prv = Object.fromEntries(entries.filter(([k]) => !k.startsWith('${
|
|
98
|
-
config.kit.env.publicPrefix
|
|
99
|
-
}')));
|
|
100
|
-
|
|
101
|
-
const pub = Object.fromEntries(entries.filter(([k]) => k.startsWith('${
|
|
102
|
-
config.kit.env.publicPrefix
|
|
103
|
-
}')));
|
|
104
|
-
|
|
105
|
-
set_private_env(prv);
|
|
106
|
-
set_public_env(pub);
|
|
107
|
-
|
|
108
|
-
this.options.public_env = pub;
|
|
109
|
-
|
|
110
|
-
if (!this.options.hooks) {
|
|
111
|
-
const module = await import(${s(hooks)});
|
|
112
|
-
|
|
113
|
-
this.options.hooks = {
|
|
114
|
-
handle: module.handle || (({ event, resolve }) => resolve(event)),
|
|
115
|
-
handleError: module.handleError || (({ error }) => console.error(error.stack)),
|
|
116
|
-
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request))
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async respond(request, options = {}) {
|
|
122
|
-
if (!(request instanceof Request)) {
|
|
123
|
-
throw new Error('The first argument to server.respond must be a Request object. See https://github.com/sveltejs/kit/pull/3384 for details');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return respond(request, this.options, options);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
`;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* @param {{
|
|
133
|
-
* cwd: string;
|
|
134
18
|
* config: import('types').ValidatedConfig;
|
|
135
19
|
* vite_config: import('vite').ResolvedConfig;
|
|
136
20
|
* vite_config_env: import('vite').ConfigEnv;
|
|
137
21
|
* manifest_data: import('types').ManifestData;
|
|
138
|
-
* build_dir: string;
|
|
139
22
|
* output_dir: string;
|
|
140
|
-
* service_worker_entry_file: string | null;
|
|
141
23
|
* }} options
|
|
142
24
|
* @param {{ vite_manifest: import('vite').Manifest, assets: import('rollup').OutputAsset[] }} client
|
|
143
25
|
*/
|
|
144
26
|
export async function build_server(options, client) {
|
|
145
|
-
const {
|
|
146
|
-
cwd,
|
|
147
|
-
config,
|
|
148
|
-
vite_config,
|
|
149
|
-
vite_config_env,
|
|
150
|
-
manifest_data,
|
|
151
|
-
build_dir,
|
|
152
|
-
output_dir,
|
|
153
|
-
service_worker_entry_file
|
|
154
|
-
} = options;
|
|
155
|
-
|
|
156
|
-
let hooks_file = resolve_entry(config.kit.files.hooks.server);
|
|
157
|
-
|
|
158
|
-
if (!hooks_file || !fs.existsSync(hooks_file)) {
|
|
159
|
-
hooks_file = path.join(config.kit.outDir, 'build/hooks.js');
|
|
160
|
-
fs.writeFileSync(hooks_file, '');
|
|
161
|
-
}
|
|
27
|
+
const { config, vite_config, vite_config_env, manifest_data, output_dir } = options;
|
|
162
28
|
|
|
163
29
|
/** @type {Record<string, string>} */
|
|
164
30
|
const input = {
|
|
165
|
-
index: `${
|
|
31
|
+
index: `${runtime_directory}/server/index.js`,
|
|
32
|
+
internal: `${config.kit.outDir}/generated/server-internal.js`
|
|
166
33
|
};
|
|
167
34
|
|
|
168
35
|
// add entry points for every endpoint...
|
|
169
36
|
manifest_data.routes.forEach((route) => {
|
|
170
37
|
if (route.endpoint) {
|
|
171
|
-
const resolved = path.resolve(
|
|
38
|
+
const resolved = path.resolve(route.endpoint.file);
|
|
172
39
|
const relative = decodeURIComponent(path.relative(config.kit.files.routes, resolved));
|
|
173
40
|
const name = posixify(path.join('entries/endpoints', relative.replace(/\.js$/, '')));
|
|
174
41
|
input[name] = resolved;
|
|
@@ -179,7 +46,7 @@ export async function build_server(options, client) {
|
|
|
179
46
|
manifest_data.nodes.forEach((node) => {
|
|
180
47
|
for (const file of [node.component, node.universal, node.server]) {
|
|
181
48
|
if (file) {
|
|
182
|
-
const resolved = path.resolve(
|
|
49
|
+
const resolved = path.resolve(file);
|
|
183
50
|
const relative = decodeURIComponent(path.relative(config.kit.files.routes, resolved));
|
|
184
51
|
|
|
185
52
|
const name = relative.startsWith('..')
|
|
@@ -193,27 +60,9 @@ export async function build_server(options, client) {
|
|
|
193
60
|
// ...and every matcher
|
|
194
61
|
Object.entries(manifest_data.matchers).forEach(([key, file]) => {
|
|
195
62
|
const name = posixify(path.join('entries/matchers', key));
|
|
196
|
-
input[name] = path.resolve(
|
|
63
|
+
input[name] = path.resolve(file);
|
|
197
64
|
});
|
|
198
65
|
|
|
199
|
-
/** @type {(file: string) => string} */
|
|
200
|
-
const app_relative = (file) => {
|
|
201
|
-
const relative_file = path.relative(build_dir, path.resolve(cwd, file));
|
|
202
|
-
return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
fs.writeFileSync(
|
|
206
|
-
input.index,
|
|
207
|
-
server_template({
|
|
208
|
-
config,
|
|
209
|
-
hooks: app_relative(hooks_file),
|
|
210
|
-
has_service_worker: config.kit.serviceWorker.register && !!service_worker_entry_file,
|
|
211
|
-
runtime: posixify(path.relative(build_dir, runtime_directory)),
|
|
212
|
-
template: load_template(cwd, config),
|
|
213
|
-
error_page: load_error_page(config)
|
|
214
|
-
})
|
|
215
|
-
);
|
|
216
|
-
|
|
217
66
|
const merged_config = mergeConfig(
|
|
218
67
|
get_default_build_config({ config, input, ssr: true, outDir: `${output_dir}/server` }),
|
|
219
68
|
await get_vite_config(vite_config, vite_config_env)
|
|
@@ -321,21 +170,20 @@ export async function build_server(options, client) {
|
|
|
321
170
|
return {
|
|
322
171
|
chunks,
|
|
323
172
|
vite_manifest,
|
|
324
|
-
methods: get_methods(
|
|
173
|
+
methods: get_methods(chunks, manifest_data)
|
|
325
174
|
};
|
|
326
175
|
}
|
|
327
176
|
|
|
328
177
|
/**
|
|
329
|
-
* @param {string} cwd
|
|
330
178
|
* @param {import('rollup').OutputChunk[]} output
|
|
331
179
|
* @param {import('types').ManifestData} manifest_data
|
|
332
180
|
*/
|
|
333
|
-
function get_methods(
|
|
181
|
+
function get_methods(output, manifest_data) {
|
|
334
182
|
/** @type {Record<string, string[]>} */
|
|
335
183
|
const lookup = {};
|
|
336
184
|
output.forEach((chunk) => {
|
|
337
185
|
if (!chunk.facadeModuleId) return;
|
|
338
|
-
const id = posixify(path.relative(
|
|
186
|
+
const id = posixify(path.relative('.', chunk.facadeModuleId));
|
|
339
187
|
lookup[id] = chunk.exports;
|
|
340
188
|
});
|
|
341
189
|
|
|
@@ -11,13 +11,14 @@ import { assets_base } from './utils.js';
|
|
|
11
11
|
* vite_config_env: import('vite').ConfigEnv;
|
|
12
12
|
* manifest_data: import('types').ManifestData;
|
|
13
13
|
* output_dir: string;
|
|
14
|
-
* service_worker_entry_file: string | null;
|
|
15
14
|
* }} options
|
|
15
|
+
* @param {string} service_worker_entry_file
|
|
16
16
|
* @param {import('types').Prerendered} prerendered
|
|
17
17
|
* @param {import('vite').Manifest} client_manifest
|
|
18
18
|
*/
|
|
19
19
|
export async function build_service_worker(
|
|
20
|
-
{ config, vite_config, manifest_data, output_dir
|
|
20
|
+
{ config, vite_config, manifest_data, output_dir },
|
|
21
|
+
service_worker_entry_file,
|
|
21
22
|
prerendered,
|
|
22
23
|
client_manifest
|
|
23
24
|
) {
|
|
@@ -127,6 +127,7 @@ export function get_build_setup_config({ config, ssr }) {
|
|
|
127
127
|
__SVELTEKIT_ADAPTER_NAME__: JSON.stringify(config.kit.adapter?.name),
|
|
128
128
|
__SVELTEKIT_APP_VERSION_FILE__: JSON.stringify(`${config.kit.appDir}/version.json`),
|
|
129
129
|
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval),
|
|
130
|
+
__SVELTEKIT_DEV__: 'false',
|
|
130
131
|
__SVELTEKIT_EMBEDDED__: config.kit.embedded ? 'true' : 'false'
|
|
131
132
|
},
|
|
132
133
|
resolve: {
|
|
@@ -3,16 +3,16 @@ import colors from 'kleur';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import sirv from 'sirv';
|
|
5
5
|
import { URL } from 'url';
|
|
6
|
-
import { isCSSRequest } from 'vite';
|
|
6
|
+
import { isCSSRequest, loadEnv } from 'vite';
|
|
7
7
|
import { getRequest, setResponse } from '../../../exports/node/index.js';
|
|
8
8
|
import { installPolyfills } from '../../../exports/node/polyfills.js';
|
|
9
9
|
import { coalesce_to_error } from '../../../utils/error.js';
|
|
10
10
|
import { posixify, resolve_entry, to_fs } from '../../../utils/filesystem.js';
|
|
11
11
|
import { should_polyfill } from '../../../utils/platform.js';
|
|
12
|
-
import { load_error_page
|
|
12
|
+
import { load_error_page } from '../../../core/config/index.js';
|
|
13
13
|
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
|
|
14
14
|
import * as sync from '../../../core/sync/sync.js';
|
|
15
|
-
import { get_mime_lookup,
|
|
15
|
+
import { get_mime_lookup, runtime_base } from '../../../core/utils.js';
|
|
16
16
|
import { compact } from '../../../utils/array.js';
|
|
17
17
|
import { not_found } from '../utils.js';
|
|
18
18
|
|
|
@@ -42,9 +42,6 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
42
42
|
|
|
43
43
|
sync.init(svelte_config, vite_config.mode);
|
|
44
44
|
|
|
45
|
-
/** @type {import('types').Respond} */
|
|
46
|
-
const respond = (await import(`${runtime_prefix}/server/index.js`)).respond;
|
|
47
|
-
|
|
48
45
|
/** @type {import('types').ManifestData} */
|
|
49
46
|
let manifest_data;
|
|
50
47
|
/** @type {import('types').SSRManifest} */
|
|
@@ -96,7 +93,7 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
96
93
|
mimeTypes: get_mime_lookup(manifest_data),
|
|
97
94
|
_: {
|
|
98
95
|
entry: {
|
|
99
|
-
file:
|
|
96
|
+
file: `${runtime_base}/client/start.js`,
|
|
100
97
|
imports: [],
|
|
101
98
|
stylesheets: [],
|
|
102
99
|
fonts: []
|
|
@@ -224,9 +221,9 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
224
221
|
};
|
|
225
222
|
}
|
|
226
223
|
|
|
227
|
-
/** @param {
|
|
228
|
-
function fix_stack_trace(
|
|
229
|
-
return
|
|
224
|
+
/** @param {string} stack */
|
|
225
|
+
function fix_stack_trace(stack) {
|
|
226
|
+
return stack ? vite.ssrRewriteStacktrace(stack) : stack;
|
|
230
227
|
}
|
|
231
228
|
|
|
232
229
|
await update_manifest();
|
|
@@ -273,7 +270,8 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
273
270
|
sync.update(svelte_config, manifest_data, file);
|
|
274
271
|
});
|
|
275
272
|
|
|
276
|
-
const { appTemplate } = svelte_config.kit.files;
|
|
273
|
+
const { appTemplate, errorTemplate, serviceWorker, hooks } = svelte_config.kit.files;
|
|
274
|
+
|
|
277
275
|
// vite client only executes a full reload if the triggering html file path is index.html
|
|
278
276
|
// kit defaults to src/app.html, so unless user changed that to index.html
|
|
279
277
|
// send the vite client a full-reload event without path being set
|
|
@@ -285,6 +283,17 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
285
283
|
});
|
|
286
284
|
}
|
|
287
285
|
|
|
286
|
+
vite.watcher.on('all', (_, file) => {
|
|
287
|
+
if (
|
|
288
|
+
file === appTemplate ||
|
|
289
|
+
file === errorTemplate ||
|
|
290
|
+
file.startsWith(serviceWorker) ||
|
|
291
|
+
file.startsWith(hooks.server)
|
|
292
|
+
) {
|
|
293
|
+
sync.server(svelte_config);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
288
297
|
// changing the svelte config requires restarting the dev server
|
|
289
298
|
// the config is only read on start and passed on to vite-plugin-svelte
|
|
290
299
|
// which needs up-to-date values to operate correctly
|
|
@@ -307,6 +316,18 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
307
316
|
}
|
|
308
317
|
});
|
|
309
318
|
|
|
319
|
+
// This shameful hack allows us to load runtime server code via Vite
|
|
320
|
+
// while apps load `HttpError` and `Redirect` in Node, without
|
|
321
|
+
// causing `instanceof` checks to fail
|
|
322
|
+
const control_module_node = await import(`../../../runtime/control.js`);
|
|
323
|
+
const control_module_vite = await vite.ssrLoadModule(`${runtime_base}/control.js`);
|
|
324
|
+
|
|
325
|
+
control_module_node.replace_implementations({
|
|
326
|
+
ActionFailure: control_module_vite.ActionFailure,
|
|
327
|
+
HttpError: control_module_vite.HttpError,
|
|
328
|
+
Redirect: control_module_vite.Redirect
|
|
329
|
+
});
|
|
330
|
+
|
|
310
331
|
vite.middlewares.use(async (req, res, next) => {
|
|
311
332
|
try {
|
|
312
333
|
const base = `${vite.config.server.https ? 'https' : 'http'}://${
|
|
@@ -332,10 +353,12 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
332
353
|
} catch (e) {
|
|
333
354
|
const error = coalesce_to_error(e);
|
|
334
355
|
res.statusCode = 500;
|
|
335
|
-
res.end(fix_stack_trace(error));
|
|
356
|
+
res.end(fix_stack_trace(/** @type {string} */ (error.stack)));
|
|
336
357
|
}
|
|
337
358
|
});
|
|
338
359
|
|
|
360
|
+
const env = loadEnv(vite_config.mode, svelte_config.kit.env.dir, '');
|
|
361
|
+
|
|
339
362
|
return () => {
|
|
340
363
|
const serve_static_middleware = vite.middlewares.stack.find(
|
|
341
364
|
(middleware) =>
|
|
@@ -385,40 +408,28 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
385
408
|
return;
|
|
386
409
|
}
|
|
387
410
|
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
/** @type {import('types').ServerHooks} */
|
|
397
|
-
const hooks = {
|
|
398
|
-
handle,
|
|
399
|
-
handleError:
|
|
400
|
-
user_hooks.handleError ||
|
|
401
|
-
(({ error: e }) => {
|
|
402
|
-
const error = /** @type {Error & { frame?: string }} */ (e);
|
|
403
|
-
console.error(colors.bold().red(error.message ?? error)); // Could be anything
|
|
404
|
-
if (error.frame) {
|
|
405
|
-
console.error(colors.gray(error.frame));
|
|
406
|
-
}
|
|
407
|
-
if (error.stack) {
|
|
408
|
-
console.error(colors.gray(error.stack));
|
|
409
|
-
}
|
|
410
|
-
}),
|
|
411
|
-
handleFetch: user_hooks.handleFetch || (({ request, fetch }) => fetch(request))
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
// TODO the / prefix will probably fail if outDir is outside the cwd (which
|
|
415
|
-
// could be the case in a monorepo setup), but without it these modules
|
|
416
|
-
// can get loaded twice via different URLs, which causes failures. Might
|
|
417
|
-
// require changes to Vite to fix
|
|
418
|
-
const { default: root } = await vite.ssrLoadModule(
|
|
419
|
-
`/${posixify(path.relative(cwd, `${svelte_config.kit.outDir}/generated/root.svelte`))}`
|
|
411
|
+
const { set_paths, set_version, set_fix_stack_trace } =
|
|
412
|
+
/** @type {import('types').ServerInternalModule} */ (
|
|
413
|
+
await vite.ssrLoadModule(`${runtime_base}/shared.js`)
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
const { Server } = /** @type {import('types').ServerModule} */ (
|
|
417
|
+
await vite.ssrLoadModule(`${runtime_base}/server/index.js`)
|
|
420
418
|
);
|
|
421
419
|
|
|
420
|
+
set_paths({
|
|
421
|
+
base: svelte_config.kit.paths.base,
|
|
422
|
+
assets
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
set_version(svelte_config.kit.version.name);
|
|
426
|
+
|
|
427
|
+
set_fix_stack_trace(fix_stack_trace);
|
|
428
|
+
|
|
429
|
+
const server = new Server(manifest);
|
|
430
|
+
|
|
431
|
+
await server.init({ env });
|
|
432
|
+
|
|
422
433
|
let request;
|
|
423
434
|
|
|
424
435
|
try {
|
|
@@ -431,20 +442,19 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
431
442
|
return res.end('Invalid request body');
|
|
432
443
|
}
|
|
433
444
|
|
|
434
|
-
const template = load_template(cwd, svelte_config);
|
|
435
|
-
const error_page = load_error_page(svelte_config);
|
|
436
|
-
|
|
437
|
-
/** @param {{ status: number; message: string }} opts */
|
|
438
|
-
const error_template = ({ status, message }) => {
|
|
439
|
-
return error_page
|
|
440
|
-
.replace(/%sveltekit\.status%/g, String(status))
|
|
441
|
-
.replace(/%sveltekit\.error\.message%/g, message);
|
|
442
|
-
};
|
|
443
|
-
|
|
444
445
|
if (manifest_error) {
|
|
445
446
|
console.error(colors.bold().red('Invalid routes'));
|
|
446
447
|
console.error(manifest_error);
|
|
447
448
|
|
|
449
|
+
const error_page = load_error_page(svelte_config);
|
|
450
|
+
|
|
451
|
+
/** @param {{ status: number; message: string }} opts */
|
|
452
|
+
const error_template = ({ status, message }) => {
|
|
453
|
+
return error_page
|
|
454
|
+
.replace(/%sveltekit\.status%/g, String(status))
|
|
455
|
+
.replace(/%sveltekit\.error\.message%/g, message);
|
|
456
|
+
};
|
|
457
|
+
|
|
448
458
|
res.writeHead(500, {
|
|
449
459
|
'Content-Type': 'text/html; charset=utf-8'
|
|
450
460
|
});
|
|
@@ -455,66 +465,14 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
455
465
|
return;
|
|
456
466
|
}
|
|
457
467
|
|
|
458
|
-
const rendered = await respond(
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
check_origin: svelte_config.kit.csrf.checkOrigin
|
|
464
|
-
},
|
|
465
|
-
dev: true,
|
|
466
|
-
embedded: svelte_config.kit.embedded,
|
|
467
|
-
handle_error: async (error, event) => {
|
|
468
|
-
const error_object = await hooks.handleError({
|
|
469
|
-
error: new Proxy(error, {
|
|
470
|
-
get: (target, property) => {
|
|
471
|
-
if (property === 'stack') {
|
|
472
|
-
return fix_stack_trace(error);
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
return Reflect.get(target, property, target);
|
|
476
|
-
}
|
|
477
|
-
}),
|
|
478
|
-
event
|
|
479
|
-
});
|
|
480
|
-
return (
|
|
481
|
-
error_object ?? { message: event.route.id != null ? 'Internal Error' : 'Not Found' }
|
|
482
|
-
);
|
|
483
|
-
},
|
|
484
|
-
hooks,
|
|
485
|
-
manifest,
|
|
486
|
-
paths: {
|
|
487
|
-
base: svelte_config.kit.paths.base,
|
|
488
|
-
assets
|
|
489
|
-
},
|
|
490
|
-
public_env: {},
|
|
491
|
-
read: (file) => fs.readFileSync(path.join(svelte_config.kit.files.assets, file)),
|
|
492
|
-
root,
|
|
493
|
-
app_template: ({ head, body, assets, nonce }) => {
|
|
494
|
-
return (
|
|
495
|
-
template
|
|
496
|
-
.replace(/%sveltekit\.assets%/g, assets)
|
|
497
|
-
.replace(/%sveltekit\.nonce%/g, nonce)
|
|
498
|
-
// head and body must be replaced last, in case someone tries to sneak in %sveltekit.assets% etc
|
|
499
|
-
.replace('%sveltekit.head%', () => head)
|
|
500
|
-
.replace('%sveltekit.body%', () => body)
|
|
501
|
-
);
|
|
502
|
-
},
|
|
503
|
-
app_template_contains_nonce: template.includes('%sveltekit.nonce%'),
|
|
504
|
-
error_template,
|
|
505
|
-
service_worker:
|
|
506
|
-
svelte_config.kit.serviceWorker.register &&
|
|
507
|
-
!!resolve_entry(svelte_config.kit.files.serviceWorker),
|
|
508
|
-
version: svelte_config.kit.version.name
|
|
468
|
+
const rendered = await server.respond(request, {
|
|
469
|
+
getClientAddress: () => {
|
|
470
|
+
const { remoteAddress } = req.socket;
|
|
471
|
+
if (remoteAddress) return remoteAddress;
|
|
472
|
+
throw new Error('Could not determine clientAddress');
|
|
509
473
|
},
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
const { remoteAddress } = req.socket;
|
|
513
|
-
if (remoteAddress) return remoteAddress;
|
|
514
|
-
throw new Error('Could not determine clientAddress');
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
);
|
|
474
|
+
read: (file) => fs.readFileSync(path.join(svelte_config.kit.files.assets, file))
|
|
475
|
+
});
|
|
518
476
|
|
|
519
477
|
if (rendered.status === 404) {
|
|
520
478
|
// @ts-expect-error
|
|
@@ -527,7 +485,7 @@ export async function dev(vite, vite_config, svelte_config) {
|
|
|
527
485
|
} catch (e) {
|
|
528
486
|
const error = coalesce_to_error(e);
|
|
529
487
|
res.statusCode = 500;
|
|
530
|
-
res.end(fix_stack_trace(error));
|
|
488
|
+
res.end(fix_stack_trace(/** @type {string} */ (error.stack)));
|
|
531
489
|
}
|
|
532
490
|
});
|
|
533
491
|
};
|