Skip to content

Commit fae056d

Browse files
committed
accept script in base64 encoding
1 parent 5c7d5f3 commit fae056d

File tree

4 files changed

+76
-43
lines changed

4 files changed

+76
-43
lines changed

ecosystem.config.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
module.exports = {
2-
apps : [{
3-
name: "dgraph-lambda",
4-
script: "./dist/index.js",
5-
instances: 4,
6-
watch: ["script.js"],
7-
exec_mode : "cluster",
8-
env: {
9-
NODE_ENV: "development",
10-
},
11-
env_production: {
12-
NODE_ENV: "production",
13-
}
14-
}]
15-
}
2+
apps: [
3+
{
4+
name: "dgraph-lambda",
5+
script: "./dist/index.js",
6+
instances: 4,
7+
exp_backoff_restart_delay: 100,
8+
max_memory_restart: "32M",
9+
watch: ["./script/script.js"],
10+
watch_options: {
11+
followSymlinks: false,
12+
},
13+
exec_mode: "cluster",
14+
env: {
15+
NODE_ENV: "development",
16+
},
17+
env_production: {
18+
NODE_ENV: "production",
19+
},
20+
},
21+
],
22+
};

script.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

script/script.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fullName = ({ parent: { firstName, lastName } }) =>
2+
`${firstName} ${lastName}`;
3+
4+
async function todoTitles({ graphql }) {
5+
const results = await graphql("{ queryTodo { title } }");
6+
return results.data.queryTodo.map((t) => t.title);
7+
}
8+
9+
self.addGraphQLResolvers({
10+
"User.fullName": fullName,
11+
"Query.todoTitles": todoTitles,
12+
});
13+
14+
async function reallyComplexDql({ parents, dql }) {
15+
const ids = parents.map((p) => p.id);
16+
const someComplexResults = await dql.query(
17+
`really-complex-query-here with ${ids}`
18+
);
19+
return parents.map((parent) => someComplexResults[parent.id]);
20+
}
21+
22+
self.addMultiParentGraphQLResolvers({
23+
"User.reallyComplexProperty": reallyComplexDql,
24+
});

src/index.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
import cluster from 'cluster';
2-
import fs from 'fs'
3-
import { scriptToExpress } from './script-to-express';
1+
import cluster from "cluster";
2+
import fs from "fs";
3+
import { scriptToExpress } from "./script-to-express";
4+
import atob from "atob";
5+
import btoa from "btoa";
6+
7+
function base64Decode(str: string) {
8+
try {
9+
const original = str.trim();
10+
const decoded = atob(original);
11+
return btoa(decoded) === original ? decoded : "";
12+
} catch (err) {
13+
console.error(err);
14+
return "";
15+
}
16+
}
417

518
async function startServer() {
6-
const source = (await fs.promises.readFile(process.env.SCRIPT_PATH || "./script.js")).toString()
7-
const app = scriptToExpress(source);
19+
const source = (
20+
await fs.promises.readFile(process.env.SCRIPT_PATH || "./script/script.js")
21+
).toString();
22+
const script = base64Decode(source) || source;
23+
24+
const app = scriptToExpress(script);
825
const port = process.env.PORT || "8686";
9-
const server = app.listen(port, () => console.log("Server Listening on port " + port + "!"))
10-
cluster.on('disconnect', () => server.close())
26+
const server = app.listen(port, () =>
27+
console.log("Server Listening on port " + port + "!")
28+
);
29+
cluster.on("disconnect", () => server.close());
30+
31+
process.on("SIGINT", () => {
32+
server.close();
33+
process.exit(0);
34+
});
1135
}
1236

1337
startServer();
14-

0 commit comments

Comments
 (0)