@graffy/common 0.16.0-alpha.9 → 0.16.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/index.cjs +64 -47
- package/index.mjs +64 -47
- package/package.json +3 -3
- package/types/ops/finalize.d.ts +8 -1
- package/types/ops/setVersion.d.ts +1 -1
package/index.cjs
CHANGED
|
@@ -14,13 +14,19 @@ function encode$6(string) {
|
|
|
14
14
|
function decode$6(u8Arr) {
|
|
15
15
|
return textDecoder.decode(u8Arr);
|
|
16
16
|
}
|
|
17
|
+
function TwosComplement(view) {
|
|
18
|
+
let lo = -view.getUint32(4) >>> 0;
|
|
19
|
+
const carry = lo ? 0 : -1;
|
|
20
|
+
let hi = ~view.getUint32(0) + carry >>> 0;
|
|
21
|
+
view.setUint32(0, hi);
|
|
22
|
+
view.setUint32(4, lo);
|
|
23
|
+
}
|
|
17
24
|
function encode$5(number) {
|
|
18
25
|
const buffer = new ArrayBuffer(8);
|
|
19
26
|
const view = new DataView(buffer);
|
|
20
27
|
view.setFloat64(0, number);
|
|
21
28
|
if (number < 0) {
|
|
22
|
-
|
|
23
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
29
|
+
TwosComplement(view);
|
|
24
30
|
} else {
|
|
25
31
|
view.setUint8(0, view.getUint8(0) | 128);
|
|
26
32
|
}
|
|
@@ -35,8 +41,7 @@ function decode$5(u8Arr) {
|
|
|
35
41
|
if (high & 128) {
|
|
36
42
|
view.setUint8(0, high & 127);
|
|
37
43
|
} else {
|
|
38
|
-
|
|
39
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
44
|
+
TwosComplement(view);
|
|
40
45
|
}
|
|
41
46
|
return view.getFloat64(0);
|
|
42
47
|
}
|
|
@@ -118,6 +123,9 @@ const stringifyDescriptor = {
|
|
|
118
123
|
}
|
|
119
124
|
};
|
|
120
125
|
function addStringify(buffer) {
|
|
126
|
+
if ("toJSON" in buffer || "toString" in buffer || Symbol.for("nodejs.util.inspect.custom") in buffer) {
|
|
127
|
+
return buffer;
|
|
128
|
+
}
|
|
121
129
|
Object.defineProperties(buffer, {
|
|
122
130
|
toJSON: stringifyDescriptor,
|
|
123
131
|
toString: stringifyDescriptor,
|
|
@@ -282,10 +290,12 @@ function keyStep(key) {
|
|
|
282
290
|
switch (key[l]) {
|
|
283
291
|
case 0:
|
|
284
292
|
newKey = key.slice(0, l);
|
|
293
|
+
addStringify(newKey);
|
|
285
294
|
step = 1;
|
|
286
295
|
break;
|
|
287
296
|
case 255:
|
|
288
297
|
newKey = key.slice(0, l);
|
|
298
|
+
addStringify(newKey);
|
|
289
299
|
newKey[l - 1]++;
|
|
290
300
|
step = -1;
|
|
291
301
|
break;
|
|
@@ -293,7 +303,7 @@ function keyStep(key) {
|
|
|
293
303
|
newKey = key;
|
|
294
304
|
step = 0;
|
|
295
305
|
}
|
|
296
|
-
return { key:
|
|
306
|
+
return { key: newKey, step };
|
|
297
307
|
}
|
|
298
308
|
function keyBefore(key) {
|
|
299
309
|
if (isMinKey(key) || isMaxKey(key))
|
|
@@ -1011,11 +1021,12 @@ function getNewerChange(node, base) {
|
|
|
1011
1021
|
return node.version >= base.version ? node : null;
|
|
1012
1022
|
}
|
|
1013
1023
|
}
|
|
1014
|
-
function setVersion(graph, version) {
|
|
1024
|
+
function setVersion(graph, version, onlyIfZero = false) {
|
|
1015
1025
|
for (const node of graph) {
|
|
1016
|
-
node.version
|
|
1026
|
+
if (!onlyIfZero || !node.version)
|
|
1027
|
+
node.version = version;
|
|
1017
1028
|
if (node.children)
|
|
1018
|
-
setVersion(node.children, version);
|
|
1029
|
+
setVersion(node.children, version, onlyIfZero);
|
|
1019
1030
|
}
|
|
1020
1031
|
return graph;
|
|
1021
1032
|
}
|
|
@@ -1037,13 +1048,13 @@ function getKnown(graph, version = 0) {
|
|
|
1037
1048
|
}
|
|
1038
1049
|
return query;
|
|
1039
1050
|
}
|
|
1040
|
-
function finalize(graph, query, version = Date.now()
|
|
1051
|
+
function finalize(graph, query, version = Date.now()) {
|
|
1041
1052
|
let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
|
|
1042
1053
|
merge(result, graph);
|
|
1043
1054
|
if (query)
|
|
1044
1055
|
result = slice(result, query).known || [];
|
|
1045
|
-
if (!
|
|
1046
|
-
result = setVersion(result, version);
|
|
1056
|
+
if (!version !== false)
|
|
1057
|
+
result = setVersion(result, version, true);
|
|
1047
1058
|
return result;
|
|
1048
1059
|
}
|
|
1049
1060
|
const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
|
|
@@ -1502,41 +1513,50 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1502
1513
|
var _a2;
|
|
1503
1514
|
if (!isDef(object))
|
|
1504
1515
|
return;
|
|
1516
|
+
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1517
|
+
if (typeof object === "object" && object && !Array.isArray(object)) {
|
|
1518
|
+
object = {
|
|
1519
|
+
...Object.fromEntries(
|
|
1520
|
+
Object.entries({
|
|
1521
|
+
$key,
|
|
1522
|
+
$ver,
|
|
1523
|
+
$ref,
|
|
1524
|
+
$val,
|
|
1525
|
+
$chi,
|
|
1526
|
+
$put
|
|
1527
|
+
}).filter(([_, val]) => isDef(val))
|
|
1528
|
+
),
|
|
1529
|
+
...props2
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1505
1532
|
if (typeof object === "object" && object && isEmpty(object))
|
|
1506
1533
|
return;
|
|
1507
|
-
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1508
1534
|
if (isDef($ver))
|
|
1509
1535
|
ver = $ver;
|
|
1510
1536
|
if (isPlainObject($key)) {
|
|
1511
1537
|
const [page, filter] = splitArgs($key);
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
ver
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
node2.
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
ver
|
|
1535
|
-
);
|
|
1536
|
-
if (!filter)
|
|
1537
|
-
node2.key = MIN_KEY;
|
|
1538
|
-
node2.prefix = true;
|
|
1539
|
-
return node2;
|
|
1538
|
+
if (page) {
|
|
1539
|
+
const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual__default.default(filter, putFilter)).map(([range]) => range);
|
|
1540
|
+
if (isGraph && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
|
|
1541
|
+
object.$key = filter || {};
|
|
1542
|
+
object.$put = foundPuts;
|
|
1543
|
+
const node2 = makeNode2(object, key, ver);
|
|
1544
|
+
if (!filter)
|
|
1545
|
+
node2.key = MIN_KEY;
|
|
1546
|
+
node2.prefix = true;
|
|
1547
|
+
return node2;
|
|
1548
|
+
}
|
|
1549
|
+
if ((!isDef(key) || Number.isInteger(key)) && (filter || isDef(page.$cursor))) {
|
|
1550
|
+
object.$key = isDef(page.$cursor) ? page.$cursor : page;
|
|
1551
|
+
const wrapper = { $key: filter || {}, $chi: [object] };
|
|
1552
|
+
if (isGraph)
|
|
1553
|
+
wrapper.$put = foundPuts;
|
|
1554
|
+
const node2 = makeNode2(wrapper, key, ver);
|
|
1555
|
+
if (!filter)
|
|
1556
|
+
node2.key = MIN_KEY;
|
|
1557
|
+
node2.prefix = true;
|
|
1558
|
+
return node2;
|
|
1559
|
+
}
|
|
1540
1560
|
}
|
|
1541
1561
|
}
|
|
1542
1562
|
let putQuery = [], prefixPuts = [];
|
|
@@ -1609,12 +1629,7 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1609
1629
|
}
|
|
1610
1630
|
}
|
|
1611
1631
|
if (isGraph && (putQuery === null || putQuery.length)) {
|
|
1612
|
-
node.children = finalize(
|
|
1613
|
-
node.children || [],
|
|
1614
|
-
putQuery,
|
|
1615
|
-
node.version,
|
|
1616
|
-
true
|
|
1617
|
-
);
|
|
1632
|
+
node.children = finalize(node.children || [], putQuery, false);
|
|
1618
1633
|
}
|
|
1619
1634
|
if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
|
|
1620
1635
|
return node;
|
|
@@ -1629,7 +1644,9 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1629
1644
|
return result;
|
|
1630
1645
|
}
|
|
1631
1646
|
function encodeGraph(obj, version = Date.now()) {
|
|
1632
|
-
|
|
1647
|
+
const encoded = encode(obj, { version, isGraph: true });
|
|
1648
|
+
const versioned = setVersion(encoded, version, true);
|
|
1649
|
+
return versioned;
|
|
1633
1650
|
}
|
|
1634
1651
|
function encodeQuery(obj, version = 0) {
|
|
1635
1652
|
return encode(obj, { version, isGraph: false });
|
package/index.mjs
CHANGED
|
@@ -9,13 +9,19 @@ function encode$6(string) {
|
|
|
9
9
|
function decode$6(u8Arr) {
|
|
10
10
|
return textDecoder.decode(u8Arr);
|
|
11
11
|
}
|
|
12
|
+
function TwosComplement(view) {
|
|
13
|
+
let lo = -view.getUint32(4) >>> 0;
|
|
14
|
+
const carry = lo ? 0 : -1;
|
|
15
|
+
let hi = ~view.getUint32(0) + carry >>> 0;
|
|
16
|
+
view.setUint32(0, hi);
|
|
17
|
+
view.setUint32(4, lo);
|
|
18
|
+
}
|
|
12
19
|
function encode$5(number) {
|
|
13
20
|
const buffer = new ArrayBuffer(8);
|
|
14
21
|
const view = new DataView(buffer);
|
|
15
22
|
view.setFloat64(0, number);
|
|
16
23
|
if (number < 0) {
|
|
17
|
-
|
|
18
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
24
|
+
TwosComplement(view);
|
|
19
25
|
} else {
|
|
20
26
|
view.setUint8(0, view.getUint8(0) | 128);
|
|
21
27
|
}
|
|
@@ -30,8 +36,7 @@ function decode$5(u8Arr) {
|
|
|
30
36
|
if (high & 128) {
|
|
31
37
|
view.setUint8(0, high & 127);
|
|
32
38
|
} else {
|
|
33
|
-
|
|
34
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
39
|
+
TwosComplement(view);
|
|
35
40
|
}
|
|
36
41
|
return view.getFloat64(0);
|
|
37
42
|
}
|
|
@@ -113,6 +118,9 @@ const stringifyDescriptor = {
|
|
|
113
118
|
}
|
|
114
119
|
};
|
|
115
120
|
function addStringify(buffer) {
|
|
121
|
+
if ("toJSON" in buffer || "toString" in buffer || Symbol.for("nodejs.util.inspect.custom") in buffer) {
|
|
122
|
+
return buffer;
|
|
123
|
+
}
|
|
116
124
|
Object.defineProperties(buffer, {
|
|
117
125
|
toJSON: stringifyDescriptor,
|
|
118
126
|
toString: stringifyDescriptor,
|
|
@@ -277,10 +285,12 @@ function keyStep(key) {
|
|
|
277
285
|
switch (key[l]) {
|
|
278
286
|
case 0:
|
|
279
287
|
newKey = key.slice(0, l);
|
|
288
|
+
addStringify(newKey);
|
|
280
289
|
step = 1;
|
|
281
290
|
break;
|
|
282
291
|
case 255:
|
|
283
292
|
newKey = key.slice(0, l);
|
|
293
|
+
addStringify(newKey);
|
|
284
294
|
newKey[l - 1]++;
|
|
285
295
|
step = -1;
|
|
286
296
|
break;
|
|
@@ -288,7 +298,7 @@ function keyStep(key) {
|
|
|
288
298
|
newKey = key;
|
|
289
299
|
step = 0;
|
|
290
300
|
}
|
|
291
|
-
return { key:
|
|
301
|
+
return { key: newKey, step };
|
|
292
302
|
}
|
|
293
303
|
function keyBefore(key) {
|
|
294
304
|
if (isMinKey(key) || isMaxKey(key))
|
|
@@ -1006,11 +1016,12 @@ function getNewerChange(node, base) {
|
|
|
1006
1016
|
return node.version >= base.version ? node : null;
|
|
1007
1017
|
}
|
|
1008
1018
|
}
|
|
1009
|
-
function setVersion(graph, version) {
|
|
1019
|
+
function setVersion(graph, version, onlyIfZero = false) {
|
|
1010
1020
|
for (const node of graph) {
|
|
1011
|
-
node.version
|
|
1021
|
+
if (!onlyIfZero || !node.version)
|
|
1022
|
+
node.version = version;
|
|
1012
1023
|
if (node.children)
|
|
1013
|
-
setVersion(node.children, version);
|
|
1024
|
+
setVersion(node.children, version, onlyIfZero);
|
|
1014
1025
|
}
|
|
1015
1026
|
return graph;
|
|
1016
1027
|
}
|
|
@@ -1032,13 +1043,13 @@ function getKnown(graph, version = 0) {
|
|
|
1032
1043
|
}
|
|
1033
1044
|
return query;
|
|
1034
1045
|
}
|
|
1035
|
-
function finalize(graph, query, version = Date.now()
|
|
1046
|
+
function finalize(graph, query, version = Date.now()) {
|
|
1036
1047
|
let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
|
|
1037
1048
|
merge(result, graph);
|
|
1038
1049
|
if (query)
|
|
1039
1050
|
result = slice(result, query).known || [];
|
|
1040
|
-
if (!
|
|
1041
|
-
result = setVersion(result, version);
|
|
1051
|
+
if (!version !== false)
|
|
1052
|
+
result = setVersion(result, version, true);
|
|
1042
1053
|
return result;
|
|
1043
1054
|
}
|
|
1044
1055
|
const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
|
|
@@ -1497,41 +1508,50 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1497
1508
|
var _a2;
|
|
1498
1509
|
if (!isDef(object))
|
|
1499
1510
|
return;
|
|
1511
|
+
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1512
|
+
if (typeof object === "object" && object && !Array.isArray(object)) {
|
|
1513
|
+
object = {
|
|
1514
|
+
...Object.fromEntries(
|
|
1515
|
+
Object.entries({
|
|
1516
|
+
$key,
|
|
1517
|
+
$ver,
|
|
1518
|
+
$ref,
|
|
1519
|
+
$val,
|
|
1520
|
+
$chi,
|
|
1521
|
+
$put
|
|
1522
|
+
}).filter(([_, val]) => isDef(val))
|
|
1523
|
+
),
|
|
1524
|
+
...props2
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1500
1527
|
if (typeof object === "object" && object && isEmpty(object))
|
|
1501
1528
|
return;
|
|
1502
|
-
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1503
1529
|
if (isDef($ver))
|
|
1504
1530
|
ver = $ver;
|
|
1505
1531
|
if (isPlainObject($key)) {
|
|
1506
1532
|
const [page, filter] = splitArgs($key);
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
ver
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
node2.
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
ver
|
|
1530
|
-
);
|
|
1531
|
-
if (!filter)
|
|
1532
|
-
node2.key = MIN_KEY;
|
|
1533
|
-
node2.prefix = true;
|
|
1534
|
-
return node2;
|
|
1533
|
+
if (page) {
|
|
1534
|
+
const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual(filter, putFilter)).map(([range]) => range);
|
|
1535
|
+
if (isGraph && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
|
|
1536
|
+
object.$key = filter || {};
|
|
1537
|
+
object.$put = foundPuts;
|
|
1538
|
+
const node2 = makeNode2(object, key, ver);
|
|
1539
|
+
if (!filter)
|
|
1540
|
+
node2.key = MIN_KEY;
|
|
1541
|
+
node2.prefix = true;
|
|
1542
|
+
return node2;
|
|
1543
|
+
}
|
|
1544
|
+
if ((!isDef(key) || Number.isInteger(key)) && (filter || isDef(page.$cursor))) {
|
|
1545
|
+
object.$key = isDef(page.$cursor) ? page.$cursor : page;
|
|
1546
|
+
const wrapper = { $key: filter || {}, $chi: [object] };
|
|
1547
|
+
if (isGraph)
|
|
1548
|
+
wrapper.$put = foundPuts;
|
|
1549
|
+
const node2 = makeNode2(wrapper, key, ver);
|
|
1550
|
+
if (!filter)
|
|
1551
|
+
node2.key = MIN_KEY;
|
|
1552
|
+
node2.prefix = true;
|
|
1553
|
+
return node2;
|
|
1554
|
+
}
|
|
1535
1555
|
}
|
|
1536
1556
|
}
|
|
1537
1557
|
let putQuery = [], prefixPuts = [];
|
|
@@ -1604,12 +1624,7 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1604
1624
|
}
|
|
1605
1625
|
}
|
|
1606
1626
|
if (isGraph && (putQuery === null || putQuery.length)) {
|
|
1607
|
-
node.children = finalize(
|
|
1608
|
-
node.children || [],
|
|
1609
|
-
putQuery,
|
|
1610
|
-
node.version,
|
|
1611
|
-
true
|
|
1612
|
-
);
|
|
1627
|
+
node.children = finalize(node.children || [], putQuery, false);
|
|
1613
1628
|
}
|
|
1614
1629
|
if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
|
|
1615
1630
|
return node;
|
|
@@ -1624,7 +1639,9 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1624
1639
|
return result;
|
|
1625
1640
|
}
|
|
1626
1641
|
function encodeGraph(obj, version = Date.now()) {
|
|
1627
|
-
|
|
1642
|
+
const encoded = encode(obj, { version, isGraph: true });
|
|
1643
|
+
const versioned = setVersion(encoded, version, true);
|
|
1644
|
+
return versioned;
|
|
1628
1645
|
}
|
|
1629
1646
|
function encodeQuery(obj, version = 0) {
|
|
1630
1647
|
return encode(obj, { version, isGraph: false });
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graffy/common",
|
|
3
3
|
"description": "Common libraries that used by various Graffy modules.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.16.0
|
|
5
|
+
"version": "0.16.0",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./index.mjs",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"lodash": "^4.17.19",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
20
|
+
"merge-async-iterators": "^0.2.1",
|
|
21
|
+
"@graffy/stream": "0.16.0"
|
|
22
22
|
}
|
|
23
23
|
}
|
package/types/ops/finalize.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param {any} graph
|
|
4
|
+
* @param {any} query
|
|
5
|
+
* @param {number | false} version
|
|
6
|
+
* @returns any
|
|
7
|
+
*/
|
|
8
|
+
export default function finalize(graph: any, query: any, version?: number | false): {
|
|
2
9
|
key: Uint8Array;
|
|
3
10
|
end: Uint8Array;
|
|
4
11
|
version: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default function setVersion(graph: any, version: any): any;
|
|
1
|
+
export default function setVersion(graph: any, version: any, onlyIfZero?: boolean): any;
|