-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathindex.js
More file actions
280 lines (238 loc) · 8.52 KB
/
Copy pathindex.js
File metadata and controls
280 lines (238 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { createHash } from 'node:crypto';
import * as fs from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
// posix so it matches the module ids Vite reports on every platform
const src = fileURLToPath(new URL('./src', import.meta.url).href).replaceAll('\\', '/');
const handoff = '#@sveltejs/adapter-node';
/** @type {typeof import('./index.js').default} */
export default function (opts = {}) {
const { out = 'build', precompress = true, envPrefix = '' } = opts;
return {
name: '@sveltejs/adapter-node',
async adapt(builder) {
fs.rmSync(out, { force: true, recursive: true });
const base = builder.config.paths.base;
const client_dir = `${out}/client${base}`;
const prerendered_dir = `${out}/prerendered${base}`;
builder.log.minor('Copying assets');
const client_files = builder.writeClient(client_dir);
const prerendered_files = builder.writePrerendered(prerendered_dir);
builder.log.minor(precompress ? 'Compressing and hashing assets' : 'Hashing assets');
const [client_compressed, prerendered_compressed] = precompress
? await Promise.all([builder.compress(client_dir), builder.compress(prerendered_dir)])
: [[], []];
const assets = create_asset_table(
base,
measure_files(client_dir, client_files, client_compressed)
);
const prerendered_assets = create_prerendered_table(
base,
measure_files(prerendered_dir, prerendered_files, prerendered_compressed),
builder.prerendered.paths
);
const server = builder.getServerDirectory();
builder.generateServerInstance(`${server}/server.js`);
if (builder.hasServerInstrumentationFile()) {
builder.instrument({
entrypoint: `${server}/adapter-index.js`,
instrumentation: `${server}/instrumentation.server.js`,
initializer: builder.createInstrumentationInitializer({ outputDirectory: server }),
module: {
exports: ['path', 'host', 'port', 'server']
}
});
}
builder.copy(server, `${out}/server`);
// values only known after the build. `dir` needs the output root
fs.writeFileSync(
`${out}/adapter-node.js`,
[
`import { dirname } from 'node:path';`,
`import { fileURLToPath } from 'node:url';`,
`export { server } from './server/server.js';`,
`export const dir = dirname(fileURLToPath(import.meta.url));`,
`export const base = ${JSON.stringify(base)};`,
`export const app_path = ${JSON.stringify(builder.getAppPath())};`,
`export const origin = ${JSON.stringify(builder.config.paths.origin)};`,
`export const env_prefix = ${JSON.stringify(envPrefix)};`,
`export const mime_types = ${JSON.stringify(builder.mimeTypes)};`,
// JSON.parse of a string loads about twice as fast as an object literal of the same size
`export const assets = JSON.parse(${JSON.stringify(JSON.stringify(assets))});`,
`export const prerendered_assets = JSON.parse(${JSON.stringify(JSON.stringify(prerendered_assets))});`
].join('\n')
);
fs.writeFileSync(`${out}/index.js`, `export * from './server/adapter-index.js';\n`);
fs.writeFileSync(`${out}/handler.js`, `export * from './server/handler.js';\n`);
},
supports: {
read: () => true,
instrumentation: () => true
},
vite: {
plugins: {
post: [
{
name: 'vite-plugin-sveltekit-adapter-node',
apply: 'build',
config() {
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
return {
ssr: {
// Vite doesn't bundle dependencies for SSR by default so we
// tell it to bundle everything and exclude prod dependencies
// in the external option below
noExternal: true
},
environments: {
ssr: {
build: {
rolldownOptions: {
// bundled with the app's server code so shared modules aren't duplicated (#15755)
input: {
'adapter-index': `${src}/index.js`,
'adapter-env': `${src}/env.js`,
handler: `${src}/handler.js`
},
// only production dependencies (and their deep imports) stay external
external: [
handoff,
...Object.keys(pkg.dependencies || {}).map(
(d) => new RegExp(`^${d}(\\/.*)?$`)
)
],
output: {
paths: { [handoff]: '../adapter-node.js' },
// the hand-off path only holds at the output root, so adapter chunks may not nest
chunkFileNames: (chunk) =>
chunk.moduleIds.some((id) => id.startsWith(src))
? 'adapter-node-[name].js'
: 'chunks/[name].js'
}
}
}
}
}
};
}
}
]
}
}
};
}
/**
* Dotfiles are not served, with the customary exception of `.well-known`
* @param {string} file
*/
function is_hidden(file) {
return file.split('/').some((segment) => segment[0] === '.') && !file.startsWith('.well-known/');
}
/**
* Size and content hash from one pass over the file, a buffer at a time
* @param {string} file
* @param {Buffer} buffer
*/
function measure(file, buffer) {
const fd = fs.openSync(file, 'r');
const hash = createHash('sha256');
let size = 0;
try {
let read;
while ((read = fs.readSync(fd, buffer)) > 0) {
hash.update(buffer.subarray(0, read));
size += read;
}
} finally {
fs.closeSync(fd);
}
return { size, etag: hash.digest('base64url') };
}
/**
* Size and content hash of every servable file, plus the sizes of the
* compressed variants where `builder.compress` wrote them.
* Files are read one at a time through a single buffer, so large outputs
* neither exhaust file descriptors nor pile up in memory
* @param {string} root
* @param {string[]} files
* @param {string[]} compressed
* @returns {AssetEntry[]}
*/
function measure_files(root, files, compressed) {
const variants = new Set(compressed);
const buffer = Buffer.allocUnsafe(64 * 1024);
/** @type {AssetEntry[]} */
const entries = [];
for (const file of files) {
if (is_hidden(file)) continue;
const abs = join(root, file);
/** @type {AssetEntry} */
const entry = { file, ...measure(abs, buffer) };
// `builder.compress` writes a `.gz` and a `.br` variant of every file it returns
if (variants.has(file)) {
entry.gz = fs.statSync(`${abs}.gz`).size;
entry.br = fs.statSync(`${abs}.br`).size;
}
entries.push(entry);
}
return entries;
}
/**
* Keys the measured files by URL: the exact pathname, plus the `/foo` and
* `/foo/` forms of `foo.html`/`foo/index.html` files
* @param {string} base
* @param {AssetEntry[]} measured
* @returns {AssetTable}
*/
function create_asset_table(base, measured) {
const entries = measured.map(
(entry) => /** @type {[string, AssetEntry]} */ ([`${base}/${entry.file}`, entry])
);
entries.sort(([a], [b]) => (a < b ? -1 : 1));
const keys = new Set(entries.map(([key]) => key));
/** @type {Array<[string, string]>} */
const aliases = [];
/**
* @param {string} alias
* @param {string} key
*/
function alias(alias, key) {
if (!keys.has(alias)) {
keys.add(alias);
aliases.push([alias, key]);
}
}
// `/foo` and `/foo/` resolve to `foo.html`, or to `foo/index.html` when only that exists.
// `foo.html` sorts first, so it claims the aliases (the resolution order sirv used)
for (const [key, entry] of entries) {
if (!entry.file.endsWith('.html')) continue;
const is_index = entry.file === 'index.html' || entry.file.endsWith('/index.html');
const with_slash = is_index ? key.slice(0, -'index.html'.length) : key.slice(0, -5) + '/';
alias(with_slash, key);
if (with_slash.length > 1) alias(with_slash.slice(0, -1), key);
}
return { entries, aliases };
}
/**
* Keys the measured files by the exact paths kit prerendered, so a lookup
* hit is precisely a prerendered page, asset or redirect and every other
* pathname (including the non-canonical trailing-slash form) misses
* @param {string} base
* @param {AssetEntry[]} measured
* @param {string[]} paths
* @returns {AssetTable}
*/
function create_prerendered_table(base, measured, paths) {
const by_file = new Map(measured.map((entry) => [entry.file, entry]));
/** @type {Array<[string, AssetEntry]>} */
const entries = [];
for (const path of paths) {
// invert `output_filename` in kit's prerenderer
const file = path.slice(base.length + 1) || 'index.html';
const entry =
by_file.get(file) ?? by_file.get(file + (file.endsWith('/') ? 'index.html' : '.html'));
if (entry) entries.push([path, entry]);
}
entries.sort(([a], [b]) => (a < b ? -1 : 1));
return { entries, aliases: [] };
}