@lensjs/core 2.3.0 → 2.3.2

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.
Files changed (72) hide show
  1. package/dist/abstracts/store.cjs +1 -0
  2. package/dist/abstracts/store.d.cts +2 -1
  3. package/dist/abstracts/store.d.ts +2 -1
  4. package/dist/abstracts/store.js +1 -0
  5. package/dist/core/lens.cjs +36 -1
  6. package/dist/core/lens.js +36 -1
  7. package/dist/{exception-C69UCHPk.d.cts → exception-BJqUL0j5.d.cts} +1 -1
  8. package/dist/{exception-3AZsPtAg.d.ts → exception-DYe0t9-2.d.ts} +1 -1
  9. package/dist/{index-QmOJr0K-.d.cts → index-CDaOwv3y.d.cts} +1 -1
  10. package/dist/{index-CZsa0Zcm.d.ts → index-DRfxZjgs.d.ts} +1 -1
  11. package/dist/index.cjs +36 -1
  12. package/dist/index.d.cts +2 -2
  13. package/dist/index.d.ts +2 -2
  14. package/dist/index.js +36 -1
  15. package/dist/mixins/queued_store.cjs +1 -0
  16. package/dist/mixins/queued_store.js +1 -0
  17. package/dist/stores/better_sqlite.cjs +34 -0
  18. package/dist/stores/better_sqlite.d.cts +3 -0
  19. package/dist/stores/better_sqlite.d.ts +3 -0
  20. package/dist/stores/better_sqlite.js +34 -0
  21. package/dist/stores/index.cjs +35 -0
  22. package/dist/stores/index.js +35 -0
  23. package/dist/stores/queued_sqlite.cjs +35 -0
  24. package/dist/stores/queued_sqlite.js +35 -0
  25. package/dist/types/index.d.cts +2 -0
  26. package/dist/types/index.d.ts +2 -0
  27. package/dist/ui/assets/{CacheActionBadge-BB4uokI1.js → CacheActionBadge-B0d8J3l3.js} +1 -1
  28. package/dist/ui/assets/CacheEntriesTable-BWcruMt2.js +1 -0
  29. package/dist/ui/assets/CacheEntryContainer-DtJ7sOfS.js +2 -0
  30. package/dist/ui/assets/{CacheEntryDetails-BeZnoIpm.js → CacheEntryDetails-BqnO7eQt.js} +1 -1
  31. package/dist/ui/assets/CacheEntryDetailsContainer-CDSo5J6T.js +2 -0
  32. package/dist/ui/assets/ExceptionContainer-CPGWtjRd.js +2 -0
  33. package/dist/ui/assets/{ExceptionDetails-BKHzv6hf.js → ExceptionDetails-pQPj3ONT.js} +1 -1
  34. package/dist/ui/assets/ExceptionDetailsContainer-ClPIK2i9.js +2 -0
  35. package/dist/ui/assets/{ExceptionTable-DzBmQLLa.js → ExceptionTable-DOeXVqQk.js} +1 -1
  36. package/dist/ui/assets/{JsonViewer-D-KPN089.js → JsonViewer-CsEn0XaU.js} +1 -1
  37. package/dist/ui/assets/{LoadMore-CLPR6Zd4.js → LoadMore-DkgyLoiB.js} +1 -1
  38. package/dist/ui/assets/QueriesContainer-EcqQVDVf.js +2 -0
  39. package/dist/ui/assets/{QueryDetailsContainer-Cqj3E6Dr.js → QueryDetailsContainer-CEB3hlHs.js} +1 -1
  40. package/dist/ui/assets/{QueryTable-DmWdZSnJ.js → QueryTable-BE7RpCNw.js} +1 -1
  41. package/dist/ui/assets/{RequestDetails-CF338Kcv.js → RequestDetails-Gd7Ai7fu.js} +1 -1
  42. package/dist/ui/assets/{RequestDetailsContainer-aW4GLool.js → RequestDetailsContainer-CnOSYjOY.js} +2 -2
  43. package/dist/ui/assets/{RequestsContainer-DdLSvAbl.js → RequestsContainer-CEnrqMqW.js} +2 -2
  44. package/dist/ui/assets/{RequetsTable-Bdp_PhGU.js → RequetsTable-DFm_vdLV.js} +1 -1
  45. package/dist/ui/assets/{StatusCode-C605nHvd.js → StatusCode-D5gEbxbO.js} +1 -1
  46. package/dist/ui/assets/{TabbedDataViewer-ofhEq_Wj.js → TabbedDataViewer-CJN6R11V.js} +2 -2
  47. package/dist/ui/assets/{Table-kak5sL5X.js → Table-C3Y9tdtZ.js} +1 -1
  48. package/dist/ui/assets/{columns-BEyDhUNq.js → columns-5NhbdeuI.js} +1 -1
  49. package/dist/ui/assets/{columns-Bu5psHyp.js → columns-BGeHl5-C.js} +1 -1
  50. package/dist/ui/assets/{columns-BvIUTkjN.js → columns-oQrpcTou.js} +1 -1
  51. package/dist/ui/assets/{copy-DzXuP4eO.js → copy-CwmmSmiO.js} +1 -1
  52. package/dist/ui/assets/index-Bg3-m46L.js +124 -0
  53. package/dist/ui/assets/{useCacheEntries-Pvte_aNc.js → useCacheEntries-OjlomqyI.js} +1 -1
  54. package/dist/ui/assets/{useExceptions-P3cnURvN.js → useExceptions-DE7MHRvJ.js} +1 -1
  55. package/dist/ui/assets/{useLensApi-BFdsfrzR.js → useLensApi-CI65OdmI.js} +1 -1
  56. package/dist/ui/assets/{useLoadMore-JCWak1Dg.js → useLoadMore-BA_-_Tw4.js} +1 -1
  57. package/dist/ui/assets/{useQueries-CNquFtm0.js → useQueries-D29NRLfq.js} +1 -1
  58. package/dist/ui/index.html +1 -1
  59. package/dist/utils/exception.d.cts +1 -1
  60. package/dist/utils/exception.d.ts +1 -1
  61. package/dist/utils/index.cjs +1 -1
  62. package/dist/utils/index.d.cts +1 -1
  63. package/dist/utils/index.d.ts +1 -1
  64. package/dist/utils/index.js +1 -1
  65. package/package.json +1 -1
  66. package/dist/ui/assets/CacheEntriesTable-B8cUXhos.js +0 -1
  67. package/dist/ui/assets/CacheEntryContainer-WkdnGvnu.js +0 -2
  68. package/dist/ui/assets/CacheEntryDetailsContainer-DI0mEvpu.js +0 -2
  69. package/dist/ui/assets/ExceptionContainer-YNcR0F5U.js +0 -2
  70. package/dist/ui/assets/ExceptionDetailsContainer-CJHILjb3.js +0 -2
  71. package/dist/ui/assets/QueriesContainer-B_PmBkHR.js +0 -2
  72. package/dist/ui/assets/index-TW_-MgRG.js +0 -124
