Use fs API to load serialized PSL (#3797)

This commit is contained in:
Manish Jethani 2021-08-02 22:02:47 +05:30 committed by GitHub
parent 035987749b
commit 502e5b0ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 1 deletions

View File

@ -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) {