ahmed-rivet-convex 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.
- package/dist/{chunk-BGJEHD4Y.js → chunk-3EUUY2JG.js} +343 -85
- package/dist/{chunk-FVFX5V7B.js → chunk-STMINOLV.js} +5 -0
- package/dist/{lazy-inspector-HSAL7IXV.js → lazy-inspector-IZ6ERJRP.js} +1 -1
- package/dist/react.js +2 -2
- package/dist/replicache/src/kv/opfs-store.d.ts +9 -5
- package/dist/zero.js +2 -2
- package/package.json +1 -1
|
@@ -139,7 +139,7 @@ import {
|
|
|
139
139
|
withWrite,
|
|
140
140
|
withWriteNoImplicitCommit,
|
|
141
141
|
wrapIterable
|
|
142
|
-
} from "./chunk-
|
|
142
|
+
} from "./chunk-STMINOLV.js";
|
|
143
143
|
|
|
144
144
|
// ../zero-schema/src/builder/table-builder.ts
|
|
145
145
|
function table(name) {
|
|
@@ -3357,11 +3357,13 @@ function dropIDBStore(name) {
|
|
|
3357
3357
|
// ../replicache/src/kv/opfs-store.ts
|
|
3358
3358
|
import { RWLock as RWLock3 } from "@rocicorp/lock";
|
|
3359
3359
|
var COMPACT_MIN_BYTES = 2 * 1024 * 1024;
|
|
3360
|
+
var COMPACT_RUNTIME_MIN_BYTES = 4 * 1024 * 1024;
|
|
3361
|
+
var COMPACT_MIN_FRAMES = 64;
|
|
3360
3362
|
var stores2 = /* @__PURE__ */ new Map();
|
|
3361
3363
|
function isOPFSSupported() {
|
|
3362
3364
|
return typeof Worker !== "undefined" && typeof Blob !== "undefined" && typeof URL !== "undefined" && typeof URL.createObjectURL === "function" && typeof navigator !== "undefined" && typeof navigator.storage?.getDirectory === "function";
|
|
3363
3365
|
}
|
|
3364
|
-
function
|
|
3366
|
+
function sanitizeOPFSFileName(name) {
|
|
3365
3367
|
let h = 2166136261;
|
|
3366
3368
|
for (let i = 0; i < name.length; i++) {
|
|
3367
3369
|
h ^= name.charCodeAt(i);
|
|
@@ -3370,20 +3372,69 @@ function sanitizeFileName(name) {
|
|
|
3370
3372
|
const safe = name.replace(/[^a-zA-Z0-9._-]/g, "_").slice(0, 96);
|
|
3371
3373
|
return `${safe}_${(h >>> 0).toString(36)}.rplog`;
|
|
3372
3374
|
}
|
|
3375
|
+
function acquireCrossTabLock(lockName, mode) {
|
|
3376
|
+
if (typeof navigator === "undefined" || !navigator.locks || typeof navigator.locks.request !== "function") {
|
|
3377
|
+
return Promise.resolve(() => {
|
|
3378
|
+
});
|
|
3379
|
+
}
|
|
3380
|
+
return new Promise((resolveAcquired, rejectAcquired) => {
|
|
3381
|
+
let settled = false;
|
|
3382
|
+
void navigator.locks.request(lockName, { mode }, () => {
|
|
3383
|
+
return new Promise((resolveRelease) => {
|
|
3384
|
+
settled = true;
|
|
3385
|
+
resolveAcquired(resolveRelease);
|
|
3386
|
+
});
|
|
3387
|
+
}).catch((err2) => {
|
|
3388
|
+
if (!settled) rejectAcquired(err2);
|
|
3389
|
+
});
|
|
3390
|
+
});
|
|
3391
|
+
}
|
|
3373
3392
|
var OPFS_WORKER_SOURCE = `
|
|
3374
3393
|
const enc = new TextEncoder();
|
|
3375
3394
|
const dec = new TextDecoder();
|
|
3376
3395
|
const OP_COMMIT = 1;
|
|
3396
|
+
const MAGIC_0 = 0x52; // 'R'
|
|
3397
|
+
const MAGIC_1 = 0x50; // 'P'
|
|
3398
|
+
const MAGIC_2 = 0x4c; // 'L'
|
|
3399
|
+
const MAGIC_3 = 0x32; // '2'
|
|
3400
|
+
const HEADER_SIZE = 8;
|
|
3377
3401
|
const COMPACT_MIN_BYTES = ${COMPACT_MIN_BYTES};
|
|
3402
|
+
const COMPACT_RUNTIME_MIN_BYTES = ${COMPACT_RUNTIME_MIN_BYTES};
|
|
3403
|
+
const COMPACT_MIN_FRAMES = ${COMPACT_MIN_FRAMES};
|
|
3404
|
+
|
|
3405
|
+
const framesSinceCompact = new Map();
|
|
3378
3406
|
|
|
3379
|
-
async function
|
|
3407
|
+
async function withIOLock(lockName, fn) {
|
|
3380
3408
|
if (typeof navigator !== 'undefined' && navigator.locks && typeof navigator.locks.request === 'function') {
|
|
3381
|
-
return navigator.locks.request(lockName, fn);
|
|
3409
|
+
return navigator.locks.request(lockName, { mode: 'exclusive' }, fn);
|
|
3382
3410
|
}
|
|
3383
3411
|
return fn();
|
|
3384
3412
|
}
|
|
3385
3413
|
|
|
3386
|
-
function
|
|
3414
|
+
function readHeader(handle, size) {
|
|
3415
|
+
if (size < HEADER_SIZE) {
|
|
3416
|
+
return { epoch: 0, headerSize: 0 };
|
|
3417
|
+
}
|
|
3418
|
+
const hdr = new Uint8Array(HEADER_SIZE);
|
|
3419
|
+
handle.read(hdr, { at: 0 });
|
|
3420
|
+
if (hdr[0] === MAGIC_0 && hdr[1] === MAGIC_1 && hdr[2] === MAGIC_2 && hdr[3] === MAGIC_3) {
|
|
3421
|
+
const epoch = new DataView(hdr.buffer).getUint32(4, true);
|
|
3422
|
+
return { epoch, headerSize: HEADER_SIZE };
|
|
3423
|
+
}
|
|
3424
|
+
return { epoch: 0, headerSize: 0 };
|
|
3425
|
+
}
|
|
3426
|
+
|
|
3427
|
+
function makeHeader(epoch) {
|
|
3428
|
+
const hdr = new Uint8Array(HEADER_SIZE);
|
|
3429
|
+
hdr[0] = MAGIC_0;
|
|
3430
|
+
hdr[1] = MAGIC_1;
|
|
3431
|
+
hdr[2] = MAGIC_2;
|
|
3432
|
+
hdr[3] = MAGIC_3;
|
|
3433
|
+
new DataView(hdr.buffer).setUint32(4, epoch >>> 0, true);
|
|
3434
|
+
return hdr;
|
|
3435
|
+
}
|
|
3436
|
+
|
|
3437
|
+
function replayFrames(buf, size, map, deltaOps) {
|
|
3387
3438
|
const view = new DataView(buf.buffer, buf.byteOffset, size);
|
|
3388
3439
|
let pos = 0;
|
|
3389
3440
|
let frameCount = 0;
|
|
@@ -3396,12 +3447,15 @@ function replayFrames(buf, size, map) {
|
|
|
3396
3447
|
if (parsed.p) {
|
|
3397
3448
|
for (let i = 0; i < parsed.p.length; i++) {
|
|
3398
3449
|
const pair = parsed.p[i];
|
|
3399
|
-
map.set(pair[0], pair[1]);
|
|
3450
|
+
if (map) map.set(pair[0], pair[1]);
|
|
3451
|
+
if (deltaOps) deltaOps.set(pair[0], { del: false, val: pair[1] });
|
|
3400
3452
|
}
|
|
3401
3453
|
}
|
|
3402
3454
|
if (parsed.d) {
|
|
3403
3455
|
for (let i = 0; i < parsed.d.length; i++) {
|
|
3404
|
-
|
|
3456
|
+
const key = parsed.d[i];
|
|
3457
|
+
if (map) map.delete(key);
|
|
3458
|
+
if (deltaOps) deltaOps.set(key, { del: true, val: undefined });
|
|
3405
3459
|
}
|
|
3406
3460
|
}
|
|
3407
3461
|
}
|
|
@@ -3420,32 +3474,96 @@ function encodeCommitFrame(puts, dels) {
|
|
|
3420
3474
|
return frame;
|
|
3421
3475
|
}
|
|
3422
3476
|
|
|
3477
|
+
function compactHandle(handle, map, currentEpoch) {
|
|
3478
|
+
const nextEpoch = ((currentEpoch >>> 0) + 1) >>> 0 || 1;
|
|
3479
|
+
const hdr = makeHeader(nextEpoch);
|
|
3480
|
+
const snapshotFrame = encodeCommitFrame(Array.from(map.entries()), []);
|
|
3481
|
+
const total = HEADER_SIZE + snapshotFrame.byteLength;
|
|
3482
|
+
handle.truncate(0);
|
|
3483
|
+
handle.write(hdr, { at: 0 });
|
|
3484
|
+
handle.write(snapshotFrame, { at: HEADER_SIZE });
|
|
3485
|
+
handle.flush();
|
|
3486
|
+
return { epoch: nextEpoch, offset: total };
|
|
3487
|
+
}
|
|
3488
|
+
|
|
3423
3489
|
self.onmessage = async (event) => {
|
|
3424
3490
|
const msg = event.data;
|
|
3425
3491
|
try {
|
|
3426
|
-
if (msg.op === '
|
|
3427
|
-
const result = await
|
|
3492
|
+
if (msg.op === 'sync') {
|
|
3493
|
+
const result = await withIOLock('pb-opfs-io:' + msg.fileName, async () => {
|
|
3428
3494
|
const root = await navigator.storage.getDirectory();
|
|
3429
3495
|
const fh = await root.getFileHandle(msg.fileName, { create: true });
|
|
3430
3496
|
if (typeof fh.createSyncAccessHandle !== 'function') {
|
|
3431
|
-
return { supported: false
|
|
3497
|
+
return { supported: false };
|
|
3432
3498
|
}
|
|
3433
3499
|
const handle = await fh.createSyncAccessHandle();
|
|
3434
3500
|
try {
|
|
3435
3501
|
const size = handle.getSize();
|
|
3436
|
-
if (size === 0)
|
|
3437
|
-
|
|
3438
|
-
|
|
3502
|
+
if (size === 0) {
|
|
3503
|
+
if (msg.knownEpoch === 0 && msg.knownOffset === 0) {
|
|
3504
|
+
return { supported: true, changed: false, epoch: 0, offset: 0 };
|
|
3505
|
+
}
|
|
3506
|
+
return { supported: true, changed: true, full: true, epoch: 0, offset: 0, entries: [] };
|
|
3507
|
+
}
|
|
3508
|
+
const { epoch, headerSize } = readHeader(handle, size);
|
|
3509
|
+
|
|
3510
|
+
// Fast path: file unchanged since our last sync/commit.
|
|
3511
|
+
if (msg.knownEpoch === epoch && msg.knownOffset === size) {
|
|
3512
|
+
return { supported: true, changed: false, epoch, offset: size };
|
|
3513
|
+
}
|
|
3514
|
+
|
|
3515
|
+
// Incremental tail-replay path: sibling tab appended frames under the same epoch.
|
|
3516
|
+
if (
|
|
3517
|
+
msg.knownEpoch === epoch &&
|
|
3518
|
+
msg.knownOffset >= headerSize &&
|
|
3519
|
+
size > msg.knownOffset
|
|
3520
|
+
) {
|
|
3521
|
+
const tailLen = size - msg.knownOffset;
|
|
3522
|
+
const buf = new Uint8Array(tailLen);
|
|
3523
|
+
handle.read(buf, { at: msg.knownOffset });
|
|
3524
|
+
const deltaOps = new Map();
|
|
3525
|
+
const { pos } = replayFrames(buf, tailLen, null, deltaOps);
|
|
3526
|
+
const puts = [];
|
|
3527
|
+
const dels = [];
|
|
3528
|
+
deltaOps.forEach((v, k) => {
|
|
3529
|
+
if (v.del) dels.push(k);
|
|
3530
|
+
else puts.push([k, v.val]);
|
|
3531
|
+
});
|
|
3532
|
+
return {
|
|
3533
|
+
supported: true,
|
|
3534
|
+
changed: true,
|
|
3535
|
+
full: false,
|
|
3536
|
+
epoch,
|
|
3537
|
+
offset: msg.knownOffset + pos,
|
|
3538
|
+
puts,
|
|
3539
|
+
dels,
|
|
3540
|
+
};
|
|
3541
|
+
}
|
|
3542
|
+
|
|
3543
|
+
// Full replay path: first boot, legacy upgrade, or sibling tab compacted/dropped.
|
|
3544
|
+
const dataLen = size - headerSize;
|
|
3545
|
+
const buf = new Uint8Array(dataLen);
|
|
3546
|
+
if (dataLen > 0) handle.read(buf, { at: headerSize });
|
|
3439
3547
|
const map = new Map();
|
|
3440
|
-
const { frameCount } = replayFrames(buf,
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
handle
|
|
3445
|
-
|
|
3446
|
-
|
|
3548
|
+
const { pos, frameCount } = replayFrames(buf, dataLen, map, null);
|
|
3549
|
+
let finalEpoch = epoch;
|
|
3550
|
+
let finalOffset = headerSize + pos;
|
|
3551
|
+
if (size > COMPACT_MIN_BYTES && frameCount > COMPACT_MIN_FRAMES) {
|
|
3552
|
+
const compacted = compactHandle(handle, map, epoch);
|
|
3553
|
+
finalEpoch = compacted.epoch;
|
|
3554
|
+
finalOffset = compacted.offset;
|
|
3555
|
+
framesSinceCompact.set(msg.fileName, 0);
|
|
3556
|
+
} else {
|
|
3557
|
+
framesSinceCompact.set(msg.fileName, frameCount);
|
|
3447
3558
|
}
|
|
3448
|
-
return {
|
|
3559
|
+
return {
|
|
3560
|
+
supported: true,
|
|
3561
|
+
changed: true,
|
|
3562
|
+
full: true,
|
|
3563
|
+
epoch: finalEpoch,
|
|
3564
|
+
offset: finalOffset,
|
|
3565
|
+
entries: Array.from(map.entries()),
|
|
3566
|
+
};
|
|
3449
3567
|
} finally {
|
|
3450
3568
|
handle.close();
|
|
3451
3569
|
}
|
|
@@ -3455,7 +3573,7 @@ self.onmessage = async (event) => {
|
|
|
3455
3573
|
}
|
|
3456
3574
|
|
|
3457
3575
|
if (msg.op === 'commit') {
|
|
3458
|
-
const result = await
|
|
3576
|
+
const result = await withIOLock('pb-opfs-io:' + msg.fileName, async () => {
|
|
3459
3577
|
const root = await navigator.storage.getDirectory();
|
|
3460
3578
|
const fh = await root.getFileHandle(msg.fileName, { create: true });
|
|
3461
3579
|
if (typeof fh.createSyncAccessHandle !== 'function') {
|
|
@@ -3463,11 +3581,39 @@ self.onmessage = async (event) => {
|
|
|
3463
3581
|
}
|
|
3464
3582
|
const handle = await fh.createSyncAccessHandle();
|
|
3465
3583
|
try {
|
|
3466
|
-
|
|
3584
|
+
let size = handle.getSize();
|
|
3585
|
+
let epoch;
|
|
3586
|
+
let headerSize;
|
|
3587
|
+
if (size === 0) {
|
|
3588
|
+
epoch = 1;
|
|
3589
|
+
headerSize = HEADER_SIZE;
|
|
3590
|
+
handle.write(makeHeader(epoch), { at: 0 });
|
|
3591
|
+
size = HEADER_SIZE;
|
|
3592
|
+
} else {
|
|
3593
|
+
const hdr = readHeader(handle, size);
|
|
3594
|
+
epoch = hdr.epoch;
|
|
3595
|
+
headerSize = hdr.headerSize;
|
|
3596
|
+
}
|
|
3467
3597
|
const frame = encodeCommitFrame(msg.puts, msg.dels);
|
|
3468
3598
|
handle.write(frame, { at: size });
|
|
3469
3599
|
handle.flush();
|
|
3470
|
-
|
|
3600
|
+
let newSize = size + frame.byteLength;
|
|
3601
|
+
const fc = (framesSinceCompact.get(msg.fileName) || 0) + 1;
|
|
3602
|
+
framesSinceCompact.set(msg.fileName, fc);
|
|
3603
|
+
|
|
3604
|
+
// Periodic runtime compaction so long-lived tabs never grow WAL unboundedly.
|
|
3605
|
+
if (newSize > COMPACT_RUNTIME_MIN_BYTES && fc >= COMPACT_MIN_FRAMES) {
|
|
3606
|
+
const dataLen = newSize - headerSize;
|
|
3607
|
+
const buf = new Uint8Array(dataLen);
|
|
3608
|
+
if (dataLen > 0) handle.read(buf, { at: headerSize });
|
|
3609
|
+
const map = new Map();
|
|
3610
|
+
replayFrames(buf, dataLen, map, null);
|
|
3611
|
+
const compacted = compactHandle(handle, map, epoch);
|
|
3612
|
+
epoch = compacted.epoch;
|
|
3613
|
+
newSize = compacted.offset;
|
|
3614
|
+
framesSinceCompact.set(msg.fileName, 0);
|
|
3615
|
+
}
|
|
3616
|
+
return { supported: true, epoch, offset: newSize };
|
|
3471
3617
|
} finally {
|
|
3472
3618
|
handle.close();
|
|
3473
3619
|
}
|
|
@@ -3477,8 +3623,9 @@ self.onmessage = async (event) => {
|
|
|
3477
3623
|
}
|
|
3478
3624
|
|
|
3479
3625
|
if (msg.op === 'drop') {
|
|
3480
|
-
await
|
|
3626
|
+
await withIOLock('pb-opfs-io:' + msg.fileName, async () => {
|
|
3481
3627
|
const root = await navigator.storage.getDirectory();
|
|
3628
|
+
framesSinceCompact.delete(msg.fileName);
|
|
3482
3629
|
try {
|
|
3483
3630
|
await root.removeEntry(msg.fileName);
|
|
3484
3631
|
} catch {
|
|
@@ -3567,14 +3714,12 @@ var OPFSReadImpl = class extends ReadImpl3 {
|
|
|
3567
3714
|
}
|
|
3568
3715
|
};
|
|
3569
3716
|
var OPFSWriteImpl = class extends WriteImplBase {
|
|
3570
|
-
#
|
|
3717
|
+
#entry;
|
|
3571
3718
|
#fileName;
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
this.#map = map;
|
|
3719
|
+
constructor(entry, release, fileName) {
|
|
3720
|
+
super(new OPFSReadImpl(entry.map, release));
|
|
3721
|
+
this.#entry = entry;
|
|
3576
3722
|
this.#fileName = fileName;
|
|
3577
|
-
this.#worker = worker;
|
|
3578
3723
|
}
|
|
3579
3724
|
async commit() {
|
|
3580
3725
|
if (this.closed) {
|
|
@@ -3584,22 +3729,32 @@ var OPFSWriteImpl = class extends WriteImplBase {
|
|
|
3584
3729
|
const dels = [];
|
|
3585
3730
|
this._pending.forEach((value, key) => {
|
|
3586
3731
|
if (value === deleteSentinel) {
|
|
3587
|
-
this.#map.delete(key);
|
|
3588
3732
|
dels.push(key);
|
|
3589
3733
|
} else {
|
|
3590
|
-
this.#map.set(key, value);
|
|
3591
3734
|
puts.push([key, value]);
|
|
3592
3735
|
}
|
|
3593
3736
|
});
|
|
3594
3737
|
this._pending.clear();
|
|
3595
3738
|
try {
|
|
3596
3739
|
if (puts.length > 0 || dels.length > 0) {
|
|
3597
|
-
await this.#worker.call({
|
|
3740
|
+
const res = await this.#entry.worker.call({
|
|
3598
3741
|
op: "commit",
|
|
3599
3742
|
fileName: this.#fileName,
|
|
3600
3743
|
puts,
|
|
3601
3744
|
dels
|
|
3602
3745
|
});
|
|
3746
|
+
if (!res.supported) {
|
|
3747
|
+
throw new Error("OPFS SyncAccessHandle unavailable during commit");
|
|
3748
|
+
}
|
|
3749
|
+
for (let i = 0; i < puts.length; i++) {
|
|
3750
|
+
const pair = puts[i];
|
|
3751
|
+
this.#entry.map.set(pair[0], pair[1]);
|
|
3752
|
+
}
|
|
3753
|
+
for (let i = 0; i < dels.length; i++) {
|
|
3754
|
+
this.#entry.map.delete(dels[i]);
|
|
3755
|
+
}
|
|
3756
|
+
this.#entry.epoch = res.epoch;
|
|
3757
|
+
this.#entry.offset = res.offset;
|
|
3603
3758
|
}
|
|
3604
3759
|
} finally {
|
|
3605
3760
|
this.release();
|
|
@@ -3609,21 +3764,26 @@ var OPFSWriteImpl = class extends WriteImplBase {
|
|
|
3609
3764
|
var OPFSStore = class {
|
|
3610
3765
|
#name;
|
|
3611
3766
|
#fileName;
|
|
3767
|
+
#txLockName;
|
|
3612
3768
|
#lc;
|
|
3613
3769
|
#entry;
|
|
3614
3770
|
#closed = false;
|
|
3615
3771
|
constructor(lc, name) {
|
|
3616
3772
|
this.#lc = lc;
|
|
3617
3773
|
this.#name = name;
|
|
3618
|
-
this.#fileName =
|
|
3774
|
+
this.#fileName = sanitizeOPFSFileName(name);
|
|
3775
|
+
this.#txLockName = `pb-opfs-tx:${this.#fileName}`;
|
|
3619
3776
|
let entry = stores2.get(name);
|
|
3620
3777
|
if (!entry) {
|
|
3621
3778
|
entry = {
|
|
3622
3779
|
lock: new RWLock3(),
|
|
3623
3780
|
map: /* @__PURE__ */ new Map(),
|
|
3624
3781
|
worker: getSharedWorkerClient(),
|
|
3625
|
-
|
|
3626
|
-
|
|
3782
|
+
epoch: -1,
|
|
3783
|
+
offset: 0,
|
|
3784
|
+
activeReads: 0,
|
|
3785
|
+
sharedLockRelease: null,
|
|
3786
|
+
readSyncPromise: null,
|
|
3627
3787
|
opfsAvailable: isOPFSSupported(),
|
|
3628
3788
|
fallbackStore: null
|
|
3629
3789
|
};
|
|
@@ -3631,40 +3791,48 @@ var OPFSStore = class {
|
|
|
3631
3791
|
}
|
|
3632
3792
|
this.#entry = entry;
|
|
3633
3793
|
}
|
|
3634
|
-
|
|
3794
|
+
/**
|
|
3795
|
+
* Synchronize `this.#entry.map` with the latest durable OPFS state.
|
|
3796
|
+
* Caller MUST already hold both the in-tab lock and cross-tab `txLockName`.
|
|
3797
|
+
*/
|
|
3798
|
+
async #syncLocked() {
|
|
3635
3799
|
if (!this.#entry.opfsAvailable) return false;
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
if (res.entries) {
|
|
3800
|
+
try {
|
|
3801
|
+
const res = await this.#entry.worker.call({
|
|
3802
|
+
op: "sync",
|
|
3803
|
+
fileName: this.#fileName,
|
|
3804
|
+
knownEpoch: this.#entry.epoch,
|
|
3805
|
+
knownOffset: this.#entry.offset
|
|
3806
|
+
});
|
|
3807
|
+
if (!res.supported) {
|
|
3808
|
+
this.#entry.opfsAvailable = false;
|
|
3809
|
+
return false;
|
|
3810
|
+
}
|
|
3811
|
+
if (res.changed) {
|
|
3812
|
+
if (res.full) {
|
|
3813
|
+
this.#entry.map.clear();
|
|
3651
3814
|
for (let i = 0; i < res.entries.length; i++) {
|
|
3652
3815
|
const pair = res.entries[i];
|
|
3653
3816
|
this.#entry.map.set(pair[0], pair[1]);
|
|
3654
3817
|
}
|
|
3818
|
+
} else {
|
|
3819
|
+
for (let i = 0; i < res.puts.length; i++) {
|
|
3820
|
+
const pair = res.puts[i];
|
|
3821
|
+
this.#entry.map.set(pair[0], pair[1]);
|
|
3822
|
+
}
|
|
3823
|
+
for (let i = 0; i < res.dels.length; i++) {
|
|
3824
|
+
this.#entry.map.delete(res.dels[i]);
|
|
3825
|
+
}
|
|
3655
3826
|
}
|
|
3656
|
-
this.#entry.
|
|
3657
|
-
|
|
3658
|
-
} catch (err2) {
|
|
3659
|
-
this.#lc.debug?.("OPFSStore falling back to IDB:", err2);
|
|
3660
|
-
this.#entry.opfsAvailable = false;
|
|
3661
|
-
return false;
|
|
3662
|
-
} finally {
|
|
3663
|
-
release();
|
|
3664
|
-
this.#entry.hydrating = null;
|
|
3827
|
+
this.#entry.epoch = res.epoch;
|
|
3828
|
+
this.#entry.offset = res.offset;
|
|
3665
3829
|
}
|
|
3666
|
-
|
|
3667
|
-
|
|
3830
|
+
return true;
|
|
3831
|
+
} catch (err2) {
|
|
3832
|
+
this.#lc.debug?.("OPFSStore falling back to IDB:", err2);
|
|
3833
|
+
this.#entry.opfsAvailable = false;
|
|
3834
|
+
return false;
|
|
3835
|
+
}
|
|
3668
3836
|
}
|
|
3669
3837
|
#getFallback() {
|
|
3670
3838
|
if (!this.#entry.fallbackStore) {
|
|
@@ -3677,22 +3845,97 @@ var OPFSStore = class {
|
|
|
3677
3845
|
}
|
|
3678
3846
|
async read() {
|
|
3679
3847
|
throwIfStoreClosed(this);
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3848
|
+
if (!this.#entry.opfsAvailable) {
|
|
3849
|
+
return this.#getFallback().read();
|
|
3850
|
+
}
|
|
3851
|
+
const releaseLocal = await this.#entry.lock.read();
|
|
3852
|
+
this.#entry.activeReads++;
|
|
3853
|
+
let ok = false;
|
|
3854
|
+
try {
|
|
3855
|
+
if (this.#entry.sharedLockRelease && !this.#entry.readSyncPromise) {
|
|
3856
|
+
ok = true;
|
|
3857
|
+
} else {
|
|
3858
|
+
if (!this.#entry.readSyncPromise) {
|
|
3859
|
+
this.#entry.readSyncPromise = (async () => {
|
|
3860
|
+
const releaseCrossTab = await acquireCrossTabLock(
|
|
3861
|
+
this.#txLockName,
|
|
3862
|
+
"shared"
|
|
3863
|
+
);
|
|
3864
|
+
this.#entry.sharedLockRelease = releaseCrossTab;
|
|
3865
|
+
try {
|
|
3866
|
+
const synced = await this.#syncLocked();
|
|
3867
|
+
if (!synced) {
|
|
3868
|
+
this.#entry.sharedLockRelease = null;
|
|
3869
|
+
releaseCrossTab();
|
|
3870
|
+
}
|
|
3871
|
+
return synced;
|
|
3872
|
+
} catch (err2) {
|
|
3873
|
+
this.#entry.sharedLockRelease = null;
|
|
3874
|
+
releaseCrossTab();
|
|
3875
|
+
throw err2;
|
|
3876
|
+
} finally {
|
|
3877
|
+
this.#entry.readSyncPromise = null;
|
|
3878
|
+
}
|
|
3879
|
+
})();
|
|
3880
|
+
}
|
|
3881
|
+
ok = await this.#entry.readSyncPromise;
|
|
3882
|
+
}
|
|
3883
|
+
} catch (err2) {
|
|
3884
|
+
this.#releaseSharedReader(releaseLocal);
|
|
3885
|
+
throw err2;
|
|
3886
|
+
}
|
|
3887
|
+
if (!ok) {
|
|
3888
|
+
this.#releaseSharedReader(releaseLocal);
|
|
3889
|
+
return this.#getFallback().read();
|
|
3890
|
+
}
|
|
3891
|
+
let released = false;
|
|
3892
|
+
const release = () => {
|
|
3893
|
+
if (released) return;
|
|
3894
|
+
released = true;
|
|
3895
|
+
this.#releaseSharedReader(releaseLocal);
|
|
3896
|
+
};
|
|
3683
3897
|
return new OPFSReadImpl(this.#entry.map, release);
|
|
3684
3898
|
}
|
|
3899
|
+
#releaseSharedReader(releaseLocal) {
|
|
3900
|
+
this.#entry.activeReads--;
|
|
3901
|
+
if (this.#entry.activeReads === 0 && this.#entry.sharedLockRelease) {
|
|
3902
|
+
const releaseCrossTab = this.#entry.sharedLockRelease;
|
|
3903
|
+
this.#entry.sharedLockRelease = null;
|
|
3904
|
+
releaseCrossTab();
|
|
3905
|
+
}
|
|
3906
|
+
releaseLocal();
|
|
3907
|
+
}
|
|
3685
3908
|
async write() {
|
|
3686
3909
|
throwIfStoreClosed(this);
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
|
|
3691
|
-
|
|
3692
|
-
|
|
3693
|
-
|
|
3694
|
-
|
|
3695
|
-
|
|
3910
|
+
if (!this.#entry.opfsAvailable) {
|
|
3911
|
+
return this.#getFallback().write();
|
|
3912
|
+
}
|
|
3913
|
+
const releaseLocal = await this.#entry.lock.write();
|
|
3914
|
+
let releaseCrossTab = null;
|
|
3915
|
+
try {
|
|
3916
|
+
releaseCrossTab = await acquireCrossTabLock(
|
|
3917
|
+
this.#txLockName,
|
|
3918
|
+
"exclusive"
|
|
3919
|
+
);
|
|
3920
|
+
const ok = await this.#syncLocked();
|
|
3921
|
+
if (!ok) {
|
|
3922
|
+
releaseCrossTab();
|
|
3923
|
+
releaseLocal();
|
|
3924
|
+
return this.#getFallback().write();
|
|
3925
|
+
}
|
|
3926
|
+
} catch (err2) {
|
|
3927
|
+
releaseCrossTab?.();
|
|
3928
|
+
releaseLocal();
|
|
3929
|
+
throw err2;
|
|
3930
|
+
}
|
|
3931
|
+
let released = false;
|
|
3932
|
+
const release = () => {
|
|
3933
|
+
if (released) return;
|
|
3934
|
+
released = true;
|
|
3935
|
+
releaseCrossTab?.();
|
|
3936
|
+
releaseLocal();
|
|
3937
|
+
};
|
|
3938
|
+
return new OPFSWriteImpl(this.#entry, release, this.#fileName);
|
|
3696
3939
|
}
|
|
3697
3940
|
async close() {
|
|
3698
3941
|
this.#closed = true;
|
|
@@ -3706,16 +3949,31 @@ var OPFSStore = class {
|
|
|
3706
3949
|
}
|
|
3707
3950
|
};
|
|
3708
3951
|
async function dropOPFSStore(name) {
|
|
3952
|
+
const fileName = sanitizeOPFSFileName(name);
|
|
3953
|
+
const txLockName = `pb-opfs-tx:${fileName}`;
|
|
3709
3954
|
const entry = stores2.get(name);
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3955
|
+
const releaseLocal = entry ? await entry.lock.write() : null;
|
|
3956
|
+
let releaseCrossTab = null;
|
|
3957
|
+
try {
|
|
3958
|
+
releaseCrossTab = await acquireCrossTabLock(txLockName, "exclusive");
|
|
3959
|
+
stores2.delete(name);
|
|
3960
|
+
if (entry) {
|
|
3961
|
+
entry.map.clear();
|
|
3962
|
+
entry.epoch = -1;
|
|
3963
|
+
entry.offset = 0;
|
|
3964
|
+
}
|
|
3965
|
+
if (isOPFSSupported()) {
|
|
3966
|
+
try {
|
|
3967
|
+
await getSharedWorkerClient().call({
|
|
3968
|
+
op: "drop",
|
|
3969
|
+
fileName
|
|
3970
|
+
});
|
|
3971
|
+
} catch {
|
|
3972
|
+
}
|
|
3718
3973
|
}
|
|
3974
|
+
} finally {
|
|
3975
|
+
releaseCrossTab?.();
|
|
3976
|
+
releaseLocal?.();
|
|
3719
3977
|
}
|
|
3720
3978
|
if (entry?.fallbackStore) {
|
|
3721
3979
|
await dropIDBStoreWithMemFallback(name);
|
|
@@ -16033,7 +16291,7 @@ function makeMessage(message, context, logLevel) {
|
|
|
16033
16291
|
}
|
|
16034
16292
|
|
|
16035
16293
|
// ../zero-client/src/client/version.ts
|
|
16036
|
-
var version2 = "2.3.
|
|
16294
|
+
var version2 = "2.3.2";
|
|
16037
16295
|
|
|
16038
16296
|
// ../zero-client/src/client/log-options.ts
|
|
16039
16297
|
var LevelFilterLogSink = class {
|
|
@@ -7808,6 +7808,11 @@ function applyChange(parentEntry, change, schema, relationship, format, withIDs
|
|
|
7808
7808
|
}
|
|
7809
7809
|
}
|
|
7810
7810
|
function applyEdit(existing, change, schema, withIDs) {
|
|
7811
|
+
for (const key in change.oldNode.row) {
|
|
7812
|
+
if (!(key in change.node.row)) {
|
|
7813
|
+
delete existing[key];
|
|
7814
|
+
}
|
|
7815
|
+
}
|
|
7811
7816
|
Object.assign(existing, change.node.row);
|
|
7812
7817
|
if (withIDs) {
|
|
7813
7818
|
existing[idSymbol] = makeID(change.node.row, schema);
|
package/dist/react.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Zero
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-3EUUY2JG.js";
|
|
4
4
|
import {
|
|
5
5
|
DEFAULT_TTL_MS
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-STMINOLV.js";
|
|
7
7
|
|
|
8
8
|
// ../zero-react/src/use-query.tsx
|
|
9
9
|
import React, { useSyncExternalStore } from "react";
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import type { LogContext } from '@rocicorp/logger';
|
|
2
2
|
import type { Read, Store, Write } from './store.ts';
|
|
3
3
|
export declare function isOPFSSupported(): boolean;
|
|
4
|
+
export declare function sanitizeOPFSFileName(name: string): string;
|
|
4
5
|
/**
|
|
5
|
-
* OPFS-backed transactional KV store for
|
|
6
|
+
* Strictly serializable, cross-tab OPFS-backed transactional KV store for
|
|
7
|
+
* Replicache/Zero.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Holds a cross-tab Web Lock (`pb-opfs-tx:<fileName>`, shared for reads,
|
|
10
|
+
* exclusive for writes/drops) for the entire transaction lifetime and syncs
|
|
11
|
+
* only newly appended WAL tail bytes (or full snapshot on compaction) before
|
|
12
|
+
* exposing the map — preventing stale-cache DAG refcount corruption when
|
|
13
|
+
* multiple tabs share the same origin. Falls back transparently to
|
|
14
|
+
* `IDBStoreWithMemFallback` if OPFS is unavailable (SSR, Node, Private Mode).
|
|
11
15
|
*/
|
|
12
16
|
export declare class OPFSStore implements Store {
|
|
13
17
|
#private;
|
package/dist/zero.js
CHANGED
package/package.json
CHANGED