@haikit/server 0.19.0 → 0.20.1
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.md +30 -0
- package/dist/routes.d.ts +8 -1
- package/dist/routes.d.ts.map +1 -1
- package/dist/routes.js +297 -92
- package/dist/routes.js.map +1 -1
- package/dist/runtime.d.ts +4 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +5 -3
- package/dist/runtime.js.map +1 -1
- package/dist/signals.d.ts +4 -3
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +5 -4
- package/dist/signals.js.map +1 -1
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +24 -1
- package/dist/store.js.map +1 -1
- package/package.json +2 -2
- package/src/routes.ts +314 -91
- package/src/runtime.ts +5 -3
- package/src/signals.ts +5 -4
- package/src/store.ts +24 -1
package/src/store.ts
CHANGED
|
@@ -9,6 +9,28 @@ import {
|
|
|
9
9
|
|
|
10
10
|
const LEASE_MS = 120_000;
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Postgres refuses U+0000 anywhere in text or jsonb, and a lone UTF-16
|
|
14
|
+
* surrogate in jsonb, so pgStore makes them storable as it saves. This store
|
|
15
|
+
* does the same, so development sees what production would store. Each
|
|
16
|
+
* becomes U+FFFD, the replacement character, in strings and object keys
|
|
17
|
+
* alike.
|
|
18
|
+
*/
|
|
19
|
+
const UNSTORABLE = /\u0000|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
|
|
20
|
+
const MAYBE_UNSTORABLE = /[\u0000\uD800-\uDFFF]/;
|
|
21
|
+
const storable = (s: string): string => (MAYBE_UNSTORABLE.test(s) ? s.replace(UNSTORABLE, "\uFFFD") : s);
|
|
22
|
+
/** A `JSON.stringify` replacer that makes every string and key storable. */
|
|
23
|
+
function storableJson(_key: string, value: unknown): unknown {
|
|
24
|
+
if (typeof value === "string") return storable(value);
|
|
25
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
26
|
+
const keys = Object.keys(value);
|
|
27
|
+
if (keys.some((k) => MAYBE_UNSTORABLE.test(k))) {
|
|
28
|
+
return Object.fromEntries(keys.map((k) => [storable(k), (value as Record<string, unknown>)[k]]));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
12
34
|
export interface MemoryStoreOptions {
|
|
13
35
|
/**
|
|
14
36
|
* Turn lease TTL. A process that dies mid-turn strands the conversation for
|
|
@@ -60,7 +82,8 @@ export function memoryStore(options: MemoryStoreOptions = {}): StoreAdapter {
|
|
|
60
82
|
let convSeq = 0;
|
|
61
83
|
let handleSeq = 0;
|
|
62
84
|
|
|
63
|
-
|
|
85
|
+
// and anything Postgres can't store comes back as pgStore would return it
|
|
86
|
+
const copy = <T>(value: T): T => JSON.parse(JSON.stringify(value, storableJson)) as T;
|
|
64
87
|
const newToken = () => globalThis.crypto.randomUUID();
|
|
65
88
|
|
|
66
89
|
/** Every durable mutation presents the token it was issued. A holder that has
|