Skip to content

Commit dcaac14

Browse files
author
sw-yx
committed
add nice warning message if body omitted
1 parent 8db4bbc commit dcaac14

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

Diff for: lib/serve.js

+57-31
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var express = require("express");
2-
var bodyParser = require("body-parser");
3-
var expressLogging = require("express-logging");
4-
var queryString = require("querystring");
5-
var path = require("path");
6-
var conf = require("./config");
7-
var jwtDecode = require("jwt-decode")
1+
var express = require('express');
2+
var bodyParser = require('body-parser');
3+
var expressLogging = require('express-logging');
4+
var queryString = require('querystring');
5+
var path = require('path');
6+
var conf = require('./config');
7+
var jwtDecode = require('jwt-decode');
88

99
function handleErr(err, response) {
1010
response.statusCode = 500;
11-
response.write("Function invocation failed: " + err.toString());
11+
response.write('Function invocation failed: ' + err.toString());
1212
response.end();
13-
console.log("Error during invocation: ", err);
13+
console.log('Error during invocation: ', err);
1414
return;
1515
}
1616

@@ -28,22 +28,35 @@ function createCallback(response) {
2828
if (lambdaResponse.body) {
2929
response.write(
3030
lambdaResponse.isBase64Encoded
31-
? Buffer.from(lambdaResponse.body, "base64")
31+
? Buffer.from(lambdaResponse.body, 'base64')
3232
: lambdaResponse.body
3333
);
34+
} else {
35+
if (
36+
process.env.CONTEXT !== 'production' ||
37+
!process.env.SILENCE_EMPTY_LAMBDA_WARNING
38+
)
39+
console.log(
40+
`Your lambda function didn't return a body, which may be a mistake. Check our Usage docs for examples (https://github.com/netlify/netlify-lambda#usage).
41+
If this is intentional, you can silence this warning by setting process.ENV.SILENCE_EMPTY_LAMBDA_WARNING to a truthy value or process.env.CONTEXT to 'production'`
42+
);
3443
}
3544
response.end();
36-
}
45+
};
3746
}
3847

3948
function promiseCallback(promise, callback) {
4049
if (!promise) return;
41-
if (typeof promise.then !== "function") return;
42-
if (typeof callback !== "function") return;
50+
if (typeof promise.then !== 'function') return;
51+
if (typeof callback !== 'function') return;
4352

4453
promise.then(
45-
function(data) {callback(null, data)},
46-
function(err) {callback(err, null)}
54+
function(data) {
55+
callback(null, data);
56+
},
57+
function(err) {
58+
callback(err, null);
59+
}
4760
);
4861
}
4962

@@ -56,7 +69,10 @@ function buildClientContext(headers) {
5669

5770
try {
5871
return {
59-
identity: { url: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL', token: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN' },
72+
identity: {
73+
url: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL',
74+
token: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN'
75+
},
6076
user: jwtDecode(parts[1])
6177
};
6278
} catch (e) {
@@ -67,14 +83,14 @@ function buildClientContext(headers) {
6783
function createHandler(dir, static) {
6884
return function(request, response) {
6985
// handle proxies without path re-writes (http-servr)
70-
var cleanPath = request.path.replace(/^\/.netlify\/functions/, "")
86+
var cleanPath = request.path.replace(/^\/.netlify\/functions/, '');
7187

72-
var func = cleanPath.split("/").filter(function(e) {
88+
var func = cleanPath.split('/').filter(function(e) {
7389
return e;
7490
})[0];
7591
var module = path.join(process.cwd(), dir, func);
76-
if(static) {
77-
delete require.cache[require.resolve(module)]
92+
if (static) {
93+
delete require.cache[require.resolve(module)];
7894
}
7995
var handler;
8096
try {
@@ -86,19 +102,27 @@ function createHandler(dir, static) {
86102

87103
var isBase64 =
88104
request.body &&
89-
!(request.headers["content-type"] || "").match(/text|application|multipart\/form-data/);
105+
!(request.headers['content-type'] || '').match(
106+
/text|application|multipart\/form-data/
107+
);
90108
var lambdaRequest = {
91109
path: request.path,
92110
httpMethod: request.method,
93111
queryStringParameters: queryString.parse(request.url.split(/\?(.+)/)[1]),
94112
headers: request.headers,
95-
body: isBase64 ? Buffer.from(request.body.toString(), "utf8").toString("base64") : request.body,
113+
body: isBase64
114+
? Buffer.from(request.body.toString(), 'utf8').toString('base64')
115+
: request.body,
96116
isBase64Encoded: isBase64
97117
};
98118

99119
var callback = createCallback(response);
100120

101-
var promise = handler.handler(lambdaRequest, { clientContext: buildClientContext(request.headers) || {} }, callback);
121+
var promise = handler.handler(
122+
lambdaRequest,
123+
{ clientContext: buildClientContext(request.headers) || {} },
124+
callback
125+
);
102126
promiseCallback(promise, callback);
103127
};
104128
}
@@ -107,20 +131,22 @@ exports.listen = function(port, static) {
107131
var config = conf.load();
108132
var app = express();
109133
var dir = config.build.functions || config.build.Functions;
110-
app.use(bodyParser.raw({limit: "6mb"}));
111-
app.use(bodyParser.text({limit: "6mb", type: "*/*"}));
112-
app.use(expressLogging(console, {
113-
blacklist: ["/favicon.ico"],
114-
}));
134+
app.use(bodyParser.raw({ limit: '6mb' }));
135+
app.use(bodyParser.text({ limit: '6mb', type: '*/*' }));
136+
app.use(
137+
expressLogging(console, {
138+
blacklist: ['/favicon.ico']
139+
})
140+
);
115141

116-
app.get("/favicon.ico", function(req, res) {
142+
app.get('/favicon.ico', function(req, res) {
117143
res.status(204).end();
118144
});
119-
app.all("*", createHandler(dir, static));
145+
app.all('*', createHandler(dir, static));
120146

121147
app.listen(port, function(err) {
122148
if (err) {
123-
console.error("Unable to start lambda server: ", err);
149+
console.error('Unable to start lambda server: ', err);
124150
process.exit(1);
125151
}
126152

0 commit comments

Comments
 (0)