@vercel/node 2.11.0 → 2.13.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/dist/{dev-server.js → dev-server.mjs} +34 -42
- package/dist/edge-functions/edge-handler-template.js +22 -45
- package/dist/edge-functions/{edge-handler.js → edge-handler.mjs} +56 -56
- package/dist/edge-functions/{edge-node-compat-plugin.js → edge-node-compat-plugin.mjs} +12 -20
- package/dist/edge-functions/{edge-wasm-plugin.js → edge-wasm-plugin.mjs} +7 -12
- package/dist/fork-dev-server.js +7 -13
- package/dist/index.d.ts +20 -12
- package/dist/index.js +143 -207
- package/dist/serverless-functions/helpers.js +221 -0
- package/dist/serverless-functions/serverless-handler.mjs +65 -0
- package/dist/typescript.js +4 -4
- package/dist/utils.js +13 -7
- package/package.json +13 -11
- package/dist/edge-functions/edge-handler.d.ts +0 -4
- package/dist/edge-functions/edge-node-compat-plugin.d.ts +0 -15
- package/dist/edge-functions/edge-wasm-plugin.d.ts +0 -21
- package/dist/fork-dev-server.d.ts +0 -34
- package/dist/serverless-functions/serverless-handler.d.ts +0 -7
- package/dist/serverless-functions/serverless-handler.js +0 -47
@@ -1,49 +1,37 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.onDevRequest = void 0;
|
4
1
|
const entrypoint = process.env.VERCEL_DEV_ENTRYPOINT;
|
5
2
|
delete process.env.VERCEL_DEV_ENTRYPOINT;
|
6
3
|
if (!entrypoint) {
|
7
4
|
throw new Error('`VERCEL_DEV_ENTRYPOINT` must be defined');
|
8
5
|
}
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
const
|
18
|
-
const
|
19
|
-
function
|
20
|
-
|
21
|
-
|
22
|
-
resolve();
|
23
|
-
});
|
24
|
-
});
|
25
|
-
}
|
26
|
-
function parseRuntime(entrypoint, entryPointPath) {
|
27
|
-
const project = new ts_morph_1.Project();
|
28
|
-
const staticConfig = static_config_1.getConfig(project, entryPointPath);
|
29
|
-
const runtime = staticConfig?.runtime;
|
30
|
-
if (runtime && !utils_1.isEdgeRuntime(runtime)) {
|
31
|
-
throw new Error(`Invalid function runtime "${runtime}" for "${entrypoint}". Valid runtimes are: ${JSON.stringify(Object.values(utils_1.EdgeRuntimes))}. Learn more: https://vercel.link/creating-edge-functions`);
|
6
|
+
import { join } from 'path';
|
7
|
+
import { createEdgeEventHandler } from './edge-functions/edge-handler.mjs';
|
8
|
+
import { createServer } from 'http';
|
9
|
+
import { createServerlessEventHandler } from './serverless-functions/serverless-handler.mjs';
|
10
|
+
import { EdgeRuntimes, isEdgeRuntime, logError } from './utils.js';
|
11
|
+
import { getConfig } from '@vercel/static-config';
|
12
|
+
import { Project } from 'ts-morph';
|
13
|
+
import asyncListen from 'async-listen';
|
14
|
+
const { default: listen } = asyncListen;
|
15
|
+
const parseConfig = (entryPointPath) => getConfig(new Project(), entryPointPath);
|
16
|
+
function getRuntime(runtime, entrypoint) {
|
17
|
+
if (runtime && !isEdgeRuntime(runtime)) {
|
18
|
+
throw new Error(`Invalid function runtime "${runtime}" for "${entrypoint}". Valid runtimes are: ${JSON.stringify(Object.values(EdgeRuntimes))}. Learn more: https://vercel.link/creating-edge-functions`);
|
32
19
|
}
|
33
20
|
return runtime;
|
34
21
|
}
|
35
22
|
async function createEventHandler(entrypoint, config, options) {
|
36
|
-
const entrypointPath =
|
37
|
-
const
|
23
|
+
const entrypointPath = join(process.cwd(), entrypoint);
|
24
|
+
const staticConfig = parseConfig(entrypointPath);
|
25
|
+
const runtime = getRuntime(staticConfig?.runtime, entrypoint);
|
38
26
|
// `middleware.js`/`middleware.ts` file is always run as
|
39
27
|
// an Edge Function, otherwise needs to be opted-in via
|
40
28
|
// `export const config = { runtime: 'edge' }`
|
41
|
-
if (config.middleware === true ||
|
42
|
-
return
|
29
|
+
if (config.middleware === true || isEdgeRuntime(runtime)) {
|
30
|
+
return createEdgeEventHandler(entrypointPath, entrypoint, config.middleware || false, config.zeroConfig);
|
43
31
|
}
|
44
|
-
return
|
32
|
+
return createServerlessEventHandler(entrypointPath, {
|
33
|
+
mode: staticConfig?.supportsResponseStreaming ? 'streaming' : 'buffer',
|
45
34
|
shouldAddHelpers: options.shouldAddHelpers,
|
46
|
-
useRequire,
|
47
35
|
});
|
48
36
|
}
|
49
37
|
let handleEvent;
|
@@ -54,15 +42,15 @@ async function main() {
|
|
54
42
|
const buildEnv = JSON.parse(process.env.VERCEL_DEV_BUILD_ENV || '{}');
|
55
43
|
delete process.env.VERCEL_DEV_BUILD_ENV;
|
56
44
|
const shouldAddHelpers = !(config.helpers === false || buildEnv.NODEJS_HELPERS === '0');
|
57
|
-
const proxyServer =
|
58
|
-
await listen(proxyServer,
|
45
|
+
const proxyServer = createServer(onDevRequest);
|
46
|
+
await listen(proxyServer, { host: '127.0.0.1', port: 0 });
|
59
47
|
try {
|
60
48
|
handleEvent = await createEventHandler(entrypoint, config, {
|
61
49
|
shouldAddHelpers,
|
62
50
|
});
|
63
51
|
}
|
64
52
|
catch (error) {
|
65
|
-
|
53
|
+
logError(error);
|
66
54
|
handlerEventError = error;
|
67
55
|
}
|
68
56
|
const address = proxyServer.address();
|
@@ -86,22 +74,26 @@ async function onDevRequest(req, res) {
|
|
86
74
|
return;
|
87
75
|
}
|
88
76
|
try {
|
89
|
-
const
|
90
|
-
res.statusCode =
|
91
|
-
for (const [key, value] of
|
92
|
-
if (
|
77
|
+
const { headers, body, status } = await handleEvent(req);
|
78
|
+
res.statusCode = status;
|
79
|
+
for (const [key, value] of headers) {
|
80
|
+
if (value !== undefined) {
|
93
81
|
res.setHeader(key, value);
|
94
82
|
}
|
95
83
|
}
|
96
|
-
|
84
|
+
if (body instanceof Buffer) {
|
85
|
+
res.end(body);
|
86
|
+
}
|
87
|
+
else {
|
88
|
+
body.pipe(res);
|
89
|
+
}
|
97
90
|
}
|
98
91
|
catch (error) {
|
99
92
|
res.statusCode = 500;
|
100
93
|
res.end(error.stack);
|
101
94
|
}
|
102
95
|
}
|
103
|
-
exports.onDevRequest = onDevRequest;
|
104
96
|
main().catch(err => {
|
105
|
-
|
97
|
+
logError(err);
|
106
98
|
process.exit(1);
|
107
99
|
});
|
@@ -1,49 +1,29 @@
|
|
1
1
|
// provided by the edge runtime:
|
2
2
|
/* global addEventListener */
|
3
3
|
|
4
|
-
function
|
5
|
-
const
|
6
|
-
const
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
// handle multi-protocol like: https,http://...
|
12
|
-
proto = allProtocols.split(/\b/).shift();
|
13
|
-
} else {
|
14
|
-
proto = 'http';
|
15
|
-
}
|
16
|
-
|
17
|
-
return `${proto}://${host}${path}`;
|
4
|
+
function getUrl(url, headers) {
|
5
|
+
const urlObj = new URL(url);
|
6
|
+
const protocol = headers.get('x-forwarded-proto');
|
7
|
+
if (protocol) urlObj.protocol = protocol.split(/\b/).shift();
|
8
|
+
urlObj.host = headers.get('x-forwarded-host');
|
9
|
+
urlObj.port = headers.get('x-forwarded-port');
|
10
|
+
return urlObj.toString();
|
18
11
|
}
|
19
12
|
|
20
|
-
async function respond(
|
21
|
-
userEdgeHandler,
|
22
|
-
requestDetails,
|
23
|
-
event,
|
24
|
-
options,
|
25
|
-
dependencies
|
26
|
-
) {
|
13
|
+
async function respond(userEdgeHandler, event, options, dependencies) {
|
27
14
|
const { Request, Response } = dependencies;
|
28
15
|
const { isMiddleware } = options;
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
method: requestDetails.method,
|
41
|
-
body: body,
|
42
|
-
});
|
43
|
-
|
44
|
-
event.request = request;
|
45
|
-
|
46
|
-
let response = await userEdgeHandler(event.request, event);
|
16
|
+
event.request.headers.set(
|
17
|
+
'host',
|
18
|
+
event.request.headers.get('x-forwarded-host')
|
19
|
+
);
|
20
|
+
let response = await userEdgeHandler(
|
21
|
+
new Request(
|
22
|
+
getUrl(event.request.url, event.request.headers),
|
23
|
+
event.request
|
24
|
+
),
|
25
|
+
event
|
26
|
+
);
|
47
27
|
|
48
28
|
if (!response) {
|
49
29
|
if (isMiddleware) {
|
@@ -85,10 +65,8 @@ async function parseRequestEvent(event) {
|
|
85
65
|
function registerFetchListener(userEdgeHandler, options, dependencies) {
|
86
66
|
addEventListener('fetch', async event => {
|
87
67
|
try {
|
88
|
-
const requestDetails = await parseRequestEvent(event);
|
89
68
|
const response = await respond(
|
90
69
|
userEdgeHandler,
|
91
|
-
requestDetails,
|
92
70
|
event,
|
93
71
|
options,
|
94
72
|
dependencies
|
@@ -100,11 +78,10 @@ function registerFetchListener(userEdgeHandler, options, dependencies) {
|
|
100
78
|
});
|
101
79
|
}
|
102
80
|
|
103
|
-
// for testing:
|
104
81
|
module.exports = {
|
105
|
-
|
106
|
-
respond,
|
107
|
-
toResponseError,
|
82
|
+
getUrl,
|
108
83
|
parseRequestEvent,
|
109
84
|
registerFetchListener,
|
85
|
+
respond,
|
86
|
+
toResponseError,
|
110
87
|
};
|
@@ -1,39 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
};
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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 edge_node_compat_plugin_1 = require("./edge-node-compat-plugin");
|
1
|
+
import { createEdgeWasmPlugin } from './edge-wasm-plugin.mjs';
|
2
|
+
import { createNodeCompatPlugin, } from './edge-node-compat-plugin.mjs';
|
3
|
+
import { EdgeRuntime, runServer } from 'edge-runtime';
|
4
|
+
import fetch, { Headers } from 'node-fetch';
|
5
|
+
import { isError } from '@vercel/error-utils';
|
6
|
+
import { readFileSync } from 'fs';
|
7
|
+
import { serializeBody, entrypointToOutputPath, logError } from '../utils.js';
|
8
|
+
import esbuild from 'esbuild';
|
9
|
+
import exitHook from 'exit-hook';
|
10
|
+
import { fileURLToPath } from 'url';
|
16
11
|
const NODE_VERSION_MAJOR = process.version.match(/^v(\d+)\.\d+/)?.[1];
|
17
12
|
const NODE_VERSION_IDENTIFIER = `node${NODE_VERSION_MAJOR}`;
|
18
13
|
if (!NODE_VERSION_MAJOR) {
|
19
14
|
throw new Error(`Unable to determine current node version: process.version=${process.version}`);
|
20
15
|
}
|
21
|
-
const
|
22
|
-
|
23
|
-
const bodyBuffer = await build_utils_1.streamToBuffer(message);
|
24
|
-
const body = bodyBuffer.toString('base64');
|
25
|
-
return JSON.stringify({
|
26
|
-
url: message.url,
|
27
|
-
method: message.method,
|
28
|
-
headers: message.headers,
|
29
|
-
body,
|
30
|
-
});
|
31
|
-
}
|
16
|
+
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
17
|
+
const edgeHandlerTemplate = readFileSync(`${__dirname}/edge-handler-template.js`);
|
32
18
|
async function compileUserCode(entrypointFullPath, entrypointRelativePath, isMiddleware) {
|
33
|
-
const { wasmAssets, plugin: edgeWasmPlugin } =
|
34
|
-
const nodeCompatPlugin =
|
19
|
+
const { wasmAssets, plugin: edgeWasmPlugin } = createEdgeWasmPlugin();
|
20
|
+
const nodeCompatPlugin = createNodeCompatPlugin();
|
35
21
|
try {
|
36
|
-
const result = await
|
22
|
+
const result = await esbuild.build({
|
37
23
|
// bundling behavior: use globals (like "browser") instead
|
38
24
|
// of "require" statements for core libraries (like "node")
|
39
25
|
platform: 'browser',
|
@@ -43,7 +29,20 @@ async function compileUserCode(entrypointFullPath, entrypointRelativePath, isMid
|
|
43
29
|
sourcemap: 'inline',
|
44
30
|
legalComments: 'none',
|
45
31
|
bundle: true,
|
46
|
-
plugins: [
|
32
|
+
plugins: [
|
33
|
+
edgeWasmPlugin,
|
34
|
+
nodeCompatPlugin.plugin,
|
35
|
+
{
|
36
|
+
name: 'import.meta.url',
|
37
|
+
setup({ onLoad }) {
|
38
|
+
onLoad({ filter: /\.[cm]?js$/, namespace: 'file' }, args => {
|
39
|
+
let code = readFileSync(args.path, 'utf8');
|
40
|
+
code = code.replace(/\bimport\.meta\.url\b/g, JSON.stringify(import.meta.url));
|
41
|
+
return { contents: code };
|
42
|
+
});
|
43
|
+
},
|
44
|
+
},
|
45
|
+
],
|
47
46
|
entryPoints: [entrypointFullPath],
|
48
47
|
write: false,
|
49
48
|
format: 'cjs',
|
@@ -71,14 +70,8 @@ async function compileUserCode(entrypointFullPath, entrypointRelativePath, isMid
|
|
71
70
|
|
72
71
|
// edge handler
|
73
72
|
${edgeHandlerTemplate};
|
74
|
-
const dependencies = {
|
75
|
-
|
76
|
-
Response
|
77
|
-
};
|
78
|
-
const options = {
|
79
|
-
isMiddleware,
|
80
|
-
entrypointLabel
|
81
|
-
};
|
73
|
+
const dependencies = { Request, Response };
|
74
|
+
const options = { isMiddleware, entrypointLabel };
|
82
75
|
registerFetchListener(userEdgeHandler, options, dependencies);
|
83
76
|
`;
|
84
77
|
return {
|
@@ -91,20 +84,21 @@ async function compileUserCode(entrypointFullPath, entrypointRelativePath, isMid
|
|
91
84
|
// We can't easily show a meaningful stack trace from ncc -> edge-runtime.
|
92
85
|
// So, stick with just the message for now.
|
93
86
|
console.error(`Failed to compile user code for edge runtime.`);
|
94
|
-
|
87
|
+
if (isError(error))
|
88
|
+
logError(error);
|
95
89
|
return undefined;
|
96
90
|
}
|
97
91
|
}
|
98
|
-
async function
|
92
|
+
async function createEdgeRuntimeServer(params) {
|
99
93
|
try {
|
100
94
|
if (!params) {
|
101
95
|
return undefined;
|
102
96
|
}
|
103
97
|
const wasmBindings = await params.wasmAssets.getContext();
|
104
98
|
const nodeCompatBindings = params.nodeCompatBindings.getContext();
|
105
|
-
const
|
99
|
+
const runtime = new EdgeRuntime({
|
106
100
|
initialCode: params.userCode,
|
107
|
-
extend:
|
101
|
+
extend: context => {
|
108
102
|
Object.assign(context, {
|
109
103
|
// This is required for esbuild wrapping logic to resolve
|
110
104
|
module: {},
|
@@ -122,21 +116,21 @@ async function createEdgeRuntime(params) {
|
|
122
116
|
return context;
|
123
117
|
},
|
124
118
|
});
|
125
|
-
const server = await
|
126
|
-
|
119
|
+
const server = await runServer({ runtime });
|
120
|
+
exitHook(() => server.close());
|
127
121
|
return server;
|
128
122
|
}
|
129
123
|
catch (error) {
|
130
124
|
// We can't easily show a meaningful stack trace from ncc -> edge-runtime.
|
131
125
|
// So, stick with just the message for now.
|
132
126
|
console.error('Failed to instantiate edge runtime.');
|
133
|
-
|
127
|
+
logError(error);
|
134
128
|
return undefined;
|
135
129
|
}
|
136
130
|
}
|
137
|
-
async function createEdgeEventHandler(entrypointFullPath, entrypointRelativePath, isMiddleware, isZeroConfig) {
|
131
|
+
export async function createEdgeEventHandler(entrypointFullPath, entrypointRelativePath, isMiddleware, isZeroConfig) {
|
138
132
|
const userCode = await compileUserCode(entrypointFullPath, entrypointRelativePath, isMiddleware);
|
139
|
-
const server = await
|
133
|
+
const server = await createEdgeRuntimeServer(userCode);
|
140
134
|
return async function (request) {
|
141
135
|
if (!server) {
|
142
136
|
// this error state is already logged, but we have to wait until here to exit the process
|
@@ -144,14 +138,21 @@ async function createEdgeEventHandler(entrypointFullPath, entrypointRelativePath
|
|
144
138
|
// an error is thrown in the function
|
145
139
|
process.exit(1);
|
146
140
|
}
|
147
|
-
const
|
141
|
+
const headers = new Headers(request.headers);
|
142
|
+
const body = await serializeBody(request);
|
143
|
+
if (body !== undefined)
|
144
|
+
headers.set('content-length', String(body.length));
|
145
|
+
const url = new URL(request.url ?? '/', server.url);
|
146
|
+
// @ts-expect-error
|
147
|
+
const response = await fetch(url, {
|
148
|
+
body,
|
149
|
+
headers,
|
150
|
+
method: request.method,
|
148
151
|
redirect: 'manual',
|
149
|
-
method: 'post',
|
150
|
-
body: await serializeRequest(request),
|
151
152
|
});
|
152
|
-
const body = await response.text();
|
153
153
|
const isUserError = response.headers.get('x-vercel-failed') === 'edge-wrapper';
|
154
154
|
if (isUserError && response.status >= 500) {
|
155
|
+
const body = await response.text();
|
155
156
|
// We can't currently get a real stack trace from the Edge Function error,
|
156
157
|
// but we can fake a basic one that is still usefult to the user.
|
157
158
|
const fakeStackTrace = ` at (${entrypointRelativePath})`;
|
@@ -162,16 +163,15 @@ async function createEdgeEventHandler(entrypointFullPath, entrypointRelativePath
|
|
162
163
|
process.exit(1);
|
163
164
|
}
|
164
165
|
return {
|
165
|
-
|
166
|
-
headers: response.headers
|
167
|
-
body,
|
166
|
+
status: response.status,
|
167
|
+
headers: response.headers,
|
168
|
+
body: response.body,
|
168
169
|
encoding: 'utf8',
|
169
170
|
};
|
170
171
|
};
|
171
172
|
}
|
172
|
-
exports.createEdgeEventHandler = createEdgeEventHandler;
|
173
173
|
function entrypointToRequestPath(entrypointRelativePath, isZeroConfig) {
|
174
174
|
// ensure the path starts with a slash to match conventions used elsewhere,
|
175
175
|
// notably when rendering serverless function paths in error messages
|
176
|
-
return '/' +
|
176
|
+
return '/' + entrypointToOutputPath(entrypointRelativePath, isZeroConfig);
|
177
177
|
}
|
@@ -1,14 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
exports.createNodeCompatPlugin = exports.NodeCompatBindings = void 0;
|
7
|
-
const buffer_1 = __importDefault(require("buffer"));
|
8
|
-
const events_1 = __importDefault(require("events"));
|
9
|
-
const async_hooks_1 = __importDefault(require("async_hooks"));
|
10
|
-
const assert_1 = __importDefault(require("assert"));
|
11
|
-
const util_1 = __importDefault(require("util"));
|
1
|
+
import BufferImplementation from 'buffer';
|
2
|
+
import EventsImplementation from 'events';
|
3
|
+
import AsyncHooksImplementation from 'async_hooks';
|
4
|
+
import AssertImplementation from 'assert';
|
5
|
+
import UtilImplementation from 'util';
|
12
6
|
const SUPPORTED_NODE_MODULES = [
|
13
7
|
'buffer',
|
14
8
|
'events',
|
@@ -26,14 +20,14 @@ function pick(obj, keys) {
|
|
26
20
|
}
|
27
21
|
const NativeModuleMap = () => {
|
28
22
|
const mods = {
|
29
|
-
buffer: pick(
|
23
|
+
buffer: pick(BufferImplementation, [
|
30
24
|
'constants',
|
31
25
|
'kMaxLength',
|
32
26
|
'kStringMaxLength',
|
33
27
|
'Buffer',
|
34
28
|
'SlowBuffer',
|
35
29
|
]),
|
36
|
-
events: pick(
|
30
|
+
events: pick(EventsImplementation, [
|
37
31
|
'EventEmitter',
|
38
32
|
'captureRejectionSymbol',
|
39
33
|
'defaultMaxListeners',
|
@@ -42,11 +36,11 @@ const NativeModuleMap = () => {
|
|
42
36
|
'on',
|
43
37
|
'once',
|
44
38
|
]),
|
45
|
-
async_hooks: pick(
|
39
|
+
async_hooks: pick(AsyncHooksImplementation, [
|
46
40
|
'AsyncLocalStorage',
|
47
41
|
'AsyncResource',
|
48
42
|
]),
|
49
|
-
assert: pick(
|
43
|
+
assert: pick(AssertImplementation, [
|
50
44
|
'AssertionError',
|
51
45
|
'deepEqual',
|
52
46
|
'deepStrictEqual',
|
@@ -67,7 +61,7 @@ const NativeModuleMap = () => {
|
|
67
61
|
'strictEqual',
|
68
62
|
'throws',
|
69
63
|
]),
|
70
|
-
util: pick(
|
64
|
+
util: pick(UtilImplementation, [
|
71
65
|
'_extend',
|
72
66
|
'callbackify',
|
73
67
|
'format',
|
@@ -79,7 +73,7 @@ const NativeModuleMap = () => {
|
|
79
73
|
return new Map(Object.entries(mods));
|
80
74
|
};
|
81
75
|
const NODE_COMPAT_NAMESPACE = 'vercel-node-compat';
|
82
|
-
class NodeCompatBindings {
|
76
|
+
export class NodeCompatBindings {
|
83
77
|
constructor() {
|
84
78
|
this.bindings = new Map();
|
85
79
|
}
|
@@ -107,13 +101,12 @@ class NodeCompatBindings {
|
|
107
101
|
return context;
|
108
102
|
}
|
109
103
|
}
|
110
|
-
exports.NodeCompatBindings = NodeCompatBindings;
|
111
104
|
/**
|
112
105
|
* Allows to enable Node.js compatibility by detecting namespaced `node:`
|
113
106
|
* imports and producing metadata to bind global variables for each.
|
114
107
|
* It requires from the consumer to add the imports.
|
115
108
|
*/
|
116
|
-
function createNodeCompatPlugin() {
|
109
|
+
export function createNodeCompatPlugin() {
|
117
110
|
const bindings = new NodeCompatBindings();
|
118
111
|
const plugin = {
|
119
112
|
name: 'vc-node-compat',
|
@@ -145,4 +138,3 @@ function createNodeCompatPlugin() {
|
|
145
138
|
bindings,
|
146
139
|
};
|
147
140
|
}
|
148
|
-
exports.createNodeCompatPlugin = createNodeCompatPlugin;
|
@@ -1,11 +1,8 @@
|
|
1
|
-
"use strict";
|
2
1
|
// copied from `edge-functions-bridge`:
|
3
2
|
// https://github.com/vercel/runtimes/blob/c076db9e3ce5635f7c2690396e3d9f791a0fd808/packages/edge-functions-bridge/src/get-edge-function-source.ts#L282-L317
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
const fs_1 = require("fs");
|
8
|
-
class WasmAssets {
|
3
|
+
import { createHash } from 'crypto';
|
4
|
+
import { promises as fs } from 'fs';
|
5
|
+
export class WasmAssets {
|
9
6
|
constructor() {
|
10
7
|
this.assets = new Map();
|
11
8
|
}
|
@@ -13,7 +10,7 @@ class WasmAssets {
|
|
13
10
|
* Declare a WebAssembly binding
|
14
11
|
*/
|
15
12
|
async declare(filePath) {
|
16
|
-
const hash = sha1(await
|
13
|
+
const hash = sha1(await fs.readFile(filePath));
|
17
14
|
const name = `wasm_${hash}`;
|
18
15
|
this.assets.set(name, filePath);
|
19
16
|
return name;
|
@@ -27,7 +24,7 @@ class WasmAssets {
|
|
27
24
|
const context = {};
|
28
25
|
for (const [name, filePath] of this.assets) {
|
29
26
|
promises.push((async () => {
|
30
|
-
const bytes = await
|
27
|
+
const bytes = await fs.readFile(filePath);
|
31
28
|
context[name] = await WebAssembly.compile(bytes);
|
32
29
|
})());
|
33
30
|
}
|
@@ -41,8 +38,7 @@ class WasmAssets {
|
|
41
38
|
return this.assets[Symbol.iterator]();
|
42
39
|
}
|
43
40
|
}
|
44
|
-
|
45
|
-
function createEdgeWasmPlugin() {
|
41
|
+
export function createEdgeWasmPlugin() {
|
46
42
|
const wasmAssets = new WasmAssets();
|
47
43
|
const plugin = {
|
48
44
|
name: 'vercel-wasm',
|
@@ -76,7 +72,6 @@ function createEdgeWasmPlugin() {
|
|
76
72
|
};
|
77
73
|
return { plugin, wasmAssets };
|
78
74
|
}
|
79
|
-
exports.createEdgeWasmPlugin = createEdgeWasmPlugin;
|
80
75
|
function sha1(data) {
|
81
|
-
return
|
76
|
+
return createHash('sha1').update(data).digest('hex');
|
82
77
|
}
|
package/dist/fork-dev-server.js
CHANGED
@@ -12,17 +12,12 @@ const path_1 = require("path");
|
|
12
12
|
function forkDevServer(options) {
|
13
13
|
let nodeOptions = process.env.NODE_OPTIONS;
|
14
14
|
const tsNodePath = options.require_.resolve('ts-node');
|
15
|
-
const esmLoader = url_1.pathToFileURL(path_1.join(tsNodePath, '..', '..', 'esm.mjs'));
|
16
|
-
const cjsLoader = path_1.join(tsNodePath, '..', '..', 'register', 'index.js');
|
17
|
-
const devServerPath = options.devServerPath || path_1.join(__dirname, 'dev-server.
|
15
|
+
const esmLoader = (0, url_1.pathToFileURL)((0, path_1.join)(tsNodePath, '..', '..', 'esm.mjs'));
|
16
|
+
const cjsLoader = (0, path_1.join)(tsNodePath, '..', '..', 'register', 'index.js');
|
17
|
+
const devServerPath = options.devServerPath || (0, path_1.join)(__dirname, 'dev-server.mjs');
|
18
18
|
if (options.maybeTranspile) {
|
19
19
|
if (options.isTypeScript) {
|
20
|
-
|
21
|
-
nodeOptions = `--loader ${esmLoader} ${nodeOptions || ''}`;
|
22
|
-
}
|
23
|
-
else {
|
24
|
-
nodeOptions = `--require ${cjsLoader} ${nodeOptions || ''}`;
|
25
|
-
}
|
20
|
+
nodeOptions = `--loader ${esmLoader} ${nodeOptions || ''}`;
|
26
21
|
}
|
27
22
|
else {
|
28
23
|
if (options.isEsm) {
|
@@ -36,9 +31,8 @@ function forkDevServer(options) {
|
|
36
31
|
const forkOptions = {
|
37
32
|
cwd: options.workPath,
|
38
33
|
execArgv: [],
|
39
|
-
env: build_utils_1.cloneEnv(process.env, options.meta.env, {
|
34
|
+
env: (0, build_utils_1.cloneEnv)(process.env, options.meta.env, {
|
40
35
|
VERCEL_DEV_ENTRYPOINT: options.entrypoint,
|
41
|
-
VERCEL_DEV_IS_ESM: options.isEsm ? '1' : undefined,
|
42
36
|
VERCEL_DEV_CONFIG: JSON.stringify(options.config),
|
43
37
|
VERCEL_DEV_BUILD_ENV: JSON.stringify(options.meta.buildEnv || {}),
|
44
38
|
TS_NODE_TRANSPILE_ONLY: '1',
|
@@ -48,7 +42,7 @@ function forkDevServer(options) {
|
|
48
42
|
NODE_OPTIONS: nodeOptions,
|
49
43
|
}),
|
50
44
|
};
|
51
|
-
const child = child_process_1.fork(devServerPath, [], forkOptions);
|
45
|
+
const child = (0, child_process_1.fork)(devServerPath, [], forkOptions);
|
52
46
|
checkForPid(devServerPath, child);
|
53
47
|
return child;
|
54
48
|
}
|
@@ -64,7 +58,7 @@ function checkForPid(path, process) {
|
|
64
58
|
* or it is listening to new requests, and we can start proxying requests.
|
65
59
|
*/
|
66
60
|
async function readMessage(child) {
|
67
|
-
const onMessage = once_1.default(child, 'message');
|
61
|
+
const onMessage = (0, once_1.default)(child, 'message');
|
68
62
|
const onExit = once_1.default.spread(child, 'close');
|
69
63
|
const result = await Promise.race([
|
70
64
|
onMessage.then(x => {
|
package/dist/index.d.ts
CHANGED
@@ -1,33 +1,41 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
+
/// <reference types="node" />
|
2
3
|
import { ServerResponse, IncomingMessage } from 'http';
|
3
|
-
|
4
|
+
import type { Headers } from 'node-fetch';
|
5
|
+
export type VercelRequestCookies = {
|
4
6
|
[key: string]: string;
|
5
7
|
};
|
6
|
-
export
|
8
|
+
export type VercelRequestQuery = {
|
7
9
|
[key: string]: string | string[];
|
8
10
|
};
|
9
|
-
export
|
10
|
-
export
|
11
|
+
export type VercelRequestBody = any;
|
12
|
+
export type VercelRequest = IncomingMessage & {
|
11
13
|
query: VercelRequestQuery;
|
12
14
|
cookies: VercelRequestCookies;
|
13
15
|
body: VercelRequestBody;
|
14
16
|
};
|
15
|
-
export
|
17
|
+
export type VercelResponse = ServerResponse & {
|
16
18
|
send: (body: any) => VercelResponse;
|
17
19
|
json: (jsonBody: any) => VercelResponse;
|
18
20
|
status: (statusCode: number) => VercelResponse;
|
19
21
|
redirect: (statusOrUrl: string | number, url?: string) => VercelResponse;
|
20
22
|
};
|
21
|
-
export
|
23
|
+
export type VercelApiHandler = (req: VercelRequest, res: VercelResponse) => void | Promise<void>;
|
22
24
|
/** @deprecated Use VercelRequestCookies instead. */
|
23
|
-
export
|
25
|
+
export type NowRequestCookies = VercelRequestCookies;
|
24
26
|
/** @deprecated Use VercelRequestQuery instead. */
|
25
|
-
export
|
27
|
+
export type NowRequestQuery = VercelRequestQuery;
|
26
28
|
/** @deprecated Use VercelRequestBody instead. */
|
27
|
-
export
|
29
|
+
export type NowRequestBody = any;
|
28
30
|
/** @deprecated Use VercelRequest instead. */
|
29
|
-
export
|
31
|
+
export type NowRequest = VercelRequest;
|
30
32
|
/** @deprecated Use VercelResponse instead. */
|
31
|
-
export
|
33
|
+
export type NowResponse = VercelResponse;
|
32
34
|
/** @deprecated Use VercelApiHandler instead. */
|
33
|
-
export
|
35
|
+
export type NowApiHandler = VercelApiHandler;
|
36
|
+
export interface VercelProxyResponse {
|
37
|
+
status: number;
|
38
|
+
headers: Headers;
|
39
|
+
body: Buffer | NodeJS.ReadableStream;
|
40
|
+
encoding: BufferEncoding;
|
41
|
+
}
|