@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,399 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// src/idb-log-store.ts
|
|
4
|
+
import { inferEnvironmentName, parseFilter, serializeToJsonl, shouldLog } from "@dxos/log";
|
|
5
|
+
|
|
6
|
+
// src/trim.ts
|
|
7
|
+
var trimJsonlToSize = (lines, maxSize) => {
|
|
8
|
+
if (lines.length === 0 || maxSize <= 0) {
|
|
9
|
+
return "";
|
|
10
|
+
}
|
|
11
|
+
const sizes = lines.map((line) => byteLengthUtf8(line));
|
|
12
|
+
let total = 0;
|
|
13
|
+
let firstIncludedIndex = lines.length;
|
|
14
|
+
for (let index = lines.length - 1; index >= 0; index--) {
|
|
15
|
+
const lineBytes = sizes[index];
|
|
16
|
+
const sepBytes = total === 0 ? 0 : 1;
|
|
17
|
+
if (total + lineBytes + sepBytes > maxSize) {
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
total += lineBytes + sepBytes;
|
|
21
|
+
firstIncludedIndex = index;
|
|
22
|
+
}
|
|
23
|
+
if (firstIncludedIndex >= lines.length) {
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
return lines.slice(firstIncludedIndex).join("\n");
|
|
27
|
+
};
|
|
28
|
+
var utf8Encoder = typeof TextEncoder !== "undefined" ? new TextEncoder() : void 0;
|
|
29
|
+
var byteLengthUtf8 = (value) => {
|
|
30
|
+
if (utf8Encoder) {
|
|
31
|
+
return utf8Encoder.encode(value).length;
|
|
32
|
+
}
|
|
33
|
+
return value.length;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// src/idb-log-store.ts
|
|
37
|
+
var DEFAULT_STORE_NAME = "logs";
|
|
38
|
+
var DEFAULT_LOG_FILTER = "debug";
|
|
39
|
+
var DEFAULT_FLUSH_INTERVAL = 250;
|
|
40
|
+
var DEFAULT_FLUSH_BATCH_SIZE = 500;
|
|
41
|
+
var DEFAULT_MAX_RECORDS = 15e4;
|
|
42
|
+
var DEFAULT_EVICTION_INTERVAL = 3e4;
|
|
43
|
+
var EVICTION_LOCK_NAME = "@dxos/log-store-idb:evictor";
|
|
44
|
+
var IdbLogStore = class {
|
|
45
|
+
#dbName;
|
|
46
|
+
#storeName;
|
|
47
|
+
#flushInterval;
|
|
48
|
+
#flushBatchSize;
|
|
49
|
+
#maxRecords;
|
|
50
|
+
#evictionInterval;
|
|
51
|
+
#tabId;
|
|
52
|
+
#filters;
|
|
53
|
+
#queue = [];
|
|
54
|
+
#flushTimer;
|
|
55
|
+
#evictionTimer;
|
|
56
|
+
#pendingFlush;
|
|
57
|
+
#db;
|
|
58
|
+
#dbPromise;
|
|
59
|
+
#closed = false;
|
|
60
|
+
#pageHideHandler;
|
|
61
|
+
#visibilityHandler;
|
|
62
|
+
constructor(options) {
|
|
63
|
+
this.#dbName = options.dbName;
|
|
64
|
+
this.#storeName = options.storeName ?? DEFAULT_STORE_NAME;
|
|
65
|
+
this.#flushInterval = options.flushInterval ?? DEFAULT_FLUSH_INTERVAL;
|
|
66
|
+
this.#flushBatchSize = options.flushBatchSize ?? DEFAULT_FLUSH_BATCH_SIZE;
|
|
67
|
+
this.#maxRecords = options.maxRecords ?? DEFAULT_MAX_RECORDS;
|
|
68
|
+
this.#evictionInterval = options.evictionInterval ?? DEFAULT_EVICTION_INTERVAL;
|
|
69
|
+
this.#tabId = options.tabId ?? inferEnvironmentName();
|
|
70
|
+
this.#filters = parseFilter(options.logFilter ?? DEFAULT_LOG_FILTER);
|
|
71
|
+
this.#installLifecycleHandlers();
|
|
72
|
+
this.#scheduleEviction();
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Log processor — register via `log.addProcessor(store.processor)`.
|
|
76
|
+
*/
|
|
77
|
+
processor = (_config, entry) => {
|
|
78
|
+
if (this.#closed) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (!shouldLog(entry, this.#filters)) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const line = serializeToJsonl(entry, {
|
|
85
|
+
env: this.#tabId
|
|
86
|
+
});
|
|
87
|
+
if (line === void 0) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.#queue.push(line);
|
|
91
|
+
if (this.#queue.length >= this.#flushBatchSize) {
|
|
92
|
+
void this.flush();
|
|
93
|
+
} else if (this.#flushTimer === void 0) {
|
|
94
|
+
this.#flushTimer = setTimeout(() => {
|
|
95
|
+
this.#flushTimer = void 0;
|
|
96
|
+
void this.flush();
|
|
97
|
+
}, this.#flushInterval);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Force a flush now and resolve once the IDB transaction commits.
|
|
102
|
+
* Concurrent calls share a single in-flight flush.
|
|
103
|
+
*/
|
|
104
|
+
async flush() {
|
|
105
|
+
if (this.#flushTimer !== void 0) {
|
|
106
|
+
clearTimeout(this.#flushTimer);
|
|
107
|
+
this.#flushTimer = void 0;
|
|
108
|
+
}
|
|
109
|
+
if (this.#pendingFlush) {
|
|
110
|
+
return this.#pendingFlush;
|
|
111
|
+
}
|
|
112
|
+
if (this.#queue.length === 0) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const batch = this.#queue;
|
|
116
|
+
this.#queue = [];
|
|
117
|
+
this.#pendingFlush = this.#writeBatch(batch).finally(() => {
|
|
118
|
+
this.#pendingFlush = void 0;
|
|
119
|
+
});
|
|
120
|
+
return this.#pendingFlush;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Read all retained log lines as a JSONL string.
|
|
124
|
+
*
|
|
125
|
+
* If `maxSize` is provided (in bytes), the newest lines are kept and older lines
|
|
126
|
+
* are dropped from the front; lines are never split.
|
|
127
|
+
*/
|
|
128
|
+
async export(options = {}) {
|
|
129
|
+
await this.flush();
|
|
130
|
+
const db = await this.#open();
|
|
131
|
+
const lines = await readAllLines(db, this.#storeName);
|
|
132
|
+
if (options.maxSize !== void 0 && options.maxSize >= 0) {
|
|
133
|
+
return trimJsonlToSize(lines, options.maxSize);
|
|
134
|
+
}
|
|
135
|
+
return lines.join("\n");
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Discard all stored log records.
|
|
139
|
+
*/
|
|
140
|
+
async clear() {
|
|
141
|
+
this.#queue = [];
|
|
142
|
+
if (this.#flushTimer !== void 0) {
|
|
143
|
+
clearTimeout(this.#flushTimer);
|
|
144
|
+
this.#flushTimer = void 0;
|
|
145
|
+
}
|
|
146
|
+
const db = await this.#open();
|
|
147
|
+
await runTransaction(db, this.#storeName, "readwrite", (store) => {
|
|
148
|
+
store.clear();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Run an eviction sweep now: trim the store down to `maxRecords` if it has grown past.
|
|
153
|
+
* Eviction runs automatically on a timer; this method is exposed for tests and for
|
|
154
|
+
* callers that want to force a sweep at a particular moment.
|
|
155
|
+
*/
|
|
156
|
+
async evictNow() {
|
|
157
|
+
await this.#maybeEvict();
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Stop timers, flush remaining entries, and close the database connection.
|
|
161
|
+
*/
|
|
162
|
+
async close() {
|
|
163
|
+
if (this.#closed) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this.#closed = true;
|
|
167
|
+
if (this.#evictionTimer !== void 0) {
|
|
168
|
+
clearInterval(this.#evictionTimer);
|
|
169
|
+
this.#evictionTimer = void 0;
|
|
170
|
+
}
|
|
171
|
+
this.#removeLifecycleHandlers();
|
|
172
|
+
try {
|
|
173
|
+
await this.flush();
|
|
174
|
+
} catch {
|
|
175
|
+
}
|
|
176
|
+
if (this.#db) {
|
|
177
|
+
this.#db.close();
|
|
178
|
+
this.#db = void 0;
|
|
179
|
+
this.#dbPromise = void 0;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async #writeBatch(batch) {
|
|
183
|
+
if (batch.length === 0) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
let db;
|
|
187
|
+
try {
|
|
188
|
+
db = await this.#open();
|
|
189
|
+
} catch {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
await runTransaction(db, this.#storeName, "readwrite", (store) => {
|
|
194
|
+
for (const line of batch) {
|
|
195
|
+
const row = {
|
|
196
|
+
line
|
|
197
|
+
};
|
|
198
|
+
store.add(row);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
} catch {
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async #open() {
|
|
205
|
+
if (this.#db) {
|
|
206
|
+
return this.#db;
|
|
207
|
+
}
|
|
208
|
+
if (!this.#dbPromise) {
|
|
209
|
+
this.#dbPromise = openDatabase(this.#dbName, this.#storeName).then((db) => {
|
|
210
|
+
this.#db = db;
|
|
211
|
+
db.onclose = () => {
|
|
212
|
+
if (this.#db === db) {
|
|
213
|
+
this.#db = void 0;
|
|
214
|
+
this.#dbPromise = void 0;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
db.onversionchange = () => {
|
|
218
|
+
db.close();
|
|
219
|
+
};
|
|
220
|
+
return db;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return this.#dbPromise;
|
|
224
|
+
}
|
|
225
|
+
#scheduleEviction() {
|
|
226
|
+
if (this.#evictionInterval <= 0) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
this.#evictionTimer = setInterval(() => {
|
|
230
|
+
void this.#maybeEvict();
|
|
231
|
+
}, this.#evictionInterval);
|
|
232
|
+
}
|
|
233
|
+
async #maybeEvict() {
|
|
234
|
+
if (this.#closed) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const locks = globalThis.navigator?.locks;
|
|
238
|
+
if (locks?.request) {
|
|
239
|
+
try {
|
|
240
|
+
await locks.request(EVICTION_LOCK_NAME, {
|
|
241
|
+
ifAvailable: true
|
|
242
|
+
}, async (lock) => {
|
|
243
|
+
if (!lock) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
await this.#evict();
|
|
247
|
+
});
|
|
248
|
+
return;
|
|
249
|
+
} catch {
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
await this.#evict();
|
|
253
|
+
}
|
|
254
|
+
async #evict() {
|
|
255
|
+
let db;
|
|
256
|
+
try {
|
|
257
|
+
db = await this.#open();
|
|
258
|
+
} catch {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const count = await runTransaction(db, this.#storeName, "readonly", (store) => promisifyRequest(store.count()));
|
|
262
|
+
if (count <= this.#maxRecords) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const toDelete = count - this.#maxRecords;
|
|
266
|
+
await runTransaction(db, this.#storeName, "readwrite", async (store) => {
|
|
267
|
+
const cutoff = await findCutoffKey(store, toDelete);
|
|
268
|
+
if (cutoff !== void 0) {
|
|
269
|
+
store.delete(IDBKeyRange.upperBound(cutoff));
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
#installLifecycleHandlers() {
|
|
274
|
+
if (typeof globalThis.addEventListener !== "function") {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
this.#pageHideHandler = () => {
|
|
278
|
+
void this.flush();
|
|
279
|
+
};
|
|
280
|
+
this.#visibilityHandler = () => {
|
|
281
|
+
if (globalThis.document?.visibilityState === "hidden") {
|
|
282
|
+
void this.flush();
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
globalThis.addEventListener("pagehide", this.#pageHideHandler);
|
|
286
|
+
globalThis.addEventListener("visibilitychange", this.#visibilityHandler);
|
|
287
|
+
}
|
|
288
|
+
#removeLifecycleHandlers() {
|
|
289
|
+
if (typeof globalThis.removeEventListener !== "function") {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (this.#pageHideHandler) {
|
|
293
|
+
globalThis.removeEventListener("pagehide", this.#pageHideHandler);
|
|
294
|
+
this.#pageHideHandler = void 0;
|
|
295
|
+
}
|
|
296
|
+
if (this.#visibilityHandler) {
|
|
297
|
+
globalThis.removeEventListener("visibilitychange", this.#visibilityHandler);
|
|
298
|
+
this.#visibilityHandler = void 0;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
var openDatabase = (name, storeName) => {
|
|
303
|
+
return new Promise((resolve, reject) => {
|
|
304
|
+
const request = indexedDB.open(name, 1);
|
|
305
|
+
request.onupgradeneeded = () => {
|
|
306
|
+
const db = request.result;
|
|
307
|
+
if (!db.objectStoreNames.contains(storeName)) {
|
|
308
|
+
db.createObjectStore(storeName, {
|
|
309
|
+
keyPath: "seq",
|
|
310
|
+
autoIncrement: true
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
request.onsuccess = () => resolve(request.result);
|
|
315
|
+
request.onerror = () => reject(request.error);
|
|
316
|
+
request.onblocked = () => reject(new Error("IndexedDB open blocked"));
|
|
317
|
+
});
|
|
318
|
+
};
|
|
319
|
+
var promisifyRequest = (request) => new Promise((resolve, reject) => {
|
|
320
|
+
request.onsuccess = () => resolve(request.result);
|
|
321
|
+
request.onerror = () => reject(request.error);
|
|
322
|
+
});
|
|
323
|
+
var runTransaction = (db, storeName, mode, body) => {
|
|
324
|
+
return new Promise((resolve, reject) => {
|
|
325
|
+
let tx;
|
|
326
|
+
try {
|
|
327
|
+
tx = db.transaction(storeName, mode);
|
|
328
|
+
} catch (err) {
|
|
329
|
+
reject(err);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
let result;
|
|
333
|
+
let bodySettled = false;
|
|
334
|
+
let bodyError;
|
|
335
|
+
tx.oncomplete = () => {
|
|
336
|
+
if (bodyError !== void 0) {
|
|
337
|
+
reject(bodyError);
|
|
338
|
+
} else if (bodySettled) {
|
|
339
|
+
resolve(result);
|
|
340
|
+
} else {
|
|
341
|
+
reject(new Error("transaction body did not settle"));
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
tx.onerror = () => reject(tx.error);
|
|
345
|
+
tx.onabort = () => reject(tx.error ?? new Error("transaction aborted"));
|
|
346
|
+
try {
|
|
347
|
+
const store = tx.objectStore(storeName);
|
|
348
|
+
const value = body(store);
|
|
349
|
+
Promise.resolve(value).then((resolved) => {
|
|
350
|
+
result = resolved;
|
|
351
|
+
bodySettled = true;
|
|
352
|
+
}).catch((err) => {
|
|
353
|
+
bodyError = err;
|
|
354
|
+
try {
|
|
355
|
+
tx.abort();
|
|
356
|
+
} catch {
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
} catch (err) {
|
|
360
|
+
bodyError = err;
|
|
361
|
+
try {
|
|
362
|
+
tx.abort();
|
|
363
|
+
} catch {
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
};
|
|
368
|
+
var readAllLines = async (db, storeName) => {
|
|
369
|
+
return runTransaction(db, storeName, "readonly", (store) => promisifyRequest(store.getAll()).then((rows) => rows.map((row) => row.line)));
|
|
370
|
+
};
|
|
371
|
+
var findCutoffKey = (store, count) => {
|
|
372
|
+
return new Promise((resolve, reject) => {
|
|
373
|
+
if (count <= 0) {
|
|
374
|
+
resolve(void 0);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const request = store.openKeyCursor();
|
|
378
|
+
let advanced = false;
|
|
379
|
+
request.onsuccess = () => {
|
|
380
|
+
const cursor = request.result;
|
|
381
|
+
if (!cursor) {
|
|
382
|
+
resolve(void 0);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (!advanced && count > 1) {
|
|
386
|
+
advanced = true;
|
|
387
|
+
cursor.advance(count - 1);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
resolve(cursor.key);
|
|
391
|
+
};
|
|
392
|
+
request.onerror = () => reject(request.error);
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
export {
|
|
396
|
+
IdbLogStore,
|
|
397
|
+
trimJsonlToSize
|
|
398
|
+
};
|
|
399
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/idb-log-store.ts", "../../../src/trim.ts"],
|
|
4
|
+
"sourcesContent": ["//\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 { 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.\n// Adjust `maxRecords` if your workload's per-entry size differs significantly.\nconst DEFAULT_MAX_RECORDS = 150_000;\nconst DEFAULT_EVICTION_INTERVAL = 30_000;\nconst EVICTION_LOCK_NAME = '@dxos/log-store-idb:evictor';\n\n/**\n * Stored log row. The line is pre-encoded JSONL (no trailing newline).\n */\ntype LogRow = {\n /** Auto-incremented sequence (commit order). */\n seq?: number;\n /** Encoded JSONL line. */\n line: 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 records. Older rows are evicted past this. Default `150_000`,\n * sized for roughly 50 MB on disk at typical JSONL line sizes (~350 bytes average\n * observed in the Composer app).\n */\n maxRecords?: 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 flushed to IndexedDB as pre-encoded JSONL strings.\n * Multiple browsing contexts can write to the same database concurrently — IDB\n * serializes transactions, so no extra coordination is needed for writes.\n * Eviction is deduplicated across tabs via the Web Locks API (`ifAvailable`).\n *\n * Flushes are triggered by:\n * - the configured interval timer,\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 #evictionInterval: number;\n readonly #tabId: string;\n readonly #filters: LogFilter[];\n\n #queue: string[] = [];\n #flushTimer: ReturnType<typeof setTimeout> | 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.#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.#flushTimer === undefined) {\n this.#flushTimer = setTimeout(() => {\n this.#flushTimer = 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.#flushTimer !== undefined) {\n clearTimeout(this.#flushTimer);\n this.#flushTimer = 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 async export(options: { maxSize?: number } = {}): Promise<string> {\n await this.flush();\n const db = await this.#open();\n const lines = await readAllLines(db, this.#storeName);\n if (options.maxSize !== undefined && options.maxSize >= 0) {\n return trimJsonlToSize(lines, options.maxSize);\n }\n return lines.join('\\n');\n }\n\n /**\n * Discard all stored log records.\n */\n async clear(): Promise<void> {\n this.#queue = [];\n if (this.#flushTimer !== undefined) {\n clearTimeout(this.#flushTimer);\n this.#flushTimer = 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` if it has grown past.\n * Eviction runs automatically on a timer; this method is exposed for tests and for\n * 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 await runTransaction(db, this.#storeName, 'readwrite', (store) => {\n for (const line of batch) {\n const row: LogRow = { line };\n store.add(row);\n }\n });\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 const count = await runTransaction(db, this.#storeName, 'readonly', (store) => promisifyRequest(store.count()));\n if (count <= this.#maxRecords) {\n return;\n }\n\n const toDelete = count - this.#maxRecords;\n await runTransaction(db, this.#storeName, 'readwrite', async (store) => {\n const cutoff = await findCutoffKey(store, toDelete);\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, 1);\n request.onupgradeneeded = () => {\n const db = request.result;\n if (!db.objectStoreNames.contains(storeName)) {\n db.createObjectStore(storeName, { keyPath: 'seq', autoIncrement: true });\n }\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 readAllLines = async (db: IDBDatabase, storeName: string): Promise<string[]> => {\n return runTransaction(db, storeName, 'readonly', (store) =>\n promisifyRequest(store.getAll()).then((rows) => (rows as LogRow[]).map((row) => row.line)),\n );\n};\n\n/**\n * Walk the store in key order and return the key of the `count`-th entry.\n * Returns `undefined` if the store has fewer than `count` entries.\n */\nconst findCutoffKey = (store: IDBObjectStore, count: number): Promise<IDBValidKey | undefined> => {\n return new Promise((resolve, reject) => {\n if (count <= 0) {\n resolve(undefined);\n return;\n }\n const request = store.openKeyCursor();\n let advanced = false;\n request.onsuccess = () => {\n const cursor = request.result;\n if (!cursor) {\n resolve(undefined);\n return;\n }\n if (!advanced && count > 1) {\n advanced = true;\n cursor.advance(count - 1);\n return;\n }\n resolve(cursor.key);\n };\n request.onerror = () => reject(request.error);\n });\n};\n", "//\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\nconst 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"],
|
|
5
|
+
"mappings": ";;;AAIA,SAKEA,sBACAC,aACAC,kBACAC,iBACK;;;ACFA,IAAMC,kBAAkB,CAACC,OAA0BC,YAAAA;AACxD,MAAID,MAAME,WAAW,KAAKD,WAAW,GAAG;AACtC,WAAO;EACT;AAEA,QAAME,QAAQH,MAAMI,IAAI,CAACC,SAASC,eAAeD,IAAAA,CAAAA;AAGjD,MAAIE,QAAQ;AACZ,MAAIC,qBAAqBR,MAAME;AAC/B,WAASO,QAAQT,MAAME,SAAS,GAAGO,SAAS,GAAGA,SAAS;AACtD,UAAMC,YAAYP,MAAMM,KAAAA;AACxB,UAAME,WAAWJ,UAAU,IAAI,IAAI;AACnC,QAAIA,QAAQG,YAAYC,WAAWV,SAAS;AAC1C;IACF;AACAM,aAASG,YAAYC;AACrBH,yBAAqBC;EACvB;AAEA,MAAID,sBAAsBR,MAAME,QAAQ;AACtC,WAAO;EACT;AACA,SAAOF,MAAMY,MAAMJ,kBAAAA,EAAoBK,KAAK,IAAA;AAC9C;AAEA,IAAMC,cAAc,OAAOC,gBAAgB,cAAc,IAAIA,YAAAA,IAAgBC;AAE7E,IAAMV,iBAAiB,CAACW,UAAAA;AACtB,MAAIH,aAAa;AACf,WAAOA,YAAYI,OAAOD,KAAAA,EAAOf;EACnC;AAEA,SAAOe,MAAMf;AACf;;;AD5BA,IAAMiB,qBAAqB;AAC3B,IAAMC,qBAAqB;AAC3B,IAAMC,yBAAyB;AAC/B,IAAMC,2BAA2B;AAGjC,IAAMC,sBAAsB;AAC5B,IAAMC,4BAA4B;AAClC,IAAMC,qBAAqB;AAsDpB,IAAMC,cAAN,MAAMA;EACF;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAET,SAAmB,CAAA;EACnB;EACA;EACA;EACA;EACA;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F,aAAGW,MAAK;QACV,QAAQ;QAER;MACF,CAAA;IACJ,SAAST,KAAK;AACZE,kBAAYF;AACZ,UAAI;AACFF,WAAGW,MAAK;MACV,QAAQ;MAER;IACF;EACF,CAAA;AACF;AAEA,IAAMlE,eAAe,OAAOF,IAAiBzB,cAAAA;AAC3C,SAAOgC,eAAeP,IAAIzB,WAAW,YAAY,CAACiC,UAChDkB,iBAAiBlB,MAAM6D,OAAM,CAAA,EAAItD,KAAK,CAACuD,SAAUA,KAAkBC,IAAI,CAAC3D,QAAQA,IAAIxB,IAAI,CAAA,CAAA;AAE5F;AAMA,IAAMyC,gBAAgB,CAACrB,OAAuBiB,UAAAA;AAC5C,SAAO,IAAIa,QAAQ,CAACC,SAASC,WAAAA;AAC3B,QAAIf,SAAS,GAAG;AACdc,cAAQhD,MAAAA;AACR;IACF;AACA,UAAM+B,UAAUd,MAAMgE,cAAa;AACnC,QAAIC,WAAW;AACfnD,YAAQ4B,YAAY,MAAA;AAClB,YAAMwB,SAASpD,QAAQsB;AACvB,UAAI,CAAC8B,QAAQ;AACXnC,gBAAQhD,MAAAA;AACR;MACF;AACA,UAAI,CAACkF,YAAYhD,QAAQ,GAAG;AAC1BgD,mBAAW;AACXC,eAAOC,QAAQlD,QAAQ,CAAA;AACvB;MACF;AACAc,cAAQmC,OAAOE,GAAG;IACpB;AACAtD,YAAQ6B,UAAU,MAAMX,OAAOlB,QAAQ8B,KAAK;EAC9C,CAAA;AACF;",
|
|
6
|
+
"names": ["inferEnvironmentName", "parseFilter", "serializeToJsonl", "shouldLog", "trimJsonlToSize", "lines", "maxSize", "length", "sizes", "map", "line", "byteLengthUtf8", "total", "firstIncludedIndex", "index", "lineBytes", "sepBytes", "slice", "join", "utf8Encoder", "TextEncoder", "undefined", "value", "encode", "DEFAULT_STORE_NAME", "DEFAULT_LOG_FILTER", "DEFAULT_FLUSH_INTERVAL", "DEFAULT_FLUSH_BATCH_SIZE", "DEFAULT_MAX_RECORDS", "DEFAULT_EVICTION_INTERVAL", "EVICTION_LOCK_NAME", "IdbLogStore", "options", "dbName", "storeName", "flushInterval", "flushBatchSize", "maxRecords", "evictionInterval", "tabId", "inferEnvironmentName", "parseFilter", "logFilter", "processor", "_config", "entry", "shouldLog", "line", "serializeToJsonl", "env", "undefined", "push", "length", "flush", "setTimeout", "clearTimeout", "batch", "finally", "export", "db", "lines", "readAllLines", "maxSize", "trimJsonlToSize", "join", "clear", "runTransaction", "store", "evictNow", "close", "clearInterval", "row", "add", "openDatabase", "then", "onclose", "onversionchange", "setInterval", "locks", "globalThis", "navigator", "request", "ifAvailable", "lock", "count", "promisifyRequest", "toDelete", "cutoff", "findCutoffKey", "delete", "IDBKeyRange", "upperBound", "addEventListener", "document", "visibilityState", "removeEventListener", "name", "Promise", "resolve", "reject", "indexedDB", "open", "onupgradeneeded", "result", "objectStoreNames", "contains", "createObjectStore", "keyPath", "autoIncrement", "onsuccess", "onerror", "error", "onblocked", "Error", "mode", "body", "tx", "transaction", "err", "bodySettled", "bodyError", "oncomplete", "onabort", "objectStore", "value", "resolved", "catch", "abort", "getAll", "rows", "map", "openKeyCursor", "advanced", "cursor", "advance", "key"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"src/trim.ts":{"bytes":4813,"imports":[],"format":"esm"},"src/idb-log-store.ts":{"bytes":44380,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"src/trim.ts","kind":"import-statement","original":"./trim"}],"format":"esm"},"src/index.ts":{"bytes":469,"imports":[{"path":"src/idb-log-store.ts","kind":"import-statement","original":"./idb-log-store"},{"path":"src/trim.ts","kind":"import-statement","original":"./trim"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":24519},"dist/lib/node-esm/index.mjs":{"imports":[{"path":"@dxos/log","kind":"import-statement","external":true}],"exports":["IdbLogStore","trimJsonlToSize"],"entryPoint":"src/index.ts","inputs":{"src/idb-log-store.ts":{"bytesInOutput":9770},"src/trim.ts":{"bytesInOutput":834},"src/index.ts":{"bytesInOutput":0}},"bytes":10842}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idb-log-store.browser.test.d.ts","sourceRoot":"","sources":["../../../src/idb-log-store.browser.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type LogProcessor } from '@dxos/log';
|
|
2
|
+
export type IdbLogStoreOptions = {
|
|
3
|
+
/** Name of the IndexedDB database. */
|
|
4
|
+
dbName: string;
|
|
5
|
+
/** Object store name. Default `'logs'`. */
|
|
6
|
+
storeName?: string;
|
|
7
|
+
/** Auto-flush interval in milliseconds. Default `250`. */
|
|
8
|
+
flushInterval?: number;
|
|
9
|
+
/** Auto-flush when in-memory queue reaches this size. Default `500`. */
|
|
10
|
+
flushBatchSize?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Soft cap on retained records. Older rows are evicted past this. Default `150_000`,
|
|
13
|
+
* sized for roughly 50 MB on disk at typical JSONL line sizes (~350 bytes average
|
|
14
|
+
* observed in the Composer app).
|
|
15
|
+
*/
|
|
16
|
+
maxRecords?: number;
|
|
17
|
+
/** Eviction sweep interval in milliseconds. Default `30_000`. */
|
|
18
|
+
evictionInterval?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Identifier embedded in every record's `i` field.
|
|
21
|
+
* Defaults to a scope-aware id of the form `<scope>:<name>:<suffix>` — see {@link inferEnvironmentName}.
|
|
22
|
+
*/
|
|
23
|
+
tabId?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Same syntax as `DX_LOG` / {@link parseFilter} (e.g. `debug`, `info`, `pattern:warn`).
|
|
26
|
+
* Entries below the minimum level are not stored. Default `debug` (TRACE is excluded).
|
|
27
|
+
*/
|
|
28
|
+
logFilter?: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* IndexedDB-backed log store.
|
|
32
|
+
*
|
|
33
|
+
* Logs are batched in memory and flushed to IndexedDB as pre-encoded JSONL strings.
|
|
34
|
+
* Multiple browsing contexts can write to the same database concurrently — IDB
|
|
35
|
+
* serializes transactions, so no extra coordination is needed for writes.
|
|
36
|
+
* Eviction is deduplicated across tabs via the Web Locks API (`ifAvailable`).
|
|
37
|
+
*
|
|
38
|
+
* Flushes are triggered by:
|
|
39
|
+
* - the configured interval timer,
|
|
40
|
+
* - the queue exceeding `flushBatchSize`,
|
|
41
|
+
* - `visibilitychange` to `hidden` and `pagehide` events.
|
|
42
|
+
*/
|
|
43
|
+
export declare class IdbLogStore {
|
|
44
|
+
#private;
|
|
45
|
+
constructor(options: IdbLogStoreOptions);
|
|
46
|
+
/**
|
|
47
|
+
* Log processor — register via `log.addProcessor(store.processor)`.
|
|
48
|
+
*/
|
|
49
|
+
readonly processor: LogProcessor;
|
|
50
|
+
/**
|
|
51
|
+
* Force a flush now and resolve once the IDB transaction commits.
|
|
52
|
+
* Concurrent calls share a single in-flight flush.
|
|
53
|
+
*/
|
|
54
|
+
flush(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Read all retained log lines as a JSONL string.
|
|
57
|
+
*
|
|
58
|
+
* If `maxSize` is provided (in bytes), the newest lines are kept and older lines
|
|
59
|
+
* are dropped from the front; lines are never split.
|
|
60
|
+
*/
|
|
61
|
+
export(options?: {
|
|
62
|
+
maxSize?: number;
|
|
63
|
+
}): Promise<string>;
|
|
64
|
+
/**
|
|
65
|
+
* Discard all stored log records.
|
|
66
|
+
*/
|
|
67
|
+
clear(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Run an eviction sweep now: trim the store down to `maxRecords` if it has grown past.
|
|
70
|
+
* Eviction runs automatically on a timer; this method is exposed for tests and for
|
|
71
|
+
* callers that want to force a sweep at a particular moment.
|
|
72
|
+
*/
|
|
73
|
+
evictNow(): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Stop timers, flush remaining entries, and close the database connection.
|
|
76
|
+
*/
|
|
77
|
+
close(): Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=idb-log-store.d.ts.map
|
|
@@ -0,0 +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;AAwBnB,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;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,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;;;;;;;;;;;;GAYG;AACH,qBAAa,WAAW;;IAoBtB,YAAY,OAAO,EAAE,kBAAkB,EAYtC;IAED;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,YAAY,CAqB9B;IAEF;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAmB3B;IAED;;;;;OAKG;IACG,MAAM,CAAC,OAAO,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAQhE;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;CAkIF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,iBAAiB,CAAC;AAChC,cAAc,QAAQ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trim a list of pre-encoded JSONL lines so the joined output (with `\n` separators)
|
|
3
|
+
* fits within `maxSize` bytes (UTF-8). The newest lines (end of array) are preferred —
|
|
4
|
+
* lines are dropped from the start. Never cuts inside a line.
|
|
5
|
+
*
|
|
6
|
+
* Returns the trimmed JSONL string (no trailing newline).
|
|
7
|
+
*/
|
|
8
|
+
export declare const trimJsonlToSize: (lines: readonly string[], maxSize: number) => string;
|
|
9
|
+
//# sourceMappingURL=trim.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trim.test.d.ts","sourceRoot":"","sources":["../../../src/trim.test.ts"],"names":[],"mappings":""}
|