Skip to content

Commit 85e51ec

Browse files
committed
First commit
0 parents  commit 85e51ec

File tree

7 files changed

+3014
-0
lines changed

7 files changed

+3014
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

Diff for: bin/cmd.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var program = require("commander");
8+
var fs = require("fs");
9+
var path = require("path");
10+
var pkg = JSON.parse(
11+
fs.readFileSync(path.join(__dirname, "..", "package.json"))
12+
);
13+
var build = require("../lib/build");
14+
var serve = require("../lib/serve");
15+
16+
program.version(pkg.version);
17+
18+
program
19+
.command("serve <dir>")
20+
.description("serve and watch functions")
21+
.action(function(cmd, options) {
22+
console.log("Starting server");
23+
var server = serve.listen(9000);
24+
build.watch(cmd, function(err, stats) {
25+
if (err) {
26+
console.error(err);
27+
return;
28+
}
29+
30+
console.log(stats.compilation);
31+
stats.compilation.chunks.forEach(function(chunk) {
32+
server.clearCache(chunk.name);
33+
});
34+
35+
console.log(stats.toString({ color: true }));
36+
});
37+
});
38+
39+
program
40+
.command("build <dir>")
41+
.description("build functions")
42+
.action(function(cmd, options) {
43+
console.log("Building functions");
44+
build
45+
.run(cmd)
46+
.then(function(stats) {
47+
console.log(stats.toString({ color: true }));
48+
})
49+
.catch(function(err) {
50+
console.error(err);
51+
process.exit(1);
52+
});
53+
});
54+
55+
console.log("hmm", process.argv);
56+
program.parse(process.argv);

Diff for: lib/build.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var fs = require("fs");
2+
var path = require("path");
3+
var conf = require("./config");
4+
var webpack = require("webpack");
5+
6+
function webpackConfig(dir) {
7+
var config = conf.load();
8+
var webpackConfig = {
9+
module: {
10+
rules: [
11+
{
12+
test: /\.js?$/,
13+
exclude: /(node_modules|bower_components)/,
14+
use: {
15+
loader: "babel-loader",
16+
options: {
17+
cacheDirectory: true,
18+
presets: ["es2015"],
19+
plugins: [
20+
"transform-class-properties",
21+
"transform-object-assign",
22+
"transform-object-rest-spread"
23+
]
24+
}
25+
}
26+
}
27+
]
28+
},
29+
context: path.join(process.cwd(), dir),
30+
entry: {},
31+
target: "node",
32+
plugins: [new webpack.IgnorePlugin(/vertx/)],
33+
output: {
34+
path: path.join(
35+
process.cwd(),
36+
config.build.functions || config.build.Functions
37+
),
38+
filename: "[name].js",
39+
libraryTarget: "commonjs"
40+
},
41+
devtool: false
42+
};
43+
fs.readdirSync(path.join(process.cwd(), dir)).forEach(function(file) {
44+
if (file.match(/\.js$/)) {
45+
var name = file.replace(/\.js$/, "");
46+
webpackConfig.entry[name] = "./" + name;
47+
}
48+
});
49+
return webpackConfig;
50+
}
51+
52+
exports.run = function(dir) {
53+
return new Promise(function(resolve, reject) {
54+
webpack(webpackConfig(dir), function(err, stats) {
55+
if (err) {
56+
return reject(err);
57+
}
58+
resolve(stats);
59+
});
60+
});
61+
};
62+
63+
exports.watch = function(dir, cb) {
64+
var compiler = webpack(webpackConfig(dir));
65+
compiler.watch(webpackConfig(dir), cb);
66+
};

Diff for: lib/config.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var toml = require("toml");
2+
var fs = require("fs");
3+
var path = require("path");
4+
5+
exports.load = function() {
6+
var configPath = path.join(process.cwd(), "netlify.toml");
7+
if (!fs.existsSync(configPath)) {
8+
console.error(
9+
"No netlify.toml found. This is needed to configure the function settings"
10+
);
11+
process.exit(1);
12+
}
13+
14+
return toml.parse(fs.readFileSync(configPath));
15+
};

