@prisma/streams-server 0.0.1 → 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.
- package/CODE_OF_CONDUCT.md +45 -0
- package/CONTRIBUTING.md +68 -0
- package/LICENSE +201 -0
- package/README.md +39 -2
- package/SECURITY.md +33 -0
- package/bin/prisma-streams-server +2 -0
- package/package.json +29 -34
- package/src/app.ts +74 -0
- package/src/app_core.ts +1983 -0
- package/src/app_local.ts +46 -0
- package/src/backpressure.ts +66 -0
- package/src/bootstrap.ts +239 -0
- package/src/config.ts +251 -0
- package/src/db/db.ts +1440 -0
- package/src/db/schema.ts +619 -0
- package/src/expiry_sweeper.ts +44 -0
- package/src/hist.ts +169 -0
- package/src/index/binary_fuse.ts +379 -0
- package/src/index/indexer.ts +745 -0
- package/src/index/run_cache.ts +84 -0
- package/src/index/run_format.ts +213 -0
- package/src/ingest.ts +655 -0
- package/src/lens/lens.ts +501 -0
- package/src/manifest.ts +114 -0
- package/src/memory.ts +155 -0
- package/src/metrics.ts +161 -0
- package/src/metrics_emitter.ts +50 -0
- package/src/notifier.ts +64 -0
- package/src/objectstore/interface.ts +13 -0
- package/src/objectstore/mock_r2.ts +269 -0
- package/src/objectstore/null.ts +32 -0
- package/src/objectstore/r2.ts +128 -0
- package/src/offset.ts +70 -0
- package/src/reader.ts +454 -0
- package/src/runtime/hash.ts +156 -0
- package/src/runtime/hash_vendor/LICENSE.hash-wasm +38 -0
- package/src/runtime/hash_vendor/NOTICE.md +8 -0
- package/src/runtime/hash_vendor/xxhash3.umd.min.cjs +7 -0
- package/src/runtime/hash_vendor/xxhash32.umd.min.cjs +7 -0
- package/src/runtime/hash_vendor/xxhash64.umd.min.cjs +7 -0
- package/src/schema/lens_schema.ts +290 -0
- package/src/schema/proof.ts +547 -0
- package/src/schema/registry.ts +405 -0
- package/src/segment/cache.ts +179 -0
- package/src/segment/format.ts +331 -0
- package/src/segment/segmenter.ts +326 -0
- package/src/segment/segmenter_worker.ts +43 -0
- package/src/segment/segmenter_workers.ts +94 -0
- package/src/server.ts +326 -0
- package/src/sqlite/adapter.ts +164 -0
- package/src/stats.ts +205 -0
- package/src/touch/engine.ts +41 -0
- package/src/touch/interpreter_worker.ts +459 -0
- package/src/touch/live_keys.ts +118 -0
- package/src/touch/live_metrics.ts +858 -0
- package/src/touch/live_templates.ts +619 -0
- package/src/touch/manager.ts +1341 -0
- package/src/touch/naming.ts +13 -0
- package/src/touch/routing_key_notifier.ts +275 -0
- package/src/touch/spec.ts +526 -0
- package/src/touch/touch_journal.ts +671 -0
- package/src/touch/touch_key_id.ts +20 -0
- package/src/touch/worker_pool.ts +189 -0
- package/src/touch/worker_protocol.ts +58 -0
- package/src/types/proper-lockfile.d.ts +1 -0
- package/src/uploader.ts +317 -0
- package/src/util/base32_crockford.ts +81 -0
- package/src/util/bloom256.ts +67 -0
- package/src/util/cleanup.ts +22 -0
- package/src/util/crc32c.ts +29 -0
- package/src/util/ds_error.ts +15 -0
- package/src/util/duration.ts +17 -0
- package/src/util/endian.ts +53 -0
- package/src/util/json_pointer.ts +148 -0
- package/src/util/log.ts +25 -0
- package/src/util/lru.ts +45 -0
- package/src/util/retry.ts +35 -0
- package/src/util/siphash.ts +71 -0
- package/src/util/stream_paths.ts +31 -0
- package/src/util/time.ts +14 -0
- package/src/util/yield.ts +3 -0
- package/build/index.d.mts +0 -1
- package/build/index.d.ts +0 -1
- package/build/index.js +0 -0
- package/build/index.mjs +0 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { xxh3BigInt, xxh3Hex } from "../runtime/hash.ts";
|
|
2
|
+
export type TemplateEncoding = "string" | "int64" | "bool" | "datetime" | "bytes";
|
|
3
|
+
|
|
4
|
+
function utf8(s: string): Uint8Array {
|
|
5
|
+
return new TextEncoder().encode(s);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function encodeU64Be(v: bigint): Uint8Array {
|
|
9
|
+
const out = new Uint8Array(8);
|
|
10
|
+
let x = v;
|
|
11
|
+
for (let i = 7; i >= 0; i--) {
|
|
12
|
+
out[i] = Number(x & 0xffn);
|
|
13
|
+
x >>= 8n;
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function xxh3Low32(bytes: Uint8Array): number {
|
|
19
|
+
const h = xxh3BigInt(bytes);
|
|
20
|
+
return Number(h & 0xffffffffn) >>> 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function canonicalizeTemplateFields(fields: Array<{ name: string; encoding: TemplateEncoding }>): Array<{ name: string; encoding: TemplateEncoding }> {
|
|
24
|
+
const out = [...fields].map((f) => ({ name: f.name, encoding: f.encoding }));
|
|
25
|
+
out.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function templateIdFor(entity: string, fieldNamesSorted: string[]): string {
|
|
30
|
+
const parts: Uint8Array[] = [utf8("tpl\0"), utf8(entity), utf8("\0")];
|
|
31
|
+
for (let i = 0; i < fieldNamesSorted.length; i++) {
|
|
32
|
+
if (i > 0) parts.push(utf8("\0"));
|
|
33
|
+
parts.push(utf8(fieldNamesSorted[i]));
|
|
34
|
+
}
|
|
35
|
+
return xxh3Hex(concat(parts));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function tableKeyFor(entity: string): string {
|
|
39
|
+
return xxh3Hex(concat([utf8("tbl\0"), utf8(entity)]));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function tableKeyIdFor(entity: string): number {
|
|
43
|
+
return xxh3Low32(concat([utf8("tbl\0"), utf8(entity)]));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function templateKeyFor(templateIdHex16: string): string {
|
|
47
|
+
const tplBytes = encodeU64Be(BigInt(`0x${templateIdHex16}`));
|
|
48
|
+
return xxh3Hex(concat([utf8("tpl\0"), tplBytes]));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function templateKeyIdFor(templateIdHex16: string): number {
|
|
52
|
+
const tplBytes = encodeU64Be(BigInt(`0x${templateIdHex16}`));
|
|
53
|
+
return xxh3Low32(concat([utf8("tpl\0"), tplBytes]));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function watchKeyFor(templateIdHex16: string, encodedArgs: string[]): string {
|
|
57
|
+
const tplBytes = encodeU64Be(BigInt(`0x${templateIdHex16}`));
|
|
58
|
+
const parts: Uint8Array[] = [utf8("key\0"), tplBytes];
|
|
59
|
+
for (const a of encodedArgs) {
|
|
60
|
+
parts.push(utf8("\0"));
|
|
61
|
+
parts.push(utf8(a));
|
|
62
|
+
}
|
|
63
|
+
return xxh3Hex(concat(parts));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function watchKeyIdFor(templateIdHex16: string, encodedArgs: string[]): number {
|
|
67
|
+
const tplBytes = encodeU64Be(BigInt(`0x${templateIdHex16}`));
|
|
68
|
+
const parts: Uint8Array[] = [utf8("key\0"), tplBytes];
|
|
69
|
+
for (const a of encodedArgs) {
|
|
70
|
+
parts.push(utf8("\0"));
|
|
71
|
+
parts.push(utf8(a));
|
|
72
|
+
}
|
|
73
|
+
return xxh3Low32(concat(parts));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function encodeTemplateArg(value: unknown, encoding: TemplateEncoding): string | null {
|
|
77
|
+
if (value === null || value === undefined) return null;
|
|
78
|
+
switch (encoding) {
|
|
79
|
+
case "string": {
|
|
80
|
+
if (typeof value === "string") return value;
|
|
81
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
82
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
case "int64": {
|
|
86
|
+
if (typeof value === "bigint") return value.toString();
|
|
87
|
+
if (typeof value === "number" && Number.isFinite(value) && Number.isInteger(value)) return String(value);
|
|
88
|
+
if (typeof value === "string" && /^-?(0|[1-9][0-9]*)$/.test(value.trim())) return value.trim();
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
case "bool": {
|
|
92
|
+
if (typeof value !== "boolean") return null;
|
|
93
|
+
return value ? "1" : "0";
|
|
94
|
+
}
|
|
95
|
+
case "datetime": {
|
|
96
|
+
if (typeof value !== "string") return null;
|
|
97
|
+
const d = new Date(value);
|
|
98
|
+
if (!Number.isFinite(d.getTime())) return null;
|
|
99
|
+
return d.toISOString();
|
|
100
|
+
}
|
|
101
|
+
case "bytes": {
|
|
102
|
+
if (typeof value !== "string") return null;
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function concat(parts: Uint8Array[]): Uint8Array {
|
|
109
|
+
let total = 0;
|
|
110
|
+
for (const p of parts) total += p.byteLength;
|
|
111
|
+
const out = new Uint8Array(total);
|
|
112
|
+
let off = 0;
|
|
113
|
+
for (const p of parts) {
|
|
114
|
+
out.set(p, off);
|
|
115
|
+
off += p.byteLength;
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|