@@ -24,6 +24,7 @@ __export(store_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(store_exports);
26
26
  var Store = class {
27
+ storeConfig;
27
28
  constructor(...args) {
28
29
  }
29
30
  getAllExceptions(_paginationParams) {
@@ -1,8 +1,9 @@
1
- import { WatcherTypeEnum, PaginationParams, Paginator, LensEntry } from '../types/index.cjs';
1
+ import { QueuedStoreConfig, WatcherTypeEnum, PaginationParams, Paginator, LensEntry } from '../types/index.cjs';
2
2
  import 'sql-formatter';
3
3
 
4
4
  type MinimalPaginatePromise = Promise<Paginator<Omit<LensEntry, "data">[]>>;
5
5
  declare abstract class Store {
6
+ protected storeConfig?: QueuedStoreConfig;
6
7
  constructor(...args: any[]);
7
8
  abstract initialize(): Promise<void>;
8
9
  abstract save(entry: {
@@ -1,8 +1,9 @@
1
- import { WatcherTypeEnum, PaginationParams, Paginator, LensEntry } from '../types/index.js';
1
+ import { QueuedStoreConfig, WatcherTypeEnum, PaginationParams, Paginator, LensEntry } from '../types/index.js';
2
2
  import 'sql-formatter';
3
3
 
4
4
  type MinimalPaginatePromise = Promise<Paginator<Omit<LensEntry, "data">[]>>;
5
5
  declare abstract class Store {
6
+ protected storeConfig?: QueuedStoreConfig;
6
7
  constructor(...args: any[]);
7
8
  abstract initialize(): Promise<void>;
8
9
  abstract save(entry: {
@@ -1,5 +1,6 @@
1
1
  // src/abstracts/store.ts
2
2
  var Store = class {
3
+ storeConfig;
3
4
  constructor(...args) {
4
5
  }
5
6
  getAllExceptions(_paginationParams) {
@@ -204,6 +204,7 @@ var path2 = __toESM(require("path"), 1);
204
204
 
205
205
  // src/abstracts/store.ts
206
206
  var Store = class {
207
+ storeConfig;
207
208
  constructor(...args) {
208
209
  }
209
210
  getAllExceptions(_paginationParams) {
@@ -236,6 +237,8 @@ var import_crypto = require("crypto");
236
237
  var import_libsql = __toESM(require("libsql"), 1);
237
238
  var import_date = require("@lensjs/date");
238
239
  var TABLE_NAME = "lens_entries";
240
+ var BYTES_IN_GB = 1024 * 1024 * 1024;
241
+ var PRUNE_BATCH_SIZE = 1e3;
239
242
  var BetterSqliteStore = class extends Store {
240
243
  connection;
241
244
  async initialize() {
@@ -257,6 +260,7 @@ var BetterSqliteStore = class extends Store {
257
260
  lens_entry_id: entry.requestId || null,
258
261
  minimalData: this.stringifyData(entry.minimal_data ?? {})
259
262
  });
263
+ this.maybePruneDatabase();
260
264
  }
261
265
  async getAllQueries(pagination) {
262
266
  return await this.paginate("query" /* QUERY */, pagination);
@@ -332,6 +336,36 @@ var BetterSqliteStore = class extends Store {
332
336
  this.connection.exec(createIndex);
333
337
  this.connection.exec(lensEntryIdIndex);
334
338
  }
339
+ maybePruneDatabase() {
340
+ const maxGb = this.storeConfig?.dbMaxSizeGb;
341
+ const pruneGb = this.storeConfig?.dbPruneSizeGb;
342
+ if (!maxGb || !pruneGb) return;
343
+ const maxBytes = maxGb * BYTES_IN_GB;
344
+ const pruneBytes = pruneGb * BYTES_IN_GB;
345
+ if (maxBytes <= 0 || pruneBytes <= 0) return;
346
+ const targetBytes = Math.max(0, maxBytes - pruneBytes);
347
+ let usedBytes = this.getDatabaseUsedBytes();
348
+ if (usedBytes < maxBytes) return;
349
+ while (usedBytes > targetBytes) {
350
+ const deletedRows = this.deleteOldestEntries(PRUNE_BATCH_SIZE);
351
+ if (deletedRows === 0) break;
352
+ usedBytes = this.getDatabaseUsedBytes();
353
+ }
354
+ this.connection.exec("PRAGMA wal_checkpoint(TRUNCATE);");
355
+ }
356
+ getDatabaseUsedBytes() {
357
+ const pageSizeResult = this.connection.prepare("PRAGMA page_size;").get();
358
+ const pageCountResult = this.connection.prepare("PRAGMA page_count;").get();
359
+ const freelistCountResult = this.connection.prepare("PRAGMA freelist_count;").get();
360
+ const usedPages = pageCountResult.page_count - freelistCountResult.freelist_count;
361
+ return usedPages * pageSizeResult.page_size;
362
+ }
363
+ deleteOldestEntries(batchSize) {
364
+ const result = this.connection.prepare(
365
+ `DELETE FROM ${TABLE_NAME} WHERE id IN (SELECT id FROM ${TABLE_NAME} ORDER BY created_at ASC LIMIT ?)`
366
+ ).run(batchSize);
367
+ return Number(result.changes ?? 0);
368
+ }
335
369
  mapRow(row, includeFullData = true) {
336
370
  let data = includeFullData ? JSON.parse(row.data) : {};
337
371
  if (!includeFullData) {
@@ -370,6 +404,7 @@ function QueuedStore(Base) {
370
404
  constructor(...args) {
371
405
  super(...args);
372
406
  const config = args[0] || {};
407
+ this.storeConfig = config;
373
408
  this.BATCH_SIZE = config.batchSize ?? 100;
374
409
  this.PROCESS_INTERVAL_MS = config.processIntervalMs ?? 100;
375
410
  this.WARN_THRESHOLD = config.warnThreshold ?? 1e5;
@@ -452,7 +487,7 @@ var import_crypto2 = require("crypto");
452
487
  var import_url = require("url");
453
488
  var path = __toESM(require("path"), 1);
454
489
  function getMeta(metaUrl) {
455
- const isESM = typeof __dirname === "undefined";
490
+ const isESM = typeof __dirname === "undefined" || typeof __filename === "undefined";
456
491
  if (isESM) {
457
492
  if (!metaUrl) {
458
493
  throw new Error("In ESM, you must pass import.meta.url to getMeta()");
package/dist/core/lens.js CHANGED
@@ -168,6 +168,7 @@ import * as path2 from "path";
168
168
 
169
169
  // src/abstracts/store.ts
170
170
  var Store = class {
171
+ storeConfig;
171
172
  constructor(...args) {
172
173
  }
173
174
  getAllExceptions(_paginationParams) {
@@ -200,6 +201,8 @@ import { randomUUID } from "crypto";
200
201
  import Database from "libsql";
201
202
  import { nowISO } from "@lensjs/date";
202
203
  var TABLE_NAME = "lens_entries";
204
+ var BYTES_IN_GB = 1024 * 1024 * 1024;
205
+ var PRUNE_BATCH_SIZE = 1e3;
203
206
  var BetterSqliteStore = class extends Store {
204
207
  connection;
205
208
  async initialize() {
@@ -221,6 +224,7 @@ var BetterSqliteStore = class extends Store {
221
224
  lens_entry_id: entry.requestId || null,
222
225
  minimalData: this.stringifyData(entry.minimal_data ?? {})
223
226
  });
227
+ this.maybePruneDatabase();
224
228
  }
225
229
  async getAllQueries(pagination) {
226
230
  return await this.paginate("query" /* QUERY */, pagination);
@@ -296,6 +300,36 @@ var BetterSqliteStore = class extends Store {
296
300
  this.connection.exec(createIndex);
297
301
  this.connection.exec(lensEntryIdIndex);
298
302
  }
303
+ maybePruneDatabase() {
304
+ const maxGb = this.storeConfig?.dbMaxSizeGb;
305
+ const pruneGb = this.storeConfig?.dbPruneSizeGb;
306
+ if (!maxGb || !pruneGb) return;
307
+ const maxBytes = maxGb * BYTES_IN_GB;
308
+ const pruneBytes = pruneGb * BYTES_IN_GB;
309
+ if (maxBytes <= 0 || pruneBytes <= 0) return;
310
+ const targetBytes = Math.max(0, maxBytes - pruneBytes);
311
+ let usedBytes = this.getDatabaseUsedBytes();
312
+ if (usedBytes < maxBytes) return;
313
+ while (usedBytes > targetBytes) {
314
+ const deletedRows = this.deleteOldestEntries(PRUNE_BATCH_SIZE);
315
+ if (deletedRows === 0) break;
316
+ usedBytes = this.getDatabaseUsedBytes();
317
+ }
318
+ this.connection.exec("PRAGMA wal_checkpoint(TRUNCATE);");
319
+ }
320
+ getDatabaseUsedBytes() {
321
+ const pageSizeResult = this.connection.prepare("PRAGMA page_size;").get();
322
+ const pageCountResult = this.connection.prepare("PRAGMA page_count;").get();
323
+ const freelistCountResult = this.connection.prepare("PRAGMA freelist_count;").get();
324
+ const usedPages = pageCountResult.page_count - freelistCountResult.freelist_count;
325
+ return usedPages * pageSizeResult.page_size;
326
+ }
327
+ deleteOldestEntries(batchSize) {
328
+ const result = this.connection.prepare(
329
+ `DELETE FROM ${TABLE_NAME} WHERE id IN (SELECT id FROM ${TABLE_NAME} ORDER BY created_at ASC LIMIT ?)`
330
+ ).run(batchSize);
331
+ return Number(result.changes ?? 0);
332
+ }
299
333
  mapRow(row, includeFullData = true) {
300
334
  let data = includeFullData ? JSON.parse(row.data) : {};
301
335
  if (!includeFullData) {
@@ -334,6 +368,7 @@ function QueuedStore(Base) {
334
368
  constructor(...args) {
335
369
  super(...args);
336
370
  const config = args[0] || {};
371
+ this.storeConfig = config;
337
372
  this.BATCH_SIZE = config.batchSize ?? 100;
338
373
  this.PROCESS_INTERVAL_MS = config.processIntervalMs ?? 100;
339
374
  this.WARN_THRESHOLD = config.warnThreshold ?? 1e5;
@@ -416,7 +451,7 @@ import { randomUUID as randomUUID2 } from "crypto";
416
451
  import { fileURLToPath } from "url";
417
452
  import * as path from "path";
418
453
  function getMeta(metaUrl) {
419
- const isESM = typeof __dirname === "undefined";
454
+ const isESM = typeof __dirname === "undefined" || typeof __filename === "undefined";
420
455
  if (isESM) {
421
456
  if (!metaUrl) {
422
457
  throw new Error("In ESM, you must pass import.meta.url to getMeta()");
@@ -49,4 +49,4 @@ declare namespace exception {
49
49
  export { exception_cleanStack as cleanStack, exception_constructErrorObject as constructErrorObject, exception_extractCodeFrame as extractCodeFrame, exception_getFileInfo as getFileInfo, exception_getStackTrace as getStackTrace };
50
50
  }
51
51
 
52
- export { getStackTrace as a, extractCodeFrame as b, cleanStack as c, constructErrorObject as d, exception as e, getFileInfo as g };
52
+ export { constructErrorObject as a, extractCodeFrame as b, cleanStack as c, getStackTrace as d, exception as e, getFileInfo as g };
@@ -49,4 +49,4 @@ declare namespace exception {
49
49
  export { exception_cleanStack as cleanStack, exception_constructErrorObject as constructErrorObject, exception_extractCodeFrame as extractCodeFrame, exception_getFileInfo as getFileInfo, exception_getStackTrace as getStackTrace };
50
50
  }
51
51
 
52
- export { getStackTrace as a, extractCodeFrame as b, cleanStack as c, constructErrorObject as d, exception as e, getFileInfo as g };
52
+ export { constructErrorObject as a, extractCodeFrame as b, cleanStack as c, getStackTrace as d, exception as e, getFileInfo as g };
@@ -40,4 +40,4 @@ declare namespace index {
40
40
  export { index_compose as compose, index_formatSqlQuery as formatSqlQuery, index_generateRandomUuid as generateRandomUuid, index_getMeta as getMeta, index_interpolateQuery as interpolateQuery, index_isStaticFile as isStaticFile, index_normalizePath as normalizePath, index_prepareIgnoredPaths as prepareIgnoredPaths, index_prettyHrTime as prettyHrTime, index_shouldIgnoreCurrentPath as shouldIgnoreCurrentPath, index_stripBeforeAssetsPath as stripBeforeAssetsPath };
41
41
  }
42
42
 
43
- export { interpolateQuery as a, getMeta as b, isStaticFile as c, shouldIgnoreCurrentPath as d, prettyHrTime as e, formatSqlQuery as f, generateRandomUuid as g, index as i, normalizePath as n, prepareIgnoredPaths as p, stripBeforeAssetsPath as s };
43
+ export { getMeta as a, interpolateQuery as b, isStaticFile as c, prettyHrTime as d, stripBeforeAssetsPath as e, formatSqlQuery as f, generateRandomUuid as g, index as i, normalizePath as n, prepareIgnoredPaths as p, shouldIgnoreCurrentPath as s };
@@ -40,4 +40,4 @@ declare namespace index {
40
40
  export { index_compose as compose, index_formatSqlQuery as formatSqlQuery, index_generateRandomUuid as generateRandomUuid, index_getMeta as getMeta, index_interpolateQuery as interpolateQuery, index_isStaticFile as isStaticFile, index_normalizePath as normalizePath, index_prepareIgnoredPaths as prepareIgnoredPaths, index_prettyHrTime as prettyHrTime, index_shouldIgnoreCurrentPath as shouldIgnoreCurrentPath, index_stripBeforeAssetsPath as stripBeforeAssetsPath };
41
41
  }
42
42
 
43
- export { interpolateQuery as a, getMeta as b, isStaticFile as c, shouldIgnoreCurrentPath as d, prettyHrTime as e, formatSqlQuery as f, generateRandomUuid as g, index as i, normalizePath as n, prepareIgnoredPaths as p, stripBeforeAssetsPath as s };
43
+ export { getMeta as a, interpolateQuery as b, isStaticFile as c, prettyHrTime as d, stripBeforeAssetsPath as e, formatSqlQuery as f, generateRandomUuid as g, index as i, normalizePath as n, prepareIgnoredPaths as p, shouldIgnoreCurrentPath as s };
package/dist/index.cjs CHANGED
@@ -230,6 +230,7 @@ var path2 = __toESM(require("path"), 1);
230
230
 
231
231
  // src/abstracts/store.ts
232
232
  var Store = class {
233
+ storeConfig;
233
234
  constructor(...args) {
234
235
  }
235
236
  getAllExceptions(_paginationParams) {
@@ -262,6 +263,8 @@ var import_crypto = require("crypto");
262
263
  var import_libsql = __toESM(require("libsql"), 1);
263
264
  var import_date = require("@lensjs/date");
264
265
  var TABLE_NAME = "lens_entries";
266
+ var BYTES_IN_GB = 1024 * 1024 * 1024;
267
+ var PRUNE_BATCH_SIZE = 1e3;
265
268
  var BetterSqliteStore = class extends Store {
266
269
  connection;
267
270
  async initialize() {
@@ -283,6 +286,7 @@ var BetterSqliteStore = class extends Store {
283
286
  lens_entry_id: entry.requestId || null,
284
287
  minimalData: this.stringifyData(entry.minimal_data ?? {})
285
288
  });
289
+ this.maybePruneDatabase();
286
290
  }
287
291
  async getAllQueries(pagination) {
288
292
  return await this.paginate("query" /* QUERY */, pagination);
@@ -358,6 +362,36 @@ var BetterSqliteStore = class extends Store {
358
362
  this.connection.exec(createIndex);
359
363
  this.connection.exec(lensEntryIdIndex);
360
364
  }
365
+ maybePruneDatabase() {
366
+ const maxGb = this.storeConfig?.dbMaxSizeGb;
367
+ const pruneGb = this.storeConfig?.dbPruneSizeGb;
368
+ if (!maxGb || !pruneGb) return;
369
+ const maxBytes = maxGb * BYTES_IN_GB;
370
+ const pruneBytes = pruneGb * BYTES_IN_GB;
371
+ if (maxBytes <= 0 || pruneBytes <= 0) return;
372
+ const targetBytes = Math.max(0, maxBytes - pruneBytes);
373
+ let usedBytes = this.getDatabaseUsedBytes();
374
+ if (usedBytes < maxBytes) return;
375
+ while (usedBytes > targetBytes) {
376
+ const deletedRows = this.deleteOldestEntries(PRUNE_BATCH_SIZE);
377
+ if (deletedRows === 0) break;
378
+ usedBytes = this.getDatabaseUsedBytes();
379
+ }
380
+ this.connection.exec("PRAGMA wal_checkpoint(TRUNCATE);");
381
+ }
382
+ getDatabaseUsedBytes() {
383
+ const pageSizeResult = this.connection.prepare("PRAGMA page_size;").get();
384
+ const pageCountResult = this.connection.prepare("PRAGMA page_count;").get();
385
+ const freelistCountResult = this.connection.prepare("PRAGMA freelist_count;").get();
386
+ const usedPages = pageCountResult.page_count - freelistCountResult.freelist_count;
387
+ return usedPages * pageSizeResult.page_size;
388
+ }
389
+ deleteOldestEntries(batchSize) {
390
+ const result = this.connection.prepare(
391
+ `DELETE FROM ${TABLE_NAME} WHERE id IN (SELECT id FROM ${TABLE_NAME} ORDER BY created_at ASC LIMIT ?)`
392
+ ).run(batchSize);
393
+ return Number(result.changes ?? 0);
394
+ }
361
395
  mapRow(row, includeFullData = true) {
362
396
  let data = includeFullData ? JSON.parse(row.data) : {};
363
397
  if (!includeFullData) {
@@ -396,6 +430,7 @@ function QueuedStore(Base) {
396
430
  constructor(...args) {
397
431
  super(...args);
398
432
  const config = args[0] || {};
433
+ this.storeConfig = config;
399
434
  this.BATCH_SIZE = config.batchSize ?? 100;
400
435
  this.PROCESS_INTERVAL_MS = config.processIntervalMs ?? 100;
401
436
  this.WARN_THRESHOLD = config.warnThreshold ?? 1e5;
@@ -549,7 +584,7 @@ var formatSqlQuery = (query, language) => {
549
584
  });
550
585
  };
551
586
  function getMeta(metaUrl) {
552
- const isESM = typeof __dirname === "undefined";
587
+ const isESM = typeof __dirname === "undefined" || typeof __filename === "undefined";
553
588
  if (isESM) {
554
589
  if (!metaUrl) {
555
590
  throw new Error("In ESM, you must pass import.meta.url to getMeta()");
package/dist/index.d.cts CHANGED
@@ -10,8 +10,8 @@ export { default as LensAdapter } from './abstracts/adapter.cjs';
10
10
  export { default as LensStore } from './abstracts/store.cjs';
11
11
  export { default as LensWatcher } from './core/watcher.cjs';
12
12
  export { getStore as getLensStore } from './context/context.cjs';
13
- export { i as lensUtils } from './index-QmOJr0K-.cjs';
14
- export { e as lensExceptionUtils } from './exception-C69UCHPk.cjs';
13
+ export { i as lensUtils } from './index-CDaOwv3y.cjs';
14
+ export { e as lensExceptionUtils } from './exception-BJqUL0j5.cjs';
15
15
  export { createEmittery, lensEmitter } from './utils/event_emitter.cjs';
16
16
  export { handleUncaughExceptions, lensContext } from './utils/async_context.cjs';
17
17
  import 'libsql';
package/dist/index.d.ts CHANGED
@@ -10,8 +10,8 @@ export { default as LensAdapter } from './abstracts/adapter.js';
10
10
  export { default as LensStore } from './abstracts/store.js';
11
11
  export { default as LensWatcher } from './core/watcher.js';
12
12
  export { getStore as getLensStore } from './context/context.js';
13
- export { i as lensUtils } from './index-CZsa0Zcm.js';
14
- export { e as lensExceptionUtils } from './exception-3AZsPtAg.js';
13
+ export { i as lensUtils } from './index-DRfxZjgs.js';
14
+ export { e as lensExceptionUtils } from './exception-DYe0t9-2.js';
15
15
  export { createEmittery, lensEmitter } from './utils/event_emitter.js';
16
16
  export { handleUncaughExceptions, lensContext } from './utils/async_context.js';
17
17
  import 'libsql';
package/dist/index.js CHANGED
@@ -183,6 +183,7 @@ import * as path2 from "path";
183
183
 
184
184
  // src/abstracts/store.ts
185
185
  var Store = class {
186
+ storeConfig;
186
187
  constructor(...args) {
187
188
  }
188
189
  getAllExceptions(_paginationParams) {
@@ -215,6 +216,8 @@ import { randomUUID } from "crypto";
215
216
  import Database from "libsql";
216
217
  import { nowISO } from "@lensjs/date";
217
218
  var TABLE_NAME = "lens_entries";
219
+ var BYTES_IN_GB = 1024 * 1024 * 1024;
220
+ var PRUNE_BATCH_SIZE = 1e3;
218
221
  var BetterSqliteStore = class extends Store {
219
222
  connection;
220
223
  async initialize() {
@@ -236,6 +239,7 @@ var BetterSqliteStore = class extends Store {
236
239
  lens_entry_id: entry.requestId || null,
237
240
  minimalData: this.stringifyData(entry.minimal_data ?? {})
238
241
  });
242
+ this.maybePruneDatabase();
239
243
  }
240
244
  async getAllQueries(pagination) {
241
245
  return await this.paginate("query" /* QUERY */, pagination);
@@ -311,6 +315,36 @@ var BetterSqliteStore = class extends Store {
311
315
  this.connection.exec(createIndex);
312
316
  this.connection.exec(lensEntryIdIndex);
313
317
  }
318
+ maybePruneDatabase() {
319
+ const maxGb = this.storeConfig?.dbMaxSizeGb;
320
+ const pruneGb = this.storeConfig?.dbPruneSizeGb;
321
+ if (!maxGb || !pruneGb) return;
322
+ const maxBytes = maxGb * BYTES_IN_GB;
323
+ const pruneBytes = pruneGb * BYTES_IN_GB;
324
+ if (maxBytes <= 0 || pruneBytes <= 0) return;
325
+ const targetBytes = Math.max(0, maxBytes - pruneBytes);
326
+ let usedBytes = this.getDatabaseUsedBytes();
327
+ if (usedBytes < maxBytes) return;
328
+ while (usedBytes > targetBytes) {
329
+ const deletedRows = this.deleteOldestEntries(PRUNE_BATCH_SIZE);
330
+ if (deletedRows === 0) break;
331
+ usedBytes = this.getDatabaseUsedBytes();
332
+ }
333
+ this.connection.exec("PRAGMA wal_checkpoint(TRUNCATE);");
334
+ }
335
+ getDatabaseUsedBytes() {
336
+ const pageSizeResult = this.connection.prepare("PRAGMA page_size;").get();
337
+ const pageCountResult = this.connection.prepare("PRAGMA page_count;").get();
338
+ const freelistCountResult = this.connection.prepare("PRAGMA freelist_count;").get();
339
+ const usedPages = pageCountResult.page_count - freelistCountResult.freelist_count;
340
+ return usedPages * pageSizeResult.page_size;
341
+ }
342
+ deleteOldestEntries(batchSize) {
343
+ const result = this.connection.prepare(
344
+ `DELETE FROM ${TABLE_NAME} WHERE id IN (SELECT id FROM ${TABLE_NAME} ORDER BY created_at ASC LIMIT ?)`
345
+ ).run(batchSize);
346
+ return Number(result.changes ?? 0);
347
+ }
314
348
  mapRow(row, includeFullData = true) {
315
349
  let data = includeFullData ? JSON.parse(row.data) : {};
316
350
  if (!includeFullData) {
@@ -349,6 +383,7 @@ function QueuedStore(Base) {
349
383
  constructor(...args) {
350
384
  super(...args);
351
385
  const config = args[0] || {};
386
+ this.storeConfig = config;
352
387
  this.BATCH_SIZE = config.batchSize ?? 100;
353
388
  this.PROCESS_INTERVAL_MS = config.processIntervalMs ?? 100;
354
389
  this.WARN_THRESHOLD = config.warnThreshold ?? 1e5;
@@ -502,7 +537,7 @@ var formatSqlQuery = (query, language) => {
502
537
  });
503
538
  };
504
539
  function getMeta(metaUrl) {
505
- const isESM = typeof __dirname === "undefined";
540
+ const isESM = typeof __dirname === "undefined" || typeof __filename === "undefined";
506
541
  if (isESM) {
507
542
  if (!metaUrl) {
508
543
  throw new Error("In ESM, you must pass import.meta.url to getMeta()");
@@ -45,6 +45,7 @@ function QueuedStore(Base) {
45
45
  constructor(...args) {
46
46
  super(...args);
47
47
  const config = args[0] || {};
48
+ this.storeConfig = config;
48
49
  this.BATCH_SIZE = config.batchSize ?? 100;
49
50
  this.PROCESS_INTERVAL_MS = config.processIntervalMs ?? 100;
50
51
  this.WARN_THRESHOLD = config.warnThreshold ?? 1e5;
@@ -11,6 +11,7 @@ function QueuedStore(Base) {
11
11
  constructor(...args) {
12
12
  super(...args);
13
13
  const config = args[0] || {};
14
+ this.storeConfig = config;
14
15
  this.BATCH_SIZE = config.batchSize ?? 100;
15
16
  this.PROCESS_INTERVAL_MS = config.processIntervalMs ?? 100;
16
17
  this.WARN_THRESHOLD = config.warnThreshold ?? 1e5;
@@ -36,6 +36,7 @@ module.exports = __toCommonJS(better_sqlite_exports);
36
36
 
37
37
  // src/abstracts/store.ts
38
38
  var Store = class {
39
+ storeConfig;
39
40
  constructor(...args) {
40
41
  }
41
42
  getAllExceptions(_paginationParams) {
@@ -68,6 +69,8 @@ var import_crypto = require("crypto");
68
69
  var import_libsql = __toESM(require("libsql"), 1);
69
70
  var import_date = require("@lensjs/date");
70
71
  var TABLE_NAME = "lens_entries";
72
+ var BYTES_IN_GB = 1024 * 1024 * 1024;
73
+ var PRUNE_BATCH_SIZE = 1e3;
71
74
  var BetterSqliteStore = class extends Store {
72
75
  connection;
73
76
  async initialize() {
@@ -89,6 +92,7 @@ var BetterSqliteStore = class extends Store {
89
92
  lens_entry_id: entry.requestId || null,
90
93
  minimalData: this.stringifyData(entry.minimal_data ?? {})
91
94
  });
95
+ this.maybePruneDatabase();
92
96
  }
93
97
  async getAllQueries(pagination) {
94
98
  return await this.paginate("query" /* QUERY */, pagination);
@@ -164,6 +168,36 @@ var BetterSqliteStore = class extends Store {
164
168
  this.connection.exec(createIndex);
165
169
  this.connection.exec(lensEntryIdIndex);
166
170
  }
171
+ maybePruneDatabase() {
172
+ const maxGb = this.storeConfig?.dbMaxSizeGb;
173
+ const pruneGb = this.storeConfig?.dbPruneSizeGb;
174
+ if (!maxGb || !pruneGb) return;
175
+ const maxBytes = maxGb * BYTES_IN_GB;
176
+ const pruneBytes = pruneGb * BYTES_IN_GB;
177
+ if (maxBytes <= 0 || pruneBytes <= 0) return;
178
+ const targetBytes = Math.max(0, maxBytes - pruneBytes);
179
+ let usedBytes = this.getDatabaseUsedBytes();
180
+ if (usedBytes < maxBytes) return;
181
+ while (usedBytes > targetBytes) {
182
+ const deletedRows = this.deleteOldestEntries(PRUNE_BATCH_SIZE);
183
+ if (deletedRows === 0) break;
184
+ usedBytes = this.getDatabaseUsedBytes();
185
+ }
186
+ this.connection.exec("PRAGMA wal_checkpoint(TRUNCATE);");
187
+ }
188
+ getDatabaseUsedBytes() {
189
+ const pageSizeResult = this.connection.prepare("PRAGMA page_size;").get();
190
+ const pageCountResult = this.connection.prepare("PRAGMA page_count;").get();
191
+ const freelistCountResult = this.connection.prepare("PRAGMA freelist_count;").get();
192
+ const usedPages = pageCountResult.page_count - freelistCountResult.freelist_count;
193
+ return usedPages * pageSizeResult.page_size;
194
+ }
195
+ deleteOldestEntries(batchSize) {
196
+ const result = this.connection.prepare(
197
+ `DELETE FROM ${TABLE_NAME} WHERE id IN (SELECT id FROM ${TABLE_NAME} ORDER BY created_at ASC LIMIT ?)`
198
+ ).run(batchSize);
199
+ return Number(result.changes ?? 0);
200
+ }
167
201
  mapRow(row, includeFullData = true) {
168
202
  let data = includeFullData ? JSON.parse(row.data) : {};
169
203
  if (!includeFullData) {
@@ -59,6 +59,9 @@ declare class BetterSqliteStore extends Store {
59
59
  count(type: WatcherTypeEnum): Promise<number>;
60
60
  find(type: WatcherTypeEnum, id: string): Promise<LensEntry | null>;
61
61
  private setupSchema;
62
+ private maybePruneDatabase;
63
+ private getDatabaseUsedBytes;
64
+ private deleteOldestEntries;
62
65
  protected mapRow(row: any, includeFullData?: boolean): LensEntry;
63
66
  protected mapRows(rows: any[], includeFullData?: boolean): LensEntry[];
64
67
  protected getSelectedColumns(includeFullData?: boolean): string;
@@ -59,6 +59,9 @@ declare class BetterSqliteStore extends Store {
59
59
  count(type: WatcherTypeEnum): Promise<number>;
60
60
  find(type: WatcherTypeEnum, id: string): Promise<LensEntry | null>;
61
61
  private setupSchema;
62
+ private maybePruneDatabase;
63
+ private getDatabaseUsedBytes;
64
+ private deleteOldestEntries;
62
65
  protected mapRow(row: any, includeFullData?: boolean): LensEntry;
63
66
  protected mapRows(rows: any[], includeFullData?: boolean): LensEntry[];
64
67
  protected getSelectedColumns(includeFullData?: boolean): string;
@@ -1,5 +1,6 @@
1
1
  // src/abstracts/store.ts
2
2
  var Store = class {
3
+ storeConfig;
3
4
  constructor(...args) {
4
5
  }
5
6
  getAllExceptions(_paginationParams) {
@@ -32,6 +33,8 @@ import { randomUUID } from "crypto";
32
33
  import Database from "libsql";
33
34
  import { nowISO } from "@lensjs/date";
34
35
  var TABLE_NAME = "lens_entries";
36
+ var BYTES_IN_GB = 1024 * 1024 * 1024;
37
+ var PRUNE_BATCH_SIZE = 1e3;
35
38
  var BetterSqliteStore = class extends Store {
36
39
  connection;
37
40
  async initialize() {
@@ -53,6 +56,7 @@ var BetterSqliteStore = class extends Store {
53
56
  lens_entry_id: entry.requestId || null,
54
57
  minimalData: this.stringifyData(entry.minimal_data ?? {})
55
58
  });
59
+ this.maybePruneDatabase();
56
60
  }
57
61
  async getAllQueries(pagination) {
58
62
  return await this.paginate("query" /* QUERY */, pagination);
@@ -128,6 +132,36 @@ var BetterSqliteStore = class extends Store {
128
132
  this.connection.exec(createIndex);
129
133
  this.connection.exec(lensEntryIdIndex);
130
134
  }
135
+ maybePruneDatabase() {
136
+ const maxGb = this.storeConfig?.dbMaxSizeGb;
137
+ const pruneGb = this.storeConfig?.dbPruneSizeGb;
138
+ if (!maxGb || !pruneGb) return;
139
+ const maxBytes = maxGb * BYTES_IN_GB;
140
+ const pruneBytes = pruneGb * BYTES_IN_GB;
141
+ if (maxBytes <= 0 || pruneBytes <= 0) return;
142
+ const targetBytes = Math.max(0, maxBytes - pruneBytes);
143
+ let usedBytes = this.getDatabaseUsedBytes();
144
+ if (usedBytes < maxBytes) return;
145
+ while (usedBytes > targetBytes) {
146
+ const deletedRows = this.deleteOldestEntries(PRUNE_BATCH_SIZE);
147
+ if (deletedRows === 0) break;
148
+ usedBytes = this.getDatabaseUsedBytes();
149
+ }
150
+ this.connection.exec("PRAGMA wal_checkpoint(TRUNCATE);");
151
+ }
152
+ getDatabaseUsedBytes() {
153
+ const pageSizeResult = this.connection.prepare("PRAGMA page_size;").get();
154
+ const pageCountResult = this.connection.prepare("PRAGMA page_count;").get();
155
+ const freelistCountResult = this.connection.prepare("PRAGMA freelist_count;").get();
156
+ const usedPages = pageCountResult.page_count - freelistCountResult.freelist_count;
157
+ return usedPages * pageSizeResult.page_size;
158
+ }
159
+ deleteOldestEntries(batchSize) {
160
+ const result = this.connection.prepare(
161
+ `DELETE FROM ${TABLE_NAME} WHERE id IN (SELECT id FROM ${TABLE_NAME} ORDER BY created_at ASC LIMIT ?)`
162
+ ).run(batchSize);
163
+ return Number(result.changes ?? 0);
164
+ }
131
165
  mapRow(row, includeFullData = true) {
132
166
  let data = includeFullData ? JSON.parse(row.data) : {};
133
167
  if (!includeFullData) {
@@ -37,6 +37,7 @@ module.exports = __toCommonJS(stores_exports);
37
37
 
38
38
  // src/abstracts/store.ts
39
39
  var Store = class {
40
+ storeConfig;
40
41
  constructor(...args) {
41
42
  }
42
43
  getAllExceptions(_paginationParams) {
@@ -69,6 +70,8 @@ var import_crypto = require("crypto");
69
70
  var import_libsql = __toESM(require("libsql"), 1);
70
71
  var import_date = require("@lensjs/date");
71
72
  var TABLE_NAME = "lens_entries";
73
+ var BYTES_IN_GB = 1024 * 1024 * 1024;
74
+ var PRUNE_BATCH_SIZE = 1e3;
72
75
  var BetterSqliteStore = class extends Store {
73
76
  connection;
74
77
  async initialize() {
@@ -90,6 +93,7 @@ var BetterSqliteStore = class extends Store {
90
93
  lens_entry_id: entry.requestId || null,
91
94
  minimalData: this.stringifyData(entry.minimal_data ?? {})
92
95
  });
96
+ this.maybePruneDatabase();
93
97
  }
94
98
  async getAllQueries(pagination) {
95
99
  return await this.paginate("query" /* QUERY */, pagination);
@@ -165,6 +169,36 @@ var BetterSqliteStore = class extends Store {
165
169
  this.connection.exec(createIndex);
166
170
  this.connection.exec(lensEntryIdIndex);
167
171
  }
172
+ maybePruneDatabase() {
173
+ const maxGb = this.storeConfig?.dbMaxSizeGb;
174
+ const pruneGb = this.storeConfig?.dbPruneSizeGb;
175
+ if (!maxGb || !pruneGb) return;
176
+ const maxBytes = maxGb * BYTES_IN_GB;
177
+ const pruneBytes = pruneGb * BYTES_IN_GB;
178
+ if (maxBytes <= 0 || pruneBytes <= 0) return;
179
+ const targetBytes = Math.max(0, maxBytes - pruneBytes);
180
+ let usedBytes = this.getDatabaseUsedBytes();
181
+ if (usedBytes < maxBytes) return;
182
+ while (usedBytes > targetBytes) {
183
+ const deletedRows = this.deleteOldestEntries(PRUNE_BATCH_SIZE);
184
+ if (deletedRows === 0) break;
185
+ usedBytes = this.getDatabaseUsedBytes();
186
+ }
187
+ this.connection.exec("PRAGMA wal_checkpoint(TRUNCATE);");
188
+ }
189
+ getDatabaseUsedBytes() {
190
+ const pageSizeResult = this.connection.prepare("PRAGMA page_size;").get();
191
+ const pageCountResult = this.connection.prepare("PRAGMA page_count;").get();
192
+ const freelistCountResult = this.connection.prepare("PRAGMA freelist_count;").get();
193
+ const usedPages = pageCountResult.page_count - freelistCountResult.freelist_count;
194
+ return usedPages * pageSizeResult.page_size;
195
+ }
196
+ deleteOldestEntries(batchSize) {
197
+ const result = this.connection.prepare(
198
+ `DELETE FROM ${TABLE_NAME} WHERE id IN (SELECT id FROM ${TABLE_NAME} ORDER BY created_at ASC LIMIT ?)`
199
+ ).run(batchSize);
200
+ return Number(result.changes ?? 0);
201
+ }
168
202
  mapRow(row, includeFullData = true) {
169
203
  let data = includeFullData ? JSON.parse(row.data) : {};
170
204
  if (!includeFullData) {
@@ -203,6 +237,7 @@ function QueuedStore(Base) {
203
237
  constructor(...args) {
204
238
  super(...args);
205
239
  const config = args[0] || {};
240
+ this.storeConfig = config;
206
241
  this.BATCH_SIZE = config.batchSize ?? 100;
207
242
  this.PROCESS_INTERVAL_MS = config.processIntervalMs ?? 100;
208
243
  this.WARN_THRESHOLD = config.warnThreshold ?? 1e5;