@vercel/node 1.13.0 → 1.13.1-canary.2

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/launcher.js DELETED
@@ -1,199 +0,0 @@
1
- const { parse, pathToFileURL } = require('url');
2
- const { createServer, Server } = require('http');
3
- const { isAbsolute } = require('path');
4
- const { Bridge } = require('./bridge.js');
5
-
6
- /**
7
- * @param {import('./types').LauncherConfiguration} config
8
- */
9
- function makeVercelLauncher(config) {
10
- const {
11
- entrypointPath,
12
- bridgePath,
13
- helpersPath,
14
- sourcemapSupportPath,
15
- shouldAddHelpers = false,
16
- shouldAddSourcemapSupport = false,
17
- } = config;
18
- return `
19
- const { parse, pathToFileURL } = require('url');
20
- const { createServer, Server } = require('http');
21
- const { isAbsolute } = require('path');
22
- const { Bridge } = require(${JSON.stringify(bridgePath)});
23
- ${
24
- shouldAddSourcemapSupport
25
- ? `require(${JSON.stringify(sourcemapSupportPath)});`
26
- : ''
27
- }
28
- const entrypointPath = ${JSON.stringify(entrypointPath)};
29
- const shouldAddHelpers = ${JSON.stringify(shouldAddHelpers)};
30
- const helpersPath = ${JSON.stringify(helpersPath)};
31
- const useRequire = false;
32
-
33
- const func = (${getVercelLauncher(config).toString()})();
34
- exports.launcher = func.launcher;`;
35
- }
36
-
37
- /**
38
- * @param {import('./types').LauncherConfiguration} config
39
- */
40
- function getVercelLauncher({
41
- entrypointPath,
42
- helpersPath,
43
- shouldAddHelpers = false,
44
- useRequire = false,
45
- }) {
46
- return function () {
47
- const bridge = new Bridge();
48
- let isServerListening = false;
49
-
50
- const originalListen = Server.prototype.listen;
51
- Server.prototype.listen = function listen() {
52
- isServerListening = true;
53
- console.log('Legacy server listening...');
54
- bridge.setServer(this);
55
- Server.prototype.listen = originalListen;
56
- bridge.listen();
57
- return this;
58
- };
59
-
60
- if (!process.env.NODE_ENV) {
61
- const region = process.env.VERCEL_REGION || process.env.NOW_REGION;
62
- process.env.NODE_ENV = region === 'dev1' ? 'development' : 'production';
63
- }
64
-
65
- /**
66
- * @param {string} p - entrypointPath
67
- */
68
- async function getListener(p) {
69
- let listener = useRequire
70
- ? require(p)
71
- : await import(isAbsolute(p) ? pathToFileURL(p).href : p);
72
-
73
- // In some cases we might have nested default props due to TS => JS
74
- for (let i = 0; i < 5; i++) {
75
- if (listener.default) listener = listener.default;
76
- }
77
-
78
- return listener;
79
- }
80
-
81
- getListener(entrypointPath)
82
- .then(listener => {
83
- if (typeof listener.listen === 'function') {
84
- Server.prototype.listen = originalListen;
85
- const server = listener;
86
- bridge.setServer(server);
87
- bridge.listen();
88
- } else if (typeof listener === 'function') {
89
- Server.prototype.listen = originalListen;
90
- if (shouldAddHelpers) {
91
- bridge.setStoreEvents(true);
92
- import(helpersPath).then(helper => {
93
- const h = helper.default || helper;
94
- const server = h.createServerWithHelpers(listener, bridge);
95
- bridge.setServer(server);
96
- bridge.listen();
97
- });
98
- } else {
99
- const server = createServer(listener);
100
- bridge.setServer(server);
101
- bridge.listen();
102
- }
103
- } else if (
104
- typeof listener === 'object' &&
105
- Object.keys(listener).length === 0
106
- ) {
107
- setTimeout(() => {
108
- if (!isServerListening) {
109
- console.error('No exports found in module %j.', entrypointPath);
110
- console.error('Did you forget to export a function or a server?');
111
- process.exit(1);
112
- }
113
- }, 5000);
114
- } else {
115
- console.error('Invalid export found in module %j.', entrypointPath);
116
- console.error('The default export must be a function or server.');
117
- }
118
- })
119
- .catch(err => {
120
- if (err.code === 'MODULE_NOT_FOUND') {
121
- console.error(err.message);
122
- console.error(
123
- 'Did you forget to add it to "dependencies" in `package.json`?'
124
- );
125
- } else {
126
- console.error(err);
127
- }
128
- process.exit(1);
129
- });
130
-
131
- return bridge;
132
- };
133
- }
134
-
135
- /**
136
- * @param {import('./types').LauncherConfiguration} config
137
- */
138
- function makeAwsLauncher(config) {
139
- const { entrypointPath, awsLambdaHandler = '' } = config;
140
- return `const { parse } = require("url");
141
- const funcName = ${JSON.stringify(awsLambdaHandler.split('.').pop())};
142
- const entrypointPath = ${JSON.stringify(entrypointPath)};
143
- exports.launcher = ${getAwsLauncher(config).toString()}`;
144
- }
145
-
146
- /**
147
- * @param {import('./types').LauncherConfiguration} config
148
- */
149
- function getAwsLauncher({ entrypointPath, awsLambdaHandler = '' }) {
150
- const funcName = awsLambdaHandler.split('.').pop() || '';
151
- if (typeof funcName !== 'string') {
152
- throw new TypeError('Expected "string"');
153
- }
154
-
155
- /**
156
- * @param {import('aws-lambda').APIGatewayProxyEvent} e
157
- * @param {import('aws-lambda').Context} context
158
- * @param {() => void} callback
159
- */
160
- function internal(e, context, callback) {
161
- const {
162
- path,
163
- method: httpMethod,
164
- body,
165
- headers,
166
- } = JSON.parse(e.body || '{}');
167
- const { query } = parse(path, true);
168
- /**
169
- * @type {{[key: string]: string}}
170
- */
171
- const queryStringParameters = {};
172
- for (const [key, value] of Object.entries(query)) {
173
- if (typeof value === 'string') {
174
- queryStringParameters[key] = value;
175
- }
176
- }
177
- const awsGatewayEvent = {
178
- resource: '/{proxy+}',
179
- path: path,
180
- httpMethod: httpMethod,
181
- body: body,
182
- isBase64Encoded: true,
183
- queryStringParameters: queryStringParameters,
184
- multiValueQueryStringParameters: query,
185
- headers: headers,
186
- };
187
-
188
- const mod = require(entrypointPath);
189
- return mod[funcName](awsGatewayEvent, context, callback);
190
- }
191
- return internal;
192
- }
193
-
194
- module.exports = {
195
- makeVercelLauncher,
196
- getVercelLauncher,
197
- makeAwsLauncher,
198
- getAwsLauncher,
199
- };