@dxos/log-store-idb 0.10.0 → 0.11.1
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
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { inferEnvironmentName, parseFilter, serializeToJsonl, shouldLog } from "@dxos/log";
|
|
2
|
+
//#region src/trim.ts
|
|
3
|
+
/**
|
|
4
|
+
* Trim a list of pre-encoded JSONL lines so the joined output (with `\n` separators)
|
|
5
|
+
* fits within `maxSize` bytes (UTF-8). The newest lines (end of array) are preferred —
|
|
6
|
+
* lines are dropped from the start. Never cuts inside a line.
|
|
7
|
+
*
|
|
8
|
+
* Returns the trimmed JSONL string (no trailing newline).
|
|
9
|
+
*/
|
|
10
|
+
var trimJsonlToSize = (lines, maxSize) => {
|
|
11
|
+
if (lines.length === 0 || maxSize <= 0) return "";
|
|
12
|
+
const sizes = lines.map((line) => byteLengthUtf8(line));
|
|
13
|
+
let total = 0;
|
|
14
|
+
let firstIncludedIndex = lines.length;
|
|
15
|
+
for (let index = lines.length - 1; index >= 0; index--) {
|
|
16
|
+
const lineBytes = sizes[index];
|
|
17
|
+
const sepBytes = total === 0 ? 0 : 1;
|
|
18
|
+
if (total + lineBytes + sepBytes > maxSize) break;
|
|
19
|
+
total += lineBytes + sepBytes;
|
|
20
|
+
firstIncludedIndex = index;
|
|
21
|
+
}
|
|
22
|
+
if (firstIncludedIndex >= lines.length) return "";
|
|
23
|
+
return lines.slice(firstIncludedIndex).join("\n");
|
|
24
|
+
};
|
|
25
|
+
var utf8Encoder = typeof TextEncoder !== "undefined" ? new TextEncoder() : void 0;
|
|
26
|
+
/** UTF-8 byte length of a string (used by eviction and trim). */
|
|
27
|
+
var byteLengthUtf8 = (value) => {
|
|
28
|
+
if (utf8Encoder) return utf8Encoder.encode(value).length;
|
|
29
|
+
return value.length;
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/idb-log-store.ts
|
|
33
|
+
var DEFAULT_STORE_NAME = "logs";
|
|
34
|
+
var DEFAULT_LOG_FILTER = "debug";
|
|
35
|
+
var DEFAULT_FLUSH_INTERVAL = 250;
|
|
36
|
+
var DEFAULT_FLUSH_BATCH_SIZE = 500;
|
|
37
|
+
var DEFAULT_MAX_RECORDS = 15e4;
|
|
38
|
+
var DEFAULT_MAX_BYTES = 50 * 1024 * 1024;
|
|
39
|
+
/** Max UTF-8 bytes for user-initiated log downloads from app UI (Reset dialog, debug settings, devtools hook). */
|
|
40
|
+
var MANUAL_LOG_EXPORT_MAX_BYTES = 200 * 1024 * 1024;
|
|
41
|
+
var DEFAULT_EVICTION_INTERVAL = 3e4;
|
|
42
|
+
var EVICTION_LOCK_NAME = "@dxos/log-store-idb:evictor";
|
|
43
|
+
var DB_VERSION = 2;
|
|
44
|
+
/**
|
|
45
|
+
* IndexedDB-backed log store.
|
|
46
|
+
*
|
|
47
|
+
* Logs are batched in memory and each flush is written as a single chunk record of
|
|
48
|
+
* pre-encoded JSONL. Multiple browsing contexts can write to the same database
|
|
49
|
+
* concurrently — IDB serializes transactions, so no extra coordination is needed for
|
|
50
|
+
* writes. Eviction is deduplicated across tabs via the Web Locks API (`ifAvailable`).
|
|
51
|
+
*
|
|
52
|
+
* Flushes are triggered by:
|
|
53
|
+
* - a deferred task after `flushInterval` (background priority where the Scheduler API
|
|
54
|
+
* is available, so flushes don't compete with input handling and rendering),
|
|
55
|
+
* - the queue exceeding `flushBatchSize`,
|
|
56
|
+
* - `visibilitychange` to `hidden` and `pagehide` events.
|
|
57
|
+
*/
|
|
58
|
+
var IdbLogStore = class {
|
|
59
|
+
#dbName;
|
|
60
|
+
#storeName;
|
|
61
|
+
#flushInterval;
|
|
62
|
+
#flushBatchSize;
|
|
63
|
+
#maxRecords;
|
|
64
|
+
#maxBytes;
|
|
65
|
+
#evictionInterval;
|
|
66
|
+
#tabId;
|
|
67
|
+
#filters;
|
|
68
|
+
/** Distinguishes chunk keys written by concurrent contexts against the same database. */
|
|
69
|
+
#writerId = Math.random().toString(36).slice(2);
|
|
70
|
+
#queue = [];
|
|
71
|
+
#writerSeq = 0;
|
|
72
|
+
#flushTask;
|
|
73
|
+
#evictionTimer;
|
|
74
|
+
#pendingFlush;
|
|
75
|
+
#db;
|
|
76
|
+
#dbPromise;
|
|
77
|
+
#closed = false;
|
|
78
|
+
#pageHideHandler;
|
|
79
|
+
#visibilityHandler;
|
|
80
|
+
constructor(options) {
|
|
81
|
+
this.#dbName = options.dbName;
|
|
82
|
+
this.#storeName = options.storeName ?? DEFAULT_STORE_NAME;
|
|
83
|
+
this.#flushInterval = options.flushInterval ?? DEFAULT_FLUSH_INTERVAL;
|
|
84
|
+
this.#flushBatchSize = options.flushBatchSize ?? DEFAULT_FLUSH_BATCH_SIZE;
|
|
85
|
+
this.#maxRecords = options.maxRecords ?? DEFAULT_MAX_RECORDS;
|
|
86
|
+
this.#maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
87
|
+
this.#evictionInterval = options.evictionInterval ?? DEFAULT_EVICTION_INTERVAL;
|
|
88
|
+
this.#tabId = options.tabId ?? inferEnvironmentName();
|
|
89
|
+
this.#filters = parseFilter(options.logFilter ?? DEFAULT_LOG_FILTER);
|
|
90
|
+
this.#installLifecycleHandlers();
|
|
91
|
+
this.#scheduleEviction();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Log processor — register via `log.addProcessor(store.processor)`.
|
|
95
|
+
*/
|
|
96
|
+
processor = (_config, entry) => {
|
|
97
|
+
if (this.#closed) return;
|
|
98
|
+
if (!shouldLog(entry, this.#filters)) return;
|
|
99
|
+
const line = serializeToJsonl(entry, { env: this.#tabId });
|
|
100
|
+
if (line === void 0) return;
|
|
101
|
+
this.#queue.push(line);
|
|
102
|
+
if (this.#queue.length >= this.#flushBatchSize) this.flush();
|
|
103
|
+
else if (this.#flushTask === void 0) this.#flushTask = scheduleBackgroundTask(() => {
|
|
104
|
+
this.#flushTask = void 0;
|
|
105
|
+
this.flush();
|
|
106
|
+
}, this.#flushInterval);
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Force a flush now and resolve once the IDB transaction commits.
|
|
110
|
+
* Concurrent calls share a single in-flight flush.
|
|
111
|
+
*/
|
|
112
|
+
async flush() {
|
|
113
|
+
if (this.#flushTask !== void 0) {
|
|
114
|
+
this.#flushTask.cancel();
|
|
115
|
+
this.#flushTask = void 0;
|
|
116
|
+
}
|
|
117
|
+
if (this.#pendingFlush) return this.#pendingFlush;
|
|
118
|
+
if (this.#queue.length === 0) return;
|
|
119
|
+
const batch = this.#queue;
|
|
120
|
+
this.#queue = [];
|
|
121
|
+
this.#pendingFlush = this.#writeBatch(batch).finally(() => {
|
|
122
|
+
this.#pendingFlush = void 0;
|
|
123
|
+
});
|
|
124
|
+
return this.#pendingFlush;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Read all retained log lines as a JSONL string.
|
|
128
|
+
*
|
|
129
|
+
* If `maxSize` is provided (in bytes), the newest lines are kept and older lines
|
|
130
|
+
* are dropped from the front; lines are never split.
|
|
131
|
+
*
|
|
132
|
+
* For full exports of very large stores, prefer {@link exportBlob} — building one
|
|
133
|
+
* giant string can exceed the engine's maximum string length.
|
|
134
|
+
*/
|
|
135
|
+
async export(options = {}) {
|
|
136
|
+
return (await this.exportBlob(options)).text();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Read all retained log lines as an NDJSON blob without materializing one giant string.
|
|
140
|
+
* Prefer this for browser downloads of large log stores.
|
|
141
|
+
*/
|
|
142
|
+
async exportBlob(options = {}) {
|
|
143
|
+
await this.flush();
|
|
144
|
+
await this.#maybeEvict();
|
|
145
|
+
const chunks = await readAllChunks(await this.#open(), this.#storeName);
|
|
146
|
+
const maxSize = options.maxSize ?? this.#maxBytes;
|
|
147
|
+
if (maxSize >= 0) {
|
|
148
|
+
const trimmed = trimJsonlToSize(chunks.flatMap((chunk) => chunk.split("\n")), maxSize);
|
|
149
|
+
return new Blob([trimmed], { type: "application/x-ndjson" });
|
|
150
|
+
}
|
|
151
|
+
return buildNdjsonBlob(chunks);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Discard all stored log records.
|
|
155
|
+
*/
|
|
156
|
+
async clear() {
|
|
157
|
+
this.#queue = [];
|
|
158
|
+
if (this.#flushTask !== void 0) {
|
|
159
|
+
this.#flushTask.cancel();
|
|
160
|
+
this.#flushTask = void 0;
|
|
161
|
+
}
|
|
162
|
+
await runTransaction(await this.#open(), this.#storeName, "readwrite", (store) => {
|
|
163
|
+
store.clear();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Run an eviction sweep now: trim the store down to `maxRecords` / `maxBytes` if it
|
|
168
|
+
* has grown past. Eviction runs automatically on a timer; this method is exposed for
|
|
169
|
+
* tests and for callers that want to force a sweep at a particular moment.
|
|
170
|
+
*/
|
|
171
|
+
async evictNow() {
|
|
172
|
+
await this.#maybeEvict();
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Stop timers, flush remaining entries, and close the database connection.
|
|
176
|
+
*/
|
|
177
|
+
async close() {
|
|
178
|
+
if (this.#closed) return;
|
|
179
|
+
this.#closed = true;
|
|
180
|
+
if (this.#evictionTimer !== void 0) {
|
|
181
|
+
clearInterval(this.#evictionTimer);
|
|
182
|
+
this.#evictionTimer = void 0;
|
|
183
|
+
}
|
|
184
|
+
this.#removeLifecycleHandlers();
|
|
185
|
+
try {
|
|
186
|
+
await this.flush();
|
|
187
|
+
} catch {}
|
|
188
|
+
if (this.#db) {
|
|
189
|
+
this.#db.close();
|
|
190
|
+
this.#db = void 0;
|
|
191
|
+
this.#dbPromise = void 0;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async #writeBatch(batch) {
|
|
195
|
+
if (batch.length === 0) return;
|
|
196
|
+
let db;
|
|
197
|
+
try {
|
|
198
|
+
db = await this.#open();
|
|
199
|
+
} catch {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
const chunk = { lines: batch.join("\n") };
|
|
204
|
+
const key = [
|
|
205
|
+
Date.now(),
|
|
206
|
+
this.#writerId,
|
|
207
|
+
this.#writerSeq++,
|
|
208
|
+
batch.length
|
|
209
|
+
];
|
|
210
|
+
await runTransaction(db, this.#storeName, "readwrite", (store) => {
|
|
211
|
+
store.add(chunk, key);
|
|
212
|
+
});
|
|
213
|
+
this.#maybeEvict();
|
|
214
|
+
} catch {}
|
|
215
|
+
}
|
|
216
|
+
async #open() {
|
|
217
|
+
if (this.#db) return this.#db;
|
|
218
|
+
if (!this.#dbPromise) this.#dbPromise = openDatabase(this.#dbName, this.#storeName).then((db) => {
|
|
219
|
+
this.#db = db;
|
|
220
|
+
db.onclose = () => {
|
|
221
|
+
if (this.#db === db) {
|
|
222
|
+
this.#db = void 0;
|
|
223
|
+
this.#dbPromise = void 0;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
db.onversionchange = () => {
|
|
227
|
+
db.close();
|
|
228
|
+
};
|
|
229
|
+
return db;
|
|
230
|
+
});
|
|
231
|
+
return this.#dbPromise;
|
|
232
|
+
}
|
|
233
|
+
#scheduleEviction() {
|
|
234
|
+
if (this.#evictionInterval <= 0) return;
|
|
235
|
+
this.#evictionTimer = setInterval(() => {
|
|
236
|
+
this.#maybeEvict();
|
|
237
|
+
}, this.#evictionInterval);
|
|
238
|
+
}
|
|
239
|
+
async #maybeEvict() {
|
|
240
|
+
if (this.#closed) return;
|
|
241
|
+
const locks = globalThis.navigator?.locks;
|
|
242
|
+
if (locks?.request) try {
|
|
243
|
+
await locks.request(EVICTION_LOCK_NAME, { ifAvailable: true }, async (lock) => {
|
|
244
|
+
if (!lock) return;
|
|
245
|
+
await this.#evict();
|
|
246
|
+
});
|
|
247
|
+
return;
|
|
248
|
+
} catch {}
|
|
249
|
+
await this.#evict();
|
|
250
|
+
}
|
|
251
|
+
async #evict() {
|
|
252
|
+
let db;
|
|
253
|
+
try {
|
|
254
|
+
db = await this.#open();
|
|
255
|
+
} catch {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
await runTransaction(db, this.#storeName, "readwrite", async (store) => {
|
|
259
|
+
const keys = await promisifyRequest(store.getAllKeys());
|
|
260
|
+
if (keys.length === 0) return;
|
|
261
|
+
const rows = await promisifyRequest(store.getAll());
|
|
262
|
+
const cutoff = findEvictionCutoff(keys.map((key, index) => ({
|
|
263
|
+
key,
|
|
264
|
+
lineCount: key[3],
|
|
265
|
+
byteLength: byteLengthUtf8(rows[index].lines)
|
|
266
|
+
})), this.#maxRecords, this.#maxBytes);
|
|
267
|
+
if (cutoff !== void 0) store.delete(IDBKeyRange.upperBound(cutoff));
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
#installLifecycleHandlers() {
|
|
271
|
+
if (typeof globalThis.addEventListener !== "function") return;
|
|
272
|
+
this.#pageHideHandler = () => {
|
|
273
|
+
this.flush();
|
|
274
|
+
};
|
|
275
|
+
this.#visibilityHandler = () => {
|
|
276
|
+
if (globalThis.document?.visibilityState === "hidden") this.flush();
|
|
277
|
+
};
|
|
278
|
+
globalThis.addEventListener("pagehide", this.#pageHideHandler);
|
|
279
|
+
globalThis.addEventListener("visibilitychange", this.#visibilityHandler);
|
|
280
|
+
}
|
|
281
|
+
#removeLifecycleHandlers() {
|
|
282
|
+
if (typeof globalThis.removeEventListener !== "function") return;
|
|
283
|
+
if (this.#pageHideHandler) {
|
|
284
|
+
globalThis.removeEventListener("pagehide", this.#pageHideHandler);
|
|
285
|
+
this.#pageHideHandler = void 0;
|
|
286
|
+
}
|
|
287
|
+
if (this.#visibilityHandler) {
|
|
288
|
+
globalThis.removeEventListener("visibilitychange", this.#visibilityHandler);
|
|
289
|
+
this.#visibilityHandler = void 0;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
var openDatabase = (name, storeName) => {
|
|
294
|
+
return new Promise((resolve, reject) => {
|
|
295
|
+
const request = indexedDB.open(name, DB_VERSION);
|
|
296
|
+
request.onupgradeneeded = () => {
|
|
297
|
+
const db = request.result;
|
|
298
|
+
if (db.objectStoreNames.contains(storeName)) db.deleteObjectStore(storeName);
|
|
299
|
+
db.createObjectStore(storeName);
|
|
300
|
+
};
|
|
301
|
+
request.onsuccess = () => resolve(request.result);
|
|
302
|
+
request.onerror = () => reject(request.error);
|
|
303
|
+
request.onblocked = () => reject(/* @__PURE__ */ new Error("IndexedDB open blocked"));
|
|
304
|
+
});
|
|
305
|
+
};
|
|
306
|
+
var promisifyRequest = (request) => new Promise((resolve, reject) => {
|
|
307
|
+
request.onsuccess = () => resolve(request.result);
|
|
308
|
+
request.onerror = () => reject(request.error);
|
|
309
|
+
});
|
|
310
|
+
var runTransaction = (db, storeName, mode, body) => {
|
|
311
|
+
return new Promise((resolve, reject) => {
|
|
312
|
+
let tx;
|
|
313
|
+
try {
|
|
314
|
+
tx = db.transaction(storeName, mode);
|
|
315
|
+
} catch (err) {
|
|
316
|
+
reject(err);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
let result;
|
|
320
|
+
let bodySettled = false;
|
|
321
|
+
let bodyError;
|
|
322
|
+
tx.oncomplete = () => {
|
|
323
|
+
if (bodyError !== void 0) reject(bodyError);
|
|
324
|
+
else if (bodySettled) resolve(result);
|
|
325
|
+
else reject(/* @__PURE__ */ new Error("transaction body did not settle"));
|
|
326
|
+
};
|
|
327
|
+
tx.onerror = () => reject(tx.error);
|
|
328
|
+
tx.onabort = () => reject(tx.error ?? /* @__PURE__ */ new Error("transaction aborted"));
|
|
329
|
+
try {
|
|
330
|
+
const value = body(tx.objectStore(storeName));
|
|
331
|
+
Promise.resolve(value).then((resolved) => {
|
|
332
|
+
result = resolved;
|
|
333
|
+
bodySettled = true;
|
|
334
|
+
}).catch((err) => {
|
|
335
|
+
bodyError = err;
|
|
336
|
+
try {
|
|
337
|
+
tx.abort();
|
|
338
|
+
} catch {}
|
|
339
|
+
});
|
|
340
|
+
} catch (err) {
|
|
341
|
+
bodyError = err;
|
|
342
|
+
try {
|
|
343
|
+
tx.abort();
|
|
344
|
+
} catch {}
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
};
|
|
348
|
+
var readAllChunks = async (db, storeName) => {
|
|
349
|
+
return runTransaction(db, storeName, "readonly", (store) => promisifyRequest(store.getAll()).then((rows) => rows.map((row) => row.lines)));
|
|
350
|
+
};
|
|
351
|
+
/** Assemble chunk payloads into one NDJSON blob without `Array.join` (avoids max-string-length errors). */
|
|
352
|
+
var buildNdjsonBlob = (chunks) => {
|
|
353
|
+
if (chunks.length === 0) return new Blob([], { type: "application/x-ndjson" });
|
|
354
|
+
const parts = [chunks[0]];
|
|
355
|
+
for (let index = 1; index < chunks.length; index++) parts.push("\n", chunks[index]);
|
|
356
|
+
return new Blob(parts, { type: "application/x-ndjson" });
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Given all chunks in ascending (key) order, return the key of the newest chunk that
|
|
360
|
+
* must be evicted — delete it and everything older — so retained totals stay within
|
|
361
|
+
* `maxRecords` and `maxBytes`. The newest chunk is always retained, even if it alone
|
|
362
|
+
* exceeds the budget. Returns `undefined` when everything fits.
|
|
363
|
+
*/
|
|
364
|
+
var findEvictionCutoff = (chunks, maxRecords, maxBytes) => {
|
|
365
|
+
let retainedLines = 0;
|
|
366
|
+
let retainedBytes = 0;
|
|
367
|
+
for (let index = chunks.length - 1; index >= 0; index--) {
|
|
368
|
+
const chunk = chunks[index];
|
|
369
|
+
retainedLines += chunk.lineCount;
|
|
370
|
+
retainedBytes += chunk.byteLength;
|
|
371
|
+
if ((retainedLines > maxRecords || retainedBytes > maxBytes) && index < chunks.length - 1) return chunk.key;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
/**
|
|
375
|
+
* Run `callback` after `delay` ms at background priority where the Scheduler API is
|
|
376
|
+
* available, so deferred work yields to input handling and rendering; falls back to a
|
|
377
|
+
* plain timeout elsewhere.
|
|
378
|
+
*/
|
|
379
|
+
var scheduleBackgroundTask = (callback, delay) => {
|
|
380
|
+
const scheduler = globalThis.scheduler;
|
|
381
|
+
if (typeof scheduler?.postTask === "function") {
|
|
382
|
+
const controller = new AbortController();
|
|
383
|
+
scheduler.postTask(callback, {
|
|
384
|
+
priority: "background",
|
|
385
|
+
delay,
|
|
386
|
+
signal: controller.signal
|
|
387
|
+
}).catch(() => {});
|
|
388
|
+
return { cancel: () => controller.abort() };
|
|
389
|
+
}
|
|
390
|
+
const timer = setTimeout(callback, delay);
|
|
391
|
+
return { cancel: () => clearTimeout(timer) };
|
|
392
|
+
};
|
|
393
|
+
//#endregion
|
|
394
|
+
export { IdbLogStore, MANUAL_LOG_EXPORT_MAX_BYTES, byteLengthUtf8, trimJsonlToSize };
|
|
395
|
+
|
|
396
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/trim.ts","../../src/idb-log-store.ts"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\n/**\n * Trim a list of pre-encoded JSONL lines so the joined output (with `\\n` separators)\n * fits within `maxSize` bytes (UTF-8). The newest lines (end of array) are preferred —\n * lines are dropped from the start. Never cuts inside a line.\n *\n * Returns the trimmed JSONL string (no trailing newline).\n */\nexport const trimJsonlToSize = (lines: readonly string[], maxSize: number): string => {\n if (lines.length === 0 || maxSize <= 0) {\n return '';\n }\n\n const sizes = lines.map((line) => byteLengthUtf8(line));\n\n // Walk newest -> oldest, accumulating until next addition would exceed maxSize.\n let total = 0;\n let firstIncludedIndex = lines.length;\n for (let index = lines.length - 1; index >= 0; index--) {\n const lineBytes = sizes[index];\n const sepBytes = total === 0 ? 0 : 1;\n if (total + lineBytes + sepBytes > maxSize) {\n break;\n }\n total += lineBytes + sepBytes;\n firstIncludedIndex = index;\n }\n\n if (firstIncludedIndex >= lines.length) {\n return '';\n }\n return lines.slice(firstIncludedIndex).join('\\n');\n};\n\nconst utf8Encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : undefined;\n\n/** UTF-8 byte length of a string (used by eviction and trim). */\nexport const byteLengthUtf8 = (value: string): number => {\n if (utf8Encoder) {\n return utf8Encoder.encode(value).length;\n }\n // Fallback: assume 1 byte per char (good enough for ASCII-heavy log lines).\n return value.length;\n};\n","//\n// Copyright 2026 DXOS.org\n//\n\nimport {\n type LogConfig,\n type LogEntry,\n type LogFilter,\n type LogProcessor,\n inferEnvironmentName,\n parseFilter,\n serializeToJsonl,\n shouldLog,\n} from '@dxos/log';\n\nimport { byteLengthUtf8, trimJsonlToSize } from './trim';\n\nconst DEFAULT_STORE_NAME = 'logs';\nconst DEFAULT_LOG_FILTER = 'debug';\nconst DEFAULT_FLUSH_INTERVAL = 250;\nconst DEFAULT_FLUSH_BATCH_SIZE = 500;\n// Sized for ~50 MB on disk at the observed Composer average of ~350 bytes per JSONL line.\nconst DEFAULT_MAX_RECORDS = 150_000;\nconst DEFAULT_MAX_BYTES = 50 * 1024 * 1024;\n/** Max UTF-8 bytes for user-initiated log downloads from app UI (Reset dialog, debug settings, devtools hook). */\nexport const MANUAL_LOG_EXPORT_MAX_BYTES = 200 * 1024 * 1024;\nconst DEFAULT_EVICTION_INTERVAL = 30_000;\nconst EVICTION_LOCK_NAME = '@dxos/log-store-idb:evictor';\n// v2 introduced chunked rows with out-of-line array keys (v1 stored one row per line\n// under an autoincrement keyPath).\nconst DB_VERSION = 2;\n\n/**\n * Chunk key: `[epochMs, writerId, writerSeq, lineCount]`.\n *\n * Keys order chunks by flush time; `writerId`/`writerSeq` keep keys from concurrent\n * writers unique. `lineCount` rides in the key so eviction can compute the retention\n * budget from `getAllKeys()` alone, without deserializing chunk values.\n */\ntype ChunkKey = [epochMs: number, writerId: string, writerSeq: number, lineCount: number];\n\n/**\n * Stored row: one flush batch of pre-encoded JSONL lines joined with `\\n` (no trailing\n * newline). Storing the whole batch as a single record keeps the per-flush cost at one\n * structured clone and one backend record regardless of batch size.\n */\ntype LogChunk = {\n lines: string;\n};\n\nexport type IdbLogStoreOptions = {\n /** Name of the IndexedDB database. */\n dbName: string;\n /** Object store name. Default `'logs'`. */\n storeName?: string;\n /** Auto-flush interval in milliseconds. Default `250`. */\n flushInterval?: number;\n /** Auto-flush when in-memory queue reaches this size. Default `500`. */\n flushBatchSize?: number;\n /**\n * Soft cap on retained log records (JSONL lines). Eviction drops whole flush chunks,\n * oldest first; the newest chunk is always retained. Default `150_000`.\n */\n maxRecords?: number;\n /**\n * Soft cap on retained log bytes (UTF-8). Eviction drops whole flush chunks, oldest\n * first; the newest chunk is always retained. Default `50 MiB`.\n */\n maxBytes?: number;\n /** Eviction sweep interval in milliseconds. Default `30_000`. */\n evictionInterval?: number;\n /**\n * Identifier embedded in every record's `i` field.\n * Defaults to a scope-aware id of the form `<scope>:<name>:<suffix>` — see {@link inferEnvironmentName}.\n */\n tabId?: string;\n /**\n * Same syntax as `DX_LOG` / {@link parseFilter} (e.g. `debug`, `info`, `pattern:warn`).\n * Entries below the minimum level are not stored. Default `debug` (TRACE is excluded).\n */\n logFilter?: string;\n};\n\n/**\n * IndexedDB-backed log store.\n *\n * Logs are batched in memory and each flush is written as a single chunk record of\n * pre-encoded JSONL. Multiple browsing contexts can write to the same database\n * concurrently — IDB serializes transactions, so no extra coordination is needed for\n * writes. Eviction is deduplicated across tabs via the Web Locks API (`ifAvailable`).\n *\n * Flushes are triggered by:\n * - a deferred task after `flushInterval` (background priority where the Scheduler API\n * is available, so flushes don't compete with input handling and rendering),\n * - the queue exceeding `flushBatchSize`,\n * - `visibilitychange` to `hidden` and `pagehide` events.\n */\nexport class IdbLogStore {\n readonly #dbName: string;\n readonly #storeName: string;\n readonly #flushInterval: number;\n readonly #flushBatchSize: number;\n readonly #maxRecords: number;\n readonly #maxBytes: number;\n readonly #evictionInterval: number;\n readonly #tabId: string;\n readonly #filters: LogFilter[];\n /** Distinguishes chunk keys written by concurrent contexts against the same database. */\n readonly #writerId = Math.random().toString(36).slice(2);\n\n #queue: string[] = [];\n #writerSeq = 0;\n #flushTask: ScheduledTask | undefined;\n #evictionTimer: ReturnType<typeof setInterval> | undefined;\n #pendingFlush: Promise<void> | undefined;\n #db: IDBDatabase | undefined;\n #dbPromise: Promise<IDBDatabase> | undefined;\n #closed = false;\n #pageHideHandler: (() => void) | undefined;\n #visibilityHandler: (() => void) | undefined;\n\n constructor(options: IdbLogStoreOptions) {\n this.#dbName = options.dbName;\n this.#storeName = options.storeName ?? DEFAULT_STORE_NAME;\n this.#flushInterval = options.flushInterval ?? DEFAULT_FLUSH_INTERVAL;\n this.#flushBatchSize = options.flushBatchSize ?? DEFAULT_FLUSH_BATCH_SIZE;\n this.#maxRecords = options.maxRecords ?? DEFAULT_MAX_RECORDS;\n this.#maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n this.#evictionInterval = options.evictionInterval ?? DEFAULT_EVICTION_INTERVAL;\n this.#tabId = options.tabId ?? inferEnvironmentName();\n this.#filters = parseFilter(options.logFilter ?? DEFAULT_LOG_FILTER);\n\n this.#installLifecycleHandlers();\n this.#scheduleEviction();\n }\n\n /**\n * Log processor — register via `log.addProcessor(store.processor)`.\n */\n readonly processor: LogProcessor = (_config: LogConfig, entry: LogEntry) => {\n if (this.#closed) {\n return;\n }\n if (!shouldLog(entry, this.#filters)) {\n return;\n }\n const line = serializeToJsonl(entry, { env: this.#tabId });\n if (line === undefined) {\n return;\n }\n this.#queue.push(line);\n\n if (this.#queue.length >= this.#flushBatchSize) {\n void this.flush();\n } else if (this.#flushTask === undefined) {\n this.#flushTask = scheduleBackgroundTask(() => {\n this.#flushTask = undefined;\n void this.flush();\n }, this.#flushInterval);\n }\n };\n\n /**\n * Force a flush now and resolve once the IDB transaction commits.\n * Concurrent calls share a single in-flight flush.\n */\n async flush(): Promise<void> {\n if (this.#flushTask !== undefined) {\n this.#flushTask.cancel();\n this.#flushTask = undefined;\n }\n if (this.#pendingFlush) {\n return this.#pendingFlush;\n }\n if (this.#queue.length === 0) {\n return;\n }\n\n const batch = this.#queue;\n this.#queue = [];\n\n this.#pendingFlush = this.#writeBatch(batch).finally(() => {\n this.#pendingFlush = undefined;\n });\n return this.#pendingFlush;\n }\n\n /**\n * Read all retained log lines as a JSONL string.\n *\n * If `maxSize` is provided (in bytes), the newest lines are kept and older lines\n * are dropped from the front; lines are never split.\n *\n * For full exports of very large stores, prefer {@link exportBlob} — building one\n * giant string can exceed the engine's maximum string length.\n */\n async export(options: { maxSize?: number } = {}): Promise<string> {\n const blob = await this.exportBlob(options);\n return blob.text();\n }\n\n /**\n * Read all retained log lines as an NDJSON blob without materializing one giant string.\n * Prefer this for browser downloads of large log stores.\n */\n async exportBlob(options: { maxSize?: number } = {}): Promise<Blob> {\n await this.flush();\n await this.#maybeEvict();\n const db = await this.#open();\n const chunks = await readAllChunks(db, this.#storeName);\n const maxSize = options.maxSize ?? this.#maxBytes;\n if (maxSize >= 0) {\n // Trim on individual lines so the size cap never under-fills by a whole chunk.\n const lines = chunks.flatMap((chunk) => chunk.split('\\n'));\n const trimmed = trimJsonlToSize(lines, maxSize);\n return new Blob([trimmed], { type: 'application/x-ndjson' });\n }\n return buildNdjsonBlob(chunks);\n }\n\n /**\n * Discard all stored log records.\n */\n async clear(): Promise<void> {\n this.#queue = [];\n if (this.#flushTask !== undefined) {\n this.#flushTask.cancel();\n this.#flushTask = undefined;\n }\n const db = await this.#open();\n await runTransaction(db, this.#storeName, 'readwrite', (store) => {\n store.clear();\n });\n }\n\n /**\n * Run an eviction sweep now: trim the store down to `maxRecords` / `maxBytes` if it\n * has grown past. Eviction runs automatically on a timer; this method is exposed for\n * tests and for callers that want to force a sweep at a particular moment.\n */\n async evictNow(): Promise<void> {\n await this.#maybeEvict();\n }\n\n /**\n * Stop timers, flush remaining entries, and close the database connection.\n */\n async close(): Promise<void> {\n if (this.#closed) {\n return;\n }\n this.#closed = true;\n\n if (this.#evictionTimer !== undefined) {\n clearInterval(this.#evictionTimer);\n this.#evictionTimer = undefined;\n }\n this.#removeLifecycleHandlers();\n\n try {\n await this.flush();\n } catch {\n // Ignore flush errors during shutdown.\n }\n\n if (this.#db) {\n this.#db.close();\n this.#db = undefined;\n this.#dbPromise = undefined;\n }\n }\n\n async #writeBatch(batch: string[]): Promise<void> {\n if (batch.length === 0) {\n return;\n }\n let db: IDBDatabase;\n try {\n db = await this.#open();\n } catch {\n // If the database can't be opened, drop the batch silently — logs must never\n // throw out of the processor path.\n return;\n }\n\n try {\n const chunk: LogChunk = { lines: batch.join('\\n') };\n const key: ChunkKey = [Date.now(), this.#writerId, this.#writerSeq++, batch.length];\n await runTransaction(db, this.#storeName, 'readwrite', (store) => {\n store.add(chunk, key);\n });\n void this.#maybeEvict();\n } catch {\n // Ignore write errors.\n }\n }\n\n async #open(): Promise<IDBDatabase> {\n if (this.#db) {\n return this.#db;\n }\n if (!this.#dbPromise) {\n this.#dbPromise = openDatabase(this.#dbName, this.#storeName).then((db) => {\n this.#db = db;\n db.onclose = () => {\n if (this.#db === db) {\n this.#db = undefined;\n this.#dbPromise = undefined;\n }\n };\n db.onversionchange = () => {\n db.close();\n };\n return db;\n });\n }\n return this.#dbPromise;\n }\n\n #scheduleEviction(): void {\n if (this.#evictionInterval <= 0) {\n return;\n }\n this.#evictionTimer = setInterval(() => {\n void this.#maybeEvict();\n }, this.#evictionInterval);\n }\n\n async #maybeEvict(): Promise<void> {\n if (this.#closed) {\n return;\n }\n const locks = (globalThis as any).navigator?.locks as LockManager | undefined;\n if (locks?.request) {\n try {\n await locks.request(EVICTION_LOCK_NAME, { ifAvailable: true }, async (lock) => {\n if (!lock) {\n return;\n }\n await this.#evict();\n });\n return;\n } catch {\n // Fall through to direct eviction if locks fail.\n }\n }\n await this.#evict();\n }\n\n async #evict(): Promise<void> {\n let db: IDBDatabase;\n try {\n db = await this.#open();\n } catch {\n return;\n }\n\n await runTransaction(db, this.#storeName, 'readwrite', async (store) => {\n const keys = (await promisifyRequest(store.getAllKeys())) as ChunkKey[];\n if (keys.length === 0) {\n return;\n }\n const rows = (await promisifyRequest(store.getAll())) as LogChunk[];\n const chunks = keys.map((key, index) => ({\n key,\n lineCount: key[3],\n byteLength: byteLengthUtf8(rows[index]!.lines),\n }));\n const cutoff = findEvictionCutoff(chunks, this.#maxRecords, this.#maxBytes);\n if (cutoff !== undefined) {\n store.delete(IDBKeyRange.upperBound(cutoff));\n }\n });\n }\n\n #installLifecycleHandlers(): void {\n if (typeof globalThis.addEventListener !== 'function') {\n return;\n }\n this.#pageHideHandler = () => {\n void this.flush();\n };\n this.#visibilityHandler = () => {\n if ((globalThis as any).document?.visibilityState === 'hidden') {\n void this.flush();\n }\n };\n globalThis.addEventListener('pagehide', this.#pageHideHandler);\n globalThis.addEventListener('visibilitychange', this.#visibilityHandler);\n }\n\n #removeLifecycleHandlers(): void {\n if (typeof globalThis.removeEventListener !== 'function') {\n return;\n }\n if (this.#pageHideHandler) {\n globalThis.removeEventListener('pagehide', this.#pageHideHandler);\n this.#pageHideHandler = undefined;\n }\n if (this.#visibilityHandler) {\n globalThis.removeEventListener('visibilitychange', this.#visibilityHandler);\n this.#visibilityHandler = undefined;\n }\n }\n}\n\nconst openDatabase = (name: string, storeName: string): Promise<IDBDatabase> => {\n return new Promise((resolve, reject) => {\n const request = indexedDB.open(name, DB_VERSION);\n request.onupgradeneeded = () => {\n const db = request.result;\n // The v1 schema stored one row per line under an autoincrement keyPath; chunked\n // storage requires out-of-line keys, so the store is recreated and v1 data is\n // discarded (logs are expendable diagnostics).\n if (db.objectStoreNames.contains(storeName)) {\n db.deleteObjectStore(storeName);\n }\n db.createObjectStore(storeName);\n };\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n request.onblocked = () => reject(new Error('IndexedDB open blocked'));\n });\n};\n\nconst promisifyRequest = <T>(request: IDBRequest<T>): Promise<T> =>\n new Promise((resolve, reject) => {\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n });\n\nconst runTransaction = <T>(\n db: IDBDatabase,\n storeName: string,\n mode: IDBTransactionMode,\n body: (store: IDBObjectStore) => T | Promise<T>,\n): Promise<T> => {\n return new Promise<T>((resolve, reject) => {\n let tx: IDBTransaction;\n try {\n tx = db.transaction(storeName, mode);\n } catch (err) {\n reject(err);\n return;\n }\n let result: T;\n let bodySettled = false;\n let bodyError: unknown;\n\n tx.oncomplete = () => {\n if (bodyError !== undefined) {\n reject(bodyError);\n } else if (bodySettled) {\n resolve(result);\n } else {\n // Body never settled (shouldn't happen in practice).\n reject(new Error('transaction body did not settle'));\n }\n };\n tx.onerror = () => reject(tx.error);\n tx.onabort = () => reject(tx.error ?? new Error('transaction aborted'));\n\n try {\n const store = tx.objectStore(storeName);\n const value = body(store);\n Promise.resolve(value)\n .then((resolved) => {\n result = resolved;\n bodySettled = true;\n })\n .catch((err) => {\n bodyError = err;\n try {\n tx.abort();\n } catch {\n // Already settled.\n }\n });\n } catch (err) {\n bodyError = err;\n try {\n tx.abort();\n } catch {\n // Ignore.\n }\n }\n });\n};\n\nconst readAllChunks = async (db: IDBDatabase, storeName: string): Promise<string[]> => {\n return runTransaction(db, storeName, 'readonly', (store) =>\n promisifyRequest(store.getAll()).then((rows) => (rows as LogChunk[]).map((row) => row.lines)),\n );\n};\n\n/** Assemble chunk payloads into one NDJSON blob without `Array.join` (avoids max-string-length errors). */\nconst buildNdjsonBlob = (chunks: readonly string[]): Blob => {\n if (chunks.length === 0) {\n return new Blob([], { type: 'application/x-ndjson' });\n }\n const parts: BlobPart[] = [chunks[0]!];\n for (let index = 1; index < chunks.length; index++) {\n parts.push('\\n', chunks[index]!);\n }\n return new Blob(parts, { type: 'application/x-ndjson' });\n};\n\ntype ChunkRetentionStats = {\n key: ChunkKey;\n lineCount: number;\n byteLength: number;\n};\n\n/**\n * Given all chunks in ascending (key) order, return the key of the newest chunk that\n * must be evicted — delete it and everything older — so retained totals stay within\n * `maxRecords` and `maxBytes`. The newest chunk is always retained, even if it alone\n * exceeds the budget. Returns `undefined` when everything fits.\n */\nconst findEvictionCutoff = (\n chunks: readonly ChunkRetentionStats[],\n maxRecords: number,\n maxBytes: number,\n): ChunkKey | undefined => {\n let retainedLines = 0;\n let retainedBytes = 0;\n for (let index = chunks.length - 1; index >= 0; index--) {\n const chunk = chunks[index]!;\n retainedLines += chunk.lineCount;\n retainedBytes += chunk.byteLength;\n if ((retainedLines > maxRecords || retainedBytes > maxBytes) && index < chunks.length - 1) {\n return chunk.key;\n }\n }\n return undefined;\n};\n\ntype ScheduledTask = { cancel: () => void };\n\ntype SchedulerLike = {\n postTask: (\n callback: () => void,\n options?: { priority?: 'background' | 'user-visible' | 'user-blocking'; delay?: number; signal?: AbortSignal },\n ) => Promise<unknown>;\n};\n\n/**\n * Run `callback` after `delay` ms at background priority where the Scheduler API is\n * available, so deferred work yields to input handling and rendering; falls back to a\n * plain timeout elsewhere.\n */\nconst scheduleBackgroundTask = (callback: () => void, delay: number): ScheduledTask => {\n // The Scheduler API is not in the TS DOM lib yet.\n const scheduler = (globalThis as { scheduler?: SchedulerLike }).scheduler;\n if (typeof scheduler?.postTask === 'function') {\n const controller = new AbortController();\n scheduler.postTask(callback, { priority: 'background', delay, signal: controller.signal }).catch(() => {\n // Rejects on abort; the callback never ran.\n });\n return { cancel: () => controller.abort() };\n }\n const timer = setTimeout(callback, delay);\n return { cancel: () => clearTimeout(timer) };\n};\n"],"mappings":";;;;;;;;;AAWA,IAAa,mBAAmB,OAA0B,YAA4B;CACpF,IAAI,MAAM,WAAW,KAAK,WAAW,GACnC,OAAO;CAGT,MAAM,QAAQ,MAAM,KAAK,SAAS,eAAe,IAAI,CAAC;CAGtD,IAAI,QAAQ;CACZ,IAAI,qBAAqB,MAAM;CAC/B,KAAK,IAAI,QAAQ,MAAM,SAAS,GAAG,SAAS,GAAG,SAAS;EACtD,MAAM,YAAY,MAAM;EACxB,MAAM,WAAW,UAAU,IAAI,IAAI;EACnC,IAAI,QAAQ,YAAY,WAAW,SACjC;EAEF,SAAS,YAAY;EACrB,qBAAqB;CACvB;CAEA,IAAI,sBAAsB,MAAM,QAC9B,OAAO;CAET,OAAO,MAAM,MAAM,kBAAkB,CAAC,CAAC,KAAK,IAAI;AAClD;AAEA,IAAM,cAAc,OAAO,gBAAgB,cAAc,IAAI,YAAY,IAAI,KAAA;;AAG7E,IAAa,kBAAkB,UAA0B;CACvD,IAAI,aACF,OAAO,YAAY,OAAO,KAAK,CAAC,CAAC;CAGnC,OAAO,MAAM;AACf;;;AC7BA,IAAM,qBAAqB;AAC3B,IAAM,qBAAqB;AAC3B,IAAM,yBAAyB;AAC/B,IAAM,2BAA2B;AAEjC,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB,KAAK,OAAO;;AAEtC,IAAa,8BAA8B,MAAM,OAAO;AACxD,IAAM,4BAA4B;AAClC,IAAM,qBAAqB;AAG3B,IAAM,aAAa;;;;;;;;;;;;;;;AAmEnB,IAAa,cAAb,MAAyB;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;CAEA,YAAqB,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC;CAEvD,SAAmB,CAAC;CACpB,aAAa;CACb;CACA;CACA;CACA;CACA;CACA,UAAU;CACV;CACA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,KAAK,CAAC,GAAG,EAAE,MAAM,uBAAuB,CAAC;CAEtD,MAAM,QAAoB,CAAC,OAAO,EAAG;CACrC,KAAK,IAAI,QAAQ,GAAG,QAAQ,OAAO,QAAQ,SACzC,MAAM,KAAK,MAAM,OAAO,MAAO;CAEjC,OAAO,IAAI,KAAK,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACzD;;;;;;;AAcA,IAAM,sBACJ,QACA,YACA,aACyB;CACzB,IAAI,gBAAgB;CACpB,IAAI,gBAAgB;CACpB,KAAK,IAAI,QAAQ,OAAO,SAAS,GAAG,SAAS,GAAG,SAAS;EACvD,MAAM,QAAQ,OAAO;EACrB,iBAAiB,MAAM;EACvB,iBAAiB,MAAM;EACvB,KAAK,gBAAgB,cAAc,gBAAgB,aAAa,QAAQ,OAAO,SAAS,GACtF,OAAO,MAAM;CAEjB;AAEF;;;;;;AAgBA,IAAM,0BAA0B,UAAsB,UAAiC;CAErF,MAAM,YAAa,WAA6C;CAChE,IAAI,OAAO,WAAW,aAAa,YAAY;EAC7C,MAAM,aAAa,IAAI,gBAAgB;EACvC,UAAU,SAAS,UAAU;GAAE,UAAU;GAAc;GAAO,QAAQ,WAAW;EAAO,CAAC,CAAC,CAAC,YAAY,CAEvG,CAAC;EACD,OAAO,EAAE,cAAc,WAAW,MAAM,EAAE;CAC5C;CACA,MAAM,QAAQ,WAAW,UAAU,KAAK;CACxC,OAAO,EAAE,cAAc,aAAa,KAAK,EAAE;AAC7C"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type LogProcessor } from '@dxos/log';
|
|
2
|
+
/** Max UTF-8 bytes for user-initiated log downloads from app UI (Reset dialog, debug settings, devtools hook). */
|
|
3
|
+
export declare const MANUAL_LOG_EXPORT_MAX_BYTES: number;
|
|
2
4
|
export type IdbLogStoreOptions = {
|
|
3
5
|
/** Name of the IndexedDB database. */
|
|
4
6
|
dbName: string;
|
|
@@ -10,11 +12,14 @@ export type IdbLogStoreOptions = {
|
|
|
10
12
|
flushBatchSize?: number;
|
|
11
13
|
/**
|
|
12
14
|
* Soft cap on retained log records (JSONL lines). Eviction drops whole flush chunks,
|
|
13
|
-
* oldest first; the newest chunk is always retained. Default `150_000
|
|
14
|
-
* roughly 50 MB on disk at typical JSONL line sizes (~350 bytes average observed in
|
|
15
|
-
* the Composer app).
|
|
15
|
+
* oldest first; the newest chunk is always retained. Default `150_000`.
|
|
16
16
|
*/
|
|
17
17
|
maxRecords?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Soft cap on retained log bytes (UTF-8). Eviction drops whole flush chunks, oldest
|
|
20
|
+
* first; the newest chunk is always retained. Default `50 MiB`.
|
|
21
|
+
*/
|
|
22
|
+
maxBytes?: number;
|
|
18
23
|
/** Eviction sweep interval in milliseconds. Default `30_000`. */
|
|
19
24
|
evictionInterval?: number;
|
|
20
25
|
/**
|
|
@@ -59,18 +64,28 @@ export declare class IdbLogStore {
|
|
|
59
64
|
*
|
|
60
65
|
* If `maxSize` is provided (in bytes), the newest lines are kept and older lines
|
|
61
66
|
* are dropped from the front; lines are never split.
|
|
67
|
+
*
|
|
68
|
+
* For full exports of very large stores, prefer {@link exportBlob} — building one
|
|
69
|
+
* giant string can exceed the engine's maximum string length.
|
|
62
70
|
*/
|
|
63
71
|
export(options?: {
|
|
64
72
|
maxSize?: number;
|
|
65
73
|
}): Promise<string>;
|
|
74
|
+
/**
|
|
75
|
+
* Read all retained log lines as an NDJSON blob without materializing one giant string.
|
|
76
|
+
* Prefer this for browser downloads of large log stores.
|
|
77
|
+
*/
|
|
78
|
+
exportBlob(options?: {
|
|
79
|
+
maxSize?: number;
|
|
80
|
+
}): Promise<Blob>;
|
|
66
81
|
/**
|
|
67
82
|
* Discard all stored log records.
|
|
68
83
|
*/
|
|
69
84
|
clear(): Promise<void>;
|
|
70
85
|
/**
|
|
71
|
-
* Run an eviction sweep now: trim the store down to `maxRecords` if it
|
|
72
|
-
* Eviction runs automatically on a timer; this method is exposed for
|
|
73
|
-
* callers that want to force a sweep at a particular moment.
|
|
86
|
+
* Run an eviction sweep now: trim the store down to `maxRecords` / `maxBytes` if it
|
|
87
|
+
* has grown past. Eviction runs automatically on a timer; this method is exposed for
|
|
88
|
+
* tests and for callers that want to force a sweep at a particular moment.
|
|
74
89
|
*/
|
|
75
90
|
evictNow(): Promise<void>;
|
|
76
91
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idb-log-store.d.ts","sourceRoot":"","sources":["../../../src/idb-log-store.ts"],"names":[],"mappings":"AAIA,OAAO,EAIL,KAAK,YAAY,EAKlB,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"idb-log-store.d.ts","sourceRoot":"","sources":["../../../src/idb-log-store.ts"],"names":[],"mappings":"AAIA,OAAO,EAIL,KAAK,YAAY,EAKlB,MAAM,WAAW,CAAC;AAWnB,kHAAkH;AAClH,eAAO,MAAM,2BAA2B,QAAoB,CAAC;AAyB7D,MAAM,MAAM,kBAAkB,GAAG;IAC/B,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,2CAA2C;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,0DAA0D;IAC1D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wEAAwE;IACxE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,qBAAa,WAAW;;IAwBtB,YAAY,OAAO,EAAE,kBAAkB,EAatC;IAED;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,YAAY,CAqB9B;IAEF;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAmB3B;IAED;;;;;;;;OAQG;IACG,MAAM,CAAC,OAAO,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAGhE;IAED;;;OAGG;IACG,UAAU,CAAC,OAAO,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAalE;IAED;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAU3B;IAED;;;;OAIG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAE9B;IAED;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAuB3B;CAsIF"}
|
package/dist/types/src/trim.d.ts
CHANGED
|
@@ -6,4 +6,6 @@
|
|
|
6
6
|
* Returns the trimmed JSONL string (no trailing newline).
|
|
7
7
|
*/
|
|
8
8
|
export declare const trimJsonlToSize: (lines: readonly string[], maxSize: number) => string;
|
|
9
|
+
/** UTF-8 byte length of a string (used by eviction and trim). */
|
|
10
|
+
export declare const byteLengthUtf8: (value: string) => number;
|
|
9
11
|
//# sourceMappingURL=trim.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trim.d.ts","sourceRoot":"","sources":["../../../src/trim.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,UAAW,SAAS,MAAM,EAAE,WAAW,MAAM,KAAG,MAwB3E,CAAC"}
|
|
1
|
+
{"version":3,"file":"trim.d.ts","sourceRoot":"","sources":["../../../src/trim.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,UAAW,SAAS,MAAM,EAAE,WAAW,MAAM,KAAG,MAwB3E,CAAC;AAIF,iEAAiE;AACjE,eAAO,MAAM,cAAc,UAAW,MAAM,KAAG,MAM9C,CAAC"}
|