@dxos/functions-runtime-cloudflare 0.8.4-main.c85a9c8dae → 0.8.4-main.e00bdcdb52
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 +73 -269
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +73 -269
- 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.map +1 -1
- package/dist/types/src/internal/data-service-impl.d.ts +1 -1
- package/dist/types/src/internal/data-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/query-service-impl.d.ts +3 -9
- package/dist/types/src/internal/query-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/queue-service-impl.d.ts +2 -2
- package/dist/types/src/internal/queue-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/service-container.d.ts +1 -1
- package/dist/types/src/internal/service-container.d.ts.map +1 -1
- package/dist/types/src/internal/utils.d.ts.map +1 -1
- package/dist/types/src/queues-api.d.ts +7 -3
- package/dist/types/src/queues-api.d.ts.map +1 -1
- 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 +15 -15
- package/src/functions-client.ts +1 -1
- package/src/internal/data-service-impl.ts +3 -2
- package/src/internal/query-service-impl.ts +8 -62
- package/src/internal/queue-service-impl.ts +1 -1
- package/src/internal/service-container.ts +2 -2
- package/src/logger.ts +10 -8
- package/src/queues-api.ts +23 -4
- package/src/space-proxy.ts +1 -1
- package/src/wrap-handler-for-cloudflare.ts +1 -1
- package/dist/types/src/internal/adapter.d.ts +0 -4
- package/dist/types/src/internal/adapter.d.ts.map +0 -1
- package/src/internal/adapter.ts +0 -52
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions-runtime-cloudflare",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.e00bdcdb52",
|
|
4
4
|
"description": "Functions runtime for Cloudflare.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -27,23 +27,23 @@
|
|
|
27
27
|
"src"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"effect": "3.
|
|
31
|
-
"@dxos/codec-protobuf": "0.8.4-main.
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/echo-db": "0.8.4-main.
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/invariant": "0.8.4-main.
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/protocols": "0.8.4-main.
|
|
42
|
-
"@dxos/util": "0.8.4-main.
|
|
30
|
+
"effect": "3.20.0",
|
|
31
|
+
"@dxos/codec-protobuf": "0.8.4-main.e00bdcdb52",
|
|
32
|
+
"@dxos/context": "0.8.4-main.e00bdcdb52",
|
|
33
|
+
"@dxos/echo": "0.8.4-main.e00bdcdb52",
|
|
34
|
+
"@dxos/errors": "0.8.4-main.e00bdcdb52",
|
|
35
|
+
"@dxos/echo-db": "0.8.4-main.e00bdcdb52",
|
|
36
|
+
"@dxos/debug": "0.8.4-main.e00bdcdb52",
|
|
37
|
+
"@dxos/echo-protocol": "0.8.4-main.e00bdcdb52",
|
|
38
|
+
"@dxos/invariant": "0.8.4-main.e00bdcdb52",
|
|
39
|
+
"@dxos/log": "0.8.4-main.e00bdcdb52",
|
|
40
|
+
"@dxos/keys": "0.8.4-main.e00bdcdb52",
|
|
41
|
+
"@dxos/protocols": "0.8.4-main.e00bdcdb52",
|
|
42
|
+
"@dxos/util": "0.8.4-main.e00bdcdb52"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@cloudflare/workers-types": "^4.20260302.0",
|
|
46
|
-
"@dxos/types": "0.8.4-main.
|
|
46
|
+
"@dxos/types": "0.8.4-main.e00bdcdb52"
|
|
47
47
|
},
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|
package/src/functions-client.ts
CHANGED
|
@@ -24,7 +24,7 @@ type Services = {
|
|
|
24
24
|
export class FunctionsClient extends Resource {
|
|
25
25
|
private readonly _serviceContainer;
|
|
26
26
|
private readonly _echoClient;
|
|
27
|
-
private readonly _executionContext: EdgeFunctionEnv.
|
|
27
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext = {};
|
|
28
28
|
|
|
29
29
|
private readonly _spaces = new Map<SpaceId, SpaceProxy>();
|
|
30
30
|
|
|
@@ -30,7 +30,7 @@ export class DataServiceImpl implements DataServiceProto {
|
|
|
30
30
|
private dataSubscriptions = new Map<string, { spaceId: SpaceId; next: (msg: BatchedDocumentUpdates) => void }>();
|
|
31
31
|
|
|
32
32
|
constructor(
|
|
33
|
-
private _executionContext: EdgeFunctionEnv.
|
|
33
|
+
private _executionContext: EdgeFunctionEnv.TraceContext,
|
|
34
34
|
private _dataService: EdgeFunctionEnv.DataService,
|
|
35
35
|
) {}
|
|
36
36
|
|
|
@@ -80,7 +80,8 @@ export class DataServiceImpl implements DataServiceProto {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
async createDocument({ spaceId, initialValue }: CreateDocumentRequest): Promise<CreateDocumentResponse> {
|
|
83
|
-
|
|
83
|
+
invariant(SpaceId.isValid(spaceId));
|
|
84
|
+
using response = await this._dataService.createDocument(this._executionContext, spaceId, initialValue);
|
|
84
85
|
return { documentId: response.documentId };
|
|
85
86
|
}
|
|
86
87
|
|
|
@@ -2,71 +2,36 @@
|
|
|
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 { QueryAST } from '@dxos/echo-protocol';
|
|
9
6
|
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
7
|
import { log } from '@dxos/log';
|
|
14
8
|
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
15
|
-
import {
|
|
16
|
-
type QueryRequest,
|
|
17
|
-
type QueryResponse,
|
|
18
|
-
type QueryResult as QueryResultProto,
|
|
19
|
-
type QueryService as QueryServiceProto,
|
|
20
|
-
} from '@dxos/protocols/proto/dxos/echo/query';
|
|
21
|
-
|
|
22
|
-
import { queryToDataServiceRequest } from './adapter';
|
|
23
|
-
import { copyUint8Array } from './utils';
|
|
9
|
+
import { type QueryRequest, type QueryResponse, type QueryService } from '@dxos/protocols/proto/dxos/echo/query';
|
|
24
10
|
|
|
25
|
-
export class QueryServiceImpl implements
|
|
11
|
+
export class QueryServiceImpl implements QueryService {
|
|
26
12
|
private _queryCount = 0;
|
|
27
13
|
|
|
28
14
|
constructor(
|
|
29
|
-
private readonly _executionContext: EdgeFunctionEnv.
|
|
15
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext,
|
|
30
16
|
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
31
17
|
) {}
|
|
32
18
|
|
|
33
19
|
execQuery(request: QueryRequest): Stream<QueryResponse> {
|
|
34
20
|
log.info('execQuery', { request });
|
|
35
|
-
const query = QueryAST.Query.pipe(Schema.decodeUnknownSync)(JSON.parse(request.query));
|
|
36
|
-
const requestedSpaceIds = getTargetSpacesForQuery(query);
|
|
37
|
-
invariant(requestedSpaceIds.length === 1, 'Only one space is supported');
|
|
38
|
-
const spaceId = requestedSpaceIds[0];
|
|
39
21
|
|
|
40
22
|
return Stream.fromPromise<QueryResponse>(
|
|
41
23
|
(async () => {
|
|
42
24
|
try {
|
|
43
25
|
this._queryCount++;
|
|
44
|
-
log.info('begin query', {
|
|
45
|
-
using queryResponse = await this._dataService.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
49
|
-
log.info('query response', { spaceId, filter: request.filter, resultCount: queryResponse.results.length });
|
|
50
|
-
return {
|
|
51
|
-
results: queryResponse.results.map(
|
|
52
|
-
(object): QueryResultProto => ({
|
|
53
|
-
id: object.objectId,
|
|
54
|
-
spaceId,
|
|
55
|
-
spaceKey: PublicKey.ZERO,
|
|
56
|
-
documentId: object.document.documentId,
|
|
57
|
-
// Rank 1 for predicate matches where ranking is not determined.
|
|
58
|
-
rank: 1,
|
|
59
|
-
// Copy returned object to avoid hanging RPC stub.
|
|
60
|
-
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
61
|
-
documentAutomerge: copyUint8Array(object.document.data),
|
|
62
|
-
}),
|
|
63
|
-
),
|
|
64
|
-
} satisfies QueryResponse;
|
|
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);
|
|
65
30
|
} catch (error) {
|
|
66
31
|
log.error('query failed', { err: error });
|
|
67
32
|
throw new RuntimeServiceError({
|
|
68
33
|
message: `Query execution failed (queryCount=${this._queryCount})`,
|
|
69
|
-
context: {
|
|
34
|
+
context: { queryCount: this._queryCount },
|
|
70
35
|
cause: error,
|
|
71
36
|
});
|
|
72
37
|
}
|
|
@@ -86,22 +51,3 @@ export class QueryServiceImpl implements QueryServiceProto {
|
|
|
86
51
|
});
|
|
87
52
|
}
|
|
88
53
|
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Lists spaces this query will select from.
|
|
92
|
-
*/
|
|
93
|
-
export const getTargetSpacesForQuery = (query: QueryAST.Query): SpaceId[] => {
|
|
94
|
-
const spaces = new Set<SpaceId>();
|
|
95
|
-
|
|
96
|
-
const visitor = (node: QueryAST.Query) => {
|
|
97
|
-
if (node.type === 'from' && node.from._tag === 'scope') {
|
|
98
|
-
if (node.from.scope.spaceIds) {
|
|
99
|
-
for (const spaceId of node.from.scope.spaceIds) {
|
|
100
|
-
spaces.add(SpaceId.make(spaceId));
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
QueryAST.visit(query, visitor);
|
|
106
|
-
return [...spaces];
|
|
107
|
-
};
|
|
@@ -7,7 +7,7 @@ import { type EdgeFunctionEnv, type FeedProtocol } from '@dxos/protocols';
|
|
|
7
7
|
|
|
8
8
|
export class QueueServiceImpl implements FeedProtocol.QueueService {
|
|
9
9
|
constructor(
|
|
10
|
-
protected _ctx: EdgeFunctionEnv.
|
|
10
|
+
protected _ctx: EdgeFunctionEnv.TraceContext,
|
|
11
11
|
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
12
12
|
) {}
|
|
13
13
|
|
|
@@ -17,7 +17,7 @@ import { QueueServiceImpl } from './queue-service-impl';
|
|
|
17
17
|
*/
|
|
18
18
|
export class ServiceContainer {
|
|
19
19
|
constructor(
|
|
20
|
-
private readonly _executionContext: EdgeFunctionEnv.
|
|
20
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext,
|
|
21
21
|
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
22
22
|
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
23
23
|
private readonly _functionsService: EdgeFunctionEnv.FunctionsAiService,
|
|
@@ -83,7 +83,7 @@ export class ServiceContainer {
|
|
|
83
83
|
subspaceTag,
|
|
84
84
|
spaceId,
|
|
85
85
|
queueId,
|
|
86
|
-
objects: objects
|
|
86
|
+
objects: objects.map((obj) => JSON.stringify(obj)),
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
}
|
package/src/logger.ts
CHANGED
|
@@ -16,27 +16,29 @@ const functionLogProcessor: LogProcessor = (config, entry) => {
|
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
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
|
+
|
|
19
23
|
switch (entry.level) {
|
|
20
24
|
case LogLevel.DEBUG:
|
|
21
|
-
console.debug(entry.message, entry.context);
|
|
22
|
-
break;
|
|
23
25
|
case LogLevel.TRACE:
|
|
24
|
-
console.debug(entry.message,
|
|
26
|
+
console.debug(entry.message, ...extras);
|
|
25
27
|
break;
|
|
26
28
|
case LogLevel.VERBOSE:
|
|
27
|
-
console.log(entry.message,
|
|
29
|
+
console.log(entry.message, ...extras);
|
|
28
30
|
break;
|
|
29
31
|
case LogLevel.INFO:
|
|
30
|
-
console.info(entry.message,
|
|
32
|
+
console.info(entry.message, ...extras);
|
|
31
33
|
break;
|
|
32
34
|
case LogLevel.WARN:
|
|
33
|
-
console.warn(entry.message,
|
|
35
|
+
console.warn(entry.message, ...extras);
|
|
34
36
|
break;
|
|
35
37
|
case LogLevel.ERROR:
|
|
36
|
-
console.error(entry.message,
|
|
38
|
+
console.error(entry.message, ...extras);
|
|
37
39
|
break;
|
|
38
40
|
default:
|
|
39
|
-
console.log(entry.message,
|
|
41
|
+
console.log(entry.message, ...extras);
|
|
40
42
|
break;
|
|
41
43
|
}
|
|
42
44
|
};
|
package/src/queues-api.ts
CHANGED
|
@@ -4,17 +4,23 @@
|
|
|
4
4
|
|
|
5
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<
|
|
23
|
+
queryQueue(queue: DXN, options?: {}): Promise<QueuesQueryResult>;
|
|
18
24
|
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void>;
|
|
19
25
|
}
|
|
20
26
|
|
|
@@ -27,8 +33,21 @@ 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
53
|
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
package/src/space-proxy.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../../../src/internal/adapter.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvD,eAAO,MAAM,yBAAyB,GAAI,OAAO,QAAQ,CAAC,KAAK,KAAG,eAAe,CAAC,YAajF,CAAC"}
|
package/src/internal/adapter.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
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, spaceIds } = extractSimpleQuery(query) ?? failUndefined();
|
|
13
|
-
invariant(spaceIds?.length === 1, 'Only one space is supported');
|
|
14
|
-
invariant(filter.type === 'object', 'Only object filters are supported');
|
|
15
|
-
|
|
16
|
-
const spaceId = 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 space IDs from a query.
|
|
28
|
-
* Supports Select(...), Options(Select(...)), and From(Select(...)) queries.
|
|
29
|
-
*/
|
|
30
|
-
const extractSimpleQuery = (
|
|
31
|
-
query: QueryAST.Query,
|
|
32
|
-
): { filter: QueryAST.Filter; spaceIds?: readonly string[] } | null => {
|
|
33
|
-
switch (query.type) {
|
|
34
|
-
case 'options': {
|
|
35
|
-
return extractSimpleQuery(query.query);
|
|
36
|
-
}
|
|
37
|
-
case 'from': {
|
|
38
|
-
const inner = extractSimpleQuery(query.query);
|
|
39
|
-
if (!inner) {
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
const spaceIds = query.from._tag === 'scope' ? query.from.scope.spaceIds : undefined;
|
|
43
|
-
return { filter: inner.filter, spaceIds: spaceIds ?? inner.spaceIds };
|
|
44
|
-
}
|
|
45
|
-
case 'select': {
|
|
46
|
-
return { filter: query.filter };
|
|
47
|
-
}
|
|
48
|
-
default: {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
};
|