From 502e5b0ec87d988a64296ee4a0c2cdca63e58031 Mon Sep 17 00:00:00 2001 From: Manish Jethani Date: Mon, 2 Aug 2021 22:02:47 +0530 Subject: [PATCH] Use fs API to load serialized PSL (#3797) --- platform/nodejs/index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index 50f25427c..d423bea69 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -25,6 +25,12 @@ import { createRequire } from 'module'; +import { readFileSync } from 'fs'; +import { dirname, resolve } from 'path'; +import { fileURLToPath } from 'url'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + import './lib/punycode.js'; import './lib/publicsuffixlist/publicsuffixlist.js'; @@ -41,6 +47,10 @@ import { /******************************************************************************/ +function loadJSON(path) { + return JSON.parse(readFileSync(resolve(__dirname, path), 'utf8')); +} + function compileList(rawText, writer, options = {}) { const lineIter = new LineIterator(rawText); const parser = new StaticFilteringParser(true); @@ -106,8 +116,13 @@ function pslInit(raw) { // Use serialized version if available try { - serialized = require('./build/publicsuffixlist.json'); + // Use loadJSON() because require() would keep the string in memory. + serialized = loadJSON('build/publicsuffixlist.json'); } catch (error) { + if (process.env.npm_lifecycle_event !== 'install') { + // This should never happen except during package installation. + console.error(error); + } } if (serialized !== null) {