@dxos/log-store-idb 0.9.1-staging.ee54ba693a → 0.11.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/dist/lib/index.mjs +396 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/types/src/idb-log-store.d.ts +21 -6
- package/dist/types/src/idb-log-store.d.ts.map +1 -1
- package/dist/types/src/trim.d.ts +2 -0
- package/dist/types/src/trim.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -7
- package/src/idb-log-store.browser.test.ts +30 -0
- package/src/idb-log-store.ts +79 -26
- package/src/trim.ts +2 -1
- package/dist/lib/browser/index.mjs +0 -406
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -408
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/log-store-idb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "IndexedDB-backed log store for @dxos/log.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -16,11 +16,7 @@
|
|
|
16
16
|
".": {
|
|
17
17
|
"source": "./src/index.ts",
|
|
18
18
|
"types": "./dist/types/src/index.d.ts",
|
|
19
|
-
"
|
|
20
|
-
"node": {
|
|
21
|
-
"require": "./dist/lib/node/index.cjs",
|
|
22
|
-
"default": "./dist/lib/node-esm/index.mjs"
|
|
23
|
-
}
|
|
19
|
+
"import": "./dist/lib/index.mjs"
|
|
24
20
|
}
|
|
25
21
|
},
|
|
26
22
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -29,7 +25,7 @@
|
|
|
29
25
|
"src"
|
|
30
26
|
],
|
|
31
27
|
"dependencies": {
|
|
32
|
-
"@dxos/log": "0.
|
|
28
|
+
"@dxos/log": "0.11.0"
|
|
33
29
|
},
|
|
34
30
|
"devDependencies": {},
|
|
35
31
|
"publishConfig": {
|
|
@@ -92,6 +92,17 @@ describe('IdbLogStore', () => {
|
|
|
92
92
|
await drop(dbName);
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
+
test('exportBlob matches export for small stores', async ({ expect }) => {
|
|
96
|
+
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
97
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, 'hello'));
|
|
98
|
+
store.processor(fakeConfig, makeEntry(LogLevel.WARN, 'world'));
|
|
99
|
+
await store.flush();
|
|
100
|
+
|
|
101
|
+
const jsonl = await store.export();
|
|
102
|
+
const blobText = await (await store.exportBlob()).text();
|
|
103
|
+
expect(blobText).toBe(jsonl);
|
|
104
|
+
});
|
|
105
|
+
|
|
95
106
|
test('persists log entries and exports them as JSONL', async ({ expect }) => {
|
|
96
107
|
store = new IdbLogStore({ dbName, flushInterval: 10 });
|
|
97
108
|
store.processor(fakeConfig, makeEntry(LogLevel.INFO, 'hello'));
|
|
@@ -259,6 +270,25 @@ describe('IdbLogStore', () => {
|
|
|
259
270
|
expect(messages[11]).toBe('new-11');
|
|
260
271
|
});
|
|
261
272
|
|
|
273
|
+
test('eviction trims down to maxBytes when lines are large', async ({ expect }) => {
|
|
274
|
+
const hugeLine = 'x'.repeat(8_000);
|
|
275
|
+
store = new IdbLogStore({
|
|
276
|
+
dbName,
|
|
277
|
+
flushInterval: 10,
|
|
278
|
+
maxRecords: 10_000,
|
|
279
|
+
maxBytes: 20_000,
|
|
280
|
+
evictionInterval: 0,
|
|
281
|
+
});
|
|
282
|
+
for (let i = 0; i < 5; i++) {
|
|
283
|
+
store.processor(fakeConfig, makeEntry(LogLevel.INFO, `${i}:${hugeLine}`));
|
|
284
|
+
await store.flush();
|
|
285
|
+
}
|
|
286
|
+
await store.evictNow();
|
|
287
|
+
const blob = await store.exportBlob();
|
|
288
|
+
expect(blob.size).toBeLessThanOrEqual(20_000);
|
|
289
|
+
expect(blob.size).toBeGreaterThan(0);
|
|
290
|
+
});
|
|
291
|
+
|
|
262
292
|
test('upgrades a v1 database, discarding old single-line rows', async ({ expect }) => {
|
|
263
293
|
await createV1Database(dbName, ['{"m":"legacy"}']);
|
|
264
294
|
|
package/src/idb-log-store.ts
CHANGED
|
@@ -13,15 +13,17 @@ import {
|
|
|
13
13
|
shouldLog,
|
|
14
14
|
} from '@dxos/log';
|
|
15
15
|
|
|
16
|
-
import { trimJsonlToSize } from './trim';
|
|
16
|
+
import { byteLengthUtf8, trimJsonlToSize } from './trim';
|
|
17
17
|
|
|
18
18
|
const DEFAULT_STORE_NAME = 'logs';
|
|
19
19
|
const DEFAULT_LOG_FILTER = 'debug';
|
|
20
20
|
const DEFAULT_FLUSH_INTERVAL = 250;
|
|
21
21
|
const DEFAULT_FLUSH_BATCH_SIZE = 500;
|
|
22
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
23
|
const DEFAULT_MAX_RECORDS = 150_000;
|
|
24
|
+
const DEFAULT_MAX_BYTES = 50 * 1024 * 1024;
|
|
25
|
+
/** Max UTF-8 bytes for user-initiated log downloads from app UI (Reset dialog, debug settings, devtools hook). */
|
|
26
|
+
export const MANUAL_LOG_EXPORT_MAX_BYTES = 200 * 1024 * 1024;
|
|
25
27
|
const DEFAULT_EVICTION_INTERVAL = 30_000;
|
|
26
28
|
const EVICTION_LOCK_NAME = '@dxos/log-store-idb:evictor';
|
|
27
29
|
// v2 introduced chunked rows with out-of-line array keys (v1 stored one row per line
|
|
@@ -57,11 +59,14 @@ export type IdbLogStoreOptions = {
|
|
|
57
59
|
flushBatchSize?: number;
|
|
58
60
|
/**
|
|
59
61
|
* Soft cap on retained log records (JSONL lines). Eviction drops whole flush chunks,
|
|
60
|
-
* oldest first; the newest chunk is always retained. Default `150_000
|
|
61
|
-
* roughly 50 MB on disk at typical JSONL line sizes (~350 bytes average observed in
|
|
62
|
-
* the Composer app).
|
|
62
|
+
* oldest first; the newest chunk is always retained. Default `150_000`.
|
|
63
63
|
*/
|
|
64
64
|
maxRecords?: number;
|
|
65
|
+
/**
|
|
66
|
+
* Soft cap on retained log bytes (UTF-8). Eviction drops whole flush chunks, oldest
|
|
67
|
+
* first; the newest chunk is always retained. Default `50 MiB`.
|
|
68
|
+
*/
|
|
69
|
+
maxBytes?: number;
|
|
65
70
|
/** Eviction sweep interval in milliseconds. Default `30_000`. */
|
|
66
71
|
evictionInterval?: number;
|
|
67
72
|
/**
|
|
@@ -96,6 +101,7 @@ export class IdbLogStore {
|
|
|
96
101
|
readonly #flushInterval: number;
|
|
97
102
|
readonly #flushBatchSize: number;
|
|
98
103
|
readonly #maxRecords: number;
|
|
104
|
+
readonly #maxBytes: number;
|
|
99
105
|
readonly #evictionInterval: number;
|
|
100
106
|
readonly #tabId: string;
|
|
101
107
|
readonly #filters: LogFilter[];
|
|
@@ -119,6 +125,7 @@ export class IdbLogStore {
|
|
|
119
125
|
this.#flushInterval = options.flushInterval ?? DEFAULT_FLUSH_INTERVAL;
|
|
120
126
|
this.#flushBatchSize = options.flushBatchSize ?? DEFAULT_FLUSH_BATCH_SIZE;
|
|
121
127
|
this.#maxRecords = options.maxRecords ?? DEFAULT_MAX_RECORDS;
|
|
128
|
+
this.#maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
122
129
|
this.#evictionInterval = options.evictionInterval ?? DEFAULT_EVICTION_INTERVAL;
|
|
123
130
|
this.#tabId = options.tabId ?? inferEnvironmentName();
|
|
124
131
|
this.#filters = parseFilter(options.logFilter ?? DEFAULT_LOG_FILTER);
|
|
@@ -183,17 +190,32 @@ export class IdbLogStore {
|
|
|
183
190
|
*
|
|
184
191
|
* If `maxSize` is provided (in bytes), the newest lines are kept and older lines
|
|
185
192
|
* are dropped from the front; lines are never split.
|
|
193
|
+
*
|
|
194
|
+
* For full exports of very large stores, prefer {@link exportBlob} — building one
|
|
195
|
+
* giant string can exceed the engine's maximum string length.
|
|
186
196
|
*/
|
|
187
197
|
async export(options: { maxSize?: number } = {}): Promise<string> {
|
|
198
|
+
const blob = await this.exportBlob(options);
|
|
199
|
+
return blob.text();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Read all retained log lines as an NDJSON blob without materializing one giant string.
|
|
204
|
+
* Prefer this for browser downloads of large log stores.
|
|
205
|
+
*/
|
|
206
|
+
async exportBlob(options: { maxSize?: number } = {}): Promise<Blob> {
|
|
188
207
|
await this.flush();
|
|
208
|
+
await this.#maybeEvict();
|
|
189
209
|
const db = await this.#open();
|
|
190
210
|
const chunks = await readAllChunks(db, this.#storeName);
|
|
191
|
-
|
|
211
|
+
const maxSize = options.maxSize ?? this.#maxBytes;
|
|
212
|
+
if (maxSize >= 0) {
|
|
192
213
|
// Trim on individual lines so the size cap never under-fills by a whole chunk.
|
|
193
214
|
const lines = chunks.flatMap((chunk) => chunk.split('\n'));
|
|
194
|
-
|
|
215
|
+
const trimmed = trimJsonlToSize(lines, maxSize);
|
|
216
|
+
return new Blob([trimmed], { type: 'application/x-ndjson' });
|
|
195
217
|
}
|
|
196
|
-
return chunks
|
|
218
|
+
return buildNdjsonBlob(chunks);
|
|
197
219
|
}
|
|
198
220
|
|
|
199
221
|
/**
|
|
@@ -212,9 +234,9 @@ export class IdbLogStore {
|
|
|
212
234
|
}
|
|
213
235
|
|
|
214
236
|
/**
|
|
215
|
-
* Run an eviction sweep now: trim the store down to `maxRecords` if it
|
|
216
|
-
* Eviction runs automatically on a timer; this method is exposed for
|
|
217
|
-
* callers that want to force a sweep at a particular moment.
|
|
237
|
+
* Run an eviction sweep now: trim the store down to `maxRecords` / `maxBytes` if it
|
|
238
|
+
* has grown past. Eviction runs automatically on a timer; this method is exposed for
|
|
239
|
+
* tests and for callers that want to force a sweep at a particular moment.
|
|
218
240
|
*/
|
|
219
241
|
async evictNow(): Promise<void> {
|
|
220
242
|
await this.#maybeEvict();
|
|
@@ -267,6 +289,7 @@ export class IdbLogStore {
|
|
|
267
289
|
await runTransaction(db, this.#storeName, 'readwrite', (store) => {
|
|
268
290
|
store.add(chunk, key);
|
|
269
291
|
});
|
|
292
|
+
void this.#maybeEvict();
|
|
270
293
|
} catch {
|
|
271
294
|
// Ignore write errors.
|
|
272
295
|
}
|
|
@@ -333,11 +356,17 @@ export class IdbLogStore {
|
|
|
333
356
|
}
|
|
334
357
|
|
|
335
358
|
await runTransaction(db, this.#storeName, 'readwrite', async (store) => {
|
|
336
|
-
// Keys carry per-chunk line counts, so the retention budget is computed from
|
|
337
|
-
// getAllKeys() alone — no count() pre-pass and no chunk values are read.
|
|
338
|
-
// Cast: IDB types keys as IDBValidKey[]; this store only ever receives ChunkKey keys.
|
|
339
359
|
const keys = (await promisifyRequest(store.getAllKeys())) as ChunkKey[];
|
|
340
|
-
|
|
360
|
+
if (keys.length === 0) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const rows = (await promisifyRequest(store.getAll())) as LogChunk[];
|
|
364
|
+
const chunks = keys.map((key, index) => ({
|
|
365
|
+
key,
|
|
366
|
+
lineCount: key[3],
|
|
367
|
+
byteLength: byteLengthUtf8(rows[index]!.lines),
|
|
368
|
+
}));
|
|
369
|
+
const cutoff = findEvictionCutoff(chunks, this.#maxRecords, this.#maxBytes);
|
|
341
370
|
if (cutoff !== undefined) {
|
|
342
371
|
store.delete(IDBKeyRange.upperBound(cutoff));
|
|
343
372
|
}
|
|
@@ -464,19 +493,43 @@ const readAllChunks = async (db: IDBDatabase, storeName: string): Promise<string
|
|
|
464
493
|
);
|
|
465
494
|
};
|
|
466
495
|
|
|
496
|
+
/** Assemble chunk payloads into one NDJSON blob without `Array.join` (avoids max-string-length errors). */
|
|
497
|
+
const buildNdjsonBlob = (chunks: readonly string[]): Blob => {
|
|
498
|
+
if (chunks.length === 0) {
|
|
499
|
+
return new Blob([], { type: 'application/x-ndjson' });
|
|
500
|
+
}
|
|
501
|
+
const parts: BlobPart[] = [chunks[0]!];
|
|
502
|
+
for (let index = 1; index < chunks.length; index++) {
|
|
503
|
+
parts.push('\n', chunks[index]!);
|
|
504
|
+
}
|
|
505
|
+
return new Blob(parts, { type: 'application/x-ndjson' });
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
type ChunkRetentionStats = {
|
|
509
|
+
key: ChunkKey;
|
|
510
|
+
lineCount: number;
|
|
511
|
+
byteLength: number;
|
|
512
|
+
};
|
|
513
|
+
|
|
467
514
|
/**
|
|
468
|
-
* Given all
|
|
469
|
-
* must be evicted — delete it and everything older — so
|
|
470
|
-
*
|
|
471
|
-
* budget. Returns `undefined` when everything fits.
|
|
515
|
+
* Given all chunks in ascending (key) order, return the key of the newest chunk that
|
|
516
|
+
* must be evicted — delete it and everything older — so retained totals stay within
|
|
517
|
+
* `maxRecords` and `maxBytes`. The newest chunk is always retained, even if it alone
|
|
518
|
+
* exceeds the budget. Returns `undefined` when everything fits.
|
|
472
519
|
*/
|
|
473
|
-
const findEvictionCutoff = (
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
520
|
+
const findEvictionCutoff = (
|
|
521
|
+
chunks: readonly ChunkRetentionStats[],
|
|
522
|
+
maxRecords: number,
|
|
523
|
+
maxBytes: number,
|
|
524
|
+
): ChunkKey | undefined => {
|
|
525
|
+
let retainedLines = 0;
|
|
526
|
+
let retainedBytes = 0;
|
|
527
|
+
for (let index = chunks.length - 1; index >= 0; index--) {
|
|
528
|
+
const chunk = chunks[index]!;
|
|
529
|
+
retainedLines += chunk.lineCount;
|
|
530
|
+
retainedBytes += chunk.byteLength;
|
|
531
|
+
if ((retainedLines > maxRecords || retainedBytes > maxBytes) && index < chunks.length - 1) {
|
|
532
|
+
return chunk.key;
|
|
480
533
|
}
|
|
481
534
|
}
|
|
482
535
|
return undefined;
|
package/src/trim.ts
CHANGED
|
@@ -37,7 +37,8 @@ export const trimJsonlToSize = (lines: readonly string[], maxSize: number): stri
|
|
|
37
37
|
|
|
38
38
|
const utf8Encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : undefined;
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
/** UTF-8 byte length of a string (used by eviction and trim). */
|
|
41
|
+
export const byteLengthUtf8 = (value: string): number => {
|
|
41
42
|
if (utf8Encoder) {
|
|
42
43
|
return utf8Encoder.encode(value).length;
|
|
43
44
|
}
|
|
@@ -1,406 +0,0 @@
|
|
|
1
|
-
// src/idb-log-store.ts
|
|
2
|
-
import { inferEnvironmentName, parseFilter, serializeToJsonl, shouldLog } from "@dxos/log";
|
|
3
|
-
|
|
4
|
-
// src/trim.ts
|
|
5
|
-
var trimJsonlToSize = (lines, maxSize) => {
|
|
6
|
-
if (lines.length === 0 || maxSize <= 0) {
|
|
7
|
-
return "";
|
|
8
|
-
}
|
|
9
|
-
const sizes = lines.map((line) => byteLengthUtf8(line));
|
|
10
|
-
let total = 0;
|
|
11
|
-
let firstIncludedIndex = lines.length;
|
|
12
|
-
for (let index = lines.length - 1; index >= 0; index--) {
|
|
13
|
-
const lineBytes = sizes[index];
|
|
14
|
-
const sepBytes = total === 0 ? 0 : 1;
|
|
15
|
-
if (total + lineBytes + sepBytes > maxSize) {
|
|
16
|
-
break;
|
|
17
|
-
}
|
|
18
|
-
total += lineBytes + sepBytes;
|
|
19
|
-
firstIncludedIndex = index;
|
|
20
|
-
}
|
|
21
|
-
if (firstIncludedIndex >= lines.length) {
|
|
22
|
-
return "";
|
|
23
|
-
}
|
|
24
|
-
return lines.slice(firstIncludedIndex).join("\n");
|
|
25
|
-
};
|
|
26
|
-
var utf8Encoder = typeof TextEncoder !== "undefined" ? new TextEncoder() : void 0;
|
|
27
|
-
var byteLengthUtf8 = (value) => {
|
|
28
|
-
if (utf8Encoder) {
|
|
29
|
-
return utf8Encoder.encode(value).length;
|
|
30
|
-
}
|
|
31
|
-
return value.length;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// src/idb-log-store.ts
|
|
35
|
-
var DEFAULT_STORE_NAME = "logs";
|
|
36
|
-
var DEFAULT_LOG_FILTER = "debug";
|
|
37
|
-
var DEFAULT_FLUSH_INTERVAL = 250;
|
|
38
|
-
var DEFAULT_FLUSH_BATCH_SIZE = 500;
|
|
39
|
-
var DEFAULT_MAX_RECORDS = 15e4;
|
|
40
|
-
var DEFAULT_EVICTION_INTERVAL = 3e4;
|
|
41
|
-
var EVICTION_LOCK_NAME = "@dxos/log-store-idb:evictor";
|
|
42
|
-
var DB_VERSION = 2;
|
|
43
|
-
var IdbLogStore = class {
|
|
44
|
-
#dbName;
|
|
45
|
-
#storeName;
|
|
46
|
-
#flushInterval;
|
|
47
|
-
#flushBatchSize;
|
|
48
|
-
#maxRecords;
|
|
49
|
-
#evictionInterval;
|
|
50
|
-
#tabId;
|
|
51
|
-
#filters;
|
|
52
|
-
/** Distinguishes chunk keys written by concurrent contexts against the same database. */
|
|
53
|
-
#writerId = Math.random().toString(36).slice(2);
|
|
54
|
-
#queue = [];
|
|
55
|
-
#writerSeq = 0;
|
|
56
|
-
#flushTask;
|
|
57
|
-
#evictionTimer;
|
|
58
|
-
#pendingFlush;
|
|
59
|
-
#db;
|
|
60
|
-
#dbPromise;
|
|
61
|
-
#closed = false;
|
|
62
|
-
#pageHideHandler;
|
|
63
|
-
#visibilityHandler;
|
|
64
|
-
constructor(options) {
|
|
65
|
-
this.#dbName = options.dbName;
|
|
66
|
-
this.#storeName = options.storeName ?? DEFAULT_STORE_NAME;
|
|
67
|
-
this.#flushInterval = options.flushInterval ?? DEFAULT_FLUSH_INTERVAL;
|
|
68
|
-
this.#flushBatchSize = options.flushBatchSize ?? DEFAULT_FLUSH_BATCH_SIZE;
|
|
69
|
-
this.#maxRecords = options.maxRecords ?? DEFAULT_MAX_RECORDS;
|
|
70
|
-
this.#evictionInterval = options.evictionInterval ?? DEFAULT_EVICTION_INTERVAL;
|
|
71
|
-
this.#tabId = options.tabId ?? inferEnvironmentName();
|
|
72
|
-
this.#filters = parseFilter(options.logFilter ?? DEFAULT_LOG_FILTER);
|
|
73
|
-
this.#installLifecycleHandlers();
|
|
74
|
-
this.#scheduleEviction();
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Log processor — register via `log.addProcessor(store.processor)`.
|
|
78
|
-
*/
|
|
79
|
-
processor = (_config, entry) => {
|
|
80
|
-
if (this.#closed) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
if (!shouldLog(entry, this.#filters)) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const line = serializeToJsonl(entry, {
|
|
87
|
-
env: this.#tabId
|
|
88
|
-
});
|
|
89
|
-
if (line === void 0) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
this.#queue.push(line);
|
|
93
|
-
if (this.#queue.length >= this.#flushBatchSize) {
|
|
94
|
-
void this.flush();
|
|
95
|
-
} else if (this.#flushTask === void 0) {
|
|
96
|
-
this.#flushTask = scheduleBackgroundTask(() => {
|
|
97
|
-
this.#flushTask = void 0;
|
|
98
|
-
void this.flush();
|
|
99
|
-
}, this.#flushInterval);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Force a flush now and resolve once the IDB transaction commits.
|
|
104
|
-
* Concurrent calls share a single in-flight flush.
|
|
105
|
-
*/
|
|
106
|
-
async flush() {
|
|
107
|
-
if (this.#flushTask !== void 0) {
|
|
108
|
-
this.#flushTask.cancel();
|
|
109
|
-
this.#flushTask = void 0;
|
|
110
|
-
}
|
|
111
|
-
if (this.#pendingFlush) {
|
|
112
|
-
return this.#pendingFlush;
|
|
113
|
-
}
|
|
114
|
-
if (this.#queue.length === 0) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
const batch = this.#queue;
|
|
118
|
-
this.#queue = [];
|
|
119
|
-
this.#pendingFlush = this.#writeBatch(batch).finally(() => {
|
|
120
|
-
this.#pendingFlush = void 0;
|
|
121
|
-
});
|
|
122
|
-
return this.#pendingFlush;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Read all retained log lines as a JSONL string.
|
|
126
|
-
*
|
|
127
|
-
* If `maxSize` is provided (in bytes), the newest lines are kept and older lines
|
|
128
|
-
* are dropped from the front; lines are never split.
|
|
129
|
-
*/
|
|
130
|
-
async export(options = {}) {
|
|
131
|
-
await this.flush();
|
|
132
|
-
const db = await this.#open();
|
|
133
|
-
const chunks = await readAllChunks(db, this.#storeName);
|
|
134
|
-
if (options.maxSize !== void 0 && options.maxSize >= 0) {
|
|
135
|
-
const lines = chunks.flatMap((chunk) => chunk.split("\n"));
|
|
136
|
-
return trimJsonlToSize(lines, options.maxSize);
|
|
137
|
-
}
|
|
138
|
-
return chunks.join("\n");
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Discard all stored log records.
|
|
142
|
-
*/
|
|
143
|
-
async clear() {
|
|
144
|
-
this.#queue = [];
|
|
145
|
-
if (this.#flushTask !== void 0) {
|
|
146
|
-
this.#flushTask.cancel();
|
|
147
|
-
this.#flushTask = void 0;
|
|
148
|
-
}
|
|
149
|
-
const db = await this.#open();
|
|
150
|
-
await runTransaction(db, this.#storeName, "readwrite", (store) => {
|
|
151
|
-
store.clear();
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Run an eviction sweep now: trim the store down to `maxRecords` if it has grown past.
|
|
156
|
-
* Eviction runs automatically on a timer; this method is exposed for tests and for
|
|
157
|
-
* callers that want to force a sweep at a particular moment.
|
|
158
|
-
*/
|
|
159
|
-
async evictNow() {
|
|
160
|
-
await this.#maybeEvict();
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Stop timers, flush remaining entries, and close the database connection.
|
|
164
|
-
*/
|
|
165
|
-
async close() {
|
|
166
|
-
if (this.#closed) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
this.#closed = true;
|
|
170
|
-
if (this.#evictionTimer !== void 0) {
|
|
171
|
-
clearInterval(this.#evictionTimer);
|
|
172
|
-
this.#evictionTimer = void 0;
|
|
173
|
-
}
|
|
174
|
-
this.#removeLifecycleHandlers();
|
|
175
|
-
try {
|
|
176
|
-
await this.flush();
|
|
177
|
-
} catch {
|
|
178
|
-
}
|
|
179
|
-
if (this.#db) {
|
|
180
|
-
this.#db.close();
|
|
181
|
-
this.#db = void 0;
|
|
182
|
-
this.#dbPromise = void 0;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
async #writeBatch(batch) {
|
|
186
|
-
if (batch.length === 0) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
let db;
|
|
190
|
-
try {
|
|
191
|
-
db = await this.#open();
|
|
192
|
-
} catch {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
try {
|
|
196
|
-
const chunk = {
|
|
197
|
-
lines: batch.join("\n")
|
|
198
|
-
};
|
|
199
|
-
const key = [
|
|
200
|
-
Date.now(),
|
|
201
|
-
this.#writerId,
|
|
202
|
-
this.#writerSeq++,
|
|
203
|
-
batch.length
|
|
204
|
-
];
|
|
205
|
-
await runTransaction(db, this.#storeName, "readwrite", (store) => {
|
|
206
|
-
store.add(chunk, key);
|
|
207
|
-
});
|
|
208
|
-
} catch {
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
async #open() {
|
|
212
|
-
if (this.#db) {
|
|
213
|
-
return this.#db;
|
|
214
|
-
}
|
|
215
|
-
if (!this.#dbPromise) {
|
|
216
|
-
this.#dbPromise = openDatabase(this.#dbName, this.#storeName).then((db) => {
|
|
217
|
-
this.#db = db;
|
|
218
|
-
db.onclose = () => {
|
|
219
|
-
if (this.#db === db) {
|
|
220
|
-
this.#db = void 0;
|
|
221
|
-
this.#dbPromise = void 0;
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
db.onversionchange = () => {
|
|
225
|
-
db.close();
|
|
226
|
-
};
|
|
227
|
-
return db;
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
return this.#dbPromise;
|
|
231
|
-
}
|
|
232
|
-
#scheduleEviction() {
|
|
233
|
-
if (this.#evictionInterval <= 0) {
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
this.#evictionTimer = setInterval(() => {
|
|
237
|
-
void this.#maybeEvict();
|
|
238
|
-
}, this.#evictionInterval);
|
|
239
|
-
}
|
|
240
|
-
async #maybeEvict() {
|
|
241
|
-
if (this.#closed) {
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
const locks = globalThis.navigator?.locks;
|
|
245
|
-
if (locks?.request) {
|
|
246
|
-
try {
|
|
247
|
-
await locks.request(EVICTION_LOCK_NAME, {
|
|
248
|
-
ifAvailable: true
|
|
249
|
-
}, async (lock) => {
|
|
250
|
-
if (!lock) {
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
await this.#evict();
|
|
254
|
-
});
|
|
255
|
-
return;
|
|
256
|
-
} catch {
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
await this.#evict();
|
|
260
|
-
}
|
|
261
|
-
async #evict() {
|
|
262
|
-
let db;
|
|
263
|
-
try {
|
|
264
|
-
db = await this.#open();
|
|
265
|
-
} catch {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
await runTransaction(db, this.#storeName, "readwrite", async (store) => {
|
|
269
|
-
const keys = await promisifyRequest(store.getAllKeys());
|
|
270
|
-
const cutoff = findEvictionCutoff(keys, this.#maxRecords);
|
|
271
|
-
if (cutoff !== void 0) {
|
|
272
|
-
store.delete(IDBKeyRange.upperBound(cutoff));
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
#installLifecycleHandlers() {
|
|
277
|
-
if (typeof globalThis.addEventListener !== "function") {
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
this.#pageHideHandler = () => {
|
|
281
|
-
void this.flush();
|
|
282
|
-
};
|
|
283
|
-
this.#visibilityHandler = () => {
|
|
284
|
-
if (globalThis.document?.visibilityState === "hidden") {
|
|
285
|
-
void this.flush();
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
globalThis.addEventListener("pagehide", this.#pageHideHandler);
|
|
289
|
-
globalThis.addEventListener("visibilitychange", this.#visibilityHandler);
|
|
290
|
-
}
|
|
291
|
-
#removeLifecycleHandlers() {
|
|
292
|
-
if (typeof globalThis.removeEventListener !== "function") {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
if (this.#pageHideHandler) {
|
|
296
|
-
globalThis.removeEventListener("pagehide", this.#pageHideHandler);
|
|
297
|
-
this.#pageHideHandler = void 0;
|
|
298
|
-
}
|
|
299
|
-
if (this.#visibilityHandler) {
|
|
300
|
-
globalThis.removeEventListener("visibilitychange", this.#visibilityHandler);
|
|
301
|
-
this.#visibilityHandler = void 0;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
var openDatabase = (name, storeName) => {
|
|
306
|
-
return new Promise((resolve, reject) => {
|
|
307
|
-
const request = indexedDB.open(name, DB_VERSION);
|
|
308
|
-
request.onupgradeneeded = () => {
|
|
309
|
-
const db = request.result;
|
|
310
|
-
if (db.objectStoreNames.contains(storeName)) {
|
|
311
|
-
db.deleteObjectStore(storeName);
|
|
312
|
-
}
|
|
313
|
-
db.createObjectStore(storeName);
|
|
314
|
-
};
|
|
315
|
-
request.onsuccess = () => resolve(request.result);
|
|
316
|
-
request.onerror = () => reject(request.error);
|
|
317
|
-
request.onblocked = () => reject(new Error("IndexedDB open blocked"));
|
|
318
|
-
});
|
|
319
|
-
};
|
|
320
|
-
var promisifyRequest = (request) => new Promise((resolve, reject) => {
|
|
321
|
-
request.onsuccess = () => resolve(request.result);
|
|
322
|
-
request.onerror = () => reject(request.error);
|
|
323
|
-
});
|
|
324
|
-
var runTransaction = (db, storeName, mode, body) => {
|
|
325
|
-
return new Promise((resolve, reject) => {
|
|
326
|
-
let tx;
|
|
327
|
-
try {
|
|
328
|
-
tx = db.transaction(storeName, mode);
|
|
329
|
-
} catch (err) {
|
|
330
|
-
reject(err);
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
let result;
|
|
334
|
-
let bodySettled = false;
|
|
335
|
-
let bodyError;
|
|
336
|
-
tx.oncomplete = () => {
|
|
337
|
-
if (bodyError !== void 0) {
|
|
338
|
-
reject(bodyError);
|
|
339
|
-
} else if (bodySettled) {
|
|
340
|
-
resolve(result);
|
|
341
|
-
} else {
|
|
342
|
-
reject(new Error("transaction body did not settle"));
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
tx.onerror = () => reject(tx.error);
|
|
346
|
-
tx.onabort = () => reject(tx.error ?? new Error("transaction aborted"));
|
|
347
|
-
try {
|
|
348
|
-
const store = tx.objectStore(storeName);
|
|
349
|
-
const value = body(store);
|
|
350
|
-
Promise.resolve(value).then((resolved) => {
|
|
351
|
-
result = resolved;
|
|
352
|
-
bodySettled = true;
|
|
353
|
-
}).catch((err) => {
|
|
354
|
-
bodyError = err;
|
|
355
|
-
try {
|
|
356
|
-
tx.abort();
|
|
357
|
-
} catch {
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
} catch (err) {
|
|
361
|
-
bodyError = err;
|
|
362
|
-
try {
|
|
363
|
-
tx.abort();
|
|
364
|
-
} catch {
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
};
|
|
369
|
-
var readAllChunks = async (db, storeName) => {
|
|
370
|
-
return runTransaction(db, storeName, "readonly", (store) => promisifyRequest(store.getAll()).then((rows) => rows.map((row) => row.lines)));
|
|
371
|
-
};
|
|
372
|
-
var findEvictionCutoff = (keys, maxRecords) => {
|
|
373
|
-
let retained = 0;
|
|
374
|
-
for (let index = keys.length - 1; index >= 0; index--) {
|
|
375
|
-
const [, , , lineCount] = keys[index];
|
|
376
|
-
retained += lineCount;
|
|
377
|
-
if (retained > maxRecords && index < keys.length - 1) {
|
|
378
|
-
return keys[index];
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
return void 0;
|
|
382
|
-
};
|
|
383
|
-
var scheduleBackgroundTask = (callback, delay) => {
|
|
384
|
-
const scheduler = globalThis.scheduler;
|
|
385
|
-
if (typeof scheduler?.postTask === "function") {
|
|
386
|
-
const controller = new AbortController();
|
|
387
|
-
scheduler.postTask(callback, {
|
|
388
|
-
priority: "background",
|
|
389
|
-
delay,
|
|
390
|
-
signal: controller.signal
|
|
391
|
-
}).catch(() => {
|
|
392
|
-
});
|
|
393
|
-
return {
|
|
394
|
-
cancel: () => controller.abort()
|
|
395
|
-
};
|
|
396
|
-
}
|
|
397
|
-
const timer = setTimeout(callback, delay);
|
|
398
|
-
return {
|
|
399
|
-
cancel: () => clearTimeout(timer)
|
|
400
|
-
};
|
|
401
|
-
};
|
|
402
|
-
export {
|
|
403
|
-
IdbLogStore,
|
|
404
|
-
trimJsonlToSize
|
|
405
|
-
};
|
|
406
|
-
//# sourceMappingURL=index.mjs.map
|