lakutata
Version:
An IoC-based universal application framework.
564 lines (538 loc) • 15.8 kB
JavaScript
/* Build Date: Mon Jan 05 2026 23:52:23 GMT+0800 (China Standard Time) */
import { i as e } from "../../../../vendor/Package.internal.524.mjs";
import { r as t } from "../../../../vendor/Package.internal.310.mjs";
import r from "os";
import i from "fs";
import s from "fs/promises";
import a from "events";
import n from "buffer";
import o from "path";
import "../../../../vendor/Package.internal.2.mjs";
import "../../../../vendor/Package.internal.3.mjs";
var l = {};
var c;
var p;
function h() {
if (p) return c;
p = 1;
var e = Object.defineProperty;
var t = Object.getOwnPropertyDescriptor;
var r = Object.getOwnPropertyNames;
var i = Object.prototype.hasOwnProperty;
var s = (t, r) => {
for (var i in r) e(t, i, {
get: r[i],
enumerable: true
});
};
var a = (s, a, n, o) => {
if (a && typeof a === "object" || typeof a === "function") {
for (let l of r(a)) if (!i.call(s, l) && l !== n) e(s, l, {
get: () => a[l],
enumerable: !(o = t(a, l)) || o.enumerable
});
}
return s;
};
var o = t => a(e({}, "__esModule", {
value: true
}), t);
var l = {};
s(l, {
defaultDeserialize: () => f,
defaultSerialize: () => d
});
c = o(l);
var h = n;
var u = (e, t = true) => {
if (e === void 0 || e === null) {
return "null";
}
if (typeof e === "string") {
return JSON.stringify(t && e.startsWith(":") ? `:${e}` : e);
}
if (h.Buffer.isBuffer(e)) {
return JSON.stringify(`:base64:${e.toString("base64")}`);
}
if (e?.toJSON) {
e = e.toJSON();
}
if (typeof e === "object") {
let r = "";
const i = Array.isArray(e);
r = i ? "[" : "{";
let s = true;
for (const a in e) {
const n = typeof e[a] === "function" || !i && e[a] === void 0;
if (!Object.hasOwn(e, a) || n) {
continue;
}
if (!s) {
r += ",";
}
s = false;
if (i) {
r += u(e[a], t);
} else if (e[a] !== void 0) {
r += `${u(a, false)}:${u(e[a], t)}`;
}
}
r += i ? "]" : "}";
return r;
}
return JSON.stringify(e);
};
var d = e => u(e, true);
var f = e => JSON.parse(e, (e, t) => {
if (typeof t === "string") {
if (t.startsWith(":base64:")) {
return h.Buffer.from(t.slice(8), "base64");
}
return t.startsWith(":") ? t.slice(1) : t;
}
return t;
});
return c;
}
var u = {};
var d = {};
var f;
function y() {
if (f) return d;
f = 1;
Object.defineProperty(d, "__esModule", {
value: true
});
d.SafeFilenameEncoder = void 0;
d.SafeFilenameEncoder = {
encode(e) {
return e.replace(/[^0-9a-zA-Z]/g, e => {
const t = e.charCodeAt(0);
if (t < 0 || t > 126) {
return e;
}
return "%" + e.charCodeAt(0).toString(16).padStart(2, "0");
}).replace(/%/g, "_");
},
decode(e) {
return decodeURIComponent(e.replace(/_/g, "%"));
}
};
return d;
}
var _;
function m() {
if (_) return u;
_ = 1;
Object.defineProperty(u, "__esModule", {
value: true
});
u.SeparatedFileHelper = void 0;
u.handleIOError = c;
const e = t;
const r = e.__importDefault(o);
const a = e.__importStar(i);
const n = e.__importStar(s);
const l = y();
function c(e) {
if (e.code === "ENOENT") {
return;
} else {
console.error(e);
}
}
class SeparatedFileHelper {
opts;
get lockFile() {
return r.default.join(this.opts.filename, ".lock");
}
get _lastExpireFile() {
return r.default.join(this.opts.filename, ".lastExpire");
}
constructor(e) {
this.opts = e;
}
getLastExpire() {
try {
return Number(a.readFileSync(this._lastExpireFile, "utf8"));
} catch (e) {
c(e);
}
return 0;
}
setLastExpire(e) {
try {
n.writeFile(this._lastExpireFile, e.toString());
} catch (e) {
c(e);
}
}
async get(e) {
try {
let t = await n.readFile(this._getKey(e));
let r = this.opts.deserialize(t);
return r;
} catch (e) {
c(e);
}
}
getSync(e) {
try {
let t = a.readFileSync(this._getKey(e));
let r = this.opts.deserialize(t);
return r;
} catch (e) {
c(e);
}
}
_getKey(e) {
return r.default.join(this.opts.filename, l.SafeFilenameEncoder.encode(e));
}
async set(e, t) {
try {
let r = this.opts.serialize(t);
await n.mkdir(this.opts.filename, {
recursive: true
});
await n.writeFile(this._getKey(e), r);
} catch (e) {
c(e);
}
}
async delete(e) {
try {
await n.unlink(this._getKey(e));
return true;
} catch (e) {
c(e);
return false;
}
}
async clear() {
await n.rm(this.opts.filename, {
recursive: true,
force: true
});
}
async clearExpire(e) {
try {
let t = await n.readdir(this.opts.filename);
for (const r of t) {
e(r);
}
await n.writeFile(this._lastExpireFile, Date.now().toString());
} catch (e) {
c(e);
}
}
async entries() {
return n.readdir(this.opts.filename).then(e => Promise.all(e.map(async e => {
e = l.SafeFilenameEncoder.decode(e);
return [ e, await this.get(e) ];
})));
}
}
u.SeparatedFileHelper = SeparatedFileHelper;
return u;
}
var v = {};
var g;
function F() {
if (g) return v;
g = 1;
Object.defineProperty(v, "__esModule", {
value: true
});
v.Field = void 0;
v.makeField = e;
class Field {
kv;
key;
defaults;
constructor(e, t, r) {
this.kv = e;
this.key = t;
this.defaults = r;
}
async get(e = this.defaults) {
return await this.kv.get(this.key) ?? e;
}
getSync(e = this.defaults) {
if (this.kv instanceof Map) {
return this.kv.get(this.key) ?? e;
} else if ("getSync" in this.kv) {
return this.kv.getSync(this.key) ?? e;
}
throw new Error("kv does not support getSync");
}
set(e, t) {
return this.kv.set(this.key, e, t);
}
delete() {
return this.kv.delete(this.key);
}
}
v.Field = Field;
function e(e, t, r) {
return new Field(e, t, r);
}
return v;
}
(function(e) {
Object.defineProperty(e, "__esModule", {
value: true
});
e.KeyvFile = e.defaultOpts = void 0;
const n = t;
const l = n.__importStar(r);
const c = n.__importStar(i);
const p = n.__importStar(s);
const u = n.__importDefault(a);
const d = h();
const f = n.__importDefault(o);
const y = m();
n.__exportStar(F(), e);
e.defaultOpts = {
deserialize: e => (0, d.defaultDeserialize)(e.toString()),
dialect: "redis",
expiredCheckDelay: 24 * 3600 * 1e3,
filename: `${l.tmpdir()}/keyv-file/default.json`,
serialize: d.defaultSerialize,
writeDelay: 100,
checkFileLock: false,
separatedFile: false
};
function _(e) {
return typeof e === "number";
}
class KeyvFile extends u.default {
ttlSupport=true;
namespace;
opts;
_data=new Map;
_lastExpire=0;
_separated;
constructor(t) {
super();
this.opts = Object.assign({}, e.defaultOpts, t);
this._separated = new y.SeparatedFileHelper(this.opts);
if (this.opts.checkFileLock) {
this.acquireFileLock();
}
if (this.opts.separatedFile) {
c.mkdirSync(this.opts.filename, {
recursive: true
});
this._lastExpire = this._separated.getLastExpire();
} else {
this._loadDataSync();
}
}
_loadDataSync() {
try {
const e = this.opts.deserialize(c.readFileSync(this.opts.filename));
if (!Array.isArray(e.cache)) {
const t = e.cache;
e.cache = [];
for (const r in t) {
if (t.hasOwnProperty(r)) {
e.cache.push([ r, t[r] ]);
}
}
}
this._data = new Map(e.cache);
this._lastExpire = e.lastExpire;
} catch (e) {
(0, y.handleIOError)(e);
this._data = new Map;
this._lastExpire = Date.now();
}
}
get _lockFile() {
if (this.opts.separatedFile) {
return this._separated.lockFile;
}
return this.opts.filename + ".lock";
}
acquireFileLock() {
try {
let e = c.openSync(this._lockFile, "wx");
c.closeSync(e);
process.on("SIGINT", () => {
this.releaseFileLock();
process.exit(0);
});
process.on("exit", () => {
this.releaseFileLock();
});
} catch (e) {
console.error(`[keyv-file] There is another process using this file`);
throw e;
}
}
releaseFileLock() {
try {
c.unlinkSync(this._lockFile);
} catch (e) {
(0, y.handleIOError)(e);
}
}
async get(e) {
if (this.opts.separatedFile) {
let t = await this._separated.get(e);
return this._getWithExpire(e, t);
}
return this.getSync(e);
}
getSync(e) {
if (this.opts.separatedFile) {
let t = this._separated.getSync(e);
return this._getWithExpire(e, t);
}
let t = void 0;
try {
const t = this._data.get(e);
return this._getWithExpire(e, t);
} catch (e) {
(0, y.handleIOError)(e);
}
return t;
}
async getMany(e) {
if (this.opts.separatedFile) {
return Promise.all(e.map(e => this.get(e)));
}
return e.map(e => this.getSync(e));
}
async set(e, t, r) {
if (r === 0) {
r = undefined;
}
t = {
expire: _(r) ? Date.now() + r : undefined,
value: t
};
this.clearExpire();
if (this.opts.separatedFile) {
return this._separated.set(e, t);
}
this._data.set(e, t);
return this.save();
}
async delete(e) {
if (this.opts.separatedFile) {
return this._separated.delete(e);
}
const t = this._data.delete(e);
await this.save();
return t;
}
async deleteMany(e) {
if (this.opts.separatedFile) {
let t = await Promise.all(e.map(e => this.delete(e)));
return t.every(e => e);
}
let t = e.every(e => this._data.delete(e));
await this.save();
return t;
}
async clear() {
if (this.opts.separatedFile) {
await this._separated.clear();
this._lastExpire = 0;
return true;
}
this._data = new Map;
this._lastExpire = Date.now();
return this.save();
}
async has(e) {
const t = await this.get(e);
return t !== undefined;
}
isExpired(e) {
return _(e.expire) && e.expire <= Date.now();
}
_getWithExpire(e, t) {
if (!t) {
return;
}
if (this.isExpired(t)) {
this.delete(e);
return;
}
return t.value;
}
clearExpire() {
const e = Date.now();
if (e - this._lastExpire <= this.opts.expiredCheckDelay) {
return;
}
this._lastExpire = e;
if (this.opts.separatedFile) {
this._separated.clearExpire(e => this.get(e));
return;
}
for (const e of this._data.keys()) {
const t = this._data.get(e);
this._getWithExpire(e, t);
}
}
async saveToDisk() {
const e = [];
for (const [t, r] of this._data) {
e.push([ t, r ]);
}
const t = this.opts.serialize({
cache: e,
lastExpire: this._lastExpire
});
await p.mkdir(f.default.dirname(this.opts.filename), {
recursive: true
});
return p.writeFile(this.opts.filename, t);
}
_savePromise;
save() {
this.clearExpire();
if (this._savePromise) {
return this._savePromise;
}
this._savePromise = new Promise((e, t) => {
setTimeout(() => {
this.saveToDisk().then(e, t).finally(() => {
this._savePromise = void 0;
});
}, this.opts.writeDelay);
});
return this._savePromise;
}
disconnect() {
return Promise.resolve();
}
async* iterator(e) {
let t = this.opts.separatedFile ? await this._separated.entries() : this._data.entries();
for (const [r, i] of t) {
if (r === undefined || i === undefined) {
continue;
}
if (!e || r.includes(e)) {
yield [ r, i.value ];
}
}
}
}
e.KeyvFile = KeyvFile;
e.default = KeyvFile;
})(l);
async function k(t) {
return new e({
store: new l.KeyvFile({
filename: t.filename,
expiredCheckDelay: t.expiredCheckDelay,
writeDelay: t.writeDelay
}),
namespace: t.namespace
});
}
export { k as CreateFileCacheAdapter };