mirror of
https://github.com/Kornstalx/5etools-mirror-2.github.io.git
synced 2025-10-28 20:45:35 -05:00
v1.209.0
This commit is contained in:
50
node/util.js
50
node/util.js
@@ -1,4 +1,4 @@
|
||||
import * as fs from "fs";
|
||||
import fs from "fs";
|
||||
import https from "https";
|
||||
|
||||
function readJson (path) {
|
||||
@@ -96,33 +96,43 @@ class PatchLoadJson {
|
||||
|
||||
static _CACHE_HTTP_REQUEST = {};
|
||||
|
||||
static async _pLoadUrl (url) {
|
||||
if (!url.startsWith("http")) return this._CACHE_HTTP_REQUEST[url] = readJson(url);
|
||||
|
||||
if (process.env.HOMEBREW_REPO_DIR && DataUtil.brew.isUrlUnderDefaultRoot(url)) {
|
||||
const urlLocal = [
|
||||
process.env.HOMEBREW_REPO_DIR.trim(),
|
||||
DataUtil.brew.getUrlRelativeToDefaultRoot(url),
|
||||
]
|
||||
.join("/")
|
||||
.replace(/\/+/g, "/");
|
||||
return this._CACHE_HTTP_REQUEST[url] = readJson(urlLocal);
|
||||
}
|
||||
|
||||
return this._CACHE_HTTP_REQUEST[url] ||= new Promise((resolve, reject) => {
|
||||
https
|
||||
.get(
|
||||
url,
|
||||
resp => {
|
||||
let stack = "";
|
||||
resp.on("data", chunk => stack += chunk);
|
||||
resp.on("end", () => resolve(JSON.parse(stack)));
|
||||
},
|
||||
)
|
||||
.on("error", err => reject(err));
|
||||
});
|
||||
}
|
||||
|
||||
static patchLoadJson () {
|
||||
if (this._PATCH_STACK++) return;
|
||||
|
||||
PatchLoadJson._CACHED = PatchLoadJson._CACHED || DataUtil.loadJSON.bind(DataUtil);
|
||||
|
||||
const pLoadUrl = async url => {
|
||||
if (!url.startsWith("http")) return readJson(url);
|
||||
|
||||
return this._CACHE_HTTP_REQUEST[url] ||= new Promise((resolve, reject) => {
|
||||
https
|
||||
.get(
|
||||
url,
|
||||
resp => {
|
||||
let stack = "";
|
||||
resp.on("data", chunk => stack += chunk);
|
||||
resp.on("end", () => resolve(JSON.parse(stack)));
|
||||
},
|
||||
)
|
||||
.on("error", err => reject(err));
|
||||
});
|
||||
};
|
||||
|
||||
const loadJsonCache = {};
|
||||
DataUtil.loadJSON = (url) => {
|
||||
if (!loadJsonCache[url]) {
|
||||
loadJsonCache[url] = (async () => {
|
||||
const data = await pLoadUrl(url);
|
||||
const data = await this._pLoadUrl(url);
|
||||
await DataUtil.pDoMetaMerge(url, data, {isSkipMetaMergeCache: true});
|
||||
return data;
|
||||
})();
|
||||
@@ -131,7 +141,7 @@ class PatchLoadJson {
|
||||
};
|
||||
|
||||
PatchLoadJson._CACHED_RAW = PatchLoadJson._CACHED_RAW || DataUtil.loadRawJSON.bind(DataUtil);
|
||||
DataUtil.loadRawJSON = async (url) => pLoadUrl(url);
|
||||
DataUtil.loadRawJSON = async (url) => this._pLoadUrl(url);
|
||||
}
|
||||
|
||||
static unpatchLoadJson () {
|
||||
|
||||
Reference in New Issue
Block a user