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,118 @@
|
|
|
1
|
+
const { LRUCache } = require('./lru-cache.cjs');
|
|
2
|
+
const dns = require("dns");
|
|
3
|
+
const DEFAULT_DNS_TTL = 60000;
|
|
4
|
+
const DEFAULT_DNS_MAX_ENTRIES = 1000;
|
|
5
|
+
|
|
6
|
+
class DNSCache {
|
|
7
|
+
cache;
|
|
8
|
+
enabled;
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
this.enabled = options.enable !== false;
|
|
11
|
+
this.cache = new LRUCache({
|
|
12
|
+
maxEntries: options.maxEntries ?? DEFAULT_DNS_MAX_ENTRIES,
|
|
13
|
+
ttl: options.ttl ?? DEFAULT_DNS_TTL
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
makeKey(hostname, family) {
|
|
17
|
+
return family ? `${hostname}:${family}` : hostname;
|
|
18
|
+
}
|
|
19
|
+
async lookup(hostname, family) {
|
|
20
|
+
if (!this.enabled) {
|
|
21
|
+
return this.resolveDNS(hostname, family);
|
|
22
|
+
}
|
|
23
|
+
const key = this.makeKey(hostname, family);
|
|
24
|
+
const cached = this.cache.get(key);
|
|
25
|
+
if (cached && cached.addresses.length > 0) {
|
|
26
|
+
const address = cached.addresses[Math.floor(Math.random() * cached.addresses.length)];
|
|
27
|
+
return { address, family: cached.family };
|
|
28
|
+
}
|
|
29
|
+
const result = await this.resolveDNS(hostname, family);
|
|
30
|
+
if (result) {
|
|
31
|
+
this.cache.set(key, {
|
|
32
|
+
addresses: [result.address],
|
|
33
|
+
family: result.family,
|
|
34
|
+
timestamp: Date.now()
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
async lookupAll(hostname, family) {
|
|
40
|
+
if (!this.enabled) {
|
|
41
|
+
return this.resolveAllDNS(hostname, family);
|
|
42
|
+
}
|
|
43
|
+
const key = this.makeKey(hostname, family);
|
|
44
|
+
const cached = this.cache.get(key);
|
|
45
|
+
if (cached && cached.addresses.length > 0) {
|
|
46
|
+
return cached.addresses.map((address) => ({ address, family: cached.family }));
|
|
47
|
+
}
|
|
48
|
+
const results = await this.resolveAllDNS(hostname, family);
|
|
49
|
+
if (results.length > 0) {
|
|
50
|
+
this.cache.set(key, {
|
|
51
|
+
addresses: results.map((r) => r.address),
|
|
52
|
+
family: results[0].family,
|
|
53
|
+
timestamp: Date.now()
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return results;
|
|
57
|
+
}
|
|
58
|
+
resolveDNS(hostname, family) {
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
const options = { family: family ?? 0 };
|
|
61
|
+
dns.lookup(hostname, options, (err, address, resultFamily) => {
|
|
62
|
+
if (err || !address || typeof address !== "string") {
|
|
63
|
+
resolve(undefined);
|
|
64
|
+
} else {
|
|
65
|
+
resolve({ address, family: resultFamily });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
resolveAllDNS(hostname, family) {
|
|
71
|
+
return new Promise((resolve) => {
|
|
72
|
+
const options = { family: family ?? 0, all: true };
|
|
73
|
+
dns.lookup(hostname, options, (err, addresses) => {
|
|
74
|
+
if (err || !addresses || !Array.isArray(addresses)) {
|
|
75
|
+
resolve([]);
|
|
76
|
+
} else {
|
|
77
|
+
resolve(addresses.map((a) => ({ address: a.address, family: a.family })));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
invalidate(hostname) {
|
|
83
|
+
this.cache.delete(this.makeKey(hostname));
|
|
84
|
+
this.cache.delete(this.makeKey(hostname, 4));
|
|
85
|
+
this.cache.delete(this.makeKey(hostname, 6));
|
|
86
|
+
}
|
|
87
|
+
clear() {
|
|
88
|
+
this.cache.clear();
|
|
89
|
+
}
|
|
90
|
+
get size() {
|
|
91
|
+
return this.cache.size;
|
|
92
|
+
}
|
|
93
|
+
get isEnabled() {
|
|
94
|
+
return this.enabled;
|
|
95
|
+
}
|
|
96
|
+
setEnabled(enabled) {
|
|
97
|
+
this.enabled = enabled;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
let globalDNSCache = null;
|
|
101
|
+
function getGlobalDNSCache(options) {
|
|
102
|
+
if (!globalDNSCache) {
|
|
103
|
+
globalDNSCache = new DNSCache(options);
|
|
104
|
+
}
|
|
105
|
+
return globalDNSCache;
|
|
106
|
+
}
|
|
107
|
+
function resetGlobalDNSCache() {
|
|
108
|
+
if (globalDNSCache) {
|
|
109
|
+
globalDNSCache.clear();
|
|
110
|
+
}
|
|
111
|
+
globalDNSCache = null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
exports.DNSCache = DNSCache;
|
|
115
|
+
exports.getGlobalDNSCache = getGlobalDNSCache;
|
|
116
|
+
exports.resetGlobalDNSCache = resetGlobalDNSCache;
|
|
117
|
+
exports.default = DNSCache;
|
|
118
|
+
module.exports = Object.assign(DNSCache, exports);
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { LRUCache } from './lru-cache.js';
|
|
2
|
+
import dns from "dns";
|
|
3
|
+
const DEFAULT_DNS_TTL = 60000;
|
|
4
|
+
const DEFAULT_DNS_MAX_ENTRIES = 1000;
|
|
5
|
+
|
|
6
|
+
export class DNSCache {
|
|
7
|
+
cache;
|
|
8
|
+
enabled;
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
this.enabled = options.enable !== false;
|
|
11
|
+
this.cache = new LRUCache({
|
|
12
|
+
maxEntries: options.maxEntries ?? DEFAULT_DNS_MAX_ENTRIES,
|
|
13
|
+
ttl: options.ttl ?? DEFAULT_DNS_TTL
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
makeKey(hostname, family) {
|
|
17
|
+
return family ? `${hostname}:${family}` : hostname;
|
|
18
|
+
}
|
|
19
|
+
async lookup(hostname, family) {
|
|
20
|
+
if (!this.enabled) {
|
|
21
|
+
return this.resolveDNS(hostname, family);
|
|
22
|
+
}
|
|
23
|
+
const key = this.makeKey(hostname, family);
|
|
24
|
+
const cached = this.cache.get(key);
|
|
25
|
+
if (cached && cached.addresses.length > 0) {
|
|
26
|
+
const address = cached.addresses[Math.floor(Math.random() * cached.addresses.length)];
|
|
27
|
+
return { address, family: cached.family };
|
|
28
|
+
}
|
|
29
|
+
const result = await this.resolveDNS(hostname, family);
|
|
30
|
+
if (result) {
|
|
31
|
+
this.cache.set(key, {
|
|
32
|
+
addresses: [result.address],
|
|
33
|
+
family: result.family,
|
|
34
|
+
timestamp: Date.now()
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
async lookupAll(hostname, family) {
|
|
40
|
+
if (!this.enabled) {
|
|
41
|
+
return this.resolveAllDNS(hostname, family);
|
|
42
|
+
}
|
|
43
|
+
const key = this.makeKey(hostname, family);
|
|
44
|
+
const cached = this.cache.get(key);
|
|
45
|
+
if (cached && cached.addresses.length > 0) {
|
|
46
|
+
return cached.addresses.map((address) => ({ address, family: cached.family }));
|
|
47
|
+
}
|
|
48
|
+
const results = await this.resolveAllDNS(hostname, family);
|
|
49
|
+
if (results.length > 0) {
|
|
50
|
+
this.cache.set(key, {
|
|
51
|
+
addresses: results.map((r) => r.address),
|
|
52
|
+
family: results[0].family,
|
|
53
|
+
timestamp: Date.now()
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return results;
|
|
57
|
+
}
|
|
58
|
+
resolveDNS(hostname, family) {
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
const options = { family: family ?? 0 };
|
|
61
|
+
dns.lookup(hostname, options, (err, address, resultFamily) => {
|
|
62
|
+
if (err || !address || typeof address !== "string") {
|
|
63
|
+
resolve(undefined);
|
|
64
|
+
} else {
|
|
65
|
+
resolve({ address, family: resultFamily });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
resolveAllDNS(hostname, family) {
|
|
71
|
+
return new Promise((resolve) => {
|
|
72
|
+
const options = { family: family ?? 0, all: true };
|
|
73
|
+
dns.lookup(hostname, options, (err, addresses) => {
|
|
74
|
+
if (err || !addresses || !Array.isArray(addresses)) {
|
|
75
|
+
resolve([]);
|
|
76
|
+
} else {
|
|
77
|
+
resolve(addresses.map((a) => ({ address: a.address, family: a.family })));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
invalidate(hostname) {
|
|
83
|
+
this.cache.delete(this.makeKey(hostname));
|
|
84
|
+
this.cache.delete(this.makeKey(hostname, 4));
|
|
85
|
+
this.cache.delete(this.makeKey(hostname, 6));
|
|
86
|
+
}
|
|
87
|
+
clear() {
|
|
88
|
+
this.cache.clear();
|
|
89
|
+
}
|
|
90
|
+
get size() {
|
|
91
|
+
return this.cache.size;
|
|
92
|
+
}
|
|
93
|
+
get isEnabled() {
|
|
94
|
+
return this.enabled;
|
|
95
|
+
}
|
|
96
|
+
setEnabled(enabled) {
|
|
97
|
+
this.enabled = enabled;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
let globalDNSCache = null;
|
|
101
|
+
export function getGlobalDNSCache(options) {
|
|
102
|
+
if (!globalDNSCache) {
|
|
103
|
+
globalDNSCache = new DNSCache(options);
|
|
104
|
+
}
|
|
105
|
+
return globalDNSCache;
|
|
106
|
+
}
|
|
107
|
+
export function resetGlobalDNSCache() {
|
|
108
|
+
if (globalDNSCache) {
|
|
109
|
+
globalDNSCache.clear();
|
|
110
|
+
}
|
|
111
|
+
globalDNSCache = null;
|
|
112
|
+
}
|
|
113
|
+
export default DNSCache;
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const { createHash } = require("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
|
+
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
|
+
|
|
262
|
+
exports.FileCacher = FileCacher;
|
|
263
|
+
exports.default = FileCacher;
|
|
264
|
+
module.exports = Object.assign(FileCacher, exports);
|