@sveltejs/adapter-vercel 2.2.0 → 2.3.0
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/files/edge.js +5 -1
- package/index.d.ts +57 -0
- package/index.js +20 -5
- package/package.json +2 -2
package/files/edge.js
CHANGED
|
@@ -8,12 +8,16 @@ const initialized = server.init({
|
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* @param {Request} request
|
|
11
|
+
* @param {import('../index.js').RequestContext} context
|
|
11
12
|
*/
|
|
12
|
-
export default async (request) => {
|
|
13
|
+
export default async (request, context) => {
|
|
13
14
|
await initialized;
|
|
14
15
|
return server.respond(request, {
|
|
15
16
|
getClientAddress() {
|
|
16
17
|
return /** @type {string} */ (request.headers.get('x-forwarded-for'));
|
|
18
|
+
},
|
|
19
|
+
platform: {
|
|
20
|
+
context
|
|
17
21
|
}
|
|
18
22
|
});
|
|
19
23
|
};
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Adapter } from '@sveltejs/kit';
|
|
2
|
+
import './ambient.js';
|
|
2
3
|
|
|
3
4
|
export default function plugin(config?: Config): Adapter;
|
|
4
5
|
|
|
@@ -77,3 +78,59 @@ export interface EdgeConfig {
|
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
export type Config = EdgeConfig | ServerlessConfig;
|
|
81
|
+
|
|
82
|
+
// we copy the RequestContext interface from `@vercel/edge` because that package can't co-exist with `@types/node`.
|
|
83
|
+
// see https://github.com/sveltejs/kit/pull/9280#issuecomment-1452110035
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* An extension to the standard `Request` object that is passed to every Edge Function.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* import type { RequestContext } from '@vercel/edge';
|
|
91
|
+
*
|
|
92
|
+
* export default async function handler(request: Request, ctx: RequestContext): Promise<Response> {
|
|
93
|
+
* // ctx is the RequestContext
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export interface RequestContext {
|
|
98
|
+
/**
|
|
99
|
+
* A method that can be used to keep the function running after a response has been sent.
|
|
100
|
+
* This is useful when you have an async task that you want to keep running even after the
|
|
101
|
+
* response has been sent and the request has ended.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
*
|
|
105
|
+
* <caption>Sending an internal error to an error tracking service</caption>
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* import type { RequestContext } from '@vercel/edge';
|
|
109
|
+
*
|
|
110
|
+
* export async function handleRequest(request: Request, ctx: RequestContext): Promise<Response> {
|
|
111
|
+
* try {
|
|
112
|
+
* return await myFunctionThatReturnsResponse();
|
|
113
|
+
* } catch (e) {
|
|
114
|
+
* ctx.waitUntil((async () => {
|
|
115
|
+
* // report this error to your error tracking service
|
|
116
|
+
* await fetch('https://my-error-tracking-service.com', {
|
|
117
|
+
* method: 'POST',
|
|
118
|
+
* body: JSON.stringify({
|
|
119
|
+
* stack: e.stack,
|
|
120
|
+
* message: e.message,
|
|
121
|
+
* name: e.name,
|
|
122
|
+
* url: request.url,
|
|
123
|
+
* }),
|
|
124
|
+
* });
|
|
125
|
+
* })());
|
|
126
|
+
* return new Response('Internal Server Error', { status: 500 });
|
|
127
|
+
* }
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
waitUntil(
|
|
132
|
+
/**
|
|
133
|
+
* A promise that will be kept alive until it resolves or rejects.
|
|
134
|
+
*/ promise: Promise<unknown>
|
|
135
|
+
): void;
|
|
136
|
+
}
|
package/index.js
CHANGED
|
@@ -6,6 +6,8 @@ import esbuild from 'esbuild';
|
|
|
6
6
|
|
|
7
7
|
const VALID_RUNTIMES = ['edge', 'nodejs16.x', 'nodejs18.x'];
|
|
8
8
|
|
|
9
|
+
const DEFAULT_FUNCTION_NAME = 'fn';
|
|
10
|
+
|
|
9
11
|
const get_default_runtime = () => {
|
|
10
12
|
const major = process.version.slice(1).split('.')[0];
|
|
11
13
|
if (major === '16') return 'nodejs16.x';
|
|
@@ -214,7 +216,7 @@ const plugin = function (defaults = {}) {
|
|
|
214
216
|
group.config.runtime === 'edge' ? generate_edge_function : generate_serverless_function;
|
|
215
217
|
|
|
216
218
|
// generate one function for the group
|
|
217
|
-
const name = singular ?
|
|
219
|
+
const name = singular ? DEFAULT_FUNCTION_NAME : `fn-${group.i}`;
|
|
218
220
|
|
|
219
221
|
await generate_function(
|
|
220
222
|
name,
|
|
@@ -287,12 +289,25 @@ const plugin = function (defaults = {}) {
|
|
|
287
289
|
}
|
|
288
290
|
}
|
|
289
291
|
|
|
290
|
-
if (singular) {
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
|
|
292
|
+
if (!singular) {
|
|
293
|
+
// we need to create a catch-all route so that 404s are handled
|
|
294
|
+
// by SvelteKit rather than Vercel
|
|
295
|
+
|
|
296
|
+
const runtime = defaults.runtime ?? get_default_runtime();
|
|
297
|
+
const generate_function =
|
|
298
|
+
runtime === 'edge' ? generate_edge_function : generate_serverless_function;
|
|
299
|
+
|
|
300
|
+
await generate_function(
|
|
301
|
+
DEFAULT_FUNCTION_NAME,
|
|
302
|
+
/** @type {any} */ ({ runtime, ...defaults }),
|
|
303
|
+
[]
|
|
304
|
+
);
|
|
294
305
|
}
|
|
295
306
|
|
|
307
|
+
// Catch-all route must come at the end, otherwise it will swallow all other routes,
|
|
308
|
+
// including ISR aliases if there is only one function
|
|
309
|
+
static_config.routes.push({ src: '/.*', dest: `/${DEFAULT_FUNCTION_NAME}` });
|
|
310
|
+
|
|
296
311
|
builder.log.minor('Copying assets...');
|
|
297
312
|
|
|
298
313
|
builder.writeClient(dirs.static);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/adapter-vercel",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/sveltejs/kit",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "^16.18.6",
|
|
31
31
|
"typescript": "^4.9.4",
|
|
32
|
-
"@sveltejs/kit": "^1.
|
|
32
|
+
"@sveltejs/kit": "^1.10.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"@sveltejs/kit": "^1.5.0"
|