Skip to content

Commit

Permalink
refactor(node): m. No fetch function on prod.
Browse files Browse the repository at this point in the history
Now that node uses h2 & udp / tcp on fly.
  • Loading branch information
amithm7 committed Dec 21, 2021
1 parent 4a9732a commit 0129245
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/helpers/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const _ENV_VARIABLES = {
const _ENV_MAPPINGS = {
runTime: "RUNTIME",
runTimeEnv: {
worker: "WORKER_ENV",
Expand Down Expand Up @@ -52,7 +52,7 @@ function _loadEnv(runtime) {
console.info("Loading env. from runtime: ", runtime);

const env = {};
for (const [key, value] of Object.entries(_ENV_VARIABLES)) {
for (const [key, value] of Object.entries(_ENV_MAPPINGS)) {
let name = null;
let type = "string";

Expand Down Expand Up @@ -106,6 +106,13 @@ export default class EnvManager {
Number(WORKER_TIMEOUT) + Number(CF_BLOCKLIST_DOWNLOAD_TIMEOUT)
);

console.debug(
"Loaded env: ",
(runtime == "worker" &&
JSON.stringify(Object.fromEntries(this.envMap))) ||
Object.fromEntries(this.envMap)
);

globalThis.env = Object.fromEntries(this.envMap); // Global `env` namespace.
this.isLoaded = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/node/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (!globalThis.fetch) {
globalThis.fetch =
process.env.NODE_ENV !== "production"
? (await import("./util-dev.js")).fetchPlus
: fetch;
: null;
globalThis.Headers = Headers;
globalThis.Request = Request;
globalThis.Response = Response;
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if (typeof addEventListener !== "undefined") {

export function handleRequest(event) {
if (!envManager.isLoaded) envManager.loadEnv();
if (!globalThis.log)
if (!globalThis.log && !console.level)
globalThis.log = new Log(
env.logLevel,
env.runTimeEnv == "production" // Set Console level only in production.
Expand Down

3 comments on commit 0129245

@ignoramous
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we use fetch to download the blocklists, which now won't work?

 2021-12-21T23:31:38.284 app[117da83a] maa [info] TypeError: fetch is not a function
2021-12-21T23:31:38.284 app[117da83a] maa [info] at fileFetch (file:///node-dir/src/blocklist-wrapper/blocklistWrapper.js:193:21)
2021-12-21T23:31:38.284 app[117da83a] maa [info] at downloadBuildBlocklist (file:///node-dir/src/blocklist-wrapper/blocklistWrapper.js:163:18)
2021-12-21T23:31:38.284 app[117da83a] maa [info] at BlocklistWrapper.initBlocklistConstruction (file:///node-dir/src/blocklist-wrapper/blocklistWrapper.js:112:22)
2021-12-21T23:31:38.284 app[117da83a] maa [info] at BlocklistWrapper.RethinkModule (file:///node-dir/src/blocklist-wrapper/blocklistWrapper.js:44:27)
2021-12-21T23:31:38.284 app[117da83a] maa [info] at async proxyRequest (file:///node-dir/src/index.js:64:5)
2021-12-21T23:31:38.284 app[117da83a] maa [info] at async handleHTTPRequest (file:///node-dir/src/server.js:434:18) 

@ignoramous
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this enough of a revert to bring back node-fetch? 814842b

@amithm7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.