@rigxyz/tapd 0.4.1 → 0.5.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.
@@ -293,9 +293,8 @@ function joinUrl(base, suffix) {
293
293
  }
294
294
 
295
295
  // src/state-db.ts
296
- import { mkdirSync as mkdirSync2 } from "fs";
296
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2, renameSync } from "fs";
297
297
  import { dirname as dirname2 } from "path";
298
- import Database from "better-sqlite3";
299
298
  var CURSOR_PREFIX = "chg_";
300
299
  function cursorGreaterThan(a, b) {
301
300
  const an = a.startsWith(CURSOR_PREFIX) ? safeBigInt(a.slice(CURSOR_PREFIX.length)) : null;
@@ -311,93 +310,109 @@ function safeBigInt(s) {
311
310
  return null;
312
311
  }
313
312
  }
314
- var SCHEMA = `
315
- CREATE TABLE IF NOT EXISTS paths (
316
- path TEXT PRIMARY KEY,
317
- last_applied_change_id TEXT,
318
- last_seen_hash TEXT,
319
- local_dirty INTEGER NOT NULL DEFAULT 0,
320
- last_scan_at TEXT
321
- );
322
-
323
- CREATE TABLE IF NOT EXISTS meta (
324
- key TEXT PRIMARY KEY,
325
- value TEXT NOT NULL
326
- );
327
- `;
313
+ var pendingFlushers = /* @__PURE__ */ new Set();
314
+ var exitHookInstalled = false;
315
+ function installExitHook() {
316
+ if (exitHookInstalled) return;
317
+ exitHookInstalled = true;
318
+ process.once("exit", () => {
319
+ for (const flush of pendingFlushers) {
320
+ try {
321
+ flush();
322
+ } catch {
323
+ }
324
+ }
325
+ });
326
+ }
327
+ function loadStore(dbPath) {
328
+ try {
329
+ const parsed = JSON.parse(readFileSync2(dbPath, "utf8"));
330
+ if (parsed && parsed.version === 1 && parsed.meta && parsed.paths) {
331
+ return { version: 1, meta: parsed.meta, paths: parsed.paths };
332
+ }
333
+ } catch {
334
+ }
335
+ return { version: 1, meta: {}, paths: {} };
336
+ }
328
337
  function openStateDb(dbPath) {
329
338
  mkdirSync2(dirname2(dbPath), { recursive: true });
330
- const db = new Database(dbPath);
331
- db.pragma("journal_mode = WAL");
332
- db.exec(SCHEMA);
333
- const stmtUpsert = db.prepare(`
334
- INSERT INTO paths (path, last_applied_change_id, last_seen_hash, local_dirty, last_scan_at)
335
- VALUES (@path, @last_applied_change_id, @last_seen_hash, @local_dirty, @last_scan_at)
336
- ON CONFLICT(path) DO UPDATE SET
337
- last_applied_change_id = excluded.last_applied_change_id,
338
- last_seen_hash = excluded.last_seen_hash,
339
- local_dirty = excluded.local_dirty,
340
- last_scan_at = excluded.last_scan_at
341
- `);
342
- const stmtGet = db.prepare(`SELECT * FROM paths WHERE path = ?`);
343
- const stmtDelete = db.prepare(`DELETE FROM paths WHERE path = ?`);
344
- const stmtListPaths = db.prepare(`SELECT path FROM paths`);
345
- const stmtSetMeta = db.prepare(`
346
- INSERT INTO meta (key, value) VALUES (?, ?)
347
- ON CONFLICT(key) DO UPDATE SET value = excluded.value
348
- `);
349
- const stmtGetMeta = db.prepare(`SELECT value FROM meta WHERE key = ?`);
339
+ const store = loadStore(dbPath);
340
+ let dirty = false;
341
+ let timer = null;
342
+ const flush = () => {
343
+ if (timer) {
344
+ clearTimeout(timer);
345
+ timer = null;
346
+ }
347
+ if (!dirty) return;
348
+ const tmp = `${dbPath}.tmp`;
349
+ writeFileSync2(tmp, JSON.stringify(store), "utf8");
350
+ renameSync(tmp, dbPath);
351
+ dirty = false;
352
+ };
353
+ const schedule = () => {
354
+ dirty = true;
355
+ if (timer) return;
356
+ timer = setTimeout(flush, 100);
357
+ timer.unref?.();
358
+ };
359
+ pendingFlushers.add(flush);
360
+ installExitHook();
350
361
  return {
351
362
  getCursor() {
352
- const row = stmtGetMeta.get("cursor");
353
- return row?.value ?? "chg_0";
363
+ return store.meta.cursor ?? "chg_0";
354
364
  },
355
365
  setCursor(cursor) {
356
- const current = stmtGetMeta.get("cursor")?.value;
366
+ const current = store.meta.cursor;
357
367
  if (current && !cursorGreaterThan(cursor, current)) return;
358
- stmtSetMeta.run("cursor", cursor);
368
+ store.meta.cursor = cursor;
369
+ schedule();
359
370
  },
360
371
  upsertPath(state) {
361
- stmtUpsert.run({
362
- path: state.path,
363
- last_applied_change_id: state.lastAppliedChangeId,
364
- last_seen_hash: state.lastSeenHash,
365
- local_dirty: state.localDirty ? 1 : 0,
366
- last_scan_at: state.lastScanAt
367
- });
372
+ store.paths[state.path] = {
373
+ lastAppliedChangeId: state.lastAppliedChangeId,
374
+ lastSeenHash: state.lastSeenHash,
375
+ localDirty: state.localDirty,
376
+ lastScanAt: state.lastScanAt
377
+ };
378
+ schedule();
368
379
  },
369
380
  getPath(path) {
370
- const row = stmtGet.get(path);
371
- if (!row) return null;
381
+ const r = store.paths[path];
382
+ if (!r) return null;
372
383
  return {
373
- path: row.path,
374
- lastAppliedChangeId: row.last_applied_change_id,
375
- lastSeenHash: row.last_seen_hash,
376
- localDirty: row.local_dirty !== 0,
377
- lastScanAt: row.last_scan_at
384
+ path,
385
+ lastAppliedChangeId: r.lastAppliedChangeId,
386
+ lastSeenHash: r.lastSeenHash,
387
+ localDirty: r.localDirty,
388
+ lastScanAt: r.lastScanAt
378
389
  };
379
390
  },
380
391
  deletePath(path) {
381
- stmtDelete.run(path);
392
+ if (path in store.paths) {
393
+ delete store.paths[path];
394
+ schedule();
395
+ }
382
396
  },
383
397
  listPaths() {
384
- return stmtListPaths.all().map((r) => r.path);
398
+ return Object.keys(store.paths);
385
399
  },
386
400
  setMeta(key, value) {
387
- stmtSetMeta.run(key, value);
401
+ store.meta[key] = value;
402
+ schedule();
388
403
  },
389
404
  getMeta(key) {
390
- const row = stmtGetMeta.get(key);
391
- return row?.value ?? null;
405
+ return store.meta[key] ?? null;
392
406
  },
393
407
  close() {
394
- db.close();
408
+ flush();
409
+ pendingFlushers.delete(flush);
395
410
  }
396
411
  };
397
412
  }
