Skip to content

Commit

Permalink
feat: Set CF-IPCountry header from env var, close #80 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Tomin authored Feb 4, 2022
1 parent 3146f22 commit 4b8b0e1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ $ nodemon --watch /path/to/worker.js --signal SIGHUP --exec 'cloudflare-worker-l
* Cloudflare [event.passThroughOnException()](https://workers.cloudflare.com/docs/reference/workers-concepts/fetch-event-lifecycle/#passthroughonexception) for runtime exception handling
* Cloudflare Environment Variables and Secrets loaded from a wrangler.toml
* Workers Sites
* Set cloudflare `CF-IPCountry` header value from `COUNTRY` environment variable (default is `DEV`)
* ... this list should probably have more things

## Contributors
Expand Down
26 changes: 25 additions & 1 deletion app/__tests__/server_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe("server", () => {
.send(body)
.expect(200, 'POST');
});

it("can init a minio client", async () => {
const app = createApp(
'addEventListener("fetch", (e) => e.respondWith(new Response("success")))',
Expand All @@ -115,5 +116,28 @@ describe("server", () => {
kvStores: [] // leave this empty so the client doesn't attempt to make requests
}
);
})
});

it("config country overrides cf-ipcountry header ", async () => {
const app = createApp(
'addEventListener("fetch", (e) => e.respondWith(new Response("hello", {status: 200, headers: {"return-country": e.request.headers.get("cf-ipcountry")}})))',
{country: 'some-country'}
);

await supertest(app)
.get("/some-route")
.expect(200, "hello")
.expect("return-country", "some-country");
});

it("set DEV as cf-ipcountry header by default", async () => {
const app = createApp(
'addEventListener("fetch", (e) => e.respondWith(new Response("hello", {status: 200, headers: {"return-country": e.request.headers.get("cf-ipcountry")}})))'
);

await supertest(app)
.get("/some-route")
.expect(200, "hello")
.expect("return-country", "DEV");
});
});
7 changes: 4 additions & 3 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const { pipeline } = require('stream');
const { promisify } = require('util');
const streamPipeline = promisify(pipeline);

async function callWorker(worker, req, res) {
async function callWorker(worker, req, res, opts) {
const url = req.protocol + "://" + req.get("host") + req.originalUrl;

const response = await worker.executeFetchEvent(url, {
headers: req.headers,
method: req.method,
body: ["GET", "HEAD"].includes(req.method) ? undefined : req.body,
ip: req.connection.remoteAddress.split(":").pop()
ip: req.connection.remoteAddress.split(":").pop(),
country: opts.country
});

res.status(response.status);
Expand Down Expand Up @@ -49,7 +50,7 @@ function createApp(workerContent, opts = {}) {
const origin = req.headers.host;
workersByOrigin[origin] = workersByOrigin[origin] || new Worker(origin, workerContent, { ...opts, kvStores });
const worker = workersByOrigin[origin];
await callWorker(worker, req, res);
await callWorker(worker, req, res, {country: opts.country});
} catch (e) {
console.warn(e);
res.status(520);
Expand Down
2 changes: 1 addition & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if (cluster.isMaster) {
env["__STATIC_CONTENT_MANIFEST"] = new Proxy({}, {get: (target, prop) => prop});
}
}
const opts = { upstreamHost: process.argv[3], kvStores, kvStore, env };
const opts = { upstreamHost: process.argv[3], kvStores, kvStore, env, country: process.env.COUNTRY };
const app = createApp(fs.readFileSync(process.argv[2]), opts);

process.on("SIGHUP", () => {
Expand Down

0 comments on commit 4b8b0e1

Please sign in to comment.