mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-03 12:31:54 +08:00
Perf: further speed up infra
This commit is contained in:
34
Build/lib/get-gorhill-publicsuffix.js
Normal file
34
Build/lib/get-gorhill-publicsuffix.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const { toASCII } = require('punycode/');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const publicSuffixPath = path.resolve(__dirname, '../../node_modules/.cache/public_suffix-list_dat.txt');
|
||||
const getPublicSuffixListDat = () => {
|
||||
if (fs.existsSync(publicSuffixPath)) {
|
||||
return fs.promises.readFile(publicSuffixPath, 'utf-8');
|
||||
}
|
||||
console.log('public_suffix_list.dat not found, fetch directly from remote.');
|
||||
return fetch('https://publicsuffix.org/list/public_suffix_list.dat').then(r => r.text());
|
||||
};
|
||||
|
||||
const getGorhillPublicSuffix = async () => {
|
||||
const customFetch = async (url) => {
|
||||
const buf = await fs.promises.readFile(url);
|
||||
return {
|
||||
arrayBuffer() { return Promise.resolve(buf.buffer); }
|
||||
};
|
||||
};
|
||||
|
||||
const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
|
||||
getPublicSuffixListDat(),
|
||||
import('gorhill-publicsuffixlist')
|
||||
]);
|
||||
|
||||
gorhill.parse(publicSuffixListDat, toASCII);
|
||||
await gorhill.enableWASM({ customFetch });
|
||||
|
||||
return gorhill;
|
||||
};
|
||||
|
||||
const getGorhillPublicSuffixPromise = getGorhillPublicSuffix();
|
||||
module.exports.getGorhillPublicSuffixPromise = getGorhillPublicSuffixPromise;
|
||||
Reference in New Issue
Block a user