@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,551 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import colors from 'kleur';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import sirv from 'sirv';
|
|
5
|
+
import { URL } from 'url';
|
|
6
|
+
import { getRequest, setResponse } from '../../node/index.js';
|
|
7
|
+
import { installPolyfills } from '../../node/polyfills.js';
|
|
8
|
+
import { coalesce_to_error } from '../../utils/error.js';
|
|
9
|
+
import { posixify } from '../../utils/filesystem.js';
|
|
10
|
+
import { parse_route_id } from '../../utils/routing.js';
|
|
11
|
+
import { load_template } from '../../core/config/index.js';
|
|
12
|
+
import { SVELTE_KIT_ASSETS } from '../../core/constants.js';
|
|
13
|
+
import * as sync from '../../core/sync/sync.js';
|
|
14
|
+
import { get_mime_lookup, runtime_base, runtime_prefix } from '../../core/utils.js';
|
|
15
|
+
import { get_env, prevent_illegal_vite_imports, resolve_entry } from '../utils.js';
|
|
16
|
+
|
|
17
|
+
// Vite doesn't expose this so we just copy the list for now
|
|
18
|
+
// https://github.com/vitejs/vite/blob/3edd1af56e980aef56641a5a51cf2932bb580d41/packages/vite/src/node/plugins/css.ts#L96
|
|
19
|
+
const style_pattern = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/;
|
|
20
|
+
|
|
21
|
+
const cwd = process.cwd();
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {import('vite').ViteDevServer} vite
|
|
25
|
+
* @param {import('vite').ResolvedConfig} vite_config
|
|
26
|
+
* @param {import('types').ValidatedConfig} svelte_config
|
|
27
|
+
* @param {Set<string>} illegal_imports
|
|
28
|
+
* @return {Promise<Promise<() => void>>}
|
|
29
|
+
*/
|
|
30
|
+
export async function dev(vite, vite_config, svelte_config, illegal_imports) {
|
|
31
|
+
installPolyfills();
|
|
32
|
+
|
|
33
|
+
sync.init(svelte_config, vite_config.mode);
|
|
34
|
+
|
|
35
|
+
/** @type {import('types').Respond} */
|
|
36
|
+
const respond = (await import(`${runtime_prefix}/server/index.js`)).respond;
|
|
37
|
+
|
|
38
|
+
/** @type {import('types').ManifestData} */
|
|
39
|
+
let manifest_data;
|
|
40
|
+
/** @type {import('types').SSRManifest} */
|
|
41
|
+
let manifest;
|
|
42
|
+
|
|
43
|
+
const extensions = [...svelte_config.extensions, ...svelte_config.kit.moduleExtensions];
|
|
44
|
+
|
|
45
|
+
/** @param {string} id */
|
|
46
|
+
async function resolve(id) {
|
|
47
|
+
const url = id.startsWith('..') ? `/@fs${path.posix.resolve(id)}` : `/${id}`;
|
|
48
|
+
|
|
49
|
+
const module = await vite.ssrLoadModule(url);
|
|
50
|
+
|
|
51
|
+
const module_node = await vite.moduleGraph.getModuleByUrl(url);
|
|
52
|
+
if (!module_node) throw new Error(`Could not find node for ${url}`);
|
|
53
|
+
|
|
54
|
+
return { module, module_node, url };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function update_manifest() {
|
|
58
|
+
({ manifest_data } = await sync.create(svelte_config));
|
|
59
|
+
|
|
60
|
+
manifest = {
|
|
61
|
+
appDir: svelte_config.kit.appDir,
|
|
62
|
+
assets: new Set(manifest_data.assets.map((asset) => asset.file)),
|
|
63
|
+
mimeTypes: get_mime_lookup(manifest_data),
|
|
64
|
+
_: {
|
|
65
|
+
entry: {
|
|
66
|
+
file: `/@fs${runtime_prefix}/client/start.js`,
|
|
67
|
+
imports: [],
|
|
68
|
+
stylesheets: []
|
|
69
|
+
},
|
|
70
|
+
nodes: manifest_data.nodes.map((node, index) => {
|
|
71
|
+
return async () => {
|
|
72
|
+
/** @type {import('types').SSRNode} */
|
|
73
|
+
const result = {};
|
|
74
|
+
|
|
75
|
+
/** @type {import('vite').ModuleNode[]} */
|
|
76
|
+
const module_nodes = [];
|
|
77
|
+
|
|
78
|
+
result.index = index;
|
|
79
|
+
|
|
80
|
+
// these are unused in dev, it's easier to include them
|
|
81
|
+
result.imports = [];
|
|
82
|
+
result.stylesheets = [];
|
|
83
|
+
|
|
84
|
+
if (node.component) {
|
|
85
|
+
result.component = async () => {
|
|
86
|
+
const { module_node, module, url } = await resolve(
|
|
87
|
+
/** @type {string} */ (node.component)
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
module_nodes.push(module_node);
|
|
91
|
+
result.file = url.endsWith('.svelte') ? url : url + '?import'; // TODO what is this for?
|
|
92
|
+
|
|
93
|
+
prevent_illegal_vite_imports(module_node, illegal_imports, extensions);
|
|
94
|
+
|
|
95
|
+
return module.default;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (node.shared) {
|
|
100
|
+
const { module, module_node } = await resolve(node.shared);
|
|
101
|
+
|
|
102
|
+
module_nodes.push(module_node);
|
|
103
|
+
|
|
104
|
+
result.shared = module;
|
|
105
|
+
|
|
106
|
+
prevent_illegal_vite_imports(module_node, illegal_imports, extensions);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (node.server) {
|
|
110
|
+
const { module } = await resolve(node.server);
|
|
111
|
+
result.server = module;
|
|
112
|
+
result.server_id = node.server;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// in dev we inline all styles to avoid FOUC. this gets populated lazily so that
|
|
116
|
+
// components/stylesheets loaded via import() during `load` are included
|
|
117
|
+
result.inline_styles = async () => {
|
|
118
|
+
const deps = new Set();
|
|
119
|
+
|
|
120
|
+
for (const module_node of module_nodes) {
|
|
121
|
+
await find_deps(vite, module_node, deps);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** @type {Record<string, string>} */
|
|
125
|
+
const styles = {};
|
|
126
|
+
|
|
127
|
+
for (const dep of deps) {
|
|
128
|
+
const parsed = new URL(dep.url, 'http://localhost/');
|
|
129
|
+
const query = parsed.searchParams;
|
|
130
|
+
|
|
131
|
+
if (
|
|
132
|
+
style_pattern.test(dep.file) ||
|
|
133
|
+
(query.has('svelte') && query.get('type') === 'style')
|
|
134
|
+
) {
|
|
135
|
+
try {
|
|
136
|
+
const mod = await vite.ssrLoadModule(dep.url);
|
|
137
|
+
styles[dep.url] = mod.default;
|
|
138
|
+
} catch {
|
|
139
|
+
// this can happen with dynamically imported modules, I think
|
|
140
|
+
// because the Vite module graph doesn't distinguish between
|
|
141
|
+
// static and dynamic imports? TODO investigate, submit fix
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return styles;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return result;
|
|
150
|
+
};
|
|
151
|
+
}),
|
|
152
|
+
routes: manifest_data.routes.map((route) => {
|
|
153
|
+
const { pattern, names, types } = parse_route_id(route.id);
|
|
154
|
+
|
|
155
|
+
if (route.type === 'page') {
|
|
156
|
+
return {
|
|
157
|
+
type: 'page',
|
|
158
|
+
id: route.id,
|
|
159
|
+
pattern,
|
|
160
|
+
names,
|
|
161
|
+
types,
|
|
162
|
+
errors: route.errors.map((id) => (id ? manifest_data.nodes.indexOf(id) : undefined)),
|
|
163
|
+
layouts: route.layouts.map((id) =>
|
|
164
|
+
id ? manifest_data.nodes.indexOf(id) : undefined
|
|
165
|
+
),
|
|
166
|
+
leaf: manifest_data.nodes.indexOf(route.leaf)
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
type: 'endpoint',
|
|
172
|
+
id: route.id,
|
|
173
|
+
pattern,
|
|
174
|
+
names,
|
|
175
|
+
types,
|
|
176
|
+
load: async () => {
|
|
177
|
+
const url = path.resolve(cwd, route.file);
|
|
178
|
+
return await vite.ssrLoadModule(url);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}),
|
|
182
|
+
matchers: async () => {
|
|
183
|
+
/** @type {Record<string, import('types').ParamMatcher>} */
|
|
184
|
+
const matchers = {};
|
|
185
|
+
|
|
186
|
+
for (const key in manifest_data.matchers) {
|
|
187
|
+
const file = manifest_data.matchers[key];
|
|
188
|
+
const url = path.resolve(cwd, file);
|
|
189
|
+
const module = await vite.ssrLoadModule(url);
|
|
190
|
+
|
|
191
|
+
if (module.match) {
|
|
192
|
+
matchers[key] = module.match;
|
|
193
|
+
} else {
|
|
194
|
+
throw new Error(`${file} does not export a \`match\` function`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return matchers;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** @param {Error} error */
|
|
205
|
+
function fix_stack_trace(error) {
|
|
206
|
+
return error.stack ? vite.ssrRewriteStacktrace(error.stack) : error.stack;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
await update_manifest();
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @param {string} event
|
|
213
|
+
* @param {(file: string) => void} cb
|
|
214
|
+
*/
|
|
215
|
+
const watch = (event, cb) => {
|
|
216
|
+
vite.watcher.on(event, (file) => {
|
|
217
|
+
if (file.startsWith(svelte_config.kit.files.routes + path.sep)) {
|
|
218
|
+
cb(file);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
/** @type {NodeJS.Timeout | null } */
|
|
223
|
+
let timeout = null;
|
|
224
|
+
/** @param {() => void} to_run */
|
|
225
|
+
const debounce = (to_run) => {
|
|
226
|
+
timeout && clearTimeout(timeout);
|
|
227
|
+
timeout = setTimeout(() => {
|
|
228
|
+
timeout = null;
|
|
229
|
+
to_run();
|
|
230
|
+
}, 100);
|
|
231
|
+
};
|
|
232
|
+
// Debounce add/unlink events because in case of folder deletion or moves
|
|
233
|
+
// they fire in rapid succession, causing needless invocations.
|
|
234
|
+
watch('add', () => debounce(update_manifest));
|
|
235
|
+
watch('unlink', () => debounce(update_manifest));
|
|
236
|
+
watch('change', (file) => {
|
|
237
|
+
// Don't run for a single file if the whole manifest is about to get updated
|
|
238
|
+
if (!timeout) {
|
|
239
|
+
sync.update(svelte_config, manifest_data, file);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
const assets = svelte_config.kit.paths.assets ? SVELTE_KIT_ASSETS : svelte_config.kit.paths.base;
|
|
244
|
+
const asset_server = sirv(svelte_config.kit.files.assets, {
|
|
245
|
+
dev: true,
|
|
246
|
+
etag: true,
|
|
247
|
+
maxAge: 0,
|
|
248
|
+
extensions: []
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
vite.middlewares.use(async (req, res, next) => {
|
|
252
|
+
try {
|
|
253
|
+
const base = `${vite.config.server.https ? 'https' : 'http'}://${
|
|
254
|
+
req.headers[':authority'] || req.headers.host
|
|
255
|
+
}`;
|
|
256
|
+
|
|
257
|
+
const decoded = decodeURI(new URL(base + req.url).pathname);
|
|
258
|
+
|
|
259
|
+
if (decoded.startsWith(assets)) {
|
|
260
|
+
const pathname = decoded.slice(assets.length);
|
|
261
|
+
const file = svelte_config.kit.files.assets + pathname;
|
|
262
|
+
|
|
263
|
+
if (fs.existsSync(file) && !fs.statSync(file).isDirectory()) {
|
|
264
|
+
if (has_correct_case(file, svelte_config.kit.files.assets)) {
|
|
265
|
+
req.url = encodeURI(pathname); // don't need query/hash
|
|
266
|
+
asset_server(req, res);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
next();
|
|
273
|
+
} catch (e) {
|
|
274
|
+
const error = coalesce_to_error(e);
|
|
275
|
+
res.statusCode = 500;
|
|
276
|
+
res.end(fix_stack_trace(error));
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`);
|
|
281
|
+
const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`);
|
|
282
|
+
|
|
283
|
+
const env = get_env(svelte_config.kit.env, vite_config.mode);
|
|
284
|
+
set_private_env(env.private);
|
|
285
|
+
set_public_env(env.public);
|
|
286
|
+
|
|
287
|
+
return () => {
|
|
288
|
+
const serve_static_middleware = vite.middlewares.stack.find(
|
|
289
|
+
(middleware) =>
|
|
290
|
+
/** @type {function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
remove_static_middlewares(vite.middlewares);
|
|
294
|
+
|
|
295
|
+
vite.middlewares.use(async (req, res) => {
|
|
296
|
+
try {
|
|
297
|
+
const base = `${vite.config.server.https ? 'https' : 'http'}://${
|
|
298
|
+
req.headers[':authority'] || req.headers.host
|
|
299
|
+
}`;
|
|
300
|
+
|
|
301
|
+
const decoded = decodeURI(new URL(base + req.url).pathname);
|
|
302
|
+
const file = posixify(path.resolve(decoded.slice(1)));
|
|
303
|
+
const is_file = fs.existsSync(file) && !fs.statSync(file).isDirectory();
|
|
304
|
+
const allowed =
|
|
305
|
+
!vite_config.server.fs.strict ||
|
|
306
|
+
vite_config.server.fs.allow.some((dir) => file.startsWith(dir));
|
|
307
|
+
|
|
308
|
+
if (is_file && allowed) {
|
|
309
|
+
// @ts-expect-error
|
|
310
|
+
serve_static_middleware.handle(req, res);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (!decoded.startsWith(svelte_config.kit.paths.base)) {
|
|
315
|
+
return not_found(
|
|
316
|
+
res,
|
|
317
|
+
`Not found (did you mean ${svelte_config.kit.paths.base + req.url}?)`
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** @type {Partial<import('types').Hooks>} */
|
|
322
|
+
const user_hooks = resolve_entry(svelte_config.kit.files.hooks)
|
|
323
|
+
? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`)
|
|
324
|
+
: {};
|
|
325
|
+
|
|
326
|
+
const handle = user_hooks.handle || (({ event, resolve }) => resolve(event));
|
|
327
|
+
|
|
328
|
+
/** @type {import('types').Hooks} */
|
|
329
|
+
const hooks = {
|
|
330
|
+
handle,
|
|
331
|
+
handleError:
|
|
332
|
+
user_hooks.handleError ||
|
|
333
|
+
(({ /** @type {Error & { frame?: string }} */ error }) => {
|
|
334
|
+
console.error(colors.bold().red(error.message));
|
|
335
|
+
if (error.frame) {
|
|
336
|
+
console.error(colors.gray(error.frame));
|
|
337
|
+
}
|
|
338
|
+
if (error.stack) {
|
|
339
|
+
console.error(colors.gray(error.stack));
|
|
340
|
+
}
|
|
341
|
+
}),
|
|
342
|
+
externalFetch: user_hooks.externalFetch || fetch
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
if (/** @type {any} */ (hooks).getContext) {
|
|
346
|
+
// TODO remove this for 1.0
|
|
347
|
+
throw new Error(
|
|
348
|
+
'The getContext hook has been removed. See https://kit.svelte.dev/docs/hooks'
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (/** @type {any} */ (hooks).serverFetch) {
|
|
353
|
+
// TODO remove this for 1.0
|
|
354
|
+
throw new Error('The serverFetch hook has been renamed to externalFetch.');
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// TODO the / prefix will probably fail if outDir is outside the cwd (which
|
|
358
|
+
// could be the case in a monorepo setup), but without it these modules
|
|
359
|
+
// can get loaded twice via different URLs, which causes failures. Might
|
|
360
|
+
// require changes to Vite to fix
|
|
361
|
+
const { default: root } = await vite.ssrLoadModule(
|
|
362
|
+
`/${posixify(path.relative(cwd, `${svelte_config.kit.outDir}/generated/root.svelte`))}`
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
const paths = await vite.ssrLoadModule(`${runtime_base}/paths.js`);
|
|
366
|
+
|
|
367
|
+
paths.set_paths({
|
|
368
|
+
base: svelte_config.kit.paths.base,
|
|
369
|
+
assets
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
let request;
|
|
373
|
+
|
|
374
|
+
try {
|
|
375
|
+
request = await getRequest(base, req);
|
|
376
|
+
} catch (/** @type {any} */ err) {
|
|
377
|
+
res.statusCode = err.status || 400;
|
|
378
|
+
return res.end(err.reason || 'Invalid request body');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const template = load_template(cwd, svelte_config);
|
|
382
|
+
|
|
383
|
+
const rendered = await respond(
|
|
384
|
+
request,
|
|
385
|
+
{
|
|
386
|
+
csp: svelte_config.kit.csp,
|
|
387
|
+
dev: true,
|
|
388
|
+
get_stack: (error) => fix_stack_trace(error),
|
|
389
|
+
handle_error: (error, event) => {
|
|
390
|
+
hooks.handleError({
|
|
391
|
+
error: new Proxy(error, {
|
|
392
|
+
get: (target, property) => {
|
|
393
|
+
if (property === 'stack') {
|
|
394
|
+
return fix_stack_trace(error);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return Reflect.get(target, property, target);
|
|
398
|
+
}
|
|
399
|
+
}),
|
|
400
|
+
event,
|
|
401
|
+
|
|
402
|
+
// TODO remove for 1.0
|
|
403
|
+
// @ts-expect-error
|
|
404
|
+
get request() {
|
|
405
|
+
throw new Error(
|
|
406
|
+
'request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details'
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
},
|
|
411
|
+
hooks,
|
|
412
|
+
hydrate: svelte_config.kit.browser.hydrate,
|
|
413
|
+
manifest,
|
|
414
|
+
method_override: svelte_config.kit.methodOverride,
|
|
415
|
+
paths: {
|
|
416
|
+
base: svelte_config.kit.paths.base,
|
|
417
|
+
assets
|
|
418
|
+
},
|
|
419
|
+
prerender: {
|
|
420
|
+
default: svelte_config.kit.prerender.default,
|
|
421
|
+
enabled: svelte_config.kit.prerender.enabled
|
|
422
|
+
},
|
|
423
|
+
public_env: env.public,
|
|
424
|
+
read: (file) => fs.readFileSync(path.join(svelte_config.kit.files.assets, file)),
|
|
425
|
+
root,
|
|
426
|
+
router: svelte_config.kit.browser.router,
|
|
427
|
+
template: ({ head, body, assets, nonce }) => {
|
|
428
|
+
return (
|
|
429
|
+
template
|
|
430
|
+
.replace(/%sveltekit\.assets%/g, assets)
|
|
431
|
+
.replace(/%sveltekit\.nonce%/g, nonce)
|
|
432
|
+
// head and body must be replaced last, in case someone tries to sneak in %sveltekit.assets% etc
|
|
433
|
+
.replace('%sveltekit.head%', () => head)
|
|
434
|
+
.replace('%sveltekit.body%', () => body)
|
|
435
|
+
);
|
|
436
|
+
},
|
|
437
|
+
template_contains_nonce: template.includes('%sveltekit.nonce%'),
|
|
438
|
+
trailing_slash: svelte_config.kit.trailingSlash
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
getClientAddress: () => {
|
|
442
|
+
const { remoteAddress } = req.socket;
|
|
443
|
+
if (remoteAddress) return remoteAddress;
|
|
444
|
+
throw new Error('Could not determine clientAddress');
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
if (rendered.status === 404) {
|
|
450
|
+
// @ts-expect-error
|
|
451
|
+
serve_static_middleware.handle(req, res, () => {
|
|
452
|
+
setResponse(res, rendered);
|
|
453
|
+
});
|
|
454
|
+
} else {
|
|
455
|
+
setResponse(res, rendered);
|
|
456
|
+
}
|
|
457
|
+
} catch (e) {
|
|
458
|
+
const error = coalesce_to_error(e);
|
|
459
|
+
res.statusCode = 500;
|
|
460
|
+
res.end(fix_stack_trace(error));
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** @param {import('http').ServerResponse} res */
|
|
467
|
+
function not_found(res, message = 'Not found') {
|
|
468
|
+
res.statusCode = 404;
|
|
469
|
+
res.end(message);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* @param {import('connect').Server} server
|
|
474
|
+
*/
|
|
475
|
+
function remove_static_middlewares(server) {
|
|
476
|
+
// We don't use viteServePublicMiddleware because of the following issues:
|
|
477
|
+
// https://github.com/vitejs/vite/issues/9260
|
|
478
|
+
// https://github.com/vitejs/vite/issues/9236
|
|
479
|
+
// https://github.com/vitejs/vite/issues/9234
|
|
480
|
+
const static_middlewares = ['viteServePublicMiddleware', 'viteServeStaticMiddleware'];
|
|
481
|
+
for (let i = server.stack.length - 1; i > 0; i--) {
|
|
482
|
+
// @ts-expect-error using internals
|
|
483
|
+
if (static_middlewares.includes(server.stack[i].handle.name)) {
|
|
484
|
+
server.stack.splice(i, 1);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* @param {import('vite').ViteDevServer} vite
|
|
491
|
+
* @param {import('vite').ModuleNode} node
|
|
492
|
+
* @param {Set<import('vite').ModuleNode>} deps
|
|
493
|
+
*/
|
|
494
|
+
async function find_deps(vite, node, deps) {
|
|
495
|
+
// since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous.
|
|
496
|
+
// instead of using `await`, we resolve all branches in parallel.
|
|
497
|
+
/** @type {Promise<void>[]} */
|
|
498
|
+
const branches = [];
|
|
499
|
+
|
|
500
|
+
/** @param {import('vite').ModuleNode} node */
|
|
501
|
+
async function add(node) {
|
|
502
|
+
if (!deps.has(node)) {
|
|
503
|
+
deps.add(node);
|
|
504
|
+
await find_deps(vite, node, deps);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** @param {string} url */
|
|
509
|
+
async function add_by_url(url) {
|
|
510
|
+
const node = await vite.moduleGraph.getModuleByUrl(url);
|
|
511
|
+
|
|
512
|
+
if (node) {
|
|
513
|
+
await add(node);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (node.ssrTransformResult) {
|
|
518
|
+
if (node.ssrTransformResult.deps) {
|
|
519
|
+
node.ssrTransformResult.deps.forEach((url) => branches.push(add_by_url(url)));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (node.ssrTransformResult.dynamicDeps) {
|
|
523
|
+
node.ssrTransformResult.dynamicDeps.forEach((url) => branches.push(add_by_url(url)));
|
|
524
|
+
}
|
|
525
|
+
} else {
|
|
526
|
+
node.importedModules.forEach((node) => branches.push(add(node)));
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
await Promise.all(branches);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Determine if a file is being requested with the correct case,
|
|
534
|
+
* to ensure consistent behaviour between dev and prod and across
|
|
535
|
+
* operating systems. Note that we can't use realpath here,
|
|
536
|
+
* because we don't want to follow symlinks
|
|
537
|
+
* @param {string} file
|
|
538
|
+
* @param {string} assets
|
|
539
|
+
* @returns {boolean}
|
|
540
|
+
*/
|
|
541
|
+
function has_correct_case(file, assets) {
|
|
542
|
+
if (file === assets) return true;
|
|
543
|
+
|
|
544
|
+
const parent = path.dirname(file);
|
|
545
|
+
|
|
546
|
+
if (fs.readdirSync(parent).includes(path.basename(file))) {
|
|
547
|
+
return has_correct_case(parent, assets);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
return false;
|
|
551
|
+
}
|