@dxos/log-store-idb 0.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/README.md +30 -0
- package/dist/lib/browser/index.mjs +397 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +399 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/idb-log-store.browser.test.d.ts +2 -0
- package/dist/types/src/idb-log-store.browser.test.d.ts.map +1 -0
- package/dist/types/src/idb-log-store.d.ts +79 -0
- package/dist/types/src/idb-log-store.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/trim.d.ts +9 -0
- package/dist/types/src/trim.d.ts.map +1 -0
- package/dist/types/src/trim.test.d.ts +2 -0
- package/dist/types/src/trim.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +38 -0
- package/src/idb-log-store.browser.test.ts +200 -0
- package/src/idb-log-store.ts +475 -0
- package/src/index.ts +6 -0
- package/src/trim.test.ts +56 -0
- package/src/trim.ts +46 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { type LogConfig, type LogEntry, LogLevel } from '@dxos/log';
|
|
8
|
+
|
|
9
|
+
import { IdbLogStore } from './idb-log-store';
|
|
10
|
+
|
|
11
|
+
const fakeConfig = {} as LogConfig;
|
|
12
|
+
|
|
13
|
+
const makeEntry = (level: LogLevel, message: string, timestamp = Date.now()): LogEntry => {
|
|
14
|
+
// Use a real LogEntry shape — getter access on `computedMeta`/`computedContext`/`computedError`
|
|
15
|
+
// is what the encoder uses, so we mock those getters with plain values.
|
|
16
|
+
return {
|
|
17
|
+
level,
|
|
18
|
+
message,
|
|
19
|
+
timestamp,
|
|
20
|
+
computedMeta: {},
|
|
21
|
+
computedContext: {},
|
|
22
|
+
computedError: undefined,
|
|
23
|
+
} as unknown as LogEntry;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
let dbCounter = 0;
|
|
27
|
+
const makeDbName = () => `dxos-log-store-test-${Date.now()}-${++dbCounter}`;
|
|
28
|
+
|
|
29
|
+
const drop = (dbName: string): Promise<void> =>
|
|
30
|
+
new Promise((resolve) => {
|
|
31
|
+
const req = indexedDB.deleteDatabase(dbName);
|
|
32
|
+
req.onsuccess = () => resolve();
|
|
33
|
+
req.onerror = () => resolve();
|
|
34
|
+
req.onblocked = () => resolve();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('IdbLogStore', () => {
|
|
38
|
+
let store: IdbLogStore | undefined;
|
|
39
|
+
let dbName = '';
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
dbName = makeDbName();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterEach(async () => {
|
|
46
|
+
await store?.close();
|
|
47
|
+
store = undefined;
|
|
48
|
+
await drop(dbName);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('persists log entries and exports them as JSONL', async ({ expect }) => {
|
|
52
|
+
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
53
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, 'hello'));
|
|
54
|
+
store.processor(fakeConfig, makeEntry(LogLevel.WARN, 'world'));
|
|
55
|
+
await store.flush();
|
|
56
|
+
|
|
57
|
+
const jsonl = await store.export();
|
|
58
|
+
const lines = jsonl.split('\n');
|
|
59
|
+
expect(lines).toHaveLength(2);
|
|
60
|
+
const first = JSON.parse(lines[0]);
|
|
61
|
+
const second = JSON.parse(lines[1]);
|
|
62
|
+
expect(first.m).toBe('hello');
|
|
63
|
+
expect(first.l).toBe('I');
|
|
64
|
+
expect(second.m).toBe('world');
|
|
65
|
+
expect(second.l).toBe('W');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('skips TRACE entries (default logFilter is debug)', async ({ expect }) => {
|
|
69
|
+
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
70
|
+
store.processor(fakeConfig, makeEntry(LogLevel.TRACE, 'noisy'));
|
|
71
|
+
store.processor(fakeConfig, makeEntry(LogLevel.DEBUG, 'debug-line'));
|
|
72
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, 'kept'));
|
|
73
|
+
await store.flush();
|
|
74
|
+
|
|
75
|
+
const jsonl = await store.export();
|
|
76
|
+
const messages = jsonl
|
|
77
|
+
.split('\n')
|
|
78
|
+
.filter(Boolean)
|
|
79
|
+
.map((line) => JSON.parse(line).m);
|
|
80
|
+
expect(messages).toHaveLength(2);
|
|
81
|
+
expect(messages).toContain('debug-line');
|
|
82
|
+
expect(messages).toContain('kept');
|
|
83
|
+
expect(messages).not.toContain('noisy');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('logFilter tightens minimum level', async ({ expect }) => {
|
|
87
|
+
store = new IdbLogStore({ dbName, flushInterval: 10, logFilter: 'info' });
|
|
88
|
+
store.processor(fakeConfig, makeEntry(LogLevel.DEBUG, 'dropped'));
|
|
89
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, 'kept'));
|
|
90
|
+
await store.flush();
|
|
91
|
+
const jsonl = await store.export();
|
|
92
|
+
const messages = jsonl
|
|
93
|
+
.split('\n')
|
|
94
|
+
.filter(Boolean)
|
|
95
|
+
.map((line) => JSON.parse(line).m);
|
|
96
|
+
expect(messages).toEqual(['kept']);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('flushes when batch threshold is exceeded', async ({ expect }) => {
|
|
100
|
+
store = new IdbLogStore({ dbName, flushBatchSize: 3, flushInterval: 60_000 });
|
|
101
|
+
for (let i = 0; i < 3; i++) {
|
|
102
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, `msg-${i}`));
|
|
103
|
+
}
|
|
104
|
+
// Threshold reached; wait for the spawned flush to commit.
|
|
105
|
+
await store.flush();
|
|
106
|
+
const jsonl = await store.export();
|
|
107
|
+
expect(jsonl.split('\n')).toHaveLength(3);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('clear removes all entries', async ({ expect }) => {
|
|
111
|
+
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
112
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, 'a'));
|
|
113
|
+
await store.flush();
|
|
114
|
+
await store.clear();
|
|
115
|
+
expect(await store.export()).toBe('');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('export with maxSize trims oldest lines first', async ({ expect }) => {
|
|
119
|
+
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
120
|
+
for (let i = 0; i < 10; i++) {
|
|
121
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, `msg-${i}`));
|
|
122
|
+
}
|
|
123
|
+
await store.flush();
|
|
124
|
+
const all = await store.export();
|
|
125
|
+
const allLines = all.split('\n');
|
|
126
|
+
expect(allLines).toHaveLength(10);
|
|
127
|
+
|
|
128
|
+
// Cap to half the byte size: should keep the newest lines, never split.
|
|
129
|
+
const halfSize = Math.floor(all.length / 2);
|
|
130
|
+
const trimmed = await store.export({ maxSize: halfSize });
|
|
131
|
+
const trimmedLines = trimmed.split('\n').filter((line) => line.length > 0);
|
|
132
|
+
|
|
133
|
+
expect(trimmedLines.length).toBeGreaterThan(0);
|
|
134
|
+
expect(trimmedLines.length).toBeLessThan(allLines.length);
|
|
135
|
+
// Every kept line must parse cleanly (i.e. not split mid-record).
|
|
136
|
+
for (const line of trimmedLines) {
|
|
137
|
+
expect(() => JSON.parse(line)).not.toThrow();
|
|
138
|
+
}
|
|
139
|
+
// Newest must be retained.
|
|
140
|
+
expect(JSON.parse(trimmedLines[trimmedLines.length - 1]).m).toBe('msg-9');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('survives reopen — persists across store instances', async ({ expect }) => {
|
|
144
|
+
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
145
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, 'persisted'));
|
|
146
|
+
await store.flush();
|
|
147
|
+
await store.close();
|
|
148
|
+
|
|
149
|
+
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
150
|
+
const jsonl = await store.export();
|
|
151
|
+
expect(jsonl).toContain('persisted');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('eviction trims down to maxRecords', async ({ expect }) => {
|
|
155
|
+
store = new IdbLogStore({
|
|
156
|
+
dbName,
|
|
157
|
+
flushInterval: 10,
|
|
158
|
+
maxRecords: 5,
|
|
159
|
+
evictionInterval: 0, // disable timer; trigger manually
|
|
160
|
+
});
|
|
161
|
+
for (let i = 0; i < 12; i++) {
|
|
162
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, `msg-${i}`));
|
|
163
|
+
}
|
|
164
|
+
await store.flush();
|
|
165
|
+
await store.evictNow();
|
|
166
|
+
const jsonl = await store.export();
|
|
167
|
+
const lines = jsonl.split('\n').filter((line) => line.length > 0);
|
|
168
|
+
expect(lines.length).toBe(5);
|
|
169
|
+
// The newest line must always survive eviction.
|
|
170
|
+
expect(JSON.parse(lines[lines.length - 1]).m).toBe('msg-11');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('multiple stores against the same db preserve all writes', async ({ expect }) => {
|
|
174
|
+
const sharedDb = makeDbName();
|
|
175
|
+
try {
|
|
176
|
+
const a = new IdbLogStore({ dbName: sharedDb, flushInterval: 10, tabId: 'tab-a' });
|
|
177
|
+
const b = new IdbLogStore({ dbName: sharedDb, flushInterval: 10, tabId: 'tab-b' });
|
|
178
|
+
a.processor(fakeConfig, makeEntry(LogLevel.INFO, 'from-a-1'));
|
|
179
|
+
b.processor(fakeConfig, makeEntry(LogLevel.INFO, 'from-b-1'));
|
|
180
|
+
a.processor(fakeConfig, makeEntry(LogLevel.INFO, 'from-a-2'));
|
|
181
|
+
await Promise.all([a.flush(), b.flush()]);
|
|
182
|
+
|
|
183
|
+
const exported = await a.export();
|
|
184
|
+
const messages = exported.split('\n').map((line) => JSON.parse(line).m);
|
|
185
|
+
expect(messages).toContain('from-a-1');
|
|
186
|
+
expect(messages).toContain('from-a-2');
|
|
187
|
+
expect(messages).toContain('from-b-1');
|
|
188
|
+
|
|
189
|
+
// Tab IDs are recorded per record.
|
|
190
|
+
const tabIds = new Set(exported.split('\n').map((line) => JSON.parse(line).i));
|
|
191
|
+
expect(tabIds.has('tab-a')).toBe(true);
|
|
192
|
+
expect(tabIds.has('tab-b')).toBe(true);
|
|
193
|
+
|
|
194
|
+
await a.close();
|
|
195
|
+
await b.close();
|
|
196
|
+
} finally {
|
|
197
|
+
await drop(sharedDb);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type LogConfig,
|
|
7
|
+
type LogEntry,
|
|
8
|
+
type LogFilter,
|
|
9
|
+
type LogProcessor,
|
|
10
|
+
inferEnvironmentName,
|
|
11
|
+
parseFilter,
|
|
12
|
+
serializeToJsonl,
|
|
13
|
+
shouldLog,
|
|
14
|
+
} from '@dxos/log';
|
|
15
|
+
|
|
16
|
+
import { trimJsonlToSize } from './trim';
|
|
17
|
+
|
|
18
|
+
const DEFAULT_STORE_NAME = 'logs';
|
|
19
|
+
const DEFAULT_LOG_FILTER = 'debug';
|
|
20
|
+
const DEFAULT_FLUSH_INTERVAL = 250;
|
|
21
|
+
const DEFAULT_FLUSH_BATCH_SIZE = 500;
|
|
22
|
+
// Sized for ~50 MB on disk at the observed Composer average of ~350 bytes per JSONL line.
|
|
23
|
+
// Adjust `maxRecords` if your workload's per-entry size differs significantly.
|
|
24
|
+
const DEFAULT_MAX_RECORDS = 150_000;
|
|
25
|
+
const DEFAULT_EVICTION_INTERVAL = 30_000;
|
|
26
|
+
const EVICTION_LOCK_NAME = '@dxos/log-store-idb:evictor';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Stored log row. The line is pre-encoded JSONL (no trailing newline).
|
|
30
|
+
*/
|
|
31
|
+
type LogRow = {
|
|
32
|
+
/** Auto-incremented sequence (commit order). */
|
|
33
|
+
seq?: number;
|
|
34
|
+
/** Encoded JSONL line. */
|
|
35
|
+
line: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type IdbLogStoreOptions = {
|
|
39
|
+
/** Name of the IndexedDB database. */
|
|
40
|
+
dbName: string;
|
|
41
|
+
/** Object store name. Default `'logs'`. */
|
|
42
|
+
storeName?: string;
|
|
43
|
+
/** Auto-flush interval in milliseconds. Default `250`. */
|
|
44
|
+
flushInterval?: number;
|
|
45
|
+
/** Auto-flush when in-memory queue reaches this size. Default `500`. */
|
|
46
|
+
flushBatchSize?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Soft cap on retained records. Older rows are evicted past this. Default `150_000`,
|
|
49
|
+
* sized for roughly 50 MB on disk at typical JSONL line sizes (~350 bytes average
|
|
50
|
+
* observed in the Composer app).
|
|
51
|
+
*/
|
|
52
|
+
maxRecords?: number;
|
|
53
|
+
/** Eviction sweep interval in milliseconds. Default `30_000`. */
|
|
54
|
+
evictionInterval?: number;
|
|
55
|
+
/**
|
|
56
|
+
* Identifier embedded in every record's `i` field.
|
|
57
|
+
* Defaults to a scope-aware id of the form `<scope>:<name>:<suffix>` — see {@link inferEnvironmentName}.
|
|
58
|
+
*/
|
|
59
|
+
tabId?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Same syntax as `DX_LOG` / {@link parseFilter} (e.g. `debug`, `info`, `pattern:warn`).
|
|
62
|
+
* Entries below the minimum level are not stored. Default `debug` (TRACE is excluded).
|
|
63
|
+
*/
|
|
64
|
+
logFilter?: string;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* IndexedDB-backed log store.
|
|
69
|
+
*
|
|
70
|
+
* Logs are batched in memory and flushed to IndexedDB as pre-encoded JSONL strings.
|
|
71
|
+
* Multiple browsing contexts can write to the same database concurrently — IDB
|
|
72
|
+
* serializes transactions, so no extra coordination is needed for writes.
|
|
73
|
+
* Eviction is deduplicated across tabs via the Web Locks API (`ifAvailable`).
|
|
74
|
+
*
|
|
75
|
+
* Flushes are triggered by:
|
|
76
|
+
* - the configured interval timer,
|
|
77
|
+
* - the queue exceeding `flushBatchSize`,
|
|
78
|
+
* - `visibilitychange` to `hidden` and `pagehide` events.
|
|
79
|
+
*/
|
|
80
|
+
export class IdbLogStore {
|
|
81
|
+
readonly #dbName: string;
|
|
82
|
+
readonly #storeName: string;
|
|
83
|
+
readonly #flushInterval: number;
|
|
84
|
+
readonly #flushBatchSize: number;
|
|
85
|
+
readonly #maxRecords: number;
|
|
86
|
+
readonly #evictionInterval: number;
|
|
87
|
+
readonly #tabId: string;
|
|
88
|
+
readonly #filters: LogFilter[];
|
|
89
|
+
|
|
90
|
+
#queue: string[] = [];
|
|
91
|
+
#flushTimer: ReturnType<typeof setTimeout> | undefined;
|
|
92
|
+
#evictionTimer: ReturnType<typeof setInterval> | undefined;
|
|
93
|
+
#pendingFlush: Promise<void> | undefined;
|
|
94
|
+
#db: IDBDatabase | undefined;
|
|
95
|
+
#dbPromise: Promise<IDBDatabase> | undefined;
|
|
96
|
+
#closed = false;
|
|
97
|
+
#pageHideHandler: (() => void) | undefined;
|
|
98
|
+
#visibilityHandler: (() => void) | undefined;
|
|
99
|
+
|
|
100
|
+
constructor(options: IdbLogStoreOptions) {
|
|
101
|
+
this.#dbName = options.dbName;
|
|
102
|
+
this.#storeName = options.storeName ?? DEFAULT_STORE_NAME;
|
|
103
|
+
this.#flushInterval = options.flushInterval ?? DEFAULT_FLUSH_INTERVAL;
|
|
104
|
+
this.#flushBatchSize = options.flushBatchSize ?? DEFAULT_FLUSH_BATCH_SIZE;
|
|
105
|
+
this.#maxRecords = options.maxRecords ?? DEFAULT_MAX_RECORDS;
|
|
106
|
+
this.#evictionInterval = options.evictionInterval ?? DEFAULT_EVICTION_INTERVAL;
|
|
107
|
+
this.#tabId = options.tabId ?? inferEnvironmentName();
|
|
108
|
+
this.#filters = parseFilter(options.logFilter ?? DEFAULT_LOG_FILTER);
|
|
109
|
+
|
|
110
|
+
this.#installLifecycleHandlers();
|
|
111
|
+
this.#scheduleEviction();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Log processor — register via `log.addProcessor(store.processor)`.
|
|
116
|
+
*/
|
|
117
|
+
readonly processor: LogProcessor = (_config: LogConfig, entry: LogEntry) => {
|
|
118
|
+
if (this.#closed) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (!shouldLog(entry, this.#filters)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const line = serializeToJsonl(entry, { env: this.#tabId });
|
|
125
|
+
if (line === undefined) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
this.#queue.push(line);
|
|
129
|
+
|
|
130
|
+
if (this.#queue.length >= this.#flushBatchSize) {
|
|
131
|
+
void this.flush();
|
|
132
|
+
} else if (this.#flushTimer === undefined) {
|
|
133
|
+
this.#flushTimer = setTimeout(() => {
|
|
134
|
+
this.#flushTimer = undefined;
|
|
135
|
+
void this.flush();
|
|
136
|
+
}, this.#flushInterval);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Force a flush now and resolve once the IDB transaction commits.
|
|
142
|
+
* Concurrent calls share a single in-flight flush.
|
|
143
|
+
*/
|
|
144
|
+
async flush(): Promise<void> {
|
|
145
|
+
if (this.#flushTimer !== undefined) {
|
|
146
|
+
clearTimeout(this.#flushTimer);
|
|
147
|
+
this.#flushTimer = undefined;
|
|
148
|
+
}
|
|
149
|
+
if (this.#pendingFlush) {
|
|
150
|
+
return this.#pendingFlush;
|
|
151
|
+
}
|
|
152
|
+
if (this.#queue.length === 0) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const batch = this.#queue;
|
|
157
|
+
this.#queue = [];
|
|
158
|
+
|
|
159
|
+
this.#pendingFlush = this.#writeBatch(batch).finally(() => {
|
|
160
|
+
this.#pendingFlush = undefined;
|
|
161
|
+
});
|
|
162
|
+
return this.#pendingFlush;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Read all retained log lines as a JSONL string.
|
|
167
|
+
*
|
|
168
|
+
* If `maxSize` is provided (in bytes), the newest lines are kept and older lines
|
|
169
|
+
* are dropped from the front; lines are never split.
|
|
170
|
+
*/
|
|
171
|
+
async export(options: { maxSize?: number } = {}): Promise<string> {
|
|
172
|
+
await this.flush();
|
|
173
|
+
const db = await this.#open();
|
|
174
|
+
const lines = await readAllLines(db, this.#storeName);
|
|
175
|
+
if (options.maxSize !== undefined && options.maxSize >= 0) {
|
|
176
|
+
return trimJsonlToSize(lines, options.maxSize);
|
|
177
|
+
}
|
|
178
|
+
return lines.join('\n');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Discard all stored log records.
|
|
183
|
+
*/
|
|
184
|
+
async clear(): Promise<void> {
|
|
185
|
+
this.#queue = [];
|
|
186
|
+
if (this.#flushTimer !== undefined) {
|
|
187
|
+
clearTimeout(this.#flushTimer);
|
|
188
|
+
this.#flushTimer = undefined;
|
|
189
|
+
}
|
|
190
|
+
const db = await this.#open();
|
|
191
|
+
await runTransaction(db, this.#storeName, 'readwrite', (store) => {
|
|
192
|
+
store.clear();
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Run an eviction sweep now: trim the store down to `maxRecords` if it has grown past.
|
|
198
|
+
* Eviction runs automatically on a timer; this method is exposed for tests and for
|
|
199
|
+
* callers that want to force a sweep at a particular moment.
|
|
200
|
+
*/
|
|
201
|
+
async evictNow(): Promise<void> {
|
|
202
|
+
await this.#maybeEvict();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Stop timers, flush remaining entries, and close the database connection.
|
|
207
|
+
*/
|
|
208
|
+
async close(): Promise<void> {
|
|
209
|
+
if (this.#closed) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
this.#closed = true;
|
|
213
|
+
|
|
214
|
+
if (this.#evictionTimer !== undefined) {
|
|
215
|
+
clearInterval(this.#evictionTimer);
|
|
216
|
+
this.#evictionTimer = undefined;
|
|
217
|
+
}
|
|
218
|
+
this.#removeLifecycleHandlers();
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
await this.flush();
|
|
222
|
+
} catch {
|
|
223
|
+
// Ignore flush errors during shutdown.
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (this.#db) {
|
|
227
|
+
this.#db.close();
|
|
228
|
+
this.#db = undefined;
|
|
229
|
+
this.#dbPromise = undefined;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async #writeBatch(batch: string[]): Promise<void> {
|
|
234
|
+
if (batch.length === 0) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
let db: IDBDatabase;
|
|
238
|
+
try {
|
|
239
|
+
db = await this.#open();
|
|
240
|
+
} catch {
|
|
241
|
+
// If the database can't be opened, drop the batch silently — logs must never
|
|
242
|
+
// throw out of the processor path.
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
await runTransaction(db, this.#storeName, 'readwrite', (store) => {
|
|
248
|
+
for (const line of batch) {
|
|
249
|
+
const row: LogRow = { line };
|
|
250
|
+
store.add(row);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
} catch {
|
|
254
|
+
// Ignore write errors.
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async #open(): Promise<IDBDatabase> {
|
|
259
|
+
if (this.#db) {
|
|
260
|
+
return this.#db;
|
|
261
|
+
}
|
|
262
|
+
if (!this.#dbPromise) {
|
|
263
|
+
this.#dbPromise = openDatabase(this.#dbName, this.#storeName).then((db) => {
|
|
264
|
+
this.#db = db;
|
|
265
|
+
db.onclose = () => {
|
|
266
|
+
if (this.#db === db) {
|
|
267
|
+
this.#db = undefined;
|
|
268
|
+
this.#dbPromise = undefined;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
db.onversionchange = () => {
|
|
272
|
+
db.close();
|
|
273
|
+
};
|
|
274
|
+
return db;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return this.#dbPromise;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
#scheduleEviction(): void {
|
|
281
|
+
if (this.#evictionInterval <= 0) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
this.#evictionTimer = setInterval(() => {
|
|
285
|
+
void this.#maybeEvict();
|
|
286
|
+
}, this.#evictionInterval);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async #maybeEvict(): Promise<void> {
|
|
290
|
+
if (this.#closed) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const locks = (globalThis as any).navigator?.locks as LockManager | undefined;
|
|
294
|
+
if (locks?.request) {
|
|
295
|
+
try {
|
|
296
|
+
await locks.request(EVICTION_LOCK_NAME, { ifAvailable: true }, async (lock) => {
|
|
297
|
+
if (!lock) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
await this.#evict();
|
|
301
|
+
});
|
|
302
|
+
return;
|
|
303
|
+
} catch {
|
|
304
|
+
// Fall through to direct eviction if locks fail.
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
await this.#evict();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async #evict(): Promise<void> {
|
|
311
|
+
let db: IDBDatabase;
|
|
312
|
+
try {
|
|
313
|
+
db = await this.#open();
|
|
314
|
+
} catch {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const count = await runTransaction(db, this.#storeName, 'readonly', (store) => promisifyRequest(store.count()));
|
|
319
|
+
if (count <= this.#maxRecords) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const toDelete = count - this.#maxRecords;
|
|
324
|
+
await runTransaction(db, this.#storeName, 'readwrite', async (store) => {
|
|
325
|
+
const cutoff = await findCutoffKey(store, toDelete);
|
|
326
|
+
if (cutoff !== undefined) {
|
|
327
|
+
store.delete(IDBKeyRange.upperBound(cutoff));
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
#installLifecycleHandlers(): void {
|
|
333
|
+
if (typeof globalThis.addEventListener !== 'function') {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
this.#pageHideHandler = () => {
|
|
337
|
+
void this.flush();
|
|
338
|
+
};
|
|
339
|
+
this.#visibilityHandler = () => {
|
|
340
|
+
if ((globalThis as any).document?.visibilityState === 'hidden') {
|
|
341
|
+
void this.flush();
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
globalThis.addEventListener('pagehide', this.#pageHideHandler);
|
|
345
|
+
globalThis.addEventListener('visibilitychange', this.#visibilityHandler);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
#removeLifecycleHandlers(): void {
|
|
349
|
+
if (typeof globalThis.removeEventListener !== 'function') {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (this.#pageHideHandler) {
|
|
353
|
+
globalThis.removeEventListener('pagehide', this.#pageHideHandler);
|
|
354
|
+
this.#pageHideHandler = undefined;
|
|
355
|
+
}
|
|
356
|
+
if (this.#visibilityHandler) {
|
|
357
|
+
globalThis.removeEventListener('visibilitychange', this.#visibilityHandler);
|
|
358
|
+
this.#visibilityHandler = undefined;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const openDatabase = (name: string, storeName: string): Promise<IDBDatabase> => {
|
|
364
|
+
return new Promise((resolve, reject) => {
|
|
365
|
+
const request = indexedDB.open(name, 1);
|
|
366
|
+
request.onupgradeneeded = () => {
|
|
367
|
+
const db = request.result;
|
|
368
|
+
if (!db.objectStoreNames.contains(storeName)) {
|
|
369
|
+
db.createObjectStore(storeName, { keyPath: 'seq', autoIncrement: true });
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
request.onsuccess = () => resolve(request.result);
|
|
373
|
+
request.onerror = () => reject(request.error);
|
|
374
|
+
request.onblocked = () => reject(new Error('IndexedDB open blocked'));
|
|
375
|
+
});
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const promisifyRequest = <T>(request: IDBRequest<T>): Promise<T> =>
|
|
379
|
+
new Promise((resolve, reject) => {
|
|
380
|
+
request.onsuccess = () => resolve(request.result);
|
|
381
|
+
request.onerror = () => reject(request.error);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const runTransaction = <T>(
|
|
385
|
+
db: IDBDatabase,
|
|
386
|
+
storeName: string,
|
|
387
|
+
mode: IDBTransactionMode,
|
|
388
|
+
body: (store: IDBObjectStore) => T | Promise<T>,
|
|
389
|
+
): Promise<T> => {
|
|
390
|
+
return new Promise<T>((resolve, reject) => {
|
|
391
|
+
let tx: IDBTransaction;
|
|
392
|
+
try {
|
|
393
|
+
tx = db.transaction(storeName, mode);
|
|
394
|
+
} catch (err) {
|
|
395
|
+
reject(err);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
let result: T;
|
|
399
|
+
let bodySettled = false;
|
|
400
|
+
let bodyError: unknown;
|
|
401
|
+
|
|
402
|
+
tx.oncomplete = () => {
|
|
403
|
+
if (bodyError !== undefined) {
|
|
404
|
+
reject(bodyError);
|
|
405
|
+
} else if (bodySettled) {
|
|
406
|
+
resolve(result);
|
|
407
|
+
} else {
|
|
408
|
+
// Body never settled (shouldn't happen in practice).
|
|
409
|
+
reject(new Error('transaction body did not settle'));
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
tx.onerror = () => reject(tx.error);
|
|
413
|
+
tx.onabort = () => reject(tx.error ?? new Error('transaction aborted'));
|
|
414
|
+
|
|
415
|
+
try {
|
|
416
|
+
const store = tx.objectStore(storeName);
|
|
417
|
+
const value = body(store);
|
|
418
|
+
Promise.resolve(value)
|
|
419
|
+
.then((resolved) => {
|
|
420
|
+
result = resolved;
|
|
421
|
+
bodySettled = true;
|
|
422
|
+
})
|
|
423
|
+
.catch((err) => {
|
|
424
|
+
bodyError = err;
|
|
425
|
+
try {
|
|
426
|
+
tx.abort();
|
|
427
|
+
} catch {
|
|
428
|
+
// Already settled.
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
} catch (err) {
|
|
432
|
+
bodyError = err;
|
|
433
|
+
try {
|
|
434
|
+
tx.abort();
|
|
435
|
+
} catch {
|
|
436
|
+
// Ignore.
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const readAllLines = async (db: IDBDatabase, storeName: string): Promise<string[]> => {
|
|
443
|
+
return runTransaction(db, storeName, 'readonly', (store) =>
|
|
444
|
+
promisifyRequest(store.getAll()).then((rows) => (rows as LogRow[]).map((row) => row.line)),
|
|
445
|
+
);
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Walk the store in key order and return the key of the `count`-th entry.
|
|
450
|
+
* Returns `undefined` if the store has fewer than `count` entries.
|
|
451
|
+
*/
|
|
452
|
+
const findCutoffKey = (store: IDBObjectStore, count: number): Promise<IDBValidKey | undefined> => {
|
|
453
|
+
return new Promise((resolve, reject) => {
|
|
454
|
+
if (count <= 0) {
|
|
455
|
+
resolve(undefined);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const request = store.openKeyCursor();
|
|
459
|
+
let advanced = false;
|
|
460
|
+
request.onsuccess = () => {
|
|
461
|
+
const cursor = request.result;
|
|
462
|
+
if (!cursor) {
|
|
463
|
+
resolve(undefined);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
if (!advanced && count > 1) {
|
|
467
|
+
advanced = true;
|
|
468
|
+
cursor.advance(count - 1);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
resolve(cursor.key);
|
|
472
|
+
};
|
|
473
|
+
request.onerror = () => reject(request.error);
|
|
474
|
+
});
|
|
475
|
+
};
|