@loro-dev/flock-sqlite 0.1.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.
@@ -0,0 +1,106 @@
1
+ import { decode_key, encode_key } from "./memcomparable";
2
+ import type { KeyPart } from "./types";
3
+
4
+ type EncodedKey = Uint8Array;
5
+
6
+ type KeyJsPart =
7
+ | { $tag: 0; _0: number }
8
+ | { $tag: 1; _0: string }
9
+ | { $tag: 2; _0: unknown }
10
+ | { $tag: 3 }
11
+ | { $tag: 4 };
12
+
13
+ function assertValidKeyPart(part: KeyPart): KeyJsPart {
14
+ if (typeof part === "number") {
15
+ if (!Number.isFinite(part)) {
16
+ throw new TypeError("Key parts must be finite numbers");
17
+ }
18
+ return { $tag: 0, _0: part };
19
+ }
20
+ if (typeof part === "string") {
21
+ return { $tag: 1, _0: part };
22
+ }
23
+ if (part === true) {
24
+ return { $tag: 3 };
25
+ }
26
+ if (part === false) {
27
+ return { $tag: 4 };
28
+ }
29
+ throw new TypeError("Key parts must be strings, numbers, or booleans");
30
+ }
31
+
32
+ function parseBytesLike(value: unknown): Uint8Array {
33
+ if (value instanceof Uint8Array) {
34
+ return value;
35
+ }
36
+ if (value && typeof value === "object") {
37
+ const buf = (value as { buf?: unknown }).buf;
38
+ if (buf instanceof Uint8Array) {
39
+ const start = (value as { start?: number }).start ?? 0;
40
+ const end = (value as { end?: number }).end ?? buf.length;
41
+ return buf.subarray(start, end);
42
+ }
43
+ }
44
+ throw new TypeError("Invalid bytes payload in memcomparable decode");
45
+ }
46
+
47
+ function fromJsPart(part: unknown): KeyPart {
48
+ const tag = (part as { $tag?: number } | null)?.$tag;
49
+ switch (tag) {
50
+ case 0:
51
+ return (part as { _0: number })._0;
52
+ case 1:
53
+ return (part as { _0: string })._0;
54
+ case 3:
55
+ return true;
56
+ case 4:
57
+ return false;
58
+ case 2:
59
+ // Bytes are not part of the public key surface; surface them as a binary blob for diagnostics.
60
+ return parseBytesLike((part as { _0: unknown })._0) as unknown as KeyPart;
61
+ default:
62
+ throw new TypeError("Unsupported memcomparable key part");
63
+ }
64
+ }
65
+
66
+ export function encodeKeyParts(parts: KeyPart[]): EncodedKey {
67
+ const normalized = parts.map(assertValidKeyPart);
68
+ return encode_key(normalized) as EncodedKey;
69
+ }
70
+
71
+ export function decodeKeyParts(bytes: EncodedKey): KeyPart[] {
72
+ const decoded = decode_key(bytes as unknown as Uint8Array) as Array<unknown>;
73
+ return decoded.map((part) => fromJsPart(part));
74
+ }
75
+
76
+ export function compareBytes(a: Uint8Array, b: Uint8Array): number {
77
+ const min = Math.min(a.length, b.length);
78
+ for (let i = 0; i < min; i += 1) {
79
+ if (a[i] !== b[i]) {
80
+ return a[i] < b[i] ? -1 : 1;
81
+ }
82
+ }
83
+ if (a.length === b.length) {
84
+ return 0;
85
+ }
86
+ return a.length < b.length ? -1 : 1;
87
+ }
88
+
89
+ export function prefixUpperBound(bytes: Uint8Array): Uint8Array | undefined {
90
+ if (bytes.length === 0) {
91
+ return undefined;
92
+ }
93
+ const data = Array.from(bytes);
94
+ for (let i = data.length - 1; i >= 0; i -= 1) {
95
+ const current = data[i];
96
+ if (current < 0xff) {
97
+ data[i] = current + 1;
98
+ return new Uint8Array(data.slice(0, i + 1));
99
+ }
100
+ }
101
+ return undefined;
102
+ }
103
+
104
+ export function keyToString(parts: KeyPart[]): string {
105
+ return JSON.stringify(parts);
106
+ }
@@ -0,0 +1,5 @@
1
+ import type * as MoonBit from "./moonbit.d.ts";
2
+
3
+ export function decode_key(bytes: MoonBit.Bytes): any;
4
+
5
+ export function encode_key(parts: any): MoonBit.Bytes;