@dxos/functions-runtime-cloudflare 0.8.4-main.ef1bc66f44 → 0.8.4-main.fcfe5033a5

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/functions-runtime-cloudflare",
3
- "version": "0.8.4-main.ef1bc66f44",
3
+ "version": "0.8.4-main.fcfe5033a5",
4
4
  "description": "Functions runtime for Cloudflare.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -27,23 +27,23 @@
27
27
  "src"
28
28
  ],
29
29
  "dependencies": {
30
- "effect": "3.19.16",
31
- "@dxos/codec-protobuf": "0.8.4-main.ef1bc66f44",
32
- "@dxos/debug": "0.8.4-main.ef1bc66f44",
33
- "@dxos/echo": "0.8.4-main.ef1bc66f44",
34
- "@dxos/context": "0.8.4-main.ef1bc66f44",
35
- "@dxos/echo-protocol": "0.8.4-main.ef1bc66f44",
36
- "@dxos/errors": "0.8.4-main.ef1bc66f44",
37
- "@dxos/invariant": "0.8.4-main.ef1bc66f44",
38
- "@dxos/keys": "0.8.4-main.ef1bc66f44",
39
- "@dxos/echo-db": "0.8.4-main.ef1bc66f44",
40
- "@dxos/log": "0.8.4-main.ef1bc66f44",
41
- "@dxos/protocols": "0.8.4-main.ef1bc66f44",
42
- "@dxos/util": "0.8.4-main.ef1bc66f44"
30
+ "effect": "3.20.0",
31
+ "@dxos/context": "0.8.4-main.fcfe5033a5",
32
+ "@dxos/codec-protobuf": "0.8.4-main.fcfe5033a5",
33
+ "@dxos/debug": "0.8.4-main.fcfe5033a5",
34
+ "@dxos/echo": "0.8.4-main.fcfe5033a5",
35
+ "@dxos/echo-protocol": "0.8.4-main.fcfe5033a5",
36
+ "@dxos/invariant": "0.8.4-main.fcfe5033a5",
37
+ "@dxos/errors": "0.8.4-main.fcfe5033a5",
38
+ "@dxos/keys": "0.8.4-main.fcfe5033a5",
39
+ "@dxos/echo-db": "0.8.4-main.fcfe5033a5",
40
+ "@dxos/protocols": "0.8.4-main.fcfe5033a5",
41
+ "@dxos/util": "0.8.4-main.fcfe5033a5",
42
+ "@dxos/log": "0.8.4-main.fcfe5033a5"
43
43
  },
44
44
  "devDependencies": {
45
- "@cloudflare/workers-types": "^4.20251210.0",
46
- "@dxos/types": "0.8.4-main.ef1bc66f44"
45
+ "@cloudflare/workers-types": "^4.20260302.0",
46
+ "@dxos/types": "0.8.4-main.fcfe5033a5"
47
47
  },
