@kevisual/auth 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app.d.ts +36 -1
- package/dist/app.js +1642 -1
- package/package.json +5 -5
- package/src/index.ts +5 -1
- package/src/query.ts +53 -0
package/dist/app.js
CHANGED
|
@@ -398,6 +398,33 @@ function checkKeyLength(alg, key) {
|
|
|
398
398
|
}
|
|
399
399
|
|
|
400
400
|
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/asn1.js
|
|
401
|
+
var formatPEM = (b64, descriptor) => {
|
|
402
|
+
const newlined = (b64.match(/.{1,64}/g) || []).join(`
|
|
403
|
+
`);
|
|
404
|
+
return `-----BEGIN ${descriptor}-----
|
|
405
|
+
${newlined}
|
|
406
|
+
-----END ${descriptor}-----`;
|
|
407
|
+
};
|
|
408
|
+
var genericExport = async (keyType, keyFormat, key) => {
|
|
409
|
+
if (isKeyObject(key)) {
|
|
410
|
+
if (key.type !== keyType) {
|
|
411
|
+
throw new TypeError(`key is not a ${keyType} key`);
|
|
412
|
+
}
|
|
413
|
+
return key.export({ format: "pem", type: keyFormat });
|
|
414
|
+
}
|
|
415
|
+
if (!isCryptoKey(key)) {
|
|
416
|
+
throw new TypeError(invalidKeyInput(key, "CryptoKey", "KeyObject"));
|
|
417
|
+
}
|
|
418
|
+
if (!key.extractable) {
|
|
419
|
+
throw new TypeError("CryptoKey is not extractable");
|
|
420
|
+
}
|
|
421
|
+
if (key.type !== keyType) {
|
|
422
|
+
throw new TypeError(`key is not a ${keyType} key`);
|
|
423
|
+
}
|
|
424
|
+
return formatPEM(encodeBase64(new Uint8Array(await crypto.subtle.exportKey(keyFormat, key))), `${keyType.toUpperCase()} KEY`);
|
|
425
|
+
};
|
|
426
|
+
var toSPKI = (key) => genericExport("public", "spki", key);
|
|
427
|
+
var toPKCS8 = (key) => genericExport("private", "pkcs8", key);
|
|
401
428
|
var bytesEqual = (a, b) => {
|
|
402
429
|
if (a.byteLength !== b.length)
|
|
403
430
|
return false;
|
|
@@ -1069,6 +1096,45 @@ function checkKeyType(alg, key, usage) {
|
|
|
1069
1096
|
}
|
|
1070
1097
|
}
|
|
1071
1098
|
|
|
1099
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/key_to_jwk.js
|
|
1100
|
+
async function keyToJWK(key) {
|
|
1101
|
+
if (isKeyObject(key)) {
|
|
1102
|
+
if (key.type === "secret") {
|
|
1103
|
+
key = key.export();
|
|
1104
|
+
} else {
|
|
1105
|
+
return key.export({ format: "jwk" });
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
if (key instanceof Uint8Array) {
|
|
1109
|
+
return {
|
|
1110
|
+
kty: "oct",
|
|
1111
|
+
k: encode2(key)
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
if (!isCryptoKey(key)) {
|
|
1115
|
+
throw new TypeError(invalidKeyInput(key, "CryptoKey", "KeyObject", "Uint8Array"));
|
|
1116
|
+
}
|
|
1117
|
+
if (!key.extractable) {
|
|
1118
|
+
throw new TypeError("non-extractable CryptoKey cannot be exported as a JWK");
|
|
1119
|
+
}
|
|
1120
|
+
const { ext, key_ops, alg, use, ...jwk } = await crypto.subtle.exportKey("jwk", key);
|
|
1121
|
+
if (jwk.kty === "AKP") {
|
|
1122
|
+
jwk.alg = alg;
|
|
1123
|
+
}
|
|
1124
|
+
return jwk;
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/key/export.js
|
|
1128
|
+
async function exportSPKI(key) {
|
|
1129
|
+
return toSPKI(key);
|
|
1130
|
+
}
|
|
1131
|
+
async function exportPKCS8(key) {
|
|
1132
|
+
return toPKCS8(key);
|
|
1133
|
+
}
|
|
1134
|
+
async function exportJWK(key) {
|
|
1135
|
+
return keyToJWK(key);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1072
1138
|
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/lib/subtle_dsa.js
|
|
1073
1139
|
function subtleAlgorithm(alg, algorithm) {
|
|
1074
1140
|
const hash = `SHA-${alg.slice(-3)}`;
|
|
@@ -1785,6 +1851,103 @@ function decodeJwt(jwt) {
|
|
|
1785
1851
|
throw new JWTInvalid("Invalid JWT Claims Set");
|
|
1786
1852
|
return result;
|
|
1787
1853
|
}
|
|
1854
|
+
// node_modules/.pnpm/jose@6.1.3/node_modules/jose/dist/webapi/key/generate_key_pair.js
|
|
1855
|
+
function getModulusLengthOption(options) {
|
|
1856
|
+
const modulusLength = options?.modulusLength ?? 2048;
|
|
1857
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) {
|
|
1858
|
+
throw new JOSENotSupported("Invalid or unsupported modulusLength option provided, 2048 bits or larger keys must be used");
|
|
1859
|
+
}
|
|
1860
|
+
return modulusLength;
|
|
1861
|
+
}
|
|
1862
|
+
async function generateKeyPair(alg, options) {
|
|
1863
|
+
let algorithm;
|
|
1864
|
+
let keyUsages;
|
|
1865
|
+
switch (alg) {
|
|
1866
|
+
case "PS256":
|
|
1867
|
+
case "PS384":
|
|
1868
|
+
case "PS512":
|
|
1869
|
+
algorithm = {
|
|
1870
|
+
name: "RSA-PSS",
|
|
1871
|
+
hash: `SHA-${alg.slice(-3)}`,
|
|
1872
|
+
publicExponent: Uint8Array.of(1, 0, 1),
|
|
1873
|
+
modulusLength: getModulusLengthOption(options)
|
|
1874
|
+
};
|
|
1875
|
+
keyUsages = ["sign", "verify"];
|
|
1876
|
+
break;
|
|
1877
|
+
case "RS256":
|
|
1878
|
+
case "RS384":
|
|
1879
|
+
case "RS512":
|
|
1880
|
+
algorithm = {
|
|
1881
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
1882
|
+
hash: `SHA-${alg.slice(-3)}`,
|
|
1883
|
+
publicExponent: Uint8Array.of(1, 0, 1),
|
|
1884
|
+
modulusLength: getModulusLengthOption(options)
|
|
1885
|
+
};
|
|
1886
|
+
keyUsages = ["sign", "verify"];
|
|
1887
|
+
break;
|
|
1888
|
+
case "RSA-OAEP":
|
|
1889
|
+
case "RSA-OAEP-256":
|
|
1890
|
+
case "RSA-OAEP-384":
|
|
1891
|
+
case "RSA-OAEP-512":
|
|
1892
|
+
algorithm = {
|
|
1893
|
+
name: "RSA-OAEP",
|
|
1894
|
+
hash: `SHA-${parseInt(alg.slice(-3), 10) || 1}`,
|
|
1895
|
+
publicExponent: Uint8Array.of(1, 0, 1),
|
|
1896
|
+
modulusLength: getModulusLengthOption(options)
|
|
1897
|
+
};
|
|
1898
|
+
keyUsages = ["decrypt", "unwrapKey", "encrypt", "wrapKey"];
|
|
1899
|
+
break;
|
|
1900
|
+
case "ES256":
|
|
1901
|
+
algorithm = { name: "ECDSA", namedCurve: "P-256" };
|
|
1902
|
+
keyUsages = ["sign", "verify"];
|
|
1903
|
+
break;
|
|
1904
|
+
case "ES384":
|
|
1905
|
+
algorithm = { name: "ECDSA", namedCurve: "P-384" };
|
|
1906
|
+
keyUsages = ["sign", "verify"];
|
|
1907
|
+
break;
|
|
1908
|
+
case "ES512":
|
|
1909
|
+
algorithm = { name: "ECDSA", namedCurve: "P-521" };
|
|
1910
|
+
keyUsages = ["sign", "verify"];
|
|
1911
|
+
break;
|
|
1912
|
+
case "Ed25519":
|
|
1913
|
+
case "EdDSA": {
|
|
1914
|
+
keyUsages = ["sign", "verify"];
|
|
1915
|
+
algorithm = { name: "Ed25519" };
|
|
1916
|
+
break;
|
|
1917
|
+
}
|
|
1918
|
+
case "ML-DSA-44":
|
|
1919
|
+
case "ML-DSA-65":
|
|
1920
|
+
case "ML-DSA-87": {
|
|
1921
|
+
keyUsages = ["sign", "verify"];
|
|
1922
|
+
algorithm = { name: alg };
|
|
1923
|
+
break;
|
|
1924
|
+
}
|
|
1925
|
+
case "ECDH-ES":
|
|
1926
|
+
case "ECDH-ES+A128KW":
|
|
1927
|
+
case "ECDH-ES+A192KW":
|
|
1928
|
+
case "ECDH-ES+A256KW": {
|
|
1929
|
+
keyUsages = ["deriveBits"];
|
|
1930
|
+
const crv = options?.crv ?? "P-256";
|
|
1931
|
+
switch (crv) {
|
|
1932
|
+
case "P-256":
|
|
1933
|
+
case "P-384":
|
|
1934
|
+
case "P-521": {
|
|
1935
|
+
algorithm = { name: "ECDH", namedCurve: crv };
|
|
1936
|
+
break;
|
|
1937
|
+
}
|
|
1938
|
+
case "X25519":
|
|
1939
|
+
algorithm = { name: "X25519" };
|
|
1940
|
+
break;
|
|
1941
|
+
default:
|
|
1942
|
+
throw new JOSENotSupported("Invalid or unsupported crv option provided, supported values are P-256, P-384, P-521, and X25519");
|
|
1943
|
+
}
|
|
1944
|
+
break;
|
|
1945
|
+
}
|
|
1946
|
+
default:
|
|
1947
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
1948
|
+
}
|
|
1949
|
+
return crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages);
|
|
1950
|
+
}
|
|
1788
1951
|
// src/auth.ts
|
|
1789
1952
|
async function verifyJWT(token, publicKey) {
|
|
1790
1953
|
try {
|
|
@@ -1847,8 +2010,1486 @@ var decodeJWT = (token) => {
|
|
|
1847
2010
|
throw new Error("Invalid token");
|
|
1848
2011
|
}
|
|
1849
2012
|
};
|
|
2013
|
+
// src/generate.ts
|
|
2014
|
+
async function generateKeyPair2() {
|
|
2015
|
+
const { privateKey, publicKey } = await generateKeyPair("RS256", {
|
|
2016
|
+
modulusLength: 2048,
|
|
2017
|
+
extractable: true
|
|
2018
|
+
});
|
|
2019
|
+
return { privateKey, publicKey };
|
|
2020
|
+
}
|
|
2021
|
+
async function createJWKS(publicKey, kid) {
|
|
2022
|
+
const jwk = await exportJWK(publicKey);
|
|
2023
|
+
jwk.kid = kid || "kid-key-1";
|
|
2024
|
+
const jwks = {
|
|
2025
|
+
keys: [jwk]
|
|
2026
|
+
};
|
|
2027
|
+
return jwks;
|
|
2028
|
+
}
|
|
2029
|
+
var generate = async (opts = {}) => {
|
|
2030
|
+
const { privateKey, publicKey } = await generateKeyPair2();
|
|
2031
|
+
const jwks = await createJWKS(publicKey, opts.kid);
|
|
2032
|
+
const privateJWK = await exportJWK(privateKey);
|
|
2033
|
+
const privatePEM = await exportPKCS8(privateKey);
|
|
2034
|
+
const publicPEM = await exportSPKI(publicKey);
|
|
2035
|
+
return {
|
|
2036
|
+
jwks,
|
|
2037
|
+
privateJWK,
|
|
2038
|
+
privatePEM,
|
|
2039
|
+
publicPEM
|
|
2040
|
+
};
|
|
2041
|
+
};
|
|
2042
|
+
// node_modules/.pnpm/@kevisual+query@0.0.38/node_modules/@kevisual/query/dist/query.js
|
|
2043
|
+
var isTextForContentType = (contentType) => {
|
|
2044
|
+
if (!contentType)
|
|
2045
|
+
return false;
|
|
2046
|
+
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
|
|
2047
|
+
return textTypes.some((type) => contentType.includes(type));
|
|
2048
|
+
};
|
|
2049
|
+
var adapter = async (opts = {}, overloadOpts) => {
|
|
2050
|
+
const controller = new AbortController;
|
|
2051
|
+
const signal = controller.signal;
|
|
2052
|
+
const isPostFile = opts.isPostFile || false;
|
|
2053
|
+
let responseType = opts.responseType || "json";
|
|
2054
|
+
if (opts.isBlob) {
|
|
2055
|
+
responseType = "blob";
|
|
2056
|
+
} else if (opts.isText) {
|
|
2057
|
+
responseType = "text";
|
|
2058
|
+
}
|
|
2059
|
+
const timeout = opts.timeout || 60000 * 3;
|
|
2060
|
+
const timer = setTimeout(() => {
|
|
2061
|
+
controller.abort();
|
|
2062
|
+
}, timeout);
|
|
2063
|
+
let method = overloadOpts?.method || opts?.method || "POST";
|
|
2064
|
+
let headers = { ...opts?.headers, ...overloadOpts?.headers };
|
|
2065
|
+
let origin = "";
|
|
2066
|
+
let url;
|
|
2067
|
+
if (opts?.url?.startsWith("http")) {
|
|
2068
|
+
url = new URL(opts.url);
|
|
2069
|
+
} else {
|
|
2070
|
+
origin = window?.location?.origin || "http://localhost:51515";
|
|
2071
|
+
url = new URL(opts.url, origin);
|
|
2072
|
+
}
|
|
2073
|
+
const isGet = method === "GET";
|
|
2074
|
+
if (isGet) {
|
|
2075
|
+
let searchParams = new URLSearchParams(opts.body);
|
|
2076
|
+
url.search = searchParams.toString();
|
|
2077
|
+
} else {
|
|
2078
|
+
const params = opts.params || {};
|
|
2079
|
+
const searchParams = new URLSearchParams(params);
|
|
2080
|
+
if (typeof opts.body === "object" && opts.body !== null) {
|
|
2081
|
+
let body2 = opts.body || {};
|
|
2082
|
+
if (!params.path && body2?.path) {
|
|
2083
|
+
searchParams.set("path", body2.path);
|
|
2084
|
+
if (body2?.key) {
|
|
2085
|
+
searchParams.set("key", body2.key);
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
url.search = searchParams.toString();
|
|
2090
|
+
}
|
|
2091
|
+
let body = undefined;
|
|
2092
|
+
if (isGet) {
|
|
2093
|
+
body = undefined;
|
|
2094
|
+
} else if (isPostFile) {
|
|
2095
|
+
body = opts.body;
|
|
2096
|
+
} else {
|
|
2097
|
+
headers = {
|
|
2098
|
+
"Content-Type": "application/json",
|
|
2099
|
+
...headers
|
|
2100
|
+
};
|
|
2101
|
+
body = JSON.stringify(opts.body);
|
|
2102
|
+
}
|
|
2103
|
+
return fetch(url, {
|
|
2104
|
+
method: method.toUpperCase(),
|
|
2105
|
+
signal,
|
|
2106
|
+
body,
|
|
2107
|
+
...overloadOpts,
|
|
2108
|
+
headers
|
|
2109
|
+
}).then(async (response) => {
|
|
2110
|
+
const contentType = response.headers.get("Content-Type");
|
|
2111
|
+
if (responseType === "blob") {
|
|
2112
|
+
return await response.blob();
|
|
2113
|
+
}
|
|
2114
|
+
const isText = responseType === "text";
|
|
2115
|
+
const isJson = contentType && contentType.includes("application/json");
|
|
2116
|
+
if (isJson && !isText) {
|
|
2117
|
+
return await response.json();
|
|
2118
|
+
} else if (isTextForContentType(contentType)) {
|
|
2119
|
+
return {
|
|
2120
|
+
code: response.status,
|
|
2121
|
+
status: response.status,
|
|
2122
|
+
data: await response.text()
|
|
2123
|
+
};
|
|
2124
|
+
} else {
|
|
2125
|
+
return response;
|
|
2126
|
+
}
|
|
2127
|
+
}).catch((err) => {
|
|
2128
|
+
if (err.name === "AbortError") {
|
|
2129
|
+
return {
|
|
2130
|
+
code: 408,
|
|
2131
|
+
message: "请求超时"
|
|
2132
|
+
};
|
|
2133
|
+
}
|
|
2134
|
+
return {
|
|
2135
|
+
code: 500,
|
|
2136
|
+
message: err.message || "网络错误"
|
|
2137
|
+
};
|
|
2138
|
+
}).finally(() => {
|
|
2139
|
+
clearTimeout(timer);
|
|
2140
|
+
});
|
|
2141
|
+
};
|
|
2142
|
+
var wrapperError = ({ code, message: message2 }) => {
|
|
2143
|
+
const result = {
|
|
2144
|
+
code: code || 500,
|
|
2145
|
+
success: false,
|
|
2146
|
+
message: message2 || "api request error",
|
|
2147
|
+
showError: (fn) => {},
|
|
2148
|
+
noMsg: true
|
|
2149
|
+
};
|
|
2150
|
+
return result;
|
|
2151
|
+
};
|
|
2152
|
+
|
|
2153
|
+
class Query {
|
|
2154
|
+
adapter;
|
|
2155
|
+
url;
|
|
2156
|
+
beforeRequest;
|
|
2157
|
+
afterResponse;
|
|
2158
|
+
headers;
|
|
2159
|
+
timeout;
|
|
2160
|
+
stop;
|
|
2161
|
+
qws;
|
|
2162
|
+
isClient = false;
|
|
2163
|
+
constructor(opts) {
|
|
2164
|
+
this.adapter = opts?.adapter || adapter;
|
|
2165
|
+
const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
|
|
2166
|
+
this.url = opts?.url || defaultURL;
|
|
2167
|
+
this.headers = opts?.headers || {
|
|
2168
|
+
"Content-Type": "application/json"
|
|
2169
|
+
};
|
|
2170
|
+
this.timeout = opts?.timeout || 60000 * 3;
|
|
2171
|
+
if (opts.beforeRequest) {
|
|
2172
|
+
this.beforeRequest = opts.beforeRequest;
|
|
2173
|
+
} else {
|
|
2174
|
+
this.beforeRequest = async (opts2) => {
|
|
2175
|
+
const token = globalThis?.localStorage?.getItem("token");
|
|
2176
|
+
if (token) {
|
|
2177
|
+
opts2.headers = {
|
|
2178
|
+
...opts2.headers,
|
|
2179
|
+
Authorization: `Bearer ${token}`
|
|
2180
|
+
};
|
|
2181
|
+
}
|
|
2182
|
+
return opts2;
|
|
2183
|
+
};
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
setQueryWs(qws) {
|
|
2187
|
+
this.qws = qws;
|
|
2188
|
+
}
|
|
2189
|
+
setStop(stop) {
|
|
2190
|
+
this.stop = stop;
|
|
2191
|
+
}
|
|
2192
|
+
async get(params, options) {
|
|
2193
|
+
return this.post(params, options);
|
|
2194
|
+
}
|
|
2195
|
+
async post(body, options) {
|
|
2196
|
+
const url = options?.url || this.url;
|
|
2197
|
+
const { headers, adapter: adapter2, beforeRequest, afterResponse, timeout, ...rest } = options || {};
|
|
2198
|
+
const _headers = { ...this.headers, ...headers };
|
|
2199
|
+
const _adapter = adapter2 || this.adapter;
|
|
2200
|
+
const _beforeRequest = beforeRequest || this.beforeRequest;
|
|
2201
|
+
const _afterResponse = afterResponse || this.afterResponse;
|
|
2202
|
+
const _timeout = timeout || this.timeout;
|
|
2203
|
+
const req = {
|
|
2204
|
+
url,
|
|
2205
|
+
headers: _headers,
|
|
2206
|
+
body,
|
|
2207
|
+
timeout: _timeout,
|
|
2208
|
+
...rest
|
|
2209
|
+
};
|
|
2210
|
+
try {
|
|
2211
|
+
if (_beforeRequest) {
|
|
2212
|
+
const res = await _beforeRequest(req);
|
|
2213
|
+
if (res === false) {
|
|
2214
|
+
return wrapperError({
|
|
2215
|
+
code: 500,
|
|
2216
|
+
message: "request is cancel",
|
|
2217
|
+
req
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
} catch (e) {
|
|
2222
|
+
console.error("request beforeFn error", e, req);
|
|
2223
|
+
return wrapperError({
|
|
2224
|
+
code: 500,
|
|
2225
|
+
message: "api request beforeFn error"
|
|
2226
|
+
});
|
|
2227
|
+
}
|
|
2228
|
+
if (this.stop && !options?.noStop) {
|
|
2229
|
+
const that = this;
|
|
2230
|
+
await new Promise((resolve) => {
|
|
2231
|
+
let timer = 0;
|
|
2232
|
+
const detect = setInterval(() => {
|
|
2233
|
+
if (!that.stop) {
|
|
2234
|
+
clearInterval(detect);
|
|
2235
|
+
resolve(true);
|
|
2236
|
+
}
|
|
2237
|
+
timer++;
|
|
2238
|
+
if (timer > 30) {
|
|
2239
|
+
console.error("request stop: timeout", req.url, timer);
|
|
2240
|
+
}
|
|
2241
|
+
}, 1000);
|
|
2242
|
+
});
|
|
2243
|
+
}
|
|
2244
|
+
return _adapter(req).then(async (res) => {
|
|
2245
|
+
try {
|
|
2246
|
+
if (_afterResponse) {
|
|
2247
|
+
return await _afterResponse(res, {
|
|
2248
|
+
req,
|
|
2249
|
+
res,
|
|
2250
|
+
fetch: adapter2
|
|
2251
|
+
});
|
|
2252
|
+
}
|
|
2253
|
+
return res;
|
|
2254
|
+
} catch (e) {
|
|
2255
|
+
console.error("request afterFn error", e, req);
|
|
2256
|
+
return wrapperError({
|
|
2257
|
+
code: 500,
|
|
2258
|
+
message: "api request afterFn error"
|
|
2259
|
+
});
|
|
2260
|
+
}
|
|
2261
|
+
});
|
|
2262
|
+
}
|
|
2263
|
+
before(fn) {
|
|
2264
|
+
this.beforeRequest = fn;
|
|
2265
|
+
}
|
|
2266
|
+
after(fn) {
|
|
2267
|
+
this.afterResponse = fn;
|
|
2268
|
+
}
|
|
2269
|
+
async fetchText(urlOrOptions, options) {
|
|
2270
|
+
let _options = { ...options };
|
|
2271
|
+
if (typeof urlOrOptions === "string" && !_options.url) {
|
|
2272
|
+
_options.url = urlOrOptions;
|
|
2273
|
+
}
|
|
2274
|
+
if (typeof urlOrOptions === "object") {
|
|
2275
|
+
_options = { ...urlOrOptions, ..._options };
|
|
2276
|
+
}
|
|
2277
|
+
const res = await adapter({
|
|
2278
|
+
method: "GET",
|
|
2279
|
+
..._options,
|
|
2280
|
+
headers: {
|
|
2281
|
+
...this.headers,
|
|
2282
|
+
..._options?.headers || {}
|
|
2283
|
+
}
|
|
2284
|
+
});
|
|
2285
|
+
if (res && !res.code) {
|
|
2286
|
+
return {
|
|
2287
|
+
code: 200,
|
|
2288
|
+
data: res
|
|
2289
|
+
};
|
|
2290
|
+
}
|
|
2291
|
+
return res;
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
// node_modules/.pnpm/lru-cache@11.2.4/node_modules/lru-cache/dist/esm/index.js
|
|
2296
|
+
var defaultPerf = typeof performance === "object" && performance && typeof performance.now === "function" ? performance : Date;
|
|
2297
|
+
var warned = new Set;
|
|
2298
|
+
var PROCESS = typeof process === "object" && !!process ? process : {};
|
|
2299
|
+
var emitWarning = (msg, type, code, fn) => {
|
|
2300
|
+
typeof PROCESS.emitWarning === "function" ? PROCESS.emitWarning(msg, type, code, fn) : console.error(`[${code}] ${type}: ${msg}`);
|
|
2301
|
+
};
|
|
2302
|
+
var AC = globalThis.AbortController;
|
|
2303
|
+
var AS = globalThis.AbortSignal;
|
|
2304
|
+
if (typeof AC === "undefined") {
|
|
2305
|
+
AS = class AbortSignal {
|
|
2306
|
+
onabort;
|
|
2307
|
+
_onabort = [];
|
|
2308
|
+
reason;
|
|
2309
|
+
aborted = false;
|
|
2310
|
+
addEventListener(_, fn) {
|
|
2311
|
+
this._onabort.push(fn);
|
|
2312
|
+
}
|
|
2313
|
+
};
|
|
2314
|
+
AC = class AbortController2 {
|
|
2315
|
+
constructor() {
|
|
2316
|
+
warnACPolyfill();
|
|
2317
|
+
}
|
|
2318
|
+
signal = new AS;
|
|
2319
|
+
abort(reason) {
|
|
2320
|
+
if (this.signal.aborted)
|
|
2321
|
+
return;
|
|
2322
|
+
this.signal.reason = reason;
|
|
2323
|
+
this.signal.aborted = true;
|
|
2324
|
+
for (const fn of this.signal._onabort) {
|
|
2325
|
+
fn(reason);
|
|
2326
|
+
}
|
|
2327
|
+
this.signal.onabort?.(reason);
|
|
2328
|
+
}
|
|
2329
|
+
};
|
|
2330
|
+
let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== "1";
|
|
2331
|
+
const warnACPolyfill = () => {
|
|
2332
|
+
if (!printACPolyfillWarning)
|
|
2333
|
+
return;
|
|
2334
|
+
printACPolyfillWarning = false;
|
|
2335
|
+
emitWarning("AbortController is not defined. If using lru-cache in " + "node 14, load an AbortController polyfill from the " + "`node-abort-controller` package. A minimal polyfill is " + "provided for use by LRUCache.fetch(), but it should not be " + "relied upon in other contexts (eg, passing it to other APIs that " + "use AbortController/AbortSignal might have undesirable effects). " + "You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.", "NO_ABORT_CONTROLLER", "ENOTSUP", warnACPolyfill);
|
|
2336
|
+
};
|
|
2337
|
+
}
|
|
2338
|
+
var shouldWarn = (code) => !warned.has(code);
|
|
2339
|
+
var TYPE = Symbol("type");
|
|
2340
|
+
var isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
|
|
2341
|
+
var getUintArray = (max) => !isPosInt(max) ? null : max <= Math.pow(2, 8) ? Uint8Array : max <= Math.pow(2, 16) ? Uint16Array : max <= Math.pow(2, 32) ? Uint32Array : max <= Number.MAX_SAFE_INTEGER ? ZeroArray : null;
|
|
2342
|
+
|
|
2343
|
+
class ZeroArray extends Array {
|
|
2344
|
+
constructor(size) {
|
|
2345
|
+
super(size);
|
|
2346
|
+
this.fill(0);
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
class Stack {
|
|
2351
|
+
heap;
|
|
2352
|
+
length;
|
|
2353
|
+
static #constructing = false;
|
|
2354
|
+
static create(max) {
|
|
2355
|
+
const HeapCls = getUintArray(max);
|
|
2356
|
+
if (!HeapCls)
|
|
2357
|
+
return [];
|
|
2358
|
+
Stack.#constructing = true;
|
|
2359
|
+
const s = new Stack(max, HeapCls);
|
|
2360
|
+
Stack.#constructing = false;
|
|
2361
|
+
return s;
|
|
2362
|
+
}
|
|
2363
|
+
constructor(max, HeapCls) {
|
|
2364
|
+
if (!Stack.#constructing) {
|
|
2365
|
+
throw new TypeError("instantiate Stack using Stack.create(n)");
|
|
2366
|
+
}
|
|
2367
|
+
this.heap = new HeapCls(max);
|
|
2368
|
+
this.length = 0;
|
|
2369
|
+
}
|
|
2370
|
+
push(n) {
|
|
2371
|
+
this.heap[this.length++] = n;
|
|
2372
|
+
}
|
|
2373
|
+
pop() {
|
|
2374
|
+
return this.heap[--this.length];
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2378
|
+
class LRUCache {
|
|
2379
|
+
#max;
|
|
2380
|
+
#maxSize;
|
|
2381
|
+
#dispose;
|
|
2382
|
+
#onInsert;
|
|
2383
|
+
#disposeAfter;
|
|
2384
|
+
#fetchMethod;
|
|
2385
|
+
#memoMethod;
|
|
2386
|
+
#perf;
|
|
2387
|
+
get perf() {
|
|
2388
|
+
return this.#perf;
|
|
2389
|
+
}
|
|
2390
|
+
ttl;
|
|
2391
|
+
ttlResolution;
|
|
2392
|
+
ttlAutopurge;
|
|
2393
|
+
updateAgeOnGet;
|
|
2394
|
+
updateAgeOnHas;
|
|
2395
|
+
allowStale;
|
|
2396
|
+
noDisposeOnSet;
|
|
2397
|
+
noUpdateTTL;
|
|
2398
|
+
maxEntrySize;
|
|
2399
|
+
sizeCalculation;
|
|
2400
|
+
noDeleteOnFetchRejection;
|
|
2401
|
+
noDeleteOnStaleGet;
|
|
2402
|
+
allowStaleOnFetchAbort;
|
|
2403
|
+
allowStaleOnFetchRejection;
|
|
2404
|
+
ignoreFetchAbort;
|
|
2405
|
+
#size;
|
|
2406
|
+
#calculatedSize;
|
|
2407
|
+
#keyMap;
|
|
2408
|
+
#keyList;
|
|
2409
|
+
#valList;
|
|
2410
|
+
#next;
|
|
2411
|
+
#prev;
|
|
2412
|
+
#head;
|
|
2413
|
+
#tail;
|
|
2414
|
+
#free;
|
|
2415
|
+
#disposed;
|
|
2416
|
+
#sizes;
|
|
2417
|
+
#starts;
|
|
2418
|
+
#ttls;
|
|
2419
|
+
#autopurgeTimers;
|
|
2420
|
+
#hasDispose;
|
|
2421
|
+
#hasFetchMethod;
|
|
2422
|
+
#hasDisposeAfter;
|
|
2423
|
+
#hasOnInsert;
|
|
2424
|
+
static unsafeExposeInternals(c) {
|
|
2425
|
+
return {
|
|
2426
|
+
starts: c.#starts,
|
|
2427
|
+
ttls: c.#ttls,
|
|
2428
|
+
autopurgeTimers: c.#autopurgeTimers,
|
|
2429
|
+
sizes: c.#sizes,
|
|
2430
|
+
keyMap: c.#keyMap,
|
|
2431
|
+
keyList: c.#keyList,
|
|
2432
|
+
valList: c.#valList,
|
|
2433
|
+
next: c.#next,
|
|
2434
|
+
prev: c.#prev,
|
|
2435
|
+
get head() {
|
|
2436
|
+
return c.#head;
|
|
2437
|
+
},
|
|
2438
|
+
get tail() {
|
|
2439
|
+
return c.#tail;
|
|
2440
|
+
},
|
|
2441
|
+
free: c.#free,
|
|
2442
|
+
isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
|
|
2443
|
+
backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
|
|
2444
|
+
moveToTail: (index) => c.#moveToTail(index),
|
|
2445
|
+
indexes: (options) => c.#indexes(options),
|
|
2446
|
+
rindexes: (options) => c.#rindexes(options),
|
|
2447
|
+
isStale: (index) => c.#isStale(index)
|
|
2448
|
+
};
|
|
2449
|
+
}
|
|
2450
|
+
get max() {
|
|
2451
|
+
return this.#max;
|
|
2452
|
+
}
|
|
2453
|
+
get maxSize() {
|
|
2454
|
+
return this.#maxSize;
|
|
2455
|
+
}
|
|
2456
|
+
get calculatedSize() {
|
|
2457
|
+
return this.#calculatedSize;
|
|
2458
|
+
}
|
|
2459
|
+
get size() {
|
|
2460
|
+
return this.#size;
|
|
2461
|
+
}
|
|
2462
|
+
get fetchMethod() {
|
|
2463
|
+
return this.#fetchMethod;
|
|
2464
|
+
}
|
|
2465
|
+
get memoMethod() {
|
|
2466
|
+
return this.#memoMethod;
|
|
2467
|
+
}
|
|
2468
|
+
get dispose() {
|
|
2469
|
+
return this.#dispose;
|
|
2470
|
+
}
|
|
2471
|
+
get onInsert() {
|
|
2472
|
+
return this.#onInsert;
|
|
2473
|
+
}
|
|
2474
|
+
get disposeAfter() {
|
|
2475
|
+
return this.#disposeAfter;
|
|
2476
|
+
}
|
|
2477
|
+
constructor(options) {
|
|
2478
|
+
const { max = 0, ttl, ttlResolution = 1, ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, onInsert, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0, maxEntrySize = 0, sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, perf } = options;
|
|
2479
|
+
if (perf !== undefined) {
|
|
2480
|
+
if (typeof perf?.now !== "function") {
|
|
2481
|
+
throw new TypeError("perf option must have a now() method if specified");
|
|
2482
|
+
}
|
|
2483
|
+
}
|
|
2484
|
+
this.#perf = perf ?? defaultPerf;
|
|
2485
|
+
if (max !== 0 && !isPosInt(max)) {
|
|
2486
|
+
throw new TypeError("max option must be a nonnegative integer");
|
|
2487
|
+
}
|
|
2488
|
+
const UintArray = max ? getUintArray(max) : Array;
|
|
2489
|
+
if (!UintArray) {
|
|
2490
|
+
throw new Error("invalid max value: " + max);
|
|
2491
|
+
}
|
|
2492
|
+
this.#max = max;
|
|
2493
|
+
this.#maxSize = maxSize;
|
|
2494
|
+
this.maxEntrySize = maxEntrySize || this.#maxSize;
|
|
2495
|
+
this.sizeCalculation = sizeCalculation;
|
|
2496
|
+
if (this.sizeCalculation) {
|
|
2497
|
+
if (!this.#maxSize && !this.maxEntrySize) {
|
|
2498
|
+
throw new TypeError("cannot set sizeCalculation without setting maxSize or maxEntrySize");
|
|
2499
|
+
}
|
|
2500
|
+
if (typeof this.sizeCalculation !== "function") {
|
|
2501
|
+
throw new TypeError("sizeCalculation set to non-function");
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
if (memoMethod !== undefined && typeof memoMethod !== "function") {
|
|
2505
|
+
throw new TypeError("memoMethod must be a function if defined");
|
|
2506
|
+
}
|
|
2507
|
+
this.#memoMethod = memoMethod;
|
|
2508
|
+
if (fetchMethod !== undefined && typeof fetchMethod !== "function") {
|
|
2509
|
+
throw new TypeError("fetchMethod must be a function if specified");
|
|
2510
|
+
}
|
|
2511
|
+
this.#fetchMethod = fetchMethod;
|
|
2512
|
+
this.#hasFetchMethod = !!fetchMethod;
|
|
2513
|
+
this.#keyMap = new Map;
|
|
2514
|
+
this.#keyList = new Array(max).fill(undefined);
|
|
2515
|
+
this.#valList = new Array(max).fill(undefined);
|
|
2516
|
+
this.#next = new UintArray(max);
|
|
2517
|
+
this.#prev = new UintArray(max);
|
|
2518
|
+
this.#head = 0;
|
|
2519
|
+
this.#tail = 0;
|
|
2520
|
+
this.#free = Stack.create(max);
|
|
2521
|
+
this.#size = 0;
|
|
2522
|
+
this.#calculatedSize = 0;
|
|
2523
|
+
if (typeof dispose === "function") {
|
|
2524
|
+
this.#dispose = dispose;
|
|
2525
|
+
}
|
|
2526
|
+
if (typeof onInsert === "function") {
|
|
2527
|
+
this.#onInsert = onInsert;
|
|
2528
|
+
}
|
|
2529
|
+
if (typeof disposeAfter === "function") {
|
|
2530
|
+
this.#disposeAfter = disposeAfter;
|
|
2531
|
+
this.#disposed = [];
|
|
2532
|
+
} else {
|
|
2533
|
+
this.#disposeAfter = undefined;
|
|
2534
|
+
this.#disposed = undefined;
|
|
2535
|
+
}
|
|
2536
|
+
this.#hasDispose = !!this.#dispose;
|
|
2537
|
+
this.#hasOnInsert = !!this.#onInsert;
|
|
2538
|
+
this.#hasDisposeAfter = !!this.#disposeAfter;
|
|
2539
|
+
this.noDisposeOnSet = !!noDisposeOnSet;
|
|
2540
|
+
this.noUpdateTTL = !!noUpdateTTL;
|
|
2541
|
+
this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
|
|
2542
|
+
this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
|
|
2543
|
+
this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
|
|
2544
|
+
this.ignoreFetchAbort = !!ignoreFetchAbort;
|
|
2545
|
+
if (this.maxEntrySize !== 0) {
|
|
2546
|
+
if (this.#maxSize !== 0) {
|
|
2547
|
+
if (!isPosInt(this.#maxSize)) {
|
|
2548
|
+
throw new TypeError("maxSize must be a positive integer if specified");
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
if (!isPosInt(this.maxEntrySize)) {
|
|
2552
|
+
throw new TypeError("maxEntrySize must be a positive integer if specified");
|
|
2553
|
+
}
|
|
2554
|
+
this.#initializeSizeTracking();
|
|
2555
|
+
}
|
|
2556
|
+
this.allowStale = !!allowStale;
|
|
2557
|
+
this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
|
|
2558
|
+
this.updateAgeOnGet = !!updateAgeOnGet;
|
|
2559
|
+
this.updateAgeOnHas = !!updateAgeOnHas;
|
|
2560
|
+
this.ttlResolution = isPosInt(ttlResolution) || ttlResolution === 0 ? ttlResolution : 1;
|
|
2561
|
+
this.ttlAutopurge = !!ttlAutopurge;
|
|
2562
|
+
this.ttl = ttl || 0;
|
|
2563
|
+
if (this.ttl) {
|
|
2564
|
+
if (!isPosInt(this.ttl)) {
|
|
2565
|
+
throw new TypeError("ttl must be a positive integer if specified");
|
|
2566
|
+
}
|
|
2567
|
+
this.#initializeTTLTracking();
|
|
2568
|
+
}
|
|
2569
|
+
if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
|
|
2570
|
+
throw new TypeError("At least one of max, maxSize, or ttl is required");
|
|
2571
|
+
}
|
|
2572
|
+
if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
|
|
2573
|
+
const code = "LRU_CACHE_UNBOUNDED";
|
|
2574
|
+
if (shouldWarn(code)) {
|
|
2575
|
+
warned.add(code);
|
|
2576
|
+
const msg = "TTL caching without ttlAutopurge, max, or maxSize can " + "result in unbounded memory consumption.";
|
|
2577
|
+
emitWarning(msg, "UnboundedCacheWarning", code, LRUCache);
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
getRemainingTTL(key) {
|
|
2582
|
+
return this.#keyMap.has(key) ? Infinity : 0;
|
|
2583
|
+
}
|
|
2584
|
+
#initializeTTLTracking() {
|
|
2585
|
+
const ttls = new ZeroArray(this.#max);
|
|
2586
|
+
const starts = new ZeroArray(this.#max);
|
|
2587
|
+
this.#ttls = ttls;
|
|
2588
|
+
this.#starts = starts;
|
|
2589
|
+
const purgeTimers = this.ttlAutopurge ? new Array(this.#max) : undefined;
|
|
2590
|
+
this.#autopurgeTimers = purgeTimers;
|
|
2591
|
+
this.#setItemTTL = (index, ttl, start = this.#perf.now()) => {
|
|
2592
|
+
starts[index] = ttl !== 0 ? start : 0;
|
|
2593
|
+
ttls[index] = ttl;
|
|
2594
|
+
if (purgeTimers?.[index]) {
|
|
2595
|
+
clearTimeout(purgeTimers[index]);
|
|
2596
|
+
purgeTimers[index] = undefined;
|
|
2597
|
+
}
|
|
2598
|
+
if (ttl !== 0 && purgeTimers) {
|
|
2599
|
+
const t = setTimeout(() => {
|
|
2600
|
+
if (this.#isStale(index)) {
|
|
2601
|
+
this.#delete(this.#keyList[index], "expire");
|
|
2602
|
+
}
|
|
2603
|
+
}, ttl + 1);
|
|
2604
|
+
if (t.unref) {
|
|
2605
|
+
t.unref();
|
|
2606
|
+
}
|
|
2607
|
+
purgeTimers[index] = t;
|
|
2608
|
+
}
|
|
2609
|
+
};
|
|
2610
|
+
this.#updateItemAge = (index) => {
|
|
2611
|
+
starts[index] = ttls[index] !== 0 ? this.#perf.now() : 0;
|
|
2612
|
+
};
|
|
2613
|
+
this.#statusTTL = (status, index) => {
|
|
2614
|
+
if (ttls[index]) {
|
|
2615
|
+
const ttl = ttls[index];
|
|
2616
|
+
const start = starts[index];
|
|
2617
|
+
if (!ttl || !start)
|
|
2618
|
+
return;
|
|
2619
|
+
status.ttl = ttl;
|
|
2620
|
+
status.start = start;
|
|
2621
|
+
status.now = cachedNow || getNow();
|
|
2622
|
+
const age = status.now - start;
|
|
2623
|
+
status.remainingTTL = ttl - age;
|
|
2624
|
+
}
|
|
2625
|
+
};
|
|
2626
|
+
let cachedNow = 0;
|
|
2627
|
+
const getNow = () => {
|
|
2628
|
+
const n = this.#perf.now();
|
|
2629
|
+
if (this.ttlResolution > 0) {
|
|
2630
|
+
cachedNow = n;
|
|
2631
|
+
const t = setTimeout(() => cachedNow = 0, this.ttlResolution);
|
|
2632
|
+
if (t.unref) {
|
|
2633
|
+
t.unref();
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
return n;
|
|
2637
|
+
};
|
|
2638
|
+
this.getRemainingTTL = (key) => {
|
|
2639
|
+
const index = this.#keyMap.get(key);
|
|
2640
|
+
if (index === undefined) {
|
|
2641
|
+
return 0;
|
|
2642
|
+
}
|
|
2643
|
+
const ttl = ttls[index];
|
|
2644
|
+
const start = starts[index];
|
|
2645
|
+
if (!ttl || !start) {
|
|
2646
|
+
return Infinity;
|
|
2647
|
+
}
|
|
2648
|
+
const age = (cachedNow || getNow()) - start;
|
|
2649
|
+
return ttl - age;
|
|
2650
|
+
};
|
|
2651
|
+
this.#isStale = (index) => {
|
|
2652
|
+
const s = starts[index];
|
|
2653
|
+
const t = ttls[index];
|
|
2654
|
+
return !!t && !!s && (cachedNow || getNow()) - s > t;
|
|
2655
|
+
};
|
|
2656
|
+
}
|
|
2657
|
+
#updateItemAge = () => {};
|
|
2658
|
+
#statusTTL = () => {};
|
|
2659
|
+
#setItemTTL = () => {};
|
|
2660
|
+
#isStale = () => false;
|
|
2661
|
+
#initializeSizeTracking() {
|
|
2662
|
+
const sizes = new ZeroArray(this.#max);
|
|
2663
|
+
this.#calculatedSize = 0;
|
|
2664
|
+
this.#sizes = sizes;
|
|
2665
|
+
this.#removeItemSize = (index) => {
|
|
2666
|
+
this.#calculatedSize -= sizes[index];
|
|
2667
|
+
sizes[index] = 0;
|
|
2668
|
+
};
|
|
2669
|
+
this.#requireSize = (k, v, size, sizeCalculation) => {
|
|
2670
|
+
if (this.#isBackgroundFetch(v)) {
|
|
2671
|
+
return 0;
|
|
2672
|
+
}
|
|
2673
|
+
if (!isPosInt(size)) {
|
|
2674
|
+
if (sizeCalculation) {
|
|
2675
|
+
if (typeof sizeCalculation !== "function") {
|
|
2676
|
+
throw new TypeError("sizeCalculation must be a function");
|
|
2677
|
+
}
|
|
2678
|
+
size = sizeCalculation(v, k);
|
|
2679
|
+
if (!isPosInt(size)) {
|
|
2680
|
+
throw new TypeError("sizeCalculation return invalid (expect positive integer)");
|
|
2681
|
+
}
|
|
2682
|
+
} else {
|
|
2683
|
+
throw new TypeError("invalid size value (must be positive integer). " + "When maxSize or maxEntrySize is used, sizeCalculation " + "or size must be set.");
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
return size;
|
|
2687
|
+
};
|
|
2688
|
+
this.#addItemSize = (index, size, status) => {
|
|
2689
|
+
sizes[index] = size;
|
|
2690
|
+
if (this.#maxSize) {
|
|
2691
|
+
const maxSize = this.#maxSize - sizes[index];
|
|
2692
|
+
while (this.#calculatedSize > maxSize) {
|
|
2693
|
+
this.#evict(true);
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
this.#calculatedSize += sizes[index];
|
|
2697
|
+
if (status) {
|
|
2698
|
+
status.entrySize = size;
|
|
2699
|
+
status.totalCalculatedSize = this.#calculatedSize;
|
|
2700
|
+
}
|
|
2701
|
+
};
|
|
2702
|
+
}
|
|
2703
|
+
#removeItemSize = (_i) => {};
|
|
2704
|
+
#addItemSize = (_i, _s, _st) => {};
|
|
2705
|
+
#requireSize = (_k, _v, size, sizeCalculation) => {
|
|
2706
|
+
if (size || sizeCalculation) {
|
|
2707
|
+
throw new TypeError("cannot set size without setting maxSize or maxEntrySize on cache");
|
|
2708
|
+
}
|
|
2709
|
+
return 0;
|
|
2710
|
+
};
|
|
2711
|
+
*#indexes({ allowStale = this.allowStale } = {}) {
|
|
2712
|
+
if (this.#size) {
|
|
2713
|
+
for (let i = this.#tail;; ) {
|
|
2714
|
+
if (!this.#isValidIndex(i)) {
|
|
2715
|
+
break;
|
|
2716
|
+
}
|
|
2717
|
+
if (allowStale || !this.#isStale(i)) {
|
|
2718
|
+
yield i;
|
|
2719
|
+
}
|
|
2720
|
+
if (i === this.#head) {
|
|
2721
|
+
break;
|
|
2722
|
+
} else {
|
|
2723
|
+
i = this.#prev[i];
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
*#rindexes({ allowStale = this.allowStale } = {}) {
|
|
2729
|
+
if (this.#size) {
|
|
2730
|
+
for (let i = this.#head;; ) {
|
|
2731
|
+
if (!this.#isValidIndex(i)) {
|
|
2732
|
+
break;
|
|
2733
|
+
}
|
|
2734
|
+
if (allowStale || !this.#isStale(i)) {
|
|
2735
|
+
yield i;
|
|
2736
|
+
}
|
|
2737
|
+
if (i === this.#tail) {
|
|
2738
|
+
break;
|
|
2739
|
+
} else {
|
|
2740
|
+
i = this.#next[i];
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
#isValidIndex(index) {
|
|
2746
|
+
return index !== undefined && this.#keyMap.get(this.#keyList[index]) === index;
|
|
2747
|
+
}
|
|
2748
|
+
*entries() {
|
|
2749
|
+
for (const i of this.#indexes()) {
|
|
2750
|
+
if (this.#valList[i] !== undefined && this.#keyList[i] !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
|
|
2751
|
+
yield [this.#keyList[i], this.#valList[i]];
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
}
|
|
2755
|
+
*rentries() {
|
|
2756
|
+
for (const i of this.#rindexes()) {
|
|
2757
|
+
if (this.#valList[i] !== undefined && this.#keyList[i] !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
|
|
2758
|
+
yield [this.#keyList[i], this.#valList[i]];
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
}
|
|
2762
|
+
*keys() {
|
|
2763
|
+
for (const i of this.#indexes()) {
|
|
2764
|
+
const k = this.#keyList[i];
|
|
2765
|
+
if (k !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
|
|
2766
|
+
yield k;
|
|
2767
|
+
}
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
*rkeys() {
|
|
2771
|
+
for (const i of this.#rindexes()) {
|
|
2772
|
+
const k = this.#keyList[i];
|
|
2773
|
+
if (k !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
|
|
2774
|
+
yield k;
|
|
2775
|
+
}
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
*values() {
|
|
2779
|
+
for (const i of this.#indexes()) {
|
|
2780
|
+
const v = this.#valList[i];
|
|
2781
|
+
if (v !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
|
|
2782
|
+
yield this.#valList[i];
|
|
2783
|
+
}
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
*rvalues() {
|
|
2787
|
+
for (const i of this.#rindexes()) {
|
|
2788
|
+
const v = this.#valList[i];
|
|
2789
|
+
if (v !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
|
|
2790
|
+
yield this.#valList[i];
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2793
|
+
}
|
|
2794
|
+
[Symbol.iterator]() {
|
|
2795
|
+
return this.entries();
|
|
2796
|
+
}
|
|
2797
|
+
[Symbol.toStringTag] = "LRUCache";
|
|
2798
|
+
find(fn, getOptions = {}) {
|
|
2799
|
+
for (const i of this.#indexes()) {
|
|
2800
|
+
const v = this.#valList[i];
|
|
2801
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
2802
|
+
if (value === undefined)
|
|
2803
|
+
continue;
|
|
2804
|
+
if (fn(value, this.#keyList[i], this)) {
|
|
2805
|
+
return this.get(this.#keyList[i], getOptions);
|
|
2806
|
+
}
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
forEach(fn, thisp = this) {
|
|
2810
|
+
for (const i of this.#indexes()) {
|
|
2811
|
+
const v = this.#valList[i];
|
|
2812
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
2813
|
+
if (value === undefined)
|
|
2814
|
+
continue;
|
|
2815
|
+
fn.call(thisp, value, this.#keyList[i], this);
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
rforEach(fn, thisp = this) {
|
|
2819
|
+
for (const i of this.#rindexes()) {
|
|
2820
|
+
const v = this.#valList[i];
|
|
2821
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
2822
|
+
if (value === undefined)
|
|
2823
|
+
continue;
|
|
2824
|
+
fn.call(thisp, value, this.#keyList[i], this);
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
purgeStale() {
|
|
2828
|
+
let deleted = false;
|
|
2829
|
+
for (const i of this.#rindexes({ allowStale: true })) {
|
|
2830
|
+
if (this.#isStale(i)) {
|
|
2831
|
+
this.#delete(this.#keyList[i], "expire");
|
|
2832
|
+
deleted = true;
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
return deleted;
|
|
2836
|
+
}
|
|
2837
|
+
info(key) {
|
|
2838
|
+
const i = this.#keyMap.get(key);
|
|
2839
|
+
if (i === undefined)
|
|
2840
|
+
return;
|
|
2841
|
+
const v = this.#valList[i];
|
|
2842
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
2843
|
+
if (value === undefined)
|
|
2844
|
+
return;
|
|
2845
|
+
const entry = { value };
|
|
2846
|
+
if (this.#ttls && this.#starts) {
|
|
2847
|
+
const ttl = this.#ttls[i];
|
|
2848
|
+
const start = this.#starts[i];
|
|
2849
|
+
if (ttl && start) {
|
|
2850
|
+
const remain = ttl - (this.#perf.now() - start);
|
|
2851
|
+
entry.ttl = remain;
|
|
2852
|
+
entry.start = Date.now();
|
|
2853
|
+
}
|
|
2854
|
+
}
|
|
2855
|
+
if (this.#sizes) {
|
|
2856
|
+
entry.size = this.#sizes[i];
|
|
2857
|
+
}
|
|
2858
|
+
return entry;
|
|
2859
|
+
}
|
|
2860
|
+
dump() {
|
|
2861
|
+
const arr = [];
|
|
2862
|
+
for (const i of this.#indexes({ allowStale: true })) {
|
|
2863
|
+
const key = this.#keyList[i];
|
|
2864
|
+
const v = this.#valList[i];
|
|
2865
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
2866
|
+
if (value === undefined || key === undefined)
|
|
2867
|
+
continue;
|
|
2868
|
+
const entry = { value };
|
|
2869
|
+
if (this.#ttls && this.#starts) {
|
|
2870
|
+
entry.ttl = this.#ttls[i];
|
|
2871
|
+
const age = this.#perf.now() - this.#starts[i];
|
|
2872
|
+
entry.start = Math.floor(Date.now() - age);
|
|
2873
|
+
}
|
|
2874
|
+
if (this.#sizes) {
|
|
2875
|
+
entry.size = this.#sizes[i];
|
|
2876
|
+
}
|
|
2877
|
+
arr.unshift([key, entry]);
|
|
2878
|
+
}
|
|
2879
|
+
return arr;
|
|
2880
|
+
}
|
|
2881
|
+
load(arr) {
|
|
2882
|
+
this.clear();
|
|
2883
|
+
for (const [key, entry] of arr) {
|
|
2884
|
+
if (entry.start) {
|
|
2885
|
+
const age = Date.now() - entry.start;
|
|
2886
|
+
entry.start = this.#perf.now() - age;
|
|
2887
|
+
}
|
|
2888
|
+
this.set(key, entry.value, entry);
|
|
2889
|
+
}
|
|
2890
|
+
}
|
|
2891
|
+
set(k, v, setOptions = {}) {
|
|
2892
|
+
if (v === undefined) {
|
|
2893
|
+
this.delete(k);
|
|
2894
|
+
return this;
|
|
2895
|
+
}
|
|
2896
|
+
const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status } = setOptions;
|
|
2897
|
+
let { noUpdateTTL = this.noUpdateTTL } = setOptions;
|
|
2898
|
+
const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
|
|
2899
|
+
if (this.maxEntrySize && size > this.maxEntrySize) {
|
|
2900
|
+
if (status) {
|
|
2901
|
+
status.set = "miss";
|
|
2902
|
+
status.maxEntrySizeExceeded = true;
|
|
2903
|
+
}
|
|
2904
|
+
this.#delete(k, "set");
|
|
2905
|
+
return this;
|
|
2906
|
+
}
|
|
2907
|
+
let index = this.#size === 0 ? undefined : this.#keyMap.get(k);
|
|
2908
|
+
if (index === undefined) {
|
|
2909
|
+
index = this.#size === 0 ? this.#tail : this.#free.length !== 0 ? this.#free.pop() : this.#size === this.#max ? this.#evict(false) : this.#size;
|
|
2910
|
+
this.#keyList[index] = k;
|
|
2911
|
+
this.#valList[index] = v;
|
|
2912
|
+
this.#keyMap.set(k, index);
|
|
2913
|
+
this.#next[this.#tail] = index;
|
|
2914
|
+
this.#prev[index] = this.#tail;
|
|
2915
|
+
this.#tail = index;
|
|
2916
|
+
this.#size++;
|
|
2917
|
+
this.#addItemSize(index, size, status);
|
|
2918
|
+
if (status)
|
|
2919
|
+
status.set = "add";
|
|
2920
|
+
noUpdateTTL = false;
|
|
2921
|
+
if (this.#hasOnInsert) {
|
|
2922
|
+
this.#onInsert?.(v, k, "add");
|
|
2923
|
+
}
|
|
2924
|
+
} else {
|
|
2925
|
+
this.#moveToTail(index);
|
|
2926
|
+
const oldVal = this.#valList[index];
|
|
2927
|
+
if (v !== oldVal) {
|
|
2928
|
+
if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
|
|
2929
|
+
oldVal.__abortController.abort(new Error("replaced"));
|
|
2930
|
+
const { __staleWhileFetching: s } = oldVal;
|
|
2931
|
+
if (s !== undefined && !noDisposeOnSet) {
|
|
2932
|
+
if (this.#hasDispose) {
|
|
2933
|
+
this.#dispose?.(s, k, "set");
|
|
2934
|
+
}
|
|
2935
|
+
if (this.#hasDisposeAfter) {
|
|
2936
|
+
this.#disposed?.push([s, k, "set"]);
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
} else if (!noDisposeOnSet) {
|
|
2940
|
+
if (this.#hasDispose) {
|
|
2941
|
+
this.#dispose?.(oldVal, k, "set");
|
|
2942
|
+
}
|
|
2943
|
+
if (this.#hasDisposeAfter) {
|
|
2944
|
+
this.#disposed?.push([oldVal, k, "set"]);
|
|
2945
|
+
}
|
|
2946
|
+
}
|
|
2947
|
+
this.#removeItemSize(index);
|
|
2948
|
+
this.#addItemSize(index, size, status);
|
|
2949
|
+
this.#valList[index] = v;
|
|
2950
|
+
if (status) {
|
|
2951
|
+
status.set = "replace";
|
|
2952
|
+
const oldValue = oldVal && this.#isBackgroundFetch(oldVal) ? oldVal.__staleWhileFetching : oldVal;
|
|
2953
|
+
if (oldValue !== undefined)
|
|
2954
|
+
status.oldValue = oldValue;
|
|
2955
|
+
}
|
|
2956
|
+
} else if (status) {
|
|
2957
|
+
status.set = "update";
|
|
2958
|
+
}
|
|
2959
|
+
if (this.#hasOnInsert) {
|
|
2960
|
+
this.onInsert?.(v, k, v === oldVal ? "update" : "replace");
|
|
2961
|
+
}
|
|
2962
|
+
}
|
|
2963
|
+
if (ttl !== 0 && !this.#ttls) {
|
|
2964
|
+
this.#initializeTTLTracking();
|
|
2965
|
+
}
|
|
2966
|
+
if (this.#ttls) {
|
|
2967
|
+
if (!noUpdateTTL) {
|
|
2968
|
+
this.#setItemTTL(index, ttl, start);
|
|
2969
|
+
}
|
|
2970
|
+
if (status)
|
|
2971
|
+
this.#statusTTL(status, index);
|
|
2972
|
+
}
|
|
2973
|
+
if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
|
|
2974
|
+
const dt = this.#disposed;
|
|
2975
|
+
let task;
|
|
2976
|
+
while (task = dt?.shift()) {
|
|
2977
|
+
this.#disposeAfter?.(...task);
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2980
|
+
return this;
|
|
2981
|
+
}
|
|
2982
|
+
pop() {
|
|
2983
|
+
try {
|
|
2984
|
+
while (this.#size) {
|
|
2985
|
+
const val = this.#valList[this.#head];
|
|
2986
|
+
this.#evict(true);
|
|
2987
|
+
if (this.#isBackgroundFetch(val)) {
|
|
2988
|
+
if (val.__staleWhileFetching) {
|
|
2989
|
+
return val.__staleWhileFetching;
|
|
2990
|
+
}
|
|
2991
|
+
} else if (val !== undefined) {
|
|
2992
|
+
return val;
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
} finally {
|
|
2996
|
+
if (this.#hasDisposeAfter && this.#disposed) {
|
|
2997
|
+
const dt = this.#disposed;
|
|
2998
|
+
let task;
|
|
2999
|
+
while (task = dt?.shift()) {
|
|
3000
|
+
this.#disposeAfter?.(...task);
|
|
3001
|
+
}
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
#evict(free) {
|
|
3006
|
+
const head = this.#head;
|
|
3007
|
+
const k = this.#keyList[head];
|
|
3008
|
+
const v = this.#valList[head];
|
|
3009
|
+
if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
|
|
3010
|
+
v.__abortController.abort(new Error("evicted"));
|
|
3011
|
+
} else if (this.#hasDispose || this.#hasDisposeAfter) {
|
|
3012
|
+
if (this.#hasDispose) {
|
|
3013
|
+
this.#dispose?.(v, k, "evict");
|
|
3014
|
+
}
|
|
3015
|
+
if (this.#hasDisposeAfter) {
|
|
3016
|
+
this.#disposed?.push([v, k, "evict"]);
|
|
3017
|
+
}
|
|
3018
|
+
}
|
|
3019
|
+
this.#removeItemSize(head);
|
|
3020
|
+
if (this.#autopurgeTimers?.[head]) {
|
|
3021
|
+
clearTimeout(this.#autopurgeTimers[head]);
|
|
3022
|
+
this.#autopurgeTimers[head] = undefined;
|
|
3023
|
+
}
|
|
3024
|
+
if (free) {
|
|
3025
|
+
this.#keyList[head] = undefined;
|
|
3026
|
+
this.#valList[head] = undefined;
|
|
3027
|
+
this.#free.push(head);
|
|
3028
|
+
}
|
|
3029
|
+
if (this.#size === 1) {
|
|
3030
|
+
this.#head = this.#tail = 0;
|
|
3031
|
+
this.#free.length = 0;
|
|
3032
|
+
} else {
|
|
3033
|
+
this.#head = this.#next[head];
|
|
3034
|
+
}
|
|
3035
|
+
this.#keyMap.delete(k);
|
|
3036
|
+
this.#size--;
|
|
3037
|
+
return head;
|
|
3038
|
+
}
|
|
3039
|
+
has(k, hasOptions = {}) {
|
|
3040
|
+
const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
|
|
3041
|
+
const index = this.#keyMap.get(k);
|
|
3042
|
+
if (index !== undefined) {
|
|
3043
|
+
const v = this.#valList[index];
|
|
3044
|
+
if (this.#isBackgroundFetch(v) && v.__staleWhileFetching === undefined) {
|
|
3045
|
+
return false;
|
|
3046
|
+
}
|
|
3047
|
+
if (!this.#isStale(index)) {
|
|
3048
|
+
if (updateAgeOnHas) {
|
|
3049
|
+
this.#updateItemAge(index);
|
|
3050
|
+
}
|
|
3051
|
+
if (status) {
|
|
3052
|
+
status.has = "hit";
|
|
3053
|
+
this.#statusTTL(status, index);
|
|
3054
|
+
}
|
|
3055
|
+
return true;
|
|
3056
|
+
} else if (status) {
|
|
3057
|
+
status.has = "stale";
|
|
3058
|
+
this.#statusTTL(status, index);
|
|
3059
|
+
}
|
|
3060
|
+
} else if (status) {
|
|
3061
|
+
status.has = "miss";
|
|
3062
|
+
}
|
|
3063
|
+
return false;
|
|
3064
|
+
}
|
|
3065
|
+
peek(k, peekOptions = {}) {
|
|
3066
|
+
const { allowStale = this.allowStale } = peekOptions;
|
|
3067
|
+
const index = this.#keyMap.get(k);
|
|
3068
|
+
if (index === undefined || !allowStale && this.#isStale(index)) {
|
|
3069
|
+
return;
|
|
3070
|
+
}
|
|
3071
|
+
const v = this.#valList[index];
|
|
3072
|
+
return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
3073
|
+
}
|
|
3074
|
+
#backgroundFetch(k, index, options, context) {
|
|
3075
|
+
const v = index === undefined ? undefined : this.#valList[index];
|
|
3076
|
+
if (this.#isBackgroundFetch(v)) {
|
|
3077
|
+
return v;
|
|
3078
|
+
}
|
|
3079
|
+
const ac = new AC;
|
|
3080
|
+
const { signal } = options;
|
|
3081
|
+
signal?.addEventListener("abort", () => ac.abort(signal.reason), {
|
|
3082
|
+
signal: ac.signal
|
|
3083
|
+
});
|
|
3084
|
+
const fetchOpts = {
|
|
3085
|
+
signal: ac.signal,
|
|
3086
|
+
options,
|
|
3087
|
+
context
|
|
3088
|
+
};
|
|
3089
|
+
const cb = (v2, updateCache = false) => {
|
|
3090
|
+
const { aborted } = ac.signal;
|
|
3091
|
+
const ignoreAbort = options.ignoreFetchAbort && v2 !== undefined;
|
|
3092
|
+
if (options.status) {
|
|
3093
|
+
if (aborted && !updateCache) {
|
|
3094
|
+
options.status.fetchAborted = true;
|
|
3095
|
+
options.status.fetchError = ac.signal.reason;
|
|
3096
|
+
if (ignoreAbort)
|
|
3097
|
+
options.status.fetchAbortIgnored = true;
|
|
3098
|
+
} else {
|
|
3099
|
+
options.status.fetchResolved = true;
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
if (aborted && !ignoreAbort && !updateCache) {
|
|
3103
|
+
return fetchFail(ac.signal.reason);
|
|
3104
|
+
}
|
|
3105
|
+
const bf2 = p;
|
|
3106
|
+
const vl = this.#valList[index];
|
|
3107
|
+
if (vl === p || ignoreAbort && updateCache && vl === undefined) {
|
|
3108
|
+
if (v2 === undefined) {
|
|
3109
|
+
if (bf2.__staleWhileFetching !== undefined) {
|
|
3110
|
+
this.#valList[index] = bf2.__staleWhileFetching;
|
|
3111
|
+
} else {
|
|
3112
|
+
this.#delete(k, "fetch");
|
|
3113
|
+
}
|
|
3114
|
+
} else {
|
|
3115
|
+
if (options.status)
|
|
3116
|
+
options.status.fetchUpdated = true;
|
|
3117
|
+
this.set(k, v2, fetchOpts.options);
|
|
3118
|
+
}
|
|
3119
|
+
}
|
|
3120
|
+
return v2;
|
|
3121
|
+
};
|
|
3122
|
+
const eb = (er) => {
|
|
3123
|
+
if (options.status) {
|
|
3124
|
+
options.status.fetchRejected = true;
|
|
3125
|
+
options.status.fetchError = er;
|
|
3126
|
+
}
|
|
3127
|
+
return fetchFail(er);
|
|
3128
|
+
};
|
|
3129
|
+
const fetchFail = (er) => {
|
|
3130
|
+
const { aborted } = ac.signal;
|
|
3131
|
+
const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
|
|
3132
|
+
const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
|
|
3133
|
+
const noDelete = allowStale || options.noDeleteOnFetchRejection;
|
|
3134
|
+
const bf2 = p;
|
|
3135
|
+
if (this.#valList[index] === p) {
|
|
3136
|
+
const del = !noDelete || bf2.__staleWhileFetching === undefined;
|
|
3137
|
+
if (del) {
|
|
3138
|
+
this.#delete(k, "fetch");
|
|
3139
|
+
} else if (!allowStaleAborted) {
|
|
3140
|
+
this.#valList[index] = bf2.__staleWhileFetching;
|
|
3141
|
+
}
|
|
3142
|
+
}
|
|
3143
|
+
if (allowStale) {
|
|
3144
|
+
if (options.status && bf2.__staleWhileFetching !== undefined) {
|
|
3145
|
+
options.status.returnedStale = true;
|
|
3146
|
+
}
|
|
3147
|
+
return bf2.__staleWhileFetching;
|
|
3148
|
+
} else if (bf2.__returned === bf2) {
|
|
3149
|
+
throw er;
|
|
3150
|
+
}
|
|
3151
|
+
};
|
|
3152
|
+
const pcall = (res, rej) => {
|
|
3153
|
+
const fmp = this.#fetchMethod?.(k, v, fetchOpts);
|
|
3154
|
+
if (fmp && fmp instanceof Promise) {
|
|
3155
|
+
fmp.then((v2) => res(v2 === undefined ? undefined : v2), rej);
|
|
3156
|
+
}
|
|
3157
|
+
ac.signal.addEventListener("abort", () => {
|
|
3158
|
+
if (!options.ignoreFetchAbort || options.allowStaleOnFetchAbort) {
|
|
3159
|
+
res(undefined);
|
|
3160
|
+
if (options.allowStaleOnFetchAbort) {
|
|
3161
|
+
res = (v2) => cb(v2, true);
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
});
|
|
3165
|
+
};
|
|
3166
|
+
if (options.status)
|
|
3167
|
+
options.status.fetchDispatched = true;
|
|
3168
|
+
const p = new Promise(pcall).then(cb, eb);
|
|
3169
|
+
const bf = Object.assign(p, {
|
|
3170
|
+
__abortController: ac,
|
|
3171
|
+
__staleWhileFetching: v,
|
|
3172
|
+
__returned: undefined
|
|
3173
|
+
});
|
|
3174
|
+
if (index === undefined) {
|
|
3175
|
+
this.set(k, bf, { ...fetchOpts.options, status: undefined });
|
|
3176
|
+
index = this.#keyMap.get(k);
|
|
3177
|
+
} else {
|
|
3178
|
+
this.#valList[index] = bf;
|
|
3179
|
+
}
|
|
3180
|
+
return bf;
|
|
3181
|
+
}
|
|
3182
|
+
#isBackgroundFetch(p) {
|
|
3183
|
+
if (!this.#hasFetchMethod)
|
|
3184
|
+
return false;
|
|
3185
|
+
const b = p;
|
|
3186
|
+
return !!b && b instanceof Promise && b.hasOwnProperty("__staleWhileFetching") && b.__abortController instanceof AC;
|
|
3187
|
+
}
|
|
3188
|
+
async fetch(k, fetchOptions = {}) {
|
|
3189
|
+
const {
|
|
3190
|
+
allowStale = this.allowStale,
|
|
3191
|
+
updateAgeOnGet = this.updateAgeOnGet,
|
|
3192
|
+
noDeleteOnStaleGet = this.noDeleteOnStaleGet,
|
|
3193
|
+
ttl = this.ttl,
|
|
3194
|
+
noDisposeOnSet = this.noDisposeOnSet,
|
|
3195
|
+
size = 0,
|
|
3196
|
+
sizeCalculation = this.sizeCalculation,
|
|
3197
|
+
noUpdateTTL = this.noUpdateTTL,
|
|
3198
|
+
noDeleteOnFetchRejection = this.noDeleteOnFetchRejection,
|
|
3199
|
+
allowStaleOnFetchRejection = this.allowStaleOnFetchRejection,
|
|
3200
|
+
ignoreFetchAbort = this.ignoreFetchAbort,
|
|
3201
|
+
allowStaleOnFetchAbort = this.allowStaleOnFetchAbort,
|
|
3202
|
+
context,
|
|
3203
|
+
forceRefresh = false,
|
|
3204
|
+
status,
|
|
3205
|
+
signal
|
|
3206
|
+
} = fetchOptions;
|
|
3207
|
+
if (!this.#hasFetchMethod) {
|
|
3208
|
+
if (status)
|
|
3209
|
+
status.fetch = "get";
|
|
3210
|
+
return this.get(k, {
|
|
3211
|
+
allowStale,
|
|
3212
|
+
updateAgeOnGet,
|
|
3213
|
+
noDeleteOnStaleGet,
|
|
3214
|
+
status
|
|
3215
|
+
});
|
|
3216
|
+
}
|
|
3217
|
+
const options = {
|
|
3218
|
+
allowStale,
|
|
3219
|
+
updateAgeOnGet,
|
|
3220
|
+
noDeleteOnStaleGet,
|
|
3221
|
+
ttl,
|
|
3222
|
+
noDisposeOnSet,
|
|
3223
|
+
size,
|
|
3224
|
+
sizeCalculation,
|
|
3225
|
+
noUpdateTTL,
|
|
3226
|
+
noDeleteOnFetchRejection,
|
|
3227
|
+
allowStaleOnFetchRejection,
|
|
3228
|
+
allowStaleOnFetchAbort,
|
|
3229
|
+
ignoreFetchAbort,
|
|
3230
|
+
status,
|
|
3231
|
+
signal
|
|
3232
|
+
};
|
|
3233
|
+
let index = this.#keyMap.get(k);
|
|
3234
|
+
if (index === undefined) {
|
|
3235
|
+
if (status)
|
|
3236
|
+
status.fetch = "miss";
|
|
3237
|
+
const p = this.#backgroundFetch(k, index, options, context);
|
|
3238
|
+
return p.__returned = p;
|
|
3239
|
+
} else {
|
|
3240
|
+
const v = this.#valList[index];
|
|
3241
|
+
if (this.#isBackgroundFetch(v)) {
|
|
3242
|
+
const stale = allowStale && v.__staleWhileFetching !== undefined;
|
|
3243
|
+
if (status) {
|
|
3244
|
+
status.fetch = "inflight";
|
|
3245
|
+
if (stale)
|
|
3246
|
+
status.returnedStale = true;
|
|
3247
|
+
}
|
|
3248
|
+
return stale ? v.__staleWhileFetching : v.__returned = v;
|
|
3249
|
+
}
|
|
3250
|
+
const isStale = this.#isStale(index);
|
|
3251
|
+
if (!forceRefresh && !isStale) {
|
|
3252
|
+
if (status)
|
|
3253
|
+
status.fetch = "hit";
|
|
3254
|
+
this.#moveToTail(index);
|
|
3255
|
+
if (updateAgeOnGet) {
|
|
3256
|
+
this.#updateItemAge(index);
|
|
3257
|
+
}
|
|
3258
|
+
if (status)
|
|
3259
|
+
this.#statusTTL(status, index);
|
|
3260
|
+
return v;
|
|
3261
|
+
}
|
|
3262
|
+
const p = this.#backgroundFetch(k, index, options, context);
|
|
3263
|
+
const hasStale = p.__staleWhileFetching !== undefined;
|
|
3264
|
+
const staleVal = hasStale && allowStale;
|
|
3265
|
+
if (status) {
|
|
3266
|
+
status.fetch = isStale ? "stale" : "refresh";
|
|
3267
|
+
if (staleVal && isStale)
|
|
3268
|
+
status.returnedStale = true;
|
|
3269
|
+
}
|
|
3270
|
+
return staleVal ? p.__staleWhileFetching : p.__returned = p;
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
3273
|
+
async forceFetch(k, fetchOptions = {}) {
|
|
3274
|
+
const v = await this.fetch(k, fetchOptions);
|
|
3275
|
+
if (v === undefined)
|
|
3276
|
+
throw new Error("fetch() returned undefined");
|
|
3277
|
+
return v;
|
|
3278
|
+
}
|
|
3279
|
+
memo(k, memoOptions = {}) {
|
|
3280
|
+
const memoMethod = this.#memoMethod;
|
|
3281
|
+
if (!memoMethod) {
|
|
3282
|
+
throw new Error("no memoMethod provided to constructor");
|
|
3283
|
+
}
|
|
3284
|
+
const { context, forceRefresh, ...options } = memoOptions;
|
|
3285
|
+
const v = this.get(k, options);
|
|
3286
|
+
if (!forceRefresh && v !== undefined)
|
|
3287
|
+
return v;
|
|
3288
|
+
const vv = memoMethod(k, v, {
|
|
3289
|
+
options,
|
|
3290
|
+
context
|
|
3291
|
+
});
|
|
3292
|
+
this.set(k, vv, options);
|
|
3293
|
+
return vv;
|
|
3294
|
+
}
|
|
3295
|
+
get(k, getOptions = {}) {
|
|
3296
|
+
const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status } = getOptions;
|
|
3297
|
+
const index = this.#keyMap.get(k);
|
|
3298
|
+
if (index !== undefined) {
|
|
3299
|
+
const value = this.#valList[index];
|
|
3300
|
+
const fetching = this.#isBackgroundFetch(value);
|
|
3301
|
+
if (status)
|
|
3302
|
+
this.#statusTTL(status, index);
|
|
3303
|
+
if (this.#isStale(index)) {
|
|
3304
|
+
if (status)
|
|
3305
|
+
status.get = "stale";
|
|
3306
|
+
if (!fetching) {
|
|
3307
|
+
if (!noDeleteOnStaleGet) {
|
|
3308
|
+
this.#delete(k, "expire");
|
|
3309
|
+
}
|
|
3310
|
+
if (status && allowStale)
|
|
3311
|
+
status.returnedStale = true;
|
|
3312
|
+
return allowStale ? value : undefined;
|
|
3313
|
+
} else {
|
|
3314
|
+
if (status && allowStale && value.__staleWhileFetching !== undefined) {
|
|
3315
|
+
status.returnedStale = true;
|
|
3316
|
+
}
|
|
3317
|
+
return allowStale ? value.__staleWhileFetching : undefined;
|
|
3318
|
+
}
|
|
3319
|
+
} else {
|
|
3320
|
+
if (status)
|
|
3321
|
+
status.get = "hit";
|
|
3322
|
+
if (fetching) {
|
|
3323
|
+
return value.__staleWhileFetching;
|
|
3324
|
+
}
|
|
3325
|
+
this.#moveToTail(index);
|
|
3326
|
+
if (updateAgeOnGet) {
|
|
3327
|
+
this.#updateItemAge(index);
|
|
3328
|
+
}
|
|
3329
|
+
return value;
|
|
3330
|
+
}
|
|
3331
|
+
} else if (status) {
|
|
3332
|
+
status.get = "miss";
|
|
3333
|
+
}
|
|
3334
|
+
}
|
|
3335
|
+
#connect(p, n) {
|
|
3336
|
+
this.#prev[n] = p;
|
|
3337
|
+
this.#next[p] = n;
|
|
3338
|
+
}
|
|
3339
|
+
#moveToTail(index) {
|
|
3340
|
+
if (index !== this.#tail) {
|
|
3341
|
+
if (index === this.#head) {
|
|
3342
|
+
this.#head = this.#next[index];
|
|
3343
|
+
} else {
|
|
3344
|
+
this.#connect(this.#prev[index], this.#next[index]);
|
|
3345
|
+
}
|
|
3346
|
+
this.#connect(this.#tail, index);
|
|
3347
|
+
this.#tail = index;
|
|
3348
|
+
}
|
|
3349
|
+
}
|
|
3350
|
+
delete(k) {
|
|
3351
|
+
return this.#delete(k, "delete");
|
|
3352
|
+
}
|
|
3353
|
+
#delete(k, reason) {
|
|
3354
|
+
let deleted = false;
|
|
3355
|
+
if (this.#size !== 0) {
|
|
3356
|
+
const index = this.#keyMap.get(k);
|
|
3357
|
+
if (index !== undefined) {
|
|
3358
|
+
if (this.#autopurgeTimers?.[index]) {
|
|
3359
|
+
clearTimeout(this.#autopurgeTimers?.[index]);
|
|
3360
|
+
this.#autopurgeTimers[index] = undefined;
|
|
3361
|
+
}
|
|
3362
|
+
deleted = true;
|
|
3363
|
+
if (this.#size === 1) {
|
|
3364
|
+
this.#clear(reason);
|
|
3365
|
+
} else {
|
|
3366
|
+
this.#removeItemSize(index);
|
|
3367
|
+
const v = this.#valList[index];
|
|
3368
|
+
if (this.#isBackgroundFetch(v)) {
|
|
3369
|
+
v.__abortController.abort(new Error("deleted"));
|
|
3370
|
+
} else if (this.#hasDispose || this.#hasDisposeAfter) {
|
|
3371
|
+
if (this.#hasDispose) {
|
|
3372
|
+
this.#dispose?.(v, k, reason);
|
|
3373
|
+
}
|
|
3374
|
+
if (this.#hasDisposeAfter) {
|
|
3375
|
+
this.#disposed?.push([v, k, reason]);
|
|
3376
|
+
}
|
|
3377
|
+
}
|
|
3378
|
+
this.#keyMap.delete(k);
|
|
3379
|
+
this.#keyList[index] = undefined;
|
|
3380
|
+
this.#valList[index] = undefined;
|
|
3381
|
+
if (index === this.#tail) {
|
|
3382
|
+
this.#tail = this.#prev[index];
|
|
3383
|
+
} else if (index === this.#head) {
|
|
3384
|
+
this.#head = this.#next[index];
|
|
3385
|
+
} else {
|
|
3386
|
+
const pi = this.#prev[index];
|
|
3387
|
+
this.#next[pi] = this.#next[index];
|
|
3388
|
+
const ni = this.#next[index];
|
|
3389
|
+
this.#prev[ni] = this.#prev[index];
|
|
3390
|
+
}
|
|
3391
|
+
this.#size--;
|
|
3392
|
+
this.#free.push(index);
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
}
|
|
3396
|
+
if (this.#hasDisposeAfter && this.#disposed?.length) {
|
|
3397
|
+
const dt = this.#disposed;
|
|
3398
|
+
let task;
|
|
3399
|
+
while (task = dt?.shift()) {
|
|
3400
|
+
this.#disposeAfter?.(...task);
|
|
3401
|
+
}
|
|
3402
|
+
}
|
|
3403
|
+
return deleted;
|
|
3404
|
+
}
|
|
3405
|
+
clear() {
|
|
3406
|
+
return this.#clear("delete");
|
|
3407
|
+
}
|
|
3408
|
+
#clear(reason) {
|
|
3409
|
+
for (const index of this.#rindexes({ allowStale: true })) {
|
|
3410
|
+
const v = this.#valList[index];
|
|
3411
|
+
if (this.#isBackgroundFetch(v)) {
|
|
3412
|
+
v.__abortController.abort(new Error("deleted"));
|
|
3413
|
+
} else {
|
|
3414
|
+
const k = this.#keyList[index];
|
|
3415
|
+
if (this.#hasDispose) {
|
|
3416
|
+
this.#dispose?.(v, k, reason);
|
|
3417
|
+
}
|
|
3418
|
+
if (this.#hasDisposeAfter) {
|
|
3419
|
+
this.#disposed?.push([v, k, reason]);
|
|
3420
|
+
}
|
|
3421
|
+
}
|
|
3422
|
+
}
|
|
3423
|
+
this.#keyMap.clear();
|
|
3424
|
+
this.#valList.fill(undefined);
|
|
3425
|
+
this.#keyList.fill(undefined);
|
|
3426
|
+
if (this.#ttls && this.#starts) {
|
|
3427
|
+
this.#ttls.fill(0);
|
|
3428
|
+
this.#starts.fill(0);
|
|
3429
|
+
for (const t of this.#autopurgeTimers ?? []) {
|
|
3430
|
+
if (t !== undefined)
|
|
3431
|
+
clearTimeout(t);
|
|
3432
|
+
}
|
|
3433
|
+
this.#autopurgeTimers?.fill(undefined);
|
|
3434
|
+
}
|
|
3435
|
+
if (this.#sizes) {
|
|
3436
|
+
this.#sizes.fill(0);
|
|
3437
|
+
}
|
|
3438
|
+
this.#head = 0;
|
|
3439
|
+
this.#tail = 0;
|
|
3440
|
+
this.#free.length = 0;
|
|
3441
|
+
this.#calculatedSize = 0;
|
|
3442
|
+
this.#size = 0;
|
|
3443
|
+
if (this.#hasDisposeAfter && this.#disposed) {
|
|
3444
|
+
const dt = this.#disposed;
|
|
3445
|
+
let task;
|
|
3446
|
+
while (task = dt?.shift()) {
|
|
3447
|
+
this.#disposeAfter?.(...task);
|
|
3448
|
+
}
|
|
3449
|
+
}
|
|
3450
|
+
}
|
|
3451
|
+
}
|
|
3452
|
+
|
|
3453
|
+
// src/query.ts
|
|
3454
|
+
class AuthQuery extends Query {
|
|
3455
|
+
cache;
|
|
3456
|
+
constructor(opts = {}) {
|
|
3457
|
+
if (!opts.url) {
|
|
3458
|
+
opts.url = "https://kevisual.cn/api/router/";
|
|
3459
|
+
}
|
|
3460
|
+
super(opts);
|
|
3461
|
+
this.cache = new LRUCache({
|
|
3462
|
+
max: 1e4,
|
|
3463
|
+
ttl: 1000 * 60 * 60 * 2
|
|
3464
|
+
});
|
|
3465
|
+
}
|
|
3466
|
+
getTokenUser = async (token) => {
|
|
3467
|
+
const res = await this.post({
|
|
3468
|
+
path: "user",
|
|
3469
|
+
key: "me",
|
|
3470
|
+
token
|
|
3471
|
+
});
|
|
3472
|
+
return res;
|
|
3473
|
+
};
|
|
3474
|
+
getTokenUserCache = async (token) => {
|
|
3475
|
+
const tokenUser = await this.cache.get(token);
|
|
3476
|
+
if (tokenUser) {
|
|
3477
|
+
return {
|
|
3478
|
+
code: 200,
|
|
3479
|
+
data: tokenUser
|
|
3480
|
+
};
|
|
3481
|
+
}
|
|
3482
|
+
const res = await this.getTokenUser(token);
|
|
3483
|
+
if (res.code === 200) {
|
|
3484
|
+
this.cache.set(token, res.data);
|
|
3485
|
+
}
|
|
3486
|
+
return res;
|
|
3487
|
+
};
|
|
3488
|
+
}
|
|
1850
3489
|
export {
|
|
1851
3490
|
verifyJWT,
|
|
1852
3491
|
signJWT,
|
|
1853
|
-
|
|
3492
|
+
generate,
|
|
3493
|
+
decodeJWT,
|
|
3494
|
+
AuthQuery
|
|
1854
3495
|
};
|