Diff for: lib/serve.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var express = require("express");
2+
var bodyParser = require("body-parser");
3+
var expressLogging = require("express-logging");
4+
var path = require("path");
5+
var base64 = require("base-64");
6+
var conf = require("./config");
7+
8+
function handleErr(err, response) {
9+
response.statusCode = 500;
10+
response.write("Function invocation failed: " + err.toString());
11+
response.end();
12+
console.log("Error during invocation: ", err);
13+
return;
14+
}
15+
16+
function createHandler(dir) {
17+
return function(request, response) {
18+
var func = request.path.split("/").filter(function(e) {
19+
return e;
20+
})[0];
21+
var module = path.join(process.cwd(), dir, func);
22+
var handler;
23+
try {
24+
handler = require(module);
25+
} catch (err) {
26+
handleErr(err, response);
27+
return;
28+
}
29+
30+
var isBase64 =
31+
request.body &&
32+
!(request.headers["content-type"] || "").match(/text|application/);
33+
var lambdaRequest = {
34+
path: request.path,
35+
httpMethod: request.method,
36+
queryStringParameters: request.query,
37+
headers: request.headers,
38+
body: isBase64 ? request.body : base64.encode(request.body),
39+
isBase64Encoded: isBase64
40+
};
41+
42+
handler.handler(lambdaRequest, {}, function(err, lambdaResponse) {
43+
if (err) {
44+
return handleErr(err, response);
45+
}
46+
47+
response.statusCode = lambdaResponse.statusCode;
48+
for (const key in lambdaResponse.headers) {
49+
response.setHeader(key, lambdaResponse.headers[key]);
50+
}
51+
response.write(
52+
lambdaResponse.isBase64Encoded
53+
? base64.decode(lambdaResponse.body)
54+
: lambdaResponse.body
55+
);
56+
response.end();
57+
});
58+
};
59+
}
60+
61+
exports.listen = function(port) {
62+
var config = conf.load();
63+
var app = express();
64+
var dir = config.build.functions || config.build.Functions;
65+
app.use(bodyParser.raw());
66+
app.use(expressLogging(console));
67+
68+
app.all("*", createHandler(dir));
69+
70+
app.listen(port, function(err) {
71+
if (err) {
72+
console.error("Unable to start lambda server: ", err);
73+
process.exit(1);
74+
}
75+
76+
console.log(`Lambda server is listening on ${port}`);
77+
});
78+
79+
return {
80+
clearCache: function(chunk) {
81+
var module = path.join(process.cwd(), dir, chunk);
82+
console.log("Clearing require cache: ", require.resolve(module));
83+
delete require.cache[require.resolve(module)];
84+
}
85+
};
86+
};

Diff for: package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "netlify-lambda",
3+
"version": "0.0.1",
4+
"description": "Build and serve lambda function with webpack compilation",
5+
"main": "bin/cmd.js",
6+
"bin": {
7+
"netlify-lambda": "bin/cmd.js"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"author": "Mathias Biilmann Christensen",
13+
"license": "MIT",
14+
"dependencies": {
15+
"babel-plugin-transform-class-properties": "^6.24.1",
16+
"babel-plugin-transform-object-assign": "^6.22.0",
17+
"babel-plugin-transform-object-rest-spread": "^6.26.0",
18+
"babel-preset-es2015": "^6.24.1",
19+
"base-64": "^0.1.0",
20+
"body-parser": "^1.18.2",
21+
"commander": "^2.11.0",
22+
"express": "^4.16.2",
23+
"express-logging": "^1.1.1",
24+
"toml": "^2.3.3",
25+
"webpack": "^3.8.1"
26+
}
27+
}

0 commit comments

Comments
 (0)