48
48
  "publishConfig": {
49
49
  "access": "public"
@@ -24,7 +24,7 @@ type Services = {
24
24
  export class FunctionsClient extends Resource {
25
25
  private readonly _serviceContainer;
26
26
  private readonly _echoClient;
27
- private readonly _executionContext: EdgeFunctionEnv.ExecutionContext = {};
27
+ private readonly _executionContext: EdgeFunctionEnv.TraceContext = {};
28
28
 
29
29
  private readonly _spaces = new Map<SpaceId, SpaceProxy>();
30
30
 
@@ -30,7 +30,7 @@ export class DataServiceImpl implements DataServiceProto {
30
30
  private dataSubscriptions = new Map<string, { spaceId: SpaceId; next: (msg: BatchedDocumentUpdates) => void }>();
31
31
 
32
32
  constructor(
33
- private _executionContext: EdgeFunctionEnv.ExecutionContext,
33
+ private _executionContext: EdgeFunctionEnv.TraceContext,
34
34
  private _dataService: EdgeFunctionEnv.DataService,
35
35
  ) {}
36
36
 
@@ -80,7 +80,8 @@ export class DataServiceImpl implements DataServiceProto {
80
80
  }
81
81
 
82
82
  async createDocument({ spaceId, initialValue }: CreateDocumentRequest): Promise<CreateDocumentResponse> {
83
- using response = await this._dataService.createDocument(this._executionContext, { spaceId, initialValue });
83
+ invariant(SpaceId.isValid(spaceId));
84
+ using response = await this._dataService.createDocument(this._executionContext, spaceId, initialValue);
84
85
  return { documentId: response.documentId };
85
86
  }
86
87
 
@@ -2,71 +2,36 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import * as Schema from 'effect/Schema';
6
-
7
5
  import { Stream } from '@dxos/codec-protobuf/stream';
8
- import { QueryAST } from '@dxos/echo-protocol';
9
6
  import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
10
- import { invariant } from '@dxos/invariant';
11
- import { PublicKey } from '@dxos/keys';
12
- import { SpaceId } from '@dxos/keys';
13
7
  import { log } from '@dxos/log';
14
8
  import { type EdgeFunctionEnv } from '@dxos/protocols';
15
- import {
16
- type QueryRequest,
17
- type QueryResponse,
18
- type QueryResult as QueryResultProto,
19
- type QueryService as QueryServiceProto,
20
- } from '@dxos/protocols/proto/dxos/echo/query';
21
-
22
- import { queryToDataServiceRequest } from './adapter';
23
- import { copyUint8Array } from './utils';
9
+ import { type QueryRequest, type QueryResponse, type QueryService } from '@dxos/protocols/proto/dxos/echo/query';
24
10
 
25
- export class QueryServiceImpl implements QueryServiceProto {
11
+ export class QueryServiceImpl implements QueryService {
26
12
  private _queryCount = 0;
27
13
 
28
14
  constructor(
29
- private readonly _executionContext: EdgeFunctionEnv.ExecutionContext,
15
+ private readonly _executionContext: EdgeFunctionEnv.TraceContext,
30
16
  private readonly _dataService: EdgeFunctionEnv.DataService,
31
17
  ) {}
32
18
 
33
19
  execQuery(request: QueryRequest): Stream<QueryResponse> {
34
20
  log.info('execQuery', { request });
35
- const query = QueryAST.Query.pipe(Schema.decodeUnknownSync)(JSON.parse(request.query));
36
- const requestedSpaceIds = getTargetSpacesForQuery(query);
37
- invariant(requestedSpaceIds.length === 1, 'Only one space is supported');
38
- const spaceId = requestedSpaceIds[0];
39
21
 
40
22
  return Stream.fromPromise<QueryResponse>(
41
23
  (async () => {
42
24
  try {
43
25
  this._queryCount++;
44
- log.info('begin query', { spaceId });
45
- using queryResponse = await this._dataService.queryDocuments(
46
- this._executionContext,
47
- queryToDataServiceRequest(query),
48
- );
49
- log.info('query response', { spaceId, filter: request.filter, resultCount: queryResponse.results.length });
50
- return {
51
- results: queryResponse.results.map(
52
- (object): QueryResultProto => ({
53
- id: object.objectId,
54
- spaceId,
55
- spaceKey: PublicKey.ZERO,
56
- documentId: object.document.documentId,
57
- // Rank 1 for predicate matches where ranking is not determined.
58
- rank: 1,
59
- // Copy returned object to avoid hanging RPC stub.
60
- // See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
61
- documentAutomerge: copyUint8Array(object.document.data),
62
- }),
63
- ),
64
- } satisfies QueryResponse;
26
+ log.info('begin query', { request });
27
+ using queryResponse = await this._dataService.execQuery(this._executionContext, request);
28
+ log.info('query response', { resultCount: queryResponse.results?.length });
29
+ return structuredClone(queryResponse);
65
30
  } catch (error) {
66
31
  log.error('query failed', { err: error });
67
32
  throw new RuntimeServiceError({
68
33
  message: `Query execution failed (queryCount=${this._queryCount})`,
69
- context: { spaceId, filter: request.filter, queryCount: this._queryCount },
34
+ context: { queryCount: this._queryCount },
70
35
  cause: error,
71
36
  });
72
37
  }
@@ -86,22 +51,3 @@ export class QueryServiceImpl implements QueryServiceProto {
86
51
  });
87
52
  }
88
53
  }
89
-
90
- /**
91
- * Lists spaces this query will select from.
92
- */
93
- export const getTargetSpacesForQuery = (query: QueryAST.Query): SpaceId[] => {
94
- const spaces = new Set<SpaceId>();
95
-
96
- const visitor = (node: QueryAST.Query) => {
97
- if (node.type === 'options') {
98
- if (node.options.spaceIds) {
99
- for (const spaceId of node.options.spaceIds) {
100
- spaces.add(SpaceId.make(spaceId));
101
- }
102
- }
103
- }
104
- };
105
- QueryAST.visit(query, visitor);
106
- return [...spaces];
107
- };
@@ -2,48 +2,43 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
6
- import { invariant } from '@dxos/invariant';
5
+ import { RuntimeServiceError } from '@dxos/errors';
7
6
  import { type EdgeFunctionEnv, type FeedProtocol } from '@dxos/protocols';
8
7
 
9
8
  export class QueueServiceImpl implements FeedProtocol.QueueService {
10
9
  constructor(
11
- protected _ctx: EdgeFunctionEnv.ExecutionContext,
10
+ protected _ctx: EdgeFunctionEnv.TraceContext,
12
11
  private readonly _queueService: EdgeFunctionEnv.QueueService,
13
12
  ) {}
13
+
14
14
  async queryQueue(request: FeedProtocol.QueryQueueRequest): Promise<FeedProtocol.QueryResult> {
15
- const { query } = request;
16
- const { queueIds, ...filter } = query!;
17
- const spaceId = query!.spaceId;
18
- const queueId = queueIds?.[0];
19
- invariant(request.query.queuesNamespace);
20
15
  try {
21
- using result = await this._queueService.query(
22
- this._ctx,
23
- `dxn:queue:${request.query.queuesNamespace}:${spaceId}:${queueId}`,
24
- filter,
25
- );
16
+ using result = await this._queueService.queryQueue(this._ctx, request);
17
+ // Copy to avoid hanging RPC stub (Workers RPC lifecycle).
26
18
  return {
27
- // Copy returned object to avoid hanging RPC stub
28
- // See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
29
19
  objects: structuredClone(result.objects),
30
20
  nextCursor: result.nextCursor,
31
21
  prevCursor: result.prevCursor,
32
22
  };
33
23
  } catch (error) {
24
+ const { query } = request;
34
25
  throw RuntimeServiceError.wrap({
35
26
  message: 'Queue query failed.',
36
- context: { subspaceTag: request.query.queuesNamespace, spaceId, queueId },
27
+ context: {
28
+ subspaceTag: query?.queuesNamespace,
29
+ spaceId: query?.spaceId,
30
+ queueId: query?.queueIds?.[0],
31
+ },
37
32
  ifTypeDiffers: true,
38
33
  })(error);
39
34
  }
40
35
  }
41
36
 
42
37
  async insertIntoQueue(request: FeedProtocol.InsertIntoQueueRequest): Promise<void> {
43
- const { subspaceTag, spaceId, queueId, objects } = request;
44
38
  try {
45
- await this._queueService.append(this._ctx, `dxn:queue:${subspaceTag}:${spaceId}:${queueId}`, objects ?? []);
39
+ using _ = await this._queueService.insertIntoQueue(this._ctx, request);
46
40
  } catch (error) {
41
+ const { subspaceTag, spaceId, queueId } = request;
47
42
  throw RuntimeServiceError.wrap({
48
43
  message: 'Queue append failed.',
49
44
  context: { subspaceTag, spaceId, queueId },
@@ -52,11 +47,20 @@ export class QueueServiceImpl implements FeedProtocol.QueueService {
52
47
  }
53
48
  }
54
49
 
55
- deleteFromQueue(request: FeedProtocol.DeleteFromQueueRequest): Promise<void> {
56
- const { subspaceTag, spaceId, queueId } = request;
57
- throw new NotImplementedError({
58
- message: 'Deleting from queue is not supported.',
59
- context: { subspaceTag, spaceId, queueId },
60
- });
50
+ async deleteFromQueue(request: FeedProtocol.DeleteFromQueueRequest): Promise<void> {
51
+ try {
52
+ using _ = await this._queueService.deleteFromQueue(this._ctx, request);
53
+ } catch (error) {
54
+ const { subspaceTag, spaceId, queueId } = request;
55
+ throw RuntimeServiceError.wrap({
56
+ message: 'Queue delete failed.',
57
+ context: { subspaceTag, spaceId, queueId },
58
+ ifTypeDiffers: true,
59
+ })(error);
60
+ }
61
+ }
62
+
63
+ async syncQueue(_: FeedProtocol.SyncQueueRequest): Promise<void> {
64
+ // No-op in Cloudflare runtime.
61
65
  }
62
66
  }
@@ -17,7 +17,7 @@ import { QueueServiceImpl } from './queue-service-impl';
17
17
  */
18
18
  export class ServiceContainer {
19
19
  constructor(
20
- private readonly _executionContext: EdgeFunctionEnv.ExecutionContext,
20
+ private readonly _executionContext: EdgeFunctionEnv.TraceContext,
21
21
  private readonly _dataService: EdgeFunctionEnv.DataService,
22
22
  private readonly _queueService: EdgeFunctionEnv.QueueService,
23
23
  private readonly _functionsService: EdgeFunctionEnv.FunctionsAiService,
@@ -54,10 +54,18 @@ export class ServiceContainer {
54
54
  }
55
55
 
56
56
  async queryQueue(queue: DXN): Promise<FeedProtocol.QueryResult> {
57
- const { spaceId } = queue.asQueueDXN() ?? {};
58
- using result = (await this._queueService.query({}, queue.toString(), { spaceId: spaceId! })) as any;
59
- // Copy returned object to avoid hanging RPC stub
60
- // See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
57
+ const parts = queue.asQueueDXN();
58
+ if (!parts) {
59
+ throw new Error('Invalid queue DXN');
60
+ }
61
+ const { subspaceTag, spaceId, queueId } = parts;
62
+ const result = await this._queueService.queryQueue(this._executionContext, {
63
+ query: {
64
+ spaceId,
65
+ queuesNamespace: subspaceTag,
66
+ queueIds: [queueId],
67
+ },
68
+ });
61
69
  return {
62
70
  objects: structuredClone(result.objects),
63
71
  nextCursor: result.nextCursor ?? null,
@@ -66,6 +74,16 @@ export class ServiceContainer {
66
74
  }
67
75
 
68
76
  async insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
69
- await this._queueService.append({}, queue.toString(), objects);
77
+ const parts = queue.asQueueDXN();
78
+ if (!parts) {
79
+ throw new Error('Invalid queue DXN');
80
+ }
81
+ const { subspaceTag, spaceId, queueId } = parts;
82
+ await this._queueService.insertIntoQueue(this._executionContext, {
83
+ subspaceTag,
84
+ spaceId,
85
+ queueId,
86
+ objects: objects as FeedProtocol.InsertIntoQueueRequest['objects'],
87
+ });
70
88
  }
71
89
  }
@@ -61,6 +61,6 @@ export class SpaceProxy extends Resource {
61
61
  owningObject: this,
62
62
  });
63
63
 
64
- await this._db.coreDatabase.open({ rootUrl: meta.rootDocumentId });
64
+ await this._db.coreDatabase.open(this._ctx, { rootUrl: meta.rootDocumentId });
65
65
  }
66
66
  }
@@ -1,12 +0,0 @@
1
- import { type QueryAST } from '@dxos/echo-protocol';
2
- import { type EdgeFunctionEnv } from '@dxos/protocols';
3
- export declare const queryToDataServiceRequest: (query: QueryAST.Query) => EdgeFunctionEnv.QueryRequest;
4
- /**
5
- * Extracts the filter and options from a query.
6
- * Supports Select(...) and Options(Select(...)) queries.
7
- */
8
- export declare const isSimpleSelectionQuery: (query: QueryAST.Query) => {
9
- filter: QueryAST.Filter;
10
- options?: QueryAST.QueryOptions;
11
- } | null;
12
- //# sourceMappingURL=adapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../../../src/internal/adapter.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvD,eAAO,MAAM,yBAAyB,GAAI,OAAO,QAAQ,CAAC,KAAK,KAAG,eAAe,CAAC,YAajF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,GACjC,OAAO,QAAQ,CAAC,KAAK,KACpB;IAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,YAAY,CAAA;CAAE,GAAG,IAgBjE,CAAC"}
@@ -1,48 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { failUndefined } from '@dxos/debug';
6
- import { type QueryAST } from '@dxos/echo-protocol';
7
- import { invariant } from '@dxos/invariant';
8
- import { SpaceId } from '@dxos/keys';
9
- import { type EdgeFunctionEnv } from '@dxos/protocols';
10
-
11
- export const queryToDataServiceRequest = (query: QueryAST.Query): EdgeFunctionEnv.QueryRequest => {
12
- const { filter, options } = isSimpleSelectionQuery(query) ?? failUndefined();
13
- invariant(options?.spaceIds?.length === 1, 'Only one space is supported');
14
- invariant(filter.type === 'object', 'Only object filters are supported');
15
-
16
- const spaceId = options.spaceIds[0];
17
- invariant(SpaceId.isValid(spaceId));
18
-
19
- return {
20
- spaceId,
21
- type: filter.typename ?? undefined,
22
- objectIds: [...(filter.id ?? [])],
23
- };
24
- };
25
-
26
- /**
27
- * Extracts the filter and options from a query.
28
- * Supports Select(...) and Options(Select(...)) queries.
29
- */
30
- export const isSimpleSelectionQuery = (
31
- query: QueryAST.Query,
32
- ): { filter: QueryAST.Filter; options?: QueryAST.QueryOptions } | null => {
33
- switch (query.type) {
34
- case 'options': {
35
- const maybeFilter = isSimpleSelectionQuery(query.query);
36
- if (!maybeFilter) {
37
- return null;
38
- }
39
- return { filter: maybeFilter.filter, options: query.options };
40
- }
41
- case 'select': {
42
- return { filter: query.filter, options: undefined };
43
- }
44
- default: {
45
- return null;
46
- }
47
- }
48
- };