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