@dxos/functions-runtime-cloudflare 0.8.4-main.21d9917
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/LICENSE +8 -0
- package/README.md +47 -0
- package/dist/lib/browser/index.mjs +1137 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +1139 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/functions-client.d.ts +33 -0
- package/dist/types/src/functions-client.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/internal/adapter.d.ts +12 -0
- package/dist/types/src/internal/adapter.d.ts.map +1 -0
- package/dist/types/src/internal/data-service-impl.d.ts +26 -0
- package/dist/types/src/internal/data-service-impl.d.ts.map +1 -0
- package/dist/types/src/internal/index.d.ts +2 -0
- package/dist/types/src/internal/index.d.ts.map +1 -0
- package/dist/types/src/internal/query-service-impl.d.ts +19 -0
- package/dist/types/src/internal/query-service-impl.d.ts.map +1 -0
- package/dist/types/src/internal/queue-service-impl.d.ts +11 -0
- package/dist/types/src/internal/queue-service-impl.d.ts.map +1 -0
- package/dist/types/src/internal/service-container.d.ts +27 -0
- package/dist/types/src/internal/service-container.d.ts.map +1 -0
- package/dist/types/src/internal/utils.d.ts +2 -0
- package/dist/types/src/internal/utils.d.ts.map +1 -0
- package/dist/types/src/logger.d.ts +2 -0
- package/dist/types/src/logger.d.ts.map +1 -0
- package/dist/types/src/queues-api.d.ts +22 -0
- package/dist/types/src/queues-api.d.ts.map +1 -0
- package/dist/types/src/space-proxy.d.ts +26 -0
- package/dist/types/src/space-proxy.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +31 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/wrap-handler-for-cloudflare.d.ts +6 -0
- package/dist/types/src/wrap-handler-for-cloudflare.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +52 -0
- package/src/functions-client.ts +88 -0
- package/src/index.ts +9 -0
- package/src/internal/adapter.ts +48 -0
- package/src/internal/data-service-impl.ts +142 -0
- package/src/internal/index.ts +5 -0
- package/src/internal/query-service-impl.ts +107 -0
- package/src/internal/queue-service-impl.ts +69 -0
- package/src/internal/service-container.ts +73 -0
- package/src/internal/utils.ts +5 -0
- package/src/logger.ts +42 -0
- package/src/queues-api.ts +38 -0
- package/src/space-proxy.ts +66 -0
- package/src/types.ts +40 -0
- package/src/wrap-handler-for-cloudflare.ts +132 -0
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/functions-runtime-cloudflare",
|
|
3
|
+
"version": "0.8.4-main.21d9917",
|
|
4
|
+
"description": "Functions runtime for Cloudflare.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "info@dxos.org",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"source": "./src/index.ts",
|
|
18
|
+
"types": "./dist/types/src/index.d.ts",
|
|
19
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
20
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"types": "dist/types/src/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"schema",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"effect": "3.19.11",
|
|
31
|
+
"@dxos/context": "0.8.4-main.21d9917",
|
|
32
|
+
"@dxos/debug": "0.8.4-main.21d9917",
|
|
33
|
+
"@dxos/codec-protobuf": "0.8.4-main.21d9917",
|
|
34
|
+
"@dxos/echo-db": "0.8.4-main.21d9917",
|
|
35
|
+
"@dxos/echo": "0.8.4-main.21d9917",
|
|
36
|
+
"@dxos/echo-protocol": "0.8.4-main.21d9917",
|
|
37
|
+
"@dxos/errors": "0.8.4-main.21d9917",
|
|
38
|
+
"@dxos/invariant": "0.8.4-main.21d9917",
|
|
39
|
+
"@dxos/keys": "0.8.4-main.21d9917",
|
|
40
|
+
"@dxos/protocols": "0.8.4-main.21d9917",
|
|
41
|
+
"@dxos/log": "0.8.4-main.21d9917",
|
|
42
|
+
"@dxos/util": "0.8.4-main.21d9917"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@cloudflare/workers-types": "^4.20251210.0",
|
|
46
|
+
"@dxos/types": "0.8.4-main.21d9917"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {}
|
|
52
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Resource } from '@dxos/context';
|
|
6
|
+
import { EchoClient } from '@dxos/echo-db';
|
|
7
|
+
import { invariant } from '@dxos/invariant';
|
|
8
|
+
import { type SpaceId } from '@dxos/keys';
|
|
9
|
+
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
10
|
+
|
|
11
|
+
import { ServiceContainer } from './internal';
|
|
12
|
+
import { SpaceProxy } from './space-proxy';
|
|
13
|
+
|
|
14
|
+
type Services = {
|
|
15
|
+
dataService: EdgeFunctionEnv.DataService;
|
|
16
|
+
queueService: EdgeFunctionEnv.QueueService;
|
|
17
|
+
functionsAiService: EdgeFunctionEnv.FunctionsAiService;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* API for functions to integrate with ECHO and HALO.
|
|
22
|
+
* @deprecated
|
|
23
|
+
*/
|
|
24
|
+
export class FunctionsClient extends Resource {
|
|
25
|
+
private readonly _serviceContainer;
|
|
26
|
+
private readonly _echoClient;
|
|
27
|
+
private readonly _executionContext: EdgeFunctionEnv.ExecutionContext = {};
|
|
28
|
+
|
|
29
|
+
private readonly _spaces = new Map<SpaceId, SpaceProxy>();
|
|
30
|
+
|
|
31
|
+
constructor(services: Services) {
|
|
32
|
+
super();
|
|
33
|
+
invariant(typeof services.dataService !== 'undefined', 'DataService is required');
|
|
34
|
+
invariant(typeof services.queueService !== 'undefined', 'QueueService is required');
|
|
35
|
+
this._serviceContainer = new ServiceContainer(
|
|
36
|
+
this._executionContext,
|
|
37
|
+
services.dataService,
|
|
38
|
+
services.queueService,
|
|
39
|
+
services.functionsAiService,
|
|
40
|
+
);
|
|
41
|
+
this._echoClient = new EchoClient({});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get echo(): EchoClient {
|
|
45
|
+
return this._echoClient;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected override async _open() {
|
|
49
|
+
const { dataService, queryService } = await this._serviceContainer.createServices();
|
|
50
|
+
this._echoClient.connectToService({ dataService, queryService });
|
|
51
|
+
await this._echoClient.open();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected override async _close() {
|
|
55
|
+
for (const space of this._spaces.values()) {
|
|
56
|
+
await space.close();
|
|
57
|
+
}
|
|
58
|
+
this._spaces.clear();
|
|
59
|
+
|
|
60
|
+
await this._echoClient.close();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async getSpace(spaceId: SpaceId): Promise<SpaceProxy> {
|
|
64
|
+
if (!this._spaces.has(spaceId)) {
|
|
65
|
+
const space = new SpaceProxy(this._serviceContainer, this._echoClient, spaceId);
|
|
66
|
+
this._spaces.set(spaceId, space);
|
|
67
|
+
}
|
|
68
|
+
const space = this._spaces.get(spaceId)!;
|
|
69
|
+
await space.open(); // No-op if already open.
|
|
70
|
+
return space;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const createClientFromEnv = async (env: any): Promise<FunctionsClient> => {
|
|
75
|
+
const client = new FunctionsClient({
|
|
76
|
+
dataService: env.DATA_SERVICE,
|
|
77
|
+
queueService: env.QUEUE_SERVICE,
|
|
78
|
+
functionsAiService: env.FUNCTIONS_AI_SERVICE,
|
|
79
|
+
});
|
|
80
|
+
await client.open();
|
|
81
|
+
return client;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
- Provides data access capabilities for user functions.
|
|
86
|
+
- No real-time replication or reactive queries -- function receives a snapshot.
|
|
87
|
+
- Function event contains the metadata but doesn't need to include the data.
|
|
88
|
+
*/
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { RequestOptions } from '@dxos/codec-protobuf';
|
|
6
|
+
import { Stream } from '@dxos/codec-protobuf/stream';
|
|
7
|
+
import { raise } from '@dxos/debug';
|
|
8
|
+
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
9
|
+
import { invariant } from '@dxos/invariant';
|
|
10
|
+
import { SpaceId } from '@dxos/keys';
|
|
11
|
+
import { log } from '@dxos/log';
|
|
12
|
+
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
13
|
+
import type {
|
|
14
|
+
BatchedDocumentUpdates,
|
|
15
|
+
CreateDocumentRequest,
|
|
16
|
+
CreateDocumentResponse,
|
|
17
|
+
DataService as DataServiceProto,
|
|
18
|
+
GetDocumentHeadsRequest,
|
|
19
|
+
GetDocumentHeadsResponse,
|
|
20
|
+
GetSpaceSyncStateRequest,
|
|
21
|
+
ReIndexHeadsRequest,
|
|
22
|
+
SpaceSyncState,
|
|
23
|
+
UpdateRequest,
|
|
24
|
+
UpdateSubscriptionRequest,
|
|
25
|
+
} from '@dxos/protocols/proto/dxos/echo/service';
|
|
26
|
+
|
|
27
|
+
import { copyUint8Array } from './utils';
|
|
28
|
+
|
|
29
|
+
export class DataServiceImpl implements DataServiceProto {
|
|
30
|
+
private dataSubscriptions = new Map<string, { spaceId: SpaceId; next: (msg: BatchedDocumentUpdates) => void }>();
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
private _executionContext: EdgeFunctionEnv.ExecutionContext,
|
|
34
|
+
private _dataService: EdgeFunctionEnv.DataService,
|
|
35
|
+
) {}
|
|
36
|
+
|
|
37
|
+
subscribe({ subscriptionId, spaceId }: { subscriptionId: string; spaceId: string }): Stream<BatchedDocumentUpdates> {
|
|
38
|
+
return new Stream(({ next }) => {
|
|
39
|
+
invariant(SpaceId.isValid(spaceId));
|
|
40
|
+
this.dataSubscriptions.set(subscriptionId, { spaceId, next });
|
|
41
|
+
|
|
42
|
+
return () => {
|
|
43
|
+
this.dataSubscriptions.delete(subscriptionId);
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async updateSubscription({ subscriptionId, addIds }: UpdateSubscriptionRequest): Promise<void> {
|
|
49
|
+
const sub =
|
|
50
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
51
|
+
raise(
|
|
52
|
+
new RuntimeServiceError({
|
|
53
|
+
message: 'Subscription not found.',
|
|
54
|
+
context: { subscriptionId },
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (addIds) {
|
|
59
|
+
log.info('request documents', { count: addIds.length });
|
|
60
|
+
// TODO(dmaretskyi): Batch.
|
|
61
|
+
for (const documentId of addIds) {
|
|
62
|
+
using document = await this._dataService.getDocument(this._executionContext, sub.spaceId, documentId);
|
|
63
|
+
log.info('document loaded', { documentId, spaceId: sub.spaceId, found: !!document });
|
|
64
|
+
if (!document) {
|
|
65
|
+
log.warn('not found', { documentId });
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
sub.next({
|
|
69
|
+
updates: [
|
|
70
|
+
{
|
|
71
|
+
documentId,
|
|
72
|
+
// Copy returned object to avoid hanging RPC stub
|
|
73
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
74
|
+
mutation: copyUint8Array(document.data),
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async createDocument({ spaceId, initialValue }: CreateDocumentRequest): Promise<CreateDocumentResponse> {
|
|
83
|
+
using response = await this._dataService.createDocument(this._executionContext, { spaceId, initialValue });
|
|
84
|
+
return { documentId: response.documentId };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async update({ updates, subscriptionId }: UpdateRequest): Promise<void> {
|
|
88
|
+
const sub =
|
|
89
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
90
|
+
raise(
|
|
91
|
+
new RuntimeServiceError({
|
|
92
|
+
message: 'Subscription not found.',
|
|
93
|
+
context: { subscriptionId },
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
// TODO(dmaretskyi): Batch.
|
|
97
|
+
try {
|
|
98
|
+
for (const update of updates ?? []) {
|
|
99
|
+
await this._dataService.changeDocument(this._executionContext, sub.spaceId, update.documentId, update.mutation);
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw RuntimeServiceError.wrap({
|
|
103
|
+
message: 'Failed to apply document updates.',
|
|
104
|
+
context: { subscriptionId },
|
|
105
|
+
ifTypeDiffers: true,
|
|
106
|
+
})(error);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async flush(): Promise<void> {
|
|
111
|
+
// No-op.
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
subscribeSpaceSyncState(_request: GetSpaceSyncStateRequest, _options?: RequestOptions): Stream<SpaceSyncState> {
|
|
115
|
+
throw new NotImplementedError({
|
|
116
|
+
message: 'subscribeSpaceSyncState is not implemented.',
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async getDocumentHeads({ documentIds: _documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
121
|
+
throw new NotImplementedError({
|
|
122
|
+
message: 'getDocumentHeads is not implemented.',
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async reIndexHeads({ documentIds: _documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
127
|
+
throw new NotImplementedError({
|
|
128
|
+
message: 'reIndexHeads is not implemented.',
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async updateIndexes(): Promise<void> {
|
|
133
|
+
log.error('updateIndexes is not available in EDGE env.');
|
|
134
|
+
// No-op.
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async waitUntilHeadsReplicated({ heads: _heads }: { heads: any }): Promise<void> {
|
|
138
|
+
throw new NotImplementedError({
|
|
139
|
+
message: 'waitUntilHeadsReplicated is not implemented.',
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import { Stream } from '@dxos/codec-protobuf/stream';
|
|
8
|
+
import { QueryAST } from '@dxos/echo-protocol';
|
|
9
|
+
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
|
+
import { log } from '@dxos/log';
|
|
14
|
+
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';
|
|
24
|
+
|
|
25
|
+
export class QueryServiceImpl implements QueryServiceProto {
|
|
26
|
+
private _queryCount = 0;
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
private readonly _executionContext: EdgeFunctionEnv.ExecutionContext,
|
|
30
|
+
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
execQuery(request: QueryRequest): Stream<QueryResponse> {
|
|
34
|
+
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
|
+
|
|
40
|
+
return Stream.fromPromise<QueryResponse>(
|
|
41
|
+
(async () => {
|
|
42
|
+
try {
|
|
43
|
+
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;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
log.error('query failed', { err: error });
|
|
67
|
+
throw new RuntimeServiceError({
|
|
68
|
+
message: `Query execution failed (queryCount=${this._queryCount})`,
|
|
69
|
+
context: { spaceId, filter: request.filter, queryCount: this._queryCount },
|
|
70
|
+
cause: error,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
})(),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async reindex() {
|
|
78
|
+
throw new NotImplementedError({
|
|
79
|
+
message: 'Reindex is not implemented.',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async setConfig() {
|
|
84
|
+
throw new NotImplementedError({
|
|
85
|
+
message: 'SetConfig is not implemented.',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
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
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
6
|
+
import { invariant } from '@dxos/invariant';
|
|
7
|
+
import { type QueueService as QueueServiceProto } from '@dxos/protocols';
|
|
8
|
+
import type {
|
|
9
|
+
DeleteFromQueueRequest,
|
|
10
|
+
EdgeFunctionEnv,
|
|
11
|
+
InsertIntoQueueRequest,
|
|
12
|
+
QueryQueueRequest,
|
|
13
|
+
QueryResult,
|
|
14
|
+
} from '@dxos/protocols';
|
|
15
|
+
|
|
16
|
+
export class QueueServiceImpl implements QueueServiceProto {
|
|
17
|
+
constructor(
|
|
18
|
+
protected _ctx: EdgeFunctionEnv.ExecutionContext,
|
|
19
|
+
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
20
|
+
) {}
|
|
21
|
+
async queryQueue(request: QueryQueueRequest): Promise<QueryResult> {
|
|
22
|
+
const { query } = request;
|
|
23
|
+
const { queueIds, ...filter } = query!;
|
|
24
|
+
const spaceId = query!.spaceId;
|
|
25
|
+
const queueId = queueIds?.[0];
|
|
26
|
+
invariant(request.query.queuesNamespace);
|
|
27
|
+
try {
|
|
28
|
+
using result = await this._queueService.query(
|
|
29
|
+
this._ctx,
|
|
30
|
+
`dxn:queue:${request.query.queuesNamespace}:${spaceId}:${queueId}`,
|
|
31
|
+
filter,
|
|
32
|
+
);
|
|
33
|
+
return {
|
|
34
|
+
// Copy returned object to avoid hanging RPC stub
|
|
35
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
36
|
+
objects: structuredClone(result.objects),
|
|
37
|
+
nextCursor: result.nextCursor,
|
|
38
|
+
prevCursor: result.prevCursor,
|
|
39
|
+
};
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw RuntimeServiceError.wrap({
|
|
42
|
+
message: 'Queue query failed.',
|
|
43
|
+
context: { subspaceTag: request.query.queuesNamespace, spaceId, queueId },
|
|
44
|
+
ifTypeDiffers: true,
|
|
45
|
+
})(error);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async insertIntoQueue(request: InsertIntoQueueRequest): Promise<void> {
|
|
50
|
+
const { subspaceTag, spaceId, queueId, objects } = request;
|
|
51
|
+
try {
|
|
52
|
+
await this._queueService.append(this._ctx, `dxn:queue:${subspaceTag}:${spaceId}:${queueId}`, objects ?? []);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw RuntimeServiceError.wrap({
|
|
55
|
+
message: 'Queue append failed.',
|
|
56
|
+
context: { subspaceTag, spaceId, queueId },
|
|
57
|
+
ifTypeDiffers: true,
|
|
58
|
+
})(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
deleteFromQueue(request: DeleteFromQueueRequest): Promise<void> {
|
|
63
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
64
|
+
throw new NotImplementedError({
|
|
65
|
+
message: 'Deleting from queue is not supported.',
|
|
66
|
+
context: { subspaceTag, spaceId, queueId },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type AnyEntity } from '@dxos/echo/internal';
|
|
6
|
+
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
7
|
+
import type { QueryResult } from '@dxos/protocols';
|
|
8
|
+
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
9
|
+
import { type QueueService as QueueServiceProto } from '@dxos/protocols';
|
|
10
|
+
import { type QueryService as QueryServiceProto } from '@dxos/protocols/proto/dxos/echo/query';
|
|
11
|
+
import type { DataService as DataServiceProto } from '@dxos/protocols/proto/dxos/echo/service';
|
|
12
|
+
|
|
13
|
+
import { DataServiceImpl } from './data-service-impl';
|
|
14
|
+
import { QueryServiceImpl } from './query-service-impl';
|
|
15
|
+
import { QueueServiceImpl } from './queue-service-impl';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* TODO: make this implement DataService and QueryService to unify API over edge and web backend
|
|
19
|
+
*/
|
|
20
|
+
export class ServiceContainer {
|
|
21
|
+
constructor(
|
|
22
|
+
private readonly _executionContext: EdgeFunctionEnv.ExecutionContext,
|
|
23
|
+
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
24
|
+
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
25
|
+
private readonly _functionsService: EdgeFunctionEnv.FunctionsAiService,
|
|
26
|
+
) {}
|
|
27
|
+
|
|
28
|
+
async getSpaceMeta(spaceId: SpaceId): Promise<EdgeFunctionEnv.SpaceMeta | undefined> {
|
|
29
|
+
using result = await this._dataService.getSpaceMeta(this._executionContext, spaceId);
|
|
30
|
+
// Copy returned object to avoid hanging RPC stub
|
|
31
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
32
|
+
return result
|
|
33
|
+
? {
|
|
34
|
+
spaceKey: result.spaceKey,
|
|
35
|
+
rootDocumentId: result.rootDocumentId,
|
|
36
|
+
}
|
|
37
|
+
: undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async createServices(): Promise<{
|
|
41
|
+
dataService: DataServiceProto;
|
|
42
|
+
queryService: QueryServiceProto;
|
|
43
|
+
queueService: QueueServiceProto;
|
|
44
|
+
functionsAiService: EdgeFunctionEnv.FunctionsAiService;
|
|
45
|
+
}> {
|
|
46
|
+
const dataService = new DataServiceImpl(this._executionContext, this._dataService);
|
|
47
|
+
const queryService = new QueryServiceImpl(this._executionContext, this._dataService);
|
|
48
|
+
const queueService = new QueueServiceImpl(this._executionContext, this._queueService);
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
dataService,
|
|
52
|
+
queryService,
|
|
53
|
+
queueService,
|
|
54
|
+
functionsAiService: this._functionsService,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async queryQueue(queue: DXN): Promise<QueryResult> {
|
|
59
|
+
const { spaceId } = queue.asQueueDXN() ?? {};
|
|
60
|
+
using result = (await this._queueService.query({}, queue.toString(), { spaceId: spaceId! })) as any;
|
|
61
|
+
// Copy returned object to avoid hanging RPC stub
|
|
62
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
63
|
+
return {
|
|
64
|
+
objects: structuredClone(result.objects),
|
|
65
|
+
nextCursor: result.nextCursor ?? null,
|
|
66
|
+
prevCursor: result.prevCursor ?? null,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
71
|
+
await this._queueService.append({}, queue.toString(), objects);
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-console */
|
|
6
|
+
|
|
7
|
+
import { LogLevel, type LogProcessor, log, shouldLog } from '@dxos/log';
|
|
8
|
+
|
|
9
|
+
export const setupFunctionsLogger = () => {
|
|
10
|
+
log.runtimeConfig.processors.length = 0;
|
|
11
|
+
log.runtimeConfig.processors.push(functionLogProcessor);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const functionLogProcessor: LogProcessor = (config, entry) => {
|
|
15
|
+
if (!shouldLog(entry, config.filters)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
switch (entry.level) {
|
|
20
|
+
case LogLevel.DEBUG:
|
|
21
|
+
console.debug(entry.message, entry.context);
|
|
22
|
+
break;
|
|
23
|
+
case LogLevel.TRACE:
|
|
24
|
+
console.debug(entry.message, entry.context);
|
|
25
|
+
break;
|
|
26
|
+
case LogLevel.VERBOSE:
|
|
27
|
+
console.log(entry.message, entry.context);
|
|
28
|
+
break;
|
|
29
|
+
case LogLevel.INFO:
|
|
30
|
+
console.info(entry.message, entry.context);
|
|
31
|
+
break;
|
|
32
|
+
case LogLevel.WARN:
|
|
33
|
+
console.warn(entry.message, entry.context);
|
|
34
|
+
break;
|
|
35
|
+
case LogLevel.ERROR:
|
|
36
|
+
console.error(entry.message, entry.context);
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
console.log(entry.message, entry.context);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type AnyEntity } from '@dxos/echo/internal';
|
|
6
|
+
import type { DXN, SpaceId } from '@dxos/keys';
|
|
7
|
+
import type { QueryResult } from '@dxos/protocols';
|
|
8
|
+
|
|
9
|
+
import type { ServiceContainer } from './internal';
|
|
10
|
+
|
|
11
|
+
// TODO(dmaretskyi): Temporary API to get the queues working.
|
|
12
|
+
// TODO(dmaretskyi): To be replaced with integrating queues into echo.
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated
|
|
15
|
+
*/
|
|
16
|
+
export interface QueuesAPI {
|
|
17
|
+
queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
|
|
18
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated
|
|
23
|
+
*/
|
|
24
|
+
export class QueuesAPIImpl implements QueuesAPI {
|
|
25
|
+
constructor(
|
|
26
|
+
private readonly _serviceContainer: ServiceContainer,
|
|
27
|
+
private readonly _spaceId: SpaceId,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
queryQueue(queue: DXN, options?: {}): Promise<QueryResult> {
|
|
31
|
+
return this._serviceContainer.queryQueue(queue);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
35
|
+
// TODO(dmaretskyi): Ugly.
|
|
36
|
+
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
37
|
+
}
|
|
38
|
+
}
|