@dxos/echo-protocol 0.8.4-main.8342792842 → 0.8.4-main.8360d9e660

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/echo-protocol",
3
- "version": "0.8.4-main.8342792842",
3
+ "version": "0.8.4-main.8360d9e660",
4
4
  "description": "Core ECHO APIs.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -29,11 +29,11 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "effect": "3.19.16",
32
- "@dxos/crypto": "0.8.4-main.8342792842",
33
- "@dxos/keys": "0.8.4-main.8342792842",
34
- "@dxos/protocols": "0.8.4-main.8342792842",
35
- "@dxos/util": "0.8.4-main.8342792842",
36
- "@dxos/invariant": "0.8.4-main.8342792842"
32
+ "@dxos/crypto": "0.8.4-main.8360d9e660",
33
+ "@dxos/protocols": "0.8.4-main.8360d9e660",
34
+ "@dxos/invariant": "0.8.4-main.8360d9e660",
35
+ "@dxos/keys": "0.8.4-main.8360d9e660",
36
+ "@dxos/util": "0.8.4-main.8360d9e660"
37
37
  },
38
38
  "publishConfig": {
39
39
  "access": "public"
@@ -0,0 +1,67 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { FeedProtocol } from '@dxos/protocols';
6
+
7
+ import type { ForeignKey } from './foreign-key';
8
+
9
+ /** Property name for meta when object is serialized to JSON. Matches @dxos/echo/internal ATTR_META. */
10
+ const ATTR_META = '@meta';
11
+
12
+ /**
13
+ * Codec for ECHO objects in feed block payload: JSON object ↔ UTF-8 bytes.
14
+ * Encodes with queue position stripped; decodes with optional position injection.
15
+ */
16
+ export class EchoFeedCodec {
17
+ static readonly #encoder = new TextEncoder();
18
+ static readonly #decoder = new TextDecoder();
19
+
20
+ /**
21
+ * Prepares a value for feed storage (strips queue position from metadata) and encodes to bytes.
22
+ */
23
+ static encode(value: Record<string, unknown>): Uint8Array {
24
+ const prepared = EchoFeedCodec.#stripQueuePosition(value);
25
+ return EchoFeedCodec.#encoder.encode(JSON.stringify(prepared));
26
+ }
27
+
28
+ /**
29
+ * Decodes feed block bytes to a JSON value.
30
+ * If position is provided, injects queue position into the decoded object's metadata.
31
+ */
32
+ static decode(data: Uint8Array, position?: number): Record<string, unknown> {
33
+ const decoded = JSON.parse(EchoFeedCodec.#decoder.decode(data));
34
+ if (position !== undefined && typeof decoded === 'object' && decoded !== null) {
35
+ EchoFeedCodec.#setQueuePosition(decoded, position);
36
+ }
37
+ return decoded;
38
+ }
39
+
40
+ static #stripQueuePosition(value: Record<string, unknown>): Record<string, unknown> {
41
+ if (typeof value !== 'object' || value === null) {
42
+ return value;
43
+ }
44
+ const obj = structuredClone(value);
45
+ const meta = obj[ATTR_META] as { keys?: ForeignKey[] } | undefined;
46
+ if (meta?.keys?.some((key: ForeignKey) => key.source === FeedProtocol.KEY_QUEUE_POSITION)) {
47
+ meta.keys = meta.keys.filter((key: ForeignKey) => key.source !== FeedProtocol.KEY_QUEUE_POSITION);
48
+ }
49
+ return obj;
50
+ }
51
+
52
+ static #setQueuePosition(obj: Record<string, any>, position: number): void {
53
+ obj[ATTR_META] ??= { keys: [] };
54
+ obj[ATTR_META]!.keys ??= [];
55
+ const keys = obj[ATTR_META]!.keys!;
56
+ for (let i = 0; i < keys.length; i++) {
57
+ if (keys[i].source === FeedProtocol.KEY_QUEUE_POSITION) {
58
+ keys.splice(i, 1);
59
+ i--;
60
+ }
61
+ }
62
+ keys.push({
63
+ source: FeedProtocol.KEY_QUEUE_POSITION,
64
+ id: position.toString(),
65
+ });
66
+ }
67
+ }
package/src/index.ts CHANGED
@@ -2,10 +2,11 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ export type * from './collection-sync';
5
6
  export * from './document-structure';
