forked from netlify/netlify-dev-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve-functions.js
274 lines (247 loc) · 8.13 KB
/
serve-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const expressLogging = require("express-logging");
const queryString = require("querystring");
const path = require("path");
const getPort = require("get-port");
const chokidar = require("chokidar");
const jwtDecode = require("jwt-decode");
const chalk = require("chalk");
const {
NETLIFYDEVLOG,
// NETLIFYDEVWARN,
NETLIFYDEVERR
} = require("netlify-cli-logo");
const { findModuleDir, findHandler } = require("./finders");
const defaultPort = 34567;
function handleErr(err, response) {
response.statusCode = 500;
response.write(
`${NETLIFYDEVERR} Function invocation failed: ` + err.toString()
);
response.end();
console.log(`${NETLIFYDEVERR} Error during invocation: `, err); // eslint-disable-line no-console
}
// function getHandlerPath(functionPath) {
// if (functionPath.match(/\.js$/)) {
// return functionPath;
// }
// return path.join(functionPath, `${path.basename(functionPath)}.js`);
// }
function buildClientContext(headers) {
// inject a client context based on auth header, ported over from netlify-lambda (https://github.com/netlify/netlify-lambda/pull/57)
if (!headers.authorization) return;
const parts = headers.authorization.split(" ");
if (parts.length !== 2 || parts[0] !== "Bearer") return;
try {
return {
identity: {
url: "NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL",
token: "NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN"
},
user: jwtDecode(parts[1])
};
} catch (_) {
// Ignore errors - bearer token is not a JWT, probably not intended for us
}
}
function createHandler(dir) {
const functions = {};
fs.readdirSync(dir).forEach(file => {
if (dir === "node_modules") {
return;
}
const functionPath = path.resolve(path.join(dir, file));
const handlerPath = findHandler(functionPath);
if (!handlerPath) {
return;
}
if (path.extname(functionPath) === ".js") {
functions[file.replace(/\.js$/, "")] = {
functionPath,
moduleDir: findModuleDir(functionPath)
};
} else if (fs.lstatSync(functionPath).isDirectory()) {
functions[file] = {
functionPath: handlerPath,
moduleDir: findModuleDir(functionPath)
};
}
});
const clearCache = action => path => {
console.log(`${NETLIFYDEVLOG} ${path} ${action}, reloading...`); // eslint-disable-line no-console
Object.keys(require.cache).forEach(k => {
delete require.cache[k];
});
};
const watcher = chokidar.watch(dir, { ignored: /node_modules/ });
watcher
.on("change", clearCache("modified"))
.on("unlink", clearCache("deleted"));
return function(request, response) {
// handle proxies without path re-writes (http-servr)
const cleanPath = request.path.replace(/^\/.netlify\/functions/, "");
const func = cleanPath.split("/").filter(function(e) {
return e;
})[0];
if (!functions[func]) {
response.statusCode = 404;
response.end("Function not found...");
return;
}
const { functionPath, moduleDir } = functions[func];
let handler;
let before = module.paths;
try {
module.paths = [moduleDir];
handler = require(functionPath);
if (typeof handler.handler !== "function") {
throw new Error(
`function ${functionPath} must export a function named handler`
);
}
module.paths = before;
} catch (error) {
module.paths = before;
handleErr(error, response);
return;
}
var isBase64Encoded = false;
var body = request.body;
if (body instanceof Buffer) {
isBase64Encoded = true;
body = body.toString("base64");
} else if (typeof body === "string") {
// body is already processed as string
} else {
body = "";
}
const lambdaRequest = {
path: request.path,
httpMethod: request.method,
queryStringParameters: queryString.parse(request.url.split(/\?(.+)/)[1]),
headers: request.headers,
body: body,
isBase64Encoded: isBase64Encoded
};
let callbackWasCalled = false;
const callback = createCallback(response);
const promise = handler.handler(
lambdaRequest,
{ clientContext: buildClientContext(request.headers) || {} },
callback
);
/** guard against using BOTH async and callback */
if (callbackWasCalled && promise && typeof promise.then === "function") {
throw new Error(
"Error: your function seems to be using both a callback and returning a promise (aka async function). This is invalid, pick one. (Hint: async!)"
);
} else {
// it is definitely an async function with no callback called, good.
promiseCallback(promise, callback);
}
/** need to keep createCallback in scope so we can know if cb was called AND handler is async */
function createCallback(response) {
return function(err, lambdaResponse) {
callbackWasCalled = true;
if (err) {
return handleErr(err, response);
}
if (lambdaResponse === undefined) {
return handleErr(
"lambda response was undefined. check your function code again.",
response
);
}
if (!Number(lambdaResponse.statusCode)) {
console.log(
`${NETLIFYDEVERR} Your function response must have a numerical statusCode. You gave: $`,
lambdaResponse.statusCode
);
return handleErr("Incorrect function response statusCode", response);
}
if (typeof lambdaResponse.body !== "string") {
console.log(
`${NETLIFYDEVERR} Your function response must have a string body. You gave:`,
lambdaResponse.body
);
return handleErr("Incorrect function response body", response);
}
response.statusCode = lambdaResponse.statusCode;
// eslint-disable-line guard-for-in
for (const key in lambdaResponse.headers) {
response.setHeader(key, lambdaResponse.headers[key]);
}
response.write(
lambdaResponse.isBase64Encoded
? Buffer.from(lambdaResponse.body, "base64")
: lambdaResponse.body
);
response.end();
};
}
};
}
function promiseCallback(promise, callback) {
if (!promise) return; // means no handler was written
if (typeof promise.then !== "function") return;
if (typeof callback !== "function") return;
promise.then(
function(data) {
callback(null, data);
},
function(err) {
callback(err, null);
}
);
}
async function serveFunctions(settings) {
const app = express();
const dir = settings.functionsDir;
const port = await getPort({
port: assignLoudly(settings.port, defaultPort)
});
app.use(
bodyParser.text({
limit: "6mb",
type: ["text/*", "application/json", "multipart/form-data"]
})
);
app.use(bodyParser.raw({ limit: "6mb", type: "*/*" }));
app.use(
expressLogging(console, {
blacklist: ["/favicon.ico"]
})
);
app.get("/favicon.ico", function(req, res) {
res.status(204).end();
});
app.all("*", createHandler(dir));
app.listen(port, function(err) {
if (err) {
console.error(`${NETLIFYDEVERR} Unable to start lambda server: `, err); // eslint-disable-line no-console
process.exit(1);
}
// add newline because this often appears alongside the client devserver's output
console.log(`\n${NETLIFYDEVLOG} Lambda server is listening on ${port}`); // eslint-disable-line no-console
});
return Promise.resolve({
port
});
}
module.exports = { serveFunctions };
// if first arg is undefined, use default, but tell user about it in case it is unintentional
function assignLoudly(
optionalValue,
fallbackValue,
tellUser = dV =>
console.log(`${NETLIFYDEVLOG} No port specified, using defaultPort of `, dV) // eslint-disable-line no-console
) {
if (fallbackValue === undefined) throw new Error("must have a fallbackValue");
if (fallbackValue !== optionalValue && optionalValue === undefined) {
tellUser(fallbackValue);
return fallbackValue;
}
return optionalValue;
}