jiren 1.5.0 → 1.6.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/README.md +321 -483
- package/components/cache.ts +1 -1
- package/components/client-node-native.ts +497 -159
- package/components/client.ts +51 -29
- package/components/metrics.ts +1 -4
- package/components/native-node.ts +7 -3
- package/components/native.ts +29 -0
- package/components/persistent-worker.ts +73 -0
- package/components/subprocess-worker.ts +65 -0
- package/components/worker-pool.ts +169 -0
- package/components/worker.ts +39 -23
- package/dist/components/cache.d.ts +76 -0
- package/dist/components/cache.d.ts.map +1 -0
- package/dist/components/cache.js +439 -0
- package/dist/components/cache.js.map +1 -0
- package/dist/components/client-node-native.d.ts +114 -0
- package/dist/components/client-node-native.d.ts.map +1 -0
- package/dist/components/client-node-native.js +744 -0
- package/dist/components/client-node-native.js.map +1 -0
- package/dist/components/metrics.d.ts +104 -0
- package/dist/components/metrics.d.ts.map +1 -0
- package/dist/components/metrics.js +296 -0
- package/dist/components/metrics.js.map +1 -0
- package/dist/components/native-node.d.ts +60 -0
- package/dist/components/native-node.d.ts.map +1 -0
- package/dist/components/native-node.js +108 -0
- package/dist/components/native-node.js.map +1 -0
- package/dist/components/types.d.ts +250 -0
- package/dist/components/types.d.ts.map +1 -0
- package/dist/components/types.js +5 -0
- package/dist/components/types.js.map +1 -0
- package/dist/index-node.d.ts +10 -0
- package/dist/index-node.d.ts.map +1 -0
- package/dist/index-node.js +12 -0
- package/dist/index-node.js.map +1 -0
- package/dist/types/index.d.ts +63 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/index-node.ts +6 -6
- package/index.ts +4 -3
- package/lib/libhttpclient.dylib +0 -0
- package/package.json +13 -5
- package/types/index.ts +0 -68
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { JirenResponse } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Two-Tier Response Cache
|
|
4
|
+
*
|
|
5
|
+
* L1: In-Memory LRU Cache (~0.001ms access) - hot data
|
|
6
|
+
* L2: Disk Cache with gzip (~5ms access) - persistence
|
|
7
|
+
*
|
|
8
|
+
* Read path: L1 → L2 → Network
|
|
9
|
+
* Write path: L1 + L2 (write-through)
|
|
10
|
+
*/
|
|
11
|
+
export declare class ResponseCache {
|
|
12
|
+
private l1;
|
|
13
|
+
private cacheDir;
|
|
14
|
+
private maxDiskSize;
|
|
15
|
+
constructor(l1Capacity?: number, cacheDir?: string, maxDiskSize?: number);
|
|
16
|
+
/**
|
|
17
|
+
* Generate cache key from URL and options
|
|
18
|
+
*/
|
|
19
|
+
private generateKey;
|
|
20
|
+
/**
|
|
21
|
+
* Get cache file path (compressed .gz file)
|
|
22
|
+
*/
|
|
23
|
+
private getCacheFilePath;
|
|
24
|
+
/**
|
|
25
|
+
* Preload L2 disk cache entry into L1 memory cache.
|
|
26
|
+
* Call this during initialization to ensure first request hits L1.
|
|
27
|
+
* @param url - Base URL to preload
|
|
28
|
+
* @param path - Optional path
|
|
29
|
+
* @param options - Optional request options
|
|
30
|
+
* @returns true if entry was preloaded, false if not found/expired
|
|
31
|
+
*/
|
|
32
|
+
preloadL1(url: string, path?: string, options?: any): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Get cached response - checks L1 first, then L2
|
|
35
|
+
*/
|
|
36
|
+
get(url: string, path?: string, options?: any): JirenResponse | null;
|
|
37
|
+
/**
|
|
38
|
+
* Get from L2 disk cache
|
|
39
|
+
*/
|
|
40
|
+
private getFromDisk;
|
|
41
|
+
/**
|
|
42
|
+
* Store response in both L1 and L2 (write-through)
|
|
43
|
+
*/
|
|
44
|
+
set(url: string, response: JirenResponse, ttl: number, path?: string, options?: any): void;
|
|
45
|
+
/**
|
|
46
|
+
* Write to L2 disk cache
|
|
47
|
+
*/
|
|
48
|
+
private setToDisk;
|
|
49
|
+
/**
|
|
50
|
+
* Clear cache for a specific URL or all
|
|
51
|
+
*/
|
|
52
|
+
clear(url?: string): void;
|
|
53
|
+
/**
|
|
54
|
+
* Clear all L2 disk cache files
|
|
55
|
+
*/
|
|
56
|
+
private clearAllDisk;
|
|
57
|
+
/**
|
|
58
|
+
* Get combined cache statistics
|
|
59
|
+
*/
|
|
60
|
+
stats(): {
|
|
61
|
+
l1: {
|
|
62
|
+
size: number;
|
|
63
|
+
capacity: number;
|
|
64
|
+
hits: number;
|
|
65
|
+
misses: number;
|
|
66
|
+
hitRate: string;
|
|
67
|
+
};
|
|
68
|
+
l2: {
|
|
69
|
+
size: number;
|
|
70
|
+
maxSize: number;
|
|
71
|
+
cacheDir: string;
|
|
72
|
+
totalSizeKB: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../components/cache.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,aAAa,EAAqB,MAAM,YAAY,CAAC;AAqOnE;;;;;;;;GAQG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,EAAE,CAAgB;IAC1B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,WAAW,CAAS;gBAEhB,UAAU,SAAM,EAAE,QAAQ,SAAiB,EAAE,WAAW,SAAM;IAW1E;;OAEG;IACH,OAAO,CAAC,WAAW;IAUnB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAIxB;;;;;;;OAOG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,OAAO;IAkB7D;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,aAAa,GAAG,IAAI;IAoBpE;;OAEG;IACH,OAAO,CAAC,WAAW;IAkDnB;;OAEG;IACH,GAAG,CACD,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,aAAa,EACvB,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,GAAG,GACZ,IAAI;IAgBP;;OAEG;YACW,SAAS;IAuDvB;;OAEG;IACH,KAAK,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IAezB;;OAEG;IACH,OAAO,CAAC,YAAY;IAapB;;OAEG;IACH,KAAK;;;;;;;;;;;;;;;CA6BN"}
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, unlinkSync, readdirSync, statSync, } from "fs";
|
|
2
|
+
import { gzipSync, gunzipSync } from "zlib";
|
|
3
|
+
import { createHash } from "crypto";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
/**
|
|
6
|
+
* Reconstruct body methods from raw data
|
|
7
|
+
*/
|
|
8
|
+
function reconstructBody(bodyData, isText) {
|
|
9
|
+
let buffer;
|
|
10
|
+
if (isText) {
|
|
11
|
+
// Text data stored directly
|
|
12
|
+
const encoder = new TextEncoder();
|
|
13
|
+
buffer = encoder.encode(bodyData).buffer;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
// Binary data stored as base64
|
|
17
|
+
const binaryString = atob(bodyData);
|
|
18
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
19
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
20
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
21
|
+
}
|
|
22
|
+
buffer = bytes.buffer;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
bodyUsed: false,
|
|
26
|
+
text: async () => {
|
|
27
|
+
if (isText)
|
|
28
|
+
return bodyData;
|
|
29
|
+
const decoder = new TextDecoder();
|
|
30
|
+
return decoder.decode(buffer);
|
|
31
|
+
},
|
|
32
|
+
json: async () => {
|
|
33
|
+
const text = isText ? bodyData : new TextDecoder().decode(buffer);
|
|
34
|
+
return JSON.parse(text);
|
|
35
|
+
},
|
|
36
|
+
arrayBuffer: async () => buffer,
|
|
37
|
+
blob: async () => new Blob([buffer]),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* L1 In-Memory LRU Cache
|
|
42
|
+
* Provides ~0.001ms access time (vs ~5ms for disk)
|
|
43
|
+
*/
|
|
44
|
+
class L1MemoryCache {
|
|
45
|
+
capacity;
|
|
46
|
+
cache = new Map();
|
|
47
|
+
head = null; // Most recently used
|
|
48
|
+
tail = null; // Least recently used
|
|
49
|
+
// Stats
|
|
50
|
+
hits = 0;
|
|
51
|
+
misses = 0;
|
|
52
|
+
constructor(capacity = 100) {
|
|
53
|
+
this.capacity = capacity;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get from L1 cache (O(1) lookup + LRU update)
|
|
57
|
+
*/
|
|
58
|
+
get(key) {
|
|
59
|
+
const node = this.cache.get(key);
|
|
60
|
+
if (!node) {
|
|
61
|
+
this.misses++;
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
// Check if expired
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
if (now - node.entry.timestamp > node.entry.ttl) {
|
|
67
|
+
this.delete(key);
|
|
68
|
+
this.misses++;
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
// Move to front (most recently used)
|
|
72
|
+
this.moveToFront(node);
|
|
73
|
+
this.hits++;
|
|
74
|
+
return node.entry;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Set in L1 cache with LRU eviction
|
|
78
|
+
*/
|
|
79
|
+
set(key, entry) {
|
|
80
|
+
// If key exists, update and move to front
|
|
81
|
+
const existing = this.cache.get(key);
|
|
82
|
+
if (existing) {
|
|
83
|
+
existing.entry = entry;
|
|
84
|
+
this.moveToFront(existing);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// Create new node
|
|
88
|
+
const node = {
|
|
89
|
+
key,
|
|
90
|
+
entry,
|
|
91
|
+
prev: null,
|
|
92
|
+
next: this.head,
|
|
93
|
+
};
|
|
94
|
+
// Add to front
|
|
95
|
+
if (this.head) {
|
|
96
|
+
this.head.prev = node;
|
|
97
|
+
}
|
|
98
|
+
this.head = node;
|
|
99
|
+
if (!this.tail) {
|
|
100
|
+
this.tail = node;
|
|
101
|
+
}
|
|
102
|
+
this.cache.set(key, node);
|
|
103
|
+
// Evict LRU if over capacity
|
|
104
|
+
if (this.cache.size > this.capacity) {
|
|
105
|
+
this.evictLRU();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Delete from L1 cache
|
|
110
|
+
*/
|
|
111
|
+
delete(key) {
|
|
112
|
+
const node = this.cache.get(key);
|
|
113
|
+
if (!node)
|
|
114
|
+
return;
|
|
115
|
+
this.removeNode(node);
|
|
116
|
+
this.cache.delete(key);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Clear all L1 cache
|
|
120
|
+
*/
|
|
121
|
+
clear() {
|
|
122
|
+
this.cache.clear();
|
|
123
|
+
this.head = null;
|
|
124
|
+
this.tail = null;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get L1 cache stats
|
|
128
|
+
*/
|
|
129
|
+
stats() {
|
|
130
|
+
return {
|
|
131
|
+
size: this.cache.size,
|
|
132
|
+
capacity: this.capacity,
|
|
133
|
+
hits: this.hits,
|
|
134
|
+
misses: this.misses,
|
|
135
|
+
hitRate: this.hits + this.misses > 0
|
|
136
|
+
? ((this.hits / (this.hits + this.misses)) * 100).toFixed(2) + "%"
|
|
137
|
+
: "0%",
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
moveToFront(node) {
|
|
141
|
+
if (node === this.head)
|
|
142
|
+
return; // Already at front
|
|
143
|
+
this.removeNode(node);
|
|
144
|
+
// Add to front
|
|
145
|
+
node.prev = null;
|
|
146
|
+
node.next = this.head;
|
|
147
|
+
if (this.head) {
|
|
148
|
+
this.head.prev = node;
|
|
149
|
+
}
|
|
150
|
+
this.head = node;
|
|
151
|
+
if (!this.tail) {
|
|
152
|
+
this.tail = node;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
removeNode(node) {
|
|
156
|
+
if (node.prev) {
|
|
157
|
+
node.prev.next = node.next;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
this.head = node.next;
|
|
161
|
+
}
|
|
162
|
+
if (node.next) {
|
|
163
|
+
node.next.prev = node.prev;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
this.tail = node.prev;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
evictLRU() {
|
|
170
|
+
if (!this.tail)
|
|
171
|
+
return;
|
|
172
|
+
const key = this.tail.key;
|
|
173
|
+
this.removeNode(this.tail);
|
|
174
|
+
this.cache.delete(key);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Two-Tier Response Cache
|
|
179
|
+
*
|
|
180
|
+
* L1: In-Memory LRU Cache (~0.001ms access) - hot data
|
|
181
|
+
* L2: Disk Cache with gzip (~5ms access) - persistence
|
|
182
|
+
*
|
|
183
|
+
* Read path: L1 → L2 → Network
|
|
184
|
+
* Write path: L1 + L2 (write-through)
|
|
185
|
+
*/
|
|
186
|
+
export class ResponseCache {
|
|
187
|
+
l1;
|
|
188
|
+
cacheDir;
|
|
189
|
+
maxDiskSize;
|
|
190
|
+
constructor(l1Capacity = 100, cacheDir = ".cache/jiren", maxDiskSize = 100) {
|
|
191
|
+
this.l1 = new L1MemoryCache(l1Capacity);
|
|
192
|
+
this.maxDiskSize = maxDiskSize;
|
|
193
|
+
this.cacheDir = cacheDir;
|
|
194
|
+
// Create cache directory if it doesn't exist
|
|
195
|
+
if (!existsSync(this.cacheDir)) {
|
|
196
|
+
mkdirSync(this.cacheDir, { recursive: true });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Generate cache key from URL and options
|
|
201
|
+
*/
|
|
202
|
+
generateKey(url, path, options) {
|
|
203
|
+
const fullUrl = path ? `${url}${path}` : url;
|
|
204
|
+
const method = options?.method || "GET";
|
|
205
|
+
const headers = JSON.stringify(options?.headers || {});
|
|
206
|
+
const key = `${method}:${fullUrl}:${headers}`;
|
|
207
|
+
// Hash the key to create a valid filename
|
|
208
|
+
return createHash("md5").update(key).digest("hex");
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Get cache file path (compressed .gz file)
|
|
212
|
+
*/
|
|
213
|
+
getCacheFilePath(key) {
|
|
214
|
+
return join(this.cacheDir, `${key}.json.gz`);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Preload L2 disk cache entry into L1 memory cache.
|
|
218
|
+
* Call this during initialization to ensure first request hits L1.
|
|
219
|
+
* @param url - Base URL to preload
|
|
220
|
+
* @param path - Optional path
|
|
221
|
+
* @param options - Optional request options
|
|
222
|
+
* @returns true if entry was preloaded, false if not found/expired
|
|
223
|
+
*/
|
|
224
|
+
preloadL1(url, path, options) {
|
|
225
|
+
const key = this.generateKey(url, path, options);
|
|
226
|
+
// Check if already in L1
|
|
227
|
+
if (this.l1.get(key)) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
// Try to load from L2 disk
|
|
231
|
+
const l2Entry = this.getFromDisk(key);
|
|
232
|
+
if (l2Entry) {
|
|
233
|
+
this.l1.set(key, l2Entry);
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Get cached response - checks L1 first, then L2
|
|
240
|
+
*/
|
|
241
|
+
get(url, path, options) {
|
|
242
|
+
const key = this.generateKey(url, path, options);
|
|
243
|
+
// L1: Check in-memory cache first (~0.001ms)
|
|
244
|
+
const l1Entry = this.l1.get(key);
|
|
245
|
+
if (l1Entry) {
|
|
246
|
+
return l1Entry.response;
|
|
247
|
+
}
|
|
248
|
+
// L2: Check disk cache (~5ms)
|
|
249
|
+
const l2Entry = this.getFromDisk(key);
|
|
250
|
+
if (l2Entry) {
|
|
251
|
+
// Promote to L1 for faster future access
|
|
252
|
+
this.l1.set(key, l2Entry);
|
|
253
|
+
return l2Entry.response;
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Get from L2 disk cache
|
|
259
|
+
*/
|
|
260
|
+
getFromDisk(key) {
|
|
261
|
+
const filePath = this.getCacheFilePath(key);
|
|
262
|
+
if (!existsSync(filePath))
|
|
263
|
+
return null;
|
|
264
|
+
try {
|
|
265
|
+
// Read compressed file
|
|
266
|
+
const compressed = readFileSync(filePath);
|
|
267
|
+
// Decompress
|
|
268
|
+
const decompressed = gunzipSync(compressed);
|
|
269
|
+
const data = decompressed.toString("utf-8");
|
|
270
|
+
const serialized = JSON.parse(data);
|
|
271
|
+
// Check if expired
|
|
272
|
+
const now = Date.now();
|
|
273
|
+
if (now - serialized.timestamp > serialized.ttl) {
|
|
274
|
+
// Delete expired cache file
|
|
275
|
+
try {
|
|
276
|
+
unlinkSync(filePath);
|
|
277
|
+
}
|
|
278
|
+
catch { }
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
// Reconstruct the response with working body methods
|
|
282
|
+
const response = {
|
|
283
|
+
status: serialized.status,
|
|
284
|
+
statusText: serialized.statusText,
|
|
285
|
+
headers: serialized.headers,
|
|
286
|
+
url: serialized.url,
|
|
287
|
+
ok: serialized.ok,
|
|
288
|
+
redirected: serialized.redirected,
|
|
289
|
+
type: serialized.type || "default",
|
|
290
|
+
body: reconstructBody(serialized.bodyData, serialized.bodyIsText),
|
|
291
|
+
};
|
|
292
|
+
return {
|
|
293
|
+
response,
|
|
294
|
+
timestamp: serialized.timestamp,
|
|
295
|
+
ttl: serialized.ttl,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
// Invalid cache file, delete it
|
|
300
|
+
try {
|
|
301
|
+
unlinkSync(filePath);
|
|
302
|
+
}
|
|
303
|
+
catch { }
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Store response in both L1 and L2 (write-through)
|
|
309
|
+
*/
|
|
310
|
+
set(url, response, ttl, path, options) {
|
|
311
|
+
const key = this.generateKey(url, path, options);
|
|
312
|
+
const entry = {
|
|
313
|
+
response,
|
|
314
|
+
timestamp: Date.now(),
|
|
315
|
+
ttl,
|
|
316
|
+
};
|
|
317
|
+
// L1: Store in memory (~0.001ms)
|
|
318
|
+
this.l1.set(key, entry);
|
|
319
|
+
// L2: Store on disk (async-ish, for persistence)
|
|
320
|
+
this.setToDisk(key, entry);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Write to L2 disk cache
|
|
324
|
+
*/
|
|
325
|
+
async setToDisk(key, entry) {
|
|
326
|
+
const filePath = this.getCacheFilePath(key);
|
|
327
|
+
try {
|
|
328
|
+
// Extract body text for serialization
|
|
329
|
+
let bodyData;
|
|
330
|
+
let bodyIsText = true;
|
|
331
|
+
try {
|
|
332
|
+
bodyData = await entry.response.body.text();
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
// If text fails, try arrayBuffer and convert to base64
|
|
336
|
+
try {
|
|
337
|
+
const buffer = await entry.response.body.arrayBuffer();
|
|
338
|
+
const bytes = new Uint8Array(buffer);
|
|
339
|
+
let binary = "";
|
|
340
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
341
|
+
binary += String.fromCharCode(bytes[i]);
|
|
342
|
+
}
|
|
343
|
+
bodyData = btoa(binary);
|
|
344
|
+
bodyIsText = false;
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
bodyData = "";
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
// Create serializable entry
|
|
351
|
+
const serialized = {
|
|
352
|
+
status: entry.response.status,
|
|
353
|
+
statusText: entry.response.statusText,
|
|
354
|
+
headers: entry.response.headers,
|
|
355
|
+
url: entry.response.url,
|
|
356
|
+
ok: entry.response.ok,
|
|
357
|
+
redirected: entry.response.redirected,
|
|
358
|
+
type: entry.response.type || "default",
|
|
359
|
+
bodyData,
|
|
360
|
+
bodyIsText,
|
|
361
|
+
timestamp: entry.timestamp,
|
|
362
|
+
ttl: entry.ttl,
|
|
363
|
+
};
|
|
364
|
+
// Convert to JSON
|
|
365
|
+
const json = JSON.stringify(serialized);
|
|
366
|
+
// Compress with gzip
|
|
367
|
+
const compressed = gzipSync(json);
|
|
368
|
+
// Write compressed file
|
|
369
|
+
writeFileSync(filePath, compressed);
|
|
370
|
+
}
|
|
371
|
+
catch (error) {
|
|
372
|
+
// Silently fail if can't write cache
|
|
373
|
+
console.warn("Failed to write cache:", error);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Clear cache for a specific URL or all
|
|
378
|
+
*/
|
|
379
|
+
clear(url) {
|
|
380
|
+
// Clear L1
|
|
381
|
+
this.l1.clear();
|
|
382
|
+
// Clear L2
|
|
383
|
+
if (url) {
|
|
384
|
+
// Clear all cache files for this URL
|
|
385
|
+
// This is approximate since we hash the keys
|
|
386
|
+
// For now, just clear all to be safe
|
|
387
|
+
this.clearAllDisk();
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
this.clearAllDisk();
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Clear all L2 disk cache files
|
|
395
|
+
*/
|
|
396
|
+
clearAllDisk() {
|
|
397
|
+
try {
|
|
398
|
+
const files = readdirSync(this.cacheDir);
|
|
399
|
+
for (const file of files) {
|
|
400
|
+
if (file.endsWith(".json.gz")) {
|
|
401
|
+
unlinkSync(join(this.cacheDir, file));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
catch (error) {
|
|
406
|
+
// Silently fail
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Get combined cache statistics
|
|
411
|
+
*/
|
|
412
|
+
stats() {
|
|
413
|
+
const l1Stats = this.l1.stats();
|
|
414
|
+
// L2 disk stats
|
|
415
|
+
let diskSize = 0;
|
|
416
|
+
let diskFiles = 0;
|
|
417
|
+
let totalDiskBytes = 0;
|
|
418
|
+
try {
|
|
419
|
+
const files = readdirSync(this.cacheDir);
|
|
420
|
+
const cacheFiles = files.filter((f) => f.endsWith(".json.gz"));
|
|
421
|
+
diskFiles = cacheFiles.length;
|
|
422
|
+
for (const file of cacheFiles) {
|
|
423
|
+
const stats = statSync(join(this.cacheDir, file));
|
|
424
|
+
totalDiskBytes += stats.size;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
catch { }
|
|
428
|
+
return {
|
|
429
|
+
l1: l1Stats,
|
|
430
|
+
l2: {
|
|
431
|
+
size: diskFiles,
|
|
432
|
+
maxSize: this.maxDiskSize,
|
|
433
|
+
cacheDir: this.cacheDir,
|
|
434
|
+
totalSizeKB: (totalDiskBytes / 1024).toFixed(2),
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../components/cache.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,SAAS,EACT,YAAY,EACZ,aAAa,EACb,UAAU,EACV,WAAW,EACX,QAAQ,GACT,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AA6B5B;;GAEG;AACH,SAAS,eAAe,CAAC,QAAgB,EAAE,MAAe;IACxD,IAAI,MAAmB,CAAC;IAExB,IAAI,MAAM,EA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EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC;YACvE,SAAS,GAAG,UAAU,CAAC,MAAM,CAAC;YAE9B,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;gBAC9B,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC;gBAClD,cAAc,IAAI,KAAK,CAAC,IAAI,CAAC;YAC/B,CAAC;QACH,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QAEV,OAAO;YACL,EAAE,EAAE,OAAO;YACX,EAAE,EAAE;gBACF,IAAI,EAAE,SAAS;gBACf,OAAO,EAAE,IAAI,CAAC,WAAW;gBACzB,QAAQ,EAAE,IAAI,CAAC,QAAQ;gBACvB,WAAW,EAAE,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;aAChD;SACF,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { RequestOptions, JirenResponse, TargetUrlConfig, UrlEndpoint, CacheConfig, RetryConfig, Interceptors, MetricsAPI } from "./types.js";
|
|
2
|
+
/** URL configuration with optional cache and antibot */
|
|
3
|
+
export type UrlConfig = string | {
|
|
4
|
+
url: string;
|
|
5
|
+
cache?: boolean | CacheConfig;
|
|
6
|
+
antibot?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/** Options for JirenClient constructor */
|
|
9
|
+
export interface JirenClientOptions<T extends readonly TargetUrlConfig[] | Record<string, UrlConfig> = readonly TargetUrlConfig[] | Record<string, UrlConfig>> {
|
|
10
|
+
/** Target URLs to pre-connect on client creation */
|
|
11
|
+
targets?: string[] | T;
|
|
12
|
+
/** Enable benchmark mode (Force HTTP/2, disable probing) */
|
|
13
|
+
benchmark?: boolean;
|
|
14
|
+
/** Global retry configuration */
|
|
15
|
+
retry?: number | RetryConfig;
|
|
16
|
+
/** Request/response interceptors */
|
|
17
|
+
interceptors?: Interceptors;
|
|
18
|
+
/** Performance mode: disable metrics, skip cache checks for non-cached endpoints (default: true) */
|
|
19
|
+
performanceMode?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export type ExtractTargetKeys<T extends readonly TargetUrlConfig[] | Record<string, UrlConfig>> = T extends readonly TargetUrlConfig[] ? T[number]["key"] : T extends Record<string, UrlConfig> ? keyof T : never;
|
|
22
|
+
export type UrlAccessor<T extends readonly TargetUrlConfig[] | Record<string, UrlConfig>> = {
|
|
23
|
+
[K in ExtractTargetKeys<T>]: UrlEndpoint;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Helper to define target URLs with type inference.
|
|
27
|
+
*/
|
|
28
|
+
export declare function defineUrls<const T extends readonly TargetUrlConfig[]>(urls: T): T;
|
|
29
|
+
export declare class JirenClient<T extends readonly TargetUrlConfig[] | Record<string, UrlConfig> = readonly TargetUrlConfig[] | Record<string, UrlConfig>> implements Disposable {
|
|
30
|
+
private ptr;
|
|
31
|
+
private urlMap;
|
|
32
|
+
private cacheConfig;
|
|
33
|
+
private antibotConfig;
|
|
34
|
+
private cache;
|
|
35
|
+
private inflightRequests;
|
|
36
|
+
private globalRetry?;
|
|
37
|
+
private requestInterceptors;
|
|
38
|
+
private responseInterceptors;
|
|
39
|
+
private errorInterceptors;
|
|
40
|
+
private targetsPromise;
|
|
41
|
+
private targetsComplete;
|
|
42
|
+
private performanceMode;
|
|
43
|
+
private readonly defaultHeadersStr;
|
|
44
|
+
private readonly defaultHeaders;
|
|
45
|
+
/** Type-safe URL accessor for warmed-up URLs */
|
|
46
|
+
readonly url: UrlAccessor<T>;
|
|
47
|
+
private metricsCollector;
|
|
48
|
+
/** Public metrics API */
|
|
49
|
+
readonly metrics: MetricsAPI;
|
|
50
|
+
constructor(options?: JirenClientOptions<T>);
|
|
51
|
+
private waitFor;
|
|
52
|
+
/**
|
|
53
|
+
* Wait for lazy pre-connection to complete.
|
|
54
|
+
*/
|
|
55
|
+
waitForTargets(): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* @deprecated Use waitForTargets() instead
|
|
58
|
+
*/
|
|
59
|
+
waitForWarmup(): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a proxy-based URL accessor for type-safe access.
|
|
62
|
+
*/
|
|
63
|
+
private createUrlAccessor;
|
|
64
|
+
/**
|
|
65
|
+
* Free the native client resources.
|
|
66
|
+
* Note: This is called automatically when the client is garbage collected,
|
|
67
|
+
* or you can use the `using` keyword for automatic cleanup in a scope.
|
|
68
|
+
*/
|
|
69
|
+
close(): void;
|
|
70
|
+
/**
|
|
71
|
+
* Dispose method for the `using` keyword (ECMAScript Explicit Resource Management)
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* using client = new JirenClient({ targets: [...] });
|
|
75
|
+
* // client is automatically closed when the scope ends
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
[Symbol.dispose](): void;
|
|
79
|
+
/**
|
|
80
|
+
* Register interceptors dynamically.
|
|
81
|
+
*/
|
|
82
|
+
use(interceptors: Interceptors): this;
|
|
83
|
+
/**
|
|
84
|
+
* Pre-connect to URLs in parallel.
|
|
85
|
+
*/
|
|
86
|
+
preconnect(urls: string[]): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* @deprecated Use preconnect() instead
|
|
89
|
+
*/
|
|
90
|
+
warmup(urls: string[]): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* @deprecated Use preconnect() instead
|
|
93
|
+
*/
|
|
94
|
+
prefetch(urls: string[]): void;
|
|
95
|
+
request<T = any>(method: string, url: string, body?: string | null, options?: RequestOptions & {
|
|
96
|
+
responseType: "json";
|
|
97
|
+
}): Promise<T>;
|
|
98
|
+
request<T = any>(method: string, url: string, body?: string | null, options?: RequestOptions & {
|
|
99
|
+
responseType: "text";
|
|
100
|
+
}): Promise<string>;
|
|
101
|
+
request<T = any>(method: string, url: string, body?: string | null, options?: RequestOptions & {
|
|
102
|
+
responseType: "arraybuffer";
|
|
103
|
+
}): Promise<ArrayBuffer>;
|
|
104
|
+
request<T = any>(method: string, url: string, body?: string | null, options?: RequestOptions & {
|
|
105
|
+
responseType: "blob";
|
|
106
|
+
}): Promise<Blob>;
|
|
107
|
+
request<T = any>(method: string, url: string, body?: string | null, options?: RequestOptions): Promise<JirenResponse<T>>;
|
|
108
|
+
private parseResponse;
|
|
109
|
+
/**
|
|
110
|
+
* Helper to prepare body and headers for requests.
|
|
111
|
+
*/
|
|
112
|
+
private prepareBody;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=client-node-native.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-node-native.d.ts","sourceRoot":"","sources":["../../components/client-node-native.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EAEb,eAAe,EAEf,WAAW,EACX,WAAW,EACX,WAAW,EACX,YAAY,EAMZ,UAAU,EACX,MAAM,YAAY,CAAC;AAiBpB,wDAAwD;AACxD,MAAM,MAAM,SAAS,GACjB,MAAM,GACN;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,GAAG,WAAW,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAEtE,0CAA0C;AAC1C,MAAM,WAAW,kBAAkB,CACjC,CAAC,SAAS,SAAS,eAAe,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAC5D,SAAS,eAAe,EAAE,GAC1B,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC;IAE7B,oDAAoD;IACpD,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;IAEvB,4DAA4D;IAC5D,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,iCAAiC;IACjC,KAAK,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC;IAE7B,oCAAoC;IACpC,YAAY,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