@dxos/functions 0.5.5-main.dc01469 → 0.5.5
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/{chunk-ERWZ4JUZ.mjs → chunk-4D4I3YMJ.mjs} +31 -31
- package/dist/lib/browser/{chunk-ERWZ4JUZ.mjs.map → chunk-4D4I3YMJ.mjs.map} +2 -2
- package/dist/lib/browser/{chunk-SXZ5DYJG.mjs → chunk-ERL6PHMU.mjs} +21 -26
- package/dist/lib/browser/{chunk-SXZ5DYJG.mjs.map → chunk-ERL6PHMU.mjs.map} +3 -3
- package/dist/lib/browser/index.mjs +2 -2
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +5 -8
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/browser/types.mjs +1 -1
- package/dist/lib/node/{chunk-BLLSDTKZ.cjs → chunk-3UYUR5N5.cjs} +34 -34
- package/dist/lib/node/{chunk-BLLSDTKZ.cjs.map → chunk-3UYUR5N5.cjs.map} +2 -2
- package/dist/lib/node/{chunk-RPHL3ORN.cjs → chunk-INM6XAL7.cjs} +37 -42
- package/dist/lib/node/{chunk-RPHL3ORN.cjs.map → chunk-INM6XAL7.cjs.map} +3 -3
- package/dist/lib/node/index.cjs +10 -10
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +11 -14
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node/types.cjs +5 -5
- package/dist/lib/node/types.cjs.map +1 -1
- package/dist/types/src/testing/setup.d.ts.map +1 -1
- package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +38 -27
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +15 -15
- package/src/runtime/dev-server.test.ts +2 -2
- package/src/runtime/scheduler.test.ts +2 -4
- package/src/testing/setup.ts +3 -5
- package/src/testing/types.ts +1 -1
- package/src/trigger/type/websocket-trigger.ts +8 -12
- package/src/types.ts +31 -31
package/dist/lib/node/types.cjs
CHANGED
|
@@ -18,13 +18,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var types_exports = {};
|
|
20
20
|
__export(types_exports, {
|
|
21
|
-
FUNCTION_SCHEMA: () =>
|
|
22
|
-
FunctionDef: () =>
|
|
23
|
-
FunctionManifestSchema: () =>
|
|
24
|
-
FunctionTrigger: () =>
|
|
21
|
+
FUNCTION_SCHEMA: () => import_chunk_3UYUR5N5.FUNCTION_SCHEMA,
|
|
22
|
+
FunctionDef: () => import_chunk_3UYUR5N5.FunctionDef,
|
|
23
|
+
FunctionManifestSchema: () => import_chunk_3UYUR5N5.FunctionManifestSchema,
|
|
24
|
+
FunctionTrigger: () => import_chunk_3UYUR5N5.FunctionTrigger
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(types_exports);
|
|
27
|
-
var
|
|
27
|
+
var import_chunk_3UYUR5N5 = require("./chunk-3UYUR5N5.cjs");
|
|
28
28
|
// Annotate the CommonJS export names for ESM import in node:
|
|
29
29
|
0 && (module.exports = {
|
|
30
30
|
FUNCTION_SCHEMA,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["types.cjs"],
|
|
4
|
-
"sourcesContent": ["import {\n FUNCTION_SCHEMA,\n FunctionDef,\n FunctionManifestSchema,\n FunctionTrigger\n} from \"./chunk-
|
|
4
|
+
"sourcesContent": ["import {\n FUNCTION_SCHEMA,\n FunctionDef,\n FunctionManifestSchema,\n FunctionTrigger\n} from \"./chunk-3UYUR5N5.cjs\";\nexport {\n FUNCTION_SCHEMA,\n FunctionDef,\n FunctionManifestSchema,\n FunctionTrigger\n};\n//# sourceMappingURL=types.cjs.map\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAKO;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../../../src/testing/setup.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../../../src/testing/setup.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAKxD,OAAO,EAAa,KAAK,gBAAgB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAMzE,MAAM,MAAM,0BAA0B,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,CAAC,CAAC;AAIrG,eAAO,MAAM,wBAAwB,gBAAuB,WAAW,UAAS,MAAM,WAAe,MAAM,sBAW1G,CAAC;AAEF,eAAO,MAAM,qBAAqB,gBACnB,WAAW,qBACL,0BAA0B,KAC5C,QAAQ,MAAM,CAahB,CAAC;AAEF,eAAO,MAAM,kBAAkB,gBAChB,WAAW,qBACL,0BAA0B,YACnC,gBAAgB;;;mCASa,KAAK;EAI7C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket-trigger.d.ts","sourceRoot":"","sources":["../../../../../src/trigger/type/websocket-trigger.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEhF,UAAU,uBAAuB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAE,cAAc,CAAC,gBAAgB,EAAE,uBAAuB,
|
|
1
|
+
{"version":3,"file":"websocket-trigger.d.ts","sourceRoot":"","sources":["../../../../../src/trigger/type/websocket-trigger.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEhF,UAAU,uBAAuB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAE,cAAc,CAAC,gBAAgB,EAAE,uBAAuB,CAoE5F,CAAC"}
|
|
@@ -6,33 +6,44 @@ import { S } from '@dxos/echo-schema';
|
|
|
6
6
|
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
7
7
|
*/
|
|
8
8
|
export type FunctionTriggerType = 'subscription' | 'timer' | 'webhook' | 'websocket';
|
|
9
|
-
declare const SubscriptionTriggerSchema: S.mutable<S.
|
|
10
|
-
type: S.
|
|
11
|
-
filter: S.
|
|
12
|
-
type:
|
|
13
|
-
props: S.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
declare const SubscriptionTriggerSchema: S.mutable<S.struct<{
|
|
10
|
+
type: S.literal<["subscription"]>;
|
|
11
|
+
filter: S.array<S.struct<{
|
|
12
|
+
type: S.$string;
|
|
13
|
+
props: S.PropertySignature<"?:", {
|
|
14
|
+
readonly [x: string]: any;
|
|
15
|
+
} | undefined, never, "?:", {
|
|
16
|
+
readonly [x: string]: any;
|
|
17
|
+
} | undefined, never>;
|
|
18
18
|
}>>;
|
|
19
|
+
options: S.PropertySignature<"?:", {
|
|
20
|
+
readonly deep?: boolean | undefined;
|
|
21
|
+
readonly delay?: number | undefined;
|
|
22
|
+
} | undefined, never, "?:", {
|
|
23
|
+
readonly deep?: boolean | undefined;
|
|
24
|
+
readonly delay?: number | undefined;
|
|
25
|
+
} | undefined, never>;
|
|
19
26
|
}>>;
|
|
20
27
|
export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;
|
|
21
|
-
declare const TimerTriggerSchema: S.mutable<S.
|
|
22
|
-
type: S.
|
|
23
|
-
cron:
|
|
28
|
+
declare const TimerTriggerSchema: S.mutable<S.struct<{
|
|
29
|
+
type: S.literal<["timer"]>;
|
|
30
|
+
cron: S.$string;
|
|
24
31
|
}>>;
|
|
25
32
|
export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
|
|
26
|
-
declare const WebhookTriggerSchema: S.mutable<S.
|
|
27
|
-
type: S.
|
|
28
|
-
method:
|
|
29
|
-
port: S.
|
|
33
|
+
declare const WebhookTriggerSchema: S.mutable<S.struct<{
|
|
34
|
+
type: S.literal<["webhook"]>;
|
|
35
|
+
method: S.$string;
|
|
36
|
+
port: S.PropertySignature<"?:", number | undefined, never, "?:", number | undefined, never>;
|
|
30
37
|
}>>;
|
|
31
38
|
export type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;
|
|
32
|
-
declare const WebsocketTriggerSchema: S.mutable<S.
|
|
33
|
-
type: S.
|
|
34
|
-
url:
|
|
35
|
-
init: S.
|
|
39
|
+
declare const WebsocketTriggerSchema: S.mutable<S.struct<{
|
|
40
|
+
type: S.literal<["websocket"]>;
|
|
41
|
+
url: S.$string;
|
|
42
|
+
init: S.PropertySignature<"?:", {
|
|
43
|
+
readonly [x: string]: any;
|
|
44
|
+
} | undefined, never, "?:", {
|
|
45
|
+
readonly [x: string]: any;
|
|
46
|
+
} | undefined, never>;
|
|
36
47
|
}>>;
|
|
37
48
|
export type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;
|
|
38
49
|
export type TriggerSpec = TimerTrigger | WebhookTrigger | WebsocketTrigger | SubscriptionTrigger;
|
|
@@ -92,8 +103,8 @@ export declare class FunctionTrigger extends FunctionTrigger_base {
|
|
|
92
103
|
/**
|
|
93
104
|
* Function manifest file.
|
|
94
105
|
*/
|
|
95
|
-
export declare const FunctionManifestSchema: S.
|
|
96
|
-
functions: S.
|
|
106
|
+
export declare const FunctionManifestSchema: S.struct<{
|
|
107
|
+
functions: S.PropertySignature<"?:", ({
|
|
97
108
|
handler: string;
|
|
98
109
|
uri: string;
|
|
99
110
|
description?: string | undefined;
|
|
@@ -105,7 +116,7 @@ export declare const FunctionManifestSchema: S.Struct<{
|
|
|
105
116
|
readonly source: string;
|
|
106
117
|
}[];
|
|
107
118
|
} | undefined;
|
|
108
|
-
}, {
|
|
119
|
+
})[] | undefined, never, "?:", ({
|
|
109
120
|
handler: string;
|
|
110
121
|
uri: string;
|
|
111
122
|
description?: string | undefined;
|
|
@@ -117,8 +128,8 @@ export declare const FunctionManifestSchema: S.Struct<{
|
|
|
117
128
|
readonly source: string;
|
|
118
129
|
}[];
|
|
119
130
|
} | undefined;
|
|
120
|
-
}, never
|
|
121
|
-
triggers: S.
|
|
131
|
+
})[] | undefined, never>;
|
|
132
|
+
triggers: S.PropertySignature<"?:", ({
|
|
122
133
|
function: string;
|
|
123
134
|
enabled?: boolean | undefined;
|
|
124
135
|
meta?: {
|
|
@@ -157,7 +168,7 @@ export declare const FunctionManifestSchema: S.Struct<{
|
|
|
157
168
|
readonly source: string;
|
|
158
169
|
}[];
|
|
159
170
|
} | undefined;
|
|
160
|
-
}, {
|
|
171
|
+
})[] | undefined, never, "?:", ({
|
|
161
172
|
function: string;
|
|
162
173
|
enabled?: boolean | undefined;
|
|
163
174
|
meta?: {
|
|
@@ -196,7 +207,7 @@ export declare const FunctionManifestSchema: S.Struct<{
|
|
|
196
207
|
readonly source: string;
|
|
197
208
|
}[];
|
|
198
209
|
} | undefined;
|
|
199
|
-
}, never
|
|
210
|
+
})[] | undefined, never>;
|
|
200
211
|
}>;
|
|
201
212
|
export type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;
|
|
202
213
|
export declare const FUNCTION_SCHEMA: (typeof FunctionDef | typeof FunctionTrigger)[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAa,CAAC,EAAe,MAAM,mBAAmB,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,OAAO,GAAG,SAAS,GAAG,WAAW,CAAC;AAErF,QAAA,MAAM,yBAAyB
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAa,CAAC,EAAe,MAAM,mBAAmB,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,OAAO,GAAG,SAAS,GAAG,WAAW,CAAC;AAErF,QAAA,MAAM,yBAAyB;;;;;;;;;;;;;;;;;GAmB9B,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,yBAAyB,CAAC,CAAC;AAElF,QAAA,MAAM,kBAAkB;;;GAKvB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAEpE,QAAA,MAAM,oBAAoB;;;;GAOzB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAExE,QAAA,MAAM,sBAAsB;;;;;;;;GAM3B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAS5E,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,cAAc,GAAG,gBAAgB,GAAG,mBAAmB,CAAC;;;;;;;;;AAEjG;;GAEG;AACH,qBAAa,WAAY,SAAQ,gBAQ/B;CAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEL;;GAEG;AACH,qBAAa,eAAgB,SAAQ,oBASnC;CAAG;AAEL;;GAEG;AACH,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAGjC,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAG5E,eAAO,MAAM,eAAe,iDAAiC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.5.5
|
|
3
|
+
"version": "0.5.5",
|
|
4
4
|
"description": "Functions API and runtime.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -45,27 +45,27 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@preact/signals-core": "^1.6.0",
|
|
47
47
|
"cron": "^3.1.6",
|
|
48
|
-
"effect": "^
|
|
48
|
+
"effect": "^2.4.19",
|
|
49
49
|
"express": "^4.19.2",
|
|
50
50
|
"get-port-please": "^3.1.1",
|
|
51
51
|
"ws": "^8.14.2",
|
|
52
|
-
"@
|
|
53
|
-
"@dxos/
|
|
54
|
-
"@dxos/context": "0.5.5
|
|
55
|
-
"@dxos/echo-db": "0.5.5
|
|
56
|
-
"@dxos/
|
|
57
|
-
"@dxos/
|
|
58
|
-
"@dxos/
|
|
59
|
-
"@
|
|
60
|
-
"@dxos/log": "0.5.5
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/util": "0.5.5
|
|
63
|
-
"@dxos/
|
|
52
|
+
"@dxos/client": "0.5.5",
|
|
53
|
+
"@dxos/async": "0.5.5",
|
|
54
|
+
"@dxos/context": "0.5.5",
|
|
55
|
+
"@dxos/echo-db": "0.5.5",
|
|
56
|
+
"@dxos/echo-schema": "0.5.5",
|
|
57
|
+
"@dxos/invariant": "0.5.5",
|
|
58
|
+
"@dxos/keys": "0.5.5",
|
|
59
|
+
"@braneframe/types": "0.5.5",
|
|
60
|
+
"@dxos/log": "0.5.5",
|
|
61
|
+
"@dxos/node-std": "0.5.5",
|
|
62
|
+
"@dxos/util": "0.5.5",
|
|
63
|
+
"@dxos/protocols": "0.5.5"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@types/express": "^4.17.17",
|
|
67
67
|
"@types/ws": "^7.4.0",
|
|
68
|
-
"@dxos/agent": "0.5.5
|
|
68
|
+
"@dxos/agent": "0.5.5"
|
|
69
69
|
},
|
|
70
70
|
"publishConfig": {
|
|
71
71
|
"access": "public"
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { expect } from 'chai';
|
|
6
|
-
import { getRandomPort } from 'get-port-please';
|
|
7
6
|
import path from 'path';
|
|
8
7
|
|
|
9
8
|
import { sleep, waitForCondition } from '@dxos/async';
|
|
@@ -17,6 +16,7 @@ import { createFunctionRuntime, testFunctionManifest } from '../testing';
|
|
|
17
16
|
import { initFunctionsPlugin } from '../testing/plugin-init';
|
|
18
17
|
|
|
19
18
|
describe('dev server', () => {
|
|
19
|
+
let port = 7201;
|
|
20
20
|
let client: Client;
|
|
21
21
|
let testBuilder: TestBuilder;
|
|
22
22
|
before(async () => {
|
|
@@ -67,7 +67,7 @@ describe('dev server', () => {
|
|
|
67
67
|
const registry = new FunctionRegistry(client);
|
|
68
68
|
const server = new DevServer(client, registry, {
|
|
69
69
|
baseDir: path.join(__dirname, '../testing'),
|
|
70
|
-
port:
|
|
70
|
+
port: port++,
|
|
71
71
|
});
|
|
72
72
|
const space = await client.spaces.create();
|
|
73
73
|
// TODO(burdon): Doesn't shut down cleanly.
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { expect } from 'chai';
|
|
6
|
-
import { getRandomPort } from 'get-port-please';
|
|
7
6
|
import WebSocket from 'ws';
|
|
8
7
|
|
|
9
8
|
import { Trigger } from '@dxos/async';
|
|
@@ -109,7 +108,6 @@ describe('scheduler', () => {
|
|
|
109
108
|
});
|
|
110
109
|
|
|
111
110
|
test('websocket', async () => {
|
|
112
|
-
const port = await getRandomPort('127.0.0.1');
|
|
113
111
|
const manifest: FunctionManifest = {
|
|
114
112
|
functions: [
|
|
115
113
|
{
|
|
@@ -125,7 +123,7 @@ describe('scheduler', () => {
|
|
|
125
123
|
spec: {
|
|
126
124
|
type: 'websocket',
|
|
127
125
|
// url: 'https://hub.dxos.network/api/mailbox/test',
|
|
128
|
-
url:
|
|
126
|
+
url: 'http://localhost:8081',
|
|
129
127
|
init: {
|
|
130
128
|
type: 'sync',
|
|
131
129
|
},
|
|
@@ -143,7 +141,7 @@ describe('scheduler', () => {
|
|
|
143
141
|
|
|
144
142
|
// Test server.
|
|
145
143
|
setTimeout(() => {
|
|
146
|
-
const wss = new WebSocket.Server({ port });
|
|
144
|
+
const wss = new WebSocket.Server({ port: 8081 });
|
|
147
145
|
wss.on('connection', (ws: WebSocket) => {
|
|
148
146
|
ws.on('message', (data) => {
|
|
149
147
|
const info = JSON.parse(new TextDecoder().decode(data as ArrayBuffer));
|
package/src/testing/setup.ts
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { getRandomPort } from 'get-port-please';
|
|
6
5
|
import path from 'node:path';
|
|
7
6
|
|
|
8
7
|
import { waitForCondition } from '@dxos/async';
|
|
@@ -17,6 +16,8 @@ import { DevServer, type DevServerOptions, Scheduler } from '../runtime';
|
|
|
17
16
|
import { TriggerRegistry } from '../trigger';
|
|
18
17
|
import { FunctionDef, FunctionTrigger } from '../types';
|
|
19
18
|
|
|
19
|
+
let functionsPort = 8081;
|
|
20
|
+
|
|
20
21
|
export type FunctionsPluginInitializer = (client: Client) => Promise<{ close: () => Promise<void> }>;
|
|
21
22
|
|
|
22
23
|
// TODO(burdon): Extend/wrap TestBuilder.
|
|
@@ -28,7 +29,6 @@ export const createInitializedClients = async (testBuilder: TestBuilder, count:
|
|
|
28
29
|
clients.map(async (client, index) => {
|
|
29
30
|
await client.initialize();
|
|
30
31
|
await client.halo.createIdentity({ displayName: `Peer ${index}` });
|
|
31
|
-
await client.spaces.isReady;
|
|
32
32
|
client.addSchema(FunctionDef, FunctionTrigger, TestType);
|
|
33
33
|
return client;
|
|
34
34
|
}),
|
|
@@ -39,11 +39,10 @@ export const createFunctionRuntime = async (
|
|
|
39
39
|
testBuilder: TestBuilder,
|
|
40
40
|
pluginInitializer: FunctionsPluginInitializer,
|
|
41
41
|
): Promise<Client> => {
|
|
42
|
-
const functionsPort = await getRandomPort('127.0.0.1');
|
|
43
42
|
const config = new Config({
|
|
44
43
|
runtime: {
|
|
45
44
|
agent: {
|
|
46
|
-
plugins: [{ id: 'dxos.org/agent/plugin/functions', config: { port: functionsPort } }],
|
|
45
|
+
plugins: [{ id: 'dxos.org/agent/plugin/functions', config: { port: functionsPort++ } }],
|
|
47
46
|
},
|
|
48
47
|
},
|
|
49
48
|
});
|
|
@@ -93,7 +92,6 @@ const startDevServer = async (
|
|
|
93
92
|
) => {
|
|
94
93
|
const server = new DevServer(client, functionRegistry, {
|
|
95
94
|
baseDir: path.join(__dirname, '../testing'),
|
|
96
|
-
port: await getRandomPort('127.0.0.1'),
|
|
97
95
|
...options,
|
|
98
96
|
});
|
|
99
97
|
await server.start();
|
package/src/testing/types.ts
CHANGED
|
@@ -30,7 +30,6 @@ export const createWebsocketTrigger: TriggerFactory<WebsocketTrigger, WebsocketT
|
|
|
30
30
|
) => {
|
|
31
31
|
const { url, init } = spec;
|
|
32
32
|
|
|
33
|
-
let wasOpen = false;
|
|
34
33
|
let ws: WebSocket;
|
|
35
34
|
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
|
|
36
35
|
const open = new Trigger<boolean>();
|
|
@@ -50,18 +49,18 @@ export const createWebsocketTrigger: TriggerFactory<WebsocketTrigger, WebsocketT
|
|
|
50
49
|
log.info('closed', { url, code: event.code });
|
|
51
50
|
// Reconnect if server closes (e.g., CF restart).
|
|
52
51
|
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
|
|
53
|
-
if (event.code === 1006
|
|
52
|
+
if (event.code === 1006) {
|
|
54
53
|
setTimeout(async () => {
|
|
55
54
|
log.info(`reconnecting in ${options.retryDelay}s...`, { url });
|
|
56
55
|
await createWebsocketTrigger(ctx, space, spec, callback, options);
|
|
57
56
|
}, options.retryDelay * 1_000);
|
|
58
57
|
}
|
|
58
|
+
|
|
59
59
|
open.wake(false);
|
|
60
60
|
},
|
|
61
61
|
|
|
62
62
|
onerror: (event) => {
|
|
63
63
|
log.catch(event.error, { url });
|
|
64
|
-
open.wake(false);
|
|
65
64
|
},
|
|
66
65
|
|
|
67
66
|
onmessage: async (event) => {
|
|
@@ -76,17 +75,14 @@ export const createWebsocketTrigger: TriggerFactory<WebsocketTrigger, WebsocketT
|
|
|
76
75
|
} satisfies Partial<WebSocket>);
|
|
77
76
|
|
|
78
77
|
const isOpen = await open.wait();
|
|
79
|
-
if (ctx.disposed) {
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
78
|
if (isOpen) {
|
|
83
|
-
wasOpen = true;
|
|
84
79
|
break;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
} else {
|
|
81
|
+
const wait = Math.pow(attempt, 2) * options.retryDelay;
|
|
82
|
+
if (attempt < options.maxAttempts) {
|
|
83
|
+
log.warn(`failed to connect; trying again in ${wait}s`, { attempt });
|
|
84
|
+
await sleep(wait * 1_000);
|
|
85
|
+
}
|
|
90
86
|
}
|
|
91
87
|
}
|
|
92
88
|
|
package/src/types.ts
CHANGED
|
@@ -13,21 +13,21 @@ import { RawObject, S, TypedObject } from '@dxos/echo-schema';
|
|
|
13
13
|
export type FunctionTriggerType = 'subscription' | 'timer' | 'webhook' | 'websocket';
|
|
14
14
|
|
|
15
15
|
const SubscriptionTriggerSchema = S.mutable(
|
|
16
|
-
S.
|
|
17
|
-
type: S.
|
|
16
|
+
S.struct({
|
|
17
|
+
type: S.literal('subscription'),
|
|
18
18
|
// TODO(burdon): Define query DSL (from ECHO).
|
|
19
|
-
filter: S.
|
|
20
|
-
S.
|
|
21
|
-
type: S.
|
|
22
|
-
props: S.optional(S.
|
|
19
|
+
filter: S.array(
|
|
20
|
+
S.struct({
|
|
21
|
+
type: S.string,
|
|
22
|
+
props: S.optional(S.record(S.string, S.any)),
|
|
23
23
|
}),
|
|
24
24
|
),
|
|
25
25
|
options: S.optional(
|
|
26
|
-
S.
|
|
26
|
+
S.struct({
|
|
27
27
|
// Watch changes to object (not just creation).
|
|
28
|
-
deep: S.optional(S.
|
|
28
|
+
deep: S.optional(S.boolean),
|
|
29
29
|
// Debounce changes (delay in ms).
|
|
30
|
-
delay: S.optional(S.
|
|
30
|
+
delay: S.optional(S.number),
|
|
31
31
|
}),
|
|
32
32
|
),
|
|
33
33
|
}),
|
|
@@ -36,36 +36,36 @@ const SubscriptionTriggerSchema = S.mutable(
|
|
|
36
36
|
export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;
|
|
37
37
|
|
|
38
38
|
const TimerTriggerSchema = S.mutable(
|
|
39
|
-
S.
|
|
40
|
-
type: S.
|
|
41
|
-
cron: S.
|
|
39
|
+
S.struct({
|
|
40
|
+
type: S.literal('timer'),
|
|
41
|
+
cron: S.string,
|
|
42
42
|
}),
|
|
43
43
|
);
|
|
44
44
|
|
|
45
45
|
export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
|
|
46
46
|
|
|
47
47
|
const WebhookTriggerSchema = S.mutable(
|
|
48
|
-
S.
|
|
49
|
-
type: S.
|
|
50
|
-
method: S.
|
|
48
|
+
S.struct({
|
|
49
|
+
type: S.literal('webhook'),
|
|
50
|
+
method: S.string,
|
|
51
51
|
// Assigned port.
|
|
52
|
-
port: S.optional(S.
|
|
52
|
+
port: S.optional(S.number),
|
|
53
53
|
}),
|
|
54
54
|
);
|
|
55
55
|
|
|
56
56
|
export type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;
|
|
57
57
|
|
|
58
58
|
const WebsocketTriggerSchema = S.mutable(
|
|
59
|
-
S.
|
|
60
|
-
type: S.
|
|
61
|
-
url: S.
|
|
62
|
-
init: S.optional(S.
|
|
59
|
+
S.struct({
|
|
60
|
+
type: S.literal('websocket'),
|
|
61
|
+
url: S.string,
|
|
62
|
+
init: S.optional(S.record(S.string, S.any)),
|
|
63
63
|
}),
|
|
64
64
|
);
|
|
65
65
|
|
|
66
66
|
export type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;
|
|
67
67
|
|
|
68
|
-
const TriggerSpecSchema = S.
|
|
68
|
+
const TriggerSpecSchema = S.union(
|
|
69
69
|
TimerTriggerSchema,
|
|
70
70
|
WebhookTriggerSchema,
|
|
71
71
|
WebsocketTriggerSchema,
|
|
@@ -81,10 +81,10 @@ export class FunctionDef extends TypedObject({
|
|
|
81
81
|
typename: 'dxos.org/type/FunctionDef',
|
|
82
82
|
version: '0.1.0',
|
|
83
83
|
})({
|
|
84
|
-
uri: S.
|
|
85
|
-
description: S.optional(S.
|
|
86
|
-
route: S.
|
|
87
|
-
handler: S.
|
|
84
|
+
uri: S.string,
|
|
85
|
+
description: S.optional(S.string),
|
|
86
|
+
route: S.string,
|
|
87
|
+
handler: S.string,
|
|
88
88
|
}) {}
|
|
89
89
|
|
|
90
90
|
/**
|
|
@@ -94,19 +94,19 @@ export class FunctionTrigger extends TypedObject({
|
|
|
94
94
|
typename: 'dxos.org/type/FunctionTrigger',
|
|
95
95
|
version: '0.1.0',
|
|
96
96
|
})({
|
|
97
|
-
enabled: S.optional(S.
|
|
98
|
-
function: S.
|
|
97
|
+
enabled: S.optional(S.boolean),
|
|
98
|
+
function: S.string.pipe(S.description('Function URI.')),
|
|
99
99
|
// The `meta` property is merged into the event data passed to the function.
|
|
100
|
-
meta: S.optional(S.mutable(S.
|
|
100
|
+
meta: S.optional(S.mutable(S.any)),
|
|
101
101
|
spec: TriggerSpecSchema,
|
|
102
102
|
}) {}
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
105
|
* Function manifest file.
|
|
106
106
|
*/
|
|
107
|
-
export const FunctionManifestSchema = S.
|
|
108
|
-
functions: S.optional(S.mutable(S.
|
|
109
|
-
triggers: S.optional(S.mutable(S.
|
|
107
|
+
export const FunctionManifestSchema = S.struct({
|
|
108
|
+
functions: S.optional(S.mutable(S.array(RawObject(FunctionDef)))),
|
|
109
|
+
triggers: S.optional(S.mutable(S.array(RawObject(FunctionTrigger)))),
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
export type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;
|