lakutata
Version:
An IoC-based universal application framework.
741 lines (609 loc) • 18.8 kB
JavaScript
import { _ as e, a as t } from "../../../vendor/Package.1.mjs";
import r from "events";
import { K as s } from "../../../vendor/Package.65.mjs";
import { Component as o } from "../../lib/core/Component.mjs";
import { Configurable as i } from "../../decorators/di/Configurable.mjs";
import { As as a } from "../../lib/helpers/As.mjs";
import { CreateFileCacheAdapter as n } from "./adapters/CreateFileCacheAdapter.mjs";
import { CreateRedisCacheAdapter as c } from "./adapters/CreateRedisCacheAdapter.mjs";
import { CreateMemcacheCacheAdapter as m } from "./adapters/CreateMemcacheCacheAdapter.mjs";
import { CreateMongoCacheAdapter as l } from "./adapters/CreateMongoCacheAdapter.mjs";
import { CreateSqliteCacheAdapter as p } from "./adapters/CreateSqliteCacheAdapter.mjs";
import { CreatePostgresCacheAdapter as h } from "./adapters/CreatePostgresCacheAdapter.mjs";
import { CreateMysqlCacheAdapter as d } from "./adapters/CreateMysqlCacheAdapter.mjs";
import { D as u } from "../../../vendor/Package.2.mjs";
import { FileCacheOptions as y } from "./options/FileCacheOptions.mjs";
import { RedisCacheOptions as f } from "./options/RedisCacheOptions.mjs";
import { MemcacheCacheOptions as j } from "./options/MemcacheCacheOptions.mjs";
import { MongoCacheOptions as b } from "./options/MongoCacheOptions.mjs";
import { SqliteCacheOptions as v } from "./options/SqliteCacheOptions.mjs";
import { PostgresCacheOptions as g } from "./options/PostgresCacheOptions.mjs";
import { MysqlCacheOptions as k } from "./options/MysqlCacheOptions.mjs";
import "buffer";
import "../../../vendor/Package.3.mjs";
import "../../lib/base/async-constructor/AsyncConstructor.mjs";
import "../../lib/base/async-constructor/Append.mjs";
import "../../lib/helpers/IsPromiseLike.mjs";
import "../../lib/helpers/IsPromise.mjs";
import "util/types";
import "../../decorators/di/Lifetime.mjs";
import "../../lib/base/internal/ObjectLifetime.mjs";
import "../../constants/DIMetadataKey.mjs";
import "../../../vendor/Package.4.mjs";
import "../../../vendor/Package.5.mjs";
import "../../exceptions/di/LifetimeLockedException.mjs";
import "../../lib/base/abstracts/Exception.mjs";
import "../../lib/base/internal/BasicInfo.mjs";
import "../../../vendor/Package.6.mjs";
import "../../../vendor/Package.7.mjs";
import "../../lib/base/internal/ThrowWarning.mjs";
import "../../lib/helpers/Templating.mjs";
import "../../lib/base/internal/CamelCase.mjs";
import "../../lib/helpers/NoCase.mjs";
import "../../exceptions/MethodNotFoundException.mjs";
import "../../lib/ioc/DependencyInjectionContainer.mjs";
import "util";
import "../../lib/ioc/Errors.mjs";
import "../../exceptions/di/DependencyInjectionException.mjs";
import "../../lib/base/internal/ConstructorSymbol.mjs";
import "node:crypto";
import "../../lib/base/internal/DataValidator.mjs";
import "node:util/types";
import "../../lib/validation/VLD.mjs";
import "../../../vendor/Package.11.mjs";
import "url";
import "../../../vendor/Package.10.mjs";
import "../../exceptions/dto/InvalidValueException.mjs";
import "../../../vendor/Package.9.mjs";
import "../../lib/helpers/IsHtml.mjs";
import "../../lib/helpers/IsXML.mjs";
import "../../constants/DTOMetadataKey.mjs";
import "../../lib/helpers/ObjectConstructor.mjs";
import "../../lib/helpers/ObjectParentConstructors.mjs";
import "../../lib/helpers/ObjectParentConstructor.mjs";
import "../../lib/helpers/ObjectPrototype.mjs";
import "../../lib/ioc/InjectionMode.mjs";
import "../../lib/ioc/Lifetime.mjs";
import "../../lib/ioc/ListModules.mjs";
import "../../../vendor/Package.8.mjs";
import "os";
import "path";
import "stream";
import "fs";
import "../../lib/ioc/Utils.mjs";
import "../../lib/ioc/FunctionTokenizer.mjs";
import "../../lib/ioc/LoadModules.mjs";
import "../../lib/ioc/Resolvers.mjs";
import "../../lib/ioc/ParamParser.mjs";
import "../../lib/base/internal/ObjectWeakRefs.mjs";
import "../../lib/base/internal/ConfigurableRecordsInjection.mjs";
import "../../lib/base/internal/ObjectConfiguration.mjs";
import "../../lib/base/internal/ObjectInjection.mjs";
import "../../lib/base/internal/ObjectContainer.mjs";
import "../../lib/helpers/DevNull.mjs";
import "../../decorators/dto/Expect.mjs";
import "../../decorators/dto/IndexSignature.mjs";
import "../../decorators/dto/Accept.mjs";
import "../../lib/base/internal/MethodValidation.mjs";
import "../../exceptions/dto/InvalidMethodAcceptException.mjs";
import "../../exceptions/dto/InvalidMethodReturnException.mjs";
import "../../lib/helpers/IsEmptyObject.mjs";
import "../../lib/base/EventEmitter.mjs";
import "../../lib/core/Provider.mjs";
import "../../../vendor/Package.13.mjs";
import "../../../vendor/Package.20.mjs";
import "constants";
import "../../../vendor/Package.18.mjs";
import "assert";
import "../../../vendor/Package.14.mjs";
import "./lib/IsDriverPackageInstalled.mjs";
import "./exceptions/CacheDriverNotFoundException.mjs";
import "../../lib/helpers/URLBuilder.mjs";
var P = new Map;
function w(e) {
return P.has(e);
}
function C(e) {
P.set(e, []);
}
function S(e) {
P.delete(e);
}
function x(e, t) {
const r = A(e);
r.push(t);
P.set(e, r);
}
function A(e) {
return P.get(e) ?? [];
}
async function I(e) {
return new Promise(((t, r) => {
const s = {
resolve: t,
reject: r
};
x(e, s);
}));
}
function M(e) {
const t = A(e);
S(e);
return t;
}
function O(e) {
const {key: t, error: r, result: s} = e;
for (const e of M(t)) {
if (r) {
e.reject(r);
} else {
e.resolve(s);
}
}
}
async function L(e, t) {
if (!w(e)) {
C(e);
try {
const r = await Promise.resolve(t());
O({
key: e,
result: r
});
return r;
} catch (t) {
O({
key: e,
error: t
});
throw t;
}
}
return I(e);
}
function T(e) {
return e !== null && typeof e === "object" && !Array.isArray(e);
}
function _(e, ...t) {
return typeof e === "function" ? e(...t) : e;
}
function D(e, t) {
return typeof e === "number" && typeof t === "number" ? e < t : false;
}
var E = class {
opts;
namespace;
_cache;
constructor(e) {
this._cache = e;
}
async get(e) {
const t = await this._cache.get(e);
if (t !== void 0 && t !== null) {
return t;
}
return void 0;
}
async set(e, t, r) {
await this._cache.set(e, t, r);
return true;
}
async delete(e) {
await this._cache.del(e);
return true;
}
async clear() {
return this._cache.reset?.();
}
async has(e) {
const t = await this._cache.get(e);
if (t) {
return true;
}
return false;
}
async getMany(e) {
return this._cache.mget(...e).then((e => e.map((e => e))));
}
async deleteMany(e) {
await this._cache.mdel(...e);
return true;
}
on(e, t) {
this._cache.on?.(e, t);
return this;
}
async disconnect() {
await (this._cache.disconnect?.());
}
};
var B = e => {
const t = new r;
const o = new s;
o.serialize = void 0;
o.deserialize = void 0;
const i = e?.stores?.length ? e.stores : [ o ];
const a = e?.nonBlocking ?? false;
const n = e?.cacheId ?? Math.random().toString(36).slice(2);
const c = async e => {
let r;
if (a) {
try {
r = await Promise.race(i.map((async t => t.get(e))));
if (r === void 0) {
return void 0;
}
} catch (r) {
t.emit("get", {
key: e,
error: r
});
}
} else {
for (const s of i) {
try {
const o = await s.get(e);
if (o !== void 0) {
r = o;
t.emit("get", {
key: e,
value: r
});
break;
}
} catch (r) {
t.emit("get", {
key: e,
error: r
});
}
}
}
return r;
};
const m = async e => {
const t = [];
for (const r of e) {
const e = await c(r);
t.push(e);
}
return t;
};
const l = async e => {
let r;
if (a) {
try {
r = await Promise.race(i.map((async t => t.get(e, {
raw: true
}))));
if (r === void 0) {
return void 0;
}
} catch (r) {
t.emit("ttl", {
key: e,
error: r
});
}
} else {
for (const s of i) {
try {
const o = await s.get(e, {
raw: true
});
if (o !== void 0) {
r = o;
t.emit("ttl", {
key: e,
value: r
});
break;
}
} catch (r) {
t.emit("ttl", {
key: e,
error: r
});
}
}
}
if (r?.expires) {
return r.expires;
}
return void 0;
};
const p = async (r, s, o, i) => {
try {
if (a) {
Promise.all(r.map((async t => t.set(s, o, i ?? e?.ttl))));
t.emit("set", {
key: s,
value: o
});
return o;
}
await Promise.all(r.map((async t => t.set(s, o, i ?? e?.ttl))));
t.emit("set", {
key: s,
value: o
});
return o;
} catch (e) {
t.emit("set", {
key: s,
value: o,
error: e
});
return Promise.reject(e);
}
};
const h = async (e, r) => {
const s = r.map((({key: e, value: t, ttl: r}) => ({
key: e,
value: t,
ttl: r
})));
try {
const s = [];
for (const t of r) {
s.push(...e.map((async e => e.set(t.key, t.value, t.ttl))));
}
if (a) {
Promise.all(s);
t.emit("mset", {
list: r
});
return r;
}
await Promise.all(s);
t.emit("mset", {
list: r
});
return r;
} catch (e) {
t.emit("mset", {
list: r,
error: e
});
return Promise.reject(e);
}
};
const d = async e => {
try {
if (a) {
Promise.all(i.map((async t => t.delete(e))));
t.emit("del", {
key: e
});
return true;
}
await Promise.all(i.map((async t => t.delete(e))));
t.emit("del", {
key: e
});
return true;
} catch (r) {
t.emit("del", {
key: e,
error: r
});
return Promise.reject(r);
}
};
const u = async e => {
try {
const r = [];
for (const t of e) {
r.push(...i.map((async e => e.delete(t))));
}
if (a) {
Promise.all(r);
t.emit("mdel", {
keys: e
});
return true;
}
await Promise.all(r);
t.emit("mdel", {
keys: e
});
return true;
} catch (r) {
t.emit("mdel", {
keys: e,
error: r
});
return Promise.reject(r);
}
};
const y = async () => {
try {
if (a) {
Promise.all(i.map((async e => e.clear())));
t.emit("clear");
return true;
}
await Promise.all(i.map((async e => e.clear())));
t.emit("clear");
return true;
} catch (e) {
t.emit("clear", e);
return Promise.reject(e);
}
};
const f = async (r, s, o, a) => L(`${n}::${r}`, (async () => {
let c;
let m;
let l = 0;
let h;
const {ttl: d, refreshThreshold: u, raw: y} = T(o) ? o : {
ttl: o,
refreshThreshold: a
};
const f = t => _(d, t) ?? e?.ttl;
for (;l < i.length; l++) {
try {
const e = await i[l].get(r, {
raw: true
});
if (e !== void 0) {
c = e.value;
m = e;
if (typeof e.expires === "number") {
h = Math.max(0, e.expires - Date.now());
}
break;
}
} catch {}
}
if (c === void 0) {
const e = await s();
const t = f(e);
await p(i, r, e, t);
return y ? {
value: e,
expires: Date.now() + t
} : e;
}
const j = D(h, _(u, c) ?? e?.refreshThreshold);
if (j) {
L(`+++${n}__${r}`, s).then((async s => {
try {
await p(e?.refreshAllStores ? i : i.slice(0, l + 1), r, s, f(s));
t.emit("refresh", {
key: r,
value: s
});
} catch (e) {
t.emit("refresh", {
key: r,
value: c,
error: e
});
}
})).catch((e => {
t.emit("refresh", {
key: r,
value: c,
error: e
});
}));
}
if (!j && l > 0) {
await p(i.slice(0, l), r, c, f(c));
}
return y ? m : c;
}));
const j = (e, r) => t.addListener(e, r);
const b = (e, r) => t.removeListener(e, r);
const v = async () => {
try {
await Promise.all(i.map((async e => e.disconnect())));
} catch (e) {
return Promise.reject(e);
}
};
const g = () => n;
return {
get: c,
mget: m,
ttl: l,
set: async (e, t, r) => p(i, e, t, r),
mset: async e => h(i, e),
del: d,
mdel: u,
clear: y,
wrap: f,
on: j,
off: b,
disconnect: v,
cacheId: g,
stores: i
};
};
const N = e => ({
class: Cacher,
stores: e?.stores,
ttl: e?.ttl,
refreshThreshold: e?.refreshThreshold,
refreshAllStores: e?.refreshAllStores,
nonBlocking: e?.nonBlocking,
cacheId: e?.cacheId
});
class Cacher extends o {
async init() {
const e = this.stores ? Array.isArray(this.stores) ? this.stores : [ this.stores ] : [];
const t = await Promise.all(e.map((e => {
switch (e.type) {
case "file":
return n(e);
case "redis":
return c(e);
case "memcache":
return m(e);
case "mongo":
return l(e);
case "sqlite":
return p(e);
case "postgres":
return h(e);
case "mysql":
return d(e);
default:
return undefined;
}
})));
const r = t.filter((e => !!e));
this.cache = B({
stores: r.length ? r : undefined,
ttl: this.ttl,
refreshThreshold: this.refreshThreshold,
refreshAllStores: this.refreshAllStores,
nonBlocking: this.nonBlocking,
cacheId: this.cacheId || this.$uuid
});
this.cache.on("set", (e => this.emit("set", e)));
this.cache.on("del", (e => this.emit("del", e)));
this.cache.on("clear", (e => this.emit("clear", e)));
this.cache.on("refresh", (e => this.emit("refresh", e)));
}
async destroy() {
await this.cache.disconnect();
}
async set(e, t, r) {
return await this.cache.set(e, t, r);
}
async multipleSet(e) {
return await this.cache.mset(e);
}
async get(e) {
return a(await this.cache.get(e));
}
async multipleGet(e) {
return await this.cache.mget(e);
}
async getTTL(e) {
const t = await this.cache.ttl(e);
if (t === undefined) return -1;
return t;
}
async del(e) {
return await this.cache.del(e);
}
async multipleDel(e) {
return await this.cache.mdel(e);
}
async clear() {
return await this.cache.clear();
}
on(e, t) {
return super.on(e, t);
}
}
e([ i(u.Alternatives(u.Array(u.Alternatives(y.Schema(), f.Schema(), j.Schema(), b.Schema(), v.Schema(), g.Schema(), k.Schema())), y.Schema(), f.Schema(), j.Schema(), b.Schema(), v.Schema(), g.Schema(), k.Schema()).optional()), t("design:type", Object) ], Cacher.prototype, "stores", void 0);
e([ i(u.Number().positive().integer().optional()), t("design:type", Number) ], Cacher.prototype, "ttl", void 0);
e([ i(u.Number().positive().integer().optional()), t("design:type", Number) ], Cacher.prototype, "refreshThreshold", void 0);
e([ i(u.Boolean().optional()), t("design:type", Boolean) ], Cacher.prototype, "refreshAllStores", void 0);
e([ i(u.Boolean().optional()), t("design:type", Boolean) ], Cacher.prototype, "nonBlocking", void 0);
e([ i(u.String().optional()), t("design:type", String) ], Cacher.prototype, "cacheId", void 0);
export { N as BuildCacherOptions, Cacher };