@dxos/echo-protocol 0.8.4-main.e098934 → 0.8.4-main.e250131250

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.
@@ -1,598 +0,0 @@
1
- import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
- var __defProp = Object.defineProperty;
3
- var __export = (target, all) => {
4
- for (var name in all)
5
- __defProp(target, name, { get: all[name], enumerable: true });
6
- };
7
-
8
- // src/document-structure.ts
9
- import { invariant } from "@dxos/invariant";
10
- import { visitValues } from "@dxos/util";
11
-
12
- // src/reference.ts
13
- import { assertArgument } from "@dxos/invariant";
14
- import { DXN, LOCAL_SPACE_TAG } from "@dxos/keys";
15
- function _define_property(obj, key, value) {
16
- if (key in obj) {
17
- Object.defineProperty(obj, key, {
18
- value,
19
- enumerable: true,
20
- configurable: true,
21
- writable: true
22
- });
23
- } else {
24
- obj[key] = value;
25
- }
26
- return obj;
27
- }
28
- var Reference = class _Reference {
29
- static fromDXN(dxn) {
30
- switch (dxn.kind) {
31
- case DXN.kind.TYPE:
32
- return new _Reference(dxn.parts[0], _Reference.TYPE_PROTOCOL, "dxos.org", dxn);
33
- case DXN.kind.ECHO:
34
- if (dxn.parts[0] === LOCAL_SPACE_TAG) {
35
- return new _Reference(dxn.parts[1], void 0, void 0, dxn);
36
- } else {
37
- return new _Reference(dxn.parts[1], void 0, dxn.parts[0], dxn);
38
- }
39
- default:
40
- return new _Reference(dxn.parts[0], void 0, dxn.parts[0], dxn);
41
- }
42
- }
43
- static fromValue(value) {
44
- return new _Reference(value.objectId, value.protocol, value.host);
45
- }
46
- /**
47
- * Reference an object in the local space.
48
- */
49
- static localObjectReference(objectId) {
50
- return new _Reference(objectId);
51
- }
52
- /**
53
- * @deprecated
54
- */
55
- // TODO(dmaretskyi): Remove.
56
- static fromLegacyTypename(type) {
57
- return new _Reference(type, _Reference.TYPE_PROTOCOL, "dxos.org");
58
- }
59
- /**
60
- * @deprecated
61
- */
62
- // TODO(dmaretskyi): Remove
63
- static fromObjectIdAndSpaceKey(objectId, spaceKey) {
64
- return new _Reference(objectId, void 0, spaceKey.toHex());
65
- }
66
- get dxn() {
67
- return this._dxn;
68
- }
69
- /**
70
- * @deprecated
71
- */
72
- // TODO(dmaretskyi): Remove.
73
- get objectId() {
74
- return this._objectId;
75
- }
76
- /**
77
- * @deprecated
78
- */
79
- // TODO(dmaretskyi): Remove.
80
- get protocol() {
81
- return this._protocol;
82
- }
83
- /**
84
- * @deprecated
85
- */
86
- // TODO(dmaretskyi): Remove.
87
- get host() {
88
- return this._host;
89
- }
90
- encode() {
91
- return {
92
- objectId: this.objectId,
93
- host: this.host,
94
- protocol: this.protocol
95
- };
96
- }
97
- // TODO(dmaretskyi): Remove in favor of `reference.dxn`.
98
- toDXN() {
99
- if (this._dxn) {
100
- return this._dxn;
101
- }
102
- if (this.protocol === _Reference.TYPE_PROTOCOL) {
103
- return new DXN(DXN.kind.TYPE, [
104
- this.objectId
105
- ]);
106
- } else {
107
- if (this.host) {
108
- return new DXN(DXN.kind.ECHO, [
109
- this.host,
110
- this.objectId
111
- ]);
112
- } else {
113
- return new DXN(DXN.kind.ECHO, [
114
- LOCAL_SPACE_TAG,
115
- this.objectId
116
- ]);
117
- }
118
- }
119
- }
120
- // prettier-ignore
121
- constructor(_objectId, _protocol, _host, _dxn) {
122
- _define_property(this, "_objectId", void 0);
123
- _define_property(this, "_protocol", void 0);
124
- _define_property(this, "_host", void 0);
125
- _define_property(this, "_dxn", void 0);
126
- this._objectId = _objectId;
127
- this._protocol = _protocol;
128
- this._host = _host;
129
- this._dxn = _dxn;
130
- }
131
- };
132
- _define_property(Reference, "TYPE_PROTOCOL", "protobuf");
133
- var REFERENCE_TYPE_TAG = "dxos.echo.model.document.Reference";
134
- var encodeReference = (reference) => ({
135
- "/": reference.toDXN().toString()
136
- });
137
- var decodeReference = (value) => {
138
- if (typeof value !== "object" || value === null || typeof value["/"] !== "string") {
139
- throw new Error("Invalid reference");
140
- }
141
- const dxnString = value["/"];
142
- if (dxnString.length % 2 === 0 && dxnString.slice(0, dxnString.length / 2) === dxnString.slice(dxnString.length / 2) && dxnString.includes("dxn:echo")) {
143
- throw new Error("Automerge bug detected!");
144
- }
145
- return Reference.fromDXN(DXN.parse(dxnString));
146
- };
147
- var isEncodedReference = (value) => typeof value === "object" && value !== null && Object.keys(value).length === 1 && typeof value["/"] === "string";
148
- var EncodedReference = Object.freeze({
149
- isEncodedReference,
150
- getReferenceString: (value) => {
151
- assertArgument(isEncodedReference(value), "value", "invalid reference");
152
- return value["/"];
153
- },
154
- toDXN: (value) => {
155
- return DXN.parse(EncodedReference.getReferenceString(value));
156
- },
157
- fromDXN: (dxn) => {
158
- return encodeReference(Reference.fromDXN(dxn));
159
- }
160
- });
161
-
162
- // src/document-structure.ts
163
- var __dxlog_file = "/__w/dxos/dxos/packages/core/echo/echo-protocol/src/document-structure.ts";
164
- var DatabaseDirectory = Object.freeze({
165
- /**
166
- * @returns Space key in hex of the space that owns the document. In hex format. Without 0x prefix.
167
- */
168
- getSpaceKey: (doc) => {
169
- const rawSpaceKey = doc.access?.spaceKey ?? doc.experimental_spaceKey;
170
- if (rawSpaceKey == null) {
171
- return null;
172
- }
173
- const rawKey = String(rawSpaceKey);
174
- invariant(!rawKey.startsWith("0x"), "Space key must not start with 0x", {
175
- F: __dxlog_file,
176
- L: 66,
177
- S: void 0,
178
- A: [
179
- "!rawKey.startsWith('0x')",
180
- "'Space key must not start with 0x'"
181
- ]
182
- });
183
- return rawKey;
184
- },
185
- getInlineObject: (doc, id) => {
186
- return doc.objects?.[id];
187
- },
188
- getLink: (doc, id) => {
189
- return doc.links?.[id]?.toString();
190
- },
191
- make: ({ spaceKey, objects, links }) => ({
192
- access: {
193
- spaceKey
194
- },
195
- objects: objects ?? {},
196
- links: links ?? {}
197
- })
198
- });
199
- var ObjectStructure = Object.freeze({
200
- /**
201
- * @throws On invalid object structure.
202
- */
203
- getTypeReference: (object) => {
204
- return object.system?.type;
205
- },
206
- /**
207
- * @throws On invalid object structure.
208
- */
209
- getEntityKind: (object) => {
210
- const kind = object.system?.kind ?? "object";
211
- invariant(kind === "object" || kind === "relation", "Invalid kind", {
212
- F: __dxlog_file,
213
- L: 124,
214
- S: void 0,
215
- A: [
216
- "kind === 'object' || kind === 'relation'",
217
- "'Invalid kind'"
218
- ]
219
- });
220
- return kind;
221
- },
222
- isDeleted: (object) => {
223
- return object.system?.deleted ?? false;
224
- },
225
- getRelationSource: (object) => {
226
- return object.system?.source;
227
- },
228
- getRelationTarget: (object) => {
229
- return object.system?.target;
230
- },
231
- /**
232
- * @returns All references in the data section of the object.
233
- */
234
- getAllOutgoingReferences: (object) => {
235
- const references = [];
236
- const visit2 = (path, value) => {
237
- if (isEncodedReference(value)) {
238
- references.push({
239
- path,
240
- reference: value
241
- });
242
- } else {
243
- visitValues(value, (value2, key) => visit2([
244
- ...path,
245
- String(key)
246
- ], value2));
247
- }
248
- };
249
- visitValues(object.data, (value, key) => visit2([
250
- String(key)
251
- ], value));
252
- return references;
253
- },
254
- makeObject: ({ type, data, keys }) => {
255
- return {
256
- system: {
257
- kind: "object",
258
- type: {
259
- "/": type
260
- }
261
- },
262
- meta: {
263
- keys: keys ?? []
264
- },
265
- data: data ?? {}
266
- };
267
- },
268
- makeRelation: ({ type, source, target, deleted, keys, data }) => {
269
- return {
270
- system: {
271
- kind: "relation",
272
- type: {
273
- "/": type
274
- },
275
- source,
276
- target,
277
- deleted: deleted ?? false
278
- },
279
- meta: {
280
- keys: keys ?? []
281
- },
282
- data: data ?? {}
283
- };
284
- }
285
- });
286
- var PROPERTY_ID = "id";
287
- var DATA_NAMESPACE = "data";
288
-
289
- // src/space-doc-version.ts
290
- var SpaceDocVersion = Object.freeze({
291
- /**
292
- * For the documents created before the versioning was introduced.
293
- */
294
- LEGACY: 0,
295
- /**
296
- * Current version.
297
- */
298
- CURRENT: 1
299
- });
300
-
301
- // src/space-id.ts
302
- import { subtleCrypto } from "@dxos/crypto";
303
- import { PublicKey, SpaceId } from "@dxos/keys";
304
- import { ComplexMap } from "@dxos/util";
305
- var SPACE_IDS_CACHE = new ComplexMap(PublicKey.hash);
306
- var createIdFromSpaceKey = async (spaceKey) => {
307
- const cachedValue = SPACE_IDS_CACHE.get(spaceKey);
308
- if (cachedValue !== void 0) {
309
- return cachedValue;
310
- }
311
- const digest = await subtleCrypto.digest("SHA-256", spaceKey.asUint8Array());
312
- const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);
313
- const spaceId = SpaceId.encode(bytes);
314
- SPACE_IDS_CACHE.set(spaceKey, spaceId);
315
- return spaceId;
316
- };
317
-
318
- // src/foreign-key.ts
319
- import { Schema, SchemaAST } from "effect";
320
- var ForeignKey_ = Schema.Struct({
321
- /**
322
- * Name of the foreign database/system.
323
- * E.g., `github.com`.
324
- */
325
- source: Schema.String,
326
- /**
327
- * Id within the foreign database.
328
- */
329
- // TODO(wittjosiah): This annotation is currently used to ensure id field shows up in forms.
330
- // TODO(dmaretskyi): `false` is not a valid value for the annotation.
331
- id: Schema.String.annotations({
332
- [SchemaAST.IdentifierAnnotationId]: false
333
- })
334
- });
335
- var ForeignKey = ForeignKey_;
336
-
337
- // src/query/ast.ts
338
- var ast_exports = {};
339
- __export(ast_exports, {
340
- Filter: () => Filter,
341
- FilterAnd: () => FilterAnd,
342
- FilterCompare: () => FilterCompare,
343
- FilterContains: () => FilterContains,
344
- FilterIn: () => FilterIn,
345
- FilterNot: () => FilterNot,
346
- FilterObject: () => FilterObject,
347
- FilterOr: () => FilterOr,
348
- FilterRange: () => FilterRange,
349
- FilterTextSearch: () => FilterTextSearch,
350
- Order: () => Order,
351
- OrderDirection: () => OrderDirection,
352
- Query: () => Query,
353
- QueryFilterClause: () => QueryFilterClause,
354
- QueryIncomingReferencesClause: () => QueryIncomingReferencesClause,
355
- QueryOptions: () => QueryOptions,
356
- QueryOptionsClause: () => QueryOptionsClause,
357
- QueryOrderClause: () => QueryOrderClause,
358
- QueryReferenceTraversalClause: () => QueryReferenceTraversalClause,
359
- QueryRelationClause: () => QueryRelationClause,
360
- QueryRelationTraversalClause: () => QueryRelationTraversalClause,
361
- QuerySelectClause: () => QuerySelectClause,
362
- QuerySetDifferenceClause: () => QuerySetDifferenceClause,
363
- QueryUnionClause: () => QueryUnionClause,
364
- fold: () => fold,
365
- visit: () => visit
366
- });
367
- import { Match, Schema as Schema2 } from "effect";
368
- import { DXN as DXN2, ObjectId } from "@dxos/keys";
369
- var TypenameSpecifier = Schema2.Union(DXN2.Schema, Schema2.Null).annotations({
370
- description: "DXN or null. Null means any type will match"
371
- });
372
- var FilterObject_ = Schema2.Struct({
373
- type: Schema2.Literal("object"),
374
- typename: TypenameSpecifier,
375
- id: Schema2.optional(Schema2.Array(ObjectId)),
376
- /**
377
- * Filter by property.
378
- * Must not include object ID.
379
- */
380
- props: Schema2.Record({
381
- key: Schema2.String.annotations({
382
- description: "Property name"
383
- }),
384
- value: Schema2.suspend(() => Filter)
385
- }),
386
- /**
387
- * Objects that have any of the given foreign keys.
388
- */
389
- foreignKeys: Schema2.optional(Schema2.Array(ForeignKey))
390
- });
391
- var FilterObject = FilterObject_;
392
- var FilterCompare_ = Schema2.Struct({
393
- type: Schema2.Literal("compare"),
394
- operator: Schema2.Literal("eq", "neq", "gt", "gte", "lt", "lte"),
395
- value: Schema2.Unknown
396
- });
397
- var FilterCompare = FilterCompare_;
398
- var FilterIn_ = Schema2.Struct({
399
- type: Schema2.Literal("in"),
400
- values: Schema2.Array(Schema2.Any)
401
- });
402
- var FilterIn = FilterIn_;
403
- var FilterContains_ = Schema2.Struct({
404
- type: Schema2.Literal("contains"),
405
- value: Schema2.Any
406
- });
407
- var FilterContains = FilterContains_;
408
- var FilterRange_ = Schema2.Struct({
409
- type: Schema2.Literal("range"),
410
- from: Schema2.Any,
411
- to: Schema2.Any
412
- });
413
- var FilterRange = FilterRange_;
414
- var FilterTextSearch_ = Schema2.Struct({
415
- type: Schema2.Literal("text-search"),
416
- text: Schema2.String,
417
- searchKind: Schema2.optional(Schema2.Literal("full-text", "vector"))
418
- });
419
- var FilterTextSearch = FilterTextSearch_;
420
- var FilterNot_ = Schema2.Struct({
421
- type: Schema2.Literal("not"),
422
- filter: Schema2.suspend(() => Filter)
423
- });
424
- var FilterNot = FilterNot_;
425
- var FilterAnd_ = Schema2.Struct({
426
- type: Schema2.Literal("and"),
427
- filters: Schema2.Array(Schema2.suspend(() => Filter))
428
- });
429
- var FilterAnd = FilterAnd_;
430
- var FilterOr_ = Schema2.Struct({
431
- type: Schema2.Literal("or"),
432
- filters: Schema2.Array(Schema2.suspend(() => Filter))
433
- });
434
- var FilterOr = FilterOr_;
435
- var Filter = Schema2.Union(FilterObject, FilterTextSearch, FilterCompare, FilterIn, FilterContains, FilterRange, FilterNot, FilterAnd, FilterOr);
436
- var QuerySelectClause_ = Schema2.Struct({
437
- type: Schema2.Literal("select"),
438
- filter: Schema2.suspend(() => Filter)
439
- });
440
- var QuerySelectClause = QuerySelectClause_;
441
- var QueryFilterClause_ = Schema2.Struct({
442
- type: Schema2.Literal("filter"),
443
- selection: Schema2.suspend(() => Query),
444
- filter: Schema2.suspend(() => Filter)
445
- });
446
- var QueryFilterClause = QueryFilterClause_;
447
- var QueryReferenceTraversalClause_ = Schema2.Struct({
448
- type: Schema2.Literal("reference-traversal"),
449
- anchor: Schema2.suspend(() => Query),
450
- property: Schema2.String
451
- });
452
- var QueryReferenceTraversalClause = QueryReferenceTraversalClause_;
453
- var QueryIncomingReferencesClause_ = Schema2.Struct({
454
- type: Schema2.Literal("incoming-references"),
455
- anchor: Schema2.suspend(() => Query),
456
- property: Schema2.String,
457
- typename: TypenameSpecifier
458
- });
459
- var QueryIncomingReferencesClause = QueryIncomingReferencesClause_;
460
- var QueryRelationClause_ = Schema2.Struct({
461
- type: Schema2.Literal("relation"),
462
- anchor: Schema2.suspend(() => Query),
463
- /**
464
- * outgoing: anchor is the source of the relation.
465
- * incoming: anchor is the target of the relation.
466
- * both: anchor is either the source or target of the relation.
467
- */
468
- direction: Schema2.Literal("outgoing", "incoming", "both"),
469
- filter: Schema2.optional(Schema2.suspend(() => Filter))
470
- });
471
- var QueryRelationClause = QueryRelationClause_;
472
- var QueryRelationTraversalClause_ = Schema2.Struct({
473
- type: Schema2.Literal("relation-traversal"),
474
- anchor: Schema2.suspend(() => Query),
475
- direction: Schema2.Literal("source", "target", "both")
476
- });
477
- var QueryRelationTraversalClause = QueryRelationTraversalClause_;
478
- var QueryUnionClause_ = Schema2.Struct({
479
- type: Schema2.Literal("union"),
480
- queries: Schema2.Array(Schema2.suspend(() => Query))
481
- });
482
- var QueryUnionClause = QueryUnionClause_;
483
- var QuerySetDifferenceClause_ = Schema2.Struct({
484
- type: Schema2.Literal("set-difference"),
485
- source: Schema2.suspend(() => Query),
486
- exclude: Schema2.suspend(() => Query)
487
- });
488
- var QuerySetDifferenceClause = QuerySetDifferenceClause_;
489
- var OrderDirection = Schema2.Literal("asc", "desc");
490
- var Order_ = Schema2.Union(Schema2.Struct({
491
- // How database wants to order them (in practice - by id).
492
- kind: Schema2.Literal("natural")
493
- }), Schema2.Struct({
494
- kind: Schema2.Literal("property"),
495
- property: Schema2.String,
496
- direction: OrderDirection
497
- }));
498
- var Order = Order_;
499
- var QueryOrderClause_ = Schema2.Struct({
500
- type: Schema2.Literal("order"),
501
- query: Schema2.suspend(() => Query),
502
- order: Schema2.Array(Order)
503
- });
504
- var QueryOrderClause = QueryOrderClause_;
505
- var QueryOptionsClause_ = Schema2.Struct({
506
- type: Schema2.Literal("options"),
507
- query: Schema2.suspend(() => Query),
508
- options: Schema2.suspend(() => QueryOptions)
509
- });
510
- var QueryOptionsClause = QueryOptionsClause_;
511
- var Query_ = Schema2.Union(QuerySelectClause, QueryFilterClause, QueryReferenceTraversalClause, QueryIncomingReferencesClause, QueryRelationClause, QueryRelationTraversalClause, QueryUnionClause, QuerySetDifferenceClause, QueryOrderClause, QueryOptionsClause);
512
- var Query = Query_;
513
- var QueryOptions = Schema2.Struct({
514
- /**
515
- * The nested select statemets will select from the given spaces.
516
- *
517
- * NOTE: Spaces and queues are unioned together if both are specified.
518
- */
519
- spaceIds: Schema2.optional(Schema2.Array(Schema2.String)),
520
- /**
521
- * The nested select statemets will select from the given queues.
522
- *
523
- * NOTE: Spaces and queues are unioned together if both are specified.
524
- */
525
- queues: Schema2.optional(Schema2.Array(DXN2.Schema)),
526
- /**
527
- * Nested select statements will use this option to filter deleted objects.
528
- */
529
- deleted: Schema2.optional(Schema2.Literal("include", "exclude", "only"))
530
- });
531
- var visit = (query, visitor) => {
532
- visitor(query);
533
- Match.value(query).pipe(Match.when({
534
- type: "filter"
535
- }, ({ selection }) => visit(selection, visitor)), Match.when({
536
- type: "reference-traversal"
537
- }, ({ anchor }) => visit(anchor, visitor)), Match.when({
538
- type: "incoming-references"
539
- }, ({ anchor }) => visit(anchor, visitor)), Match.when({
540
- type: "relation"
541
- }, ({ anchor }) => visit(anchor, visitor)), Match.when({
542
- type: "options"
543
- }, ({ query: query2 }) => visit(query2, visitor)), Match.when({
544
- type: "relation-traversal"
545
- }, ({ anchor }) => visit(anchor, visitor)), Match.when({
546
- type: "union"
547
- }, ({ queries }) => queries.forEach((q) => visit(q, visitor))), Match.when({
548
- type: "set-difference"
549
- }, ({ source, exclude }) => {
550
- visit(source, visitor);
551
- visit(exclude, visitor);
552
- }), Match.when({
553
- type: "order"
554
- }, ({ query: query2 }) => visit(query2, visitor)), Match.when({
555
- type: "select"
556
- }, () => {
557
- }), Match.exhaustive);
558
- };
559
- var fold = (query, reducer) => {
560
- return Match.value(query).pipe(Match.withReturnType(), Match.when({
561
- type: "filter"
562
- }, ({ selection }) => fold(selection, reducer)), Match.when({
563
- type: "reference-traversal"
564
- }, ({ anchor }) => fold(anchor, reducer)), Match.when({
565
- type: "incoming-references"
566
- }, ({ anchor }) => fold(anchor, reducer)), Match.when({
567
- type: "relation"
568
- }, ({ anchor }) => fold(anchor, reducer)), Match.when({
569
- type: "options"
570
- }, ({ query: query2 }) => fold(query2, reducer)), Match.when({
571
- type: "relation-traversal"
572
- }, ({ anchor }) => fold(anchor, reducer)), Match.when({
573
- type: "union"
574
- }, ({ queries }) => queries.flatMap((q) => fold(q, reducer))), Match.when({
575
- type: "set-difference"
576
- }, ({ source, exclude }) => fold(source, reducer).concat(fold(exclude, reducer))), Match.when({
577
- type: "order"
578
- }, ({ query: query2 }) => fold(query2, reducer)), Match.when({
579
- type: "select"
580
- }, () => []), Match.exhaustive);
581
- };
582
- export {
583
- DATA_NAMESPACE,
584
- DatabaseDirectory,
585
- EncodedReference,
586
- ForeignKey,
587
- ObjectStructure,
588
- PROPERTY_ID,
589
- ast_exports as QueryAST,
590
- REFERENCE_TYPE_TAG,
591
- Reference,
592
- SpaceDocVersion,
593
- createIdFromSpaceKey,
594
- decodeReference,
595
- encodeReference,
596
- isEncodedReference
597
- };
598
- //# sourceMappingURL=index.mjs.map