@dxos/functions 0.8.4-main.b97322e → 0.8.4-main.dedc0f3
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/bundler/index.mjs +56 -38
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-ANP3DFCO.mjs +623 -0
- package/dist/lib/browser/chunk-ANP3DFCO.mjs.map +7 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +22 -8
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +892 -130
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +77 -39
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +55 -38
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs +625 -0
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +21 -8
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +892 -130
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +77 -39
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/bundler/bundler.d.ts +11 -12
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +3 -2
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +77 -8
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/examples/fib.d.ts +7 -0
- package/dist/types/src/examples/fib.d.ts.map +1 -0
- package/dist/types/src/examples/index.d.ts +4 -0
- package/dist/types/src/examples/index.d.ts.map +1 -0
- package/dist/types/src/examples/reply.d.ts +3 -0
- package/dist/types/src/examples/reply.d.ts.map +1 -0
- package/dist/types/src/examples/sleep.d.ts +5 -0
- package/dist/types/src/examples/sleep.d.ts.map +1 -0
- package/dist/types/src/executor/executor.d.ts +4 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +40 -8
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +6 -1
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +16 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +75 -6
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +65 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +2 -1
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +25 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +21 -6
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +5 -5
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +1 -1
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +37 -5
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +7 -2
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/testing/logger.d.ts +3 -3
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/persist-database.test.d.ts +2 -0
- package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
- package/dist/types/src/testing/services.d.ts +6 -17
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +20 -22
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/triggers/index.d.ts +4 -0
- package/dist/types/src/triggers/index.d.ts.map +1 -0
- package/dist/types/src/triggers/input-builder.d.ts +3 -0
- package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +75 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +49 -249
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +10 -6
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +39 -34
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +32 -33
- package/src/edge/functions.ts +8 -5
- package/src/errors.ts +8 -0
- package/src/examples/fib.ts +30 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +18 -0
- package/src/examples/sleep.ts +22 -0
- package/src/executor/executor.ts +9 -9
- package/src/handler.ts +99 -18
- package/src/index.ts +3 -3
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +79 -3
- package/src/services/database.ts +118 -18
- package/src/services/event-logger.ts +68 -37
- package/src/services/index.ts +2 -1
- package/src/services/local-function-execution.ts +114 -0
- package/src/services/queues.ts +37 -10
- package/src/services/remote-function-execution-service.ts +46 -0
- package/src/services/service-container.ts +11 -10
- package/src/services/service-registry.ts +5 -2
- package/src/services/tracing.ts +105 -7
- package/src/testing/layer.ts +83 -3
- package/src/testing/logger.ts +4 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +10 -63
- package/src/trace.ts +17 -19
- package/src/triggers/index.ts +7 -0
- package/src/triggers/input-builder.ts +35 -0
- package/src/triggers/invocation-tracer.ts +99 -0
- package/src/triggers/trigger-dispatcher.test.ts +652 -0
- package/src/triggers/trigger-dispatcher.ts +512 -0
- package/src/triggers/trigger-state-store.ts +60 -0
- package/src/types.ts +22 -33
- package/src/url.ts +13 -10
- package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
- package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/src/services/function-call-service.ts +0 -64
package/package.json
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.dedc0f3",
|
|
4
4
|
"description": "Functions API and runtime.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "info@dxos.org",
|
|
9
|
-
"sideEffects":
|
|
9
|
+
"sideEffects": false,
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"source": "./src/index.ts",
|
|
14
|
+
"types": "./dist/types/src/index.d.ts",
|
|
15
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
16
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
17
|
+
},
|
|
12
18
|
"./bundler": {
|
|
19
|
+
"source": "./src/bundler/index.ts",
|
|
13
20
|
"types": "./dist/types/src/bundler/index.d.ts",
|
|
14
21
|
"browser": "./dist/lib/browser/bundler/index.mjs",
|
|
15
22
|
"node": "./dist/lib/node-esm/bundler/index.mjs"
|
|
16
23
|
},
|
|
17
24
|
"./edge": {
|
|
25
|
+
"source": "./src/edge/index.ts",
|
|
18
26
|
"types": "./dist/types/src/edge/index.d.ts",
|
|
19
27
|
"browser": "./dist/lib/browser/edge/index.mjs",
|
|
20
28
|
"node": "./dist/lib/node-esm/edge/index.mjs"
|
|
21
29
|
},
|
|
22
|
-
".": {
|
|
23
|
-
"types": "./dist/types/src/index.d.ts",
|
|
24
|
-
"browser": "./dist/lib/browser/index.mjs",
|
|
25
|
-
"node": "./dist/lib/node-esm/index.mjs"
|
|
26
|
-
},
|
|
27
30
|
"./testing": {
|
|
31
|
+
"source": "./src/testing/index.ts",
|
|
28
32
|
"types": "./dist/types/src/testing/index.d.ts",
|
|
29
33
|
"browser": "./dist/lib/browser/testing/index.mjs",
|
|
30
34
|
"node": "./dist/lib/node-esm/testing/index.mjs"
|
|
@@ -47,48 +51,49 @@
|
|
|
47
51
|
"src"
|
|
48
52
|
],
|
|
49
53
|
"dependencies": {
|
|
50
|
-
"@effect/platform": "0.
|
|
54
|
+
"@effect/platform": "0.90.2",
|
|
51
55
|
"@preact/signals-core": "^1.9.0",
|
|
52
56
|
"cron": "^3.1.6",
|
|
53
57
|
"cron-schedule": "^5.0.4",
|
|
54
|
-
"effect": "3.17.
|
|
58
|
+
"effect": "3.17.7",
|
|
55
59
|
"esbuild-wasm": "^0.16.14",
|
|
56
60
|
"express": "^4.19.2",
|
|
57
61
|
"get-port-please": "^3.1.1",
|
|
58
62
|
"i18next": "^24.2.1",
|
|
59
63
|
"iso-did": "^1.6.0",
|
|
60
64
|
"ws": "^8.14.2",
|
|
61
|
-
"@dxos/ai": "0.8.4-main.
|
|
62
|
-
"@dxos/async": "0.8.4-main.
|
|
63
|
-
"@dxos/
|
|
64
|
-
"@dxos/
|
|
65
|
-
"@dxos/
|
|
66
|
-
"@dxos/
|
|
67
|
-
"@dxos/
|
|
68
|
-
"@dxos/echo-
|
|
69
|
-
"@dxos/echo
|
|
70
|
-
"@dxos/echo-
|
|
71
|
-
"@dxos/
|
|
72
|
-
"@dxos/
|
|
73
|
-
"@dxos/
|
|
74
|
-
"@dxos/invariant": "0.8.4-main.
|
|
75
|
-
"@dxos/
|
|
76
|
-
"@dxos/
|
|
77
|
-
"@dxos/
|
|
78
|
-
"@dxos/
|
|
79
|
-
"@dxos/
|
|
80
|
-
"@dxos/
|
|
81
|
-
"@dxos/
|
|
65
|
+
"@dxos/ai": "0.8.4-main.dedc0f3",
|
|
66
|
+
"@dxos/async": "0.8.4-main.dedc0f3",
|
|
67
|
+
"@dxos/context": "0.8.4-main.dedc0f3",
|
|
68
|
+
"@dxos/client": "0.8.4-main.dedc0f3",
|
|
69
|
+
"@dxos/client-protocol": "0.8.4-main.dedc0f3",
|
|
70
|
+
"@dxos/crypto": "0.8.4-main.dedc0f3",
|
|
71
|
+
"@dxos/debug": "0.8.4-main.dedc0f3",
|
|
72
|
+
"@dxos/echo-db": "0.8.4-main.dedc0f3",
|
|
73
|
+
"@dxos/echo": "0.8.4-main.dedc0f3",
|
|
74
|
+
"@dxos/echo-pipeline": "0.8.4-main.dedc0f3",
|
|
75
|
+
"@dxos/echo-protocol": "0.8.4-main.dedc0f3",
|
|
76
|
+
"@dxos/echo-schema": "0.8.4-main.dedc0f3",
|
|
77
|
+
"@dxos/edge-client": "0.8.4-main.dedc0f3",
|
|
78
|
+
"@dxos/invariant": "0.8.4-main.dedc0f3",
|
|
79
|
+
"@dxos/effect": "0.8.4-main.dedc0f3",
|
|
80
|
+
"@dxos/errors": "0.8.4-main.dedc0f3",
|
|
81
|
+
"@dxos/keys": "0.8.4-main.dedc0f3",
|
|
82
|
+
"@dxos/kv-store": "0.8.4-main.dedc0f3",
|
|
83
|
+
"@dxos/live-object": "0.8.4-main.dedc0f3",
|
|
84
|
+
"@dxos/log": "0.8.4-main.dedc0f3",
|
|
85
|
+
"@dxos/node-std": "0.8.4-main.dedc0f3",
|
|
86
|
+
"@dxos/protocols": "0.8.4-main.dedc0f3",
|
|
87
|
+
"@dxos/util": "0.8.4-main.dedc0f3",
|
|
88
|
+
"@dxos/schema": "0.8.4-main.dedc0f3"
|
|
82
89
|
},
|
|
83
90
|
"devDependencies": {
|
|
84
91
|
"@types/express": "^4.17.17",
|
|
85
92
|
"@types/ws": "^7.4.0",
|
|
86
|
-
"@dxos/agent": "0.8.4-main.
|
|
93
|
+
"@dxos/agent": "0.8.4-main.dedc0f3"
|
|
87
94
|
},
|
|
88
95
|
"publishConfig": {
|
|
89
96
|
"access": "public"
|
|
90
97
|
},
|
|
91
|
-
"scripts": {
|
|
92
|
-
"gen-schema": "ts-node ./tools/schema.ts"
|
|
93
|
-
}
|
|
98
|
+
"scripts": {}
|
|
94
99
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
import wasmUrl from 'esbuild-wasm/esbuild.wasm?url';
|
|
7
|
-
import { beforeAll, describe, expect, test } from 'vitest';
|
|
7
|
+
import { assert, beforeAll, describe, expect, test } from 'vitest';
|
|
8
8
|
|
|
9
9
|
import { isNode } from '@dxos/util';
|
|
10
10
|
|
|
@@ -20,8 +20,8 @@ describe('Bundler', () => {
|
|
|
20
20
|
test('Basic', async () => {
|
|
21
21
|
const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
|
|
22
22
|
const result = await bundler.bundle({ source: 'const x = 100' }); // TODO(burdon): Test import.
|
|
23
|
-
|
|
24
|
-
expect(result.
|
|
23
|
+
assert(!('error' in result), 'error should not exist');
|
|
24
|
+
expect(result.asset).toBeDefined();
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
test('Import', async () => {
|
|
@@ -33,8 +33,8 @@ describe('Bundler', () => {
|
|
|
33
33
|
const query = Filter.typename('dxos.org/type/Example');
|
|
34
34
|
`,
|
|
35
35
|
});
|
|
36
|
-
|
|
37
|
-
expect(result.
|
|
36
|
+
assert(!('error' in result), 'error should not exist');
|
|
37
|
+
expect(result.asset).toBeDefined();
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
// TODO(dmaretskyi): Flaky on CI.
|
|
@@ -46,14 +46,13 @@ describe('Bundler', () => {
|
|
|
46
46
|
invariant(true);
|
|
47
47
|
`,
|
|
48
48
|
});
|
|
49
|
-
|
|
50
|
-
expect(result.
|
|
49
|
+
assert(!('error' in result), 'error should not exist');
|
|
50
|
+
expect(result.asset).toBeDefined();
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
test('Error', async () => {
|
|
54
54
|
const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
|
|
55
55
|
const result = await bundler.bundle({ source: "import missing from './module'; missing();" });
|
|
56
|
-
|
|
57
|
-
expect(result.error).to.exist;
|
|
56
|
+
assert('error' in result, 'error should exist');
|
|
58
57
|
});
|
|
59
58
|
});
|
package/src/bundler/bundler.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { FetchHttpClient, HttpClient } from '@effect/platform';
|
|
6
|
-
import { Duration, Effect,
|
|
7
|
-
import { type BuildOptions, type
|
|
6
|
+
import { Duration, Effect, Schedule, pipe } from 'effect';
|
|
7
|
+
import { type BuildOptions, type BuildResult, type Loader, type Plugin, build, initialize } from 'esbuild-wasm';
|
|
8
8
|
|
|
9
9
|
import { subtleCrypto } from '@dxos/crypto';
|
|
10
10
|
import { runAndForwardErrors } from '@dxos/effect';
|
|
@@ -18,26 +18,26 @@ export type Import = {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
export type BundleOptions = {
|
|
21
|
-
/**
|
|
22
|
-
* Path to the source file on the local file system.
|
|
23
|
-
* If provided, the path will be used instead of the `source` code.
|
|
24
|
-
*/
|
|
25
|
-
path?: string;
|
|
26
|
-
|
|
27
21
|
/**
|
|
28
22
|
* Source code to bundle.
|
|
29
|
-
* Required if `path` is not provided.
|
|
30
23
|
*/
|
|
31
|
-
source
|
|
24
|
+
source: string;
|
|
32
25
|
};
|
|
33
26
|
|
|
34
|
-
export type BundleResult =
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
27
|
+
export type BundleResult =
|
|
28
|
+
| {
|
|
29
|
+
timestamp: number;
|
|
30
|
+
sourceHash: Buffer;
|
|
31
|
+
error: unknown;
|
|
32
|
+
}
|
|
33
|
+
| {
|
|
34
|
+
timestamp: number;
|
|
35
|
+
sourceHash: Buffer;
|
|
36
|
+
imports: Import[];
|
|
37
|
+
entryPoint: string;
|
|
38
|
+
asset: Uint8Array;
|
|
39
|
+
bundle: string;
|
|
40
|
+
};
|
|
41
41
|
|
|
42
42
|
export type BundlerOptions = {
|
|
43
43
|
platform: BuildOptions['platform'];
|
|
@@ -58,16 +58,9 @@ export const initializeBundler = async (options: { wasmUrl: string }) => {
|
|
|
58
58
|
export class Bundler {
|
|
59
59
|
constructor(private readonly _options: BundlerOptions) {}
|
|
60
60
|
|
|
61
|
-
async bundle({
|
|
61
|
+
async bundle({ source }: BundleOptions): Promise<BundleResult> {
|
|
62
62
|
const { sandboxedModules: providedModules, ...options } = this._options;
|
|
63
|
-
|
|
64
|
-
const createResult = async (result?: Partial<BundleResult>) => {
|
|
65
|
-
return {
|
|
66
|
-
timestamp: Date.now(),
|
|
67
|
-
sourceHash: source ? Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))) : undefined,
|
|
68
|
-
...result,
|
|
69
|
-
};
|
|
70
|
-
};
|
|
63
|
+
const sourceHash = Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source)));
|
|
71
64
|
|
|
72
65
|
if (this._options.platform === 'browser') {
|
|
73
66
|
invariant(initialized, 'Compiler not initialized.');
|
|
@@ -83,7 +76,10 @@ export class Bundler {
|
|
|
83
76
|
conditions: ['workerd', 'browser'],
|
|
84
77
|
metafile: true,
|
|
85
78
|
write: false,
|
|
86
|
-
entryPoints:
|
|
79
|
+
entryPoints: {
|
|
80
|
+
// Gets mapped to `userFunc.js` by esbuild.
|
|
81
|
+
userFunc: 'memory:main.tsx',
|
|
82
|
+
},
|
|
87
83
|
bundle: true,
|
|
88
84
|
format: 'esm',
|
|
89
85
|
plugins: [
|
|
@@ -136,12 +132,17 @@ export class Bundler {
|
|
|
136
132
|
|
|
137
133
|
log('compile complete', result.metafile);
|
|
138
134
|
|
|
139
|
-
|
|
135
|
+
const entryPoint = 'userFunc.js';
|
|
136
|
+
return {
|
|
137
|
+
timestamp: Date.now(),
|
|
138
|
+
sourceHash,
|
|
140
139
|
imports: this.analyzeImports(result),
|
|
140
|
+
entryPoint,
|
|
141
|
+
asset: result.outputFiles![0].contents,
|
|
141
142
|
bundle: result.outputFiles![0].text,
|
|
142
|
-
}
|
|
143
|
+
};
|
|
143
144
|
} catch (err) {
|
|
144
|
-
return
|
|
145
|
+
return { timestamp: Date.now(), sourceHash, error: err };
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
148
|
|
|
@@ -153,7 +154,6 @@ export class Bundler {
|
|
|
153
154
|
const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
|
|
154
155
|
return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {
|
|
155
156
|
const namedImports: string[] = [];
|
|
156
|
-
|
|
157
157
|
const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
|
|
158
158
|
if (parsedImport?.[2]) {
|
|
159
159
|
NAMED_IMPORTS_REGEX.lastIndex = 0;
|
|
@@ -207,10 +207,9 @@ const IMPORT_REGEX =
|
|
|
207
207
|
const NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
|
|
208
208
|
|
|
209
209
|
const allMatches = (regex: RegExp, str: string) => {
|
|
210
|
-
regex.lastIndex = 0;
|
|
211
|
-
|
|
212
210
|
let match;
|
|
213
211
|
const matches = [];
|
|
212
|
+
regex.lastIndex = 0;
|
|
214
213
|
while ((match = regex.exec(str))) {
|
|
215
214
|
matches.push(match);
|
|
216
215
|
}
|
package/src/edge/functions.ts
CHANGED
|
@@ -14,21 +14,24 @@ import { type UploadFunctionResponseBody } from '@dxos/protocols';
|
|
|
14
14
|
|
|
15
15
|
export type UploadWorkerArgs = {
|
|
16
16
|
client: Client;
|
|
17
|
-
source: string;
|
|
18
17
|
version: string;
|
|
19
18
|
name?: string;
|
|
20
19
|
functionId?: string;
|
|
21
20
|
ownerPublicKey: PublicKey;
|
|
21
|
+
entryPoint: string;
|
|
22
|
+
assets: Record<string, Uint8Array>;
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
export const uploadWorkerFunction = async ({
|
|
25
26
|
client,
|
|
26
27
|
version,
|
|
27
|
-
source,
|
|
28
28
|
name,
|
|
29
29
|
functionId,
|
|
30
30
|
ownerPublicKey,
|
|
31
|
+
entryPoint,
|
|
32
|
+
assets,
|
|
31
33
|
}: UploadWorkerArgs): Promise<UploadFunctionResponseBody> => {
|
|
34
|
+
log('uploading function', { functionId, name, version, ownerPublicKey });
|
|
32
35
|
const edgeUrl = client.config.values.runtime?.services?.edge?.url;
|
|
33
36
|
invariant(edgeUrl, 'Edge is not configured.');
|
|
34
37
|
const edgeClient = new EdgeHttpClient(edgeUrl);
|
|
@@ -36,15 +39,15 @@ export const uploadWorkerFunction = async ({
|
|
|
36
39
|
edgeClient.setIdentity(edgeIdentity);
|
|
37
40
|
const response = await edgeClient.uploadFunction(
|
|
38
41
|
{ functionId },
|
|
39
|
-
{ name, version,
|
|
42
|
+
{ name, version, ownerPublicKey: ownerPublicKey.toHex(), entryPoint, assets },
|
|
40
43
|
);
|
|
41
44
|
|
|
42
45
|
// TODO(burdon): Edge service log.
|
|
43
|
-
log
|
|
46
|
+
log('uploaded', {
|
|
44
47
|
identityKey: edgeIdentity.identityKey,
|
|
45
48
|
functionId,
|
|
46
49
|
name,
|
|
47
|
-
|
|
50
|
+
version,
|
|
48
51
|
response,
|
|
49
52
|
});
|
|
50
53
|
|
package/src/errors.ts
CHANGED
|
@@ -10,4 +10,12 @@ export class ServiceNotAvailableError extends BaseError.extend('SERVICE_NOT_AVAI
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
export class FunctionNotFoundError extends BaseError.extend('FUNCTION_NOT_FOUND') {
|
|
14
|
+
constructor(functionKey: string) {
|
|
15
|
+
super(`Function not found: ${functionKey}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
export class FunctionError extends BaseError.extend('FUNCTION_ERROR') {}
|
|
20
|
+
|
|
21
|
+
export class TriggerStateNotFoundError extends BaseError.extend('TRIGGER_STATE_NOT_FOUND') {}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect, Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { defineFunction } from '../handler';
|
|
8
|
+
|
|
9
|
+
export default defineFunction({
|
|
10
|
+
name: 'example.org/function/fib',
|
|
11
|
+
description: 'Function that calculates a Fibonacci number',
|
|
12
|
+
inputSchema: Schema.Struct({
|
|
13
|
+
iterations: Schema.optional(Schema.Number).annotations({
|
|
14
|
+
description: 'Number of iterations',
|
|
15
|
+
default: 100_000,
|
|
16
|
+
}),
|
|
17
|
+
}),
|
|
18
|
+
outputSchema: Schema.Struct({
|
|
19
|
+
result: Schema.String,
|
|
20
|
+
}),
|
|
21
|
+
handler: Effect.fn(function* ({ data: { iterations = 100_000 } }) {
|
|
22
|
+
let a = 0n;
|
|
23
|
+
let b = 1n;
|
|
24
|
+
for (let i = 0; i < iterations; i++) {
|
|
25
|
+
a += b;
|
|
26
|
+
b = a - b;
|
|
27
|
+
}
|
|
28
|
+
return { result: a.toString() };
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Console, Effect, Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { defineFunction } from '../handler';
|
|
8
|
+
|
|
9
|
+
export default defineFunction({
|
|
10
|
+
name: 'example.org/function/reply',
|
|
11
|
+
description: 'Function that echoes the input',
|
|
12
|
+
inputSchema: Schema.Any,
|
|
13
|
+
outputSchema: Schema.Any,
|
|
14
|
+
handler: Effect.fn(function* ({ data }) {
|
|
15
|
+
yield* Console.log('reply', { data });
|
|
16
|
+
return data;
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect, Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { defineFunction } from '../handler';
|
|
8
|
+
|
|
9
|
+
export default defineFunction({
|
|
10
|
+
name: 'example.org/function/sleep',
|
|
11
|
+
description: 'Function that sleeps for a given amount of time',
|
|
12
|
+
inputSchema: Schema.Struct({
|
|
13
|
+
duration: Schema.optional(Schema.Number).annotations({
|
|
14
|
+
description: 'Milliseconds to sleep',
|
|
15
|
+
default: 100_000,
|
|
16
|
+
}),
|
|
17
|
+
}),
|
|
18
|
+
outputSchema: Schema.Void,
|
|
19
|
+
handler: Effect.fn(function* ({ data: { duration = 100_000 } }) {
|
|
20
|
+
yield* Effect.sleep(duration);
|
|
21
|
+
}),
|
|
22
|
+
});
|
package/src/executor/executor.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { Effect, Schema } from 'effect';
|
|
6
6
|
|
|
7
|
-
import type
|
|
7
|
+
import { type SpaceId } from '@dxos/client/echo';
|
|
8
8
|
import { runAndForwardErrors } from '@dxos/effect';
|
|
9
9
|
|
|
10
10
|
import type { FunctionContext, FunctionDefinition } from '../handler';
|
|
@@ -13,27 +13,27 @@ import type { ServiceContainer, Services } from '../services';
|
|
|
13
13
|
export class FunctionExecutor {
|
|
14
14
|
constructor(private readonly _services: ServiceContainer) {}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Invoke function.
|
|
18
|
+
*/
|
|
16
19
|
// TODO(dmaretskyi): Invocation context: queue, space, etc...
|
|
17
20
|
async invoke<F extends FunctionDefinition<any, any>>(
|
|
18
|
-
|
|
21
|
+
functionDef: F,
|
|
19
22
|
input: F extends FunctionDefinition<infer I, infer _O> ? I : never,
|
|
20
23
|
): Promise<F extends FunctionDefinition<infer _I, infer O> ? O : never> {
|
|
21
24
|
// Assert input matches schema
|
|
22
|
-
const assertInput =
|
|
25
|
+
const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
|
|
23
26
|
(assertInput as any)(input);
|
|
24
27
|
|
|
25
28
|
const context: FunctionContext = {
|
|
29
|
+
space: undefined,
|
|
26
30
|
getService: this._services.getService.bind(this._services),
|
|
27
31
|
getSpace: async (_spaceId: SpaceId) => {
|
|
28
32
|
throw new Error('Not available. Use the database service instead.');
|
|
29
33
|
},
|
|
30
|
-
space: undefined,
|
|
31
|
-
get ai(): never {
|
|
32
|
-
throw new Error('Not available. Use the ai service instead.');
|
|
33
|
-
},
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
const result =
|
|
36
|
+
const result = functionDef.handler({ context, data: input });
|
|
37
37
|
|
|
38
38
|
let data: unknown;
|
|
39
39
|
if (Effect.isEffect(result)) {
|
|
@@ -46,7 +46,7 @@ export class FunctionExecutor {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// Assert output matches schema
|
|
49
|
-
const assertOutput =
|
|
49
|
+
const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
|
|
50
50
|
(assertOutput as any)(data);
|
|
51
51
|
|
|
52
52
|
return data as any;
|
package/src/handler.ts
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { type Context, Effect, Schema, type Types } from 'effect';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
import type { EchoDatabase } from '@dxos/echo-db';
|
|
7
|
+
import { Obj, Type } from '@dxos/echo';
|
|
8
|
+
import { type EchoDatabase } from '@dxos/echo-db';
|
|
10
9
|
import { type HasId } from '@dxos/echo-schema';
|
|
11
|
-
import {
|
|
10
|
+
import { assertArgument } from '@dxos/invariant';
|
|
11
|
+
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
12
12
|
import { type QueryResult } from '@dxos/protocols';
|
|
13
13
|
|
|
14
|
-
import
|
|
14
|
+
import { FunctionType } from './schema';
|
|
15
|
+
import { type Services } from './services';
|
|
15
16
|
|
|
16
17
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
17
18
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
@@ -44,8 +45,6 @@ export interface FunctionContext {
|
|
|
44
45
|
*/
|
|
45
46
|
space: SpaceAPI | undefined;
|
|
46
47
|
|
|
47
|
-
ai: AiServiceClient;
|
|
48
|
-
|
|
49
48
|
/**
|
|
50
49
|
* Resolves a service available to the function.
|
|
51
50
|
* @throws if the service is not available.
|
|
@@ -87,7 +86,12 @@ const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
|
87
86
|
// const _: SpaceAPI = {} as Space;
|
|
88
87
|
};
|
|
89
88
|
|
|
90
|
-
|
|
89
|
+
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
90
|
+
|
|
91
|
+
export type FunctionDefinition<T = any, O = any> = {
|
|
92
|
+
[typeId]: true;
|
|
93
|
+
// TODO(dmaretskyi): Use `key` for FQN and `name` for human-readable-name.
|
|
94
|
+
key: string;
|
|
91
95
|
name: string;
|
|
92
96
|
description?: string;
|
|
93
97
|
inputSchema: Schema.Schema<T, any>;
|
|
@@ -95,14 +99,17 @@ export type FunctionDefinition<T = {}, O = any> = {
|
|
|
95
99
|
handler: FunctionHandler<T, O>;
|
|
96
100
|
};
|
|
97
101
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
export const defineFunction: {
|
|
103
|
+
<I, O>(params: {
|
|
104
|
+
// TODO(dmaretskyi): Make `key` required.
|
|
105
|
+
key?: string;
|
|
106
|
+
name: string;
|
|
107
|
+
description?: string;
|
|
108
|
+
inputSchema: Schema.Schema<I, any>;
|
|
109
|
+
outputSchema?: Schema.Schema<O, any>;
|
|
110
|
+
handler: Types.NoInfer<FunctionHandler<I, O>>;
|
|
111
|
+
}): FunctionDefinition<I, O>;
|
|
112
|
+
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler }) => {
|
|
106
113
|
if (!Schema.isSchema(inputSchema)) {
|
|
107
114
|
throw new Error('Input schema must be a valid schema');
|
|
108
115
|
}
|
|
@@ -110,11 +117,85 @@ export const defineFunction = <T, O>({
|
|
|
110
117
|
throw new Error('Handler must be a function');
|
|
111
118
|
}
|
|
112
119
|
|
|
120
|
+
// Captures the function definition location.
|
|
121
|
+
const limit = Error.stackTraceLimit;
|
|
122
|
+
Error.stackTraceLimit = 2;
|
|
123
|
+
const traceError = new Error();
|
|
124
|
+
Error.stackTraceLimit = limit;
|
|
125
|
+
let cache: false | string = false;
|
|
126
|
+
const captureStackTrace = () => {
|
|
127
|
+
if (cache !== false) {
|
|
128
|
+
return cache;
|
|
129
|
+
}
|
|
130
|
+
if (traceError.stack !== undefined) {
|
|
131
|
+
const stack = traceError.stack.split('\n');
|
|
132
|
+
if (stack[2] !== undefined) {
|
|
133
|
+
cache = stack[2].trim();
|
|
134
|
+
return cache;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const handlerWithSpan = (...args: any[]) => {
|
|
140
|
+
const result = (handler as any)(...args);
|
|
141
|
+
if (Effect.isEffect(result)) {
|
|
142
|
+
return Effect.withSpan(result, `${key ?? name}`, {
|
|
143
|
+
captureStackTrace,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
};
|
|
148
|
+
|
|
113
149
|
return {
|
|
150
|
+
[typeId]: true,
|
|
151
|
+
key: key ?? name,
|
|
114
152
|
name,
|
|
115
153
|
description,
|
|
116
154
|
inputSchema,
|
|
117
155
|
outputSchema,
|
|
118
|
-
handler,
|
|
156
|
+
handler: handlerWithSpan,
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export const FunctionDefinition = {
|
|
161
|
+
make: defineFunction,
|
|
162
|
+
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
163
|
+
return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
|
|
164
|
+
},
|
|
165
|
+
serialize: (functionDef: FunctionDefinition.Any): FunctionType => {
|
|
166
|
+
assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
|
|
167
|
+
return serializeFunction(functionDef);
|
|
168
|
+
},
|
|
169
|
+
deserialize: (functionObj: FunctionType): FunctionDefinition.Any => {
|
|
170
|
+
assertArgument(Obj.instanceOf(FunctionType, functionObj), 'functionObj');
|
|
171
|
+
return deserializeFunction(functionObj);
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
export declare namespace FunctionDefinition {
|
|
175
|
+
export type Any = FunctionDefinition<any, any>;
|
|
176
|
+
export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
|
|
177
|
+
export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const serializeFunction = (functionDef: FunctionDefinition<any, any>): FunctionType =>
|
|
181
|
+
Obj.make(FunctionType, {
|
|
182
|
+
key: functionDef.name,
|
|
183
|
+
name: functionDef.name,
|
|
184
|
+
version: '0.1.0',
|
|
185
|
+
description: functionDef.description,
|
|
186
|
+
inputSchema: Type.toJsonSchema(functionDef.inputSchema),
|
|
187
|
+
outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
export const deserializeFunction = (functionObj: FunctionType): FunctionDefinition<unknown, unknown> => {
|
|
191
|
+
return {
|
|
192
|
+
[typeId]: true,
|
|
193
|
+
// TODO(dmaretskyi): Fix key.
|
|
194
|
+
key: functionObj.key ?? functionObj.name,
|
|
195
|
+
name: functionObj.name,
|
|
196
|
+
description: functionObj.description,
|
|
197
|
+
inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
|
|
198
|
+
outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
|
|
199
|
+
handler: () => {},
|
|
119
200
|
};
|
|
120
201
|
};
|
package/src/index.ts
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export * from './errors';
|
|
5
6
|
export * from './handler';
|
|
6
7
|
export * from './schema';
|
|
7
8
|
export * from './trace';
|
|
8
9
|
export * from './types';
|
|
9
10
|
export * from './url';
|
|
11
|
+
export * from './triggers';
|
|
10
12
|
export * from './services';
|
|
11
13
|
export * from './executor';
|
|
12
|
-
export * from './
|
|
13
|
-
|
|
14
|
-
// Blow up cache
|
|
14
|
+
export * as exampleFunctions from './examples';
|