@duraxis/storage-disk 0.1.1 → 0.2.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/CHANGELOG.md +33 -0
- package/README.md +5 -1
- package/dist/async-disk-storage.driver.d.ts +18 -1
- package/dist/disk-format.d.ts +8 -0
- package/dist/index.cjs +140 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +143 -48
- package/dist/index.js.map +1 -1
- package/dist/ownership.d.ts +1 -2
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# @duraxis/storage-disk changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a4bed1c: Harden bounded production pilots with hierarchical resource admission, query and transaction deadlines, tracked pending I/O, bounded materialization, weighted caches, incremental listings and journal replay, and verified single-owner garbage collection.
|
|
8
|
+
|
|
9
|
+
Breaking pre-1.0 changes: Core requires bounded-read adapter capabilities; resource limits now reject oversized work; destructive S3 GC fails closed; encrypted expiring v3 cursors replace signed database cursors. Restart pagination and provision stable encryption keys. Tenant context checks strengthen application isolation but do not authorize raw storage access.
|
|
10
|
+
|
|
11
|
+
Add conservative commit reconciliation and bounded atomic idempotency receipts without callback retries. Preserve manifest CAS, physical persistence formats and migration checksum identities. See docs/PRODUCTION-HARDENING.md for defaults, deployment constraints, receipt retention and remaining index/FK accounting limitations.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [a4bed1c]
|
|
16
|
+
- @duraxis/storage@0.2.0
|
|
17
|
+
|
|
18
|
+
## 0.1.3
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- 8c357b5: fix: padronize the published versions
|
|
23
|
+
- 8c357b5: fix: padronize the published versions
|
|
24
|
+
- Updated dependencies [8c357b5]
|
|
25
|
+
- Updated dependencies [8c357b5]
|
|
26
|
+
- @duraxis/storage@0.1.2
|
|
27
|
+
|
|
28
|
+
## 0.1.2
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- e6facc7: fix: padronize the published versions
|
|
33
|
+
- Updated dependencies [e6facc7]
|
|
34
|
+
- @duraxis/storage@0.1.1
|
|
35
|
+
|
|
3
36
|
## 0.1.1
|
|
4
37
|
|
|
5
38
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ npm install @duraxis/storage-disk
|
|
|
11
11
|
yarn add @duraxis/storage-disk
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
Node.js
|
|
14
|
+
Node.js 24.15.0 or newer is required.
|
|
15
15
|
|
|
16
16
|
## Basic use
|
|
17
17
|
|
|
@@ -169,3 +169,7 @@ lock, and the same directory can then be reopened with its persisted data intact
|
|
|
169
169
|
The driver does not coordinate multiple hosts. It preserves atomic storage operations, journal
|
|
170
170
|
recovery, object versions, and checksummed physical formats, but should be evaluated against the
|
|
171
171
|
failure model and storage stack of each deployment.
|
|
172
|
+
|
|
173
|
+
## Production resource and lifecycle limits
|
|
174
|
+
|
|
175
|
+
See the [production hardening guide](../../docs/PRODUCTION-HARDENING.md) for bounded defaults, cancellation, verified maintenance, tenant trust boundaries, encrypted cursors, upgrade instructions and remaining pilot limitations. Core rejects storage adapters without bounded-read capability; destructive shared-storage garbage collection is unsupported.
|
|
@@ -12,6 +12,8 @@ export interface AsyncDiskStorageOptions {
|
|
|
12
12
|
readonly maxKeyBytes?: number;
|
|
13
13
|
readonly maxObjectBytes?: number;
|
|
14
14
|
readonly maxJournalBytes?: number;
|
|
15
|
+
readonly maxMetadataBytes?: number;
|
|
16
|
+
readonly maxQueuedOperations?: number;
|
|
15
17
|
readonly nodeCacheHint?: number;
|
|
16
18
|
readonly faultInjector?: (point: DiskFaultPoint, context: DiskFaultContext) => void | Promise<void>;
|
|
17
19
|
}
|
|
@@ -26,10 +28,15 @@ export declare class AsyncDiskStorageDriver implements StorageDriver {
|
|
|
26
28
|
private readonly root;
|
|
27
29
|
private readonly registryKey;
|
|
28
30
|
private readonly options;
|
|
31
|
+
readonly capabilities: Readonly<{
|
|
32
|
+
boundedReads: true;
|
|
33
|
+
}>;
|
|
29
34
|
private static readonly activeDirectories;
|
|
30
35
|
private readonly entries;
|
|
31
36
|
private readonly ownership;
|
|
32
37
|
private readonly metrics;
|
|
38
|
+
private metadataBytes;
|
|
39
|
+
private queuedOperations;
|
|
33
40
|
private operationTail;
|
|
34
41
|
private journal;
|
|
35
42
|
private journalBytes;
|
|
@@ -46,12 +53,22 @@ export declare class AsyncDiskStorageDriver implements StorageDriver {
|
|
|
46
53
|
get durability(): DiskDurabilityCapabilities;
|
|
47
54
|
getMetrics(): Readonly<StorageDriverMetrics>;
|
|
48
55
|
private initialize;
|
|
56
|
+
private setEntry;
|
|
57
|
+
private deleteEntry;
|
|
49
58
|
private validateKey;
|
|
50
59
|
private assertUsable;
|
|
51
60
|
private exclusive;
|
|
52
|
-
get(key: string
|
|
61
|
+
get(key: string, options?: {
|
|
62
|
+
readonly signal?: AbortSignal;
|
|
63
|
+
readonly maxBytes?: number;
|
|
64
|
+
}): Promise<StorageObject | null>;
|
|
53
65
|
put(key: string, data: Uint8Array, options: WriteOptions): Promise<WriteResult>;
|
|
54
66
|
delete(key: string, options: DeleteOptions): Promise<DeleteResult>;
|
|
67
|
+
verifyExclusiveOwnership(): Promise<void>;
|
|
68
|
+
iterate(prefix: string, options?: {
|
|
69
|
+
readonly pageSize?: number;
|
|
70
|
+
readonly signal?: AbortSignal;
|
|
71
|
+
}): AsyncIterable<StorageObjectInfo>;
|
|
55
72
|
list(prefix: string): Promise<readonly StorageObjectInfo[]>;
|
|
56
73
|
private append;
|
|
57
74
|
compact(): Promise<void>;
|
package/dist/disk-format.d.ts
CHANGED
|
@@ -26,3 +26,11 @@ export declare function encodePhysicalObject(key: string, metadata: StoredMetada
|
|
|
26
26
|
export declare function decodePhysicalObject(bytes: Uint8Array, expectedKey: string, expected: StoredMetadata): Uint8Array;
|
|
27
27
|
export declare function encodeJournalFrame(payload: JournalPayload): Uint8Array;
|
|
28
28
|
export declare function decodeJournal(bytes: Uint8Array): DecodedJournal;
|
|
29
|
+
export declare function replayJournal(handle: {
|
|
30
|
+
stat(): Promise<{
|
|
31
|
+
readonly size: number;
|
|
32
|
+
}>;
|
|
33
|
+
read(buffer: Uint8Array, offset: number, length: number, position: number): Promise<{
|
|
34
|
+
readonly bytesRead: number;
|
|
35
|
+
}>;
|
|
36
|
+
}, maxBytes: number, consume: (payload: JournalPayload) => void): Promise<Omit<DecodedJournal, "frames">>;
|
package/dist/index.cjs
CHANGED
|
@@ -113,28 +113,37 @@ function decodeFrame(bytes, expectedSequence) {
|
|
|
113
113
|
if (sha256(JSON.stringify(payload)) != record.checksum) throw corruption("Storage journal integrity check failed");
|
|
114
114
|
return payload;
|
|
115
115
|
}
|
|
116
|
-
function
|
|
117
|
-
const
|
|
116
|
+
async function replayJournal(handle, maxBytes, consume) {
|
|
117
|
+
const info = await handle.stat();
|
|
118
|
+
if (info.size > maxBytes) throw new _duraxis_storage.StorageError("Storage journal exceeds configured limit", "RESOURCE_LIMIT");
|
|
118
119
|
let offset = 0;
|
|
119
120
|
let sequence = 1;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
const read = async (buffer, position) => {
|
|
122
|
+
let received = 0;
|
|
123
|
+
while (received < buffer.byteLength) {
|
|
124
|
+
const result = await handle.read(buffer, received, buffer.byteLength - received, position + received);
|
|
125
|
+
if (!result.bytesRead) break;
|
|
126
|
+
received += result.bytesRead;
|
|
127
|
+
}
|
|
128
|
+
return received;
|
|
129
|
+
};
|
|
130
|
+
while (offset < info.size) {
|
|
131
|
+
const header = /* @__PURE__ */ new Uint8Array(4);
|
|
132
|
+
if (await read(header, offset) != 4) return {
|
|
123
133
|
validBytes: offset,
|
|
124
134
|
incompleteTail: true
|
|
125
135
|
};
|
|
126
|
-
const length = new DataView(
|
|
127
|
-
if (length
|
|
128
|
-
|
|
129
|
-
|
|
136
|
+
const length = new DataView(header.buffer).getUint32(0, false);
|
|
137
|
+
if (!length || length > 65536) throw corruption("Invalid storage journal frame length");
|
|
138
|
+
const payload = new Uint8Array(length);
|
|
139
|
+
if (await read(payload, offset + 4) != length) return {
|
|
130
140
|
validBytes: offset,
|
|
131
141
|
incompleteTail: true
|
|
132
142
|
};
|
|
133
|
-
|
|
143
|
+
consume(decodeFrame(payload, sequence++));
|
|
134
144
|
offset += 4 + length;
|
|
135
145
|
}
|
|
136
146
|
return {
|
|
137
|
-
frames,
|
|
138
147
|
validBytes: offset,
|
|
139
148
|
incompleteTail: false
|
|
140
149
|
};
|
|
@@ -510,6 +519,19 @@ var DirectoryOwnership = class {
|
|
|
510
519
|
this.#owned = true;
|
|
511
520
|
});
|
|
512
521
|
}
|
|
522
|
+
async verify() {
|
|
523
|
+
if (!this.#owned) throw ownershipConflict("Storage directory is not owned");
|
|
524
|
+
await withGuard(this.root, async () => {
|
|
525
|
+
const observed = await observe((0, node_path.join)(this.root, LOCK_FILE));
|
|
526
|
+
if (!observed) throw ownershipConflict("Storage owner lock is missing");
|
|
527
|
+
try {
|
|
528
|
+
if (parseOwner(observed.bytes).ownerId != this.#ownerId) throw ownershipConflict("Storage ownership changed");
|
|
529
|
+
await assertObservedUnchanged(observed);
|
|
530
|
+
} finally {
|
|
531
|
+
await observed.handle.close();
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
}
|
|
513
535
|
async release() {
|
|
514
536
|
if (!this.#owned) return;
|
|
515
537
|
await withGuard(this.root, async () => {
|
|
@@ -550,6 +572,9 @@ var JOURNAL_FILE = "journal.log";
|
|
|
550
572
|
var PREVIOUS_JOURNAL = "journal.previous";
|
|
551
573
|
var NEXT_JOURNAL = "journal.next";
|
|
552
574
|
var OBJECTS_DIRECTORY = "objects";
|
|
575
|
+
var MAX_KEY_BYTES = 4096;
|
|
576
|
+
var MAX_OBJECT_BYTES = 268435456;
|
|
577
|
+
var MAX_JOURNAL_BYTES = 1073741824;
|
|
553
578
|
function nodeErrorCode(error) {
|
|
554
579
|
return error && typeof error == "object" && "code" in error ? String(error.code) : void 0;
|
|
555
580
|
}
|
|
@@ -589,6 +614,7 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
589
614
|
root;
|
|
590
615
|
registryKey;
|
|
591
616
|
options;
|
|
617
|
+
capabilities = Object.freeze({ boundedReads: true });
|
|
592
618
|
static activeDirectories = /* @__PURE__ */ new Set();
|
|
593
619
|
entries = /* @__PURE__ */ new Map();
|
|
594
620
|
ownership;
|
|
@@ -601,6 +627,8 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
601
627
|
bytesWritten: 0,
|
|
602
628
|
synchronizationWaitMs: 0
|
|
603
629
|
};
|
|
630
|
+
metadataBytes = 0;
|
|
631
|
+
queuedOperations = 0;
|
|
604
632
|
operationTail = Promise.resolve();
|
|
605
633
|
journal;
|
|
606
634
|
journalBytes = 0;
|
|
@@ -619,13 +647,14 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
619
647
|
}
|
|
620
648
|
static async open(options) {
|
|
621
649
|
if (!options || typeof options.directory != "string" || options.directory.trim() == "") throw new TypeError("A storage directory is required");
|
|
622
|
-
const maxKeyBytes = options.maxKeyBytes ??
|
|
623
|
-
const maxObjectBytes = options.maxObjectBytes ??
|
|
624
|
-
const maxJournalBytes = options.maxJournalBytes ??
|
|
650
|
+
const maxKeyBytes = options.maxKeyBytes ?? MAX_KEY_BYTES;
|
|
651
|
+
const maxObjectBytes = options.maxObjectBytes ?? MAX_OBJECT_BYTES;
|
|
652
|
+
const maxJournalBytes = options.maxJournalBytes ?? MAX_JOURNAL_BYTES;
|
|
625
653
|
const ownershipRecovery = normalizeOwnershipRecovery(options.ownershipRecovery);
|
|
626
654
|
if (!Number.isSafeInteger(maxKeyBytes) || maxKeyBytes < 64 || maxKeyBytes > 65536) throw new RangeError("Invalid maximum storage key size");
|
|
627
|
-
if (!Number.isSafeInteger(maxObjectBytes) || maxObjectBytes < 0 || maxObjectBytes >
|
|
655
|
+
if (!Number.isSafeInteger(maxObjectBytes) || maxObjectBytes < 0 || maxObjectBytes > MAX_JOURNAL_BYTES) throw new RangeError("Invalid maximum object size");
|
|
628
656
|
if (!Number.isSafeInteger(maxJournalBytes) || maxJournalBytes < 1048576) throw new RangeError("Invalid maximum journal size");
|
|
657
|
+
for (const value of [options.maxMetadataBytes, options.maxQueuedOperations]) if (value !== void 0 && (!Number.isSafeInteger(value) || value < 1)) throw new RangeError("Invalid storage resource limit");
|
|
629
658
|
const requested = (0, node_path.resolve)(options.directory);
|
|
630
659
|
await (0, node_fs_promises.mkdir)(requested, { recursive: true });
|
|
631
660
|
const root = await (0, node_fs_promises.realpath)(requested);
|
|
@@ -682,19 +711,22 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
682
711
|
await syncAndClose(await (0, node_fs_promises.open)(journalPath, "wx", 384), "New storage journal");
|
|
683
712
|
await this.syncDirectory(this.root);
|
|
684
713
|
}
|
|
685
|
-
const
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
714
|
+
const input = await (0, node_fs_promises.open)(journalPath, "r");
|
|
715
|
+
let decoded;
|
|
716
|
+
try {
|
|
717
|
+
decoded = await replayJournal(input, this.options.maxJournalBytes, (frame) => {
|
|
718
|
+
this.validateKey(frame.key);
|
|
719
|
+
this.sequence = frame.sequence;
|
|
720
|
+
if (frame.op == "put") this.setEntry(frame.key, {
|
|
721
|
+
version: frame.version,
|
|
722
|
+
createdAt: frame.createdAt,
|
|
723
|
+
size: frame.size,
|
|
724
|
+
digest: frame.digest
|
|
725
|
+
});
|
|
726
|
+
else this.deleteEntry(frame.key);
|
|
696
727
|
});
|
|
697
|
-
|
|
728
|
+
} finally {
|
|
729
|
+
await input.close();
|
|
698
730
|
}
|
|
699
731
|
if (decoded.incompleteTail) {
|
|
700
732
|
await (0, node_fs_promises.truncate)(journalPath, decoded.validBytes);
|
|
@@ -705,6 +737,15 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
705
737
|
await this.assertCurrentObjectsExist();
|
|
706
738
|
await this.cleanupPhysicalObjects();
|
|
707
739
|
}
|
|
740
|
+
setEntry(key, value) {
|
|
741
|
+
const size = this.entries.has(key) ? 0 : key.length * 2 + 256;
|
|
742
|
+
if (this.metadataBytes + size > (this.options.maxMetadataBytes ?? 67108864)) throw new _duraxis_storage.StorageError("Resident storage metadata limit exceeded", "RESOURCE_LIMIT");
|
|
743
|
+
this.metadataBytes += size;
|
|
744
|
+
this.entries.set(key, value);
|
|
745
|
+
}
|
|
746
|
+
deleteEntry(key) {
|
|
747
|
+
if (this.entries.delete(key)) this.metadataBytes -= key.length * 2 + 256;
|
|
748
|
+
}
|
|
708
749
|
validateKey(key) {
|
|
709
750
|
(0, _duraxis_storage.assertStorageKey)(key);
|
|
710
751
|
if (Buffer.byteLength(key, "utf8") > this.options.maxKeyBytes) throw new _duraxis_storage.StorageError("Storage key exceeds configured byte limit", "INVALID_KEY");
|
|
@@ -715,6 +756,8 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
715
756
|
}
|
|
716
757
|
async exclusive(operation) {
|
|
717
758
|
this.assertUsable();
|
|
759
|
+
if (this.queuedOperations >= (this.options.maxQueuedOperations ?? 256)) throw new _duraxis_storage.StorageError("Storage operation admission limit exceeded", "RESOURCE_LIMIT");
|
|
760
|
+
this.queuedOperations++;
|
|
718
761
|
const before = performance.now();
|
|
719
762
|
const previous = this.operationTail;
|
|
720
763
|
let release;
|
|
@@ -727,15 +770,18 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
727
770
|
this.assertUsable();
|
|
728
771
|
return await operation();
|
|
729
772
|
} finally {
|
|
773
|
+
this.queuedOperations--;
|
|
730
774
|
release();
|
|
731
775
|
}
|
|
732
776
|
}
|
|
733
|
-
get(key) {
|
|
777
|
+
get(key, options = {}) {
|
|
734
778
|
return this.exclusive(async () => {
|
|
735
779
|
this.validateKey(key);
|
|
780
|
+
options.signal?.throwIfAborted();
|
|
736
781
|
this.metrics.reads++;
|
|
737
782
|
const metadata = this.entries.get(key);
|
|
738
783
|
if (!metadata) return null;
|
|
784
|
+
if (metadata.size > (options.maxBytes ?? this.options.maxObjectBytes)) throw new _duraxis_storage.StorageError("Storage read exceeds byte limit", "RESOURCE_LIMIT");
|
|
739
785
|
const path = this.objectPath(key, metadata.version);
|
|
740
786
|
let info;
|
|
741
787
|
try {
|
|
@@ -745,7 +791,7 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
745
791
|
throw error;
|
|
746
792
|
}
|
|
747
793
|
if (info.size > this.options.maxObjectBytes + 32768) throw new _duraxis_storage.StorageError("Physical object exceeds configured size", "CORRUPTION");
|
|
748
|
-
const physical = await (0, node_fs_promises.readFile)(path);
|
|
794
|
+
const physical = await (0, node_fs_promises.readFile)(path, { signal: options.signal });
|
|
749
795
|
this.metrics.bytesRead += physical.byteLength;
|
|
750
796
|
return {
|
|
751
797
|
data: decodePhysicalObject(physical, key, metadata),
|
|
@@ -758,11 +804,13 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
758
804
|
this.validateKey(key);
|
|
759
805
|
if (!(data instanceof Uint8Array)) throw new TypeError("Storage data must be a Uint8Array");
|
|
760
806
|
if (data.byteLength > this.options.maxObjectBytes) throw new _duraxis_storage.StorageError("Storage object exceeds configured byte limit", "RESOURCE_LIMIT");
|
|
807
|
+
(0, _duraxis_storage.assertWriteCondition)(options?.condition);
|
|
761
808
|
const current = this.entries.get(key);
|
|
762
809
|
if (options.condition.type == "if-absent" ? current != null : current == null || current.version != options.condition.version) return {
|
|
763
810
|
success: false,
|
|
764
811
|
reason: "conflict"
|
|
765
812
|
};
|
|
813
|
+
if (!this.entries.has(key) && this.metadataBytes + key.length * 2 + 256 > (this.options.maxMetadataBytes ?? 67108864)) throw new _duraxis_storage.StorageError("Resident storage metadata limit exceeded", "RESOURCE_LIMIT");
|
|
766
814
|
this.metrics.writes++;
|
|
767
815
|
const metadata = {
|
|
768
816
|
version: (0, node_crypto.randomUUID)(),
|
|
@@ -810,7 +858,7 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
810
858
|
digest: metadata.digest
|
|
811
859
|
};
|
|
812
860
|
await this.append(frame);
|
|
813
|
-
this.
|
|
861
|
+
this.setEntry(key, metadata);
|
|
814
862
|
this.sequence = frame.sequence;
|
|
815
863
|
this.metrics.bytesWritten += physical.byteLength;
|
|
816
864
|
if (current) await this.removeObsolete(this.objectPath(key, current.version));
|
|
@@ -827,6 +875,7 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
827
875
|
delete(key, options) {
|
|
828
876
|
return this.exclusive(async () => {
|
|
829
877
|
this.validateKey(key);
|
|
878
|
+
(0, _duraxis_storage.assertWriteCondition)(options?.condition);
|
|
830
879
|
const current = this.entries.get(key);
|
|
831
880
|
if (options.condition.type == "if-absent") return current ? {
|
|
832
881
|
success: false,
|
|
@@ -844,21 +893,61 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
844
893
|
key
|
|
845
894
|
};
|
|
846
895
|
await this.append(frame);
|
|
847
|
-
this.
|
|
896
|
+
this.deleteEntry(key);
|
|
848
897
|
this.sequence = frame.sequence;
|
|
849
898
|
await this.removeObsolete(this.objectPath(key, current.version));
|
|
850
899
|
return { success: true };
|
|
851
900
|
});
|
|
852
901
|
}
|
|
902
|
+
verifyExclusiveOwnership() {
|
|
903
|
+
return this.exclusive(() => this.ownership.verify());
|
|
904
|
+
}
|
|
905
|
+
async *iterate(prefix, options = {}) {
|
|
906
|
+
if (typeof prefix != "string" || prefix.includes("\0")) throw new _duraxis_storage.StorageError("Invalid storage prefix", "INVALID_KEY");
|
|
907
|
+
const size = options.pageSize ?? 256;
|
|
908
|
+
if (!Number.isSafeInteger(size) || size < 1 || size > 1e3) throw new RangeError("Invalid listing page size");
|
|
909
|
+
const iterator = this.entries.entries();
|
|
910
|
+
let done = false;
|
|
911
|
+
while (!done) {
|
|
912
|
+
options.signal?.throwIfAborted();
|
|
913
|
+
const page = await this.exclusive(async () => {
|
|
914
|
+
const entries = [];
|
|
915
|
+
let visited = 0;
|
|
916
|
+
while (visited++ < size) {
|
|
917
|
+
const next = iterator.next();
|
|
918
|
+
if (next.done) {
|
|
919
|
+
done = true;
|
|
920
|
+
break;
|
|
921
|
+
}
|
|
922
|
+
const [key, value] = next.value;
|
|
923
|
+
if (key.startsWith(prefix)) entries.push({
|
|
924
|
+
key,
|
|
925
|
+
version: value.version,
|
|
926
|
+
createdAt: value.createdAt
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
return entries;
|
|
930
|
+
});
|
|
931
|
+
for (const entry of page) {
|
|
932
|
+
options.signal?.throwIfAborted();
|
|
933
|
+
yield entry;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
853
937
|
list(prefix) {
|
|
854
938
|
return this.exclusive(async () => {
|
|
855
939
|
if (typeof prefix != "string" || prefix.includes("\0") || Buffer.byteLength(prefix, "utf8") > this.options.maxKeyBytes) throw new _duraxis_storage.StorageError("Invalid storage prefix", "INVALID_KEY");
|
|
856
940
|
this.metrics.lists++;
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
941
|
+
const result = [];
|
|
942
|
+
for (const [key, value] of this.entries) if (key.startsWith(prefix)) {
|
|
943
|
+
if (result.length >= 1e5) throw new _duraxis_storage.StorageError("Legacy listing limit exceeded; use iterate()", "RESOURCE_LIMIT");
|
|
944
|
+
result.push({
|
|
945
|
+
key,
|
|
946
|
+
version: value.version,
|
|
947
|
+
createdAt: value.createdAt
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
return result.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
862
951
|
});
|
|
863
952
|
}
|
|
864
953
|
async append(frame) {
|
|
@@ -894,7 +983,7 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
894
983
|
let offset = 0;
|
|
895
984
|
let sequence = 0;
|
|
896
985
|
try {
|
|
897
|
-
for (const [key, metadata] of
|
|
986
|
+
for (const [key, metadata] of this.entries) {
|
|
898
987
|
const bytes = encodeJournalFrame({
|
|
899
988
|
format: 1,
|
|
900
989
|
sequence: ++sequence,
|
|
@@ -905,6 +994,7 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
905
994
|
size: metadata.size,
|
|
906
995
|
digest: metadata.digest
|
|
907
996
|
});
|
|
997
|
+
if (offset + bytes.byteLength > this.options.maxJournalBytes) throw new _duraxis_storage.StorageError("Compacted journal exceeds configured limit", "RESOURCE_LIMIT");
|
|
908
998
|
await writeAll(next, bytes, offset);
|
|
909
999
|
offset += bytes.byteLength;
|
|
910
1000
|
}
|
|
@@ -975,19 +1065,20 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
975
1065
|
}
|
|
976
1066
|
}
|
|
977
1067
|
async cleanupPhysicalObjects() {
|
|
978
|
-
const referenced =
|
|
1068
|
+
const referenced = /* @__PURE__ */ new Map();
|
|
1069
|
+
for (const [key, metadata] of this.entries) referenced.set(sha256(new TextEncoder().encode(key)), metadata.version);
|
|
979
1070
|
const walk = async (directory) => {
|
|
980
1071
|
let items;
|
|
981
1072
|
try {
|
|
982
|
-
items = await (0, node_fs_promises.
|
|
1073
|
+
items = await (0, node_fs_promises.opendir)(directory);
|
|
983
1074
|
} catch (error) {
|
|
984
1075
|
if (nodeErrorCode(error) == "ENOENT") return;
|
|
985
1076
|
throw error;
|
|
986
1077
|
}
|
|
987
|
-
for (const item of items) {
|
|
1078
|
+
for await (const item of items) {
|
|
988
1079
|
const path = (0, node_path.join)(directory, item.name);
|
|
989
1080
|
if (item.isDirectory()) await walk(path);
|
|
990
|
-
else if (item.isFile() && (item.name.endsWith(".tmp") || item.name.endsWith(".obj") &&
|
|
1081
|
+
else if (item.isFile() && (item.name.endsWith(".tmp") || item.name.endsWith(".obj") && referenced.get((0, node_path.basename)((0, node_path.dirname)(path))) + ".obj" != item.name)) await this.removeObsolete(path);
|
|
991
1082
|
}
|
|
992
1083
|
};
|
|
993
1084
|
await walk((0, node_path.join)(this.root, OBJECTS_DIRECTORY));
|
|
@@ -1003,11 +1094,15 @@ var AsyncDiskStorageDriver = class AsyncDiskStorageDriver {
|
|
|
1003
1094
|
if (hasNext) await (0, node_fs_promises.rename)(next, current);
|
|
1004
1095
|
else if (hasPrevious) await (0, node_fs_promises.rename)(previous, current);
|
|
1005
1096
|
} else if (hasPrevious) try {
|
|
1006
|
-
|
|
1097
|
+
const input = await (0, node_fs_promises.open)(current, "r");
|
|
1098
|
+
try {
|
|
1099
|
+
await replayJournal(input, this.options.maxJournalBytes, () => {});
|
|
1100
|
+
} finally {
|
|
1101
|
+
await input.close();
|
|
1102
|
+
}
|
|
1007
1103
|
await this.removeObsolete(previous);
|
|
1008
|
-
} catch {
|
|
1009
|
-
|
|
1010
|
-
await (0, node_fs_promises.rename)(previous, current);
|
|
1104
|
+
} catch (cause) {
|
|
1105
|
+
throw new _duraxis_storage.StorageError("Cannot establish journal rotation outcome; refusing to restore stale journal", "CORRUPTION", { cause });
|
|
1011
1106
|
}
|
|
1012
1107
|
if (await exists(current) && await exists(next)) await this.removeObsolete(next);
|
|
1013
1108
|
if (await exists(current) && await exists(previous)) await this.removeObsolete(previous);
|