forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
727 lines (596 loc) · 20.8 KB
/
index.js
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { nodeFileTrace } from '@vercel/nft';
import esbuild from 'esbuild';
import { get_pathname, pattern_to_src } from './utils.js';
import { VERSION } from '@sveltejs/kit';
const name = '@sveltejs/adapter-vercel';
const DEFAULT_FUNCTION_NAME = 'fn';
const get_default_runtime = () => {
const major = process.version.slice(1).split('.')[0];
if (major === '18') return 'nodejs18.x';
if (major === '20') return 'nodejs20.x';
throw new Error(
`Unsupported Node.js version: ${process.version}. Please use Node 18 or Node 20 to build your project, or explicitly specify a runtime in your adapter configuration.`
);
};
// https://vercel.com/docs/functions/edge-functions/edge-runtime#compatible-node.js-modules
const compatible_node_modules = ['async_hooks', 'events', 'buffer', 'assert', 'util'];
/** @type {import('./index.js').default} **/
const plugin = function (defaults = {}) {
if ('edge' in defaults) {
throw new Error("{ edge: true } has been removed in favour of { runtime: 'edge' }");
}
return {
name,
async adapt(builder) {
if (!builder.routes) {
throw new Error(
'@sveltejs/adapter-vercel >=2.x (possibly installed through @sveltejs/adapter-auto) requires @sveltejs/kit version 1.5 or higher. ' +
'Either downgrade the adapter or upgrade @sveltejs/kit'
);
}
const dir = '.vercel/output';
const tmp = builder.getBuildDirectory('vercel-tmp');
builder.rimraf(dir);
builder.rimraf(tmp);
if (fs.existsSync('vercel.json')) {
const vercel_file = fs.readFileSync('vercel.json', 'utf-8');
const vercel_config = JSON.parse(vercel_file);
validate_vercel_json(builder, vercel_config);
}
const files = fileURLToPath(new URL('./files', import.meta.url).href);
const dirs = {
static: `${dir}/static${builder.config.kit.paths.base}`,
functions: `${dir}/functions`
};
builder.log.minor('Copying assets...');
builder.writeClient(dirs.static);
builder.writePrerendered(dirs.static);
const static_config = static_vercel_config(builder, defaults, dirs.static);
builder.log.minor('Generating serverless function...');
/**
* @param {string} name
* @param {import('./index.js').ServerlessConfig} config
* @param {import('@sveltejs/kit').RouteDefinition<import('./index.js').Config>[]} routes
*/
async function generate_serverless_function(name, config, routes) {
const dir = `${dirs.functions}/${name}.func`;
const relativePath = path.posix.relative(tmp, builder.getServerDirectory());
builder.copy(`${files}/serverless.js`, `${tmp}/index.js`, {
replace: {
SERVER: `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});
write(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({ relativePath, routes })};\n`
);
await create_function_bundle(builder, `${tmp}/index.js`, dir, config);
for (const asset of builder.findServerAssets(routes)) {
// TODO use symlinks, once Build Output API supports doing so
builder.copy(`${builder.getServerDirectory()}/${asset}`, `${dir}/${asset}`);
}
}
/**
* @param {string} name
* @param {import('./index.js').EdgeConfig} config
* @param {import('@sveltejs/kit').RouteDefinition<import('./index.js').EdgeConfig>[]} routes
*/
async function generate_edge_function(name, config, routes) {
const tmp = builder.getBuildDirectory(`vercel-tmp/${name}`);
const relativePath = path.posix.relative(tmp, builder.getServerDirectory());
builder.copy(`${files}/edge.js`, `${tmp}/edge.js`, {
replace: {
SERVER: `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});
write(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({ relativePath, routes })};\n`
);
try {
const result = await esbuild.build({
entryPoints: [`${tmp}/edge.js`],
outfile: `${dirs.functions}/${name}.func/index.js`,
target: 'es2020', // TODO verify what the edge runtime supports
bundle: true,
platform: 'browser',
format: 'esm',
external: [
...compatible_node_modules,
...compatible_node_modules.map((id) => `node:${id}`),
...(config.external || [])
],
sourcemap: 'linked',
banner: { js: 'globalThis.global = globalThis;' },
loader: {
'.wasm': 'copy',
'.woff': 'copy',
'.woff2': 'copy',
'.ttf': 'copy',
'.eot': 'copy',
'.otf': 'copy'
}
});
if (result.warnings.length > 0) {
const formatted = await esbuild.formatMessages(result.warnings, {
kind: 'warning',
color: true
});
console.error(formatted.join('\n'));
}
} catch (err) {
const error = /** @type {import('esbuild').BuildFailure} */ (err);
for (const e of error.errors) {
for (const node of e.notes) {
const match =
/The package "(.+)" wasn't found on the file system but is built into node/.exec(
node.text
);
if (match) {
node.text = `Cannot use "${match[1]}" when deploying to Vercel Edge Functions.`;
}
}
}
const formatted = await esbuild.formatMessages(error.errors, {
kind: 'error',
color: true
});
console.error(formatted.join('\n'));
throw new Error(
`Bundling with esbuild failed with ${error.errors.length} ${
error.errors.length === 1 ? 'error' : 'errors'
}`
);
}
write(
`${dirs.functions}/${name}.func/.vc-config.json`,
JSON.stringify(
{
runtime: config.runtime,
regions: config.regions,
entrypoint: 'index.js',
framework: {
slug: 'sveltekit',
version: VERSION
}
},
null,
'\t'
)
);
}
/** @type {Map<string, { i: number, config: import('./index.js').Config, routes: import('@sveltejs/kit').RouteDefinition<import('./index.js').Config>[] }>} */
const groups = new Map();
/** @type {Map<string, { hash: string, route_id: string }>} */
const conflicts = new Map();
/** @type {Map<string, string>} */
const functions = new Map();
/** @type {Map<import('@sveltejs/kit').RouteDefinition<import('./index.js').Config>, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */
const isr_config = new Map();
/** @type {Set<string>} */
const ignored_isr = new Set();
// group routes by config
for (const route of builder.routes) {
const runtime = route.config?.runtime ?? defaults?.runtime ?? get_default_runtime();
const config = { runtime, ...defaults, ...route.config };
if (is_prerendered(route)) {
if (config.isr) {
ignored_isr.add(route.id);
}
continue;
}
const node_runtime = /nodejs([0-9]+)\.x/.exec(runtime);
if (runtime !== 'edge' && (!node_runtime || parseInt(node_runtime[1]) < 18)) {
throw new Error(
`Invalid runtime '${runtime}' for route ${route.id}. Valid runtimes are 'edge' and 'nodejs18.x' or higher ` +
'(see the Node.js Version section in your Vercel project settings for info on the currently supported versions).'
);
}
if (config.isr) {
const directory = path.relative('.', builder.config.kit.files.routes + route.id);
if (!runtime.startsWith('nodejs')) {
throw new Error(
`${directory}: Routes using \`isr\` must use a Node.js runtime (for example 'nodejs20.x')`
);
}
if (config.isr.allowQuery?.includes('__pathname')) {
throw new Error(
`${directory}: \`__pathname\` is a reserved query parameter for \`isr.allowQuery\``
);
}
isr_config.set(route, {
expiration: config.isr.expiration,
bypassToken: config.isr.bypassToken,
allowQuery: ['__pathname', ...(config.isr.allowQuery ?? [])],
group: isr_config.size + 1,
passQuery: true
});
}
const hash = hash_config(config);
// first, check there are no routes with incompatible configs that will be merged
const pattern = route.pattern.toString();
const existing = conflicts.get(pattern);
if (existing) {
if (existing.hash !== hash) {
throw new Error(
`The ${route.id} and ${existing.route_id} routes must be merged into a single function that matches the ${route.pattern} regex, but they have incompatible configs. You must either rename one of the routes, or make their configs match.`
);
}
} else {
conflicts.set(pattern, { hash, route_id: route.id });
}
// then, create a group for each config
const id = config.split ? `${hash}-${groups.size}` : hash;
let group = groups.get(id);
if (!group) {
group = { i: groups.size, config, routes: [] };
groups.set(id, group);
}
group.routes.push(route);
}
if (ignored_isr.size) {
builder.log.warn(
'\nWarning: The following routes have an ISR config which is ignored because the route is prerendered:'
);
for (const ignored of ignored_isr) {
console.log(` - ${ignored}`);
}
console.log(
'Either remove the "prerender" option from these routes to use ISR, or remove the ISR config.\n'
);
}
const singular = groups.size === 1;
for (const group of groups.values()) {
const generate_function =
group.config.runtime === 'edge' ? generate_edge_function : generate_serverless_function;
// generate one function for the group
const name = singular ? DEFAULT_FUNCTION_NAME : `fn-${group.i}`;
await generate_function(
name,
/** @type {any} */ (group.config),
/** @type {import('@sveltejs/kit').RouteDefinition<any>[]} */ (group.routes)
);
for (const route of group.routes) {
functions.set(route.pattern.toString(), name);
}
}
for (const route of builder.routes) {
if (is_prerendered(route)) continue;
const pattern = route.pattern.toString();
const src = pattern_to_src(pattern);
const name = functions.get(pattern) ?? 'fn-0';
const isr = isr_config.get(route);
if (isr) {
const isr_name = route.id.slice(1) || '__root__'; // should we check that __root__ isn't a route?
const base = `${dirs.functions}/${isr_name}`;
builder.mkdirp(base);
const target = `${dirs.functions}/${name}.func`;
const relative = path.relative(path.dirname(base), target);
// create a symlink to the actual function, but use the
// route name so that we can derive the correct URL
fs.symlinkSync(relative, `${base}.func`);
fs.symlinkSync(`../${relative}`, `${base}/__data.json.func`);
const pathname = get_pathname(route);
const json = JSON.stringify(isr, null, '\t');
write(`${base}.prerender-config.json`, json);
write(`${base}/__data.json.prerender-config.json`, json);
const q = `?__pathname=/${pathname}`;
static_config.routes.push({
src: src + '$',
dest: `/${isr_name}${q}`
});
static_config.routes.push({
src: src + '/__data.json$',
dest: `/${isr_name}/__data.json${q}`
});
} else if (!singular) {
static_config.routes.push({ src: src + '(?:/__data.json)?$', dest: `/${name}` });
}
}
if (!singular) {
// we need to create a catch-all route so that 404s are handled
// by SvelteKit rather than Vercel
const runtime = defaults.runtime ?? get_default_runtime();
const generate_function =
runtime === 'edge' ? generate_edge_function : generate_serverless_function;
await generate_function(
DEFAULT_FUNCTION_NAME,
/** @type {any} */ ({ runtime, ...defaults }),
[]
);
}
// Catch-all route must come at the end, otherwise it will swallow all other routes,
// including ISR aliases if there is only one function
static_config.routes.push({ src: '/.*', dest: `/${DEFAULT_FUNCTION_NAME}` });
builder.log.minor('Writing routes...');
write(`${dir}/config.json`, JSON.stringify(static_config, null, '\t'));
},
supports: {
// reading from the filesystem only works in serverless functions
read: ({ config, route }) => {
const runtime = config.runtime ?? defaults.runtime;
if (runtime === 'edge') {
throw new Error(
`${name}: Cannot use \`read\` from \`$app/server\` in route \`${route.id}\` configured with \`runtime: 'edge'\``
);
}
return true;
}
}
};
};
/** @param {import('./index.js').EdgeConfig & import('./index.js').ServerlessConfig} config */
function hash_config(config) {
return [
config.runtime ?? '',
config.external ?? '',
config.regions ?? '',
config.memory ?? '',
config.maxDuration ?? '',
!!config.isr // need to distinguish ISR from non-ISR functions, because ISR functions can't use streaming mode
].join('/');
}
/**
* @param {string} file
* @param {string} data
*/
function write(file, data) {
try {
fs.mkdirSync(path.dirname(file), { recursive: true });
} catch {
// do nothing
}
fs.writeFileSync(file, data);
}
// This function is duplicated in adapter-static
/**
* @param {import('@sveltejs/kit').Builder} builder
* @param {import('./index.js').Config} config
* @param {string} dir
*/
function static_vercel_config(builder, config, dir) {
/** @type {any[]} */
const prerendered_redirects = [];
/** @type {Record<string, { path: string }>} */
const overrides = {};
/** @type {import('./index.js').ImagesConfig | undefined} */
const images = config.images;
for (let [src, redirect] of builder.prerendered.redirects) {
if (src.replace(/\/$/, '') === redirect.location.replace(/\/$/, '')) {
// ignore the extreme edge case of a `/foo` -> `/foo/` redirect,
// which would only arise if the response was generated by a
// `handle` hook or outside the app altogether (since you
// can't declaratively create both routes)
} else {
// redirect both `/foo` and `/foo/` to `redirect.location`
src = src.replace(/\/?$/, '/?');
}
prerendered_redirects.push({
src,
headers: {
Location: redirect.location
},
status: redirect.status
});
}
for (const [path, page] of builder.prerendered.pages) {
let overrides_path = path.slice(1);
if (path !== '/') {
/** @type {string | undefined} */
let counterpart_route = path + '/';
if (path.endsWith('/')) {
counterpart_route = path.slice(0, -1);
overrides_path = path.slice(1, -1);
}
prerendered_redirects.push(
{ src: path, dest: counterpart_route },
{ src: counterpart_route, status: 308, headers: { Location: path } }
);
}
overrides[page.file] = { path: overrides_path };
}
const routes = [
...prerendered_redirects,
{
src: `/${builder.getAppPath()}/immutable/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
}
];
// https://vercel.com/docs/deployments/skew-protection
if (process.env.VERCEL_SKEW_PROTECTION_ENABLED) {
routes.push({
src: '/.*',
has: [
{
type: 'header',
key: 'Sec-Fetch-Dest',
value: 'document'
}
],
headers: {
'Set-Cookie': `__vdpl=${process.env.VERCEL_DEPLOYMENT_ID}; Path=${builder.config.kit.paths.base}/; SameSite=Strict; Secure; HttpOnly`
},
continue: true
});
// this is a dreadful hack that is necessary until the Vercel Build Output API
// allows you to set multiple cookies for a single route. essentially, since we
// know that the entry file will be requested immediately, we can set the second
// cookie in _that_ response rather than the document response
const base = `${dir}/${builder.config.kit.appDir}/immutable/entry`;
const entry = fs.readdirSync(base).find((file) => file.startsWith('start.'));
if (!entry) {
throw new Error('Could not find entry point');
}
routes.splice(-2, 0, {
src: `/${builder.getAppPath()}/immutable/entry/${entry}`,
headers: {
'Set-Cookie': `__vdpl=; Path=/${builder.getAppPath()}/version.json; SameSite=Strict; Secure; HttpOnly`
},
continue: true
});
}
routes.push({
handle: 'filesystem'
});
return {
version: 3,
routes,
overrides,
images
};
}
/**
* @param {import('@sveltejs/kit').Builder} builder
* @param {string} entry
* @param {string} dir
* @param {import('./index.js').ServerlessConfig} config
*/
async function create_function_bundle(builder, entry, dir, config) {
fs.rmSync(dir, { force: true, recursive: true });
let base = entry;
while (base !== (base = path.dirname(base)));
const traced = await nodeFileTrace([entry], { base });
/** @type {Map<string, string[]>} */
const resolution_failures = new Map();
traced.warnings.forEach((error) => {
// pending https://github.com/vercel/nft/issues/284
if (error.message.startsWith('Failed to resolve dependency node:')) return;
// parse errors are likely not js and can safely be ignored,
// such as this html file in "main" meant for nw instead of node:
// https://github.com/vercel/nft/issues/311
if (error.message.startsWith('Failed to parse')) return;
if (error.message.startsWith('Failed to resolve dependency')) {
const match = /Cannot find module '(.+?)' loaded from (.+)/;
const [, module, importer] = match.exec(error.message) ?? [, error.message, '(unknown)'];
if (!resolution_failures.has(importer)) {
resolution_failures.set(importer, []);
}
/** @type {string[]} */ (resolution_failures.get(importer)).push(module);
} else {
throw error;
}
});
if (resolution_failures.size > 0) {
const cwd = process.cwd();
builder.log.warn(
'Warning: The following modules failed to locate dependencies that may (or may not) be required for your app to work:'
);
for (const [importer, modules] of resolution_failures) {
console.error(` ${path.relative(cwd, importer)}`);
for (const module of modules) {
console.error(` - \u001B[1m\u001B[36m${module}\u001B[39m\u001B[22m`);
}
}
}
const files = Array.from(traced.fileList);
// find common ancestor directory
/** @type {string[]} */
let common_parts = files[0]?.split(path.sep) ?? [];
for (let i = 1; i < files.length; i += 1) {
const file = files[i];
const parts = file.split(path.sep);
for (let j = 0; j < common_parts.length; j += 1) {
if (parts[j] !== common_parts[j]) {
common_parts = common_parts.slice(0, j);
break;
}
}
}
const ancestor = base + common_parts.join(path.sep);
for (const file of traced.fileList) {
const source = base + file;
const dest = path.join(dir, path.relative(ancestor, source));
const stats = fs.statSync(source);
const is_dir = stats.isDirectory();
const realpath = fs.realpathSync(source);
try {
fs.mkdirSync(path.dirname(dest), { recursive: true });
} catch {
// do nothing
}
if (source !== realpath) {
const realdest = path.join(dir, path.relative(ancestor, realpath));
fs.symlinkSync(path.relative(path.dirname(dest), realdest), dest, is_dir ? 'dir' : 'file');
} else if (!is_dir) {
fs.copyFileSync(source, dest);
}
}
write(
`${dir}/.vc-config.json`,
JSON.stringify(
{
runtime: config.runtime,
regions: config.regions,
memory: config.memory,
maxDuration: config.maxDuration,
handler: path.relative(base + ancestor, entry),
launcherType: 'Nodejs',
experimentalResponseStreaming: !config.isr,
framework: {
slug: 'sveltekit',
version: VERSION
}
},
null,
'\t'
)
);
write(`${dir}/package.json`, JSON.stringify({ type: 'module' }));
}
/**
*
* @param {import('@sveltejs/kit').Builder} builder
* @param {any} vercel_config
*/
function validate_vercel_json(builder, vercel_config) {
if (builder.routes.length > 0 && !builder.routes[0].api) {
// bail — we're on an older SvelteKit version that doesn't
// populate `route.api.methods`, so we can't check
// to see if cron paths are valid
return;
}
const crons = /** @type {Array<unknown>} */ (
Array.isArray(vercel_config?.crons) ? vercel_config.crons : []
);
/** For a route to be considered 'valid', it must be an API route with a GET handler */
const valid_routes = builder.routes.filter((route) => route.api.methods.includes('GET'));
/** @type {Array<string>} */
const unmatched_paths = [];
for (const cron of crons) {
if (typeof cron !== 'object' || cron === null || !('path' in cron)) {
continue;
}
const { path } = cron;
if (typeof path !== 'string') {
continue;
}
if (!valid_routes.some((route) => route.pattern.test(path))) {
unmatched_paths.push(path);
}
}
if (unmatched_paths.length) {
builder.log.warn(
'\nWarning: vercel.json defines cron tasks that use paths that do not correspond to an API route with a GET handler (ignore this if the request is handled in your `handle` hook):'
);
for (const path of unmatched_paths) {
console.log(` - ${path}`);
}
console.log('');
}
}
/** @param {import('@sveltejs/kit').RouteDefinition} route */
function is_prerendered(route) {
return (
route.prerender === true ||
(route.prerender === 'auto' && route.segments.every((segment) => !segment.dynamic))
);
}
export default plugin;