@dxos/functions-runtime-cloudflare 0.8.4-main.3c1ae3b
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 +752 -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 +754 -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 +32 -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 +25 -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 +12 -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/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 +25 -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 +48 -0
- package/src/functions-client.ts +81 -0
- package/src/index.ts +9 -0
- package/src/internal/adapter.ts +48 -0
- package/src/internal/data-service-impl.ts +125 -0
- package/src/internal/index.ts +5 -0
- package/src/internal/query-service-impl.ts +103 -0
- package/src/internal/queue-service-impl.ts +51 -0
- package/src/internal/service-container.ts +54 -0
- package/src/logger.ts +40 -0
- package/src/queues-api.ts +38 -0
- package/src/space-proxy.ts +65 -0
- package/src/types.ts +40 -0
- package/src/wrap-handler-for-cloudflare.ts +132 -0
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* API for functions to integrate with ECHO and HALO.
|
|
21
|
+
* @deprecated
|
|
22
|
+
*/
|
|
23
|
+
export class FunctionsClient extends Resource {
|
|
24
|
+
private readonly _serviceContainer;
|
|
25
|
+
private readonly _echoClient;
|
|
26
|
+
private readonly _executionContext: EdgeFunctionEnv.ExecutionContext = {};
|
|
27
|
+
|
|
28
|
+
private readonly _spaces = new Map<SpaceId, SpaceProxy>();
|
|
29
|
+
|
|
30
|
+
constructor(services: Services) {
|
|
31
|
+
super();
|
|
32
|
+
invariant(typeof services.dataService !== 'undefined', 'DataService is required');
|
|
33
|
+
invariant(typeof services.queueService !== 'undefined', 'QueueService is required');
|
|
34
|
+
this._serviceContainer = new ServiceContainer(this._executionContext, services.dataService, services.queueService);
|
|
35
|
+
this._echoClient = new EchoClient({});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get echo(): EchoClient {
|
|
39
|
+
return this._echoClient;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected override async _open() {
|
|
43
|
+
const { dataService, queryService } = await this._serviceContainer.createServices();
|
|
44
|
+
this._echoClient.connectToService({ dataService, queryService });
|
|
45
|
+
await this._echoClient.open();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected override async _close() {
|
|
49
|
+
for (const space of this._spaces.values()) {
|
|
50
|
+
await space.close();
|
|
51
|
+
}
|
|
52
|
+
this._spaces.clear();
|
|
53
|
+
|
|
54
|
+
await this._echoClient.close();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async getSpace(spaceId: SpaceId): Promise<SpaceProxy> {
|
|
58
|
+
if (!this._spaces.has(spaceId)) {
|
|
59
|
+
const space = new SpaceProxy(this._serviceContainer, this._echoClient, spaceId);
|
|
60
|
+
this._spaces.set(spaceId, space);
|
|
61
|
+
}
|
|
62
|
+
const space = this._spaces.get(spaceId)!;
|
|
63
|
+
await space.open(); // No-op if already open.
|
|
64
|
+
return space;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const createClientFromEnv = async (env: any): Promise<FunctionsClient> => {
|
|
69
|
+
const client = new FunctionsClient({
|
|
70
|
+
dataService: env.DATA_SERVICE,
|
|
71
|
+
queueService: env.QUEUE_SERVICE,
|
|
72
|
+
});
|
|
73
|
+
await client.open();
|
|
74
|
+
return client;
|
|
75
|
+
};
|
|
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
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,125 @@
|
|
|
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
|
+
DataService as DataServiceProto,
|
|
16
|
+
GetDocumentHeadsRequest,
|
|
17
|
+
GetDocumentHeadsResponse,
|
|
18
|
+
GetSpaceSyncStateRequest,
|
|
19
|
+
ReIndexHeadsRequest,
|
|
20
|
+
SpaceSyncState,
|
|
21
|
+
UpdateRequest,
|
|
22
|
+
UpdateSubscriptionRequest,
|
|
23
|
+
} from '@dxos/protocols/proto/dxos/echo/service';
|
|
24
|
+
|
|
25
|
+
export class DataServiceImpl implements DataServiceProto {
|
|
26
|
+
private dataSubscriptions = new Map<string, { spaceId: SpaceId; next: (msg: BatchedDocumentUpdates) => void }>();
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
private _executionContext: EdgeFunctionEnv.ExecutionContext,
|
|
30
|
+
private _dataService: EdgeFunctionEnv.DataService,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
subscribe({ subscriptionId, spaceId }: { subscriptionId: string; spaceId: string }): Stream<BatchedDocumentUpdates> {
|
|
34
|
+
return new Stream(({ next }) => {
|
|
35
|
+
invariant(SpaceId.isValid(spaceId));
|
|
36
|
+
this.dataSubscriptions.set(subscriptionId, { spaceId, next });
|
|
37
|
+
|
|
38
|
+
return () => {
|
|
39
|
+
this.dataSubscriptions.delete(subscriptionId);
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async updateSubscription({ subscriptionId, addIds, removeIds }: UpdateSubscriptionRequest): Promise<void> {
|
|
45
|
+
const sub =
|
|
46
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
47
|
+
raise(
|
|
48
|
+
new RuntimeServiceError({
|
|
49
|
+
message: 'Subscription not found.',
|
|
50
|
+
context: { subscriptionId },
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
if (addIds) {
|
|
55
|
+
log.info('request documents', { count: addIds.length });
|
|
56
|
+
// TODO(dmaretskyi): Batch.
|
|
57
|
+
for (const documentId of addIds) {
|
|
58
|
+
const document = await this._dataService.getDocument(this._executionContext, sub.spaceId, documentId);
|
|
59
|
+
log.info('document loaded', { documentId, spaceId: sub.spaceId, found: !!document });
|
|
60
|
+
if (!document) {
|
|
61
|
+
log.warn('not found', { documentId });
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
sub.next({ updates: [{ documentId, mutation: document.data }] });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async update({ updates, subscriptionId }: UpdateRequest): Promise<void> {
|
|
70
|
+
const sub =
|
|
71
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
72
|
+
raise(
|
|
73
|
+
new RuntimeServiceError({
|
|
74
|
+
message: 'Subscription not found.',
|
|
75
|
+
context: { subscriptionId },
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
// TODO(dmaretskyi): Batch.
|
|
79
|
+
try {
|
|
80
|
+
for (const update of updates ?? []) {
|
|
81
|
+
await this._dataService.changeDocument(this._executionContext, sub.spaceId, update.documentId, update.mutation);
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
throw RuntimeServiceError.wrap({
|
|
85
|
+
message: 'Failed to apply document updates.',
|
|
86
|
+
context: { subscriptionId },
|
|
87
|
+
ifTypeDiffers: true,
|
|
88
|
+
})(error);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async flush(): Promise<void> {
|
|
93
|
+
// No-op.
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
subscribeSpaceSyncState(request: GetSpaceSyncStateRequest, options?: RequestOptions): Stream<SpaceSyncState> {
|
|
97
|
+
throw new NotImplementedError({
|
|
98
|
+
message: 'subscribeSpaceSyncState is not implemented.',
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async getDocumentHeads({ documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
103
|
+
throw new NotImplementedError({
|
|
104
|
+
message: 'getDocumentHeads is not implemented.',
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async reIndexHeads({ documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
109
|
+
throw new NotImplementedError({
|
|
110
|
+
message: 'reIndexHeads is not implemented.',
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async updateIndexes(): Promise<void> {
|
|
115
|
+
throw new NotImplementedError({
|
|
116
|
+
message: 'updateIndexes is not implemented.',
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async waitUntilHeadsReplicated({ heads }: { heads: any }): Promise<void> {
|
|
121
|
+
throw new NotImplementedError({
|
|
122
|
+
message: 'waitUntilHeadsReplicated is not implemented.',
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
|
|
24
|
+
export class QueryServiceImpl implements QueryServiceProto {
|
|
25
|
+
private _queryCount = 0;
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
private readonly _executionContext: EdgeFunctionEnv.ExecutionContext,
|
|
29
|
+
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
30
|
+
) {}
|
|
31
|
+
|
|
32
|
+
execQuery(request: QueryRequest): Stream<QueryResponse> {
|
|
33
|
+
log.info('execQuery', { request });
|
|
34
|
+
const query = QueryAST.Query.pipe(Schema.decodeUnknownSync)(JSON.parse(request.query));
|
|
35
|
+
const requestedSpaceIds = getTargetSpacesForQuery(query);
|
|
36
|
+
invariant(requestedSpaceIds.length === 1, 'Only one space is supported');
|
|
37
|
+
const spaceId = requestedSpaceIds[0];
|
|
38
|
+
|
|
39
|
+
return Stream.fromPromise<QueryResponse>(
|
|
40
|
+
(async () => {
|
|
41
|
+
try {
|
|
42
|
+
this._queryCount++;
|
|
43
|
+
log.info('begin query', { spaceId });
|
|
44
|
+
const queryResponse = await this._dataService.queryDocuments(
|
|
45
|
+
this._executionContext,
|
|
46
|
+
queryToDataServiceRequest(query),
|
|
47
|
+
);
|
|
48
|
+
log.info('query response', { spaceId, filter: request.filter, resultCount: queryResponse.results.length });
|
|
49
|
+
return {
|
|
50
|
+
results: queryResponse.results.map(
|
|
51
|
+
(object): QueryResultProto => ({
|
|
52
|
+
id: object.objectId,
|
|
53
|
+
spaceId,
|
|
54
|
+
spaceKey: PublicKey.ZERO,
|
|
55
|
+
documentId: object.document.documentId,
|
|
56
|
+
rank: 0,
|
|
57
|
+
documentAutomerge: object.document.data,
|
|
58
|
+
}),
|
|
59
|
+
),
|
|
60
|
+
} satisfies QueryResponse;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
log.error('query failed', { err: error });
|
|
63
|
+
throw new RuntimeServiceError({
|
|
64
|
+
message: `Query execution failed (queryCount=${this._queryCount})`,
|
|
65
|
+
context: { spaceId, filter: request.filter, queryCount: this._queryCount },
|
|
66
|
+
cause: error,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
})(),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async reindex() {
|
|
74
|
+
throw new NotImplementedError({
|
|
75
|
+
message: 'Reindex is not implemented.',
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async setConfig() {
|
|
80
|
+
throw new NotImplementedError({
|
|
81
|
+
message: 'SetConfig is not implemented.',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Lists spaces this query will select from.
|
|
88
|
+
*/
|
|
89
|
+
export const getTargetSpacesForQuery = (query: QueryAST.Query): SpaceId[] => {
|
|
90
|
+
const spaces = new Set<SpaceId>();
|
|
91
|
+
|
|
92
|
+
const visitor = (node: QueryAST.Query) => {
|
|
93
|
+
if (node.type === 'options') {
|
|
94
|
+
if (node.options.spaceIds) {
|
|
95
|
+
for (const spaceId of node.options.spaceIds) {
|
|
96
|
+
spaces.add(SpaceId.make(spaceId));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
QueryAST.visit(query, visitor);
|
|
102
|
+
return [...spaces];
|
|
103
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
6
|
+
import type { ObjectId, SpaceId } from '@dxos/keys';
|
|
7
|
+
import { type QueueService as QueueServiceProto } from '@dxos/protocols';
|
|
8
|
+
import type { EdgeFunctionEnv, QueryResult, QueueQuery } from '@dxos/protocols';
|
|
9
|
+
|
|
10
|
+
export class QueueServiceImpl implements QueueServiceProto {
|
|
11
|
+
constructor(
|
|
12
|
+
protected _ctx: EdgeFunctionEnv.ExecutionContext,
|
|
13
|
+
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
14
|
+
) {}
|
|
15
|
+
async queryQueue(subspaceTag: string, spaceId: SpaceId, { queueId, ...query }: QueueQuery): Promise<QueryResult> {
|
|
16
|
+
try {
|
|
17
|
+
const result = await this._queueService.query(this._ctx, `dxn:queue:${subspaceTag}:${spaceId}:${queueId}`, query);
|
|
18
|
+
return result;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw RuntimeServiceError.wrap({
|
|
21
|
+
message: 'Queue query failed.',
|
|
22
|
+
context: { subspaceTag, spaceId, queueId },
|
|
23
|
+
ifTypeDiffers: true,
|
|
24
|
+
})(error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async insertIntoQueue(subspaceTag: string, spaceId: SpaceId, queueId: ObjectId, objects: unknown[]): Promise<void> {
|
|
29
|
+
try {
|
|
30
|
+
const result = await this._queueService.append(
|
|
31
|
+
this._ctx,
|
|
32
|
+
`dxn:queue:${subspaceTag}:${spaceId}:${queueId}`,
|
|
33
|
+
objects,
|
|
34
|
+
);
|
|
35
|
+
return result;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw RuntimeServiceError.wrap({
|
|
38
|
+
message: 'Queue append failed.',
|
|
39
|
+
context: { subspaceTag, spaceId, queueId },
|
|
40
|
+
ifTypeDiffers: true,
|
|
41
|
+
})(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
deleteFromQueue(subspaceTag: string, spaceId: SpaceId, queueId: ObjectId, _objectIds: ObjectId[]): Promise<void> {
|
|
46
|
+
throw new NotImplementedError({
|
|
47
|
+
message: 'Deleting from queue is not supported.',
|
|
48
|
+
context: { subspaceTag, spaceId, queueId },
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { HasId } 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
|
+
) {}
|
|
26
|
+
|
|
27
|
+
async getSpaceMeta(spaceId: SpaceId): Promise<EdgeFunctionEnv.SpaceMeta | undefined> {
|
|
28
|
+
return this._dataService.getSpaceMeta(this._executionContext, spaceId);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async createServices(): Promise<{
|
|
32
|
+
dataService: DataServiceProto;
|
|
33
|
+
queryService: QueryServiceProto;
|
|
34
|
+
queueService: QueueServiceProto;
|
|
35
|
+
}> {
|
|
36
|
+
const dataService = new DataServiceImpl(this._executionContext, this._dataService);
|
|
37
|
+
const queryService = new QueryServiceImpl(this._executionContext, this._dataService);
|
|
38
|
+
const queueService = new QueueServiceImpl(this._executionContext, this._queueService);
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
dataService,
|
|
42
|
+
queryService,
|
|
43
|
+
queueService,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
queryQueue(queue: DXN): Promise<QueryResult> {
|
|
48
|
+
return this._queueService.query({}, queue.toString(), {});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void> {
|
|
52
|
+
return this._queueService.append({}, queue.toString(), objects);
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { LogLevel, type LogProcessor, log, shouldLog } from '@dxos/log';
|
|
6
|
+
|
|
7
|
+
export const setupFunctionsLogger = () => {
|
|
8
|
+
log.runtimeConfig.processors.length = 0;
|
|
9
|
+
log.runtimeConfig.processors.push(functionLogProcessor);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const functionLogProcessor: LogProcessor = (config, entry) => {
|
|
13
|
+
if (!shouldLog(entry, config.filters)) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
switch (entry.level) {
|
|
18
|
+
case LogLevel.DEBUG:
|
|
19
|
+
console.debug(entry.message, entry.context);
|
|
20
|
+
break;
|
|
21
|
+
case LogLevel.TRACE:
|
|
22
|
+
console.debug(entry.message, entry.context);
|
|
23
|
+
break;
|
|
24
|
+
case LogLevel.VERBOSE:
|
|
25
|
+
console.log(entry.message, entry.context);
|
|
26
|
+
break;
|
|
27
|
+
case LogLevel.INFO:
|
|
28
|
+
console.info(entry.message, entry.context);
|
|
29
|
+
break;
|
|
30
|
+
case LogLevel.WARN:
|
|
31
|
+
console.warn(entry.message, entry.context);
|
|
32
|
+
break;
|
|
33
|
+
case LogLevel.ERROR:
|
|
34
|
+
console.error(entry.message, entry.context);
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
console.log(entry.message, entry.context);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { HasId } 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: HasId[]): 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: HasId[]): Promise<void> {
|
|
35
|
+
// TODO(dmaretskyi): Ugly.
|
|
36
|
+
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Resource } from '@dxos/context';
|
|
6
|
+
import { type CoreDatabase, type EchoClient, type EchoDatabase } from '@dxos/echo-db';
|
|
7
|
+
import { invariant } from '@dxos/invariant';
|
|
8
|
+
import { PublicKey, type SpaceId } from '@dxos/keys';
|
|
9
|
+
|
|
10
|
+
import type { ServiceContainer } from './internal';
|
|
11
|
+
import { type QueuesAPI, QueuesAPIImpl } from './queues-api';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated
|
|
15
|
+
*/
|
|
16
|
+
export class SpaceProxy extends Resource {
|
|
17
|
+
private _db?: EchoDatabase = undefined;
|
|
18
|
+
private _queuesApi: QueuesAPIImpl;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly _serviceContainer: ServiceContainer,
|
|
22
|
+
private readonly _echoClient: EchoClient,
|
|
23
|
+
private readonly _id: SpaceId,
|
|
24
|
+
) {
|
|
25
|
+
super();
|
|
26
|
+
this._queuesApi = new QueuesAPIImpl(this._serviceContainer, this._id);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get id(): SpaceId {
|
|
30
|
+
return this._id;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get db(): EchoDatabase {
|
|
34
|
+
invariant(this._db);
|
|
35
|
+
return this._db;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @deprecated Use db API.
|
|
40
|
+
*/
|
|
41
|
+
get crud(): CoreDatabase {
|
|
42
|
+
invariant(this._db);
|
|
43
|
+
return this._db.coreDatabase;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get queues(): QueuesAPI {
|
|
47
|
+
return this._queuesApi;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
protected override async _open() {
|
|
51
|
+
const meta = await this._serviceContainer.getSpaceMeta(this._id);
|
|
52
|
+
if (!meta) {
|
|
53
|
+
throw new Error(`Space not found: ${this._id}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this._db = this._echoClient.constructDatabase({
|
|
57
|
+
spaceId: this._id,
|
|
58
|
+
spaceKey: PublicKey.from(meta.spaceKey),
|
|
59
|
+
reactiveSchemaQuery: false,
|
|
60
|
+
owningObject: this,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await this._db.coreDatabase.open({ rootUrl: meta.rootDocumentId });
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { JsonSchemaType } from '@dxos/echo/internal';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Is used for to route the request to the metadata handler instead of the main handler.
|
|
9
|
+
*/
|
|
10
|
+
export const FUNCTION_ROUTE_HEADER = 'X-DXOS-Function-Route';
|
|
11
|
+
|
|
12
|
+
export enum FunctionRouteValue {
|
|
13
|
+
Meta = 'meta',
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type FunctionMetadata = {
|
|
17
|
+
/**
|
|
18
|
+
* FQN.
|
|
19
|
+
*/
|
|
20
|
+
key: string;
|
|
21
|
+
/**
|
|
22
|
+
* Human-readable name.
|
|
23
|
+
*/
|
|
24
|
+
name?: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Description.
|
|
28
|
+
*/
|
|
29
|
+
description?: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Input schema.
|
|
33
|
+
*/
|
|
34
|
+
inputSchema?: JsonSchemaType;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Output schema.
|
|
38
|
+
*/
|
|
39
|
+
outputSchema?: JsonSchemaType;
|
|
40
|
+
};
|