Skip to content

Commit

Permalink
assign default values for missing env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Jan 18, 2022
1 parent 511ab03 commit 1bdd319
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
33 changes: 30 additions & 3 deletions src/core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const _ENV_VAR_MAPPINGS = {
runTime: {
name: "RUNTIME",
type: "string",
default: "node",
},
runTimeEnv: {
name: {
Expand All @@ -34,46 +35,61 @@ const _ENV_VAR_MAPPINGS = {
deno: "DENO_ENV",
},
type: "string",
default: {
worker: "development",
node: "development",
deno: "development",
},
},
cloudPlatform: {
name: "CLOUD_PLATFORM",
type: "string",
default: "fly",
},
logLevel: {
name: "LOG_LEVEL",
type: "string",
default: "debug",
},
blocklistUrl: {
name: "CF_BLOCKLIST_URL",
type: "string",
default: "https://dist.rethinkdns.com/blocklists/",
},
latestTimestamp: {
name: "CF_LATEST_BLOCKLIST_TIMESTAMP",
type: "string",
default: "1638959365361",
},
dnsResolverUrl: {
name: "CF_DNS_RESOLVER_URL",
type: "string",
default: "https://cloudflare-dns.com/dns-query",
},
onInvalidFlagStopProcessing: {
name: "CF_ON_INVALID_FLAG_STOPPROCESSING",
type: "boolean",
default: false,

This comment has been minimized.

Copy link
@amithm7

amithm7 Jan 18, 2022

Contributor

default values have to be string, as type conversion is done from string type.

if (type === "boolean") env[key] = env[key] === "true";

This comment has been minimized.

Copy link
@ignoramous

ignoramous Jan 18, 2022

Author Contributor

Thanks. Fixed: 65a3509

},
workerTimeout: {
name: "WORKER_TIMEOUT",
type: "number",
default: 10000, // 10s
},
fetchTimeout: {
name: "CF_BLOCKLIST_DOWNLOAD_TIMEOUT",
type: "number",
default: 5000, // 5s
},
tdNodecount: {
name: "TD_NODE_COUNT",
type: "number",
default: 42112224,
},
tdParts: {
name: "TD_PARTS",
type: "number",
default: 2,
},

// set to on - off aggressive cache plugin
Expand All @@ -99,6 +115,7 @@ function _getRuntimeEnv(runtime) {
for (const [key, mappedKey] of Object.entries(_ENV_VAR_MAPPINGS)) {
let name = null;
let type = null;
let val = null;

if (typeof mappedKey !== "object") continue;

Expand All @@ -107,6 +124,12 @@ function _getRuntimeEnv(runtime) {
} else {
name = mappedKey.name;
}
if (typeof mappedKey.name === "object") {
val = mappedKey.default[runtime];
} else {
val = mappedKey.default;
}

type = mappedKey.type;

if (!type) {
Expand All @@ -120,9 +143,13 @@ function _getRuntimeEnv(runtime) {
else if (runtime === "worker") env[key] = globalThis[name];
else throw new Error(`unsupported runtime: ${runtime}`);

// Type conversion & type safety
// env. variables are assumed strings by default, so they are converted
// to the specified type from "string" type (exclusively), if necessary.
// assign default value when user-defined value is missing
if (env[key] === null || env[key] === undefined) {

This comment has been minimized.

Copy link
@amithm7

amithm7 Jan 18, 2022

Contributor

env[key] == null

This comment has been minimized.

Copy link
@ignoramous

ignoramous Jan 18, 2022

Author Contributor

linter complained, and so, I thought why not be explicit instead of shutting down the linter...

This comment has been minimized.

Copy link
@amithm7

amithm7 Jan 18, 2022

Contributor

== works for null

"eqeqeq": ["error", "smart"],

https://eslint.org/docs/rules/eqeqeq#smart

This comment has been minimized.

Copy link
@ignoramous

ignoramous Jan 19, 2022

Author Contributor

I think my local eslint is messed up.

Anywho, doing this from a fresher git clone worked: 33e9107

console.warn(key, "env[key] default value:", val);
env[key] = val;
}

// env vars are strings by default, type cast as specified
if (type === "boolean") env[key] = env[key] === "true";
else if (type === "number") env[key] = Number(env[key]);
else if (type === "string") env[key] = env[key] || "";
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ webpack_config = "webpack.config.cjs"
WORKER_TIMEOUT = 10000
CLOUD_PLATFORM = "cloudflare"
LOG_LEVEL = "info"
CF_BLOCKLIST_DOWNLOAD_TIMEOUT = 400
CF_BLOCKLIST_DOWNLOAD_TIMEOUT = 5000
RUNTIME = "worker"
WORKER_ENV = "production"
CF_BLOCKLIST_URL = "https://cf.rethinkdns.com/blocklists/"
Expand Down

0 comments on commit 1bdd319

Please sign in to comment.