@vercel/node 2.5.18 → 2.5.19
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/dist/dev-server.js +11 -243
- package/dist/edge-functions/edge-handler-template.js +73 -0
- package/dist/edge-functions/edge-handler.d.ts +4 -0
- package/dist/edge-functions/edge-handler.js +145 -0
- package/dist/{edge-wasm-plugin.d.ts → edge-functions/edge-wasm-plugin.d.ts} +0 -0
- package/dist/{edge-wasm-plugin.js → edge-functions/edge-wasm-plugin.js} +0 -0
- package/dist/index.js +13 -1
- package/dist/serverless-functions/serverless-handler.d.ts +7 -0
- package/dist/serverless-functions/serverless-handler.js +47 -0
- package/dist/utils.js +13 -1
- package/package.json +3 -3
package/dist/dev-server.js
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.fixConfigDev = exports.onDevRequest =
|
3
|
+
exports.fixConfigDev = exports.onDevRequest = void 0;
|
7
4
|
const entrypoint = process.env.VERCEL_DEV_ENTRYPOINT;
|
8
5
|
delete process.env.VERCEL_DEV_ENTRYPOINT;
|
9
6
|
const tsconfig = process.env.VERCEL_DEV_TSCONFIG;
|
@@ -62,30 +59,11 @@ if (!process.env.VERCEL_DEV_IS_ESM) {
|
|
62
59
|
useRequire = true;
|
63
60
|
}
|
64
61
|
const http_1 = require("http");
|
65
|
-
const launcher_js_1 = require("@vercel/node-bridge/launcher.js");
|
66
|
-
const build_utils_1 = require("@vercel/build-utils");
|
67
|
-
const exit_hook_1 = __importDefault(require("exit-hook"));
|
68
|
-
const edge_runtime_1 = require("edge-runtime");
|
69
62
|
const static_config_1 = require("@vercel/static-config");
|
70
63
|
const ts_morph_1 = require("ts-morph");
|
71
|
-
const
|
72
|
-
const
|
73
|
-
const
|
74
|
-
const NODE_VERSION_MAJOR = process.version.match(/^v(\d+)\.\d+/)?.[1];
|
75
|
-
const NODE_VERSION_IDENTIFIER = `node${NODE_VERSION_MAJOR}`;
|
76
|
-
if (!NODE_VERSION_MAJOR) {
|
77
|
-
throw new Error(`Unable to determine current node version: process.version=${process.version}`);
|
78
|
-
}
|
79
|
-
function logError(error) {
|
80
|
-
console.error(error.message);
|
81
|
-
if (error.stack) {
|
82
|
-
// only show the stack trace if debug is enabled
|
83
|
-
// because it points to internals, not user code
|
84
|
-
const errorPrefixLength = 'Error: '.length;
|
85
|
-
const errorMessageLength = errorPrefixLength + error.message.length;
|
86
|
-
build_utils_1.debug(error.stack.substring(errorMessageLength + 1));
|
87
|
-
}
|
88
|
-
}
|
64
|
+
const utils_1 = require("./utils");
|
65
|
+
const edge_handler_1 = require("./edge-functions/edge-handler");
|
66
|
+
const serverless_handler_1 = require("./serverless-functions/serverless-handler");
|
89
67
|
function listen(server, port, host) {
|
90
68
|
return new Promise(resolve => {
|
91
69
|
server.listen(port, host, () => {
|
@@ -93,204 +71,6 @@ function listen(server, port, host) {
|
|
93
71
|
});
|
94
72
|
});
|
95
73
|
}
|
96
|
-
async function createServerlessEventHandler(entrypoint, options) {
|
97
|
-
const launcher = launcher_js_1.getVercelLauncher({
|
98
|
-
entrypointPath: entrypoint,
|
99
|
-
helpersPath: './helpers.js',
|
100
|
-
shouldAddHelpers: options.shouldAddHelpers,
|
101
|
-
useRequire,
|
102
|
-
// not used
|
103
|
-
bridgePath: '',
|
104
|
-
sourcemapSupportPath: '',
|
105
|
-
});
|
106
|
-
const bridge = launcher();
|
107
|
-
return async function (request) {
|
108
|
-
const body = await rawBody(request);
|
109
|
-
const event = {
|
110
|
-
Action: 'Invoke',
|
111
|
-
body: JSON.stringify({
|
112
|
-
method: request.method,
|
113
|
-
path: request.url,
|
114
|
-
headers: request.headers,
|
115
|
-
encoding: 'base64',
|
116
|
-
body: body.toString('base64'),
|
117
|
-
}),
|
118
|
-
};
|
119
|
-
return bridge.launcher(event, {
|
120
|
-
callbackWaitsForEmptyEventLoop: false,
|
121
|
-
});
|
122
|
-
};
|
123
|
-
}
|
124
|
-
async function serializeRequest(message) {
|
125
|
-
const bodyBuffer = await build_utils_1.streamToBuffer(message);
|
126
|
-
const body = bodyBuffer.toString('base64');
|
127
|
-
return JSON.stringify({
|
128
|
-
url: message.url,
|
129
|
-
method: message.method,
|
130
|
-
headers: message.headers,
|
131
|
-
body,
|
132
|
-
});
|
133
|
-
}
|
134
|
-
async function compileUserCode(entrypointPath, entrypointLabel, isMiddleware) {
|
135
|
-
const { wasmAssets, plugin: edgeWasmPlugin } = edge_wasm_plugin_1.createEdgeWasmPlugin();
|
136
|
-
try {
|
137
|
-
const result = await esbuild_1.default.build({
|
138
|
-
// bundling behavior: use globals (like "browser") instead
|
139
|
-
// of "require" statements for core libraries (like "node")
|
140
|
-
platform: 'browser',
|
141
|
-
// target syntax: only use syntax available on the current
|
142
|
-
// version of node
|
143
|
-
target: NODE_VERSION_IDENTIFIER,
|
144
|
-
sourcemap: 'inline',
|
145
|
-
bundle: true,
|
146
|
-
plugins: [edgeWasmPlugin],
|
147
|
-
entryPoints: [entrypointPath],
|
148
|
-
write: false,
|
149
|
-
format: 'cjs',
|
150
|
-
});
|
151
|
-
const compiledFile = result.outputFiles?.[0];
|
152
|
-
if (!compiledFile) {
|
153
|
-
throw new Error(`Compilation of ${entrypointLabel} produced no output files.`);
|
154
|
-
}
|
155
|
-
const userCode = `
|
156
|
-
${compiledFile.text};
|
157
|
-
|
158
|
-
const isMiddleware = ${isMiddleware};
|
159
|
-
|
160
|
-
addEventListener('fetch', async (event) => {
|
161
|
-
try {
|
162
|
-
let serializedRequest = await event.request.text();
|
163
|
-
let requestDetails = JSON.parse(serializedRequest);
|
164
|
-
|
165
|
-
let body;
|
166
|
-
|
167
|
-
if (requestDetails.method !== 'GET' && requestDetails.method !== 'HEAD') {
|
168
|
-
body = Uint8Array.from(atob(requestDetails.body), c => c.charCodeAt(0));
|
169
|
-
}
|
170
|
-
|
171
|
-
let requestUrl = requestDetails.headers['x-forwarded-proto'] + '://' + requestDetails.headers['x-forwarded-host'] + requestDetails.url;
|
172
|
-
|
173
|
-
let request = new Request(requestUrl, {
|
174
|
-
headers: requestDetails.headers,
|
175
|
-
method: requestDetails.method,
|
176
|
-
body: body
|
177
|
-
});
|
178
|
-
|
179
|
-
event.request = request;
|
180
|
-
|
181
|
-
let edgeHandler = module.exports.default;
|
182
|
-
if (!edgeHandler) {
|
183
|
-
throw new Error('No default export was found. Add a default export to handle requests. Learn more: https://vercel.link/creating-edge-middleware');
|
184
|
-
}
|
185
|
-
|
186
|
-
let response = await edgeHandler(event.request, event);
|
187
|
-
|
188
|
-
if (!response) {
|
189
|
-
if (isMiddleware) {
|
190
|
-
// allow empty responses to pass through
|
191
|
-
response = new Response(null, {
|
192
|
-
headers: {
|
193
|
-
'x-middleware-next': '1',
|
194
|
-
},
|
195
|
-
});
|
196
|
-
} else {
|
197
|
-
throw new Error('Edge Function "${entrypointLabel}" did not return a response.');
|
198
|
-
}
|
199
|
-
}
|
200
|
-
|
201
|
-
return event.respondWith(response);
|
202
|
-
} catch (error) {
|
203
|
-
// we can't easily show a meaningful stack trace
|
204
|
-
// so, stick to just the error message for now
|
205
|
-
const msg = error.cause
|
206
|
-
? (error.message + ': ' + (error.cause.message || error.cause))
|
207
|
-
: error.message;
|
208
|
-
event.respondWith(new Response(msg, {
|
209
|
-
status: 500,
|
210
|
-
headers: {
|
211
|
-
'x-vercel-failed': 'edge-wrapper'
|
212
|
-
}
|
213
|
-
}));
|
214
|
-
}
|
215
|
-
})`;
|
216
|
-
return { userCode, wasmAssets };
|
217
|
-
}
|
218
|
-
catch (error) {
|
219
|
-
// We can't easily show a meaningful stack trace from ncc -> edge-runtime.
|
220
|
-
// So, stick with just the message for now.
|
221
|
-
console.error(`Failed to compile user code for edge runtime.`);
|
222
|
-
logError(error);
|
223
|
-
return undefined;
|
224
|
-
}
|
225
|
-
}
|
226
|
-
async function createEdgeRuntime(params) {
|
227
|
-
try {
|
228
|
-
if (!params) {
|
229
|
-
return undefined;
|
230
|
-
}
|
231
|
-
const wasmBindings = await params.wasmAssets.getContext();
|
232
|
-
const edgeRuntime = new edge_runtime_1.EdgeRuntime({
|
233
|
-
initialCode: params.userCode,
|
234
|
-
extend: (context) => {
|
235
|
-
Object.assign(context, {
|
236
|
-
// This is required for esbuild wrapping logic to resolve
|
237
|
-
module: {},
|
238
|
-
// This is required for environment variable access.
|
239
|
-
// In production, env var access is provided by static analysis
|
240
|
-
// so that only the used values are available.
|
241
|
-
process: {
|
242
|
-
env: process.env,
|
243
|
-
},
|
244
|
-
// These are the global bindings for WebAssembly module
|
245
|
-
...wasmBindings,
|
246
|
-
});
|
247
|
-
return context;
|
248
|
-
},
|
249
|
-
});
|
250
|
-
const server = await edge_runtime_1.runServer({ runtime: edgeRuntime });
|
251
|
-
exit_hook_1.default(server.close);
|
252
|
-
return server;
|
253
|
-
}
|
254
|
-
catch (error) {
|
255
|
-
// We can't easily show a meaningful stack trace from ncc -> edge-runtime.
|
256
|
-
// So, stick with just the message for now.
|
257
|
-
console.error('Failed to instantiate edge runtime.');
|
258
|
-
logError(error);
|
259
|
-
return undefined;
|
260
|
-
}
|
261
|
-
}
|
262
|
-
async function createEdgeEventHandler(entrypointPath, entrypointLabel, isMiddleware) {
|
263
|
-
const userCode = await compileUserCode(entrypointPath, entrypointLabel, isMiddleware);
|
264
|
-
const server = await createEdgeRuntime(userCode);
|
265
|
-
return async function (request) {
|
266
|
-
if (!server) {
|
267
|
-
// this error state is already logged, but we have to wait until here to exit the process
|
268
|
-
// this matches the serverless function bridge launcher's behavior when
|
269
|
-
// an error is thrown in the function
|
270
|
-
process.exit(1);
|
271
|
-
}
|
272
|
-
const response = await node_fetch_1.default(server.url, {
|
273
|
-
redirect: 'manual',
|
274
|
-
method: 'post',
|
275
|
-
body: await serializeRequest(request),
|
276
|
-
});
|
277
|
-
const body = await response.text();
|
278
|
-
const isUserError = response.headers.get('x-vercel-failed') === 'edge-wrapper';
|
279
|
-
if (isUserError && response.status >= 500) {
|
280
|
-
// this error was "unhandled" from the user code's perspective
|
281
|
-
console.log(`Unhandled rejection: ${body}`);
|
282
|
-
// this matches the serverless function bridge launcher's behavior when
|
283
|
-
// an error is thrown in the function
|
284
|
-
process.exit(1);
|
285
|
-
}
|
286
|
-
return {
|
287
|
-
statusCode: response.status,
|
288
|
-
headers: response.headers.raw(),
|
289
|
-
body,
|
290
|
-
encoding: 'utf8',
|
291
|
-
};
|
292
|
-
};
|
293
|
-
}
|
294
74
|
const validRuntimes = ['experimental-edge'];
|
295
75
|
function parseRuntime(entrypoint, entryPointPath) {
|
296
76
|
const project = new ts_morph_1.Project();
|
@@ -308,9 +88,12 @@ async function createEventHandler(entrypoint, config, options) {
|
|
308
88
|
// an Edge Function, otherwise needs to be opted-in via
|
309
89
|
// `export const config = { runtime: 'experimental-edge' }`
|
310
90
|
if (config.middleware === true || runtime === 'experimental-edge') {
|
311
|
-
return createEdgeEventHandler(entrypointPath, entrypoint, config.middleware || false);
|
91
|
+
return edge_handler_1.createEdgeEventHandler(entrypointPath, entrypoint, config.middleware || false);
|
312
92
|
}
|
313
|
-
return createServerlessEventHandler(entrypointPath,
|
93
|
+
return serverless_handler_1.createServerlessEventHandler(entrypointPath, {
|
94
|
+
shouldAddHelpers: options.shouldAddHelpers,
|
95
|
+
useRequire,
|
96
|
+
});
|
314
97
|
}
|
315
98
|
let handleEvent;
|
316
99
|
let handlerEventError;
|
@@ -328,7 +111,7 @@ async function main() {
|
|
328
111
|
});
|
329
112
|
}
|
330
113
|
catch (error) {
|
331
|
-
logError(error);
|
114
|
+
utils_1.logError(error);
|
332
115
|
handlerEventError = error;
|
333
116
|
}
|
334
117
|
const address = proxyServer.address();
|
@@ -339,21 +122,6 @@ async function main() {
|
|
339
122
|
console.log('Dev server listening:', address);
|
340
123
|
}
|
341
124
|
}
|
342
|
-
function rawBody(readable) {
|
343
|
-
return new Promise((resolve, reject) => {
|
344
|
-
let bytes = 0;
|
345
|
-
const chunks = [];
|
346
|
-
readable.on('error', reject);
|
347
|
-
readable.on('data', chunk => {
|
348
|
-
chunks.push(chunk);
|
349
|
-
bytes += chunk.length;
|
350
|
-
});
|
351
|
-
readable.on('end', () => {
|
352
|
-
resolve(Buffer.concat(chunks, bytes));
|
353
|
-
});
|
354
|
-
});
|
355
|
-
}
|
356
|
-
exports.rawBody = rawBody;
|
357
125
|
async function onDevRequest(req, res) {
|
358
126
|
if (handlerEventError) {
|
359
127
|
// this error state is already logged, but we have to wait until here to exit the process
|
@@ -396,6 +164,6 @@ function fixConfigDev(config) {
|
|
396
164
|
}
|
397
165
|
exports.fixConfigDev = fixConfigDev;
|
398
166
|
main().catch(err => {
|
399
|
-
logError(err);
|
167
|
+
utils_1.logError(err);
|
400
168
|
process.exit(1);
|
401
169
|
});
|
@@ -0,0 +1,73 @@
|
|
1
|
+
// provided by the edge runtime:
|
2
|
+
/* global addEventListener Request Response atob */
|
3
|
+
|
4
|
+
// provided by our edge handler logic:
|
5
|
+
/* global IS_MIDDLEWARE ENTRYPOINT_LABEL */
|
6
|
+
|
7
|
+
function buildUrl(requestDetails) {
|
8
|
+
let proto = requestDetails.headers['x-forwarded-proto'];
|
9
|
+
let host = requestDetails.headers['x-forwarded-host'];
|
10
|
+
let path = requestDetails.url;
|
11
|
+
return `${proto}://${host}${path}`;
|
12
|
+
}
|
13
|
+
|
14
|
+
addEventListener('fetch', async event => {
|
15
|
+
try {
|
16
|
+
let serializedRequest = await event.request.text();
|
17
|
+
let requestDetails = JSON.parse(serializedRequest);
|
18
|
+
|
19
|
+
let body;
|
20
|
+
|
21
|
+
if (requestDetails.method !== 'GET' && requestDetails.method !== 'HEAD') {
|
22
|
+
body = Uint8Array.from(atob(requestDetails.body), c => c.charCodeAt(0));
|
23
|
+
}
|
24
|
+
|
25
|
+
let request = new Request(buildUrl(requestDetails), {
|
26
|
+
headers: requestDetails.headers,
|
27
|
+
method: requestDetails.method,
|
28
|
+
body: body,
|
29
|
+
});
|
30
|
+
|
31
|
+
event.request = request;
|
32
|
+
|
33
|
+
let edgeHandler = module.exports.default;
|
34
|
+
if (!edgeHandler) {
|
35
|
+
throw new Error(
|
36
|
+
'No default export was found. Add a default export to handle requests. Learn more: https://vercel.link/creating-edge-middleware'
|
37
|
+
);
|
38
|
+
}
|
39
|
+
|
40
|
+
let response = await edgeHandler(event.request, event);
|
41
|
+
|
42
|
+
if (!response) {
|
43
|
+
if (IS_MIDDLEWARE) {
|
44
|
+
// allow empty responses to pass through
|
45
|
+
response = new Response(null, {
|
46
|
+
headers: {
|
47
|
+
'x-middleware-next': '1',
|
48
|
+
},
|
49
|
+
});
|
50
|
+
} else {
|
51
|
+
throw new Error(
|
52
|
+
`Edge Function "${ENTRYPOINT_LABEL}" did not return a response.`
|
53
|
+
);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
return event.respondWith(response);
|
58
|
+
} catch (error) {
|
59
|
+
// we can't easily show a meaningful stack trace
|
60
|
+
// so, stick to just the error message for now
|
61
|
+
const msg = error.cause
|
62
|
+
? error.message + ': ' + (error.cause.message || error.cause)
|
63
|
+
: error.message;
|
64
|
+
event.respondWith(
|
65
|
+
new Response(msg, {
|
66
|
+
status: 500,
|
67
|
+
headers: {
|
68
|
+
'x-vercel-failed': 'edge-wrapper',
|
69
|
+
},
|
70
|
+
})
|
71
|
+
);
|
72
|
+
}
|
73
|
+
});
|
@@ -0,0 +1,4 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { IncomingMessage } from 'http';
|
3
|
+
import { VercelProxyResponse } from '@vercel/node-bridge/types';
|
4
|
+
export declare function createEdgeEventHandler(entrypointPath: string, entrypointLabel: string, isMiddleware: boolean): Promise<(request: IncomingMessage) => Promise<VercelProxyResponse>>;
|
@@ -0,0 +1,145 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.createEdgeEventHandler = void 0;
|
7
|
+
const build_utils_1 = require("@vercel/build-utils");
|
8
|
+
const exit_hook_1 = __importDefault(require("exit-hook"));
|
9
|
+
const edge_runtime_1 = require("edge-runtime");
|
10
|
+
const esbuild_1 = __importDefault(require("esbuild"));
|
11
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
12
|
+
const edge_wasm_plugin_1 = require("./edge-wasm-plugin");
|
13
|
+
const utils_1 = require("../utils");
|
14
|
+
const fs_1 = require("fs");
|
15
|
+
const NODE_VERSION_MAJOR = process.version.match(/^v(\d+)\.\d+/)?.[1];
|
16
|
+
const NODE_VERSION_IDENTIFIER = `node${NODE_VERSION_MAJOR}`;
|
17
|
+
if (!NODE_VERSION_MAJOR) {
|
18
|
+
throw new Error(`Unable to determine current node version: process.version=${process.version}`);
|
19
|
+
}
|
20
|
+
const edgeHandlerTemplate = fs_1.readFileSync(`${__dirname}/edge-handler-template.js`);
|
21
|
+
async function serializeRequest(message) {
|
22
|
+
const bodyBuffer = await build_utils_1.streamToBuffer(message);
|
23
|
+
const body = bodyBuffer.toString('base64');
|
24
|
+
return JSON.stringify({
|
25
|
+
url: message.url,
|
26
|
+
method: message.method,
|
27
|
+
headers: message.headers,
|
28
|
+
body,
|
29
|
+
});
|
30
|
+
}
|
31
|
+
async function compileUserCode(entrypointPath, entrypointLabel, isMiddleware) {
|
32
|
+
const { wasmAssets, plugin: edgeWasmPlugin } = edge_wasm_plugin_1.createEdgeWasmPlugin();
|
33
|
+
try {
|
34
|
+
const result = await esbuild_1.default.build({
|
35
|
+
// bundling behavior: use globals (like "browser") instead
|
36
|
+
// of "require" statements for core libraries (like "node")
|
37
|
+
platform: 'browser',
|
38
|
+
// target syntax: only use syntax available on the current
|
39
|
+
// version of node
|
40
|
+
target: NODE_VERSION_IDENTIFIER,
|
41
|
+
sourcemap: 'inline',
|
42
|
+
legalComments: 'none',
|
43
|
+
bundle: true,
|
44
|
+
plugins: [edgeWasmPlugin],
|
45
|
+
entryPoints: [entrypointPath],
|
46
|
+
write: false,
|
47
|
+
format: 'cjs',
|
48
|
+
});
|
49
|
+
const compiledFile = result.outputFiles?.[0];
|
50
|
+
if (!compiledFile) {
|
51
|
+
throw new Error(`Compilation of ${entrypointLabel} produced no output files.`);
|
52
|
+
}
|
53
|
+
const userCode = `
|
54
|
+
// strict mode
|
55
|
+
"use strict";var regeneratorRuntime;
|
56
|
+
|
57
|
+
// user code
|
58
|
+
${compiledFile.text};
|
59
|
+
|
60
|
+
// request metadata
|
61
|
+
const IS_MIDDLEWARE = ${isMiddleware};
|
62
|
+
const ENTRYPOINT_LABEL = '${entrypointLabel}';
|
63
|
+
|
64
|
+
// edge handler
|
65
|
+
${edgeHandlerTemplate}
|
66
|
+
`;
|
67
|
+
return { userCode, wasmAssets };
|
68
|
+
}
|
69
|
+
catch (error) {
|
70
|
+
// We can't easily show a meaningful stack trace from ncc -> edge-runtime.
|
71
|
+
// So, stick with just the message for now.
|
72
|
+
console.error(`Failed to compile user code for edge runtime.`);
|
73
|
+
utils_1.logError(error);
|
74
|
+
return undefined;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
async function createEdgeRuntime(params) {
|
78
|
+
try {
|
79
|
+
if (!params) {
|
80
|
+
return undefined;
|
81
|
+
}
|
82
|
+
const wasmBindings = await params.wasmAssets.getContext();
|
83
|
+
const edgeRuntime = new edge_runtime_1.EdgeRuntime({
|
84
|
+
initialCode: params.userCode,
|
85
|
+
extend: (context) => {
|
86
|
+
Object.assign(context, {
|
87
|
+
// This is required for esbuild wrapping logic to resolve
|
88
|
+
module: {},
|
89
|
+
// This is required for environment variable access.
|
90
|
+
// In production, env var access is provided by static analysis
|
91
|
+
// so that only the used values are available.
|
92
|
+
process: {
|
93
|
+
env: process.env,
|
94
|
+
},
|
95
|
+
// These are the global bindings for WebAssembly module
|
96
|
+
...wasmBindings,
|
97
|
+
});
|
98
|
+
return context;
|
99
|
+
},
|
100
|
+
});
|
101
|
+
const server = await edge_runtime_1.runServer({ runtime: edgeRuntime });
|
102
|
+
exit_hook_1.default(server.close);
|
103
|
+
return server;
|
104
|
+
}
|
105
|
+
catch (error) {
|
106
|
+
// We can't easily show a meaningful stack trace from ncc -> edge-runtime.
|
107
|
+
// So, stick with just the message for now.
|
108
|
+
console.error('Failed to instantiate edge runtime.');
|
109
|
+
utils_1.logError(error);
|
110
|
+
return undefined;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
async function createEdgeEventHandler(entrypointPath, entrypointLabel, isMiddleware) {
|
114
|
+
const userCode = await compileUserCode(entrypointPath, entrypointLabel, isMiddleware);
|
115
|
+
const server = await createEdgeRuntime(userCode);
|
116
|
+
return async function (request) {
|
117
|
+
if (!server) {
|
118
|
+
// this error state is already logged, but we have to wait until here to exit the process
|
119
|
+
// this matches the serverless function bridge launcher's behavior when
|
120
|
+
// an error is thrown in the function
|
121
|
+
process.exit(1);
|
122
|
+
}
|
123
|
+
const response = await node_fetch_1.default(server.url, {
|
124
|
+
redirect: 'manual',
|
125
|
+
method: 'post',
|
126
|
+
body: await serializeRequest(request),
|
127
|
+
});
|
128
|
+
const body = await response.text();
|
129
|
+
const isUserError = response.headers.get('x-vercel-failed') === 'edge-wrapper';
|
130
|
+
if (isUserError && response.status >= 500) {
|
131
|
+
// this error was "unhandled" from the user code's perspective
|
132
|
+
console.log(`Unhandled rejection: ${body}`);
|
133
|
+
// this matches the serverless function bridge launcher's behavior when
|
134
|
+
// an error is thrown in the function
|
135
|
+
process.exit(1);
|
136
|
+
}
|
137
|
+
return {
|
138
|
+
statusCode: response.status,
|
139
|
+
headers: response.headers.raw(),
|
140
|
+
body,
|
141
|
+
encoding: 'utf8',
|
142
|
+
};
|
143
|
+
};
|
144
|
+
}
|
145
|
+
exports.createEdgeEventHandler = createEdgeEventHandler;
|
File without changes
|
File without changes
|
package/dist/index.js
CHANGED
@@ -305529,9 +305529,10 @@ function filterDiagnostics(diagnostics, ignore) {
|
|
305529
305529
|
"use strict";
|
305530
305530
|
|
305531
305531
|
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
305532
|
-
exports.entrypointToOutputPath = exports.getRegExpFromMatchers = void 0;
|
305532
|
+
exports.logError = exports.entrypointToOutputPath = exports.getRegExpFromMatchers = void 0;
|
305533
305533
|
const path_1 = __webpack_require__(85622);
|
305534
305534
|
const path_to_regexp_1 = __webpack_require__(91786);
|
305535
|
+
const build_utils_1 = __webpack_require__(63445);
|
305535
305536
|
function getRegExpFromMatchers(matcherOrMatchers) {
|
305536
305537
|
if (!matcherOrMatchers) {
|
305537
305538
|
return '^/.*$';
|
@@ -305573,6 +305574,17 @@ function entrypointToOutputPath(entrypoint, zeroConfig) {
|
|
305573
305574
|
return entrypoint;
|
305574
305575
|
}
|
305575
305576
|
exports.entrypointToOutputPath = entrypointToOutputPath;
|
305577
|
+
function logError(error) {
|
305578
|
+
console.error(error.message);
|
305579
|
+
if (error.stack) {
|
305580
|
+
// only show the stack trace if debug is enabled
|
305581
|
+
// because it points to internals, not user code
|
305582
|
+
const errorPrefixLength = 'Error: '.length;
|
305583
|
+
const errorMessageLength = errorPrefixLength + error.message.length;
|
305584
|
+
build_utils_1.debug(error.stack.substring(errorMessageLength + 1));
|
305585
|
+
}
|
305586
|
+
}
|
305587
|
+
exports.logError = logError;
|
305576
305588
|
|
305577
305589
|
|
305578
305590
|
/***/ }),
|
@@ -0,0 +1,7 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { IncomingMessage } from 'http';
|
3
|
+
import { VercelProxyResponse } from '@vercel/node-bridge/types';
|
4
|
+
export declare function createServerlessEventHandler(entrypoint: string, options: {
|
5
|
+
shouldAddHelpers: boolean;
|
6
|
+
useRequire: boolean;
|
7
|
+
}): Promise<(request: IncomingMessage) => Promise<VercelProxyResponse>>;
|
@@ -0,0 +1,47 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.createServerlessEventHandler = void 0;
|
4
|
+
const launcher_js_1 = require("@vercel/node-bridge/launcher.js");
|
5
|
+
function rawBody(readable) {
|
6
|
+
return new Promise((resolve, reject) => {
|
7
|
+
let bytes = 0;
|
8
|
+
const chunks = [];
|
9
|
+
readable.on('error', reject);
|
10
|
+
readable.on('data', chunk => {
|
11
|
+
chunks.push(chunk);
|
12
|
+
bytes += chunk.length;
|
13
|
+
});
|
14
|
+
readable.on('end', () => {
|
15
|
+
resolve(Buffer.concat(chunks, bytes));
|
16
|
+
});
|
17
|
+
});
|
18
|
+
}
|
19
|
+
async function createServerlessEventHandler(entrypoint, options) {
|
20
|
+
const launcher = launcher_js_1.getVercelLauncher({
|
21
|
+
entrypointPath: entrypoint,
|
22
|
+
helpersPath: './helpers.js',
|
23
|
+
shouldAddHelpers: options.shouldAddHelpers,
|
24
|
+
useRequire: options.useRequire,
|
25
|
+
// not used
|
26
|
+
bridgePath: '',
|
27
|
+
sourcemapSupportPath: '',
|
28
|
+
});
|
29
|
+
const bridge = launcher();
|
30
|
+
return async function (request) {
|
31
|
+
const body = await rawBody(request);
|
32
|
+
const event = {
|
33
|
+
Action: 'Invoke',
|
34
|
+
body: JSON.stringify({
|
35
|
+
method: request.method,
|
36
|
+
path: request.url,
|
37
|
+
headers: request.headers,
|
38
|
+
encoding: 'base64',
|
39
|
+
body: body.toString('base64'),
|
40
|
+
}),
|
41
|
+
};
|
42
|
+
return bridge.launcher(event, {
|
43
|
+
callbackWaitsForEmptyEventLoop: false,
|
44
|
+
});
|
45
|
+
};
|
46
|
+
}
|
47
|
+
exports.createServerlessEventHandler = createServerlessEventHandler;
|
package/dist/utils.js
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.entrypointToOutputPath = exports.getRegExpFromMatchers = void 0;
|
3
|
+
exports.logError = exports.entrypointToOutputPath = exports.getRegExpFromMatchers = void 0;
|
4
4
|
const path_1 = require("path");
|
5
5
|
const path_to_regexp_1 = require("path-to-regexp");
|
6
|
+
const build_utils_1 = require("@vercel/build-utils");
|
6
7
|
function getRegExpFromMatchers(matcherOrMatchers) {
|
7
8
|
if (!matcherOrMatchers) {
|
8
9
|
return '^/.*$';
|
@@ -44,3 +45,14 @@ function entrypointToOutputPath(entrypoint, zeroConfig) {
|
|
44
45
|
return entrypoint;
|
45
46
|
}
|
46
47
|
exports.entrypointToOutputPath = entrypointToOutputPath;
|
48
|
+
function logError(error) {
|
49
|
+
console.error(error.message);
|
50
|
+
if (error.stack) {
|
51
|
+
// only show the stack trace if debug is enabled
|
52
|
+
// because it points to internals, not user code
|
53
|
+
const errorPrefixLength = 'Error: '.length;
|
54
|
+
const errorMessageLength = errorPrefixLength + error.message.length;
|
55
|
+
build_utils_1.debug(error.stack.substring(errorMessageLength + 1));
|
56
|
+
}
|
57
|
+
}
|
58
|
+
exports.logError = logError;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vercel/node",
|
3
|
-
"version": "2.5.
|
3
|
+
"version": "2.5.19",
|
4
4
|
"license": "MIT",
|
5
5
|
"main": "./dist/index",
|
6
6
|
"homepage": "https://vercel.com/docs/runtimes#official-runtimes/node-js",
|
@@ -31,7 +31,7 @@
|
|
31
31
|
"dependencies": {
|
32
32
|
"@edge-runtime/vm": "1.1.0-beta.32",
|
33
33
|
"@types/node": "*",
|
34
|
-
"@vercel/build-utils": "5.5.
|
34
|
+
"@vercel/build-utils": "5.5.1",
|
35
35
|
"@vercel/node-bridge": "3.0.0",
|
36
36
|
"@vercel/static-config": "2.0.3",
|
37
37
|
"edge-runtime": "1.1.0-beta.32",
|
@@ -61,5 +61,5 @@
|
|
61
61
|
"source-map-support": "0.5.12",
|
62
62
|
"test-listen": "1.1.0"
|
63
63
|
},
|
64
|
-
"gitHead": "
|
64
|
+
"gitHead": "bf5cfa9a41f8c1f9e2a3d3fc30735708ca72ad09"
|
65
65
|
}
|