@dxos/functions-runtime-cloudflare 0.8.4-main.03d5cd7b56
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 +967 -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 +969 -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/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 +13 -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 +25 -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 +26 -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 +12 -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/data-service-impl.ts +143 -0
- package/src/internal/index.ts +5 -0
- package/src/internal/query-service-impl.ts +53 -0
- package/src/internal/queue-service-impl.ts +66 -0
- package/src/internal/service-container.ts +89 -0
- package/src/internal/utils.ts +5 -0
- package/src/logger.ts +44 -0
- package/src/queues-api.ts +57 -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.03d5cd7b56",
|
|
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.20.0",
|
|
31
|
+
"@dxos/codec-protobuf": "0.8.4-main.03d5cd7b56",
|
|
32
|
+
"@dxos/context": "0.8.4-main.03d5cd7b56",
|
|
33
|
+
"@dxos/echo": "0.8.4-main.03d5cd7b56",
|
|
34
|
+
"@dxos/debug": "0.8.4-main.03d5cd7b56",
|
|
35
|
+
"@dxos/echo-db": "0.8.4-main.03d5cd7b56",
|
|
36
|
+
"@dxos/invariant": "0.8.4-main.03d5cd7b56",
|
|
37
|
+
"@dxos/echo-protocol": "0.8.4-main.03d5cd7b56",
|
|
38
|
+
"@dxos/log": "0.8.4-main.03d5cd7b56",
|
|
39
|
+
"@dxos/keys": "0.8.4-main.03d5cd7b56",
|
|
40
|
+
"@dxos/protocols": "0.8.4-main.03d5cd7b56",
|
|
41
|
+
"@dxos/errors": "0.8.4-main.03d5cd7b56",
|
|
42
|
+
"@dxos/util": "0.8.4-main.03d5cd7b56"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@cloudflare/workers-types": "^4.20260302.0",
|
|
46
|
+
"@dxos/types": "0.8.4-main.03d5cd7b56"
|
|
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.TraceContext = {};
|
|
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,143 @@
|
|
|
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.TraceContext,
|
|
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
|
+
invariant(SpaceId.isValid(spaceId));
|
|
84
|
+
using response = await this._dataService.createDocument(this._executionContext, spaceId, initialValue);
|
|
85
|
+
return { documentId: response.documentId };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async update({ updates, subscriptionId }: UpdateRequest): Promise<void> {
|
|
89
|
+
const sub =
|
|
90
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
91
|
+
raise(
|
|
92
|
+
new RuntimeServiceError({
|
|
93
|
+
message: 'Subscription not found.',
|
|
94
|
+
context: { subscriptionId },
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
// TODO(dmaretskyi): Batch.
|
|
98
|
+
try {
|
|
99
|
+
for (const update of updates ?? []) {
|
|
100
|
+
await this._dataService.changeDocument(this._executionContext, sub.spaceId, update.documentId, update.mutation);
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
throw RuntimeServiceError.wrap({
|
|
104
|
+
message: 'Failed to apply document updates.',
|
|
105
|
+
context: { subscriptionId },
|
|
106
|
+
ifTypeDiffers: true,
|
|
107
|
+
})(error);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async flush(): Promise<void> {
|
|
112
|
+
// No-op.
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
subscribeSpaceSyncState(_request: GetSpaceSyncStateRequest, _options?: RequestOptions): Stream<SpaceSyncState> {
|
|
116
|
+
throw new NotImplementedError({
|
|
117
|
+
message: 'subscribeSpaceSyncState is not implemented.',
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async getDocumentHeads({ documentIds: _documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
122
|
+
throw new NotImplementedError({
|
|
123
|
+
message: 'getDocumentHeads is not implemented.',
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async reIndexHeads({ documentIds: _documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
128
|
+
throw new NotImplementedError({
|
|
129
|
+
message: 'reIndexHeads is not implemented.',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async updateIndexes(): Promise<void> {
|
|
134
|
+
log.error('updateIndexes is not available in EDGE env.');
|
|
135
|
+
// No-op.
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async waitUntilHeadsReplicated({ heads: _heads }: { heads: any }): Promise<void> {
|
|
139
|
+
throw new NotImplementedError({
|
|
140
|
+
message: 'waitUntilHeadsReplicated is not implemented.',
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Stream } from '@dxos/codec-protobuf/stream';
|
|
6
|
+
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
7
|
+
import { log } from '@dxos/log';
|
|
8
|
+
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
9
|
+
import { type QueryRequest, type QueryResponse, type QueryService } from '@dxos/protocols/proto/dxos/echo/query';
|
|
10
|
+
|
|
11
|
+
export class QueryServiceImpl implements QueryService {
|
|
12
|
+
private _queryCount = 0;
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext,
|
|
16
|
+
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
execQuery(request: QueryRequest): Stream<QueryResponse> {
|
|
20
|
+
log.info('execQuery', { request });
|
|
21
|
+
|
|
22
|
+
return Stream.fromPromise<QueryResponse>(
|
|
23
|
+
(async () => {
|
|
24
|
+
try {
|
|
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
|
+
});
|
|
37
|
+
}
|
|
38
|
+
})(),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async reindex() {
|
|
43
|
+
throw new NotImplementedError({
|
|
44
|
+
message: 'Reindex is not implemented.',
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async setConfig() {
|
|
49
|
+
throw new NotImplementedError({
|
|
50
|
+
message: 'SetConfig is not implemented.',
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { RuntimeServiceError } from '@dxos/errors';
|
|
6
|
+
import { type EdgeFunctionEnv, type FeedProtocol } from '@dxos/protocols';
|
|
7
|
+
|
|
8
|
+
export class QueueServiceImpl implements FeedProtocol.QueueService {
|
|
9
|
+
constructor(
|
|
10
|
+
protected _ctx: EdgeFunctionEnv.TraceContext,
|
|
11
|
+
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
12
|
+
) {}
|
|
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
|
+
}
|
|
35
|
+
}
|
|
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
|
+
}
|
|
48
|
+
}
|
|
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.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
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 EdgeFunctionEnv, type FeedProtocol } from '@dxos/protocols';
|
|
8
|
+
import { type QueryService as QueryServiceProto } from '@dxos/protocols/proto/dxos/echo/query';
|
|
9
|
+
import type { DataService as DataServiceProto } from '@dxos/protocols/proto/dxos/echo/service';
|
|
10
|
+
|
|
11
|
+
import { DataServiceImpl } from './data-service-impl';
|
|
12
|
+
import { QueryServiceImpl } from './query-service-impl';
|
|
13
|
+
import { QueueServiceImpl } from './queue-service-impl';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* TODO: make this implement DataService and QueryService to unify API over edge and web backend
|
|
17
|
+
*/
|
|
18
|
+
export class ServiceContainer {
|
|
19
|
+
constructor(
|
|
20
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext,
|
|
21
|
+
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
22
|
+
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
23
|
+
private readonly _functionsService: EdgeFunctionEnv.FunctionsAiService,
|
|
24
|
+
) {}
|
|
25
|
+
|
|
26
|
+
async getSpaceMeta(spaceId: SpaceId): Promise<EdgeFunctionEnv.SpaceMeta | undefined> {
|
|
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;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async createServices(): Promise<{
|
|
39
|
+
dataService: DataServiceProto;
|
|
40
|
+
queryService: QueryServiceProto;
|
|
41
|
+
queueService: FeedProtocol.QueueService;
|
|
42
|
+
functionsAiService: EdgeFunctionEnv.FunctionsAiService;
|
|
43
|
+
}> {
|
|
44
|
+
const dataService = new DataServiceImpl(this._executionContext, this._dataService);
|
|
45
|
+
const queryService = new QueryServiceImpl(this._executionContext, this._dataService);
|
|
46
|
+
const queueService = new QueueServiceImpl(this._executionContext, this._queueService);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
dataService,
|
|
50
|
+
queryService,
|
|
51
|
+
queueService,
|
|
52
|
+
functionsAiService: this._functionsService,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
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
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
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.map((obj) => JSON.stringify(obj)),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
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
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
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 { log } from '@dxos/log';
|
|
8
|
+
|
|
9
|
+
import type { ServiceContainer } from './internal';
|
|
10
|
+
|
|
11
|
+
export interface QueuesQueryResult {
|
|
12
|
+
objects: AnyEntity[];
|
|
13
|
+
nextCursor: string | null;
|
|
14
|
+
prevCursor: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// TODO(dmaretskyi): Temporary API to get the queues working.
|
|
18
|
+
// TODO(dmaretskyi): To be replaced with integrating queues into echo.
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated
|
|
21
|
+
*/
|
|
22
|
+
export interface QueuesAPI {
|
|
23
|
+
queryQueue(queue: DXN, options?: {}): Promise<QueuesQueryResult>;
|
|
24
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated
|
|
29
|
+
*/
|
|
30
|
+
export class QueuesAPIImpl implements QueuesAPI {
|
|
31
|
+
constructor(
|
|
32
|
+
private readonly _serviceContainer: ServiceContainer,
|
|
33
|
+
private readonly _spaceId: SpaceId,
|
|
34
|
+
) {}
|
|
35
|
+
|
|
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
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
54
|
+
// TODO(dmaretskyi): Ugly.
|
|
55
|
+
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Resource } from '@dxos/context';
|
|
6
|
+
import { type Database } from '@dxos/echo';
|
|
7
|
+
import { type CoreDatabase, type EchoClient, type EchoDatabaseImpl } from '@dxos/echo-db';
|
|
8
|
+
import { invariant } from '@dxos/invariant';
|
|
9
|
+
import { PublicKey, type SpaceId } from '@dxos/keys';
|
|
10
|
+
|
|
11
|
+
import type { ServiceContainer } from './internal';
|
|
12
|
+
import { type QueuesAPI, QueuesAPIImpl } from './queues-api';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated
|
|
16
|
+
*/
|
|
17
|
+
export class SpaceProxy extends Resource {
|
|
18
|
+
private _db?: EchoDatabaseImpl = undefined;
|
|
19
|
+
private _queuesApi: QueuesAPIImpl;
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
private readonly _serviceContainer: ServiceContainer,
|
|
23
|
+
private readonly _echoClient: EchoClient,
|
|
24
|
+
private readonly _id: SpaceId,
|
|
25
|
+
) {
|
|
26
|
+
super();
|
|
27
|
+
this._queuesApi = new QueuesAPIImpl(this._serviceContainer, this._id);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get id(): SpaceId {
|
|
31
|
+
return this._id;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get db(): Database.Database {
|
|
35
|
+
invariant(this._db);
|
|
36
|
+
return this._db;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated Use db API.
|
|
41
|
+
*/
|
|
42
|
+
get crud(): CoreDatabase {
|
|
43
|
+
invariant(this._db);
|
|
44
|
+
return this._db.coreDatabase;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get queues(): QueuesAPI {
|
|
48
|
+
return this._queuesApi;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected override async _open() {
|
|
52
|
+
const meta = await this._serviceContainer.getSpaceMeta(this._id);
|
|
53
|
+
if (!meta) {
|
|
54
|
+
throw new Error(`Space not found: ${this._id}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this._db = this._echoClient.constructDatabase({
|
|
58
|
+
spaceId: this._id,
|
|
59
|
+
spaceKey: PublicKey.from(meta.spaceKey),
|
|
60
|
+
reactiveSchemaQuery: false,
|
|
61
|
+
owningObject: this,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
await this._db.coreDatabase.open(this._ctx, { rootUrl: meta.rootDocumentId });
|
|
65
|
+
}
|
|
66
|
+
}
|