@sveltejs/kit 1.0.0-next.31 → 1.0.0-next.310
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/assets/app/env.js +20 -0
- package/assets/app/navigation.js +24 -0
- package/assets/app/paths.js +1 -0
- package/assets/app/stores.js +97 -0
- package/assets/client/singletons.js +13 -0
- package/assets/client/start.js +1655 -0
- package/assets/components/error.svelte +18 -2
- package/assets/env.js +8 -0
- package/assets/paths.js +13 -0
- package/assets/server/index.js +2862 -0
- package/dist/chunks/amp_hook.js +56 -0
- package/dist/chunks/cert.js +28154 -0
- package/dist/chunks/constants.js +663 -0
- package/dist/chunks/filesystem.js +110 -0
- package/dist/chunks/index.js +515 -0
- package/dist/chunks/index2.js +1326 -0
- package/dist/chunks/index3.js +118 -0
- package/dist/chunks/index4.js +185 -0
- package/dist/chunks/index5.js +251 -0
- package/dist/chunks/index6.js +15585 -0
- package/dist/chunks/index7.js +4207 -0
- package/dist/chunks/misc.js +78 -0
- package/dist/chunks/multipart-parser.js +449 -0
- package/dist/chunks/object.js +83 -0
- package/dist/chunks/sync.js +983 -0
- package/dist/chunks/url.js +56 -0
- package/dist/cli.js +1023 -91
- package/dist/hooks.js +28 -0
- package/dist/install-fetch.js +6518 -0
- package/dist/node.js +94 -0
- package/package.json +92 -54
- package/svelte-kit.js +2 -0
- package/types/ambient.d.ts +298 -0
- package/types/index.d.ts +258 -0
- package/types/internal.d.ts +314 -0
- package/types/private.d.ts +269 -0
- package/CHANGELOG.md +0 -344
- package/assets/runtime/app/navigation.js +0 -23
- package/assets/runtime/app/navigation.js.map +0 -1
- package/assets/runtime/app/paths.js +0 -2
- package/assets/runtime/app/paths.js.map +0 -1
- package/assets/runtime/app/stores.js +0 -78
- package/assets/runtime/app/stores.js.map +0 -1
- package/assets/runtime/internal/singletons.js +0 -15
- package/assets/runtime/internal/singletons.js.map +0 -1
- package/assets/runtime/internal/start.js +0 -591
- package/assets/runtime/internal/start.js.map +0 -1
- package/assets/runtime/utils-85ebcc60.js +0 -18
- package/assets/runtime/utils-85ebcc60.js.map +0 -1
- package/dist/api.js +0 -44
- package/dist/api.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/create_app.js +0 -580
- package/dist/create_app.js.map +0 -1
- package/dist/index.js +0 -375
- package/dist/index.js.map +0 -1
- package/dist/index2.js +0 -12205
- package/dist/index2.js.map +0 -1
- package/dist/index3.js +0 -549
- package/dist/index3.js.map +0 -1
- package/dist/index4.js +0 -74
- package/dist/index4.js.map +0 -1
- package/dist/index5.js +0 -468
- package/dist/index5.js.map +0 -1
- package/dist/index6.js +0 -735
- package/dist/index6.js.map +0 -1
- package/dist/renderer.js +0 -2425
- package/dist/renderer.js.map +0 -1
- package/dist/standard.js +0 -103
- package/dist/standard.js.map +0 -1
- package/dist/utils.js +0 -58
- package/dist/utils.js.map +0 -1
- package/svelte-kit +0 -3
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { s, p as parse_route_id } from './misc.js';
|
|
2
|
+
import { a as get_mime_lookup } from '../cli.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {{
|
|
6
|
+
* build_data: import('types').BuildData;
|
|
7
|
+
* relative_path: string;
|
|
8
|
+
* routes: import('types').RouteData[];
|
|
9
|
+
* format?: 'esm' | 'cjs'
|
|
10
|
+
* }} opts
|
|
11
|
+
*/
|
|
12
|
+
function generate_manifest({ build_data, relative_path, routes, format = 'esm' }) {
|
|
13
|
+
/** @typedef {{ index: number, path: string }} LookupEntry */
|
|
14
|
+
/** @type {Map<string, LookupEntry>} */
|
|
15
|
+
const bundled_nodes = new Map();
|
|
16
|
+
|
|
17
|
+
// 0 and 1 are special, they correspond to the root layout and root error nodes
|
|
18
|
+
bundled_nodes.set(build_data.manifest_data.components[0], {
|
|
19
|
+
path: `${relative_path}/nodes/0.js`,
|
|
20
|
+
index: 0
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
bundled_nodes.set(build_data.manifest_data.components[1], {
|
|
24
|
+
path: `${relative_path}/nodes/1.js`,
|
|
25
|
+
index: 1
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
routes.forEach((route) => {
|
|
29
|
+
if (route.type === 'page') {
|
|
30
|
+
[...route.a, ...route.b].forEach((component) => {
|
|
31
|
+
if (component && !bundled_nodes.has(component)) {
|
|
32
|
+
const i = build_data.manifest_data.components.indexOf(component);
|
|
33
|
+
|
|
34
|
+
bundled_nodes.set(component, {
|
|
35
|
+
path: `${relative_path}/nodes/${i}.js`,
|
|
36
|
+
index: bundled_nodes.size
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** @type {(path: string) => string} */
|
|
44
|
+
const load =
|
|
45
|
+
format === 'esm'
|
|
46
|
+
? (path) => `import('${path}')`
|
|
47
|
+
: (path) => `Promise.resolve().then(() => require('${path}'))`;
|
|
48
|
+
|
|
49
|
+
/** @type {(path: string) => string} */
|
|
50
|
+
const loader = (path) => `() => ${load(path)}`;
|
|
51
|
+
|
|
52
|
+
const assets = build_data.manifest_data.assets.map((asset) => asset.file);
|
|
53
|
+
if (build_data.service_worker) {
|
|
54
|
+
assets.push(build_data.service_worker);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** @param {string | undefined} id */
|
|
58
|
+
const get_index = (id) => id && /** @type {LookupEntry} */ (bundled_nodes.get(id)).index;
|
|
59
|
+
|
|
60
|
+
const matchers = new Set();
|
|
61
|
+
|
|
62
|
+
// prettier-ignore
|
|
63
|
+
return `{
|
|
64
|
+
appDir: ${s(build_data.app_dir)},
|
|
65
|
+
assets: new Set(${s(assets)}),
|
|
66
|
+
mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
|
|
67
|
+
_: {
|
|
68
|
+
entry: ${s(build_data.client.entry)},
|
|
69
|
+
nodes: [
|
|
70
|
+
${Array.from(bundled_nodes.values()).map(node => loader(node.path)).join(',\n\t\t\t\t')}
|
|
71
|
+
],
|
|
72
|
+
routes: [
|
|
73
|
+
${routes.map(route => {
|
|
74
|
+
const { pattern, names, types } = parse_route_id(route.id);
|
|
75
|
+
|
|
76
|
+
types.forEach(type => {
|
|
77
|
+
if (type) matchers.add(type);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (route.type === 'page') {
|
|
81
|
+
return `{
|
|
82
|
+
type: 'page',
|
|
83
|
+
id: ${s(route.id)},
|
|
84
|
+
pattern: ${pattern},
|
|
85
|
+
names: ${s(names)},
|
|
86
|
+
types: ${s(types)},
|
|
87
|
+
path: ${route.path ? s(route.path) : null},
|
|
88
|
+
shadow: ${route.shadow ? loader(`${relative_path}/${build_data.server.vite_manifest[route.shadow].file}`) : null},
|
|
89
|
+
a: ${s(route.a.map(get_index))},
|
|
90
|
+
b: ${s(route.b.map(get_index))}
|
|
91
|
+
}`.replace(/^\t\t/gm, '');
|
|
92
|
+
} else {
|
|
93
|
+
if (!build_data.server.vite_manifest[route.file]) {
|
|
94
|
+
// this is necessary in cases where a .css file snuck in —
|
|
95
|
+
// perhaps it would be better to disallow these (and others?)
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return `{
|
|
100
|
+
type: 'endpoint',
|
|
101
|
+
id: ${s(route.id)},
|
|
102
|
+
pattern: ${pattern},
|
|
103
|
+
names: ${s(names)},
|
|
104
|
+
types: ${s(types)},
|
|
105
|
+
load: ${loader(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)}
|
|
106
|
+
}`.replace(/^\t\t/gm, '');
|
|
107
|
+
}
|
|
108
|
+
}).filter(Boolean).join(',\n\t\t\t\t')}
|
|
109
|
+
],
|
|
110
|
+
matchers: async () => {
|
|
111
|
+
${Array.from(matchers).map(type => `const { match: ${type} } = await ${load(`${relative_path}/entries/matchers/${type}.js`)}`).join('\n\t\t\t\t')}
|
|
112
|
+
return { ${Array.from(matchers).join(', ')} };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}`.replace(/^\t/gm, '');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export { generate_manifest as g };
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { $ } from '../cli.js';
|
|
2
|
+
import { r as rimraf, m as mkdirp, c as copy } from './filesystem.js';
|
|
3
|
+
import { g as generate_manifest } from './index3.js';
|
|
4
|
+
import 'sade';
|
|
5
|
+
import 'fs';
|
|
6
|
+
import 'path';
|
|
7
|
+
import 'child_process';
|
|
8
|
+
import 'net';
|
|
9
|
+
import 'url';
|
|
10
|
+
import 'os';
|
|
11
|
+
import './misc.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {{
|
|
15
|
+
* config: import('types').ValidatedConfig;
|
|
16
|
+
* build_data: import('types').BuildData;
|
|
17
|
+
* prerendered: import('types').Prerendered;
|
|
18
|
+
* log: import('types').Logger;
|
|
19
|
+
* }} opts
|
|
20
|
+
* @returns {import('types').Builder}
|
|
21
|
+
*/
|
|
22
|
+
function create_builder({ config, build_data, prerendered, log }) {
|
|
23
|
+
/** @type {Set<string>} */
|
|
24
|
+
const prerendered_paths = new Set(prerendered.paths);
|
|
25
|
+
|
|
26
|
+
/** @param {import('types').RouteData} route */
|
|
27
|
+
// TODO routes should come pre-filtered
|
|
28
|
+
function not_prerendered(route) {
|
|
29
|
+
if (route.type === 'page' && route.path) {
|
|
30
|
+
return !prerendered_paths.has(route.path);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
log,
|
|
38
|
+
rimraf,
|
|
39
|
+
mkdirp,
|
|
40
|
+
copy,
|
|
41
|
+
|
|
42
|
+
config,
|
|
43
|
+
prerendered,
|
|
44
|
+
|
|
45
|
+
createEntries(fn) {
|
|
46
|
+
const { routes } = build_data.manifest_data;
|
|
47
|
+
|
|
48
|
+
/** @type {import('types').RouteDefinition[]} */
|
|
49
|
+
const facades = routes.map((route) => ({
|
|
50
|
+
type: route.type,
|
|
51
|
+
segments: route.id.split('/').map((segment) => ({
|
|
52
|
+
dynamic: segment.includes('['),
|
|
53
|
+
rest: segment.includes('[...'),
|
|
54
|
+
content: segment
|
|
55
|
+
})),
|
|
56
|
+
pattern: route.pattern,
|
|
57
|
+
methods: route.type === 'page' ? ['get'] : build_data.server.methods[route.file]
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
|
|
62
|
+
for (let i = 0; i < routes.length; i += 1) {
|
|
63
|
+
const route = routes[i];
|
|
64
|
+
const { id, filter, complete } = fn(facades[i]);
|
|
65
|
+
|
|
66
|
+
if (seen.has(id)) continue;
|
|
67
|
+
seen.add(id);
|
|
68
|
+
|
|
69
|
+
const group = [route];
|
|
70
|
+
|
|
71
|
+
// figure out which lower priority routes should be considered fallbacks
|
|
72
|
+
for (let j = i + 1; j < routes.length; j += 1) {
|
|
73
|
+
if (filter(facades[j])) {
|
|
74
|
+
group.push(routes[j]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const filtered = new Set(group.filter(not_prerendered));
|
|
79
|
+
|
|
80
|
+
// heuristic: if /foo/[bar] is included, /foo/[bar].json should
|
|
81
|
+
// also be included, since the page likely needs the endpoint
|
|
82
|
+
filtered.forEach((route) => {
|
|
83
|
+
if (route.type === 'page') {
|
|
84
|
+
const endpoint = routes.find((candidate) => candidate.id === route.id + '.json');
|
|
85
|
+
|
|
86
|
+
if (endpoint) {
|
|
87
|
+
filtered.add(endpoint);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (filtered.size > 0) {
|
|
93
|
+
complete({
|
|
94
|
+
generateManifest: ({ relativePath, format }) =>
|
|
95
|
+
generate_manifest({
|
|
96
|
+
build_data,
|
|
97
|
+
relative_path: relativePath,
|
|
98
|
+
routes: Array.from(filtered),
|
|
99
|
+
format
|
|
100
|
+
})
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
generateManifest: ({ relativePath, format }) => {
|
|
107
|
+
return generate_manifest({
|
|
108
|
+
build_data,
|
|
109
|
+
relative_path: relativePath,
|
|
110
|
+
routes: build_data.manifest_data.routes.filter(not_prerendered),
|
|
111
|
+
format
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
getBuildDirectory(name) {
|
|
116
|
+
return `${config.kit.outDir}/${name}`;
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
getClientDirectory() {
|
|
120
|
+
return `${config.kit.outDir}/output/client`;
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
getServerDirectory() {
|
|
124
|
+
return `${config.kit.outDir}/output/server`;
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
getStaticDirectory() {
|
|
128
|
+
return config.kit.files.assets;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
writeClient(dest) {
|
|
132
|
+
return copy(`${config.kit.outDir}/output/client`, dest, {
|
|
133
|
+
filter: (file) => file[0] !== '.'
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
writePrerendered(dest, { fallback } = {}) {
|
|
138
|
+
const source = `${config.kit.outDir}/output/prerendered`;
|
|
139
|
+
const files = [...copy(`${source}/pages`, dest), ...copy(`${source}/dependencies`, dest)];
|
|
140
|
+
|
|
141
|
+
if (fallback) {
|
|
142
|
+
files.push(fallback);
|
|
143
|
+
copy(`${source}/fallback.html`, `${dest}/${fallback}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return files;
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
writeServer(dest) {
|
|
150
|
+
return copy(`${config.kit.outDir}/output/server`, dest, {
|
|
151
|
+
filter: (file) => file[0] !== '.'
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
writeStatic(dest) {
|
|
156
|
+
return copy(config.kit.files.assets, dest);
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
// @ts-expect-error
|
|
160
|
+
async prerender() {
|
|
161
|
+
throw new Error(
|
|
162
|
+
'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param {import('types').ValidatedConfig} config
|
|
170
|
+
* @param {import('types').BuildData} build_data
|
|
171
|
+
* @param {import('types').Prerendered} prerendered
|
|
172
|
+
* @param {{ log: import('types').Logger }} opts
|
|
173
|
+
*/
|
|
174
|
+
async function adapt(config, build_data, prerendered, { log }) {
|
|
175
|
+
const { name, adapt } = config.kit.adapter;
|
|
176
|
+
|
|
177
|
+
console.log($.bold().cyan(`\n> Using ${name}`));
|
|
178
|
+
|
|
179
|
+
const builder = create_builder({ config, build_data, prerendered, log });
|
|
180
|
+
await adapt(builder);
|
|
181
|
+
|
|
182
|
+
log.success('done');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export { adapt };
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import fs__default from 'fs';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import https from 'https';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { S as SVELTE_KIT_ASSETS, s as sirv } from './constants.js';
|
|
6
|
+
import { pathToFileURL } from 'url';
|
|
7
|
+
import { getRequest, setResponse } from '../node.js';
|
|
8
|
+
import { installFetch } from '../install-fetch.js';
|
|
9
|
+
import { n as normalize_path } from './url.js';
|
|
10
|
+
import 'querystring';
|
|
11
|
+
import 'stream';
|
|
12
|
+
import 'node:http';
|
|
13
|
+
import 'node:https';
|
|
14
|
+
import 'node:zlib';
|
|
15
|
+
import 'node:stream';
|
|
16
|
+
import 'node:util';
|
|
17
|
+
import 'node:url';
|
|
18
|
+
import 'net';
|
|
19
|
+
|
|
20
|
+
/** @typedef {import('http').IncomingMessage} Req */
|
|
21
|
+
/** @typedef {import('http').ServerResponse} Res */
|
|
22
|
+
/** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} dir
|
|
26
|
+
* @returns {Handler}
|
|
27
|
+
*/
|
|
28
|
+
const mutable = (dir) =>
|
|
29
|
+
fs__default.existsSync(dir)
|
|
30
|
+
? sirv(dir, {
|
|
31
|
+
etag: true,
|
|
32
|
+
maxAge: 0
|
|
33
|
+
})
|
|
34
|
+
: (req, res, next) => next();
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {{
|
|
38
|
+
* port: number;
|
|
39
|
+
* host?: string;
|
|
40
|
+
* config: import('types').ValidatedConfig;
|
|
41
|
+
* https?: boolean;
|
|
42
|
+
* cwd?: string;
|
|
43
|
+
* }} opts
|
|
44
|
+
*/
|
|
45
|
+
async function preview({ port, host, config, https: use_https = false }) {
|
|
46
|
+
installFetch();
|
|
47
|
+
|
|
48
|
+
const { paths } = config.kit;
|
|
49
|
+
const base = paths.base;
|
|
50
|
+
const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
|
|
51
|
+
|
|
52
|
+
const etag = `"${Date.now()}"`;
|
|
53
|
+
|
|
54
|
+
const index_file = join(config.kit.outDir, 'output/server/index.js');
|
|
55
|
+
const manifest_file = join(config.kit.outDir, 'output/server/manifest.js');
|
|
56
|
+
|
|
57
|
+
/** @type {import('types').ServerModule} */
|
|
58
|
+
const { Server, override } = await import(pathToFileURL(index_file).href);
|
|
59
|
+
const { manifest } = await import(pathToFileURL(manifest_file).href);
|
|
60
|
+
|
|
61
|
+
override({
|
|
62
|
+
paths: { base, assets },
|
|
63
|
+
prerendering: false,
|
|
64
|
+
protocol: use_https ? 'https' : 'http',
|
|
65
|
+
read: (file) => fs__default.readFileSync(join(config.kit.files.assets, file))
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const server = new Server(manifest);
|
|
69
|
+
|
|
70
|
+
const handle = compose([
|
|
71
|
+
// files in `static`
|
|
72
|
+
scoped(assets, mutable(config.kit.files.assets)),
|
|
73
|
+
|
|
74
|
+
// immutable generated client assets
|
|
75
|
+
scoped(
|
|
76
|
+
assets,
|
|
77
|
+
sirv(join(config.kit.outDir, 'output/client'), {
|
|
78
|
+
maxAge: 31536000,
|
|
79
|
+
immutable: true
|
|
80
|
+
})
|
|
81
|
+
),
|
|
82
|
+
|
|
83
|
+
// prerendered dependencies
|
|
84
|
+
scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))),
|
|
85
|
+
|
|
86
|
+
// prerendered pages (we can't just use sirv because we need to
|
|
87
|
+
// preserve the correct trailingSlash behaviour)
|
|
88
|
+
scoped(base, (req, res, next) => {
|
|
89
|
+
let if_none_match_value = req.headers['if-none-match'];
|
|
90
|
+
|
|
91
|
+
if (if_none_match_value?.startsWith('W/"')) {
|
|
92
|
+
if_none_match_value = if_none_match_value.substring(2);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (if_none_match_value === etag) {
|
|
96
|
+
res.statusCode = 304;
|
|
97
|
+
res.end();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const { pathname, search } = new URL(/** @type {string} */ (req.url), 'http://dummy');
|
|
102
|
+
|
|
103
|
+
const normalized = normalize_path(pathname, config.kit.trailingSlash);
|
|
104
|
+
|
|
105
|
+
if (normalized !== pathname) {
|
|
106
|
+
res.writeHead(307, {
|
|
107
|
+
location: base + normalized + search
|
|
108
|
+
});
|
|
109
|
+
res.end();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// only treat this as a page if it doesn't include an extension
|
|
114
|
+
if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
|
|
115
|
+
const file = join(
|
|
116
|
+
config.kit.outDir,
|
|
117
|
+
'output/prerendered/pages' + pathname + (pathname.endsWith('/') ? 'index.html' : '.html')
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
if (fs__default.existsSync(file)) {
|
|
121
|
+
res.writeHead(200, {
|
|
122
|
+
'content-type': 'text/html',
|
|
123
|
+
etag
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
fs__default.createReadStream(file).pipe(res);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
next();
|
|
132
|
+
}),
|
|
133
|
+
|
|
134
|
+
// SSR
|
|
135
|
+
async (req, res) => {
|
|
136
|
+
const protocol = use_https ? 'https' : 'http';
|
|
137
|
+
const host = req.headers['host'];
|
|
138
|
+
|
|
139
|
+
let request;
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
request = await getRequest(`${protocol}://${host}`, req);
|
|
143
|
+
} catch (/** @type {any} */ err) {
|
|
144
|
+
res.statusCode = err.status || 400;
|
|
145
|
+
return res.end(err.reason || 'Invalid request body');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
setResponse(
|
|
149
|
+
res,
|
|
150
|
+
await server.respond(request, {
|
|
151
|
+
getClientAddress: () => {
|
|
152
|
+
const { remoteAddress } = req.socket;
|
|
153
|
+
if (remoteAddress) return remoteAddress;
|
|
154
|
+
throw new Error('Could not determine clientAddress');
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
]);
|
|
160
|
+
|
|
161
|
+
const vite_config = (config.kit.vite && (await config.kit.vite())) || {};
|
|
162
|
+
|
|
163
|
+
const http_server = await get_server(use_https, vite_config, (req, res) => {
|
|
164
|
+
if (req.url == null) {
|
|
165
|
+
throw new Error('Invalid request url');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
handle(req, res);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return new Promise((fulfil) => {
|
|
172
|
+
http_server.listen(port, host || '0.0.0.0', () => {
|
|
173
|
+
fulfil(http_server);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @param {boolean} use_https
|
|
180
|
+
* @param {import('vite').UserConfig} user_config
|
|
181
|
+
* @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler
|
|
182
|
+
* @returns {Promise<import('net').Server>}
|
|
183
|
+
*/
|
|
184
|
+
async function get_server(use_https, user_config, handler) {
|
|
185
|
+
/** @type {https.ServerOptions} */
|
|
186
|
+
const https_options = {};
|
|
187
|
+
|
|
188
|
+
if (use_https) {
|
|
189
|
+
const secure_opts = user_config.server
|
|
190
|
+
? /** @type {import('tls').SecureContextOptions} */ (user_config.server.https)
|
|
191
|
+
: {};
|
|
192
|
+
|
|
193
|
+
if (secure_opts.key && secure_opts.cert) {
|
|
194
|
+
https_options.key = secure_opts.key.toString();
|
|
195
|
+
https_options.cert = secure_opts.cert.toString();
|
|
196
|
+
} else {
|
|
197
|
+
https_options.key = https_options.cert = (await import('./cert.js')).createCertificate();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return use_https
|
|
202
|
+
? https.createServer(/** @type {https.ServerOptions} */ (https_options), handler)
|
|
203
|
+
: http.createServer(handler);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** @param {Handler[]} handlers */
|
|
207
|
+
function compose(handlers) {
|
|
208
|
+
/**
|
|
209
|
+
* @param {Req} req
|
|
210
|
+
* @param {Res} res
|
|
211
|
+
*/
|
|
212
|
+
return (req, res) => {
|
|
213
|
+
/** @param {number} i */
|
|
214
|
+
function next(i) {
|
|
215
|
+
const handler = handlers[i];
|
|
216
|
+
|
|
217
|
+
if (handler) {
|
|
218
|
+
handler(req, res, () => next(i + 1));
|
|
219
|
+
} else {
|
|
220
|
+
res.statusCode = 404;
|
|
221
|
+
res.end('Not found');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
next(0);
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* @param {string} scope
|
|
231
|
+
* @param {Handler} handler
|
|
232
|
+
* @returns {Handler}
|
|
233
|
+
*/
|
|
234
|
+
function scoped(scope, handler) {
|
|
235
|
+
if (scope === '') return handler;
|
|
236
|
+
|
|
237
|
+
return (req, res, next) => {
|
|
238
|
+
if (req.url?.startsWith(scope)) {
|
|
239
|
+
const original_url = req.url;
|
|
240
|
+
req.url = req.url.slice(scope.length);
|
|
241
|
+
handler(req, res, () => {
|
|
242
|
+
req.url = original_url;
|
|
243
|
+
next();
|
|
244
|
+
});
|
|
245
|
+
} else {
|
|
246
|
+
next();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export { preview };
|