rebuild hrm

This commit is contained in:
2026-04-13 09:30:59 +08:00
parent 44a98495ab
commit cd31c74da8
12034 changed files with 548007 additions and 451275 deletions

View File

@@ -5,7 +5,8 @@
"use strict";
const nextTick = require("process").nextTick;
// eslint-disable-next-line n/prefer-global/process
const { nextTick } = require("process");
/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").PathLike} PathLike */
@@ -22,12 +23,12 @@ const nextTick = require("process").nextTick;
* @param {string} path path
* @returns {string} dirname
*/
const dirname = path => {
const dirname = (path) => {
let idx = path.length - 1;
while (idx >= 0) {
const c = path.charCodeAt(idx);
const char = path.charCodeAt(idx);
// slash or backslash
if (c === 47 || c === 92) break;
if (char === 47 || char === 92) break;
idx--;
}
if (idx < 0) return "";
@@ -50,18 +51,23 @@ const runCallbacks = (callbacks, err, result) => {
for (const callback of callbacks) {
try {
callback(err, result);
} catch (e) {
if (!error) error = e;
} catch (err) {
if (!error) error = err;
}
}
callbacks.length = 0;
if (error) throw error;
};
// eslint-disable-next-line jsdoc/reject-function-type
/** @typedef {Function} EXPECTED_FUNCTION */
// eslint-disable-next-line jsdoc/reject-any-type
/** @typedef {any} EXPECTED_ANY */
class OperationMergerBackend {
/**
* @param {Function | undefined} provider async method in filesystem
* @param {Function | undefined} syncProvider sync method in filesystem
* @param {EXPECTED_FUNCTION | undefined} provider async method in filesystem
* @param {EXPECTED_FUNCTION | undefined} syncProvider sync method in filesystem
* @param {BaseFileSystem} providerContext call context for the provider methods
*/
constructor(provider, syncProvider, providerContext) {
@@ -71,15 +77,18 @@ class OperationMergerBackend {
this._activeAsyncOperations = new Map();
this.provide = this._provider
? /**
* @param {PathLike | PathOrFileDescriptor} path path
* @param {object | FileSystemCallback<any> | undefined} options options
* @param {FileSystemCallback<any>=} callback callback
* @returns {any} result
*/
(path, options, callback) => {
? // Comment to align jsdoc
/**
* @param {PathLike | PathOrFileDescriptor} path path
* @param {object | FileSystemCallback<EXPECTED_ANY> | undefined} options options
* @param {FileSystemCallback<EXPECTED_ANY>=} callback callback
* @returns {EXPECTED_ANY} result
*/
(path, options, callback) => {
if (typeof options === "function") {
callback = /** @type {FileSystemCallback<any>} */ (options);
callback =
/** @type {FileSystemCallback<EXPECTED_ANY>} */
(options);
options = undefined;
}
if (
@@ -88,18 +97,18 @@ class OperationMergerBackend {
!(path instanceof URL) &&
typeof path !== "number"
) {
/** @type {Function} */
/** @type {EXPECTED_FUNCTION} */
(callback)(
new TypeError("path must be a string, Buffer, URL or number")
new TypeError("path must be a string, Buffer, URL or number"),
);
return;
}
if (options) {
return /** @type {Function} */ (this._provider).call(
return /** @type {EXPECTED_FUNCTION} */ (this._provider).call(
this._providerContext,
path,
options,
callback
callback,
);
}
let callbacks = this._activeAsyncOperations.get(path);
@@ -108,37 +117,38 @@ class OperationMergerBackend {
return;
}
this._activeAsyncOperations.set(path, (callbacks = [callback]));
/** @type {Function} */
/** @type {EXPECTED_FUNCTION} */
(provider)(
path,
/**
* @param {Error} err error
* @param {any} result result
* @param {EXPECTED_ANY} result result
*/
(err, result) => {
this._activeAsyncOperations.delete(path);
runCallbacks(callbacks, err, result);
}
},
);
}
}
: null;
this.provideSync = this._syncProvider
? /**
* @param {PathLike | PathOrFileDescriptor} path path
* @param {object=} options options
* @returns {any} result
*/
(path, options) => {
return /** @type {Function} */ (this._syncProvider).call(
? // Comment to align jsdoc
/**
* @param {PathLike | PathOrFileDescriptor} path path
* @param {object=} options options
* @returns {EXPECTED_ANY} result
*/
(path, options) =>
/** @type {EXPECTED_FUNCTION} */ (this._syncProvider).call(
this._providerContext,
path,
options
);
}
options,
)
: null;
}
purge() {}
purgeParent() {}
}
@@ -169,16 +179,16 @@ const STORAGE_MODE_ASYNC = 2;
/**
* @callback Provide
* @param {PathLike | PathOrFileDescriptor} path path
* @param {any} options options
* @param {FileSystemCallback<any>} callback callback
* @param {EXPECTED_ANY} options options
* @param {FileSystemCallback<EXPECTED_ANY>} callback callback
* @returns {void}
*/
class CacheBackend {
/**
* @param {number} duration max cache duration of items
* @param {function | undefined} provider async method
* @param {function | undefined} syncProvider sync method
* @param {EXPECTED_FUNCTION | undefined} provider async method
* @param {EXPECTED_FUNCTION | undefined} syncProvider sync method
* @param {BaseFileSystem} providerContext call context for the provider methods
*/
constructor(duration, provider, syncProvider, providerContext) {
@@ -186,9 +196,9 @@ class CacheBackend {
this._provider = provider;
this._syncProvider = syncProvider;
this._providerContext = providerContext;
/** @type {Map<string, FileSystemCallback<any>[]>} */
/** @type {Map<string, FileSystemCallback<EXPECTED_ANY>[]>} */
this._activeAsyncOperations = new Map();
/** @type {Map<string, { err: Error | null, result?: any, level: Set<string> }>} */
/** @type {Map<string, { err: Error | null, result?: EXPECTED_ANY, level: Set<string> }>} */
this._data = new Map();
/** @type {Set<string>[]} */
this._levels = [];
@@ -204,16 +214,18 @@ class CacheBackend {
/** @type {number | undefined} */
this._nextDecay = undefined;
// eslint-disable-next-line no-warning-comments
// @ts-ignore
this.provide = provider ? this.provide.bind(this) : null;
// eslint-disable-next-line no-warning-comments
// @ts-ignore
this.provideSync = syncProvider ? this.provideSync.bind(this) : null;
}
/**
* @param {PathLike | PathOrFileDescriptor} path path
* @param {any} options options
* @param {FileSystemCallback<any>} callback callback
* @param {EXPECTED_ANY} options options
* @param {FileSystemCallback<EXPECTED_ANY>} callback callback
* @returns {void}
*/
provide(path, options, callback) {
@@ -232,11 +244,11 @@ class CacheBackend {
}
const strPath = typeof path !== "string" ? path.toString() : path;
if (options) {
return /** @type {Function} */ (this._provider).call(
return /** @type {EXPECTED_FUNCTION} */ (this._provider).call(
this._providerContext,
path,
options,
callback
callback,
);
}
@@ -246,7 +258,7 @@ class CacheBackend {
}
// Check in cache
let cacheEntry = this._data.get(strPath);
const cacheEntry = this._data.get(strPath);
if (cacheEntry !== undefined) {
if (cacheEntry.err) return nextTick(callback, cacheEntry.err);
return nextTick(callback, null, cacheEntry.result);
@@ -261,13 +273,13 @@ class CacheBackend {
this._activeAsyncOperations.set(strPath, (callbacks = [callback]));
// Run the operation
/** @type {Function} */
/** @type {EXPECTED_FUNCTION} */
(this._provider).call(
this._providerContext,
path,
/**
* @param {Error | null} err error
* @param {any} [result] result
* @param {EXPECTED_ANY=} result result
*/
(err, result) => {
this._activeAsyncOperations.delete(strPath);
@@ -277,18 +289,18 @@ class CacheBackend {
this._enterAsyncMode();
runCallbacks(
/** @type {FileSystemCallback<any>[]} */ (callbacks),
/** @type {FileSystemCallback<EXPECTED_ANY>[]} */ (callbacks),
err,
result
result,
);
}
},
);
}
/**
* @param {PathLike | PathOrFileDescriptor} path path
* @param {any} options options
* @returns {any} result
* @param {EXPECTED_ANY} options options
* @returns {EXPECTED_ANY} result
*/
provideSync(path, options) {
if (
@@ -301,10 +313,10 @@ class CacheBackend {
}
const strPath = typeof path !== "string" ? path.toString() : path;
if (options) {
return /** @type {Function} */ (this._syncProvider).call(
return /** @type {EXPECTED_FUNCTION} */ (this._syncProvider).call(
this._providerContext,
path,
options
options,
);
}
@@ -314,7 +326,7 @@ class CacheBackend {
}
// Check in cache
let cacheEntry = this._data.get(strPath);
const cacheEntry = this._data.get(strPath);
if (cacheEntry !== undefined) {
if (cacheEntry.err) throw cacheEntry.err;
return cacheEntry.result;
@@ -329,9 +341,9 @@ class CacheBackend {
// When in idle mode, we will enter sync mode
let result;
try {
result = /** @type {Function} */ (this._syncProvider).call(
result = /** @type {EXPECTED_FUNCTION} */ (this._syncProvider).call(
this._providerContext,
path
path,
);
} catch (err) {
this._storeResult(strPath, /** @type {Error} */ (err), undefined);
@@ -350,7 +362,7 @@ class CacheBackend {
}
/**
* @param {string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>} [what] what to purge
* @param {(string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>)=} what what to purge
*/
purge(what) {
if (!what) {
@@ -368,7 +380,7 @@ class CacheBackend {
typeof what === "number"
) {
const strWhat = typeof what !== "string" ? what.toString() : what;
for (let [key, data] of this._data) {
for (const [key, data] of this._data) {
if (key.startsWith(strWhat)) {
this._data.delete(key);
data.level.delete(key);
@@ -378,7 +390,7 @@ class CacheBackend {
this._enterIdleMode();
}
} else {
for (let [key, data] of this._data) {
for (const [key, data] of this._data) {
for (const item of what) {
const strItem = typeof item !== "string" ? item.toString() : item;
if (key.startsWith(strItem)) {
@@ -395,7 +407,7 @@ class CacheBackend {
}
/**
* @param {string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>} [what] what to purge
* @param {(string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>)=} what what to purge
*/
purgeParent(what) {
if (!what) {
@@ -421,7 +433,7 @@ class CacheBackend {
/**
* @param {string} path path
* @param {Error | null} err error
* @param {any} result result
* @param {EXPECTED_ANY} result result
*/
_storeResult(path, err, result) {
if (this._data.has(path)) return;
@@ -434,7 +446,7 @@ class CacheBackend {
const nextLevel = (this._currentLevel + 1) % this._levels.length;
const decay = this._levels[nextLevel];
this._currentLevel = nextLevel;
for (let item of decay) {
for (const item of decay) {
this._data.delete(item);
}
decay.clear();
@@ -468,13 +480,14 @@ class CacheBackend {
this._runDecays();
// _runDecays may change the mode
if (
/** @type {STORAGE_MODE_IDLE | STORAGE_MODE_SYNC | STORAGE_MODE_ASYNC}*/
/** @type {STORAGE_MODE_IDLE | STORAGE_MODE_SYNC | STORAGE_MODE_ASYNC} */
(this._mode) === STORAGE_MODE_IDLE
)
) {
return;
}
timeout = Math.max(
0,
/** @type {number} */ (this._nextDecay) - Date.now()
/** @type {number} */ (this._nextDecay) - Date.now(),
);
break;
}
@@ -502,8 +515,8 @@ class CacheBackend {
}
/**
* @template {function} Provider
* @template {function} AsyncProvider
* @template {EXPECTED_FUNCTION} Provider
* @template {EXPECTED_FUNCTION} AsyncProvider
* @template FileSystem
* @param {number} duration duration in ms files are cached
* @param {Provider | undefined} provider provider
@@ -530,7 +543,7 @@ module.exports = class CachedInputFileSystem {
duration,
this.fileSystem.lstat,
this.fileSystem.lstatSync,
this.fileSystem
this.fileSystem,
);
const lstat = this._lstatBackend.provide;
this.lstat = /** @type {FileSystem["lstat"]} */ (lstat);
@@ -541,7 +554,7 @@ module.exports = class CachedInputFileSystem {
duration,
this.fileSystem.stat,
this.fileSystem.statSync,
this.fileSystem
this.fileSystem,
);
const stat = this._statBackend.provide;
this.stat = /** @type {FileSystem["stat"]} */ (stat);
@@ -552,7 +565,7 @@ module.exports = class CachedInputFileSystem {
duration,
this.fileSystem.readdir,
this.fileSystem.readdirSync,
this.fileSystem
this.fileSystem,
);
const readdir = this._readdirBackend.provide;
this.readdir = /** @type {FileSystem["readdir"]} */ (readdir);
@@ -565,7 +578,7 @@ module.exports = class CachedInputFileSystem {
duration,
this.fileSystem.readFile,
this.fileSystem.readFileSync,
this.fileSystem
this.fileSystem,
);
const readFile = this._readFileBackend.provide;
this.readFile = /** @type {FileSystem["readFile"]} */ (readFile);
@@ -582,18 +595,18 @@ module.exports = class CachedInputFileSystem {
(
/**
* @param {string} path path
* @param {FileSystemCallback<any>} callback
* @param {FileSystemCallback<EXPECTED_ANY>} callback callback
*/
(path, callback) => {
this.readFile(path, (err, buffer) => {
if (err) return callback(err);
if (!buffer || buffer.length === 0)
return callback(new Error("No file content"));
{return callback(new Error("No file content"));}
let data;
try {
data = JSON.parse(buffer.toString("utf-8"));
} catch (e) {
return callback(/** @type {Error} */ (e));
data = JSON.parse(buffer.toString("utf8"));
} catch (err_) {
return callback(/** @type {Error} */ (err_));
}
callback(null, data);
});
@@ -605,15 +618,15 @@ module.exports = class CachedInputFileSystem {
(
/**
* @param {string} path path
* @returns {any} result
* @returns {EXPECTED_ANY} result
*/
(path) => {
const buffer = this.readFileSync(path);
const data = JSON.parse(buffer.toString("utf-8"));
const data = JSON.parse(buffer.toString("utf8"));
return data;
}
)),
this.fileSystem
this.fileSystem,
);
const readJson = this._readJsonBackend.provide;
this.readJson = /** @type {FileSystem["readJson"]} */ (readJson);
@@ -626,7 +639,7 @@ module.exports = class CachedInputFileSystem {
duration,
this.fileSystem.readlink,
this.fileSystem.readlinkSync,
this.fileSystem
this.fileSystem,
);
const readlink = this._readlinkBackend.provide;
this.readlink = /** @type {FileSystem["readlink"]} */ (readlink);
@@ -639,7 +652,7 @@ module.exports = class CachedInputFileSystem {
duration,
this.fileSystem.realpath,
this.fileSystem.realpathSync,
this.fileSystem
this.fileSystem,
);
const realpath = this._realpathBackend.provide;
this.realpath = /** @type {FileSystem["realpath"]} */ (realpath);
@@ -650,7 +663,7 @@ module.exports = class CachedInputFileSystem {
}
/**
* @param {string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>} [what] what to purge
* @param {(string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>)=} what what to purge
*/
purge(what) {
this._statBackend.purge(what);