pi-readseek 0.4.12 → 0.4.14

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/src/map-cache.ts DELETED
@@ -1,82 +0,0 @@
1
- import { stat } from "node:fs/promises";
2
- import type { FileMap } from "./readseek/types.js";
3
- import { readseekMap } from "./readseek-client.js";
4
-
5
- interface CacheEntry {
6
- mtimeMs: number;
7
- map: FileMap | null;
8
- }
9
-
10
- export const MAP_CACHE_MAX_SIZE = 500;
11
-
12
- interface MapCacheGlobalState {
13
- cache: Map<string, CacheEntry>;
14
- inflight: Map<string, Promise<FileMap | null>>;
15
- maxSize: number;
16
- }
17
-
18
- const MAP_CACHE_STATE_KEY = Symbol.for("pi-readseek.mapCacheState.v1");
19
-
20
- type MapCacheGlobalSlots = typeof globalThis & Record<symbol, MapCacheGlobalState | undefined>;
21
-
22
- function getMapCacheState(): MapCacheGlobalState {
23
- const globalObject = globalThis as MapCacheGlobalSlots;
24
- const state = globalObject[MAP_CACHE_STATE_KEY] ?? {
25
- cache: new Map<string, CacheEntry>(),
26
- inflight: new Map<string, Promise<FileMap | null>>(),
27
- maxSize: MAP_CACHE_MAX_SIZE,
28
- };
29
- globalObject[MAP_CACHE_STATE_KEY] = state;
30
- return state;
31
- }
32
- // Move an existing entry to the most-recently-used position (Map insertion-order tail).
33
- function touchMapEntry<K, V>(map: Map<K, V>, key: K, value: V): void {
34
- map.delete(key);
35
- map.set(key, value);
36
- }
37
-
38
- function rememberInMemory(absPath: string, entry: CacheEntry): void {
39
- const state = getMapCacheState();
40
- touchMapEntry(state.cache, absPath, entry);
41
- if (state.cache.size > state.maxSize) {
42
- const oldestKey = state.cache.keys().next().value;
43
- if (oldestKey !== undefined) state.cache.delete(oldestKey);
44
- }
45
- }
46
-
47
- /**
48
- * Get or generate a structural file map, with mtime-based in-memory caching.
49
- * Readseek already caches map results in `.readseek/maps/` with its own binary
50
- * format. The in-memory layer here avoids redundant process spawns within a
51
- * single session.
52
- *
53
- * Returns null on any failure — never throws.
54
- */
55
- export async function getOrGenerateMap(absPath: string): Promise<FileMap | null> {
56
- try {
57
- const fileStat = await stat(absPath);
58
- const { mtimeMs } = fileStat;
59
- const state = getMapCacheState();
60
- const cached = state.cache.get(absPath);
61
- if (cached && cached.mtimeMs === mtimeMs) {
62
- touchMapEntry(state.cache, absPath, cached);
63
- return cached.map;
64
- }
65
- const inflight = state.inflight.get(absPath);
66
- if (inflight) return inflight;
67
-
68
- const generation = (async () => {
69
- const map = await readseekMap(absPath, fileStat.size);
70
- rememberInMemory(absPath, { mtimeMs, map });
71
- return map;
72
- })()
73
- .catch(() => null)
74
- .finally(() => {
75
- state.inflight.delete(absPath);
76
- });
77
- state.inflight.set(absPath, generation);
78
- return generation;
79
- } catch {
80
- return null;
81
- }
82
- }