@gitpagedocs/tools 1.1.61 → 1.1.63
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 +33 -33
- package/package.json +1 -1
- package/src/ai/bootstrap.ts +50 -50
- package/src/ai/catalog.ts +188 -188
- package/src/ai/factory.ts +14 -14
- package/src/ai/http/streaming.ts +60 -60
- package/src/ai/index.ts +27 -27
- package/src/ai/legacy-adapter.ts +33 -33
- package/src/ai/model-registry.ts +13 -13
- package/src/ai/providers/anthropic-provider.ts +83 -83
- package/src/ai/providers/cohere-provider.ts +66 -66
- package/src/ai/providers/gemini-provider.ts +84 -84
- package/src/ai/providers/ollama-provider.ts +69 -69
- package/src/ai/providers/openai-compatible-provider.ts +75 -75
- package/src/ai/providers/shared.ts +72 -72
- package/src/ai/registry.ts +29 -29
- package/src/cache/file-cache.ts +86 -86
- package/src/cache/index.ts +9 -9
- package/src/cache/memory-cache.ts +47 -47
- package/src/cache/web-storage-cache.ts +91 -91
- package/src/config/config-loader.ts +59 -59
- package/src/config/index.ts +1 -1
- package/src/constants/config.ts +8 -8
- package/src/constants/index.ts +1 -1
- package/src/crypto/doc-access.ts +62 -62
- package/src/crypto/index.ts +4 -4
- package/src/crypto/node-crypto-service.ts +87 -87
- package/src/crypto/web-crypto-service.ts +102 -102
- package/src/crypto/web.ts +4 -4
- package/src/documentation/doc-generator.ts +74 -74
- package/src/documentation/doc-updater.ts +47 -47
- package/src/documentation/index.ts +18 -18
- package/src/documentation/marker-patcher.ts +33 -33
- package/src/documentation/sections.ts +84 -84
- package/src/errors/app-error.ts +84 -84
- package/src/errors/index.ts +1 -1
- package/src/filesystem/file-service.ts +121 -121
- package/src/filesystem/index.ts +2 -2
- package/src/index.ts +25 -25
- package/src/logger/index.ts +2 -2
- package/src/logger/logger.ts +73 -73
- package/src/logger/redaction.ts +43 -43
- package/src/platform/index.ts +1 -1
- package/src/platform/user-config-dir.ts +48 -48
- package/src/ports/ai.ts +109 -109
- package/src/ports/cache.ts +16 -16
- package/src/ports/config.ts +21 -21
- package/src/ports/crypto.ts +33 -33
- package/src/ports/index.ts +15 -15
- package/src/ports/logger.ts +23 -23
- package/src/ports/security.ts +42 -42
- package/src/security/credential-vault.ts +117 -117
- package/src/security/file-vault-storage.ts +25 -25
- package/src/security/index.ts +8 -8
- package/src/security/migrate-plaintext-key.ts +38 -38
- package/src/security/password-gate.ts +62 -62
- package/src/security/web-storage-vault-storage.ts +23 -23
- package/src/security/web.ts +9 -9
package/src/cache/file-cache.ts
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import type { Cache, CacheEntryOptions } from "../ports/cache";
|
|
5
|
-
import { CacheError } from "../errors/app-error";
|
|
6
|
-
|
|
7
|
-
interface PersistedEntry {
|
|
8
|
-
readonly value: unknown;
|
|
9
|
-
readonly expiresAt?: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type PersistedMap = Record<string, PersistedEntry>;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* JSON-file backed cache (Node). Suitable for CLI/MCP persistence across runs.
|
|
16
|
-
* One file holds the whole namespace; loaded lazily and written on mutation.
|
|
17
|
-
*/
|
|
18
|
-
export class FileCache<TValue = unknown> implements Cache<TValue> {
|
|
19
|
-
private cache?: PersistedMap;
|
|
20
|
-
|
|
21
|
-
constructor(
|
|
22
|
-
private readonly filePath: string,
|
|
23
|
-
private readonly now: () => number = () => Date.now(),
|
|
24
|
-
) {}
|
|
25
|
-
|
|
26
|
-
private async load(): Promise<PersistedMap> {
|
|
27
|
-
if (this.cache) return this.cache;
|
|
28
|
-
if (!existsSync(this.filePath)) {
|
|
29
|
-
this.cache = {};
|
|
30
|
-
return this.cache;
|
|
31
|
-
}
|
|
32
|
-
try {
|
|
33
|
-
const raw = await readFile(this.filePath, "utf8");
|
|
34
|
-
this.cache = raw.trim() ? (JSON.parse(raw) as PersistedMap) : {};
|
|
35
|
-
} catch (cause) {
|
|
36
|
-
throw new CacheError(`Failed to read cache file: ${this.filePath}`, { cause });
|
|
37
|
-
}
|
|
38
|
-
return this.cache;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private async persist(): Promise<void> {
|
|
42
|
-
const dir = path.dirname(this.filePath);
|
|
43
|
-
if (!existsSync(dir)) await mkdir(dir, { recursive: true });
|
|
44
|
-
await writeFile(this.filePath, JSON.stringify(this.cache ?? {}, null, 2), "utf8");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private isExpired(entry: PersistedEntry): boolean {
|
|
48
|
-
return entry.expiresAt !== undefined && entry.expiresAt <= this.now();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async get(key: string): Promise<TValue | undefined> {
|
|
52
|
-
const map = await this.load();
|
|
53
|
-
const entry = map[key];
|
|
54
|
-
if (!entry) return undefined;
|
|
55
|
-
if (this.isExpired(entry)) {
|
|
56
|
-
delete map[key];
|
|
57
|
-
await this.persist();
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
return entry.value as TValue;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async set(key: string, value: TValue, options?: CacheEntryOptions): Promise<void> {
|
|
64
|
-
const map = await this.load();
|
|
65
|
-
map[key] = {
|
|
66
|
-
value,
|
|
67
|
-
expiresAt: options?.ttlMs !== undefined ? this.now() + options.ttlMs : undefined,
|
|
68
|
-
};
|
|
69
|
-
await this.persist();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async has(key: string): Promise<boolean> {
|
|
73
|
-
return (await this.get(key)) !== undefined;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async delete(key: string): Promise<void> {
|
|
77
|
-
const map = await this.load();
|
|
78
|
-
delete map[key];
|
|
79
|
-
await this.persist();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async clear(): Promise<void> {
|
|
83
|
-
this.cache = {};
|
|
84
|
-
await this.persist();
|
|
85
|
-
}
|
|
86
|
-
}
|
|
1
|
+
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { Cache, CacheEntryOptions } from "../ports/cache";
|
|
5
|
+
import { CacheError } from "../errors/app-error";
|
|
6
|
+
|
|
7
|
+
interface PersistedEntry {
|
|
8
|
+
readonly value: unknown;
|
|
9
|
+
readonly expiresAt?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type PersistedMap = Record<string, PersistedEntry>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* JSON-file backed cache (Node). Suitable for CLI/MCP persistence across runs.
|
|
16
|
+
* One file holds the whole namespace; loaded lazily and written on mutation.
|
|
17
|
+
*/
|
|
18
|
+
export class FileCache<TValue = unknown> implements Cache<TValue> {
|
|
19
|
+
private cache?: PersistedMap;
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
private readonly filePath: string,
|
|
23
|
+
private readonly now: () => number = () => Date.now(),
|
|
24
|
+
) {}
|
|
25
|
+
|
|
26
|
+
private async load(): Promise<PersistedMap> {
|
|
27
|
+
if (this.cache) return this.cache;
|
|
28
|
+
if (!existsSync(this.filePath)) {
|
|
29
|
+
this.cache = {};
|
|
30
|
+
return this.cache;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const raw = await readFile(this.filePath, "utf8");
|
|
34
|
+
this.cache = raw.trim() ? (JSON.parse(raw) as PersistedMap) : {};
|
|
35
|
+
} catch (cause) {
|
|
36
|
+
throw new CacheError(`Failed to read cache file: ${this.filePath}`, { cause });
|
|
37
|
+
}
|
|
38
|
+
return this.cache;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private async persist(): Promise<void> {
|
|
42
|
+
const dir = path.dirname(this.filePath);
|
|
43
|
+
if (!existsSync(dir)) await mkdir(dir, { recursive: true });
|
|
44
|
+
await writeFile(this.filePath, JSON.stringify(this.cache ?? {}, null, 2), "utf8");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private isExpired(entry: PersistedEntry): boolean {
|
|
48
|
+
return entry.expiresAt !== undefined && entry.expiresAt <= this.now();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async get(key: string): Promise<TValue | undefined> {
|
|
52
|
+
const map = await this.load();
|
|
53
|
+
const entry = map[key];
|
|
54
|
+
if (!entry) return undefined;
|
|
55
|
+
if (this.isExpired(entry)) {
|
|
56
|
+
delete map[key];
|
|
57
|
+
await this.persist();
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return entry.value as TValue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async set(key: string, value: TValue, options?: CacheEntryOptions): Promise<void> {
|
|
64
|
+
const map = await this.load();
|
|
65
|
+
map[key] = {
|
|
66
|
+
value,
|
|
67
|
+
expiresAt: options?.ttlMs !== undefined ? this.now() + options.ttlMs : undefined,
|
|
68
|
+
};
|
|
69
|
+
await this.persist();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async has(key: string): Promise<boolean> {
|
|
73
|
+
return (await this.get(key)) !== undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async delete(key: string): Promise<void> {
|
|
77
|
+
const map = await this.load();
|
|
78
|
+
delete map[key];
|
|
79
|
+
await this.persist();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async clear(): Promise<void> {
|
|
83
|
+
this.cache = {};
|
|
84
|
+
await this.persist();
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/cache/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { MemoryCache } from "./memory-cache";
|
|
2
|
-
export type { Clock } from "./memory-cache";
|
|
3
|
-
export { FileCache } from "./file-cache";
|
|
4
|
-
export {
|
|
5
|
-
WebStorageCache,
|
|
6
|
-
createLocalStorageCache,
|
|
7
|
-
createSessionStorageCache,
|
|
8
|
-
} from "./web-storage-cache";
|
|
9
|
-
export type { WebStorageLike } from "./web-storage-cache";
|
|
1
|
+
export { MemoryCache } from "./memory-cache";
|
|
2
|
+
export type { Clock } from "./memory-cache";
|
|
3
|
+
export { FileCache } from "./file-cache";
|
|
4
|
+
export {
|
|
5
|
+
WebStorageCache,
|
|
6
|
+
createLocalStorageCache,
|
|
7
|
+
createSessionStorageCache,
|
|
8
|
+
} from "./web-storage-cache";
|
|
9
|
+
export type { WebStorageLike } from "./web-storage-cache";
|
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import type { Cache, CacheEntryOptions } from "../ports/cache";
|
|
2
|
-
|
|
3
|
-
interface Entry<TValue> {
|
|
4
|
-
readonly value: TValue;
|
|
5
|
-
readonly expiresAt?: number;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/** Clock injection keeps the cache testable without Date.now() flakiness. */
|
|
9
|
-
export type Clock = () => number;
|
|
10
|
-
|
|
11
|
-
/** In-process cache with optional TTL. Default Strategy for hot, ephemeral data. */
|
|
12
|
-
export class MemoryCache<TValue = unknown> implements Cache<TValue> {
|
|
13
|
-
private readonly store = new Map<string, Entry<TValue>>();
|
|
14
|
-
|
|
15
|
-
constructor(private readonly now: Clock = () => Date.now()) {}
|
|
16
|
-
|
|
17
|
-
private isExpired(entry: Entry<TValue>): boolean {
|
|
18
|
-
return entry.expiresAt !== undefined && entry.expiresAt <= this.now();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async get(key: string): Promise<TValue | undefined> {
|
|
22
|
-
const entry = this.store.get(key);
|
|
23
|
-
if (!entry) return undefined;
|
|
24
|
-
if (this.isExpired(entry)) {
|
|
25
|
-
this.store.delete(key);
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
return entry.value;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async set(key: string, value: TValue, options?: CacheEntryOptions): Promise<void> {
|
|
32
|
-
const expiresAt = options?.ttlMs !== undefined ? this.now() + options.ttlMs : undefined;
|
|
33
|
-
this.store.set(key, { value, expiresAt });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async has(key: string): Promise<boolean> {
|
|
37
|
-
return (await this.get(key)) !== undefined;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async delete(key: string): Promise<void> {
|
|
41
|
-
this.store.delete(key);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async clear(): Promise<void> {
|
|
45
|
-
this.store.clear();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
1
|
+
import type { Cache, CacheEntryOptions } from "../ports/cache";
|
|
2
|
+
|
|
3
|
+
interface Entry<TValue> {
|
|
4
|
+
readonly value: TValue;
|
|
5
|
+
readonly expiresAt?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Clock injection keeps the cache testable without Date.now() flakiness. */
|
|
9
|
+
export type Clock = () => number;
|
|
10
|
+
|
|
11
|
+
/** In-process cache with optional TTL. Default Strategy for hot, ephemeral data. */
|
|
12
|
+
export class MemoryCache<TValue = unknown> implements Cache<TValue> {
|
|
13
|
+
private readonly store = new Map<string, Entry<TValue>>();
|
|
14
|
+
|
|
15
|
+
constructor(private readonly now: Clock = () => Date.now()) {}
|
|
16
|
+
|
|
17
|
+
private isExpired(entry: Entry<TValue>): boolean {
|
|
18
|
+
return entry.expiresAt !== undefined && entry.expiresAt <= this.now();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async get(key: string): Promise<TValue | undefined> {
|
|
22
|
+
const entry = this.store.get(key);
|
|
23
|
+
if (!entry) return undefined;
|
|
24
|
+
if (this.isExpired(entry)) {
|
|
25
|
+
this.store.delete(key);
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
return entry.value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async set(key: string, value: TValue, options?: CacheEntryOptions): Promise<void> {
|
|
32
|
+
const expiresAt = options?.ttlMs !== undefined ? this.now() + options.ttlMs : undefined;
|
|
33
|
+
this.store.set(key, { value, expiresAt });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async has(key: string): Promise<boolean> {
|
|
37
|
+
return (await this.get(key)) !== undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async delete(key: string): Promise<void> {
|
|
41
|
+
this.store.delete(key);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async clear(): Promise<void> {
|
|
45
|
+
this.store.clear();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
import type { Cache, CacheEntryOptions } from "../ports/cache";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Minimal subset of the Web Storage API. Injected so this module stays free of
|
|
5
|
-
* DOM lib types and works in the Node-typed tools package. The browser passes
|
|
6
|
-
* `window.localStorage` (→ LocalStorageCache) or `window.sessionStorage`
|
|
7
|
-
* (→ SessionStorageCache).
|
|
8
|
-
*/
|
|
9
|
-
export interface WebStorageLike {
|
|
10
|
-
getItem(key: string): string | null;
|
|
11
|
-
setItem(key: string, value: string): void;
|
|
12
|
-
removeItem(key: string): void;
|
|
13
|
-
key(index: number): string | null;
|
|
14
|
-
readonly length: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface StoredEntry {
|
|
18
|
-
readonly value: unknown;
|
|
19
|
-
readonly expiresAt?: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Cache backed by a Web Storage-like store, namespaced by key prefix. */
|
|
23
|
-
export class WebStorageCache<TValue = unknown> implements Cache<TValue> {
|
|
24
|
-
constructor(
|
|
25
|
-
private readonly storage: WebStorageLike,
|
|
26
|
-
private readonly prefix = "gpd:cache:",
|
|
27
|
-
private readonly now: () => number = () => Date.now(),
|
|
28
|
-
) {}
|
|
29
|
-
|
|
30
|
-
private k(key: string): string {
|
|
31
|
-
return `${this.prefix}${key}`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async get(key: string): Promise<TValue | undefined> {
|
|
35
|
-
const raw = this.storage.getItem(this.k(key));
|
|
36
|
-
if (raw === null) return undefined;
|
|
37
|
-
let entry: StoredEntry;
|
|
38
|
-
try {
|
|
39
|
-
entry = JSON.parse(raw) as StoredEntry;
|
|
40
|
-
} catch {
|
|
41
|
-
this.storage.removeItem(this.k(key));
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
if (entry.expiresAt !== undefined && entry.expiresAt <= this.now()) {
|
|
45
|
-
this.storage.removeItem(this.k(key));
|
|
46
|
-
return undefined;
|
|
47
|
-
}
|
|
48
|
-
return entry.value as TValue;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async set(key: string, value: TValue, options?: CacheEntryOptions): Promise<void> {
|
|
52
|
-
const entry: StoredEntry = {
|
|
53
|
-
value,
|
|
54
|
-
expiresAt: options?.ttlMs !== undefined ? this.now() + options.ttlMs : undefined,
|
|
55
|
-
};
|
|
56
|
-
this.storage.setItem(this.k(key), JSON.stringify(entry));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async has(key: string): Promise<boolean> {
|
|
60
|
-
return (await this.get(key)) !== undefined;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async delete(key: string): Promise<void> {
|
|
64
|
-
this.storage.removeItem(this.k(key));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async clear(): Promise<void> {
|
|
68
|
-
const toRemove: string[] = [];
|
|
69
|
-
for (let i = 0; i < this.storage.length; i += 1) {
|
|
70
|
-
const k = this.storage.key(i);
|
|
71
|
-
if (k && k.startsWith(this.prefix)) toRemove.push(k);
|
|
72
|
-
}
|
|
73
|
-
for (const k of toRemove) this.storage.removeItem(k);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** localStorage-backed cache (persistent). */
|
|
78
|
-
export function createLocalStorageCache<TValue = unknown>(
|
|
79
|
-
storage: WebStorageLike,
|
|
80
|
-
prefix?: string,
|
|
81
|
-
): WebStorageCache<TValue> {
|
|
82
|
-
return new WebStorageCache<TValue>(storage, prefix ?? "gpd:local:");
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** sessionStorage-backed cache (per-session). */
|
|
86
|
-
export function createSessionStorageCache<TValue = unknown>(
|
|
87
|
-
storage: WebStorageLike,
|
|
88
|
-
prefix?: string,
|
|
89
|
-
): WebStorageCache<TValue> {
|
|
90
|
-
return new WebStorageCache<TValue>(storage, prefix ?? "gpd:session:");
|
|
91
|
-
}
|
|
1
|
+
import type { Cache, CacheEntryOptions } from "../ports/cache";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Minimal subset of the Web Storage API. Injected so this module stays free of
|
|
5
|
+
* DOM lib types and works in the Node-typed tools package. The browser passes
|
|
6
|
+
* `window.localStorage` (→ LocalStorageCache) or `window.sessionStorage`
|
|
7
|
+
* (→ SessionStorageCache).
|
|
8
|
+
*/
|
|
9
|
+
export interface WebStorageLike {
|
|
10
|
+
getItem(key: string): string | null;
|
|
11
|
+
setItem(key: string, value: string): void;
|
|
12
|
+
removeItem(key: string): void;
|
|
13
|
+
key(index: number): string | null;
|
|
14
|
+
readonly length: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface StoredEntry {
|
|
18
|
+
readonly value: unknown;
|
|
19
|
+
readonly expiresAt?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Cache backed by a Web Storage-like store, namespaced by key prefix. */
|
|
23
|
+
export class WebStorageCache<TValue = unknown> implements Cache<TValue> {
|
|
24
|
+
constructor(
|
|
25
|
+
private readonly storage: WebStorageLike,
|
|
26
|
+
private readonly prefix = "gpd:cache:",
|
|
27
|
+
private readonly now: () => number = () => Date.now(),
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
private k(key: string): string {
|
|
31
|
+
return `${this.prefix}${key}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async get(key: string): Promise<TValue | undefined> {
|
|
35
|
+
const raw = this.storage.getItem(this.k(key));
|
|
36
|
+
if (raw === null) return undefined;
|
|
37
|
+
let entry: StoredEntry;
|
|
38
|
+
try {
|
|
39
|
+
entry = JSON.parse(raw) as StoredEntry;
|
|
40
|
+
} catch {
|
|
41
|
+
this.storage.removeItem(this.k(key));
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
if (entry.expiresAt !== undefined && entry.expiresAt <= this.now()) {
|
|
45
|
+
this.storage.removeItem(this.k(key));
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
return entry.value as TValue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async set(key: string, value: TValue, options?: CacheEntryOptions): Promise<void> {
|
|
52
|
+
const entry: StoredEntry = {
|
|
53
|
+
value,
|
|
54
|
+
expiresAt: options?.ttlMs !== undefined ? this.now() + options.ttlMs : undefined,
|
|
55
|
+
};
|
|
56
|
+
this.storage.setItem(this.k(key), JSON.stringify(entry));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async has(key: string): Promise<boolean> {
|
|
60
|
+
return (await this.get(key)) !== undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async delete(key: string): Promise<void> {
|
|
64
|
+
this.storage.removeItem(this.k(key));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async clear(): Promise<void> {
|
|
68
|
+
const toRemove: string[] = [];
|
|
69
|
+
for (let i = 0; i < this.storage.length; i += 1) {
|
|
70
|
+
const k = this.storage.key(i);
|
|
71
|
+
if (k && k.startsWith(this.prefix)) toRemove.push(k);
|
|
72
|
+
}
|
|
73
|
+
for (const k of toRemove) this.storage.removeItem(k);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** localStorage-backed cache (persistent). */
|
|
78
|
+
export function createLocalStorageCache<TValue = unknown>(
|
|
79
|
+
storage: WebStorageLike,
|
|
80
|
+
prefix?: string,
|
|
81
|
+
): WebStorageCache<TValue> {
|
|
82
|
+
return new WebStorageCache<TValue>(storage, prefix ?? "gpd:local:");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** sessionStorage-backed cache (per-session). */
|
|
86
|
+
export function createSessionStorageCache<TValue = unknown>(
|
|
87
|
+
storage: WebStorageLike,
|
|
88
|
+
prefix?: string,
|
|
89
|
+
): WebStorageCache<TValue> {
|
|
90
|
+
return new WebStorageCache<TValue>(storage, prefix ?? "gpd:session:");
|
|
91
|
+
}
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { readFile } from "node:fs/promises";
|
|
4
|
-
import { pathToFileURL } from "node:url";
|
|
5
|
-
import type { ConfigExtension, ConfigLoader, LoadedConfig } from "../ports/config";
|
|
6
|
-
import { CONFIG_BASE, CONFIG_EXTENSIONS } from "../constants/config";
|
|
7
|
-
import { ConfigurationError } from "../errors/app-error";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Node config loader for gitpagedocs/config.{json,js,ts}. Behaviour mirrors the
|
|
11
|
-
* frontend loader (src/entities/docs/api/io/config-loader.ts) so the contract
|
|
12
|
-
* is identical: JSON is parsed, JS/TS are dynamically imported (default export
|
|
13
|
-
* or module namespace).
|
|
14
|
-
*/
|
|
15
|
-
export class GitPageDocsConfigLoader implements ConfigLoader {
|
|
16
|
-
async resolveConfigPath(cwd: string = process.cwd()): Promise<string | undefined> {
|
|
17
|
-
const base = path.join(cwd, CONFIG_BASE);
|
|
18
|
-
for (const ext of CONFIG_EXTENSIONS) {
|
|
19
|
-
const fullPath = base + ext;
|
|
20
|
-
if (existsSync(fullPath)) return fullPath;
|
|
21
|
-
}
|
|
22
|
-
return undefined;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async loadGitPageDocsConfig<TConfig = Record<string, unknown>>(
|
|
26
|
-
cwd: string = process.cwd(),
|
|
27
|
-
): Promise<LoadedConfig<TConfig>> {
|
|
28
|
-
const sourcePath = await this.resolveConfigPath(cwd);
|
|
29
|
-
if (!sourcePath) {
|
|
30
|
-
throw new ConfigurationError(
|
|
31
|
-
`No config file found. Expected one of: ${CONFIG_EXTENSIONS.map((e) => CONFIG_BASE + e).join(", ")}`,
|
|
32
|
-
{ details: { cwd } },
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
const extension = path.extname(sourcePath) as ConfigExtension;
|
|
36
|
-
const config = await this.parse<TConfig>(sourcePath, extension);
|
|
37
|
-
return { config, sourcePath, extension };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private async parse<TConfig>(resolvedPath: string, ext: ConfigExtension): Promise<TConfig> {
|
|
41
|
-
if (ext === ".json") {
|
|
42
|
-
const text = await readFile(resolvedPath, "utf8");
|
|
43
|
-
return JSON.parse(text) as TConfig;
|
|
44
|
-
}
|
|
45
|
-
if (ext === ".js" || ext === ".ts") {
|
|
46
|
-
const mod = (await import(pathToFileURL(resolvedPath).href)) as Record<string, unknown>;
|
|
47
|
-
const value = (mod.default ?? mod) as TConfig | undefined;
|
|
48
|
-
if (value == null) {
|
|
49
|
-
throw new ConfigurationError(
|
|
50
|
-
`Config file ${resolvedPath} did not export default or module.exports.`,
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
return value;
|
|
54
|
-
}
|
|
55
|
-
throw new ConfigurationError(`Unsupported config extension: ${ext}`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export const defaultConfigLoader = new GitPageDocsConfigLoader();
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import type { ConfigExtension, ConfigLoader, LoadedConfig } from "../ports/config";
|
|
6
|
+
import { CONFIG_BASE, CONFIG_EXTENSIONS } from "../constants/config";
|
|
7
|
+
import { ConfigurationError } from "../errors/app-error";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Node config loader for gitpagedocs/config.{json,js,ts}. Behaviour mirrors the
|
|
11
|
+
* frontend loader (src/entities/docs/api/io/config-loader.ts) so the contract
|
|
12
|
+
* is identical: JSON is parsed, JS/TS are dynamically imported (default export
|
|
13
|
+
* or module namespace).
|
|
14
|
+
*/
|
|
15
|
+
export class GitPageDocsConfigLoader implements ConfigLoader {
|
|
16
|
+
async resolveConfigPath(cwd: string = process.cwd()): Promise<string | undefined> {
|
|
17
|
+
const base = path.join(cwd, CONFIG_BASE);
|
|
18
|
+
for (const ext of CONFIG_EXTENSIONS) {
|
|
19
|
+
const fullPath = base + ext;
|
|
20
|
+
if (existsSync(fullPath)) return fullPath;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async loadGitPageDocsConfig<TConfig = Record<string, unknown>>(
|
|
26
|
+
cwd: string = process.cwd(),
|
|
27
|
+
): Promise<LoadedConfig<TConfig>> {
|
|
28
|
+
const sourcePath = await this.resolveConfigPath(cwd);
|
|
29
|
+
if (!sourcePath) {
|
|
30
|
+
throw new ConfigurationError(
|
|
31
|
+
`No config file found. Expected one of: ${CONFIG_EXTENSIONS.map((e) => CONFIG_BASE + e).join(", ")}`,
|
|
32
|
+
{ details: { cwd } },
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
const extension = path.extname(sourcePath) as ConfigExtension;
|
|
36
|
+
const config = await this.parse<TConfig>(sourcePath, extension);
|
|
37
|
+
return { config, sourcePath, extension };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private async parse<TConfig>(resolvedPath: string, ext: ConfigExtension): Promise<TConfig> {
|
|
41
|
+
if (ext === ".json") {
|
|
42
|
+
const text = await readFile(resolvedPath, "utf8");
|
|
43
|
+
return JSON.parse(text) as TConfig;
|
|
44
|
+
}
|
|
45
|
+
if (ext === ".js" || ext === ".ts") {
|
|
46
|
+
const mod = (await import(pathToFileURL(resolvedPath).href)) as Record<string, unknown>;
|
|
47
|
+
const value = (mod.default ?? mod) as TConfig | undefined;
|
|
48
|
+
if (value == null) {
|
|
49
|
+
throw new ConfigurationError(
|
|
50
|
+
`Config file ${resolvedPath} did not export default or module.exports.`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
throw new ConfigurationError(`Unsupported config extension: ${ext}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const defaultConfigLoader = new GitPageDocsConfigLoader();
|
package/src/config/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { GitPageDocsConfigLoader, defaultConfigLoader } from "./config-loader";
|
|
1
|
+
export { GitPageDocsConfigLoader, defaultConfigLoader } from "./config-loader";
|
package/src/constants/config.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Config constants — mirror of src/shared/config/constants.ts so the shared
|
|
3
|
-
* core stays contract-compatible with the frontend's config loader.
|
|
4
|
-
*/
|
|
5
|
-
export const CONFIG_BASE = "gitpagedocs/config";
|
|
6
|
-
export const CONFIG_EXTENSIONS = [".json", ".js", ".ts"] as const;
|
|
7
|
-
export const DEFAULT_CONFIG_PATH = "gitpagedocs/config.json";
|
|
8
|
-
export const DEFAULT_HIERARCHY = { md: 0, html: 1, video: 2, audio: 3 } as const;
|
|
1
|
+
/**
|
|
2
|
+
* Config constants — mirror of src/shared/config/constants.ts so the shared
|
|
3
|
+
* core stays contract-compatible with the frontend's config loader.
|
|
4
|
+
*/
|
|
5
|
+
export const CONFIG_BASE = "gitpagedocs/config";
|
|
6
|
+
export const CONFIG_EXTENSIONS = [".json", ".js", ".ts"] as const;
|
|
7
|
+
export const DEFAULT_CONFIG_PATH = "gitpagedocs/config.json";
|
|
8
|
+
export const DEFAULT_HIERARCHY = { md: 0, html: 1, video: 2, audio: 3 } as const;
|
package/src/constants/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./config";
|
|
1
|
+
export * from "./config";
|