@rljson/rljson 0.0.84 → 0.0.86
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/README.architecture.md +10 -0
- package/README.public.md +58 -0
- package/dist/README.architecture.md +10 -0
- package/dist/README.public.md +58 -0
- package/dist/index.d.ts +2 -0
- package/dist/rljson.js +145 -3
- package/dist/rljson_version.d.ts +1 -1
- package/dist/sync/connector-payload.d.ts +9 -0
- package/dist/sync/ref-stamp.d.ts +71 -0
- package/dist/sync/sync-events.d.ts +2 -0
- package/dist/tables/tables.d.ts +63 -0
- package/package.json +21 -23
package/README.architecture.md
CHANGED
|
@@ -633,6 +633,7 @@ The payload transmitted on the wire between Connector and Server:
|
|
|
633
633
|
| `seq` | `number` | Predecessor chain | Monotonic counter per (client, route) |
|
|
634
634
|
| `p` | `InsertHistoryTimeId[]` | Predecessor chain | Causal predecessor timeIds |
|
|
635
635
|
| `cksum` | `string` | Acknowledgment | Content checksum for ACK verification |
|
|
636
|
+
| `stamp` | `RefStamp` | Hub order | Where a stamping hub placed the ref |
|
|
636
637
|
|
|
637
638
|
### AckPayload
|
|
638
639
|
|
|
@@ -646,6 +647,14 @@ received and processed a ref:
|
|
|
646
647
|
| `receivedBy` | `number` | Count of confirming clients |
|
|
647
648
|
| `totalClients` | `number` | Total receiver clients at broadcast |
|
|
648
649
|
|
|
650
|
+
### RefStamp
|
|
651
|
+
|
|
652
|
+
`(domain, epoch, hub, n)`, set by the first hub that relays a ref and forwarded
|
|
653
|
+
unchanged by every hub after it. Ordered lexicographically by
|
|
654
|
+
`compareRefStamp`, a total order computed from no clock. The sender of a ref is
|
|
655
|
+
excluded from the hub's fan-out, so it receives its stamp as a `StampPayload`
|
|
656
|
+
(`{ r, stamp }`) on `${route}:stamp`.
|
|
657
|
+
|
|
649
658
|
### GapFill
|
|
650
659
|
|
|
651
660
|
When a Connector detects a gap in sequence numbers from a peer, it requests
|
|
@@ -686,6 +695,7 @@ Helper to generate typed socket event names from a route:
|
|
|
686
695
|
| `${route}:gapfill:req` | client → server | Request missing refs |
|
|
687
696
|
| `${route}:gapfill:res` | server → client | Supply missing refs |
|
|
688
697
|
| `${route}:bootstrap` | server → client | Latest ref on connect / heartbeat |
|
|
698
|
+
| `${route}:stamp` | server → sender | The stamp its announced ref got |
|
|
689
699
|
|
|
690
700
|
### ClientId
|
|
691
701
|
|
package/README.public.md
CHANGED
|
@@ -32,11 +32,13 @@ Core types, validation, and sync protocol for the RLJSON data format.
|
|
|
32
32
|
- [Sync Protocol](#sync-protocol)
|
|
33
33
|
- [ConnectorPayload](#connectorpayload)
|
|
34
34
|
- [AckPayload](#ackpayload)
|
|
35
|
+
- [RefStamp](#refstamp)
|
|
35
36
|
- [GapFill](#gapfill)
|
|
36
37
|
- [SyncConfig](#syncconfig)
|
|
37
38
|
- [SyncEventNames](#synceventnames)
|
|
38
39
|
- [ClientId](#clientid)
|
|
39
40
|
- [Utilities](#utilities)
|
|
41
|
+
- [Tables](#tables)
|
|
40
42
|
- [Row, ref and rowOf](#row-ref-and-rowof)
|
|
41
43
|
- [TimeId](#timeid)
|
|
42
44
|
- [RemoveDuplicates](#removeduplicates)
|
|
@@ -282,6 +284,7 @@ const enriched: ConnectorPayload = {
|
|
|
282
284
|
seq: 42, // monotonic sequence number
|
|
283
285
|
p: ['prev-timeId'], // causal predecessors
|
|
284
286
|
cksum: 'sha256:...', // content checksum
|
|
287
|
+
stamp: { domain: 'office', epoch: 3, hub: 'node-7', n: 12 }, // hub order
|
|
285
288
|
};
|
|
286
289
|
```
|
|
287
290
|
|
|
@@ -300,6 +303,39 @@ const ack: AckPayload = {
|
|
|
300
303
|
};
|
|
301
304
|
```
|
|
302
305
|
|
|
306
|
+
### RefStamp
|
|
307
|
+
|
|
308
|
+
Where a stamping hub placed a ref in the order it relays them — a fact about
|
|
309
|
+
what the fleet saw, read from no clock. The first hub that relays a ref stamps
|
|
310
|
+
it; a hub that receives a payload already carrying a `stamp` forwards it
|
|
311
|
+
unchanged.
|
|
312
|
+
|
|
313
|
+
| Field | Meaning |
|
|
314
|
+
| -------- | ---------------------------------------------------- |
|
|
315
|
+
| `domain` | The network domain whose hub stamped the ref |
|
|
316
|
+
| `epoch` | Advanced each time a hub takes office in that domain |
|
|
317
|
+
| `hub` | The node id of the stamping hub |
|
|
318
|
+
| `n` | Monotonic within `(domain, epoch, hub)` |
|
|
319
|
+
|
|
320
|
+
`compareRefStamp` orders stamps lexicographically on `(domain, epoch, hub, n)`.
|
|
321
|
+
It is a total order, so every machine sorts the same stamps the same way, and
|
|
322
|
+
`hub` keeps two hubs that both believe they hold one epoch distinct.
|
|
323
|
+
|
|
324
|
+
A hub forwards an announcement to everyone except its sender, so the sender
|
|
325
|
+
learns its own stamp from a `StampPayload` on the `${route}:stamp` event.
|
|
326
|
+
`isRefStamp` checks a value received from the wire before anything orders by
|
|
327
|
+
it.
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
import { compareRefStamp, isRefStamp, RefStamp } from '@rljson/rljson';
|
|
331
|
+
|
|
332
|
+
const a: RefStamp = { domain: 'office', epoch: 3, hub: 'node-7', n: 12 };
|
|
333
|
+
const b: RefStamp = { domain: 'office', epoch: 4, hub: 'node-2', n: 1 };
|
|
334
|
+
|
|
335
|
+
compareRefStamp(a, b); // < 0 — a later epoch orders after, whatever its n
|
|
336
|
+
isRefStamp({ domain: 'office' }); // false
|
|
337
|
+
```
|
|
338
|
+
|
|
303
339
|
### GapFill
|
|
304
340
|
|
|
305
341
|
Types for requesting and receiving missing refs when a sequence gap is
|
|
@@ -346,6 +382,7 @@ const events = syncEvents('/sharedTree');
|
|
|
346
382
|
// events.gapFillReq → '/sharedTree:gapfill:req'
|
|
347
383
|
// events.gapFillRes → '/sharedTree:gapfill:res'
|
|
348
384
|
// events.bootstrap → '/sharedTree:bootstrap'
|
|
385
|
+
// events.stamp → '/sharedTree:stamp'
|
|
349
386
|
```
|
|
350
387
|
|
|
351
388
|
### ClientId
|
|
@@ -389,6 +426,27 @@ const onConflict: ConflictCallback = (conflict: Conflict) => {
|
|
|
389
426
|
|
|
390
427
|
## Utilities
|
|
391
428
|
|
|
429
|
+
### Tables
|
|
430
|
+
|
|
431
|
+
`Tables` is a read-only view on the tables of an Rljson dump. By default it
|
|
432
|
+
hides insert history tables and tables whose key starts with `_`.
|
|
433
|
+
|
|
434
|
+
```ts
|
|
435
|
+
import { Tables } from '@rljson/rljson';
|
|
436
|
+
|
|
437
|
+
const tables = new Tables(rljson);
|
|
438
|
+
tables.ls(); // ['cars', 'tableCfgs'] — sorted table keys
|
|
439
|
+
tables.ls({ internal: true }); // also insert history and `_` tables
|
|
440
|
+
tables.ls({ long: true }); // aligned lines: 'cars components 2'
|
|
441
|
+
tables.all; // the visible tables in ls() order
|
|
442
|
+
tables.count; // number of visible tables
|
|
443
|
+
tables.get('cars'); // the table or undefined
|
|
444
|
+
tables.has('carsInsertHistory'); // true, also for internal tables
|
|
445
|
+
tables.ofType('components'); // sorted keys of that type, internal included
|
|
446
|
+
tables.cfg('cars'); // the latest TableCfg of the table
|
|
447
|
+
tables.rowCount('cars'); // 2
|
|
448
|
+
```
|
|
449
|
+
|
|
392
450
|
### Row, ref and rowOf
|
|
393
451
|
|
|
394
452
|
A `Row` is a row of an Rljson table: a JSON object whose `_hash` is written by
|
|
@@ -633,6 +633,7 @@ The payload transmitted on the wire between Connector and Server:
|
|
|
633
633
|
| `seq` | `number` | Predecessor chain | Monotonic counter per (client, route) |
|
|
634
634
|
| `p` | `InsertHistoryTimeId[]` | Predecessor chain | Causal predecessor timeIds |
|
|
635
635
|
| `cksum` | `string` | Acknowledgment | Content checksum for ACK verification |
|
|
636
|
+
| `stamp` | `RefStamp` | Hub order | Where a stamping hub placed the ref |
|
|
636
637
|
|
|
637
638
|
### AckPayload
|
|
638
639
|
|
|
@@ -646,6 +647,14 @@ received and processed a ref:
|
|
|
646
647
|
| `receivedBy` | `number` | Count of confirming clients |
|
|
647
648
|
| `totalClients` | `number` | Total receiver clients at broadcast |
|
|
648
649
|
|
|
650
|
+
### RefStamp
|
|
651
|
+
|
|
652
|
+
`(domain, epoch, hub, n)`, set by the first hub that relays a ref and forwarded
|
|
653
|
+
unchanged by every hub after it. Ordered lexicographically by
|
|
654
|
+
`compareRefStamp`, a total order computed from no clock. The sender of a ref is
|
|
655
|
+
excluded from the hub's fan-out, so it receives its stamp as a `StampPayload`
|
|
656
|
+
(`{ r, stamp }`) on `${route}:stamp`.
|
|
657
|
+
|
|
649
658
|
### GapFill
|
|
650
659
|
|
|
651
660
|
When a Connector detects a gap in sequence numbers from a peer, it requests
|
|
@@ -686,6 +695,7 @@ Helper to generate typed socket event names from a route:
|
|
|
686
695
|
| `${route}:gapfill:req` | client → server | Request missing refs |
|
|
687
696
|
| `${route}:gapfill:res` | server → client | Supply missing refs |
|
|
688
697
|
| `${route}:bootstrap` | server → client | Latest ref on connect / heartbeat |
|
|
698
|
+
| `${route}:stamp` | server → sender | The stamp its announced ref got |
|
|
689
699
|
|
|
690
700
|
### ClientId
|
|
691
701
|
|
package/dist/README.public.md
CHANGED
|
@@ -32,11 +32,13 @@ Core types, validation, and sync protocol for the RLJSON data format.
|
|
|
32
32
|
- [Sync Protocol](#sync-protocol)
|
|
33
33
|
- [ConnectorPayload](#connectorpayload)
|
|
34
34
|
- [AckPayload](#ackpayload)
|
|
35
|
+
- [RefStamp](#refstamp)
|
|
35
36
|
- [GapFill](#gapfill)
|
|
36
37
|
- [SyncConfig](#syncconfig)
|
|
37
38
|
- [SyncEventNames](#synceventnames)
|
|
38
39
|
- [ClientId](#clientid)
|
|
39
40
|
- [Utilities](#utilities)
|
|
41
|
+
- [Tables](#tables)
|
|
40
42
|
- [Row, ref and rowOf](#row-ref-and-rowof)
|
|
41
43
|
- [TimeId](#timeid)
|
|
42
44
|
- [RemoveDuplicates](#removeduplicates)
|
|
@@ -282,6 +284,7 @@ const enriched: ConnectorPayload = {
|
|
|
282
284
|
seq: 42, // monotonic sequence number
|
|
283
285
|
p: ['prev-timeId'], // causal predecessors
|
|
284
286
|
cksum: 'sha256:...', // content checksum
|
|
287
|
+
stamp: { domain: 'office', epoch: 3, hub: 'node-7', n: 12 }, // hub order
|
|
285
288
|
};
|
|
286
289
|
```
|
|
287
290
|
|
|
@@ -300,6 +303,39 @@ const ack: AckPayload = {
|
|
|
300
303
|
};
|
|
301
304
|
```
|
|
302
305
|
|
|
306
|
+
### RefStamp
|
|
307
|
+
|
|
308
|
+
Where a stamping hub placed a ref in the order it relays them — a fact about
|
|
309
|
+
what the fleet saw, read from no clock. The first hub that relays a ref stamps
|
|
310
|
+
it; a hub that receives a payload already carrying a `stamp` forwards it
|
|
311
|
+
unchanged.
|
|
312
|
+
|
|
313
|
+
| Field | Meaning |
|
|
314
|
+
| -------- | ---------------------------------------------------- |
|
|
315
|
+
| `domain` | The network domain whose hub stamped the ref |
|
|
316
|
+
| `epoch` | Advanced each time a hub takes office in that domain |
|
|
317
|
+
| `hub` | The node id of the stamping hub |
|
|
318
|
+
| `n` | Monotonic within `(domain, epoch, hub)` |
|
|
319
|
+
|
|
320
|
+
`compareRefStamp` orders stamps lexicographically on `(domain, epoch, hub, n)`.
|
|
321
|
+
It is a total order, so every machine sorts the same stamps the same way, and
|
|
322
|
+
`hub` keeps two hubs that both believe they hold one epoch distinct.
|
|
323
|
+
|
|
324
|
+
A hub forwards an announcement to everyone except its sender, so the sender
|
|
325
|
+
learns its own stamp from a `StampPayload` on the `${route}:stamp` event.
|
|
326
|
+
`isRefStamp` checks a value received from the wire before anything orders by
|
|
327
|
+
it.
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
import { compareRefStamp, isRefStamp, RefStamp } from '@rljson/rljson';
|
|
331
|
+
|
|
332
|
+
const a: RefStamp = { domain: 'office', epoch: 3, hub: 'node-7', n: 12 };
|
|
333
|
+
const b: RefStamp = { domain: 'office', epoch: 4, hub: 'node-2', n: 1 };
|
|
334
|
+
|
|
335
|
+
compareRefStamp(a, b); // < 0 — a later epoch orders after, whatever its n
|
|
336
|
+
isRefStamp({ domain: 'office' }); // false
|
|
337
|
+
```
|
|
338
|
+
|
|
303
339
|
### GapFill
|
|
304
340
|
|
|
305
341
|
Types for requesting and receiving missing refs when a sequence gap is
|
|
@@ -346,6 +382,7 @@ const events = syncEvents('/sharedTree');
|
|
|
346
382
|
// events.gapFillReq → '/sharedTree:gapfill:req'
|
|
347
383
|
// events.gapFillRes → '/sharedTree:gapfill:res'
|
|
348
384
|
// events.bootstrap → '/sharedTree:bootstrap'
|
|
385
|
+
// events.stamp → '/sharedTree:stamp'
|
|
349
386
|
```
|
|
350
387
|
|
|
351
388
|
### ClientId
|
|
@@ -389,6 +426,27 @@ const onConflict: ConflictCallback = (conflict: Conflict) => {
|
|
|
389
426
|
|
|
390
427
|
## Utilities
|
|
391
428
|
|
|
429
|
+
### Tables
|
|
430
|
+
|
|
431
|
+
`Tables` is a read-only view on the tables of an Rljson dump. By default it
|
|
432
|
+
hides insert history tables and tables whose key starts with `_`.
|
|
433
|
+
|
|
434
|
+
```ts
|
|
435
|
+
import { Tables } from '@rljson/rljson';
|
|
436
|
+
|
|
437
|
+
const tables = new Tables(rljson);
|
|
438
|
+
tables.ls(); // ['cars', 'tableCfgs'] — sorted table keys
|
|
439
|
+
tables.ls({ internal: true }); // also insert history and `_` tables
|
|
440
|
+
tables.ls({ long: true }); // aligned lines: 'cars components 2'
|
|
441
|
+
tables.all; // the visible tables in ls() order
|
|
442
|
+
tables.count; // number of visible tables
|
|
443
|
+
tables.get('cars'); // the table or undefined
|
|
444
|
+
tables.has('carsInsertHistory'); // true, also for internal tables
|
|
445
|
+
tables.ofType('components'); // sorted keys of that type, internal included
|
|
446
|
+
tables.cfg('cars'); // the latest TableCfg of the table
|
|
447
|
+
tables.rowCount('cars'); // 2
|
|
448
|
+
```
|
|
449
|
+
|
|
392
450
|
### Row, ref and rowOf
|
|
393
451
|
|
|
394
452
|
A `Row` is a row of an Rljson table: a JSON object whose `_hash` is written by
|
package/dist/index.d.ts
CHANGED
|
@@ -21,8 +21,10 @@ export * from './sync/client-id.ts';
|
|
|
21
21
|
export * from './sync/conflict.ts';
|
|
22
22
|
export * from './sync/connector-payload.ts';
|
|
23
23
|
export * from './sync/gap-fill.ts';
|
|
24
|
+
export * from './sync/ref-stamp.ts';
|
|
24
25
|
export * from './sync/sync-config.ts';
|
|
25
26
|
export * from './sync/sync-events.ts';
|
|
27
|
+
export * from './tables/tables.ts';
|
|
26
28
|
export * from './tools/ref.ts';
|
|
27
29
|
export * from './tools/resolve-slice-ids.ts';
|
|
28
30
|
export * from './tools/remove-duplicates.ts';
|
package/dist/rljson.js
CHANGED
|
@@ -1980,7 +1980,13 @@ var connectorPayloadFullExample = () => ({
|
|
|
1980
1980
|
t: 1700000000001,
|
|
1981
1981
|
seq: 42,
|
|
1982
1982
|
p: ["1700000000000:XyZw"],
|
|
1983
|
-
cksum: "sha256:abc123def456"
|
|
1983
|
+
cksum: "sha256:abc123def456",
|
|
1984
|
+
stamp: {
|
|
1985
|
+
domain: "example",
|
|
1986
|
+
epoch: 1,
|
|
1987
|
+
hub: "hub_ExAmPlE",
|
|
1988
|
+
n: 1
|
|
1989
|
+
}
|
|
1984
1990
|
});
|
|
1985
1991
|
//#endregion
|
|
1986
1992
|
//#region src/sync/gap-fill.ts
|
|
@@ -2009,6 +2015,56 @@ var gapFillResponseExample = () => ({
|
|
|
2009
2015
|
}]
|
|
2010
2016
|
});
|
|
2011
2017
|
//#endregion
|
|
2018
|
+
//#region src/sync/ref-stamp.ts
|
|
2019
|
+
// @license
|
|
2020
|
+
/**
|
|
2021
|
+
* Orders two stamps lexicographically on `(domain, epoch, hub, n)`.
|
|
2022
|
+
*
|
|
2023
|
+
* A total order: every pair compares the same way on every machine, and only
|
|
2024
|
+
* equal stamps compare as `0`.
|
|
2025
|
+
* @param a - The first stamp
|
|
2026
|
+
* @param b - The second stamp
|
|
2027
|
+
* @returns A negative number if `a` orders first, a positive number if `b`
|
|
2028
|
+
* does, `0` if they are equal
|
|
2029
|
+
*/
|
|
2030
|
+
var compareRefStamp = (a, b) => {
|
|
2031
|
+
if (a.domain !== b.domain) return a.domain < b.domain ? -1 : 1;
|
|
2032
|
+
if (a.epoch !== b.epoch) return a.epoch < b.epoch ? -1 : 1;
|
|
2033
|
+
if (a.hub !== b.hub) return a.hub < b.hub ? -1 : 1;
|
|
2034
|
+
if (a.n !== b.n) return a.n < b.n ? -1 : 1;
|
|
2035
|
+
return 0;
|
|
2036
|
+
};
|
|
2037
|
+
/**
|
|
2038
|
+
* Checks whether a value received from the wire is a well-formed RefStamp.
|
|
2039
|
+
*
|
|
2040
|
+
* A stamp arrives inside a JSON payload from another process, so it is
|
|
2041
|
+
* checked before anything orders by it.
|
|
2042
|
+
* @param value - The value to check
|
|
2043
|
+
* @returns `true` if `value` has a string `domain` and `hub` and a
|
|
2044
|
+
* non-negative integer `epoch` and `n`
|
|
2045
|
+
*/
|
|
2046
|
+
var isRefStamp = (value) => {
|
|
2047
|
+
if (typeof value !== "object" || value === null) return false;
|
|
2048
|
+
const stamp = value;
|
|
2049
|
+
return typeof stamp.domain === "string" && typeof stamp.hub === "string" && Number.isSafeInteger(stamp.epoch) && stamp.epoch >= 0 && Number.isSafeInteger(stamp.n) && stamp.n >= 0;
|
|
2050
|
+
};
|
|
2051
|
+
/**
|
|
2052
|
+
* Returns an example RefStamp for documentation and testing.
|
|
2053
|
+
*/
|
|
2054
|
+
var refStampExample = () => ({
|
|
2055
|
+
domain: "example",
|
|
2056
|
+
epoch: 1,
|
|
2057
|
+
hub: "hub_ExAmPlE",
|
|
2058
|
+
n: 1
|
|
2059
|
+
});
|
|
2060
|
+
/**
|
|
2061
|
+
* Returns an example StampPayload for documentation and testing.
|
|
2062
|
+
*/
|
|
2063
|
+
var stampPayloadExample = () => ({
|
|
2064
|
+
r: "1700000000001:EfGh",
|
|
2065
|
+
stamp: refStampExample()
|
|
2066
|
+
});
|
|
2067
|
+
//#endregion
|
|
2012
2068
|
//#region src/sync/sync-config.ts
|
|
2013
2069
|
// @license
|
|
2014
2070
|
/**
|
|
@@ -2043,9 +2099,95 @@ var syncEvents = (route) => ({
|
|
|
2043
2099
|
ackClient: `${route}:ack:client`,
|
|
2044
2100
|
gapFillReq: `${route}:gapfill:req`,
|
|
2045
2101
|
gapFillRes: `${route}:gapfill:res`,
|
|
2046
|
-
bootstrap: `${route}:bootstrap
|
|
2102
|
+
bootstrap: `${route}:bootstrap`,
|
|
2103
|
+
stamp: `${route}:stamp`
|
|
2047
2104
|
});
|
|
2048
2105
|
//#endregion
|
|
2106
|
+
//#region src/tables/tables.ts
|
|
2107
|
+
// @license
|
|
2108
|
+
/**
|
|
2109
|
+
* Read only view on the tables of an Rljson dump.
|
|
2110
|
+
*
|
|
2111
|
+
* By default insert history tables and tables whose key starts with `_`
|
|
2112
|
+
* are hidden. Pass `internal: true` to ls() to include them.
|
|
2113
|
+
*/
|
|
2114
|
+
var Tables = class Tables {
|
|
2115
|
+
/**
|
|
2116
|
+
* Creates a view on the tables of the Rljson
|
|
2117
|
+
* @param rljson - the Rljson dump to wrap
|
|
2118
|
+
*/
|
|
2119
|
+
constructor(rljson) {
|
|
2120
|
+
this._rljson = rljson;
|
|
2121
|
+
}
|
|
2122
|
+
_rljson;
|
|
2123
|
+
/** The wrapped Rljson */
|
|
2124
|
+
get rljson() {
|
|
2125
|
+
return this._rljson;
|
|
2126
|
+
}
|
|
2127
|
+
/**
|
|
2128
|
+
* The sorted table keys.
|
|
2129
|
+
* @param options - see TablesLsOptions
|
|
2130
|
+
* @returns table keys, or aligned lines `name type rowCount` with `long`
|
|
2131
|
+
*/
|
|
2132
|
+
ls(options = {}) {
|
|
2133
|
+
const keys = this._keys(options.internal === true);
|
|
2134
|
+
if (!options.long) return keys;
|
|
2135
|
+
const nameWidth = Math.max(0, ...keys.map((k) => k.length));
|
|
2136
|
+
const typeWidth = Math.max(0, ...keys.map((k) => this.get(k)._type.length));
|
|
2137
|
+
return keys.map((k) => `${k.padEnd(nameWidth)} ${this.get(k)._type.padEnd(typeWidth)} ${this.rowCount(k)}`);
|
|
2138
|
+
}
|
|
2139
|
+
/** The visible tables in ls() order */
|
|
2140
|
+
get all() {
|
|
2141
|
+
return this.ls().map((k) => this.get(k));
|
|
2142
|
+
}
|
|
2143
|
+
/** The number of visible tables */
|
|
2144
|
+
get count() {
|
|
2145
|
+
return this.ls().length;
|
|
2146
|
+
}
|
|
2147
|
+
/**
|
|
2148
|
+
* The table with the given key or undefined
|
|
2149
|
+
* @param key - the table key
|
|
2150
|
+
*/
|
|
2151
|
+
get(key) {
|
|
2152
|
+
const table = this._rljson[key];
|
|
2153
|
+
return Tables._isTable(table) ? table : void 0;
|
|
2154
|
+
}
|
|
2155
|
+
/**
|
|
2156
|
+
* True if a table with the key exists, including internal tables
|
|
2157
|
+
* @param key - the table key
|
|
2158
|
+
*/
|
|
2159
|
+
has(key) {
|
|
2160
|
+
return this.get(key) !== void 0;
|
|
2161
|
+
}
|
|
2162
|
+
/**
|
|
2163
|
+
* The sorted keys of all tables with the given type, including internal
|
|
2164
|
+
* @param type - the content type
|
|
2165
|
+
*/
|
|
2166
|
+
ofType(type) {
|
|
2167
|
+
return this._keys(true).filter((k) => this.get(k)._type === type);
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* The latest table config for the key, or undefined
|
|
2171
|
+
* @param key - the table key
|
|
2172
|
+
*/
|
|
2173
|
+
cfg(key) {
|
|
2174
|
+
return (this.get("tableCfgs")?._data ?? []).filter((c) => c.key === key).pop();
|
|
2175
|
+
}
|
|
2176
|
+
/**
|
|
2177
|
+
* The number of rows of the table, 0 if it does not exist
|
|
2178
|
+
* @param key - the table key
|
|
2179
|
+
*/
|
|
2180
|
+
rowCount(key) {
|
|
2181
|
+
return this.get(key)?._data.length ?? 0;
|
|
2182
|
+
}
|
|
2183
|
+
_keys(internal) {
|
|
2184
|
+
return Object.keys(this._rljson).filter((k) => this.has(k)).filter((k) => internal || !k.startsWith("_") && this.get(k)._type !== "insertHistory").sort();
|
|
2185
|
+
}
|
|
2186
|
+
static _isTable(value) {
|
|
2187
|
+
return typeof value === "object" && value !== null && typeof value._type === "string" && Array.isArray(value._data);
|
|
2188
|
+
}
|
|
2189
|
+
};
|
|
2190
|
+
//#endregion
|
|
2049
2191
|
//#region src/tools/ref.ts
|
|
2050
2192
|
// @license
|
|
2051
2193
|
/**
|
|
@@ -2932,4 +3074,4 @@ var Validate = class {
|
|
|
2932
3074
|
//#region src/index.ts
|
|
2933
3075
|
// @license
|
|
2934
3076
|
//#endregion
|
|
2935
|
-
export { BaseValidator, Example, InsertValidator, Route, Validate, ackPayloadExample, ackPayloadPartialExample, addColumnsToTableCfg, bakeryExample, clientId, clientIdExample, connectorPayloadExample, connectorPayloadFullExample, contentTypes, createCakeTableCfg, createEditHistoryTableCfg, createEditTableCfg, createInsertHistoryTableCfg, createLayerTableCfg, createMultiEditTableCfg, createSliceIdsTableCfg, createTreesTableCfg, exampleBuffetsTable, exampleCakesTable, exampleComponentsTable, exampleInsert, exampleInsertHistoryTable, exampleLayersTable, exampleRevision, exampleRljson, exampleSliceIdsTable, exampleTableCfg, exampleTableCfgTable, exampleTreesTable, exampleTypedefs, gapFillRequestExample, gapFillResponseExample, getTimeIdTimestamp, getTimeIdUniquePart, isClientId, isTimeId, isValidFieldName, iterateTables, iterateTablesSync, mergeSliceIds, ref, removeDuplicates, reservedFieldNames, reservedTableKeys, resolveSliceIds, routeRefSeperator, routeSliceIdIndicators, routeSliceIdSeperator, rowOf, syncConfigCausalOnlyExample, syncConfigDefault, syncConfigFullExample, syncEvents, throwOnInvalidTableCfg, timeId, treeFromObject, validateInsert, validateRljsonAgainstTableCfg };
|
|
3077
|
+
export { BaseValidator, Example, InsertValidator, Route, Tables, Validate, ackPayloadExample, ackPayloadPartialExample, addColumnsToTableCfg, bakeryExample, clientId, clientIdExample, compareRefStamp, connectorPayloadExample, connectorPayloadFullExample, contentTypes, createCakeTableCfg, createEditHistoryTableCfg, createEditTableCfg, createInsertHistoryTableCfg, createLayerTableCfg, createMultiEditTableCfg, createSliceIdsTableCfg, createTreesTableCfg, exampleBuffetsTable, exampleCakesTable, exampleComponentsTable, exampleInsert, exampleInsertHistoryTable, exampleLayersTable, exampleRevision, exampleRljson, exampleSliceIdsTable, exampleTableCfg, exampleTableCfgTable, exampleTreesTable, exampleTypedefs, gapFillRequestExample, gapFillResponseExample, getTimeIdTimestamp, getTimeIdUniquePart, isClientId, isRefStamp, isTimeId, isValidFieldName, iterateTables, iterateTablesSync, mergeSliceIds, ref, refStampExample, removeDuplicates, reservedFieldNames, reservedTableKeys, resolveSliceIds, routeRefSeperator, routeSliceIdIndicators, routeSliceIdSeperator, rowOf, stampPayloadExample, syncConfigCausalOnlyExample, syncConfigDefault, syncConfigFullExample, syncEvents, throwOnInvalidTableCfg, timeId, treeFromObject, validateInsert, validateRljsonAgainstTableCfg };
|
package/dist/rljson_version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The version of the `@rljson/rljson` package. */
|
|
2
|
-
export declare const rljsonVersion = "0.0.
|
|
2
|
+
export declare const rljsonVersion = "0.0.86";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { InsertHistoryTimeId } from '../insertHistory/insertHistory.ts';
|
|
2
2
|
import { ClientId } from './client-id.ts';
|
|
3
|
+
import { RefStamp } from './ref-stamp.ts';
|
|
3
4
|
/**
|
|
4
5
|
* Wire-protocol payload transmitted between Connector and Server.
|
|
5
6
|
*
|
|
@@ -17,6 +18,7 @@ import { ClientId } from './client-id.ts';
|
|
|
17
18
|
* | `seq` | Predecessor chain | Monotonic counter per (client, route) |
|
|
18
19
|
* | `p` | Predecessor chain | Causal predecessor timeIds |
|
|
19
20
|
* | `cksum` | Acknowledgment | Content checksum for ACK verification |
|
|
21
|
+
* | `stamp` | Hub order | Where a stamping hub placed the ref |
|
|
20
22
|
*/
|
|
21
23
|
export type ConnectorPayload = {
|
|
22
24
|
/** The ref (InsertHistoryTimeId) being announced. */
|
|
@@ -33,6 +35,13 @@ export type ConnectorPayload = {
|
|
|
33
35
|
p?: InsertHistoryTimeId[];
|
|
34
36
|
/** Content checksum of the referenced data, for ACK verification. */
|
|
35
37
|
cksum?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Where a stamping hub placed the ref in the order it relays them.
|
|
40
|
+
*
|
|
41
|
+
* Set by the first hub that relays the ref; a hub that receives a payload
|
|
42
|
+
* already carrying a stamp forwards it unchanged.
|
|
43
|
+
*/
|
|
44
|
+
stamp?: RefStamp;
|
|
36
45
|
};
|
|
37
46
|
/**
|
|
38
47
|
* Returns a minimal example ConnectorPayload (backward-compatible format).
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a ref stands in the order a hub relayed it — a fact about what the
|
|
3
|
+
* fleet saw, not a timestamp.
|
|
4
|
+
*
|
|
5
|
+
* A hub that stamps assigns one to every ref it relays on a route. The tuple
|
|
6
|
+
* orders lexicographically on `(domain, epoch, hub, n)`, see
|
|
7
|
+
* {@link compareRefStamp}. No field is read from a clock, so two machines with
|
|
8
|
+
* different wall clocks order the same two refs the same way.
|
|
9
|
+
*
|
|
10
|
+
* | Field | Meaning |
|
|
11
|
+
* |----------|----------------------------------------------------------|
|
|
12
|
+
* | `domain` | The network domain whose hub stamped the ref |
|
|
13
|
+
* | `epoch` | Advanced each time a hub takes office in that domain |
|
|
14
|
+
* | `hub` | The node id of the stamping hub |
|
|
15
|
+
* | `n` | Monotonic within `(domain, epoch, hub)` |
|
|
16
|
+
*
|
|
17
|
+
* `hub` keeps two hubs that believe they hold the same epoch — a split brain —
|
|
18
|
+
* distinct and still totally ordered.
|
|
19
|
+
*/
|
|
20
|
+
export type RefStamp = {
|
|
21
|
+
/** The network domain whose hub stamped the ref. */
|
|
22
|
+
domain: string;
|
|
23
|
+
/** Advanced each time a hub takes office in the domain. */
|
|
24
|
+
epoch: number;
|
|
25
|
+
/** The node id of the stamping hub. */
|
|
26
|
+
hub: string;
|
|
27
|
+
/** Monotonic within `(domain, epoch, hub)`. */
|
|
28
|
+
n: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Server → Client: the stamp a hub gave a ref the client announced.
|
|
32
|
+
*
|
|
33
|
+
* A hub forwards an announcement to every client except its sender, so the
|
|
34
|
+
* sender never sees its own stamp on the ref event. This payload, sent on the
|
|
35
|
+
* `${route}:stamp` event to the sender only, is how it learns it.
|
|
36
|
+
*/
|
|
37
|
+
export type StampPayload = {
|
|
38
|
+
/** The ref that was stamped. */
|
|
39
|
+
r: string;
|
|
40
|
+
/** The stamp the hub gave it. */
|
|
41
|
+
stamp: RefStamp;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Orders two stamps lexicographically on `(domain, epoch, hub, n)`.
|
|
45
|
+
*
|
|
46
|
+
* A total order: every pair compares the same way on every machine, and only
|
|
47
|
+
* equal stamps compare as `0`.
|
|
48
|
+
* @param a - The first stamp
|
|
49
|
+
* @param b - The second stamp
|
|
50
|
+
* @returns A negative number if `a` orders first, a positive number if `b`
|
|
51
|
+
* does, `0` if they are equal
|
|
52
|
+
*/
|
|
53
|
+
export declare const compareRefStamp: (a: RefStamp, b: RefStamp) => number;
|
|
54
|
+
/**
|
|
55
|
+
* Checks whether a value received from the wire is a well-formed RefStamp.
|
|
56
|
+
*
|
|
57
|
+
* A stamp arrives inside a JSON payload from another process, so it is
|
|
58
|
+
* checked before anything orders by it.
|
|
59
|
+
* @param value - The value to check
|
|
60
|
+
* @returns `true` if `value` has a string `domain` and `hub` and a
|
|
61
|
+
* non-negative integer `epoch` and `n`
|
|
62
|
+
*/
|
|
63
|
+
export declare const isRefStamp: (value: unknown) => value is RefStamp;
|
|
64
|
+
/**
|
|
65
|
+
* Returns an example RefStamp for documentation and testing.
|
|
66
|
+
*/
|
|
67
|
+
export declare const refStampExample: () => RefStamp;
|
|
68
|
+
/**
|
|
69
|
+
* Returns an example StampPayload for documentation and testing.
|
|
70
|
+
*/
|
|
71
|
+
export declare const stampPayloadExample: () => StampPayload;
|
|
@@ -23,6 +23,8 @@ export type SyncEventNames = {
|
|
|
23
23
|
gapFillRes: string;
|
|
24
24
|
/** Server → Client: bootstrap latest ref on connect / heartbeat. */
|
|
25
25
|
bootstrap: string;
|
|
26
|
+
/** Server → Client (sender only): the stamp its announced ref received. */
|
|
27
|
+
stamp: string;
|
|
26
28
|
};
|
|
27
29
|
/**
|
|
28
30
|
* Creates typed, route-specific socket event names for the sync protocol.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { TableCfg } from '../content/table-cfg.ts';
|
|
2
|
+
import { Rljson, TableType } from '../rljson.ts';
|
|
3
|
+
import { ContentType, TableKey } from '../typedefs.ts';
|
|
4
|
+
/** Options for Tables.ls */
|
|
5
|
+
export interface TablesLsOptions {
|
|
6
|
+
/** Return aligned lines `name type rowCount` instead of names */
|
|
7
|
+
long?: boolean;
|
|
8
|
+
/** Include insert history tables and tables starting with `_` */
|
|
9
|
+
internal?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Read only view on the tables of an Rljson dump.
|
|
13
|
+
*
|
|
14
|
+
* By default insert history tables and tables whose key starts with `_`
|
|
15
|
+
* are hidden. Pass `internal: true` to ls() to include them.
|
|
16
|
+
*/
|
|
17
|
+
export declare class Tables {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a view on the tables of the Rljson
|
|
20
|
+
* @param rljson - the Rljson dump to wrap
|
|
21
|
+
*/
|
|
22
|
+
constructor(rljson: Rljson);
|
|
23
|
+
private readonly _rljson;
|
|
24
|
+
/** The wrapped Rljson */
|
|
25
|
+
get rljson(): Rljson;
|
|
26
|
+
/**
|
|
27
|
+
* The sorted table keys.
|
|
28
|
+
* @param options - see TablesLsOptions
|
|
29
|
+
* @returns table keys, or aligned lines `name type rowCount` with `long`
|
|
30
|
+
*/
|
|
31
|
+
ls(options?: TablesLsOptions): string[];
|
|
32
|
+
/** The visible tables in ls() order */
|
|
33
|
+
get all(): TableType[];
|
|
34
|
+
/** The number of visible tables */
|
|
35
|
+
get count(): number;
|
|
36
|
+
/**
|
|
37
|
+
* The table with the given key or undefined
|
|
38
|
+
* @param key - the table key
|
|
39
|
+
*/
|
|
40
|
+
get(key: TableKey): TableType | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* True if a table with the key exists, including internal tables
|
|
43
|
+
* @param key - the table key
|
|
44
|
+
*/
|
|
45
|
+
has(key: TableKey): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The sorted keys of all tables with the given type, including internal
|
|
48
|
+
* @param type - the content type
|
|
49
|
+
*/
|
|
50
|
+
ofType(type: ContentType): TableKey[];
|
|
51
|
+
/**
|
|
52
|
+
* The latest table config for the key, or undefined
|
|
53
|
+
* @param key - the table key
|
|
54
|
+
*/
|
|
55
|
+
cfg(key: TableKey): TableCfg | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* The number of rows of the table, 0 if it does not exist
|
|
58
|
+
* @param key - the table key
|
|
59
|
+
*/
|
|
60
|
+
rowCount(key: TableKey): number;
|
|
61
|
+
private _keys;
|
|
62
|
+
private static _isTable;
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rljson/rljson",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"packageManager": "pnpm@11.10.0+sha512.0b7f8b98060031904c017e3a41eb187a16d40eeb829b95c4f8cb03681761fc4ab53dd219115b9b447f4dce1a05a214764461e7d3703392a9f32f9511ce8c86c8",
|
|
3
|
+
"version": "0.0.86",
|
|
5
4
|
"description": "The RLJSON data format specification",
|
|
6
5
|
"homepage": "https://github.com/rljson/rljson",
|
|
7
6
|
"bugs": "https://github.com/rljson/rljson/issues",
|
|
@@ -20,37 +19,36 @@
|
|
|
20
19
|
"dist"
|
|
21
20
|
],
|
|
22
21
|
"type": "module",
|
|
23
|
-
"scripts": {
|
|
24
|
-
"build": "pnpm test && vite build && tsc && cp README* dist && mkdir dist/src && cp src/example.ts dist/src",
|
|
25
|
-
"test": "vitest run --coverage && npm run lint",
|
|
26
|
-
"prepublishOnly": "npm run build",
|
|
27
|
-
"lint": "eslint",
|
|
28
|
-
"updateGoldens": "cross-env UPDATE_GOLDENS=true npm test"
|
|
29
|
-
},
|
|
30
22
|
"devDependencies": {
|
|
31
|
-
"@types/node": "^26.6.
|
|
32
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
33
|
-
"@typescript-eslint/parser": "^8.
|
|
34
|
-
"@vitest/coverage-v8": "^5.0.
|
|
23
|
+
"@types/node": "^26.6.4",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^8.71.1",
|
|
25
|
+
"@typescript-eslint/parser": "^8.71.1",
|
|
26
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
35
27
|
"cross-env": "^10.1.0",
|
|
36
|
-
"eslint": "^10.
|
|
37
|
-
"eslint-plugin-jsdoc": "^
|
|
38
|
-
"eslint-plugin-tsdoc": "^0.5.
|
|
39
|
-
"globals": "^17.
|
|
28
|
+
"eslint": "^10.12.0",
|
|
29
|
+
"eslint-plugin-jsdoc": "^65.2.1",
|
|
30
|
+
"eslint-plugin-tsdoc": "^0.5.4",
|
|
31
|
+
"globals": "^17.13.0",
|
|
40
32
|
"jsdoc": "^4.0.5",
|
|
41
33
|
"read-pkg": "^10.1.0",
|
|
42
34
|
"typescript": "~6.0.3",
|
|
43
|
-
"typescript-eslint": "^8.
|
|
44
|
-
"vite": "^8.3.
|
|
35
|
+
"typescript-eslint": "^8.71.1",
|
|
36
|
+
"vite": "^8.3.4",
|
|
45
37
|
"vite-node": "^6.0.0",
|
|
46
|
-
"vite-plugin-dts": "^5.1.
|
|
38
|
+
"vite-plugin-dts": "^5.1.2",
|
|
47
39
|
"vite-tsconfig-paths": "^6.1.1",
|
|
48
|
-
"vitest": "^5.0.
|
|
40
|
+
"vitest": "^5.0.3",
|
|
49
41
|
"vitest-dom": "^0.1.1"
|
|
50
42
|
},
|
|
51
43
|
"dependencies": {
|
|
52
44
|
"@rljson/hash": "0.0.19",
|
|
53
45
|
"@rljson/json": "0.0.23",
|
|
54
|
-
"nanoid": "^6.0.
|
|
46
|
+
"nanoid": "^6.0.2"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "pnpm test && vite build && tsc && cp README* dist && mkdir dist/src && cp src/example.ts dist/src",
|
|
50
|
+
"test": "vitest run --coverage && npm run lint",
|
|
51
|
+
"lint": "eslint",
|
|
52
|
+
"updateGoldens": "cross-env UPDATE_GOLDENS=true npm test"
|
|
55
53
|
}
|
|
56
|
-
}
|
|
54
|
+
}
|