7
+ export * from './echo-feed-codec';
8
+ export * from './foreign-key';
9
+ export * from './query';
6
10
  export * from './reference';
7
11
  export * from './space-doc-version';
8
- export type * from './collection-sync';
9
12
  export * from './space-id';
10
- export * from './foreign-key';
11
- export * from './query';
package/src/query/ast.ts CHANGED
@@ -166,7 +166,7 @@ export const Filter = Schema.Union(
166
166
  FilterNot,
167
167
  FilterAnd,
168
168
  FilterOr,
169
- ).annotations({ identifier: 'dxos.org/schema/Filter' });
169
+ ).annotations({ identifier: 'org.dxos.schema.filter' });
170
170
 
171
171
  export type Filter = Schema.Schema.Type<typeof Filter>;
172
172
 
@@ -355,6 +355,21 @@ const QueryLimitClause_ = Schema.Struct({
355
355
  export interface QueryLimitClause extends Schema.Schema.Type<typeof QueryLimitClause_> {}
356
356
  export const QueryLimitClause: Schema.Schema<QueryLimitClause> = QueryLimitClause_;
357
357
 
358
+ export const QueryFromClause_ = Schema.Struct({
359
+ type: Schema.Literal('from'),
360
+ query: Schema.suspend(() => Query),
361
+ from: Schema.Union(
362
+ Schema.TaggedStruct('scope', {
363
+ scope: Schema.suspend(() => Scope),
364
+ }),
365
+ Schema.TaggedStruct('query', {
366
+ query: Schema.suspend(() => Query),
367
+ }),
368
+ ),
369
+ });
370
+ export interface QueryFromClause extends Schema.Schema.Type<typeof QueryFromClause_> {}
371
+ export const QueryFromClause: Schema.Schema<QueryFromClause> = QueryFromClause_;
372
+
358
373
  const Query_ = Schema.Union(
359
374
  QuerySelectClause,
360
375
  QueryFilterClause,
@@ -368,12 +383,25 @@ const Query_ = Schema.Union(
368
383
  QueryOrderClause,
369
384
  QueryOptionsClause,
370
385
  QueryLimitClause,
371
- ).annotations({ identifier: 'dxos.org/schema/Query' });
386
+ QueryFromClause,
387
+ ).annotations({ identifier: 'org.dxos.schema.query' });
372
388
 
373
389
  export type Query = Schema.Schema.Type<typeof Query_>;
374
390
  export const Query: Schema.Schema<Query> = Query_;
375
391
 
376
392
  export const QueryOptions = Schema.Struct({
393
+ /**
394
+ * Nested select statements will use this option to filter deleted objects.
395
+ */
396
+ deleted: Schema.optional(Schema.Literal('include', 'exclude', 'only')),
397
+ });
398
+
399
+ export interface QueryOptions extends Schema.Schema.Type<typeof QueryOptions> {}
400
+
401
+ /**
402
+ * Specifies the scope of the data to query from.
403
+ */
404
+ export const Scope = Schema.Struct({
377
405
  /**
378
406
  * The nested select statemets will select from the given spaces.
379
407
  *
@@ -392,14 +420,8 @@ export const QueryOptions = Schema.Struct({
392
420
  * NOTE: Spaces and queues are unioned together if both are specified.
393
421
  */
394
422
  queues: Schema.optional(Schema.Array(DXN.Schema)),
395
-
396
- /**
397
- * Nested select statements will use this option to filter deleted objects.
398
- */
399
- deleted: Schema.optional(Schema.Literal('include', 'exclude', 'only')),
400
423
  });
401
-
402
- export interface QueryOptions extends Schema.Schema.Type<typeof QueryOptions> {}
424
+ export interface Scope extends Schema.Schema.Type<typeof Scope> {}
403
425
 
404
426
  export const visit = (query: Query, visitor: (node: Query) => void) => {
405
427
  visitor(query);
@@ -419,11 +441,49 @@ export const visit = (query: Query, visitor: (node: Query) => void) => {
419
441
  }),
420
442
  Match.when({ type: 'order' }, ({ query }) => visit(query, visitor)),
421
443
  Match.when({ type: 'limit' }, ({ query }) => visit(query, visitor)),
444
+ Match.when({ type: 'from' }, (node) => {
445
+ visit(node.query, visitor);
446
+ if (node.from._tag === 'query') {
447
+ visit(node.from.query, visitor);
448
+ }
449
+ }),
422
450
  Match.when({ type: 'select' }, () => {}),
423
451
  Match.exhaustive,
424
452
  );
425
453
  };
