wire-mesh-core 4.2.4 → 4.2.5
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.
|
@@ -8,25 +8,40 @@ function headKey(peer) {
|
|
|
8
8
|
function entryKey(peer, seq) {
|
|
9
9
|
return `data/${require_domain_device_id.deviceIdToHex(peer)}/entry/${String(seq)}`;
|
|
10
10
|
}
|
|
11
|
+
/** Thrown when the head a store holds for a log is not a decimal integer of at most 2^53 - 1: an empty text, a hexadecimal, an exponent, a sign, a fraction, leading zeros or anything else that is not the plain digits a head is written as, or an integer above the safe range. Nothing is written when it is thrown, so a corrupt store neither reads as an empty log nor is overwritten as one. */
|
|
12
|
+
var InvalidLogHeadError = class extends Error {
|
|
13
|
+
text;
|
|
14
|
+
constructor(peer, text) {
|
|
15
|
+
super(`the head of the log of ${require_domain_device_id.deviceIdToHex(peer)} is not a decimal integer of at most 2^53 - 1: ${JSON.stringify(text)}`);
|
|
16
|
+
this.name = "InvalidLogHeadError";
|
|
17
|
+
this.text = text;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const DECIMAL_HEAD = /^(?:0|[1-9][0-9]*)$/;
|
|
11
21
|
async function readHeadSeq(storage, peer) {
|
|
12
22
|
const raw = await storage.get(headKey(peer));
|
|
13
23
|
if (raw === void 0) return 0;
|
|
14
|
-
|
|
24
|
+
const text = new TextDecoder().decode(raw);
|
|
25
|
+
const head = Number(text);
|
|
26
|
+
if (!DECIMAL_HEAD.test(text) || !Number.isSafeInteger(head)) throw new InvalidLogHeadError(peer, text);
|
|
27
|
+
return head;
|
|
15
28
|
}
|
|
16
29
|
async function writeHeadSeq(storage, peer, seq) {
|
|
17
30
|
await storage.set(headKey(peer), new TextEncoder().encode(String(seq)));
|
|
18
31
|
}
|
|
19
32
|
/**
|
|
20
|
-
* The current head-seq this store holds for `peer`'s log
|
|
33
|
+
* The current head-seq this store holds for `peer`'s log, own log if peer is this device's own identity, a replicated peer's log otherwise (any store may hold and answer for a peer's log, per data-domain.cddl's own fan-out rule; nothing here distinguishes "my own log" from "a replica I'm holding"). 0 means nothing is held yet. Throws InvalidLogHeadError for a stored head that is not a decimal integer of at most 2^53 - 1.
|
|
21
34
|
*/
|
|
22
35
|
async function headSeqFor(storage, peer) {
|
|
23
36
|
return readHeadSeq(storage, peer);
|
|
24
37
|
}
|
|
25
38
|
/**
|
|
26
|
-
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Only manages the local append and head-seq bookkeeping
|
|
39
|
+
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Throws a RangeError, writing nothing, when the next sequence is not a safe integer: a peer reading the announcing frame holds it as a number and could not hold it exactly. Throws InvalidLogHeadError, writing nothing, when the stored head is not a decimal integer. Only manages the local append and head-seq bookkeeping; the caller decides when and to which peers to actually send the returned frame (proactively on every append, batched, on a timer, or not at all), the same "primitive vs. policy" split sendDirectManageRequest already draws for the manage-request path.
|
|
27
40
|
*/
|
|
28
41
|
async function appendOwnEntry(options, entry) {
|
|
29
|
-
const
|
|
42
|
+
const currentHead = await readHeadSeq(options.storage, options.identity.deviceId);
|
|
43
|
+
const seq = currentHead + 1;
|
|
44
|
+
if (!Number.isSafeInteger(seq)) throw new RangeError(`the log of this device holds ${String(currentHead)} entries, so its next sequence is not an integer a peer can hold exactly`);
|
|
30
45
|
await options.storage.set(entryKey(options.identity.deviceId, seq), entry);
|
|
31
46
|
await writeHeadSeq(options.storage, options.identity.deviceId, seq);
|
|
32
47
|
return {
|
|
@@ -82,7 +97,7 @@ async function handleDataRequest(storage, frame, limit) {
|
|
|
82
97
|
};
|
|
83
98
|
}
|
|
84
99
|
/**
|
|
85
|
-
* Handles an incoming data-entries
|
|
100
|
+
* Handles an incoming data-entries. The caller must refuse a frame naming its own device and serialise applications per peer (see the module doc). Persists the given entries starting right after from-seq, but ONLY when from-seq exactly matches this store's own current head for `frame.peer`, data-domain.cddl's own gap-rejection obligation. A mismatch covers both directions: from-seq ahead of our head is a real gap (we're missing something in between); from-seq behind our head is a stale or replayed response we've already applied. Either way the caller's own retry path is the same, re-issue a data-request from this store's own current headSeqFor(peer), which handleDataHave already computes correctly on the next data-have it sees.
|
|
86
101
|
*/
|
|
87
102
|
async function handleDataEntries(storage, frame) {
|
|
88
103
|
const ourHead = await readHeadSeq(storage, frame.peer);
|
|
@@ -104,6 +119,7 @@ async function readEntries(storage, peer, fromSeqExclusive) {
|
|
|
104
119
|
return readRun(storage, peer, fromSeqExclusive + 1, head);
|
|
105
120
|
}
|
|
106
121
|
//#endregion
|
|
122
|
+
exports.InvalidLogHeadError = InvalidLogHeadError;
|
|
107
123
|
exports.appendOwnEntry = appendOwnEntry;
|
|
108
124
|
exports.handleDataEntries = handleDataEntries;
|
|
109
125
|
exports.handleDataHave = handleDataHave;
|
|
@@ -2,16 +2,21 @@ import { D as DeviceId, E as DataRequestFrame, T as DataHaveFrame, w as DataEntr
|
|
|
2
2
|
import { t as KeyValueStorage } from "../storage-jbGFAYIV.cjs";
|
|
3
3
|
import { r as IdentityPort } from "../identity-CQof0iEM.cjs";
|
|
4
4
|
//#region src/domain/data-sync.d.ts
|
|
5
|
+
/** Thrown when the head a store holds for a log is not a decimal integer of at most 2^53 - 1: an empty text, a hexadecimal, an exponent, a sign, a fraction, leading zeros or anything else that is not the plain digits a head is written as, or an integer above the safe range. Nothing is written when it is thrown, so a corrupt store neither reads as an empty log nor is overwritten as one. */
|
|
6
|
+
export declare class InvalidLogHeadError extends Error {
|
|
7
|
+
readonly text: string;
|
|
8
|
+
constructor(peer: DeviceId, text: string);
|
|
9
|
+
}
|
|
5
10
|
export interface DataSyncOptions {
|
|
6
11
|
identity: IdentityPort;
|
|
7
12
|
storage: KeyValueStorage;
|
|
8
13
|
}
|
|
9
14
|
/**
|
|
10
|
-
* The current head-seq this store holds for `peer`'s log
|
|
15
|
+
* The current head-seq this store holds for `peer`'s log, own log if peer is this device's own identity, a replicated peer's log otherwise (any store may hold and answer for a peer's log, per data-domain.cddl's own fan-out rule; nothing here distinguishes "my own log" from "a replica I'm holding"). 0 means nothing is held yet. Throws InvalidLogHeadError for a stored head that is not a decimal integer of at most 2^53 - 1.
|
|
11
16
|
*/
|
|
12
17
|
export declare function headSeqFor(storage: Readonly<KeyValueStorage>, peer: DeviceId): Promise<number>;
|
|
13
18
|
/**
|
|
14
|
-
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Only manages the local append and head-seq bookkeeping
|
|
19
|
+
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Throws a RangeError, writing nothing, when the next sequence is not a safe integer: a peer reading the announcing frame holds it as a number and could not hold it exactly. Throws InvalidLogHeadError, writing nothing, when the stored head is not a decimal integer. Only manages the local append and head-seq bookkeeping; the caller decides when and to which peers to actually send the returned frame (proactively on every append, batched, on a timer, or not at all), the same "primitive vs. policy" split sendDirectManageRequest already draws for the manage-request path.
|
|
15
20
|
*/
|
|
16
21
|
export declare function appendOwnEntry(options: Readonly<DataSyncOptions>, entry: Uint8Array<ArrayBuffer>): Promise<{
|
|
17
22
|
seq: number;
|
|
@@ -32,7 +37,7 @@ export type HandleDataEntriesResult = {
|
|
|
32
37
|
reason: "gap";
|
|
33
38
|
};
|
|
34
39
|
/**
|
|
35
|
-
* Handles an incoming data-entries
|
|
40
|
+
* Handles an incoming data-entries. The caller must refuse a frame naming its own device and serialise applications per peer (see the module doc). Persists the given entries starting right after from-seq, but ONLY when from-seq exactly matches this store's own current head for `frame.peer`, data-domain.cddl's own gap-rejection obligation. A mismatch covers both directions: from-seq ahead of our head is a real gap (we're missing something in between); from-seq behind our head is a stale or replayed response we've already applied. Either way the caller's own retry path is the same, re-issue a data-request from this store's own current headSeqFor(peer), which handleDataHave already computes correctly on the next data-have it sees.
|
|
36
41
|
*/
|
|
37
42
|
export declare function handleDataEntries(storage: Readonly<KeyValueStorage>, frame: Readonly<DataEntriesFrame>): Promise<HandleDataEntriesResult>;
|
|
38
43
|
/**
|
|
@@ -2,16 +2,21 @@ import { D as DeviceId, E as DataRequestFrame, T as DataHaveFrame, w as DataEntr
|
|
|
2
2
|
import { t as KeyValueStorage } from "../storage-jbGFAYIV.mjs";
|
|
3
3
|
import { r as IdentityPort } from "../identity-CUr-jaAB.mjs";
|
|
4
4
|
//#region src/domain/data-sync.d.ts
|
|
5
|
+
/** Thrown when the head a store holds for a log is not a decimal integer of at most 2^53 - 1: an empty text, a hexadecimal, an exponent, a sign, a fraction, leading zeros or anything else that is not the plain digits a head is written as, or an integer above the safe range. Nothing is written when it is thrown, so a corrupt store neither reads as an empty log nor is overwritten as one. */
|
|
6
|
+
export declare class InvalidLogHeadError extends Error {
|
|
7
|
+
readonly text: string;
|
|
8
|
+
constructor(peer: DeviceId, text: string);
|
|
9
|
+
}
|
|
5
10
|
export interface DataSyncOptions {
|
|
6
11
|
identity: IdentityPort;
|
|
7
12
|
storage: KeyValueStorage;
|
|
8
13
|
}
|
|
9
14
|
/**
|
|
10
|
-
* The current head-seq this store holds for `peer`'s log
|
|
15
|
+
* The current head-seq this store holds for `peer`'s log, own log if peer is this device's own identity, a replicated peer's log otherwise (any store may hold and answer for a peer's log, per data-domain.cddl's own fan-out rule; nothing here distinguishes "my own log" from "a replica I'm holding"). 0 means nothing is held yet. Throws InvalidLogHeadError for a stored head that is not a decimal integer of at most 2^53 - 1.
|
|
11
16
|
*/
|
|
12
17
|
export declare function headSeqFor(storage: Readonly<KeyValueStorage>, peer: DeviceId): Promise<number>;
|
|
13
18
|
/**
|
|
14
|
-
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Only manages the local append and head-seq bookkeeping
|
|
19
|
+
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Throws a RangeError, writing nothing, when the next sequence is not a safe integer: a peer reading the announcing frame holds it as a number and could not hold it exactly. Throws InvalidLogHeadError, writing nothing, when the stored head is not a decimal integer. Only manages the local append and head-seq bookkeeping; the caller decides when and to which peers to actually send the returned frame (proactively on every append, batched, on a timer, or not at all), the same "primitive vs. policy" split sendDirectManageRequest already draws for the manage-request path.
|
|
15
20
|
*/
|
|
16
21
|
export declare function appendOwnEntry(options: Readonly<DataSyncOptions>, entry: Uint8Array<ArrayBuffer>): Promise<{
|
|
17
22
|
seq: number;
|
|
@@ -32,7 +37,7 @@ export type HandleDataEntriesResult = {
|
|
|
32
37
|
reason: "gap";
|
|
33
38
|
};
|
|
34
39
|
/**
|
|
35
|
-
* Handles an incoming data-entries
|
|
40
|
+
* Handles an incoming data-entries. The caller must refuse a frame naming its own device and serialise applications per peer (see the module doc). Persists the given entries starting right after from-seq, but ONLY when from-seq exactly matches this store's own current head for `frame.peer`, data-domain.cddl's own gap-rejection obligation. A mismatch covers both directions: from-seq ahead of our head is a real gap (we're missing something in between); from-seq behind our head is a stale or replayed response we've already applied. Either way the caller's own retry path is the same, re-issue a data-request from this store's own current headSeqFor(peer), which handleDataHave already computes correctly on the next data-have it sees.
|
|
36
41
|
*/
|
|
37
42
|
export declare function handleDataEntries(storage: Readonly<KeyValueStorage>, frame: Readonly<DataEntriesFrame>): Promise<HandleDataEntriesResult>;
|
|
38
43
|
/**
|
|
@@ -7,25 +7,40 @@ function headKey(peer) {
|
|
|
7
7
|
function entryKey(peer, seq) {
|
|
8
8
|
return `data/${deviceIdToHex(peer)}/entry/${String(seq)}`;
|
|
9
9
|
}
|
|
10
|
+
/** Thrown when the head a store holds for a log is not a decimal integer of at most 2^53 - 1: an empty text, a hexadecimal, an exponent, a sign, a fraction, leading zeros or anything else that is not the plain digits a head is written as, or an integer above the safe range. Nothing is written when it is thrown, so a corrupt store neither reads as an empty log nor is overwritten as one. */
|
|
11
|
+
var InvalidLogHeadError = class extends Error {
|
|
12
|
+
text;
|
|
13
|
+
constructor(peer, text) {
|
|
14
|
+
super(`the head of the log of ${deviceIdToHex(peer)} is not a decimal integer of at most 2^53 - 1: ${JSON.stringify(text)}`);
|
|
15
|
+
this.name = "InvalidLogHeadError";
|
|
16
|
+
this.text = text;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const DECIMAL_HEAD = /^(?:0|[1-9][0-9]*)$/;
|
|
10
20
|
async function readHeadSeq(storage, peer) {
|
|
11
21
|
const raw = await storage.get(headKey(peer));
|
|
12
22
|
if (raw === void 0) return 0;
|
|
13
|
-
|
|
23
|
+
const text = new TextDecoder().decode(raw);
|
|
24
|
+
const head = Number(text);
|
|
25
|
+
if (!DECIMAL_HEAD.test(text) || !Number.isSafeInteger(head)) throw new InvalidLogHeadError(peer, text);
|
|
26
|
+
return head;
|
|
14
27
|
}
|
|
15
28
|
async function writeHeadSeq(storage, peer, seq) {
|
|
16
29
|
await storage.set(headKey(peer), new TextEncoder().encode(String(seq)));
|
|
17
30
|
}
|
|
18
31
|
/**
|
|
19
|
-
* The current head-seq this store holds for `peer`'s log
|
|
32
|
+
* The current head-seq this store holds for `peer`'s log, own log if peer is this device's own identity, a replicated peer's log otherwise (any store may hold and answer for a peer's log, per data-domain.cddl's own fan-out rule; nothing here distinguishes "my own log" from "a replica I'm holding"). 0 means nothing is held yet. Throws InvalidLogHeadError for a stored head that is not a decimal integer of at most 2^53 - 1.
|
|
20
33
|
*/
|
|
21
34
|
async function headSeqFor(storage, peer) {
|
|
22
35
|
return readHeadSeq(storage, peer);
|
|
23
36
|
}
|
|
24
37
|
/**
|
|
25
|
-
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Only manages the local append and head-seq bookkeeping
|
|
38
|
+
* Appends one entry to this identity's own oplog and returns both the new sequence and the data-have frame announcing it. Throws a RangeError, writing nothing, when the next sequence is not a safe integer: a peer reading the announcing frame holds it as a number and could not hold it exactly. Throws InvalidLogHeadError, writing nothing, when the stored head is not a decimal integer. Only manages the local append and head-seq bookkeeping; the caller decides when and to which peers to actually send the returned frame (proactively on every append, batched, on a timer, or not at all), the same "primitive vs. policy" split sendDirectManageRequest already draws for the manage-request path.
|
|
26
39
|
*/
|
|
27
40
|
async function appendOwnEntry(options, entry) {
|
|
28
|
-
const
|
|
41
|
+
const currentHead = await readHeadSeq(options.storage, options.identity.deviceId);
|
|
42
|
+
const seq = currentHead + 1;
|
|
43
|
+
if (!Number.isSafeInteger(seq)) throw new RangeError(`the log of this device holds ${String(currentHead)} entries, so its next sequence is not an integer a peer can hold exactly`);
|
|
29
44
|
await options.storage.set(entryKey(options.identity.deviceId, seq), entry);
|
|
30
45
|
await writeHeadSeq(options.storage, options.identity.deviceId, seq);
|
|
31
46
|
return {
|
|
@@ -81,7 +96,7 @@ async function handleDataRequest(storage, frame, limit) {
|
|
|
81
96
|
};
|
|
82
97
|
}
|
|
83
98
|
/**
|
|
84
|
-
* Handles an incoming data-entries
|
|
99
|
+
* Handles an incoming data-entries. The caller must refuse a frame naming its own device and serialise applications per peer (see the module doc). Persists the given entries starting right after from-seq, but ONLY when from-seq exactly matches this store's own current head for `frame.peer`, data-domain.cddl's own gap-rejection obligation. A mismatch covers both directions: from-seq ahead of our head is a real gap (we're missing something in between); from-seq behind our head is a stale or replayed response we've already applied. Either way the caller's own retry path is the same, re-issue a data-request from this store's own current headSeqFor(peer), which handleDataHave already computes correctly on the next data-have it sees.
|
|
85
100
|
*/
|
|
86
101
|
async function handleDataEntries(storage, frame) {
|
|
87
102
|
const ourHead = await readHeadSeq(storage, frame.peer);
|
|
@@ -103,4 +118,4 @@ async function readEntries(storage, peer, fromSeqExclusive) {
|
|
|
103
118
|
return readRun(storage, peer, fromSeqExclusive + 1, head);
|
|
104
119
|
}
|
|
105
120
|
//#endregion
|
|
106
|
-
export { appendOwnEntry, handleDataEntries, handleDataHave, handleDataRequest, headSeqFor, readEntries };
|
|
121
|
+
export { InvalidLogHeadError, appendOwnEntry, handleDataEntries, handleDataHave, handleDataRequest, headSeqFor, readEntries };
|