rezo 1.0.0
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/LICENSE +202 -0
- package/README.md +1507 -0
- package/assets/icon.svg +37 -0
- package/assets/logo-dark.svg +47 -0
- package/assets/logo.svg +58 -0
- package/dist/adapters/curl.cjs +1034 -0
- package/dist/adapters/curl.js +1031 -0
- package/dist/adapters/entries/curl.cjs +4 -0
- package/dist/adapters/entries/curl.d.ts +2136 -0
- package/dist/adapters/entries/curl.js +2 -0
- package/dist/adapters/entries/fetch.cjs +2 -0
- package/dist/adapters/entries/fetch.d.ts +2127 -0
- package/dist/adapters/entries/fetch.js +1 -0
- package/dist/adapters/entries/http.cjs +2 -0
- package/dist/adapters/entries/http.d.ts +2126 -0
- package/dist/adapters/entries/http.js +1 -0
- package/dist/adapters/entries/http2.cjs +4 -0
- package/dist/adapters/entries/http2.d.ts +2136 -0
- package/dist/adapters/entries/http2.js +2 -0
- package/dist/adapters/entries/react-native.cjs +2 -0
- package/dist/adapters/entries/react-native.d.ts +2126 -0
- package/dist/adapters/entries/react-native.js +1 -0
- package/dist/adapters/entries/xhr.cjs +2 -0
- package/dist/adapters/entries/xhr.d.ts +2127 -0
- package/dist/adapters/entries/xhr.js +1 -0
- package/dist/adapters/fetch.cjs +740 -0
- package/dist/adapters/fetch.js +739 -0
- package/dist/adapters/http.cjs +1153 -0
- package/dist/adapters/http.js +1151 -0
- package/dist/adapters/http2.cjs +957 -0
- package/dist/adapters/http2.js +956 -0
- package/dist/adapters/index.cjs +6 -0
- package/dist/adapters/index.js +7 -0
- package/dist/adapters/picker.cjs +342 -0
- package/dist/adapters/picker.js +331 -0
- package/dist/adapters/react-native.cjs +545 -0
- package/dist/adapters/react-native.js +544 -0
- package/dist/adapters/xhr.cjs +622 -0
- package/dist/adapters/xhr.js +621 -0
- package/dist/cache/dns-cache.cjs +118 -0
- package/dist/cache/dns-cache.js +113 -0
- package/dist/cache/file-cacher.cjs +264 -0
- package/dist/cache/file-cacher.js +261 -0
- package/dist/cache/index.cjs +13 -0
- package/dist/cache/index.js +5 -0
- package/dist/cache/lru-cache.cjs +96 -0
- package/dist/cache/lru-cache.js +93 -0
- package/dist/cache/response-cache.cjs +314 -0
- package/dist/cache/response-cache.js +310 -0
- package/dist/cache/url-store.cjs +288 -0
- package/dist/cache/url-store.js +285 -0
- package/dist/core/hooks.cjs +133 -0
- package/dist/core/hooks.js +120 -0
- package/dist/core/rezo.cjs +464 -0
- package/dist/core/rezo.js +458 -0
- package/dist/crawler.d.ts +6255 -0
- package/dist/dom/index.cjs +1 -0
- package/dist/dom/index.d.ts +23 -0
- package/dist/dom/index.js +1 -0
- package/dist/entries/crawler.cjs +5 -0
- package/dist/entries/crawler.js +2 -0
- package/dist/errors/rezo-error.cjs +722 -0
- package/dist/errors/rezo-error.js +716 -0
- package/dist/index.cjs +34 -0
- package/dist/index.d.ts +3335 -0
- package/dist/index.js +26 -0
- package/dist/platform/browser.cjs +9 -0
- package/dist/platform/browser.d.ts +3203 -0
- package/dist/platform/browser.js +7 -0
- package/dist/platform/bun.cjs +9 -0
- package/dist/platform/bun.d.ts +3203 -0
- package/dist/platform/bun.js +7 -0
- package/dist/platform/deno.cjs +9 -0
- package/dist/platform/deno.d.ts +3203 -0
- package/dist/platform/deno.js +7 -0
- package/dist/platform/node.cjs +9 -0
- package/dist/platform/node.d.ts +3203 -0
- package/dist/platform/node.js +7 -0
- package/dist/platform/react-native.cjs +9 -0
- package/dist/platform/react-native.d.ts +3203 -0
- package/dist/platform/react-native.js +7 -0
- package/dist/platform/worker.cjs +9 -0
- package/dist/platform/worker.d.ts +3203 -0
- package/dist/platform/worker.js +7 -0
- package/dist/plugin/addon/decodo/index.cjs +1 -0
- package/dist/plugin/addon/decodo/index.js +1 -0
- package/dist/plugin/addon/decodo/options.cjs +1 -0
- package/dist/plugin/addon/decodo/options.js +1 -0
- package/dist/plugin/addon/oxylabs/index.cjs +1 -0
- package/dist/plugin/addon/oxylabs/index.js +1 -0
- package/dist/plugin/addon/oxylabs/options.cjs +1 -0
- package/dist/plugin/addon/oxylabs/options.js +1 -0
- package/dist/plugin/crawler-options.cjs +1 -0
- package/dist/plugin/crawler-options.js +1 -0
- package/dist/plugin/crawler.cjs +519 -0
- package/dist/plugin/crawler.js +517 -0
- package/dist/plugin/index.cjs +36 -0
- package/dist/plugin/index.js +32 -0
- package/dist/proxy/index.cjs +142 -0
- package/dist/proxy/index.js +139 -0
- package/dist/responses/buildError.cjs +452 -0
- package/dist/responses/buildError.js +441 -0
- package/dist/responses/buildResponse.cjs +365 -0
- package/dist/responses/buildResponse.js +361 -0
- package/dist/responses/download.cjs +54 -0
- package/dist/responses/download.js +52 -0
- package/dist/responses/stream.cjs +60 -0
- package/dist/responses/stream.js +58 -0
- package/dist/responses/upload.cjs +54 -0
- package/dist/responses/upload.js +52 -0
- package/dist/types/cookies.cjs +394 -0
- package/dist/types/cookies.js +391 -0
- package/dist/types/download.cjs +10 -0
- package/dist/types/download.js +10 -0
- package/dist/types/rezo-request.cjs +131 -0
- package/dist/types/rezo-request.js +131 -0
- package/dist/utils/agent-merger.cjs +111 -0
- package/dist/utils/agent-merger.js +108 -0
- package/dist/utils/compression.cjs +84 -0
- package/dist/utils/compression.js +82 -0
- package/dist/utils/cookies.cjs +514 -0
- package/dist/utils/cookies.js +511 -0
- package/dist/utils/data-operations.cjs +75 -0
- package/dist/utils/data-operations.js +73 -0
- package/dist/utils/form-data.cjs +164 -0
- package/dist/utils/form-data.js +161 -0
- package/dist/utils/headers.cjs +162 -0
- package/dist/utils/headers.js +161 -0
- package/dist/utils/http-config.cjs +723 -0
- package/dist/utils/http-config.js +718 -0
- package/dist/utils/index.cjs +8 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/tools.cjs +18 -0
- package/dist/utils/tools.js +15 -0
- package/package.json +172 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
function detectRuntime() {
|
|
5
|
+
if (typeof globalThis.Bun !== "undefined") {
|
|
6
|
+
return "bun";
|
|
7
|
+
}
|
|
8
|
+
if (typeof globalThis.Deno !== "undefined") {
|
|
9
|
+
return "deno";
|
|
10
|
+
}
|
|
11
|
+
return "node";
|
|
12
|
+
}
|
|
13
|
+
async function createDatabase(dbPath) {
|
|
14
|
+
const runtime = detectRuntime();
|
|
15
|
+
if (runtime === "bun") {
|
|
16
|
+
const { Database } = await import("bun:sqlite");
|
|
17
|
+
const db = new Database(dbPath);
|
|
18
|
+
return {
|
|
19
|
+
run: (sql, ...params) => db.run(sql, ...params),
|
|
20
|
+
get: (sql, ...params) => db.query(sql).get(...params),
|
|
21
|
+
all: (sql, ...params) => db.query(sql).all(...params),
|
|
22
|
+
close: () => db.close()
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (runtime === "deno") {
|
|
26
|
+
try {
|
|
27
|
+
const { Database } = await import("node:sqlite");
|
|
28
|
+
const db = new Database(dbPath);
|
|
29
|
+
return {
|
|
30
|
+
run: (sql, ...params) => db.exec(sql, params),
|
|
31
|
+
get: (sql, ...params) => {
|
|
32
|
+
const stmt = db.prepare(sql);
|
|
33
|
+
return stmt.get(...params);
|
|
34
|
+
},
|
|
35
|
+
all: (sql, ...params) => {
|
|
36
|
+
const stmt = db.prepare(sql);
|
|
37
|
+
return stmt.all(...params);
|
|
38
|
+
},
|
|
39
|
+
close: () => db.close()
|
|
40
|
+
};
|
|
41
|
+
} catch {
|
|
42
|
+
throw new Error("Deno SQLite support requires Node.js compatibility mode");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
46
|
+
const db = new DatabaseSync(dbPath);
|
|
47
|
+
return {
|
|
48
|
+
run: (sql, ...params) => db.exec(sql),
|
|
49
|
+
get: (sql, ...params) => {
|
|
50
|
+
const stmt = db.prepare(sql);
|
|
51
|
+
return stmt.get(...params);
|
|
52
|
+
},
|
|
53
|
+
all: (sql, ...params) => {
|
|
54
|
+
const stmt = db.prepare(sql);
|
|
55
|
+
return stmt.all(...params);
|
|
56
|
+
},
|
|
57
|
+
close: () => db.close()
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async function compressData(data) {
|
|
61
|
+
try {
|
|
62
|
+
const zlib = await import("node:zlib");
|
|
63
|
+
if (typeof zlib.zstdCompressSync === "function") {
|
|
64
|
+
return zlib.zstdCompressSync(data);
|
|
65
|
+
}
|
|
66
|
+
return data;
|
|
67
|
+
} catch {
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function decompressData(data) {
|
|
72
|
+
try {
|
|
73
|
+
const zlib = await import("node:zlib");
|
|
74
|
+
if (typeof zlib.zstdDecompressSync === "function") {
|
|
75
|
+
return zlib.zstdDecompressSync(data);
|
|
76
|
+
}
|
|
77
|
+
return data;
|
|
78
|
+
} catch {
|
|
79
|
+
return data;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class FileCacher {
|
|
84
|
+
databases = new Map;
|
|
85
|
+
options;
|
|
86
|
+
cacheDir;
|
|
87
|
+
closed = false;
|
|
88
|
+
constructor(options = {}) {
|
|
89
|
+
this.options = {
|
|
90
|
+
cacheDir: options.cacheDir || "./cache",
|
|
91
|
+
ttl: options.ttl || 604800000,
|
|
92
|
+
compression: options.compression ?? false,
|
|
93
|
+
softDelete: options.softDelete ?? false,
|
|
94
|
+
encryptNamespace: options.encryptNamespace ?? false,
|
|
95
|
+
maxEntries: options.maxEntries ?? 0
|
|
96
|
+
};
|
|
97
|
+
this.cacheDir = path.resolve(this.options.cacheDir);
|
|
98
|
+
if (!fs.existsSync(this.cacheDir)) {
|
|
99
|
+
fs.mkdirSync(this.cacheDir, { recursive: true });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
static async create(options = {}) {
|
|
103
|
+
const cacher = new FileCacher(options);
|
|
104
|
+
return cacher;
|
|
105
|
+
}
|
|
106
|
+
async getDatabase(namespace) {
|
|
107
|
+
const nsKey = this.options.encryptNamespace ? createHash("md5").update(namespace).digest("hex") : namespace.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
108
|
+
if (this.databases.has(nsKey)) {
|
|
109
|
+
return this.databases.get(nsKey);
|
|
110
|
+
}
|
|
111
|
+
const dbPath = path.join(this.cacheDir, `${nsKey}.db`);
|
|
112
|
+
const db = await createDatabase(dbPath);
|
|
113
|
+
db.run(`
|
|
114
|
+
CREATE TABLE IF NOT EXISTS cache (
|
|
115
|
+
key TEXT PRIMARY KEY,
|
|
116
|
+
value TEXT NOT NULL,
|
|
117
|
+
expiresAt INTEGER NOT NULL,
|
|
118
|
+
createdAt INTEGER NOT NULL,
|
|
119
|
+
compressed INTEGER DEFAULT 0,
|
|
120
|
+
deleted INTEGER DEFAULT 0
|
|
121
|
+
)
|
|
122
|
+
`);
|
|
123
|
+
db.run("CREATE INDEX IF NOT EXISTS idx_expires ON cache(expiresAt)");
|
|
124
|
+
db.run("CREATE INDEX IF NOT EXISTS idx_deleted ON cache(deleted)");
|
|
125
|
+
this.databases.set(nsKey, db);
|
|
126
|
+
return db;
|
|
127
|
+
}
|
|
128
|
+
async set(key, value, ttl, namespace = "default") {
|
|
129
|
+
if (this.closed)
|
|
130
|
+
throw new Error("FileCacher is closed");
|
|
131
|
+
const db = await this.getDatabase(namespace);
|
|
132
|
+
const now = Date.now();
|
|
133
|
+
const expiresAt = now + (ttl ?? this.options.ttl);
|
|
134
|
+
let serialized = JSON.stringify(value);
|
|
135
|
+
let compressed = 0;
|
|
136
|
+
if (this.options.compression) {
|
|
137
|
+
try {
|
|
138
|
+
const compressedData = await compressData(Buffer.from(serialized, "utf-8"));
|
|
139
|
+
serialized = compressedData.toString("base64");
|
|
140
|
+
compressed = 1;
|
|
141
|
+
} catch {}
|
|
142
|
+
}
|
|
143
|
+
db.run(`
|
|
144
|
+
INSERT OR REPLACE INTO cache (key, value, expiresAt, createdAt, compressed, deleted)
|
|
145
|
+
VALUES (?, ?, ?, ?, ?, 0)
|
|
146
|
+
`, key, serialized, expiresAt, now, compressed);
|
|
147
|
+
if (this.options.maxEntries > 0) {
|
|
148
|
+
const count = db.get("SELECT COUNT(*) as cnt FROM cache WHERE deleted = 0");
|
|
149
|
+
if (count && count.cnt > this.options.maxEntries) {
|
|
150
|
+
const excess = count.cnt - this.options.maxEntries;
|
|
151
|
+
db.run(`
|
|
152
|
+
DELETE FROM cache WHERE key IN (
|
|
153
|
+
SELECT key FROM cache WHERE deleted = 0 ORDER BY createdAt ASC LIMIT ?
|
|
154
|
+
)
|
|
155
|
+
`, excess);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async get(key, namespace = "default") {
|
|
160
|
+
if (this.closed)
|
|
161
|
+
throw new Error("FileCacher is closed");
|
|
162
|
+
const db = await this.getDatabase(namespace);
|
|
163
|
+
const entry = db.get("SELECT * FROM cache WHERE key = ? AND deleted = 0", key);
|
|
164
|
+
if (!entry)
|
|
165
|
+
return null;
|
|
166
|
+
if (entry.expiresAt < Date.now()) {
|
|
167
|
+
if (this.options.softDelete) {
|
|
168
|
+
db.run("UPDATE cache SET deleted = 1 WHERE key = ?", key);
|
|
169
|
+
} else {
|
|
170
|
+
db.run("DELETE FROM cache WHERE key = ?", key);
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
let value = entry.value;
|
|
175
|
+
if (entry.compressed) {
|
|
176
|
+
try {
|
|
177
|
+
const decompressed = await decompressData(Buffer.from(value, "base64"));
|
|
178
|
+
value = decompressed.toString("utf-8");
|
|
179
|
+
} catch {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
return JSON.parse(value);
|
|
185
|
+
} catch {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async has(key, namespace = "default") {
|
|
190
|
+
if (this.closed)
|
|
191
|
+
return false;
|
|
192
|
+
const db = await this.getDatabase(namespace);
|
|
193
|
+
const entry = db.get("SELECT key, expiresAt FROM cache WHERE key = ? AND deleted = 0", key);
|
|
194
|
+
if (!entry)
|
|
195
|
+
return false;
|
|
196
|
+
return entry.expiresAt >= Date.now();
|
|
197
|
+
}
|
|
198
|
+
async delete(key, namespace = "default") {
|
|
199
|
+
if (this.closed)
|
|
200
|
+
return false;
|
|
201
|
+
const db = await this.getDatabase(namespace);
|
|
202
|
+
if (this.options.softDelete) {
|
|
203
|
+
db.run("UPDATE cache SET deleted = 1 WHERE key = ?", key);
|
|
204
|
+
} else {
|
|
205
|
+
db.run("DELETE FROM cache WHERE key = ?", key);
|
|
206
|
+
}
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
async clear(namespace = "default") {
|
|
210
|
+
if (this.closed)
|
|
211
|
+
return;
|
|
212
|
+
const db = await this.getDatabase(namespace);
|
|
213
|
+
if (this.options.softDelete) {
|
|
214
|
+
db.run("UPDATE cache SET deleted = 1");
|
|
215
|
+
} else {
|
|
216
|
+
db.run("DELETE FROM cache");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async cleanup(namespace = "default") {
|
|
220
|
+
if (this.closed)
|
|
221
|
+
return 0;
|
|
222
|
+
const db = await this.getDatabase(namespace);
|
|
223
|
+
const now = Date.now();
|
|
224
|
+
const countBefore = db.get("SELECT COUNT(*) as cnt FROM cache");
|
|
225
|
+
db.run("DELETE FROM cache WHERE expiresAt < ? OR deleted = 1", now);
|
|
226
|
+
const countAfter = db.get("SELECT COUNT(*) as cnt FROM cache");
|
|
227
|
+
return (countBefore?.cnt || 0) - (countAfter?.cnt || 0);
|
|
228
|
+
}
|
|
229
|
+
async stats(namespace = "default") {
|
|
230
|
+
if (this.closed)
|
|
231
|
+
return { count: 0, expired: 0, deleted: 0 };
|
|
232
|
+
const db = await this.getDatabase(namespace);
|
|
233
|
+
const now = Date.now();
|
|
234
|
+
const total = db.get("SELECT COUNT(*) as cnt FROM cache WHERE deleted = 0");
|
|
235
|
+
const expired = db.get("SELECT COUNT(*) as cnt FROM cache WHERE expiresAt < ? AND deleted = 0", now);
|
|
236
|
+
const deleted = db.get("SELECT COUNT(*) as cnt FROM cache WHERE deleted = 1");
|
|
237
|
+
return {
|
|
238
|
+
count: total?.cnt || 0,
|
|
239
|
+
expired: expired?.cnt || 0,
|
|
240
|
+
deleted: deleted?.cnt || 0
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async close() {
|
|
244
|
+
if (this.closed)
|
|
245
|
+
return;
|
|
246
|
+
this.closed = true;
|
|
247
|
+
for (const db of this.databases.values()) {
|
|
248
|
+
try {
|
|
249
|
+
db.close();
|
|
250
|
+
} catch {}
|
|
251
|
+
}
|
|
252
|
+
this.databases.clear();
|
|
253
|
+
}
|
|
254
|
+
get isClosed() {
|
|
255
|
+
return this.closed;
|
|
256
|
+
}
|
|
257
|
+
get directory() {
|
|
258
|
+
return this.cacheDir;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
export default FileCacher;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const _mod_78rn6v = require('./lru-cache.cjs');
|
|
2
|
+
exports.LRUCache = _mod_78rn6v.LRUCache;;
|
|
3
|
+
const _mod_7nn01n = require('./dns-cache.cjs');
|
|
4
|
+
exports.DNSCache = _mod_7nn01n.DNSCache;
|
|
5
|
+
exports.getGlobalDNSCache = _mod_7nn01n.getGlobalDNSCache;
|
|
6
|
+
exports.resetGlobalDNSCache = _mod_7nn01n.resetGlobalDNSCache;;
|
|
7
|
+
const _mod_2ymoud = require('./response-cache.cjs');
|
|
8
|
+
exports.ResponseCache = _mod_2ymoud.ResponseCache;
|
|
9
|
+
exports.normalizeResponseCacheConfig = _mod_2ymoud.normalizeResponseCacheConfig;;
|
|
10
|
+
const _mod_8aprzi = require('./file-cacher.cjs');
|
|
11
|
+
exports.FileCacher = _mod_8aprzi.FileCacher;;
|
|
12
|
+
const _mod_kdtltr = require('./url-store.cjs');
|
|
13
|
+
exports.UrlStore = _mod_kdtltr.UrlStore;;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { LRUCache } from './lru-cache.js';
|
|
2
|
+
export { DNSCache, getGlobalDNSCache, resetGlobalDNSCache } from './dns-cache.js';
|
|
3
|
+
export { ResponseCache, normalizeResponseCacheConfig } from './response-cache.js';
|
|
4
|
+
export { FileCacher } from './file-cacher.js';
|
|
5
|
+
export { UrlStore } from './url-store.js';
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
class LRUCache {
|
|
2
|
+
cache = new Map;
|
|
3
|
+
maxEntries;
|
|
4
|
+
defaultTTL;
|
|
5
|
+
onEvict;
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.maxEntries = options.maxEntries ?? 500;
|
|
8
|
+
this.defaultTTL = options.ttl ?? 3000000;
|
|
9
|
+
this.onEvict = options.onEvict;
|
|
10
|
+
}
|
|
11
|
+
get(key) {
|
|
12
|
+
const entry = this.cache.get(key);
|
|
13
|
+
if (!entry) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (entry.expires > 0 && Date.now() > entry.expires) {
|
|
17
|
+
this.delete(key);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.cache.delete(key);
|
|
21
|
+
this.cache.set(key, entry);
|
|
22
|
+
return entry.value;
|
|
23
|
+
}
|
|
24
|
+
set(key, value, ttl) {
|
|
25
|
+
if (this.cache.has(key)) {
|
|
26
|
+
this.cache.delete(key);
|
|
27
|
+
}
|
|
28
|
+
while (this.cache.size >= this.maxEntries) {
|
|
29
|
+
const oldestKey = this.cache.keys().next().value;
|
|
30
|
+
if (oldestKey !== undefined) {
|
|
31
|
+
const oldEntry = this.cache.get(oldestKey);
|
|
32
|
+
this.cache.delete(oldestKey);
|
|
33
|
+
if (this.onEvict && oldEntry) {
|
|
34
|
+
this.onEvict(oldestKey, oldEntry.value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const actualTTL = ttl ?? this.defaultTTL;
|
|
39
|
+
const expires = actualTTL > 0 ? Date.now() + actualTTL : 0;
|
|
40
|
+
this.cache.set(key, { value, expires });
|
|
41
|
+
}
|
|
42
|
+
has(key) {
|
|
43
|
+
const entry = this.cache.get(key);
|
|
44
|
+
if (!entry) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (entry.expires > 0 && Date.now() > entry.expires) {
|
|
48
|
+
this.delete(key);
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
delete(key) {
|
|
54
|
+
const entry = this.cache.get(key);
|
|
55
|
+
const deleted = this.cache.delete(key);
|
|
56
|
+
if (deleted && this.onEvict && entry) {
|
|
57
|
+
this.onEvict(key, entry.value);
|
|
58
|
+
}
|
|
59
|
+
return deleted;
|
|
60
|
+
}
|
|
61
|
+
clear() {
|
|
62
|
+
if (this.onEvict) {
|
|
63
|
+
for (const [key, entry] of this.cache) {
|
|
64
|
+
this.onEvict(key, entry.value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.cache.clear();
|
|
68
|
+
}
|
|
69
|
+
get size() {
|
|
70
|
+
return this.cache.size;
|
|
71
|
+
}
|
|
72
|
+
keys() {
|
|
73
|
+
return Array.from(this.cache.keys());
|
|
74
|
+
}
|
|
75
|
+
prune() {
|
|
76
|
+
const now = Date.now();
|
|
77
|
+
let pruned = 0;
|
|
78
|
+
for (const [key, entry] of this.cache) {
|
|
79
|
+
if (entry.expires > 0 && now > entry.expires) {
|
|
80
|
+
this.delete(key);
|
|
81
|
+
pruned++;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return pruned;
|
|
85
|
+
}
|
|
86
|
+
getStats() {
|
|
87
|
+
return {
|
|
88
|
+
size: this.cache.size,
|
|
89
|
+
maxEntries: this.maxEntries
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
exports.LRUCache = LRUCache;
|
|
95
|
+
exports.default = LRUCache;
|
|
96
|
+
module.exports = Object.assign(LRUCache, exports);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export class LRUCache {
|
|
2
|
+
cache = new Map;
|
|
3
|
+
maxEntries;
|
|
4
|
+
defaultTTL;
|
|
5
|
+
onEvict;
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.maxEntries = options.maxEntries ?? 500;
|
|
8
|
+
this.defaultTTL = options.ttl ?? 3000000;
|
|
9
|
+
this.onEvict = options.onEvict;
|
|
10
|
+
}
|
|
11
|
+
get(key) {
|
|
12
|
+
const entry = this.cache.get(key);
|
|
13
|
+
if (!entry) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (entry.expires > 0 && Date.now() > entry.expires) {
|
|
17
|
+
this.delete(key);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.cache.delete(key);
|
|
21
|
+
this.cache.set(key, entry);
|
|
22
|
+
return entry.value;
|
|
23
|
+
}
|
|
24
|
+
set(key, value, ttl) {
|
|
25
|
+
if (this.cache.has(key)) {
|
|
26
|
+
this.cache.delete(key);
|
|
27
|
+
}
|
|
28
|
+
while (this.cache.size >= this.maxEntries) {
|
|
29
|
+
const oldestKey = this.cache.keys().next().value;
|
|
30
|
+
if (oldestKey !== undefined) {
|
|
31
|
+
const oldEntry = this.cache.get(oldestKey);
|
|
32
|
+
this.cache.delete(oldestKey);
|
|
33
|
+
if (this.onEvict && oldEntry) {
|
|
34
|
+
this.onEvict(oldestKey, oldEntry.value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const actualTTL = ttl ?? this.defaultTTL;
|
|
39
|
+
const expires = actualTTL > 0 ? Date.now() + actualTTL : 0;
|
|
40
|
+
this.cache.set(key, { value, expires });
|
|
41
|
+
}
|
|
42
|
+
has(key) {
|
|
43
|
+
const entry = this.cache.get(key);
|
|
44
|
+
if (!entry) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (entry.expires > 0 && Date.now() > entry.expires) {
|
|
48
|
+
this.delete(key);
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
delete(key) {
|
|
54
|
+
const entry = this.cache.get(key);
|
|
55
|
+
const deleted = this.cache.delete(key);
|
|
56
|
+
if (deleted && this.onEvict && entry) {
|
|
57
|
+
this.onEvict(key, entry.value);
|
|
58
|
+
}
|
|
59
|
+
return deleted;
|
|
60
|
+
}
|
|
61
|
+
clear() {
|
|
62
|
+
if (this.onEvict) {
|
|
63
|
+
for (const [key, entry] of this.cache) {
|
|
64
|
+
this.onEvict(key, entry.value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.cache.clear();
|
|
68
|
+
}
|
|
69
|
+
get size() {
|
|
70
|
+
return this.cache.size;
|
|
71
|
+
}
|
|
72
|
+
keys() {
|
|
73
|
+
return Array.from(this.cache.keys());
|
|
74
|
+
}
|
|
75
|
+
prune() {
|
|
76
|
+
const now = Date.now();
|
|
77
|
+
let pruned = 0;
|
|
78
|
+
for (const [key, entry] of this.cache) {
|
|
79
|
+
if (entry.expires > 0 && now > entry.expires) {
|
|
80
|
+
this.delete(key);
|
|
81
|
+
pruned++;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return pruned;
|
|
85
|
+
}
|
|
86
|
+
getStats() {
|
|
87
|
+
return {
|
|
88
|
+
size: this.cache.size,
|
|
89
|
+
maxEntries: this.maxEntries
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export default LRUCache;
|