426
454
 
455
+ /**
456
+ * Recursively transforms a query tree bottom-up.
457
+ * The mapper receives each node with its children already transformed.
458
+ */
459
+ export const map = (query: Query, mapper: (node: Query) => Query): Query => {
460
+ const mapped: Query = Match.value(query).pipe(
461
+ Match.when({ type: 'filter' }, (node) => ({ ...node, selection: map(node.selection, mapper) })),
462
+ Match.when({ type: 'reference-traversal' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
463
+ Match.when({ type: 'incoming-references' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
464
+ Match.when({ type: 'relation' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
465
+ Match.when({ type: 'relation-traversal' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
466
+ Match.when({ type: 'hierarchy-traversal' }, (node) => ({ ...node, anchor: map(node.anchor, mapper) })),
467
+ Match.when({ type: 'options' }, (node) => ({ ...node, query: map(node.query, mapper) })),
468
+ Match.when({ type: 'order' }, (node) => ({ ...node, query: map(node.query, mapper) })),
469
+ Match.when({ type: 'limit' }, (node) => ({ ...node, query: map(node.query, mapper) })),
470
+ Match.when({ type: 'from' }, (node) => ({
471
+ ...node,
472
+ query: map(node.query, mapper),
473
+ ...(node.from._tag === 'query' ? { from: { _tag: 'query' as const, query: map(node.from.query, mapper) } } : {}),
474
+ })),
475
+ Match.when({ type: 'union' }, (node) => ({ ...node, queries: node.queries.map((q) => map(q, mapper)) })),
476
+ Match.when({ type: 'set-difference' }, (node) => ({
477
+ ...node,
478
+ source: map(node.source, mapper),
479
+ exclude: map(node.exclude, mapper),
480
+ })),
481
+ Match.when({ type: 'select' }, (node) => node),
482
+ Match.exhaustive,
483
+ );
484
+ return mapper(mapped);
485
+ };
486
+
427
487
  export const fold = <T>(query: Query, reducer: (node: Query) => T): T[] => {
428
488
  return Match.value(query).pipe(
429
489
  Match.withReturnType<T[]>(),
@@ -440,6 +500,13 @@ export const fold = <T>(query: Query, reducer: (node: Query) => T): T[] => {
440
500
  ),
441
501
  Match.when({ type: 'order' }, ({ query }) => fold(query, reducer)),
442
502
  Match.when({ type: 'limit' }, ({ query }) => fold(query, reducer)),
503
+ Match.when({ type: 'from' }, (node) => {
504
+ const results = fold(node.query, reducer);
505
+ if (node.from._tag === 'query') {
506
+ return results.concat(fold(node.from.query, reducer));
507
+ }
508
+ return results;
509
+ }),
443
510
  Match.when({ type: 'select' }, () => []),
444
511
  Match.exhaustive,
445
512
  );