1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js

308 lines
7.3 KiB
JavaScript
Raw Normal View History

2023-12-18 13:12:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
class Storage {
constructor(duration) {
this.duration = duration;
this.running = new Map();
this.data = new Map();
this.levels = [];
2024-01-16 21:26:16 +08:00
if (duration > 0) {
this.levels.push(
new Set(),
new Set(),
new Set(),
new Set(),
new Set(),
new Set(),
new Set(),
new Set(),
new Set()
);
for (let i = 8000; i < duration; i += 500) this.levels.push(new Set());
2023-12-18 13:12:25 +08:00
}
this.count = 0;
this.interval = null;
this.needTickCheck = false;
this.nextTick = null;
this.passive = true;
this.tick = this.tick.bind(this);
}
ensureTick() {
2024-01-16 21:26:16 +08:00
if (!this.interval && this.duration > 0 && !this.nextTick)
this.interval = setInterval(
this.tick,
Math.floor(this.duration / this.levels.length)
);
2023-12-18 13:12:25 +08:00
}
finished(name, err, result) {
const callbacks = this.running.get(name);
this.running.delete(name);
2024-01-16 21:26:16 +08:00
if (this.duration > 0) {
2023-12-18 13:12:25 +08:00
this.data.set(name, [err, result]);
const levelData = this.levels[0];
this.count -= levelData.size;
levelData.add(name);
this.count += levelData.size;
this.ensureTick();
}
2024-01-16 21:26:16 +08:00
for (let i = 0; i < callbacks.length; i++) {
2023-12-18 13:12:25 +08:00
callbacks[i](err, result);
}
}
finishedSync(name, err, result) {
2024-01-16 21:26:16 +08:00
if (this.duration > 0) {
2023-12-18 13:12:25 +08:00
this.data.set(name, [err, result]);
const levelData = this.levels[0];
this.count -= levelData.size;
levelData.add(name);
this.count += levelData.size;
this.ensureTick();
}
}
provide(name, provider, callback) {
2024-01-16 21:26:16 +08:00
if (typeof name !== "string") {
2023-12-18 13:12:25 +08:00
callback(new TypeError("path must be a string"));
return;
}
let running = this.running.get(name);
2024-01-16 21:26:16 +08:00
if (running) {
2023-12-18 13:12:25 +08:00
running.push(callback);
return;
}
2024-01-16 21:26:16 +08:00
if (this.duration > 0) {
2023-12-18 13:12:25 +08:00
this.checkTicks();
const data = this.data.get(name);
2024-01-16 21:26:16 +08:00
if (data) {
2023-12-18 13:12:25 +08:00
return process.nextTick(() => {
callback.apply(null, data);
});
}
}
2024-01-16 21:26:16 +08:00
this.running.set(name, (running = [callback]));
2023-12-18 13:12:25 +08:00
provider(name, (err, result) => {
this.finished(name, err, result);
});
}
provideSync(name, provider) {
2024-01-16 21:26:16 +08:00
if (typeof name !== "string") {
2023-12-18 13:12:25 +08:00
throw new TypeError("path must be a string");
}
2024-01-16 21:26:16 +08:00
if (this.duration > 0) {
2023-12-18 13:12:25 +08:00
this.checkTicks();
const data = this.data.get(name);
2024-01-16 21:26:16 +08:00
if (data) {
if (data[0]) throw data[0];
2023-12-18 13:12:25 +08:00
return data[1];
}
}
let result;
try {
result = provider(name);
2024-01-16 21:26:16 +08:00
} catch (e) {
2023-12-18 13:12:25 +08:00
this.finishedSync(name, e);
throw e;
}
this.finishedSync(name, null, result);
return result;
}
tick() {
const decay = this.levels.pop();
2024-01-16 21:26:16 +08:00
for (let item of decay) {
2023-12-18 13:12:25 +08:00
this.data.delete(item);
}
this.count -= decay.size;
decay.clear();
this.levels.unshift(decay);
2024-01-16 21:26:16 +08:00
if (this.count === 0) {
2023-12-18 13:12:25 +08:00
clearInterval(this.interval);
this.interval = null;
this.nextTick = null;
return true;
2024-01-16 21:26:16 +08:00
} else if (this.nextTick) {
2023-12-18 13:12:25 +08:00
this.nextTick += Math.floor(this.duration / this.levels.length);
const time = new Date().getTime();
2024-01-16 21:26:16 +08:00
if (this.nextTick > time) {
2023-12-18 13:12:25 +08:00
this.nextTick = null;
2024-01-16 21:26:16 +08:00
this.interval = setInterval(
this.tick,
Math.floor(this.duration / this.levels.length)
);
2023-12-18 13:12:25 +08:00
return true;
}
2024-01-16 21:26:16 +08:00
} else if (this.passive) {
2023-12-18 13:12:25 +08:00
clearInterval(this.interval);
this.interval = null;
2024-01-16 21:26:16 +08:00
this.nextTick =
new Date().getTime() + Math.floor(this.duration / this.levels.length);
2023-12-18 13:12:25 +08:00
} else {
this.passive = true;
}
}
checkTicks() {
this.passive = false;
2024-01-16 21:26:16 +08:00
if (this.nextTick) {
while (!this.tick());
2023-12-18 13:12:25 +08:00
}
}
purge(what) {
2024-01-16 21:26:16 +08:00
if (!what) {
2023-12-18 13:12:25 +08:00
this.count = 0;
clearInterval(this.interval);
this.nextTick = null;
this.data.clear();
this.levels.forEach(level => {
level.clear();
});
2024-01-16 21:26:16 +08:00
} else if (typeof what === "string") {
for (let key of this.data.keys()) {
if (key.startsWith(what)) this.data.delete(key);
2023-12-18 13:12:25 +08:00
}
} else {
2024-01-16 21:26:16 +08:00
for (let i = what.length - 1; i >= 0; i--) {
2023-12-18 13:12:25 +08:00
this.purge(what[i]);
}
}
}
}
module.exports = class CachedInputFileSystem {
constructor(fileSystem, duration) {
this.fileSystem = fileSystem;
this._statStorage = new Storage(duration);
this._readdirStorage = new Storage(duration);
this._readFileStorage = new Storage(duration);
this._readJsonStorage = new Storage(duration);
this._readlinkStorage = new Storage(duration);
2024-01-16 21:26:16 +08:00
this._stat = this.fileSystem.stat
? this.fileSystem.stat.bind(this.fileSystem)
: null;
if (!this._stat) this.stat = null;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
this._statSync = this.fileSystem.statSync
? this.fileSystem.statSync.bind(this.fileSystem)
: null;
if (!this._statSync) this.statSync = null;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
this._readdir = this.fileSystem.readdir
? this.fileSystem.readdir.bind(this.fileSystem)
: null;
if (!this._readdir) this.readdir = null;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
this._readdirSync = this.fileSystem.readdirSync
? this.fileSystem.readdirSync.bind(this.fileSystem)
: null;
if (!this._readdirSync) this.readdirSync = null;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
this._readFile = this.fileSystem.readFile
? this.fileSystem.readFile.bind(this.fileSystem)
: null;
if (!this._readFile) this.readFile = null;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
this._readFileSync = this.fileSystem.readFileSync
? this.fileSystem.readFileSync.bind(this.fileSystem)
: null;
if (!this._readFileSync) this.readFileSync = null;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
if (this.fileSystem.readJson) {
2023-12-18 13:12:25 +08:00
this._readJson = this.fileSystem.readJson.bind(this.fileSystem);
2024-01-16 21:26:16 +08:00
} else if (this.readFile) {
2023-12-18 13:12:25 +08:00
this._readJson = (path, callback) => {
this.readFile(path, (err, buffer) => {
2024-01-16 21:26:16 +08:00
if (err) return callback(err);
2023-12-18 13:12:25 +08:00
let data;
try {
data = JSON.parse(buffer.toString("utf-8"));
2024-01-16 21:26:16 +08:00
} catch (e) {
2023-12-18 13:12:25 +08:00
return callback(e);
}
callback(null, data);
});
};
} else {
this.readJson = null;
}
2024-01-16 21:26:16 +08:00
if (this.fileSystem.readJsonSync) {
2023-12-18 13:12:25 +08:00
this._readJsonSync = this.fileSystem.readJsonSync.bind(this.fileSystem);
2024-01-16 21:26:16 +08:00
} else if (this.readFileSync) {
this._readJsonSync = path => {
2023-12-18 13:12:25 +08:00
const buffer = this.readFileSync(path);
const data = JSON.parse(buffer.toString("utf-8"));
return data;
};
} else {
this.readJsonSync = null;
}
2024-01-16 21:26:16 +08:00
this._readlink = this.fileSystem.readlink
? this.fileSystem.readlink.bind(this.fileSystem)
: null;
if (!this._readlink) this.readlink = null;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
this._readlinkSync = this.fileSystem.readlinkSync
? this.fileSystem.readlinkSync.bind(this.fileSystem)
: null;
if (!this._readlinkSync) this.readlinkSync = null;
2023-12-18 13:12:25 +08:00
}
stat(path, callback) {
this._statStorage.provide(path, this._stat, callback);
}
readdir(path, callback) {
this._readdirStorage.provide(path, this._readdir, callback);
}
readFile(path, callback) {
this._readFileStorage.provide(path, this._readFile, callback);
}
readJson(path, callback) {
this._readJsonStorage.provide(path, this._readJson, callback);
}
readlink(path, callback) {
this._readlinkStorage.provide(path, this._readlink, callback);
}
statSync(path) {
return this._statStorage.provideSync(path, this._statSync);
}
readdirSync(path) {
return this._readdirStorage.provideSync(path, this._readdirSync);
}
readFileSync(path) {
return this._readFileStorage.provideSync(path, this._readFileSync);
}
readJsonSync(path) {
return this._readJsonStorage.provideSync(path, this._readJsonSync);
}
readlinkSync(path) {
return this._readlinkStorage.provideSync(path, this._readlinkSync);
}
purge(what) {
this._statStorage.purge(what);
this._readdirStorage.purge(what);
this._readFileStorage.purge(what);
this._readlinkStorage.purge(what);
this._readJsonStorage.purge(what);
}
};