@sveltejs/adapter-vercel 6.1.0 → 6.1.1

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/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Adapter } from '@sveltejs/kit';
2
2
  import './ambient.js';
3
+ import { RuntimeConfigKey } from './utils.js';
3
4
 
4
5
  export default function plugin(config?: Config): Adapter;
5
6
 
@@ -8,7 +9,7 @@ export interface ServerlessConfig {
8
9
  * Whether to use [Edge Functions](https://vercel.com/docs/concepts/functions/edge-functions) (`'edge'`) or [Serverless Functions](https://vercel.com/docs/concepts/functions/serverless-functions) (`'nodejs18.x'`, `'nodejs20.x'` etc).
9
10
  * @default Same as the build environment
10
11
  */
11
- runtime?: `nodejs${number}.x` | `experimental_bun1.x`;
12
+ runtime?: Exclude<RuntimeConfigKey, 'edge'>;
12
13
  /**
13
14
  * To which regions to deploy the app. A list of regions.
14
15
  * More info: https://vercel.com/docs/concepts/edge-network/regions
package/index.js CHANGED
@@ -5,7 +5,7 @@ import process from 'node:process';
5
5
  import { fileURLToPath } from 'node:url';
6
6
  import { nodeFileTrace } from '@vercel/nft';
7
7
  import esbuild from 'esbuild';
8
- import { get_pathname, parse_isr_expiration, pattern_to_src } from './utils.js';
8
+ import { get_pathname, parse_isr_expiration, pattern_to_src, resolve_runtime } from './utils.js';
9
9
  import { VERSION } from '@sveltejs/kit';
10
10
 
11
11
  /**
@@ -24,30 +24,6 @@ const INTERNAL = '![-]'; // this name is guaranteed not to conflict with user ro
24
24
 
25
25
  const [kit_major, kit_minor] = VERSION.split('.');
26
26
 
27
- const get_default_runtime = () => {
28
- const major = Number(process.version.slice(1).split('.')[0]);
29
-
30
- // If we're building on Vercel, we know that the version will be fine because Vercel
31
- // provides Node (and Vercel won't provide something it doesn't support).
32
- // Also means we're not on the hook for updating the adapter every time a new Node
33
- // version is added to Vercel.
34
- if (!process.env.VERCEL) {
35
- if (major < 20 || major > 22) {
36
- throw new Error(
37
- `Building locally with unsupported Node.js version: ${process.version}. Please use Node 20 or 22 to build your project, or explicitly specify a runtime in your adapter configuration.`
38
- );
39
- }
40
-
41
- if (major % 2 !== 0) {
42
- throw new Error(
43
- `Unsupported Node.js version: ${process.version}. Please use an even-numbered Node version to build your project, or explicitly specify a runtime in your adapter configuration.`
44
- );
45
- }
46
- }
47
-
48
- return `nodejs${major}.x`;
49
- };
50
-
51
27
  // https://vercel.com/docs/functions/edge-functions/edge-runtime#compatible-node.js-modules
52
28
  const compatible_node_modules = ['async_hooks', 'events', 'buffer', 'assert', 'util'];
53
29
 
@@ -294,12 +270,8 @@ const plugin = function (defaults = {}) {
294
270
 
295
271
  // group routes by config
296
272
  for (const route of builder.routes) {
297
- const runtime = (
298
- route.config?.runtime ??
299
- defaults?.runtime ??
300
- get_default_runtime()
301
- ).replace('experimental_', '');
302
- const config = { runtime, ...defaults, ...route.config };
273
+ const runtime = resolve_runtime(defaults.runtime, route.config.runtime);
274
+ const config = { ...defaults, ...route.config, runtime };
303
275
 
304
276
  if (is_prerendered(route)) {
305
277
  if (config.isr) {
@@ -308,23 +280,10 @@ const plugin = function (defaults = {}) {
308
280
  continue;
309
281
  }
310
282
 
311
- const node_runtime = /nodejs([0-9]+)\.x/.exec(runtime);
312
- const bun_runtime = /^bun/.exec(runtime);
313
- if (
314
- runtime !== 'edge' &&
315
- !bun_runtime &&
316
- (!node_runtime || parseInt(node_runtime[1]) < 20)
317
- ) {
318
- throw new Error(
319
- `Invalid runtime '${runtime}' for route ${route.id}. Valid runtimes are 'edge', 'experimental_bun1.x', 'nodejs20.x' or 'nodejs22.x' ` +
320
- '(see the Node.js Version section in your Vercel project settings for info on the currently supported versions).'
321
- );
322
- }
323
-
324
283
  if (config.isr) {
325
284
  const directory = path.relative('.', builder.config.kit.files.routes + route.id);
326
285
 
327
- if (!runtime.startsWith('nodejs') && !bun_runtime) {
286
+ if (runtime === 'edge') {
328
287
  throw new Error(
329
288
  `${directory}: Routes using \`isr\` must use a Node.js or Bun runtime (for example 'nodejs22.x' or 'experimental_bun1.x')`
330
289
  );
@@ -409,13 +368,13 @@ const plugin = function (defaults = {}) {
409
368
  // we need to create a catch-all route so that 404s are handled
410
369
  // by SvelteKit rather than Vercel
411
370
 
412
- const runtime = (defaults.runtime ?? get_default_runtime()).replace('experimental_', '');
371
+ const runtime = resolve_runtime(defaults.runtime);
413
372
  const generate_function =
414
373
  runtime === 'edge' ? generate_edge_function : generate_serverless_function;
415
374
 
416
375
  await generate_function(
417
376
  `${INTERNAL}/catchall`,
418
- /** @type {any} */ ({ runtime, ...defaults }),
377
+ /** @type {any} */ ({ ...defaults, runtime }),
419
378
  []
420
379
  );
421
380
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/adapter-vercel",
3
- "version": "6.1.0",
3
+ "version": "6.1.1",
4
4
  "description": "A SvelteKit adapter that creates a Vercel app",
5
5
  "keywords": [
6
6
  "adapter",
@@ -42,7 +42,7 @@
42
42
  "@types/node": "^18.19.119",
43
43
  "typescript": "^5.3.3",
44
44
  "vitest": "^3.2.4",
45
- "@sveltejs/kit": "^2.48.1"
45
+ "@sveltejs/kit": "^2.48.3"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "@sveltejs/kit": "^2.4.0"
package/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ import process from 'node:process';
2
+
1
3
  /** @param {import("@sveltejs/kit").RouteDefinition<any>} route */
2
4
  export function get_pathname(route) {
3
5
  let i = 1;
@@ -106,3 +108,47 @@ export function parse_isr_expiration(value, route_id) {
106
108
  }
107
109
  return parsed;
108
110
  }
111
+
112
+ /**
113
+ * @param {string | undefined} default_key
114
+ * @param {string | undefined} [override_key]
115
+ * @returns {RuntimeKey}
116
+ */
117
+ export function resolve_runtime(default_key, override_key) {
118
+ const key = (override_key ?? default_key ?? get_default_runtime()).replace('experimental_', '');
119
+ assert_is_valid_runtime(key);
120
+ return key;
121
+ }
122
+
123
+ /** @returns {RuntimeKey} */
124
+ function get_default_runtime() {
125
+ // TODO may someday need to auto-detect Bun, but this will be complicated because you may want to run your build
126
+ // with Bun but not have your serverless runtime be in Bun. Vercel will likely have to attach something to `globalThis` or similar
127
+ // to tell us what the bun configuration is.
128
+ const major = Number(process.version.slice(1).split('.')[0]);
129
+
130
+ if (major !== 20 && major !== 22) {
131
+ throw new Error(
132
+ `Unsupported Node.js version: ${process.version}. Please use Node 20 or 22 to build your project, or explicitly specify a runtime in your adapter configuration.`
133
+ );
134
+ }
135
+
136
+ return `nodejs${major}.x`;
137
+ }
138
+
139
+ const valid_runtimes = /** @type {const} */ (['nodejs20.x', 'nodejs22.x', 'bun1.x', 'edge']);
140
+
141
+ /**
142
+ * @param {string} key
143
+ * @returns {asserts key is RuntimeKey}
144
+ */
145
+ function assert_is_valid_runtime(key) {
146
+ if (!valid_runtimes.includes(/** @type {RuntimeKey} */ (key))) {
147
+ throw new Error(
148
+ `Unsupported runtime: ${key}. Supported runtimes are: ${valid_runtimes.join(', ')}. See the Node.js Version section in your Vercel project settings for info on the currently supported versions.`
149
+ );
150
+ }
151
+ }
152
+
153
+ /** @typedef {Exclude<RuntimeKey, 'bun1.x'> | 'experimental_bun1.x'} RuntimeConfigKey */
154
+ /** @typedef {typeof valid_runtimes[number]} RuntimeKey */