Skip to content

Commit

Permalink
add nice warning message if body omitted
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Feb 3, 2019
1 parent 8db4bbc commit dcaac14
Showing 1 changed file with 57 additions and 31 deletions.
88 changes: 57 additions & 31 deletions lib/serve.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var express = require("express");
var bodyParser = require("body-parser");
var expressLogging = require("express-logging");
var queryString = require("querystring");
var path = require("path");
var conf = require("./config");
var jwtDecode = require("jwt-decode")
var express = require('express');
var bodyParser = require('body-parser');
var expressLogging = require('express-logging');
var queryString = require('querystring');
var path = require('path');
var conf = require('./config');
var jwtDecode = require('jwt-decode');

function handleErr(err, response) {
response.statusCode = 500;
response.write("Function invocation failed: " + err.toString());
response.write('Function invocation failed: ' + err.toString());
response.end();
console.log("Error during invocation: ", err);
console.log('Error during invocation: ', err);
return;
}

Expand All @@ -28,22 +28,35 @@ function createCallback(response) {
if (lambdaResponse.body) {
response.write(
lambdaResponse.isBase64Encoded
? Buffer.from(lambdaResponse.body, "base64")
? Buffer.from(lambdaResponse.body, 'base64')
: lambdaResponse.body
);
} else {
if (
process.env.CONTEXT !== 'production' ||
!process.env.SILENCE_EMPTY_LAMBDA_WARNING
)
console.log(
`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).
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'`
);
}
response.end();
}
};
}

function promiseCallback(promise, callback) {
if (!promise) return;
if (typeof promise.then !== "function") return;
if (typeof callback !== "function") return;
if (typeof promise.then !== 'function') return;
if (typeof callback !== 'function') return;

promise.then(
function(data) {callback(null, data)},
function(err) {callback(err, null)}
function(data) {
callback(null, data);
},
function(err) {
callback(err, null);
}
);
}

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

try {
return {
identity: { url: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL', token: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN' },
identity: {
url: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL',
token: 'NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN'
},
user: jwtDecode(parts[1])
};
} catch (e) {
Expand All @@ -67,14 +83,14 @@ function buildClientContext(headers) {
function createHandler(dir, static) {
return function(request, response) {
// handle proxies without path re-writes (http-servr)
var cleanPath = request.path.replace(/^\/.netlify\/functions/, "")
var cleanPath = request.path.replace(/^\/.netlify\/functions/, '');

var func = cleanPath.split("/").filter(function(e) {
var func = cleanPath.split('/').filter(function(e) {
return e;
})[0];
var module = path.join(process.cwd(), dir, func);
if(static) {
delete require.cache[require.resolve(module)]
if (static) {
delete require.cache[require.resolve(module)];
}
var handler;
try {
Expand All @@ -86,19 +102,27 @@ function createHandler(dir, static) {

var isBase64 =
request.body &&
!(request.headers["content-type"] || "").match(/text|application|multipart\/form-data/);
!(request.headers['content-type'] || '').match(
/text|application|multipart\/form-data/
);
var lambdaRequest = {
path: request.path,
httpMethod: request.method,
queryStringParameters: queryString.parse(request.url.split(/\?(.+)/)[1]),
headers: request.headers,
body: isBase64 ? Buffer.from(request.body.toString(), "utf8").toString("base64") : request.body,
body: isBase64
? Buffer.from(request.body.toString(), 'utf8').toString('base64')
: request.body,
isBase64Encoded: isBase64
};

var callback = createCallback(response);

var promise = handler.handler(lambdaRequest, { clientContext: buildClientContext(request.headers) || {} }, callback);
var promise = handler.handler(
lambdaRequest,
{ clientContext: buildClientContext(request.headers) || {} },
callback
);
promiseCallback(promise, callback);
};
}
Expand All @@ -107,20 +131,22 @@ exports.listen = function(port, static) {
var config = conf.load();
var app = express();
var dir = config.build.functions || config.build.Functions;
app.use(bodyParser.raw({limit: "6mb"}));
app.use(bodyParser.text({limit: "6mb", type: "*/*"}));
app.use(expressLogging(console, {
blacklist: ["/favicon.ico"],
}));
app.use(bodyParser.raw({ limit: '6mb' }));
app.use(bodyParser.text({ limit: '6mb', type: '*/*' }));
app.use(
expressLogging(console, {
blacklist: ['/favicon.ico']
})
);

app.get("/favicon.ico", function(req, res) {
app.get('/favicon.ico', function(req, res) {
res.status(204).end();
});
app.all("*", createHandler(dir, static));
app.all('*', createHandler(dir, static));

app.listen(port, function(err) {
if (err) {
console.error("Unable to start lambda server: ", err);
console.error('Unable to start lambda server: ', err);
process.exit(1);
}

Expand Down

0 comments on commit dcaac14

Please sign in to comment.