statewire 0.0.1 → 0.0.2
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 +14 -13
- package/dist/client/changes.d.ts +2 -2
- package/dist/client/changes.d.ts.map +1 -1
- package/dist/client/changes.js +3 -10
- package/dist/client/changes.js.map +1 -1
- package/dist/client/connection.d.ts +18 -39
- package/dist/client/connection.d.ts.map +1 -1
- package/dist/client/connection.js +61 -54
- package/dist/client/connection.js.map +1 -1
- package/dist/client/core.d.ts +7 -11
- package/dist/client/core.d.ts.map +1 -1
- package/dist/client/core.js +58 -55
- package/dist/client/core.js.map +1 -1
- package/dist/client/session.d.ts +8 -25
- package/dist/client/session.d.ts.map +1 -1
- package/dist/client/session.js +56 -36
- package/dist/client/session.js.map +1 -1
- package/dist/client/sessionStore.d.ts +1 -1
- package/dist/client/sessionStore.js.map +1 -1
- package/dist/client/timing.d.ts +2 -1
- package/dist/client/timing.d.ts.map +1 -1
- package/dist/client/timing.js +5 -1
- package/dist/client/timing.js.map +1 -1
- package/dist/host/adapter.d.ts +8 -0
- package/dist/host/adapter.d.ts.map +1 -0
- package/dist/host/adapter.js +23 -0
- package/dist/host/adapter.js.map +1 -0
- package/dist/host/draft.d.ts +21 -7
- package/dist/host/draft.d.ts.map +1 -1
- package/dist/host/draft.js +190 -103
- package/dist/host/draft.js.map +1 -1
- package/dist/host/host.d.ts +18 -35
- package/dist/host/host.d.ts.map +1 -1
- package/dist/host/host.js +81 -53
- package/dist/host/host.js.map +1 -1
- package/dist/host/socket.d.ts +14 -0
- package/dist/host/socket.d.ts.map +1 -0
- package/dist/host/socket.js +459 -0
- package/dist/host/socket.js.map +1 -0
- package/dist/host-internal.d.ts +27 -0
- package/dist/host-internal.d.ts.map +1 -0
- package/dist/host-internal.js +3 -0
- package/dist/host.d.ts +44 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +4 -0
- package/dist/index.d.ts +5 -28
- package/dist/index.js +2 -3
- package/dist/statewire.d.ts +94 -10
- package/dist/statewire.d.ts.map +1 -1
- package/dist/statewire.js +23 -35
- package/dist/statewire.js.map +1 -1
- package/dist/transports/fake-sse.d.ts +12 -0
- package/dist/transports/fake-sse.d.ts.map +1 -0
- package/dist/transports/fake-sse.js +39 -0
- package/dist/transports/fake-sse.js.map +1 -0
- package/dist/transports/fixtures.d.ts +33 -0
- package/dist/transports/fixtures.d.ts.map +1 -0
- package/dist/transports/fixtures.js +50 -0
- package/dist/transports/fixtures.js.map +1 -0
- package/dist/transports/remote.d.ts +6 -13
- package/dist/transports/remote.d.ts.map +1 -1
- package/dist/transports/remote.js +5 -24
- package/dist/transports/remote.js.map +1 -1
- package/dist/transports/sse.d.ts +10 -20
- package/dist/transports/sse.d.ts.map +1 -1
- package/dist/transports/sse.js +42 -41
- package/dist/transports/sse.js.map +1 -1
- package/dist/transports/testing.d.ts +4 -3
- package/dist/transports/testing.d.ts.map +1 -1
- package/dist/transports/testing.js +19 -46
- package/dist/transports/testing.js.map +1 -1
- package/dist/transports/ws.d.ts +4 -13
- package/dist/transports/ws.d.ts.map +1 -1
- package/dist/transports/ws.js +57 -60
- package/dist/transports/ws.js.map +1 -1
- package/dist/types.d.ts +53 -100
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/util/draftTraps.d.ts +6 -0
- package/dist/util/draftTraps.d.ts.map +1 -0
- package/dist/util/draftTraps.js +25 -0
- package/dist/util/draftTraps.js.map +1 -0
- package/dist/util/macrotask.d.ts +5 -0
- package/dist/util/macrotask.d.ts.map +1 -0
- package/dist/util/macrotask.js +25 -0
- package/dist/util/macrotask.js.map +1 -0
- package/dist/util/methodProxy.d.ts +5 -0
- package/dist/util/methodProxy.d.ts.map +1 -0
- package/dist/util/methodProxy.js +17 -0
- package/dist/util/methodProxy.js.map +1 -0
- package/dist/util/randomId.d.ts +5 -0
- package/dist/util/randomId.d.ts.map +1 -0
- package/dist/util/randomId.js +6 -0
- package/dist/util/randomId.js.map +1 -0
- package/dist/{client/optimistic.d.ts → util/recordMutations.d.ts} +2 -2
- package/dist/util/recordMutations.d.ts.map +1 -0
- package/dist/{client/optimistic.js → util/recordMutations.js} +5 -24
- package/dist/util/recordMutations.js.map +1 -0
- package/dist/util/statePath.d.ts +14 -0
- package/dist/util/statePath.d.ts.map +1 -0
- package/dist/util/statePath.js +31 -0
- package/dist/util/statePath.js.map +1 -0
- package/dist/wire/meta.d.ts +6 -1
- package/dist/wire/meta.d.ts.map +1 -1
- package/dist/wire/meta.js +30 -28
- package/dist/wire/meta.js.map +1 -1
- package/dist/wire/ops.d.ts +5 -26
- package/dist/wire/ops.d.ts.map +1 -1
- package/dist/wire/ops.js +67 -80
- package/dist/wire/ops.js.map +1 -1
- package/package.json +15 -11
- package/src/client/changes.ts +5 -15
- package/src/client/connection.ts +87 -113
- package/src/client/core.ts +57 -91
- package/src/client/session.ts +87 -70
- package/src/client/sessionStore.ts +1 -1
- package/src/client/timing.ts +6 -0
- package/src/host/adapter.ts +33 -0
- package/src/host/draft.ts +293 -125
- package/src/host/host.ts +107 -106
- package/src/host/socket.ts +542 -0
- package/src/host-internal.ts +28 -0
- package/src/host.ts +61 -0
- package/src/index.ts +2 -62
- package/src/statewire.ts +154 -61
- package/src/transports/fake-sse.ts +51 -0
- package/src/transports/fixtures.ts +60 -0
- package/src/transports/remote.ts +12 -54
- package/src/transports/sse.ts +68 -81
- package/src/transports/testing.ts +27 -59
- package/src/transports/ws.ts +76 -92
- package/src/types.ts +50 -131
- package/src/util/draftTraps.ts +32 -0
- package/src/util/macrotask.ts +25 -0
- package/src/util/methodProxy.ts +16 -0
- package/src/util/randomId.ts +4 -0
- package/src/{client/optimistic.ts → util/recordMutations.ts} +0 -0
- package/src/util/statePath.ts +61 -0
- package/src/wire/meta.ts +42 -47
- package/src/wire/ops.ts +107 -106
- package/LICENSE +0 -21
- package/dist/client/optimistic.d.ts.map +0 -1
- package/dist/client/optimistic.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/server.d.ts +0 -15
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -3
- package/src/server.ts +0 -32
package/src/wire/meta.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { walkStatePath } from "../util/statePath";
|
|
2
|
+
|
|
1
3
|
export type StatewireMetaEntry = {
|
|
2
4
|
path: (string | number)[];
|
|
3
5
|
type: string;
|
|
@@ -24,40 +26,19 @@ export const reviveValue = (value: unknown, meta: unknown): unknown => {
|
|
|
24
26
|
typeof entry === "object" && entry !== null ? entry.path : null;
|
|
25
27
|
if (!isPath(path)) return fault("meta entry must carry a path array");
|
|
26
28
|
if (entry.type !== "date") continue;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
for (const seg of path) {
|
|
31
|
-
parent = node;
|
|
32
|
-
key = seg;
|
|
33
|
-
if (
|
|
34
|
-
typeof seg === "string" &&
|
|
35
|
-
typeof node === "object" &&
|
|
36
|
-
node !== null &&
|
|
37
|
-
!Array.isArray(node) &&
|
|
38
|
-
Object.hasOwn(node, seg)
|
|
39
|
-
) {
|
|
40
|
-
node = (node as Record<string, unknown>)[seg];
|
|
41
|
-
} else if (
|
|
42
|
-
typeof seg === "number" &&
|
|
43
|
-
Array.isArray(node) &&
|
|
44
|
-
seg >= 0 &&
|
|
45
|
-
seg < node.length
|
|
46
|
-
) {
|
|
47
|
-
node = node[seg];
|
|
48
|
-
} else {
|
|
49
|
-
return fault(`meta path ${path.join(".")} does not resolve`);
|
|
50
|
-
}
|
|
29
|
+
const walked = walkStatePath(value, path, "resolve");
|
|
30
|
+
if (walked === undefined) {
|
|
31
|
+
return fault(`meta path ${path.join(".")} does not resolve`);
|
|
51
32
|
}
|
|
52
|
-
if (typeof
|
|
33
|
+
if (typeof walked.leaf !== "string") {
|
|
53
34
|
return fault(`meta path ${path.join(".")} must name a string`);
|
|
54
35
|
}
|
|
55
|
-
const date = new Date(
|
|
36
|
+
const date = new Date(walked.leaf);
|
|
56
37
|
if (Number.isNaN(date.getTime())) {
|
|
57
38
|
return fault(`invalid date at meta path ${path.join(".")}`);
|
|
58
39
|
}
|
|
59
|
-
if (parent ===
|
|
60
|
-
(parent as Record<string | number, unknown>)[key] = date;
|
|
40
|
+
if (walked.parent === undefined) return date;
|
|
41
|
+
(walked.parent as Record<string | number, unknown>)[walked.key] = date;
|
|
61
42
|
}
|
|
62
43
|
return value;
|
|
63
44
|
};
|
|
@@ -77,30 +58,44 @@ export const reviveResponse = <T extends { payload?: unknown; meta?: unknown }>(
|
|
|
77
58
|
return { ...rest, payload: reviveValue(rest.payload, meta) } as T;
|
|
78
59
|
};
|
|
79
60
|
|
|
61
|
+
const walkDates = (
|
|
62
|
+
node: unknown,
|
|
63
|
+
path: (string | number)[],
|
|
64
|
+
meta: StatewireMetaEntry[],
|
|
65
|
+
): unknown => {
|
|
66
|
+
if (node instanceof Date) {
|
|
67
|
+
meta.push({ path, type: "date" });
|
|
68
|
+
return node.toISOString();
|
|
69
|
+
}
|
|
70
|
+
if (Array.isArray(node)) {
|
|
71
|
+
return node.map((item, i) => walkDates(item, [...path, i], meta));
|
|
72
|
+
}
|
|
73
|
+
if (typeof node === "object" && node !== null) {
|
|
74
|
+
return Object.fromEntries(
|
|
75
|
+
Object.keys(node).map((key) => [
|
|
76
|
+
key,
|
|
77
|
+
walkDates((node as Record<string, unknown>)[key], [...path, key], meta),
|
|
78
|
+
]),
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return node;
|
|
82
|
+
};
|
|
83
|
+
|
|
80
84
|
/** Encode Date instances inside command params as canonical ISO strings,
|
|
81
85
|
* recording their paths (starting at the param index) in meta. */
|
|
82
86
|
export const encodeParams = (
|
|
83
87
|
params: readonly unknown[],
|
|
84
88
|
): { params: unknown[]; meta?: StatewireMetaEntry[] } => {
|
|
85
89
|
const meta: StatewireMetaEntry[] = [];
|
|
86
|
-
const
|
|
87
|
-
if (node instanceof Date) {
|
|
88
|
-
meta.push({ path, type: "date" });
|
|
89
|
-
return node.toISOString();
|
|
90
|
-
}
|
|
91
|
-
if (Array.isArray(node)) {
|
|
92
|
-
return node.map((item, i) => walk(item, [...path, i]));
|
|
93
|
-
}
|
|
94
|
-
if (typeof node === "object" && node !== null) {
|
|
95
|
-
return Object.fromEntries(
|
|
96
|
-
Object.keys(node).map((key) => [
|
|
97
|
-
key,
|
|
98
|
-
walk((node as Record<string, unknown>)[key], [...path, key]),
|
|
99
|
-
]),
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
return node;
|
|
103
|
-
};
|
|
104
|
-
const encoded = params.map((param, i) => walk(param, [i]));
|
|
90
|
+
const encoded = params.map((param, i) => walkDates(param, [i], meta));
|
|
105
91
|
return meta.length > 0 ? { params: encoded, meta } : { params: encoded };
|
|
106
92
|
};
|
|
93
|
+
|
|
94
|
+
/** Encode Date instances inside a wire value, recording their paths in meta. */
|
|
95
|
+
export const encodeValue = (
|
|
96
|
+
value: unknown,
|
|
97
|
+
): { value: unknown; meta?: StatewireMetaEntry[] } => {
|
|
98
|
+
const meta: StatewireMetaEntry[] = [];
|
|
99
|
+
const encoded = walkDates(value, [], meta);
|
|
100
|
+
return meta.length > 0 ? { value: encoded, meta } : { value: encoded };
|
|
101
|
+
};
|
package/src/wire/ops.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import { recordMutations } from "../
|
|
1
|
+
import { recordMutations } from "../util/recordMutations";
|
|
2
|
+
import { isUnsafeKey, walkStatePath } from "../util/statePath";
|
|
3
|
+
import type { Statewire } from "../types";
|
|
2
4
|
|
|
3
5
|
export type StatewirePath = readonly (string | number)[];
|
|
4
6
|
|
|
5
|
-
export type StatewireOp =
|
|
6
|
-
| { op: "replace"; path: StatewirePath; value: unknown }
|
|
7
|
-
| { op: "add"; path: StatewirePath; value: unknown }
|
|
8
|
-
| { op: "append"; path: StatewirePath; value: unknown }
|
|
9
|
-
| { op: "remove"; path: StatewirePath }
|
|
10
|
-
| { op: "event"; value: object };
|
|
11
|
-
|
|
12
|
-
const isUnsafe = (key: string, parent: string | number | undefined) =>
|
|
13
|
-
key === "__proto__" || (key === "prototype" && parent === "constructor");
|
|
14
|
-
|
|
15
7
|
const warnStripped = (key: string, parent: string | undefined) => {
|
|
16
8
|
if (
|
|
17
9
|
typeof process === "undefined" ||
|
|
@@ -24,58 +16,102 @@ const warnStripped = (key: string, parent: string | undefined) => {
|
|
|
24
16
|
}
|
|
25
17
|
};
|
|
26
18
|
|
|
27
|
-
|
|
28
|
-
|
|
19
|
+
const unserializable = (
|
|
20
|
+
kind: "function" | "undefined",
|
|
21
|
+
path: readonly (string | number)[],
|
|
22
|
+
): never => {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`statewire: cannot serialize ${kind}` +
|
|
25
|
+
(path.length === 0 ? "" : ` at ${path.join(".")}`),
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const sanitizeAt = (
|
|
30
|
+
value: unknown,
|
|
31
|
+
parent: string | undefined,
|
|
32
|
+
path: readonly (string | number)[],
|
|
33
|
+
): unknown => {
|
|
29
34
|
if (value instanceof Date) return new Date(value.getTime());
|
|
30
|
-
if (Array.isArray(value))
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
if (Array.isArray(value)) {
|
|
36
|
+
return value.map((item, index) => {
|
|
37
|
+
if (item === undefined) unserializable("undefined", [...path, index]);
|
|
38
|
+
return sanitizeAt(item, undefined, [...path, index]);
|
|
39
|
+
});
|
|
35
40
|
}
|
|
36
|
-
|
|
41
|
+
if (typeof value === "object" && value !== null) {
|
|
42
|
+
const out: Record<string, unknown> = {};
|
|
43
|
+
for (const key of Object.keys(value)) {
|
|
44
|
+
if (isUnsafeKey(key, parent)) {
|
|
45
|
+
warnStripped(key, parent);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const item = (value as Record<string, unknown>)[key];
|
|
49
|
+
if (item === undefined) continue;
|
|
50
|
+
out[key] = sanitizeAt(item, key, [...path, key]);
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === "function") unserializable("function", path);
|
|
55
|
+
return value;
|
|
37
56
|
};
|
|
38
57
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
58
|
+
// One clone pass: strips unsafe keys (with a warning); explicit-undefined
|
|
59
|
+
// object entries are omitted (delete semantics), functions and undefined
|
|
60
|
+
// list items throw.
|
|
61
|
+
export const sanitizeValue = (value: unknown, parent?: string): unknown =>
|
|
62
|
+
sanitizeAt(value, parent, parent === undefined ? [] : [parent]);
|
|
63
|
+
|
|
64
|
+
type MutableOp = Extract<Statewire.Op, { op: "replace" | "add" | "remove" }>;
|
|
43
65
|
|
|
44
66
|
const invalid = (op: MutableOp) =>
|
|
45
67
|
new Error(`Statewire cannot apply ${op.op} at path: ${op.path.join(".")}`);
|
|
46
68
|
|
|
47
|
-
const
|
|
48
|
-
current:
|
|
49
|
-
op: Extract<MutableOp, { op: "
|
|
69
|
+
const insertedText = (
|
|
70
|
+
current: string,
|
|
71
|
+
op: Extract<MutableOp, { op: "add" }>,
|
|
72
|
+
offset: number,
|
|
50
73
|
): string => {
|
|
51
|
-
if (typeof
|
|
74
|
+
if (typeof op.value !== "string" || offset > current.length) {
|
|
52
75
|
throw invalid(op);
|
|
53
76
|
}
|
|
54
|
-
return current + op.value;
|
|
77
|
+
return current.slice(0, offset) + op.value + current.slice(offset);
|
|
55
78
|
};
|
|
56
79
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
80
|
+
const applyAdd = (
|
|
81
|
+
box: { state: unknown },
|
|
82
|
+
op: Extract<MutableOp, { op: "add" }>,
|
|
83
|
+
) => {
|
|
84
|
+
const offset = op.path[op.path.length - 1];
|
|
85
|
+
const walked = walkStatePath(box.state, op.path.slice(0, -1), "resolve");
|
|
86
|
+
if (
|
|
87
|
+
walked === undefined ||
|
|
88
|
+
typeof offset !== "number" ||
|
|
89
|
+
!Number.isInteger(offset) ||
|
|
90
|
+
offset < 0
|
|
91
|
+
) {
|
|
92
|
+
throw invalid(op);
|
|
93
|
+
}
|
|
94
|
+
const target = walked.leaf;
|
|
95
|
+
if (Array.isArray(target)) {
|
|
96
|
+
if (offset > target.length) throw invalid(op);
|
|
97
|
+
target.splice(offset, 0, sanitizeValue(op.value));
|
|
98
|
+
} else if (typeof target === "string") {
|
|
99
|
+
const next = insertedText(target, op, offset);
|
|
100
|
+
if (op.path.length === 1) {
|
|
101
|
+
box.state = next;
|
|
102
|
+
} else {
|
|
103
|
+
(walked.parent as Record<string | number, unknown>)[walked.key] = next;
|
|
104
|
+
}
|
|
62
105
|
} else {
|
|
63
106
|
throw invalid(op);
|
|
64
107
|
}
|
|
65
108
|
};
|
|
66
109
|
|
|
67
110
|
const applyInList = (list: unknown[], index: number, op: MutableOp) => {
|
|
111
|
+
if (index >= list.length) throw invalid(op);
|
|
68
112
|
if (op.op === "replace") {
|
|
69
|
-
if (index >= list.length) throw invalid(op);
|
|
70
113
|
list[index] = sanitizeValue(op.value);
|
|
71
|
-
} else if (op.op === "add") {
|
|
72
|
-
if (index > list.length) throw invalid(op);
|
|
73
|
-
list.splice(index, 0, sanitizeValue(op.value));
|
|
74
|
-
} else if (op.op === "append") {
|
|
75
|
-
if (index >= list.length) throw invalid(op);
|
|
76
|
-
list[index] = appended(list[index], op);
|
|
77
114
|
} else {
|
|
78
|
-
if (index >= list.length) throw invalid(op);
|
|
79
115
|
list.splice(index, 1);
|
|
80
116
|
}
|
|
81
117
|
};
|
|
@@ -85,55 +121,31 @@ const applyInObject = (
|
|
|
85
121
|
key: string,
|
|
86
122
|
op: MutableOp,
|
|
87
123
|
) => {
|
|
88
|
-
const exists = Object.hasOwn(obj, key);
|
|
89
124
|
if (op.op === "replace") {
|
|
90
125
|
obj[key] = sanitizeValue(op.value, key);
|
|
91
|
-
} else if (op.op === "append") {
|
|
92
|
-
obj[key] = appended(exists ? obj[key] : undefined, op);
|
|
93
|
-
} else if (op.op === "remove") {
|
|
94
|
-
if (!exists) throw invalid(op);
|
|
95
|
-
delete obj[key];
|
|
96
126
|
} else {
|
|
97
|
-
throw invalid(op);
|
|
127
|
+
if (!Object.hasOwn(obj, key)) throw invalid(op);
|
|
128
|
+
delete obj[key];
|
|
98
129
|
}
|
|
99
130
|
};
|
|
100
131
|
|
|
101
132
|
const applyOpToDraft = (box: { state: unknown }, op: MutableOp): void => {
|
|
133
|
+
if (op.op === "add") {
|
|
134
|
+
if (op.path.length === 0) throw invalid(op);
|
|
135
|
+
applyAdd(box, op);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
102
138
|
if (op.path.length === 0) {
|
|
103
|
-
|
|
139
|
+
if (op.op !== "replace") throw invalid(op);
|
|
140
|
+
box.state = sanitizeValue(op.value);
|
|
104
141
|
return;
|
|
105
142
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
throw invalid(op);
|
|
113
|
-
}
|
|
114
|
-
if (last) {
|
|
115
|
-
applyInList(node, key, op);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (key >= node.length) throw invalid(op);
|
|
119
|
-
node = node[key];
|
|
120
|
-
} else {
|
|
121
|
-
if (
|
|
122
|
-
isUnsafe(key, op.path[depth - 1]) ||
|
|
123
|
-
typeof node !== "object" ||
|
|
124
|
-
node === null ||
|
|
125
|
-
Array.isArray(node)
|
|
126
|
-
) {
|
|
127
|
-
throw invalid(op);
|
|
128
|
-
}
|
|
129
|
-
const obj = node as Record<string, unknown>;
|
|
130
|
-
if (last) {
|
|
131
|
-
applyInObject(obj, key, op);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (!Object.hasOwn(obj, key)) throw invalid(op);
|
|
135
|
-
node = obj[key];
|
|
136
|
-
}
|
|
143
|
+
const walked = walkStatePath(box.state, op.path, "parent");
|
|
144
|
+
if (walked === undefined) throw invalid(op);
|
|
145
|
+
if (typeof walked.key === "number") {
|
|
146
|
+
applyInList(walked.parent as unknown[], walked.key, op);
|
|
147
|
+
} else {
|
|
148
|
+
applyInObject(walked.parent as Record<string, unknown>, walked.key, op);
|
|
137
149
|
}
|
|
138
150
|
};
|
|
139
151
|
|
|
@@ -165,37 +177,26 @@ export const applyOps = (
|
|
|
165
177
|
ops: readonly unknown[],
|
|
166
178
|
): {
|
|
167
179
|
state: unknown;
|
|
168
|
-
events:
|
|
180
|
+
events: unknown[];
|
|
169
181
|
changedPaths: StatewirePath[];
|
|
170
182
|
} => {
|
|
171
|
-
const events:
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
const flush = () => {
|
|
175
|
-
if (segment.length === 0) return;
|
|
176
|
-
const ops = segment;
|
|
177
|
-
segment = [];
|
|
178
|
-
const recorded = recordMutations({ state }, (draft) => {
|
|
179
|
-
for (const op of ops) applyOpToDraft(draft, op);
|
|
180
|
-
});
|
|
181
|
-
state = recorded.state.state;
|
|
182
|
-
changedPaths.push(...unboxPaths(recorded.changedPaths));
|
|
183
|
-
};
|
|
184
|
-
for (const op of ops as StatewireOp[]) {
|
|
183
|
+
const events: unknown[] = [];
|
|
184
|
+
const mutable: MutableOp[] = [];
|
|
185
|
+
for (const op of ops as Statewire.Op[]) {
|
|
185
186
|
if (op.op === "event") {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
op.op === "replace" ||
|
|
190
|
-
op.op === "add" ||
|
|
191
|
-
op.op === "append" ||
|
|
192
|
-
op.op === "remove"
|
|
193
|
-
) {
|
|
194
|
-
segment.push(op);
|
|
187
|
+
events.push(sanitizeValue(op.value));
|
|
188
|
+
} else if (op.op === "replace" || op.op === "add" || op.op === "remove") {
|
|
189
|
+
mutable.push(op);
|
|
195
190
|
} else {
|
|
196
191
|
warnUnknownOp((op as { op: unknown }).op);
|
|
197
192
|
}
|
|
198
193
|
}
|
|
199
|
-
|
|
200
|
-
|
|
194
|
+
const recorded = recordMutations({ state }, (draft) => {
|
|
195
|
+
for (const op of mutable) applyOpToDraft(draft, op);
|
|
196
|
+
});
|
|
197
|
+
return {
|
|
198
|
+
state: recorded.state.state,
|
|
199
|
+
events,
|
|
200
|
+
changedPaths: unboxPaths(recorded.changedPaths),
|
|
201
|
+
};
|
|
201
202
|
};
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 AgentbaseAI Inc.
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"optimistic.d.ts","names":[],"sources":["../../src/client/optimistic.ts"],"mappings":";KAAK;KACA,gBAAgB;cAuER,kBAAmB,GAC9B,MAAM,GACN,SAAS,OAAO;EACb,OAAO;EAAG,cAAc"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"optimistic.js","names":["PathKey","Path","DRAFT","Symbol","Node","base","Record","PropertyKey","copy","modified","isArray","parent","path","proxy","assigned","Set","isDraftable","value","Array","proto","Object","getPrototypeOf","prototype","nodeOf","undefined","latest","node","prepareCopy","slice","markModified","n","resolveAssigned","container","key","keys","child","finalize","result","has","recordMutations","T","mutate","draft","state","changedPaths","seen","record","join","add","push","createNode","touched","prop","Proxy","get","_target","current","Number","set","next","source","hasOwn","deleteProperty","defineProperty","Error","setPrototypeOf","preventExtensions","Reflect","ownKeys","getOwnPropertyDescriptor","desc","configurable","root"],"sources":["../../src/client/optimistic.ts"],"sourcesContent":["type PathKey = string | number;\ntype Path = readonly PathKey[];\n\nconst DRAFT = Symbol(\"statewire.draft\");\n\ntype Node = {\n base: Record<PropertyKey, unknown>;\n copy: Record<PropertyKey, unknown> | null;\n modified: boolean;\n isArray: boolean;\n parent: Node | null;\n path: Path;\n proxy: unknown;\n assigned: Set<PropertyKey> | null;\n};\n\nconst isDraftable = (value: unknown): value is object => {\n if (typeof value !== \"object\" || value === null) return false;\n if (Array.isArray(value)) return true;\n const proto: unknown = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n};\n\nconst nodeOf = (value: unknown): Node | undefined =>\n typeof value === \"object\" && value !== null\n ? (value as { [DRAFT]?: Node })[DRAFT]\n : undefined;\n\nconst latest = (node: Node) => node.copy ?? node.base;\n\nconst prepareCopy = (node: Node) => {\n node.copy ??= node.isArray\n ? ((node.base as unknown as unknown[]).slice() as unknown as Record<\n PropertyKey,\n unknown\n >)\n : { ...node.base };\n};\n\nconst markModified = (node: Node) => {\n for (let n: Node | null = node; n && !n.modified; n = n.parent) {\n n.modified = true;\n }\n};\n\nconst resolveAssigned = (container: object) => {\n for (const key of Object.keys(container)) {\n const value = (container as Record<string, unknown>)[key];\n const child = nodeOf(value);\n if (child) {\n (container as Record<string, unknown>)[key] = finalize(child);\n } else if (isDraftable(value)) {\n resolveAssigned(value);\n }\n }\n};\n\nconst finalize = (node: Node): unknown => {\n if (!node.modified) return node.base;\n const result = node.copy!;\n for (const key of Object.keys(result)) {\n const value = result[key];\n const child = nodeOf(value);\n if (child) {\n result[key] = finalize(child);\n } else if (node.assigned?.has(key) && isDraftable(value)) {\n resolveAssigned(value);\n }\n }\n return result;\n};\n\nexport const recordMutations = <T>(\n base: T,\n mutate: (draft: T) => void,\n): { state: T; changedPaths: Path[] } => {\n if (!isDraftable(base)) return { state: base, changedPaths: [] };\n\n const changedPaths: Path[] = [];\n const seen = new Set<string>();\n const record = (path: Path) => {\n const key = path.join(\"\u0000\");\n if (seen.has(key)) return;\n seen.add(key);\n changedPaths.push(path);\n };\n\n const createNode = (value: object, parent: Node | null, path: Path): Node => {\n const node: Node = {\n base: value as Record<PropertyKey, unknown>,\n copy: null,\n modified: false,\n isArray: Array.isArray(value),\n parent,\n path,\n proxy: null,\n assigned: null,\n };\n const touched = (prop: PropertyKey) => {\n if (typeof prop !== \"string\") return;\n record(node.isArray ? node.path : [...node.path, prop]);\n };\n node.proxy = new Proxy(value, {\n get: (_target, prop) => {\n if (prop === DRAFT) return node;\n const current = latest(node)[prop];\n if (typeof prop === \"symbol\") return current;\n if (nodeOf(current)) return current;\n if (isDraftable(current) && current === node.base[prop]) {\n prepareCopy(node);\n const child = createNode(current, node, [\n ...path,\n node.isArray ? Number(prop) : prop,\n ]);\n node.copy![prop] = child.proxy;\n return child.proxy;\n }\n return current;\n },\n set: (_target, prop, next) => {\n const source = latest(node);\n if (Object.hasOwn(source, prop) && source[prop] === next) return true;\n prepareCopy(node);\n markModified(node);\n touched(prop);\n node.copy![prop] = next;\n (node.assigned ??= new Set()).add(prop);\n return true;\n },\n deleteProperty: (_target, prop) => {\n if (!Object.hasOwn(latest(node), prop)) return true;\n prepareCopy(node);\n markModified(node);\n touched(prop);\n delete node.copy![prop];\n return true;\n },\n defineProperty: () => {\n throw new Error(\n \"statewire: defineProperty is not supported inside a mutator\",\n );\n },\n setPrototypeOf: () => {\n throw new Error(\n \"statewire: setPrototypeOf is not supported inside a mutator\",\n );\n },\n preventExtensions: () => {\n throw new Error(\n \"statewire: preventExtensions is not supported inside a mutator\",\n );\n },\n has: (_target, prop) => prop === DRAFT || Reflect.has(latest(node), prop),\n ownKeys: () => Reflect.ownKeys(latest(node)),\n getOwnPropertyDescriptor: (_target, prop) => {\n const desc = Reflect.getOwnPropertyDescriptor(latest(node), prop);\n if (!desc) return undefined;\n return {\n ...desc,\n configurable: !node.isArray || prop !== \"length\",\n };\n },\n });\n return node;\n };\n\n const root = createNode(base as unknown as object, null, []);\n mutate(root.proxy as T);\n return { state: finalize(root) as T, changedPaths };\n};\n"],"mappings":";AAGA,MAAME,QAAQC,OAAO,iBAAiB;AAatC,MAAMa,eAAeC,UAAoC;CACvD,IAAI,OAAOA,UAAU,YAAYA,UAAU,MAAM,OAAO;CACxD,IAAIC,MAAMR,QAAQO,KAAK,GAAG,OAAO;CACjC,MAAME,QAAiBC,OAAOC,eAAeJ,KAAK;CAClD,OAAOE,UAAUC,OAAOE,aAAaH,UAAU;AACjD;AAEA,MAAMI,UAAUN,UACd,OAAOA,UAAU,YAAYA,UAAU,OAClCA,MAA6Bf,SAC9BsB,KAAAA;AAEN,MAAMC,UAAUC,SAAeA,KAAKlB,QAAQkB,KAAKrB;AAEjD,MAAMsB,eAAeD,SAAe;CAClCA,KAAKlB,SAASkB,KAAKhB,UACbgB,KAAKrB,KAA8BuB,MAAM,IAI3C,EAAE,GAAGF,KAAKrB,KAAK;AACrB;AAEA,MAAMwB,gBAAgBH,SAAe;CACnC,KAAK,IAAII,IAAiBJ,MAAMI,KAAK,CAACA,EAAErB,UAAUqB,IAAIA,EAAEnB,QACtDmB,EAAErB,WAAW;AAEjB;AAEA,MAAMsB,mBAAmBC,cAAs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AID,MACR,6DACF;GACF;GACAE,yBAAyB;IACvB,MAAM,IAAIF,MACR,gEACF;GACF;GACA1B,MAAMiB,SAASH,SAASA,SAASlD,SAASiE,QAAQ7B,IAAIb,OAAOC,IAAI,GAAG0B,IAAI;GACxEgB,eAAeD,QAAQC,QAAQ3C,OAAOC,IAAI,CAAC;GAC3C2C,2BAA2Bd,SAASH,SAAS;IAC3C,MAAMkB,OAAOH,QAAQE,yBAAyB5C,OAAOC,IAAI,GAAG0B,IAAI;IAChE,IAAI,CAACkB,MAAM,OAAO9C,KAAAA;IAClB,OAAO;KACL,GAAG8C;KACHC,cAAc,CAAC7C,KAAKhB,WAAW0C,SAAS;IAC1C;GACF;EACF,CAAC;EACD,OAAO1B;CACT;CAEA,MAAM8C,OAAOtB,WAAW7C,MAA2B,MAAM,CAAA,CAAE;CAC3DoC,OAAO+B,KAAK3D,KAAU;CACtB,OAAO;EAAE8B,OAAOP,SAASoC,IAAI;EAAQ5B;CAAa;AACpD"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;kBA4BiB;OACH,aAAa;OACb,mBAAmB;OACnB,UACV,cACA,kBAAkB,oBAAoB,mBACtC,gBACE,mBAAmB,QAAQ,WAAW;OAC9B,cAAc;OACd,aAAa;OACb,YAAY;OACZ,QACV,QACA,kBAAkB,oBAAoB,mBACtC,gBACE,iBAAiB,QAAQ,WAAW;OAC5B,QACV,QACA,kBAAkB,oBAAoB,qBACpC,iBAAiB,QAAQ;OACjB,WAAW;OACX,QAAQ,kBAAkB,oBAAoB,qBACxD,iBAAiB;OACP,cAAc,kBAAkB,qBAC1C,uBAAuB;OACb,cACV,kBAAkB,oBAAoB,qBACpC,uBAAuB;OACf,WACV,QACA,kBAAkB,oBAAoB,qBACpC,oBAAoB,QAAQ;OACpB,oBAAoB;OACpB,cAAc;OACd,UAAU;OACV,KAAK"}
|
package/dist/server.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { StatewireCommands, StatewireStateSource, StatewireTransport } from "./types.js";
|
|
2
|
-
import { CommandExecution, CommandExecutionResolvers, StatewireHandledCommands, StatewireStateCtl, useStatewireCommands, useStatewireEmit, useStatewireState } from "./host/host.js";
|
|
3
|
-
import { StatewireReject } from "./host/engine.js";
|
|
4
|
-
//#region src/server.d.ts
|
|
5
|
-
declare namespace StatewireServer {
|
|
6
|
-
type StateCtl = StatewireStateCtl;
|
|
7
|
-
type StateSource = StatewireStateSource;
|
|
8
|
-
type Execution<T = void> = CommandExecution<T>;
|
|
9
|
-
type ExecutionResolvers<T = void> = CommandExecutionResolvers<T>;
|
|
10
|
-
type HandledCommands<THandlers extends StatewireCommands> = StatewireHandledCommands<THandlers>;
|
|
11
|
-
type Transport<TState = any, TCommands extends StatewireCommands = StatewireCommands, TEvent = any> = StatewireTransport<TState, TCommands, TEvent>;
|
|
12
|
-
}
|
|
13
|
-
//#endregion
|
|
14
|
-
export { CommandExecution, StatewireReject, StatewireServer, useStatewireCommands, useStatewireEmit, useStatewireState };
|
|
15
|
-
//# sourceMappingURL=server.d.ts.map
|
package/dist/server.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","names":[],"sources":["../src/server.ts"],"mappings":";;;;kBAmBiB;OACH,WAAW;OACX,cAAc;OACd,UAAU,YAAY,iBAAiB;OACvC,mBAAmB,YAAY,0BAA0B;OACzD,gBAAgB,kBAAkB,qBAC5C,yBAAyB;OACf,UACV,cACA,kBAAkB,oBAAoB,mBACtC,gBACE,mBAAmB,QAAQ,WAAW"}
|
package/dist/server.js
DELETED
package/src/server.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CommandExecution,
|
|
3
|
-
CommandExecutionResolvers,
|
|
4
|
-
StatewireHandledCommands,
|
|
5
|
-
StatewireStateCtl,
|
|
6
|
-
} from "./host/host";
|
|
7
|
-
import type {
|
|
8
|
-
StatewireCommands,
|
|
9
|
-
StatewireStateSource,
|
|
10
|
-
StatewireTransport,
|
|
11
|
-
} from "./types";
|
|
12
|
-
|
|
13
|
-
export { StatewireReject } from "./host/engine";
|
|
14
|
-
export {
|
|
15
|
-
CommandExecution,
|
|
16
|
-
useStatewireState,
|
|
17
|
-
useStatewireCommands,
|
|
18
|
-
useStatewireEmit,
|
|
19
|
-
} from "./host/host";
|
|
20
|
-
export namespace StatewireServer {
|
|
21
|
-
export type StateCtl = StatewireStateCtl;
|
|
22
|
-
export type StateSource = StatewireStateSource;
|
|
23
|
-
export type Execution<T = void> = CommandExecution<T>;
|
|
24
|
-
export type ExecutionResolvers<T = void> = CommandExecutionResolvers<T>;
|
|
25
|
-
export type HandledCommands<THandlers extends StatewireCommands> =
|
|
26
|
-
StatewireHandledCommands<THandlers>;
|
|
27
|
-
export type Transport<
|
|
28
|
-
TState = any,
|
|
29
|
-
TCommands extends StatewireCommands = StatewireCommands,
|
|
30
|
-
TEvent = any,
|
|
31
|
-
> = StatewireTransport<TState, TCommands, TEvent>;
|
|
32
|
-
}
|