@dxos/functions-runtime-cloudflare 0.8.4-main.66e292d → 0.8.4-main.6fa680abb7
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/dist/lib/browser/index.mjs +774 -213
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +774 -213
- 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 -0
- package/dist/types/src/functions-client.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +4 -1
- 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 +8 -8
- 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 +6 -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.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -15
- package/src/functions-client.ts +9 -2
- package/src/index.ts +4 -1
- package/src/internal/data-service-impl.ts +67 -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 +46 -11
- package/src/internal/utils.ts +5 -0
- package/src/logger.ts +42 -0
- package/src/queues-api.ts +6 -6
- package/src/space-proxy.ts +4 -3
- package/src/wrap-handler-for-cloudflare.ts +3 -3
- 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,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions-runtime-cloudflare",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.6fa680abb7",
|
|
4
4
|
"description": "Functions runtime for Cloudflare.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"sideEffects": false,
|
|
@@ -23,22 +27,23 @@
|
|
|
23
27
|
"src"
|
|
24
28
|
],
|
|
25
29
|
"dependencies": {
|
|
26
|
-
"effect": "3.
|
|
27
|
-
"@dxos/codec-protobuf": "0.8.4-main.
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/debug": "0.8.4-main.
|
|
30
|
-
"@dxos/echo": "0.8.4-main.
|
|
31
|
-
"@dxos/echo-protocol": "0.8.4-main.
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/invariant": "0.8.4-main.
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/protocols": "0.8.4-main.
|
|
30
|
+
"effect": "3.19.16",
|
|
31
|
+
"@dxos/codec-protobuf": "0.8.4-main.6fa680abb7",
|
|
32
|
+
"@dxos/echo-db": "0.8.4-main.6fa680abb7",
|
|
33
|
+
"@dxos/debug": "0.8.4-main.6fa680abb7",
|
|
34
|
+
"@dxos/echo": "0.8.4-main.6fa680abb7",
|
|
35
|
+
"@dxos/echo-protocol": "0.8.4-main.6fa680abb7",
|
|
36
|
+
"@dxos/errors": "0.8.4-main.6fa680abb7",
|
|
37
|
+
"@dxos/invariant": "0.8.4-main.6fa680abb7",
|
|
38
|
+
"@dxos/context": "0.8.4-main.6fa680abb7",
|
|
39
|
+
"@dxos/keys": "0.8.4-main.6fa680abb7",
|
|
40
|
+
"@dxos/log": "0.8.4-main.6fa680abb7",
|
|
41
|
+
"@dxos/protocols": "0.8.4-main.6fa680abb7",
|
|
42
|
+
"@dxos/util": "0.8.4-main.6fa680abb7"
|
|
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.6fa680abb7"
|
|
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,6 +75,7 @@ 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;
|
package/src/index.ts
CHANGED
|
@@ -2,5 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export * from './functions-client';
|
|
6
|
+
export * from './internal';
|
|
6
7
|
export * from './types';
|
|
8
|
+
export * from './wrap-handler-for-cloudflare';
|
|
9
|
+
export * from './logger';
|
|
@@ -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,98 @@ 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
|
+
using response = await this._dataService.createDocument(this._executionContext, { spaceId, initialValue });
|
|
84
|
+
return { documentId: response.documentId };
|
|
85
|
+
}
|
|
86
|
+
|
|
61
87
|
async update({ updates, subscriptionId }: UpdateRequest): Promise<void> {
|
|
62
|
-
const sub =
|
|
88
|
+
const sub =
|
|
89
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
90
|
+
raise(
|
|
91
|
+
new RuntimeServiceError({
|
|
92
|
+
message: 'Subscription not found.',
|
|
93
|
+
context: { subscriptionId },
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
63
96
|
// TODO(dmaretskyi): Batch.
|
|
64
|
-
|
|
65
|
-
|
|
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);
|
|
66
107
|
}
|
|
67
|
-
throw new Error('Method not implemented.');
|
|
68
108
|
}
|
|
69
109
|
|
|
70
110
|
async flush(): Promise<void> {
|
|
71
111
|
// No-op.
|
|
72
112
|
}
|
|
73
113
|
|
|
74
|
-
subscribeSpaceSyncState(
|
|
75
|
-
throw new
|
|
114
|
+
subscribeSpaceSyncState(_request: GetSpaceSyncStateRequest, _options?: RequestOptions): Stream<SpaceSyncState> {
|
|
115
|
+
throw new NotImplementedError({
|
|
116
|
+
message: 'subscribeSpaceSyncState is not implemented.',
|
|
117
|
+
});
|
|
76
118
|
}
|
|
77
119
|
|
|
78
|
-
async getDocumentHeads({ documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
79
|
-
throw new
|
|
120
|
+
async getDocumentHeads({ documentIds: _documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
121
|
+
throw new NotImplementedError({
|
|
122
|
+
message: 'getDocumentHeads is not implemented.',
|
|
123
|
+
});
|
|
80
124
|
}
|
|
81
125
|
|
|
82
|
-
async reIndexHeads({ documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
83
|
-
throw new
|
|
126
|
+
async reIndexHeads({ documentIds: _documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
127
|
+
throw new NotImplementedError({
|
|
128
|
+
message: 'reIndexHeads is not implemented.',
|
|
129
|
+
});
|
|
84
130
|
}
|
|
85
131
|
|
|
86
132
|
async updateIndexes(): Promise<void> {
|
|
87
|
-
|
|
133
|
+
log.error('updateIndexes is not available in EDGE env.');
|
|
134
|
+
// No-op.
|
|
88
135
|
}
|
|
89
136
|
|
|
90
|
-
async waitUntilHeadsReplicated({ heads }: { heads: any }): Promise<void> {
|
|
91
|
-
throw new
|
|
137
|
+
async waitUntilHeadsReplicated({ heads: _heads }: { heads: any }): Promise<void> {
|
|
138
|
+
throw new NotImplementedError({
|
|
139
|
+
message: 'waitUntilHeadsReplicated is not implemented.',
|
|
140
|
+
});
|
|
92
141
|
}
|
|
93
142
|
}
|
|
@@ -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,11 +2,9 @@
|
|
|
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
9
|
import type { DataService as DataServiceProto } from '@dxos/protocols/proto/dxos/echo/service';
|
|
12
10
|
|
|
@@ -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,41 @@ 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
|
+
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
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
objects: structuredClone(result.objects),
|
|
71
|
+
nextCursor: result.nextCursor ?? null,
|
|
72
|
+
prevCursor: result.prevCursor ?? null,
|
|
73
|
+
};
|
|
49
74
|
}
|
|
50
75
|
|
|
51
|
-
insertIntoQueue(queue: DXN, objects:
|
|
52
|
-
|
|
76
|
+
async insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
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
|
+
});
|
|
53
88
|
}
|
|
54
89
|
}
|
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
|
+
};
|
package/src/queues-api.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
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 type
|
|
7
|
+
import { type FeedProtocol } from '@dxos/protocols';
|
|
8
8
|
|
|
9
9
|
import type { ServiceContainer } from './internal';
|
|
10
10
|
|
|
@@ -14,8 +14,8 @@ import type { ServiceContainer } from './internal';
|
|
|
14
14
|
* @deprecated
|
|
15
15
|
*/
|
|
16
16
|
export interface QueuesAPI {
|
|
17
|
-
queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
|
|
18
|
-
insertIntoQueue(queue: DXN, objects:
|
|
17
|
+
queryQueue(queue: DXN, options?: {}): Promise<FeedProtocol.QueryResult>;
|
|
18
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void>;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -27,11 +27,11 @@ export class QueuesAPIImpl implements QueuesAPI {
|
|
|
27
27
|
private readonly _spaceId: SpaceId,
|
|
28
28
|
) {}
|
|
29
29
|
|
|
30
|
-
queryQueue(queue: DXN, options?: {}): Promise<QueryResult> {
|
|
30
|
+
queryQueue(queue: DXN, options?: {}): Promise<FeedProtocol.QueryResult> {
|
|
31
31
|
return this._serviceContainer.queryQueue(queue);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
insertIntoQueue(queue: DXN, objects:
|
|
34
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
35
35
|
// TODO(dmaretskyi): Ugly.
|
|
36
36
|
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
37
37
|
}
|
package/src/space-proxy.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Resource } from '@dxos/context';
|
|
6
|
-
import { type
|
|
6
|
+
import { type Database } from '@dxos/echo';
|
|
7
|
+
import { type CoreDatabase, type EchoClient, type EchoDatabaseImpl } from '@dxos/echo-db';
|
|
7
8
|
import { invariant } from '@dxos/invariant';
|
|
8
9
|
import { PublicKey, type SpaceId } from '@dxos/keys';
|
|
9
10
|
|
|
@@ -14,7 +15,7 @@ import { type QueuesAPI, QueuesAPIImpl } from './queues-api';
|
|
|
14
15
|
* @deprecated
|
|
15
16
|
*/
|
|
16
17
|
export class SpaceProxy extends Resource {
|
|
17
|
-
private _db?:
|
|
18
|
+
private _db?: EchoDatabaseImpl = undefined;
|
|
18
19
|
private _queuesApi: QueuesAPIImpl;
|
|
19
20
|
|
|
20
21
|
constructor(
|
|
@@ -30,7 +31,7 @@ export class SpaceProxy extends Resource {
|
|
|
30
31
|
return this._id;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
get db():
|
|
34
|
+
get db(): Database.Database {
|
|
34
35
|
invariant(this._db);
|
|
35
36
|
return this._db;
|
|
36
37
|
}
|