Skip to content

Commit

Permalink
feat(KVStore): Expose the KV Store from create app (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
gja authored Jan 2, 2019
1 parent b900e2b commit 5844ace
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
15 changes: 15 additions & 0 deletions app/__tests__/server_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,19 @@ describe("server", () => {
.get("/some-route")
.expect(200, "127.0.0.1");
});

it("creates stores and passes it to the worker", async () => {
const app = createApp(
'addEventListener("fetch", (e) => e.respondWith(MYSTORE.get("key").then(v => new Response(v))))',
{
kvStores: ["MYSTORE"]
}
);

await app.stores.MYSTORE.put("key", "value");

await supertest(app)
.get("/some-route")
.expect(200, "value");
});
});
2 changes: 1 addition & 1 deletion app/__tests__/worker_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe("Workers", () => {
const worker = new Worker(
"foo.com",
`addEventListener("fetch", (e) => {e.respondWith(new Response("foo")); e.waitUntil(MYSTORE.put("foo", "bar"))})`,
{ kvStores: ["MYSTORE"], kvStoreFactory: kvStoreFactory }
{ kvStores: { MYSTORE: kvStoreFactory.getClient("MYSTORE") } }
);

const response = await worker.executeFetchEvent(`http://foo.com/blah`);
Expand Down
15 changes: 11 additions & 4 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ async function callWorker(worker, req, res) {
res.end(Buffer.from(data), "binary");
}

function createApp(workerContent, opts) {
function buildKVStores(kvStoreFactory, kvStores) {
return kvStores.reduce((acc, name) => {
acc[name] = kvStoreFactory.getClient(name);
return acc;
}, {});
}

function createApp(workerContent, opts = {}) {
let workersByOrigin = {};
const kvStoreFactory = new InMemoryKVStore();
const kvStores = buildKVStores(new InMemoryKVStore(), opts.kvStores || []);
const app = express();
app.use(bodyParser.raw({ type: "*/*" }));
app.use(async (req, res) => {
try {
const origin = req.headers.host;
workersByOrigin[origin] =
workersByOrigin[origin] || new Worker(origin, workerContent, { kvStoreFactory, ...opts });
workersByOrigin[origin] = workersByOrigin[origin] || new Worker(origin, workerContent, { ...opts, kvStores });
const worker = workersByOrigin[origin];
await callWorker(worker, req, res);
} catch (e) {
Expand All @@ -47,6 +53,7 @@ function createApp(workerContent, opts) {
opts = Object.assign({}, opts, newOpts);
workersByOrigin = {};
};
app.stores = kvStores;

return app;
}
Expand Down
11 changes: 2 additions & 9 deletions app/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ const btoa = require("btoa");
const crypto = new (require("node-webcrypto-ossl"))();
const { TextDecoder, TextEncoder } = require("util");

function buildKVStores(kvStoreFactory, kvStores) {
return kvStores.reduce((acc, name) => {
acc[name] = kvStoreFactory.getClient(name);
return acc;
}, {});
}

function chomp(str) {
return str.substr(0, str.length - 1);
}
Expand All @@ -40,14 +33,14 @@ function buildRequest(url, opts) {

class Worker {
constructor(origin, workerContents, opts = {}) {
const { upstreamHost, kvStores = [], kvStoreFactory = require("./in-memory-kv-store") } = opts;
const { upstreamHost, kvStores = {} } = opts;
this.listeners = {
fetch: e => e.respondWith(this.fetchUpstream(e.request))
};
this.upstreamHost = upstreamHost;
this.origin = origin;

this.evaluateWorkerContents(workerContents, buildKVStores(kvStoreFactory, kvStores));
this.evaluateWorkerContents(workerContents, kvStores);
}

evaluateWorkerContents(workerContents, kvStores) {
Expand Down

0 comments on commit 5844ace

Please sign in to comment.