398
413
 
399
414
  // src/tapignore.ts
400
- import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
415
+ import { existsSync as existsSync2, readFileSync as readFileSync3 } from "fs";
401
416
  import { join as join2 } from "path";
402
417
  import ignore from "ignore";
403
418
  var BUILTIN_IGNORE_LINES = [
@@ -433,7 +448,7 @@ function loadIgnore(rootDir) {
433
448
  ig.add(BUILTIN_IGNORE_LINES.join("\n"));
434
449
  const projectIgnore = join2(rootDir, ".tapignore");
435
450
  if (existsSync2(projectIgnore)) {
436
- ig.add(readFileSync2(projectIgnore, "utf8"));
451
+ ig.add(readFileSync3(projectIgnore, "utf8"));
437
452
  }
438
453
  return ig;
439
454
  }
@@ -568,7 +583,7 @@ async function hashFile(absPath) {
568
583
 
569
584
  // src/init.ts
570
585
  import { createHash as createHash2 } from "crypto";
571
- import { readFileSync as readFileSync3 } from "fs";
586
+ import { readFileSync as readFileSync4 } from "fs";
572
587
  import { join as join4 } from "path";
573
588
 
574
589
  // src/pool.ts
@@ -647,7 +662,7 @@ async function init(opts) {
647
662
  reused += 1;
648
663
  return;
649
664
  }
650
- const bytes = readFileSync3(join4(opts.rootDir, f.path));
665
+ const bytes = readFileSync4(join4(opts.rootDir, f.path));
651
666
  const actual = sha256OfBuffer(bytes);
652
667
  if (actual !== f.hash) {
653
668
  mutatedDuringScan.add(f.path);
@@ -818,10 +833,10 @@ import {
818
833
  existsSync as existsSync3,
819
834
  mkdirSync as mkdirSync3,
820
835
  readdirSync as readdirSync2,
821
- readFileSync as readFileSync4,
836
+ readFileSync as readFileSync5,
822
837
  realpathSync,
823
838
  statSync as statSync2,
824
- writeFileSync as writeFileSync2
839
+ writeFileSync as writeFileSync3
825
840
  } from "fs";
826
841
  import { dirname as dirname3, isAbsolute, join as join6, relative } from "path";
827
842
  var NAME_RE = /^[a-z0-9][a-z0-9_-]*$/;
@@ -907,14 +922,14 @@ var MOUNT_FILE_SUFFIX = ".local.json";
907
922
  function writeImportMount(rootDir, name, mount) {
908
923
  const path = importMountFile(rootDir, name);
909
924
  mkdirSync3(dirname3(path), { recursive: true });
910
- writeFileSync2(path, JSON.stringify(mount, null, 2) + "\n", { mode: 384 });
925
+ writeFileSync3(path, JSON.stringify(mount, null, 2) + "\n", { mode: 384 });
911
926
  }
912
927
  function readImportMount(rootDir, name) {
913
928
  const file = importMountFile(rootDir, name);
914
929
  if (!existsSync3(file)) return { ok: false, reason: "missing_token_file" };
915
930
  let parsed;
916
931
  try {
917
- parsed = JSON.parse(readFileSync4(file, "utf8"));
932
+ parsed = JSON.parse(readFileSync5(file, "utf8"));
918
933
  } catch {
919
934
  return { ok: false, reason: "malformed_json" };
920
935
  }
@@ -998,7 +1013,7 @@ function pathsOverlap(a, b) {
998
1013
  }
999
1014
 
1000
1015
  // src/import-invite.ts
1001
- import { existsSync as existsSync4, readFileSync as readFileSync5, rmSync } from "fs";
1016
+ import { existsSync as existsSync4, readFileSync as readFileSync6, rmSync } from "fs";
1002
1017
  import { dirname as dirname4, join as joinPath2 } from "path";
1003
1018
  var ImportValidationFailed = class extends Error {
1004
1019
  constructor(reason) {
@@ -1095,7 +1110,7 @@ function readRawMount(rootDir, name) {
1095
1110
  const file = importMountFile(rootDir, name);
1096
1111
  if (!existsSync4(file)) return null;
1097
1112
  try {
1098
- return JSON.parse(readFileSync5(file, "utf8"));
1113
+ return JSON.parse(readFileSync6(file, "utf8"));
1099
1114
  } catch {
1100
1115
  return null;
1101
1116
  }
@@ -1147,11 +1162,11 @@ import {
1147
1162
  lstatSync,
1148
1163
  mkdirSync as mkdirSync4,
1149
1164
  readdirSync as readdirSync3,
1150
- readFileSync as readFileSync6,
1151
- renameSync,
1165
+ readFileSync as readFileSync7,
1166
+ renameSync as renameSync2,
1152
1167
  rmdirSync,
1153
1168
  rmSync as rmSync2,
1154
- writeFileSync as writeFileSync3
1169
+ writeFileSync as writeFileSync4
1155
1170
  } from "fs";
1156
1171
  import { basename, dirname as dirname5, extname, join as join7, sep as sep2 } from "path";
1157
1172
  var STATE_KEY_CONFLICT_COUNT = "conflict_count";
@@ -1173,7 +1188,7 @@ function sha256Hex(bytes) {
1173
1188
  return createHash3("sha256").update(bytes).digest("hex");
1174
1189
  }
1175
1190
  function sha256OfFile(absPath) {
1176
- return `sha256:${sha256Hex(readFileSync6(absPath))}`;
1191
+ return `sha256:${sha256Hex(readFileSync7(absPath))}`;
1177
1192
  }
1178
1193
  function conflictSidecarPath(path, ev) {
1179
1194
  const dir = dirname5(path);
@@ -1306,8 +1321,8 @@ async function applyEvent(opts) {
1306
1321
  const sidecarAbs = join7(rootDir, sidecarPath);
1307
1322
  mkdirSync4(dirname5(sidecarAbs), { recursive: true });
1308
1323
  const tmp2 = `${sidecarAbs}.tap-tmp.${randomBytes(6).toString("hex")}`;
1309
- writeFileSync3(tmp2, bytes, { mode: ev.executable ? 493 : 420 });
1310
- renameSync(tmp2, sidecarAbs);
1324
+ writeFileSync4(tmp2, bytes, { mode: ev.executable ? 493 : 420 });
1325
+ renameSync2(tmp2, sidecarAbs);
1311
1326
  stateDb.upsertPath({
1312
1327
  path: ev.path,
1313
1328
  lastAppliedChangeId: ev.id,
@@ -1320,8 +1335,8 @@ async function applyEvent(opts) {
1320
1335
  }
1321
1336
  mkdirSync4(dirname5(abs), { recursive: true });
1322
1337
  const tmp = `${abs}.tap-tmp.${randomBytes(6).toString("hex")}`;
1323
- writeFileSync3(tmp, bytes, { mode: ev.executable ? 493 : 420 });
1324
- renameSync(tmp, abs);
1338
+ writeFileSync4(tmp, bytes, { mode: ev.executable ? 493 : 420 });
1339
+ renameSync2(tmp, abs);
1325
1340
  stateDb.upsertPath({
1326
1341
  path: ev.path,
1327
1342
  lastAppliedChangeId: ev.id,
@@ -1633,7 +1648,7 @@ async function startSubscription(opts) {
1633
1648
 
1634
1649
  // src/uploader.ts
1635
1650
  import { createHash as createHash4 } from "crypto";
1636
- import { readFileSync as readFileSync7 } from "fs";
1651
+ import { readFileSync as readFileSync8 } from "fs";
1637
1652
  import { join as join8 } from "path";
1638
1653
  var UPLOAD_CONCURRENCY2 = 8;
1639
1654
  function sha256OfBuffer2(bytes) {
@@ -1713,7 +1728,7 @@ async function scanAndPush(opts) {
1713
1728
  reused += 1;
1714
1729
  return;
1715
1730
  }
1716
- const bytes = readFileSync7(join8(rootDir, f.path));
1731
+ const bytes = readFileSync8(join8(rootDir, f.path));
1717
1732
  const actual = sha256OfBuffer2(bytes);
1718
1733
  if (actual !== f.hash) {
1719
1734
  mutatedPaths.add(f.path);
@@ -2022,4 +2037,4 @@ export {
2022
2037
  scanAndPush,
2023
2038
  start
2024
2039
  };
2025
- //# sourceMappingURL=chunk-JBX2ZRMD.js.map
2040
+ //# sourceMappingURL=chunk-XMYPUAYD.js.map