@dxos/echo 0.16.0 → 0.17.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/dist/lib/Annotation.mjs +2 -2
- package/dist/lib/Database.mjs +1 -1
- package/dist/lib/Error.mjs +1 -1
- package/dist/lib/Feed.mjs +1 -1
- package/dist/lib/Format.mjs +2 -2
- package/dist/lib/Migration.mjs +1 -1
- package/dist/lib/chunk-Annotation.mjs +14 -11
- package/dist/lib/chunk-Annotation.mjs.map +1 -1
- package/dist/lib/chunk-Blob.mjs +1 -1
- package/dist/lib/chunk-Collection.mjs +1 -1
- package/dist/lib/chunk-Database.mjs +93 -7
- package/dist/lib/chunk-Database.mjs.map +1 -1
- package/dist/lib/chunk-Dataset.mjs +1 -1
- package/dist/lib/chunk-Entity.mjs +2 -2
- package/dist/lib/chunk-Entity.mjs.map +1 -1
- package/dist/lib/chunk-Error.mjs +13 -1
- package/dist/lib/chunk-Error.mjs.map +1 -1
- package/dist/lib/chunk-Feed.mjs +311 -17
- package/dist/lib/chunk-Feed.mjs.map +1 -1
- package/dist/lib/chunk-Filter.mjs +4 -3
- package/dist/lib/chunk-Filter.mjs.map +1 -1
- package/dist/lib/chunk-Format.mjs +2 -1
- package/dist/lib/chunk-Hypergraph.mjs +1 -1
- package/dist/lib/chunk-Hypergraph.mjs.map +1 -1
- package/dist/lib/chunk-Migration.mjs +155 -5
- package/dist/lib/chunk-Migration.mjs.map +1 -1
- package/dist/lib/chunk-Obj.mjs +12 -5
- package/dist/lib/chunk-Obj.mjs.map +1 -1
- package/dist/lib/chunk-Query.mjs +2 -2
- package/dist/lib/chunk-QueryResult.mjs.map +1 -1
- package/dist/lib/chunk-Ref2.mjs +2 -2
- package/dist/lib/chunk-Registry.mjs +1 -1
- package/dist/lib/chunk-Registry.mjs.map +1 -1
- package/dist/lib/chunk-Relation.mjs +5 -5
- package/dist/lib/chunk-Relation.mjs.map +1 -1
- package/dist/lib/chunk-Scope.mjs +16 -2
- package/dist/lib/chunk-Scope.mjs.map +1 -1
- package/dist/lib/chunk-Tag.mjs +4 -4
- package/dist/lib/chunk-Tag.mjs.map +1 -1
- package/dist/lib/chunk-Text.mjs +2 -2
- package/dist/lib/chunk-Type.mjs +26 -7
- package/dist/lib/chunk-Type.mjs.map +1 -1
- package/dist/lib/chunk-View.mjs +1 -1
- package/dist/lib/chunk-atom-snapshot.mjs +29 -1
- package/dist/lib/chunk-atom-snapshot.mjs.map +1 -1
- package/dist/lib/chunk-entity-dictionary.mjs +28 -19
- package/dist/lib/chunk-entity-dictionary.mjs.map +1 -1
- package/dist/lib/chunk-internal.mjs +231 -79
- package/dist/lib/chunk-internal.mjs.map +1 -1
- package/dist/lib/chunk-json-schema.mjs +1 -1
- package/dist/lib/chunk-ref.mjs +38 -34
- package/dist/lib/chunk-ref.mjs.map +1 -1
- package/dist/lib/chunk-select.mjs +7 -3
- package/dist/lib/chunk-select.mjs.map +1 -1
- package/dist/lib/index.mjs +2 -2
- package/dist/lib/internal.mjs +6 -6
- package/dist/lib/testing.mjs +2 -2
- package/dist/lib/testing.mjs.map +1 -1
- package/dist/types/src/Annotation.d.ts +1 -1
- package/dist/types/src/Annotation.d.ts.map +1 -1
- package/dist/types/src/Database.d.ts +30 -13
- package/dist/types/src/Database.d.ts.map +1 -1
- package/dist/types/src/Entity.d.ts +3 -4
- package/dist/types/src/Entity.d.ts.map +1 -1
- package/dist/types/src/Error.d.ts +36 -0
- package/dist/types/src/Error.d.ts.map +1 -1
- package/dist/types/src/Feed.d.ts +261 -18
- package/dist/types/src/Feed.d.ts.map +1 -1
- package/dist/types/src/Filter.d.ts +7 -1
- package/dist/types/src/Filter.d.ts.map +1 -1
- package/dist/types/src/Hypergraph.d.ts +1 -1
- package/dist/types/src/Migration.d.ts +213 -2
- package/dist/types/src/Migration.d.ts.map +1 -1
- package/dist/types/src/Migration.test.d.ts +2 -0
- package/dist/types/src/Migration.test.d.ts.map +1 -0
- package/dist/types/src/Obj.d.ts +4 -4
- package/dist/types/src/Obj.d.ts.map +1 -1
- package/dist/types/src/QueryResult.d.ts +16 -0
- package/dist/types/src/QueryResult.d.ts.map +1 -1
- package/dist/types/src/Relation.d.ts +3 -4
- package/dist/types/src/Relation.d.ts.map +1 -1
- package/dist/types/src/Scope.d.ts +32 -1
- package/dist/types/src/Scope.d.ts.map +1 -1
- package/dist/types/src/Tag.d.ts.map +1 -1
- package/dist/types/src/Type.d.ts +27 -2
- package/dist/types/src/Type.d.ts.map +1 -1
- package/dist/types/src/internal/Annotation/annotations.d.ts +35 -1
- package/dist/types/src/internal/Annotation/annotations.d.ts.map +1 -1
- package/dist/types/src/internal/Annotation/atoms.d.ts +2 -2
- package/dist/types/src/internal/Annotation/atoms.d.ts.map +1 -1
- package/dist/types/src/internal/Entity/api.d.ts +11 -0
- package/dist/types/src/internal/Entity/api.d.ts.map +1 -1
- package/dist/types/src/internal/Entity/model.d.ts +3 -2
- package/dist/types/src/internal/Entity/model.d.ts.map +1 -1
- package/dist/types/src/internal/Format/string.d.ts +5 -1
- package/dist/types/src/internal/Format/string.d.ts.map +1 -1
- package/dist/types/src/internal/Obj/atoms.d.ts +1 -1
- package/dist/types/src/internal/Obj/atoms.d.ts.map +1 -1
- package/dist/types/src/internal/Obj/json-serializer.d.ts.map +1 -1
- package/dist/types/src/internal/Query/pretty.d.ts.map +1 -1
- package/dist/types/src/internal/Ref/ref.d.ts +5 -1
- package/dist/types/src/internal/Ref/ref.d.ts.map +1 -1
- package/dist/types/src/internal/Ref/strong-deps.d.ts +3 -1
- package/dist/types/src/internal/Ref/strong-deps.d.ts.map +1 -1
- package/dist/types/src/internal/common/api/meta.d.ts +4 -5
- package/dist/types/src/internal/common/api/meta.d.ts.map +1 -1
- package/dist/types/src/internal/common/atom-snapshot.d.ts +10 -0
- package/dist/types/src/internal/common/atom-snapshot.d.ts.map +1 -1
- package/dist/types/src/internal/common/proxy/change-context.d.ts +4 -0
- package/dist/types/src/internal/common/proxy/change-context.d.ts.map +1 -1
- package/dist/types/src/internal/common/proxy/ownership.d.ts +5 -0
- package/dist/types/src/internal/common/proxy/ownership.d.ts.map +1 -1
- package/dist/types/src/internal/common/proxy/proxy-utils.d.ts.map +1 -1
- package/dist/types/src/internal/common/proxy/schema-validator.d.ts +17 -2
- package/dist/types/src/internal/common/proxy/schema-validator.d.ts.map +1 -1
- package/dist/types/src/internal/common/proxy/symbols.d.ts +10 -0
- package/dist/types/src/internal/common/proxy/symbols.d.ts.map +1 -1
- package/dist/types/src/internal/common/proxy/typed-handler.d.ts +0 -1
- package/dist/types/src/internal/common/proxy/typed-handler.d.ts.map +1 -1
- package/dist/types/src/internal/common/types/meta.d.ts +8 -4
- package/dist/types/src/internal/common/types/meta.d.ts.map +1 -1
- package/dist/types/src/internal/common/types/model-symbols.d.ts +6 -0
- package/dist/types/src/internal/common/types/model-symbols.d.ts.map +1 -1
- package/dist/types/src/internal/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -15
- package/src/Annotation.test.ts +34 -0
- package/src/Annotation.ts +3 -0
- package/src/Database.ts +46 -15
- package/src/Entity.ts +3 -4
- package/src/Error.ts +9 -0
- package/src/Feed.test.ts +160 -0
- package/src/Feed.ts +412 -29
- package/src/Filter.ts +8 -1
- package/src/Hypergraph.ts +1 -1
- package/src/Migration.test.ts +137 -0
- package/src/Migration.ts +376 -4
- package/src/Obj.test.ts +44 -4
- package/src/Obj.ts +19 -5
- package/src/QueryResult.ts +14 -0
- package/src/Relation.ts +3 -4
- package/src/Scope.ts +43 -1
- package/src/Tag.md +5 -6
- package/src/Tag.ts +3 -2
- package/src/Type.test.ts +52 -0
- package/src/Type.ts +75 -22
- package/src/Type.tst.ts +155 -0
- package/src/internal/Annotation/annotations.test.ts +19 -0
- package/src/internal/Annotation/annotations.ts +67 -1
- package/src/internal/Annotation/atoms.ts +10 -9
- package/src/internal/Entity/api.ts +20 -1
- package/src/internal/Entity/model.ts +3 -0
- package/src/internal/Filter/match.ts +2 -2
- package/src/internal/Format/string.ts +7 -2
- package/src/internal/JsonSchema/json-schema.test.ts +2 -2
- package/src/internal/Obj/atoms.ts +9 -5
- package/src/internal/Obj/json-serializer.test.ts +34 -7
- package/src/internal/Obj/json-serializer.ts +18 -3
- package/src/internal/Query/pretty.ts +3 -2
- package/src/internal/Ref/ref.test.ts +27 -2
- package/src/internal/Ref/ref.ts +18 -2
- package/src/internal/Ref/strong-deps.ts +21 -2
- package/src/internal/common/api/meta.ts +7 -8
- package/src/internal/common/atom-snapshot.ts +61 -0
- package/src/internal/common/proxy/change-context.ts +5 -0
- package/src/internal/common/proxy/change.test.ts +62 -31
- package/src/internal/common/proxy/json-serializer.ts +3 -2
- package/src/internal/common/proxy/ownership.ts +14 -1
- package/src/internal/common/proxy/proxy-utils.ts +7 -1
- package/src/internal/common/proxy/schema-validator.ts +116 -27
- package/src/internal/common/proxy/symbols.ts +12 -0
- package/src/internal/common/proxy/typed-handler.test.ts +43 -0
- package/src/internal/common/proxy/typed-handler.ts +105 -33
- package/src/internal/common/types/meta.ts +25 -20
- package/src/internal/common/types/model-symbols.ts +7 -0
- package/src/internal/index.ts +1 -2
|
@@ -2,19 +2,25 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { describe, test } from 'vitest';
|
|
6
|
-
|
|
7
|
-
import { URI } from '@dxos/keys';
|
|
5
|
+
import { beforeEach, describe, test } from 'vitest';
|
|
8
6
|
|
|
9
7
|
import * as Obj from '../../../Obj.ts';
|
|
10
|
-
import * as Ref from '../../../Ref.ts';
|
|
11
8
|
import * as Relation from '../../../Relation.ts';
|
|
9
|
+
import * as Tag from '../../../Tag.ts';
|
|
12
10
|
import { TestSchema } from '../../../testing/index.ts';
|
|
13
11
|
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
// One Tag object per label within a test, so a test can name the same tag twice; compared by label.
|
|
13
|
+
let tagsByLabel = new Map<string, Tag.Tag>();
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
tagsByLabel = new Map();
|
|
16
|
+
});
|
|
17
|
+
const tag = (label: string): Tag.Tag => {
|
|
18
|
+
const existing = tagsByLabel.get(label) ?? Tag.make({ label });
|
|
19
|
+
tagsByLabel.set(label, existing);
|
|
20
|
+
return existing;
|
|
21
|
+
};
|
|
22
|
+
const tagLabels = (entity: Obj.Unknown | Relation.Unknown): string[] =>
|
|
23
|
+
Obj.getMeta(entity as Obj.Unknown).tags.map((tag) => tag.label);
|
|
18
24
|
|
|
19
25
|
/**
|
|
20
26
|
* Tests for Obj.update context enforcement and mutator type safety.
|
|
@@ -52,7 +58,7 @@ describe('Obj.update enforcement', () => {
|
|
|
52
58
|
|
|
53
59
|
// No compile-time error: TypeScript's structural typing allows readonly objects
|
|
54
60
|
// to be passed to Mutable<T> parameters. Enforcement is runtime-only.
|
|
55
|
-
expect(() => Obj.addTag(obj,
|
|
61
|
+
expect(() => Obj.addTag(obj, tag('tag'))).toThrow(/outside of Obj.update/);
|
|
56
62
|
});
|
|
57
63
|
|
|
58
64
|
test('getMeta mutation outside change throws', ({ expect }) => {
|
|
@@ -61,7 +67,7 @@ describe('Obj.update enforcement', () => {
|
|
|
61
67
|
|
|
62
68
|
// Runtime errors for direct meta mutations.
|
|
63
69
|
expect(() => ((meta as any).keys = [])).toThrow(/outside of Obj.update/);
|
|
64
|
-
expect(() => ((meta as any).tags = [
|
|
70
|
+
expect(() => ((meta as any).tags = [tag('tag')])).toThrow(/outside of Obj.update/);
|
|
65
71
|
});
|
|
66
72
|
|
|
67
73
|
test('getMeta returns mutable EntityMeta inside change callback', ({ expect }) => {
|
|
@@ -72,11 +78,11 @@ describe('Obj.update enforcement', () => {
|
|
|
72
78
|
|
|
73
79
|
// These should compile without errors because meta is EntityMeta (mutable).
|
|
74
80
|
meta.keys = [];
|
|
75
|
-
meta.tags = [
|
|
81
|
+
meta.tags = [tag('tag')];
|
|
76
82
|
meta.keys.push({ source: 'test', id: '123' });
|
|
77
83
|
});
|
|
78
84
|
|
|
79
|
-
expect(
|
|
85
|
+
expect(tagLabels(obj)).toEqual(['tag']);
|
|
80
86
|
expect(Obj.getMeta(obj).keys).toHaveLength(1);
|
|
81
87
|
});
|
|
82
88
|
|
|
@@ -85,12 +91,12 @@ describe('Obj.update enforcement', () => {
|
|
|
85
91
|
|
|
86
92
|
// These should compile without errors inside change callback.
|
|
87
93
|
Obj.update(obj, (obj) => {
|
|
88
|
-
Obj.addTag(obj,
|
|
94
|
+
Obj.addTag(obj, tag('my-tag'));
|
|
89
95
|
Obj.setValue(obj, ['name'], 'Updated');
|
|
90
96
|
});
|
|
91
97
|
|
|
92
98
|
expect(obj.name).toBe('Updated');
|
|
93
|
-
expect(
|
|
99
|
+
expect(tagLabels(obj)).toContain('my-tag');
|
|
94
100
|
});
|
|
95
101
|
|
|
96
102
|
test('Relation property mutation outside change throws', ({ expect }) => {
|
|
@@ -118,7 +124,7 @@ describe('Obj.update enforcement', () => {
|
|
|
118
124
|
|
|
119
125
|
// No compile-time error: TypeScript's structural typing allows readonly objects
|
|
120
126
|
// to be passed to Mutable<T> parameters. Enforcement is runtime-only.
|
|
121
|
-
expect(() => Relation.addTag(rel,
|
|
127
|
+
expect(() => Relation.addTag(rel, tag('tag'))).toThrow(/outside of Obj.update/);
|
|
122
128
|
});
|
|
123
129
|
});
|
|
124
130
|
|
|
@@ -162,17 +168,42 @@ describe('Obj.update enforcement', () => {
|
|
|
162
168
|
expect(Obj.getMeta(obj).tags).toEqual([]);
|
|
163
169
|
|
|
164
170
|
Obj.update(obj, (obj) => {
|
|
165
|
-
Obj.addTag(obj,
|
|
166
|
-
Obj.addTag(obj,
|
|
171
|
+
Obj.addTag(obj, tag('tag-1'));
|
|
172
|
+
Obj.addTag(obj, tag('tag-2'));
|
|
167
173
|
});
|
|
168
174
|
|
|
169
|
-
expect(
|
|
175
|
+
expect(tagLabels(obj)).toEqual(['tag-1', 'tag-2']);
|
|
170
176
|
|
|
171
177
|
Obj.update(obj, (obj) => {
|
|
172
|
-
Obj.removeTag(obj,
|
|
178
|
+
Obj.removeTag(obj, tag('tag-1'));
|
|
173
179
|
});
|
|
174
180
|
|
|
175
|
-
expect(
|
|
181
|
+
expect(tagLabels(obj)).toEqual(['tag-2']);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('tags are held by identity, not owned or copied', ({ expect }) => {
|
|
185
|
+
const urgent = Tag.make({ label: 'urgent' });
|
|
186
|
+
const obj = Obj.make(TestSchema.Person, { name: 'Test' });
|
|
187
|
+
Obj.update(obj, (obj) => {
|
|
188
|
+
Obj.addTag(obj, urgent);
|
|
189
|
+
// Editing the holder does not hand out write access to the tag.
|
|
190
|
+
expect(Obj.getMeta(obj).tags[0]).toBe(urgent);
|
|
191
|
+
});
|
|
192
|
+
expect(Obj.getMeta(obj).tags[0]).toBe(urgent);
|
|
193
|
+
|
|
194
|
+
// The tag stays its own root: it is edited through its own update and notifies its own subscribers.
|
|
195
|
+
let tagFires = 0;
|
|
196
|
+
const unsubscribe = Obj.subscribe(urgent, () => tagFires++);
|
|
197
|
+
Obj.update(urgent, (urgent) => {
|
|
198
|
+
urgent.label = 'later';
|
|
199
|
+
});
|
|
200
|
+
unsubscribe();
|
|
201
|
+
expect(tagFires).toBe(1);
|
|
202
|
+
expect(Obj.getMeta(obj).tags[0].label).toBe('later');
|
|
203
|
+
|
|
204
|
+
const seeded = Obj.make(TestSchema.Person, { [Obj.Meta]: { tags: [urgent] }, name: 'Seeded' });
|
|
205
|
+
expect(Obj.getMeta(seeded).tags[0]).toBe(urgent);
|
|
206
|
+
expect(Obj.getSnapshot(seeded)).toBeDefined();
|
|
176
207
|
});
|
|
177
208
|
|
|
178
209
|
test('deleteKeys removes foreign keys by source', ({ expect }) => {
|
|
@@ -210,12 +241,12 @@ describe('Obj.update enforcement', () => {
|
|
|
210
241
|
|
|
211
242
|
Obj.update(obj, (obj) => {
|
|
212
243
|
const meta = Obj.getMeta(obj);
|
|
213
|
-
meta.tags = [
|
|
244
|
+
meta.tags = [tag('tag-1'), tag('tag-2')];
|
|
214
245
|
meta.keys.push({ source: 'external', id: '123' });
|
|
215
246
|
});
|
|
216
247
|
|
|
217
248
|
// Changes should persist after the change callback.
|
|
218
|
-
expect(
|
|
249
|
+
expect(tagLabels(obj)).toEqual(['tag-1', 'tag-2']);
|
|
219
250
|
expect(Obj.getMeta(obj).keys).toEqual([{ source: 'external', id: '123' }]);
|
|
220
251
|
});
|
|
221
252
|
|
|
@@ -226,13 +257,13 @@ describe('Obj.update enforcement', () => {
|
|
|
226
257
|
obj.name = 'Name 1';
|
|
227
258
|
obj.name = 'Name 2';
|
|
228
259
|
obj.name = 'Name 3';
|
|
229
|
-
Obj.addTag(obj,
|
|
230
|
-
Obj.addTag(obj,
|
|
260
|
+
Obj.addTag(obj, tag('tag-1'));
|
|
261
|
+
Obj.addTag(obj, tag('tag-2'));
|
|
231
262
|
});
|
|
232
263
|
|
|
233
264
|
// All mutations should persist.
|
|
234
265
|
expect(obj.name).toBe('Name 3');
|
|
235
|
-
expect(
|
|
266
|
+
expect(tagLabels(obj)).toEqual(['tag-1', 'tag-2']);
|
|
236
267
|
});
|
|
237
268
|
});
|
|
238
269
|
|
|
@@ -465,11 +496,11 @@ describe('Obj.update enforcement', () => {
|
|
|
465
496
|
|
|
466
497
|
Relation.update(rel, (rel) => {
|
|
467
498
|
const meta = Relation.getMeta(rel);
|
|
468
|
-
meta.tags = [
|
|
499
|
+
meta.tags = [tag('rel-tag')];
|
|
469
500
|
meta.keys.push({ source: 'rel-source', id: 'rel-key' });
|
|
470
501
|
});
|
|
471
502
|
|
|
472
|
-
expect(
|
|
503
|
+
expect(tagLabels(rel)).toEqual(['rel-tag']);
|
|
473
504
|
expect(Relation.getMeta(rel).keys).toHaveLength(1);
|
|
474
505
|
});
|
|
475
506
|
|
|
@@ -482,16 +513,16 @@ describe('Obj.update enforcement', () => {
|
|
|
482
513
|
});
|
|
483
514
|
|
|
484
515
|
Relation.update(rel, (rel) => {
|
|
485
|
-
Relation.addTag(rel,
|
|
516
|
+
Relation.addTag(rel, tag('important'));
|
|
486
517
|
});
|
|
487
518
|
|
|
488
|
-
expect(
|
|
519
|
+
expect(tagLabels(rel)).toContain('important');
|
|
489
520
|
|
|
490
521
|
Relation.update(rel, (rel) => {
|
|
491
|
-
Relation.removeTag(rel,
|
|
522
|
+
Relation.removeTag(rel, tag('important'));
|
|
492
523
|
});
|
|
493
524
|
|
|
494
|
-
expect(
|
|
525
|
+
expect(tagLabels(rel)).not.toContain('important');
|
|
495
526
|
});
|
|
496
527
|
});
|
|
497
528
|
|
|
@@ -99,9 +99,10 @@ const serializeMeta = (meta: EntityMeta) => {
|
|
|
99
99
|
const { tags, annotations, ...rest } = meta;
|
|
100
100
|
const compact = {
|
|
101
101
|
...rest,
|
|
102
|
-
|
|
102
|
+
// Tags are entities held by identity; they serialize as references, like relation endpoints.
|
|
103
|
+
...(tags != null && tags.length > 0 ? { tags: tags.map((tag) => Ref.make(tag)) } : {}),
|
|
103
104
|
...(annotations != null && Object.keys(annotations).length > 0 ? { annotations } : {}),
|
|
104
105
|
};
|
|
105
|
-
// Use `serializeData` so
|
|
106
|
+
// Use `serializeData` so the tag refs encode to the on-wire reference form.
|
|
106
107
|
return serializeData(compact);
|
|
107
108
|
};
|
|
@@ -8,7 +8,7 @@ import { KindId } from '../types/index.ts';
|
|
|
8
8
|
import { queueOwnerNotification } from './change-context.ts';
|
|
9
9
|
import { defineHiddenProperty } from './define-hidden-property.ts';
|
|
10
10
|
import { getProxyTarget, isProxy, isValidProxyTarget } from './proxy-utils.ts';
|
|
11
|
-
import { EventId } from './symbols.ts';
|
|
11
|
+
import { EntityElementsId, EventId } from './symbols.ts';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Symbol to store the owning ECHO object reference on nested JS objects (records).
|
|
@@ -20,6 +20,13 @@ import { EventId } from './symbols.ts';
|
|
|
20
20
|
*/
|
|
21
21
|
const EchoOwner = Symbol.for('@dxos/echo/Owner');
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Whether the array holds root entities by identity (see {@link EntityElementsId}); its elements are
|
|
25
|
+
* never owned, copied or walked as nested data.
|
|
26
|
+
*/
|
|
27
|
+
export const hasEntityElements = (value: unknown): boolean =>
|
|
28
|
+
Array.isArray(value) && Reflect.get(value, EntityElementsId) === true;
|
|
29
|
+
|
|
23
30
|
/**
|
|
24
31
|
* Get the raw target from a value, unwrapping proxy if needed.
|
|
25
32
|
*/
|
|
@@ -113,6 +120,9 @@ export const setOwnerRecursive = (
|
|
|
113
120
|
depth: depth + 1,
|
|
114
121
|
allowedPreviousOwner: newAllowedPreviousOwner,
|
|
115
122
|
};
|
|
123
|
+
if (hasEntityElements(actualValue)) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
116
126
|
if (Array.isArray(actualValue)) {
|
|
117
127
|
for (const item of actualValue) {
|
|
118
128
|
if (isValidProxyTarget(item) || isProxy(item)) {
|
|
@@ -159,6 +169,9 @@ export const traverseObjectGraph = (
|
|
|
159
169
|
return true;
|
|
160
170
|
}
|
|
161
171
|
|
|
172
|
+
if (hasEntityElements(actualValue)) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
162
175
|
if (Array.isArray(actualValue)) {
|
|
163
176
|
for (const item of actualValue) {
|
|
164
177
|
if (traverseObjectGraph(item, visitor, visited)) {
|
|
@@ -7,6 +7,7 @@ import { invariant } from '@dxos/invariant';
|
|
|
7
7
|
import { defineHiddenProperty } from './define-hidden-property.ts';
|
|
8
8
|
import { createPropertyDeleteError, createPropertySetError } from './errors.ts';
|
|
9
9
|
import { type ReactiveHandler } from './proxy-types.ts';
|
|
10
|
+
import { EntityElementsId } from './symbols.ts';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Carries a proxy on its own target, so `value[symbolProxy] === value` identifies a proxy: read through
|
|
@@ -222,7 +223,12 @@ const normalizeForStorage = (value: any, seen = new Set<object>()): any => {
|
|
|
222
223
|
const MUTABLE_PROXY_HANDLER: ProxyHandler<any> = {
|
|
223
224
|
get: (target, property, receiver) => {
|
|
224
225
|
const value = Reflect.get(target, property, receiver);
|
|
225
|
-
|
|
226
|
+
// An entity held by identity (e.g. a tag in `meta.tags`) is not part of this object, so editing
|
|
227
|
+
// the holder must not hand out write access to it.
|
|
228
|
+
if (isProxy(value) && !(Array.isArray(target) && Reflect.get(target, EntityElementsId) === true)) {
|
|
229
|
+
return getMutableProxy(value);
|
|
230
|
+
}
|
|
231
|
+
return value;
|
|
226
232
|
},
|
|
227
233
|
set: (target, property, value, receiver) => {
|
|
228
234
|
const handler: ReactiveHandler<any> | undefined = Reflect.get(target, symbolReactiveHandler);
|
|
@@ -10,6 +10,7 @@ import * as SchemaEx from '@dxos/effect/SchemaEx';
|
|
|
10
10
|
import { invariant } from '@dxos/invariant';
|
|
11
11
|
|
|
12
12
|
import { SchemaAstId, SchemaId } from '../types/index.ts';
|
|
13
|
+
import { EntityValueAnnotationId } from './symbols.ts';
|
|
13
14
|
|
|
14
15
|
// TODO(burdon): Reconcile with @dxos/effect visit().
|
|
15
16
|
|
|
@@ -20,9 +21,10 @@ const formatIssue = SchemaIssue.makeFormatterStandardSchemaV1();
|
|
|
20
21
|
* Asserts a value against a schema, naming the offending field.
|
|
21
22
|
*
|
|
22
23
|
* v4's `Schema.asserts` throws the fixed message "Schema validation failed" and puts the detail on
|
|
23
|
-
* the error's `cause`, which leaves nothing actionable in a stack trace or a UI toast.
|
|
24
|
+
* the error's `cause`, which leaves nothing actionable in a stack trace or a UI toast. `basePath` is
|
|
25
|
+
* where `value` sits in its object, for a caller that validates one property's value on its own.
|
|
24
26
|
*/
|
|
25
|
-
export const assertsWithDetail = (schema: Schema.Top, value: unknown): void => {
|
|
27
|
+
export const assertsWithDetail = (schema: Schema.Top, value: unknown, basePath: readonly PropertyKey[] = []): void => {
|
|
26
28
|
try {
|
|
27
29
|
Schema.asserts(schema, value);
|
|
28
30
|
} catch (err) {
|
|
@@ -31,7 +33,10 @@ export const assertsWithDetail = (schema: Schema.Top, value: unknown): void => {
|
|
|
31
33
|
throw err;
|
|
32
34
|
}
|
|
33
35
|
const detail = formatIssue(issue)
|
|
34
|
-
.issues.map(({ message, path }) =>
|
|
36
|
+
.issues.map(({ message, path = [] }) => {
|
|
37
|
+
const fullPath = [...basePath, ...path];
|
|
38
|
+
return fullPath.length > 0 ? `${fullPath.map(String).join('.')}: ${message}` : message;
|
|
39
|
+
})
|
|
35
40
|
.join('; ');
|
|
36
41
|
throw new Error(detail.length > 0 ? `Schema validation failed — ${detail}` : 'Schema validation failed', {
|
|
37
42
|
cause: issue,
|
|
@@ -66,6 +71,20 @@ export class SchemaValidator {
|
|
|
66
71
|
propertyPath: KeyPath,
|
|
67
72
|
getProperty: (path: KeyPath) => any = () => null,
|
|
68
73
|
): Schema.Top {
|
|
74
|
+
const ast = this.getPropertyAstChain(rootObjectSchema, propertyPath, getProperty).at(-1) ?? rootObjectSchema.ast;
|
|
75
|
+
return ast === rootObjectSchema.ast ? rootObjectSchema : Schema.make<Schema.Top>(ast);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The AST of every property along `propertyPath`, outermost first (one per path segment), so a caller
|
|
80
|
+
* can find an annotation set on an enclosing field rather than on the written leaf.
|
|
81
|
+
*/
|
|
82
|
+
public static getPropertyAstChain(
|
|
83
|
+
rootObjectSchema: Schema.Top,
|
|
84
|
+
propertyPath: KeyPath,
|
|
85
|
+
getProperty: (path: KeyPath) => any = () => null,
|
|
86
|
+
): SchemaAST.AST[] {
|
|
87
|
+
const chain: SchemaAST.AST[] = [];
|
|
69
88
|
let ast = rootObjectSchema.ast;
|
|
70
89
|
for (let i = 0; i < propertyPath.length; i++) {
|
|
71
90
|
const propertyName = propertyPath[i];
|
|
@@ -76,21 +95,20 @@ export class SchemaValidator {
|
|
|
76
95
|
const propertyType = getPropertyType(ast, propertyName.toString(), (propertyName) =>
|
|
77
96
|
getProperty([...propertyPath.slice(0, i), propertyName]),
|
|
78
97
|
);
|
|
79
|
-
if (propertyType
|
|
98
|
+
if (propertyType != null) {
|
|
99
|
+
ast = propertyType;
|
|
100
|
+
} else {
|
|
80
101
|
const indexSignatureType = getIndexSignatureValueType(ast);
|
|
81
|
-
if (indexSignatureType
|
|
82
|
-
|
|
83
|
-
continue;
|
|
102
|
+
if (indexSignatureType == null) {
|
|
103
|
+
throw new TypeError(`Unknown property: ${formatPropertyPath([...propertyPath.slice(0, i), propertyName])}`);
|
|
84
104
|
}
|
|
85
|
-
|
|
86
|
-
throw new TypeError(`Unknown property: ${formatPropertyPath([...propertyPath.slice(0, i), propertyName])}`);
|
|
105
|
+
ast = indexSignatureType;
|
|
87
106
|
}
|
|
88
|
-
|
|
89
|
-
ast = propertyType;
|
|
90
107
|
}
|
|
108
|
+
chain.push(ast);
|
|
91
109
|
}
|
|
92
110
|
|
|
93
|
-
return
|
|
111
|
+
return chain;
|
|
94
112
|
}
|
|
95
113
|
|
|
96
114
|
/**
|
|
@@ -169,6 +187,31 @@ export class SchemaValidator {
|
|
|
169
187
|
return property != null && SchemaAST.isOptional(property.type);
|
|
170
188
|
}
|
|
171
189
|
|
|
190
|
+
/** What writing `undefined` at `propertyPath` deletes: an optional key, a union member's tag, or a key validated as a value. */
|
|
191
|
+
public static getClearedKey(
|
|
192
|
+
rootObjectSchema: Schema.Top,
|
|
193
|
+
propertyPath: KeyPath,
|
|
194
|
+
getProperty: (path: KeyPath) => unknown,
|
|
195
|
+
): 'optional' | 'union-tag' | 'other' {
|
|
196
|
+
const propertyName = propertyPath.at(-1);
|
|
197
|
+
if (propertyName === undefined) {
|
|
198
|
+
return 'other';
|
|
199
|
+
}
|
|
200
|
+
const parentPath = propertyPath.slice(0, -1);
|
|
201
|
+
const parentAst =
|
|
202
|
+
this.getPropertyAstChain(rootObjectSchema, parentPath, getProperty).at(-1) ?? rootObjectSchema.ast;
|
|
203
|
+
const property = getPropertySignature(parentAst, propertyName.toString(), (name) =>
|
|
204
|
+
getProperty([...parentPath, name]),
|
|
205
|
+
);
|
|
206
|
+
if (property == null) {
|
|
207
|
+
return 'other';
|
|
208
|
+
}
|
|
209
|
+
if (isUnionTag(parentAst, property.name)) {
|
|
210
|
+
return 'union-tag';
|
|
211
|
+
}
|
|
212
|
+
return SchemaAST.isOptional(property.type) ? 'optional' : 'other';
|
|
213
|
+
}
|
|
214
|
+
|
|
172
215
|
/** The AST that property `prop` of a typed target follows. */
|
|
173
216
|
public static getTargetPropertyAst(target: any, prop: string | symbol): SchemaAST.AST {
|
|
174
217
|
const ast = getTargetAst(target);
|
|
@@ -274,7 +317,7 @@ const getProperties = (
|
|
|
274
317
|
|
|
275
318
|
const typeDiscriminators = getTypeDiscriminators(typeAstList);
|
|
276
319
|
const targetPropertyValue = getTargetPropertyFn(String(typeDiscriminators[0].name));
|
|
277
|
-
const typeIndex = typeDiscriminators
|
|
320
|
+
const typeIndex = selectMember(typeDiscriminators, targetPropertyValue);
|
|
278
321
|
invariant(typeIndex !== -1, 'discriminator field not set on target');
|
|
279
322
|
return SchemaAST.getPropertySignatures(typeAstList[typeIndex]);
|
|
280
323
|
};
|
|
@@ -295,16 +338,7 @@ const getPropertyType = (
|
|
|
295
338
|
return ast;
|
|
296
339
|
}
|
|
297
340
|
|
|
298
|
-
const
|
|
299
|
-
return SchemaAST.isObjects(t) || (SchemaAST.isUnion(t) && t.types.some((t) => SchemaAST.isObjects(t)));
|
|
300
|
-
});
|
|
301
|
-
if (typeOrDiscriminatedUnion == null) {
|
|
302
|
-
return null;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const targetProperty = getProperties(typeOrDiscriminatedUnion, getTargetPropertyFn).find(
|
|
306
|
-
(p) => p.name === propertyName,
|
|
307
|
-
);
|
|
341
|
+
const targetProperty = getPropertySignature(ast, propertyName, getTargetPropertyFn);
|
|
308
342
|
if (targetProperty != null) {
|
|
309
343
|
return unwrapAst(targetProperty.type);
|
|
310
344
|
}
|
|
@@ -317,6 +351,32 @@ const getPropertyType = (
|
|
|
317
351
|
return null;
|
|
318
352
|
};
|
|
319
353
|
|
|
354
|
+
/**
|
|
355
|
+
* The signature of `propertyName` on a struct, or on the member of a discriminated union that the target's
|
|
356
|
+
* current discriminator selects.
|
|
357
|
+
*/
|
|
358
|
+
const getPropertySignature = (
|
|
359
|
+
ast: SchemaAST.AST,
|
|
360
|
+
propertyName: string,
|
|
361
|
+
getTargetPropertyFn: (propertyName: string) => any,
|
|
362
|
+
): SchemaAST.PropertySignature | null => {
|
|
363
|
+
const typeOrDiscriminatedUnion = unwrapAst(ast, (t) => {
|
|
364
|
+
return SchemaAST.isObjects(t) || (SchemaAST.isUnion(t) && t.types.some((t) => SchemaAST.isObjects(t)));
|
|
365
|
+
});
|
|
366
|
+
if (typeOrDiscriminatedUnion == null) {
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return getProperties(typeOrDiscriminatedUnion, getTargetPropertyFn).find((p) => p.name === propertyName) ?? null;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
/** Whether `propertyName` is the tag selecting a member of the discriminated union at `ast`. */
|
|
374
|
+
const isUnionTag = (ast: SchemaAST.AST, propertyName: PropertyKey): boolean => {
|
|
375
|
+
const union = unwrapAst(ast, (t) => SchemaAST.isUnion(t) && t.types.some((member) => SchemaAST.isObjects(member)));
|
|
376
|
+
const members = union == null ? [] : flattenUnion(union).filter((member) => SchemaAST.isObjects(member));
|
|
377
|
+
return members.length > 1 && getTypeDiscriminators(members)[0].name === propertyName;
|
|
378
|
+
};
|
|
379
|
+
|
|
320
380
|
/**
|
|
321
381
|
* Unwraps the shapes a tag takes on after a JSON schema round-trip: optional (its constructor
|
|
322
382
|
* default leaves it out of `required`) and single-member union (a one-entry `anyOf`).
|
|
@@ -328,6 +388,26 @@ const unwrapDiscriminator = (ast: SchemaAST.AST): SchemaAST.AST => {
|
|
|
328
388
|
: unwrapped;
|
|
329
389
|
};
|
|
330
390
|
|
|
391
|
+
/** The tag a union member's discriminator property declares, seen through {@link unwrapDiscriminator}. */
|
|
392
|
+
const discriminatorValue = (property: SchemaAST.PropertySignature): unknown => {
|
|
393
|
+
const tag = unwrapDiscriminator(property.type);
|
|
394
|
+
return SchemaAST.isLiteral(tag) ? tag.literal : undefined;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* The index of the union member a target's tag selects; with no tag, the one member whose tag may be omitted (-1 when
|
|
399
|
+
* none or several may be).
|
|
400
|
+
*/
|
|
401
|
+
const selectMember = (typeDiscriminators: SchemaAST.PropertySignature[], tag: unknown): number => {
|
|
402
|
+
if (tag === undefined) {
|
|
403
|
+
const omittable = typeDiscriminators.flatMap((property, index) =>
|
|
404
|
+
SchemaAST.isOptional(property.type) ? [index] : [],
|
|
405
|
+
);
|
|
406
|
+
return omittable.length === 1 ? omittable[0] : -1;
|
|
407
|
+
}
|
|
408
|
+
return typeDiscriminators.findIndex((property) => tag === discriminatorValue(property));
|
|
409
|
+
};
|
|
410
|
+
|
|
331
411
|
const getTypeDiscriminators = (typeAstList: SchemaAST.Objects[]): SchemaAST.PropertySignature[] => {
|
|
332
412
|
const discriminatorPropCandidates = typeAstList
|
|
333
413
|
.flatMap(SchemaAST.getPropertySignatures)
|
|
@@ -429,14 +509,13 @@ const resolveTypeLiteral = (
|
|
|
429
509
|
|
|
430
510
|
const typeDiscriminators = getTypeDiscriminators(typeAstList);
|
|
431
511
|
const targetPropertyValue = getTargetPropertyFn(String(typeDiscriminators[0].name));
|
|
432
|
-
const typeIndex = typeDiscriminators
|
|
433
|
-
(property) => targetPropertyValue === (property.type as SchemaAST.Literal).literal,
|
|
434
|
-
);
|
|
512
|
+
const typeIndex = selectMember(typeDiscriminators, targetPropertyValue);
|
|
435
513
|
invariant(typeIndex !== -1, 'discriminator field not set on target');
|
|
436
514
|
return typeAstList[typeIndex];
|
|
437
515
|
};
|
|
438
516
|
|
|
439
|
-
|
|
517
|
+
/** True for an object literal (or `Object.create(null)`), as opposed to an array, byte array or class instance. */
|
|
518
|
+
export const isPlainRecord = (value: unknown): value is Record<string, unknown> => {
|
|
440
519
|
if (typeof value !== 'object' || value === null || Array.isArray(value) || value instanceof Uint8Array) {
|
|
441
520
|
return false;
|
|
442
521
|
}
|
|
@@ -457,3 +536,13 @@ export const checkIdNotPresentOnSchema = (schema: Schema.Codec<any, any, any>) =
|
|
|
457
536
|
|
|
458
537
|
// TODO(burdon): Reconcile with JsonPath.
|
|
459
538
|
type KeyPath = readonly (string | number)[];
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Whether the AST describes an array of root entities held by identity (elements annotated with
|
|
542
|
+
* {@link EntityValueAnnotationId}).
|
|
543
|
+
*/
|
|
544
|
+
export const isEntityArrayAst = (ast: SchemaAST.AST): boolean => {
|
|
545
|
+
const arrayAst = unwrapArray(ast);
|
|
546
|
+
const elementAst = arrayAst?.rest[0];
|
|
547
|
+
return elementAst != null && SchemaAST.getAnnotation<boolean>(elementAst, EntityValueAnnotationId) === true;
|
|
548
|
+
};
|
|
@@ -14,3 +14,15 @@ export const ChangeId = Symbol.for('@dxos/live-object/ChangeId');
|
|
|
14
14
|
* shared proxy handler can enforce the read-only gate for every variant without dispatching.
|
|
15
15
|
*/
|
|
16
16
|
export const ChangeKeyId = Symbol.for('@dxos/live-object/ChangeKey');
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Marks an array whose elements are root entities held by identity (e.g. `meta.tags`), so the
|
|
20
|
+
* ownership, copy and reactify walkers treat each element as an opaque reference, not nested data.
|
|
21
|
+
*/
|
|
22
|
+
export const EntityElementsId = Symbol.for('@dxos/live-object/EntityElements');
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Schema annotation on a declaration whose values are root entities held by identity; an array of
|
|
26
|
+
* them is marked with {@link EntityElementsId} when a typed target is prepared or assigned.
|
|
27
|
+
*/
|
|
28
|
+
export const EntityValueAnnotationId = '@dxos/echo/annotation/EntityValue';
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
|
+
import { inspect } from 'node:util';
|
|
6
7
|
import { describe, expect, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { DXN } from '@dxos/keys';
|
|
@@ -15,6 +16,7 @@ import { Ref } from '../../Ref/index.ts';
|
|
|
15
16
|
import { SchemaAstId, SchemaId, getSchema } from '../types/index.ts';
|
|
16
17
|
import { foreignKey, getMeta } from '../types/meta.ts';
|
|
17
18
|
import { makeObject } from './make-object.ts';
|
|
19
|
+
import { getProxyTarget } from './proxy-utils.ts';
|
|
18
20
|
import { change, subscribe } from './reactive.ts';
|
|
19
21
|
|
|
20
22
|
describe('nested schemas', () => {
|
|
@@ -30,6 +32,47 @@ describe('nested schemas', () => {
|
|
|
30
32
|
});
|
|
31
33
|
});
|
|
32
34
|
|
|
35
|
+
describe('nested record layout', () => {
|
|
36
|
+
const schema = Schema.Struct({ outer: Schema.Struct({ inner: Schema.Struct({ value: Schema.Number }) }) });
|
|
37
|
+
|
|
38
|
+
// A per-record prototype or a deleted property turns the record into a V8 dictionary (~0.5 KiB each), which for
|
|
39
|
+
// a value with many nested records (an operation's JSON Schema) is most of the object's heap.
|
|
40
|
+
test('nested records share one prototype and keep their metadata as own properties', () => {
|
|
41
|
+
const first = makeObject(schema, { outer: { inner: { value: 1 } } });
|
|
42
|
+
const second = makeObject(schema, { outer: { inner: { value: 2 } } });
|
|
43
|
+
const records = [first.outer, first.outer.inner, second.outer, second.outer.inner].map((record) =>
|
|
44
|
+
getProxyTarget<object>(record),
|
|
45
|
+
);
|
|
46
|
+
const [prototype] = records.map((record) => Object.getPrototypeOf(record));
|
|
47
|
+
for (const record of records) {
|
|
48
|
+
expect(Object.getPrototypeOf(record)).toBe(prototype);
|
|
49
|
+
expect(Object.hasOwn(record, SchemaAstId)).toBe(true);
|
|
50
|
+
}
|
|
51
|
+
expect(Object.getPrototypeOf(getProxyTarget<object>(first))).not.toBe(prototype);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('nested records stay reactive and inspectable', () => {
|
|
55
|
+
const object = makeObject(schema, { outer: { inner: { value: 1 } } });
|
|
56
|
+
let calls = 0;
|
|
57
|
+
subscribe(object, () => calls++);
|
|
58
|
+
change(object, (mutable) => {
|
|
59
|
+
mutable.outer.inner.value = 2;
|
|
60
|
+
});
|
|
61
|
+
expect(object.outer.inner.value).toBe(2);
|
|
62
|
+
expect(calls).toBeGreaterThan(0);
|
|
63
|
+
expect(Object.getPrototypeOf(object.outer)).toBe(Object.prototype);
|
|
64
|
+
expect(inspect(object.outer)).toMatch(/^Typed /);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('nested arrays print without their hidden owner', () => {
|
|
68
|
+
const schema = Schema.Struct({ secret: Schema.String, items: Schema.Array(Schema.Number) });
|
|
69
|
+
const object = makeObject(schema, { secret: 'root-only', items: [1, 2] });
|
|
70
|
+
const printed = inspect(object.items, { showHidden: true });
|
|
71
|
+
expect(printed).toMatch(/^Typed /);
|
|
72
|
+
expect(printed).not.toContain('root-only');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
33
76
|
describe('complex schema validations', () => {
|
|
34
77
|
test('any', () => {
|
|
35
78
|
const schema = Schema.Struct({ field: Schema.Any });
|