@dxos/functions 0.5.3-main.a3ea5f1 → 0.5.3-main.ac5e73c
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 +301 -282
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +303 -286
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/{function → registry}/function-registry.d.ts +4 -4
- package/dist/types/src/registry/function-registry.d.ts.map +1 -0
- package/dist/types/src/registry/function-registry.test.d.ts.map +1 -0
- package/dist/types/src/registry/index.d.ts.map +1 -0
- package/dist/types/src/runtime/dev-server.d.ts +1 -1
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.d.ts +1 -2
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
- package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -1
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +19 -33
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +18 -31
- package/schema/functions.json +9 -18
- package/src/index.ts +1 -1
- package/src/{function → registry}/function-registry.test.ts +10 -10
- package/src/registry/function-registry.ts +84 -0
- package/src/runtime/dev-server.test.ts +2 -2
- package/src/runtime/dev-server.ts +6 -5
- package/src/runtime/scheduler.test.ts +1 -1
- package/src/runtime/scheduler.ts +8 -16
- package/src/testing/functions-integration.test.ts +1 -1
- package/src/testing/setup.ts +1 -1
- package/src/trigger/trigger-registry.test.ts +34 -60
- package/src/trigger/trigger-registry.ts +6 -19
- package/src/trigger/type/subscription-trigger.ts +10 -17
- package/src/types.ts +10 -12
- package/dist/lib/browser/chunk-366QG6IX.mjs +0 -81
- package/dist/lib/browser/chunk-366QG6IX.mjs.map +0 -7
- package/dist/lib/browser/types.mjs +0 -12
- package/dist/lib/browser/types.mjs.map +0 -7
- package/dist/lib/node/chunk-3VSJ57ZZ.cjs +0 -97
- package/dist/lib/node/chunk-3VSJ57ZZ.cjs.map +0 -7
- package/dist/lib/node/types.cjs +0 -33
- package/dist/lib/node/types.cjs.map +0 -7
- package/dist/types/src/function/function-registry.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
- package/dist/types/src/function/index.d.ts.map +0 -1
- package/dist/types/src/util.d.ts +0 -15
- package/dist/types/src/util.d.ts.map +0 -1
- package/dist/types/src/util.test.d.ts +0 -2
- package/dist/types/src/util.test.d.ts.map +0 -1
- package/src/function/function-registry.ts +0 -90
- package/src/util.test.ts +0 -43
- package/src/util.ts +0 -48
- /package/dist/types/src/{function → registry}/function-registry.test.d.ts +0 -0
- /package/dist/types/src/{function → registry}/index.d.ts +0 -0
- /package/src/{function → registry}/index.ts +0 -0
package/dist/lib/node/index.cjs
CHANGED
|
@@ -29,116 +29,189 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
var node_exports = {};
|
|
30
30
|
__export(node_exports, {
|
|
31
31
|
DevServer: () => DevServer,
|
|
32
|
-
FunctionDef: () =>
|
|
33
|
-
FunctionManifestSchema: () =>
|
|
32
|
+
FunctionDef: () => FunctionDef,
|
|
33
|
+
FunctionManifestSchema: () => FunctionManifestSchema,
|
|
34
34
|
FunctionRegistry: () => FunctionRegistry,
|
|
35
|
-
FunctionTrigger: () =>
|
|
35
|
+
FunctionTrigger: () => FunctionTrigger,
|
|
36
36
|
Scheduler: () => Scheduler,
|
|
37
37
|
TriggerRegistry: () => TriggerRegistry,
|
|
38
38
|
subscriptionHandler: () => subscriptionHandler
|
|
39
39
|
});
|
|
40
40
|
module.exports = __toCommonJS(node_exports);
|
|
41
|
-
var
|
|
41
|
+
var import_client = require("@dxos/client");
|
|
42
|
+
var import_log = require("@dxos/log");
|
|
43
|
+
var import_util = require("@dxos/util");
|
|
42
44
|
var import_async = require("@dxos/async");
|
|
43
45
|
var import_echo = require("@dxos/client/echo");
|
|
44
46
|
var import_context = require("@dxos/context");
|
|
45
47
|
var import_keys = require("@dxos/keys");
|
|
46
|
-
var import_log = require("@dxos/log");
|
|
47
|
-
var import_util = require("@dxos/util");
|
|
48
|
-
var import_client = require("@dxos/client");
|
|
49
|
-
var import_log2 = require("@dxos/log");
|
|
50
48
|
var import_util2 = require("@dxos/util");
|
|
49
|
+
var import_echo_schema = require("@dxos/echo-schema");
|
|
51
50
|
var import_express = __toESM(require("express"));
|
|
52
51
|
var import_get_port_please = require("get-port-please");
|
|
53
52
|
var import_node_path = require("node:path");
|
|
54
53
|
var import_async2 = require("@dxos/async");
|
|
55
54
|
var import_context2 = require("@dxos/context");
|
|
56
55
|
var import_invariant = require("@dxos/invariant");
|
|
57
|
-
var
|
|
56
|
+
var import_log2 = require("@dxos/log");
|
|
58
57
|
var import_node_path2 = __toESM(require("node:path"));
|
|
59
|
-
var import_async3 = require("@dxos/async");
|
|
60
58
|
var import_context3 = require("@dxos/context");
|
|
61
|
-
var
|
|
62
|
-
var
|
|
59
|
+
var import_log3 = require("@dxos/log");
|
|
60
|
+
var import_async3 = require("@dxos/async");
|
|
63
61
|
var import_echo2 = require("@dxos/client/echo");
|
|
64
62
|
var import_context4 = require("@dxos/context");
|
|
65
|
-
var import_echo_schema = require("@dxos/echo-schema");
|
|
66
63
|
var import_invariant2 = require("@dxos/invariant");
|
|
67
64
|
var import_keys2 = require("@dxos/keys");
|
|
68
|
-
var
|
|
65
|
+
var import_log4 = require("@dxos/log");
|
|
69
66
|
var import_util3 = require("@dxos/util");
|
|
70
67
|
var import_types = require("@braneframe/types");
|
|
71
|
-
var
|
|
68
|
+
var import_async4 = require("@dxos/async");
|
|
72
69
|
var import_echo_db = require("@dxos/echo-db");
|
|
73
|
-
var
|
|
70
|
+
var import_log5 = require("@dxos/log");
|
|
74
71
|
var import_cron = require("cron");
|
|
75
|
-
var
|
|
76
|
-
var
|
|
72
|
+
var import_async5 = require("@dxos/async");
|
|
73
|
+
var import_log6 = require("@dxos/log");
|
|
77
74
|
var import_get_port_please2 = require("get-port-please");
|
|
78
75
|
var import_node_http = __toESM(require("node:http"));
|
|
79
|
-
var
|
|
76
|
+
var import_log7 = require("@dxos/log");
|
|
80
77
|
var import_ws = __toESM(require("ws"));
|
|
81
|
-
var
|
|
82
|
-
var
|
|
83
|
-
var
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
78
|
+
var import_async6 = require("@dxos/async");
|
|
79
|
+
var import_log8 = require("@dxos/log");
|
|
80
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
81
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
82
|
+
}) : x)(function(x) {
|
|
83
|
+
if (typeof require !== "undefined")
|
|
84
|
+
return require.apply(this, arguments);
|
|
85
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
86
|
+
});
|
|
87
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
|
|
88
|
+
var subscriptionHandler = (handler) => {
|
|
89
|
+
return ({ event: { data }, context, ...rest }) => {
|
|
90
|
+
const { client } = context;
|
|
91
|
+
const space = data.spaceKey ? client.spaces.get(import_client.PublicKey.from(data.spaceKey)) : void 0;
|
|
92
|
+
const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util.nonNullable) : [];
|
|
93
|
+
if (!!data.spaceKey && !space) {
|
|
94
|
+
import_log.log.warn("invalid space", {
|
|
95
|
+
data
|
|
96
|
+
}, {
|
|
97
|
+
F: __dxlog_file,
|
|
98
|
+
L: 91,
|
|
99
|
+
S: void 0,
|
|
100
|
+
C: (f, a) => f(...a)
|
|
101
|
+
});
|
|
96
102
|
} else {
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
import_log.log.info("handler", {
|
|
104
|
+
space: space?.key.truncate(),
|
|
105
|
+
objects: objects?.length
|
|
106
|
+
}, {
|
|
107
|
+
F: __dxlog_file,
|
|
108
|
+
L: 93,
|
|
109
|
+
S: void 0,
|
|
110
|
+
C: (f, a) => f(...a)
|
|
111
|
+
});
|
|
99
112
|
}
|
|
100
|
-
|
|
101
|
-
|
|
113
|
+
return handler({
|
|
114
|
+
event: {
|
|
115
|
+
data: {
|
|
116
|
+
...data,
|
|
117
|
+
space,
|
|
118
|
+
objects
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
context,
|
|
122
|
+
...rest
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
var omitEchoId = (schema) => import_echo_schema.S.make(import_echo_schema.AST.omit(schema.ast, [
|
|
127
|
+
"id"
|
|
128
|
+
]));
|
|
129
|
+
var SubscriptionTriggerSchema = import_echo_schema.S.struct({
|
|
130
|
+
type: import_echo_schema.S.literal("subscription"),
|
|
131
|
+
// TODO(burdon): Define query DSL.
|
|
132
|
+
filter: import_echo_schema.S.array(import_echo_schema.S.struct({
|
|
133
|
+
type: import_echo_schema.S.string,
|
|
134
|
+
props: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any))
|
|
135
|
+
})),
|
|
136
|
+
options: import_echo_schema.S.optional(import_echo_schema.S.struct({
|
|
137
|
+
// Watch changes to object (not just creation).
|
|
138
|
+
deep: import_echo_schema.S.optional(import_echo_schema.S.boolean),
|
|
139
|
+
// Debounce changes (delay in ms).
|
|
140
|
+
delay: import_echo_schema.S.optional(import_echo_schema.S.number)
|
|
141
|
+
}))
|
|
142
|
+
});
|
|
143
|
+
var TimerTriggerSchema = import_echo_schema.S.struct({
|
|
144
|
+
type: import_echo_schema.S.literal("timer"),
|
|
145
|
+
cron: import_echo_schema.S.string
|
|
146
|
+
});
|
|
147
|
+
var WebhookTriggerSchema = import_echo_schema.S.mutable(import_echo_schema.S.struct({
|
|
148
|
+
type: import_echo_schema.S.literal("webhook"),
|
|
149
|
+
method: import_echo_schema.S.string,
|
|
150
|
+
// Assigned port.
|
|
151
|
+
port: import_echo_schema.S.optional(import_echo_schema.S.number)
|
|
152
|
+
}));
|
|
153
|
+
var WebsocketTriggerSchema = import_echo_schema.S.struct({
|
|
154
|
+
type: import_echo_schema.S.literal("websocket"),
|
|
155
|
+
url: import_echo_schema.S.string,
|
|
156
|
+
init: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any))
|
|
157
|
+
});
|
|
158
|
+
var TriggerSpecSchema = import_echo_schema.S.union(TimerTriggerSchema, WebhookTriggerSchema, WebsocketTriggerSchema, SubscriptionTriggerSchema);
|
|
159
|
+
var FunctionDef = class extends (0, import_echo_schema.TypedObject)({
|
|
160
|
+
typename: "dxos.org/type/FunctionDef",
|
|
161
|
+
version: "0.1.0"
|
|
162
|
+
})({
|
|
163
|
+
uri: import_echo_schema.S.string,
|
|
164
|
+
description: import_echo_schema.S.optional(import_echo_schema.S.string),
|
|
165
|
+
route: import_echo_schema.S.string,
|
|
166
|
+
// TODO(burdon): NPM/GitHub/Docker/CF URL?
|
|
167
|
+
handler: import_echo_schema.S.string
|
|
168
|
+
}) {
|
|
169
|
+
};
|
|
170
|
+
var FunctionTrigger = class extends (0, import_echo_schema.TypedObject)({
|
|
171
|
+
typename: "dxos.org/type/FunctionTrigger",
|
|
172
|
+
version: "0.1.0"
|
|
173
|
+
})({
|
|
174
|
+
function: import_echo_schema.S.string.pipe(import_echo_schema.S.description("Function ID/URI.")),
|
|
175
|
+
// Context passed to a function.
|
|
176
|
+
meta: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any)),
|
|
177
|
+
spec: TriggerSpecSchema
|
|
178
|
+
}) {
|
|
102
179
|
};
|
|
103
|
-
var
|
|
104
|
-
|
|
180
|
+
var FunctionManifestSchema = import_echo_schema.S.struct({
|
|
181
|
+
functions: import_echo_schema.S.optional(import_echo_schema.S.mutable(import_echo_schema.S.array(omitEchoId(FunctionDef)))),
|
|
182
|
+
triggers: import_echo_schema.S.optional(import_echo_schema.S.mutable(import_echo_schema.S.array(omitEchoId(FunctionTrigger))))
|
|
183
|
+
});
|
|
105
184
|
var FunctionRegistry = class extends import_context.Resource {
|
|
106
185
|
constructor(_client) {
|
|
107
186
|
super();
|
|
108
187
|
this._client = _client;
|
|
109
|
-
this._functionBySpaceKey = new
|
|
110
|
-
this.
|
|
188
|
+
this._functionBySpaceKey = new import_util2.ComplexMap(import_keys.PublicKey.hash);
|
|
189
|
+
this.onFunctionsRegistered = new import_async.Event();
|
|
111
190
|
}
|
|
112
191
|
getFunctions(space) {
|
|
113
192
|
return this._functionBySpaceKey.get(space.key) ?? [];
|
|
114
193
|
}
|
|
115
194
|
/**
|
|
116
|
-
*
|
|
195
|
+
* The method loads function definitions from the manifest into the space.
|
|
117
196
|
* We first load all the definitions from the space to deduplicate by functionId.
|
|
118
197
|
*/
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
functions: functions?.length ?? 0
|
|
123
|
-
}, {
|
|
124
|
-
F: __dxlog_file,
|
|
125
|
-
L: 39,
|
|
126
|
-
S: this,
|
|
127
|
-
C: (f, a) => f(...a)
|
|
128
|
-
});
|
|
129
|
-
if (!functions?.length) {
|
|
198
|
+
// TODO(burdon): This should not be space specific (they are static for the agent).
|
|
199
|
+
async register(space, manifest) {
|
|
200
|
+
if (!manifest.functions?.length) {
|
|
130
201
|
return;
|
|
131
202
|
}
|
|
132
|
-
if (!space.db.graph.runtimeSchemaRegistry.
|
|
133
|
-
space.db.graph.runtimeSchemaRegistry.registerSchema(
|
|
203
|
+
if (!space.db.graph.runtimeSchemaRegistry.isSchemaRegistered(FunctionDef)) {
|
|
204
|
+
space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionDef);
|
|
134
205
|
}
|
|
135
|
-
const { objects:
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
206
|
+
const { objects: existingDefinitions } = await space.db.query(import_echo.Filter.schema(FunctionDef)).run();
|
|
207
|
+
const newDefinitions = getNewDefinitions(manifest.functions, existingDefinitions);
|
|
208
|
+
const reactiveObjects = newDefinitions.map((template) => (0, import_echo.create)(FunctionDef, {
|
|
209
|
+
...template
|
|
210
|
+
}));
|
|
211
|
+
reactiveObjects.forEach((obj) => space.db.add(obj));
|
|
139
212
|
}
|
|
140
213
|
async _open() {
|
|
141
|
-
const
|
|
214
|
+
const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
142
215
|
for (const space of spaces) {
|
|
143
216
|
if (this._functionBySpaceKey.has(space.key)) {
|
|
144
217
|
continue;
|
|
@@ -149,65 +222,31 @@ var FunctionRegistry = class extends import_context.Resource {
|
|
|
149
222
|
if (this._ctx.disposed) {
|
|
150
223
|
break;
|
|
151
224
|
}
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
if (
|
|
155
|
-
registered.push(...
|
|
156
|
-
this.
|
|
225
|
+
const functionsSubscription = space.db.query(import_echo.Filter.schema(FunctionDef)).subscribe((definitions) => {
|
|
226
|
+
const newFunctions = getNewDefinitions(definitions.objects, registered);
|
|
227
|
+
if (newFunctions.length > 0) {
|
|
228
|
+
registered.push(...newFunctions);
|
|
229
|
+
this.onFunctionsRegistered.emit({
|
|
157
230
|
space,
|
|
158
|
-
|
|
231
|
+
newFunctions
|
|
159
232
|
});
|
|
160
233
|
}
|
|
161
|
-
})
|
|
234
|
+
});
|
|
235
|
+
this._ctx.onDispose(functionsSubscription);
|
|
162
236
|
}
|
|
163
237
|
});
|
|
164
|
-
this._ctx.onDispose(() =>
|
|
238
|
+
this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
|
|
165
239
|
}
|
|
166
240
|
async _close(_) {
|
|
167
241
|
this._functionBySpaceKey.clear();
|
|
168
242
|
}
|
|
169
243
|
};
|
|
170
|
-
var
|
|
171
|
-
|
|
172
|
-
return ({ event: { data }, context, ...rest }) => {
|
|
173
|
-
const { client } = context;
|
|
174
|
-
const space = data.spaceKey ? client.spaces.get(import_client.PublicKey.from(data.spaceKey)) : void 0;
|
|
175
|
-
const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util2.nonNullable) : [];
|
|
176
|
-
if (!!data.spaceKey && !space) {
|
|
177
|
-
import_log2.log.warn("invalid space", {
|
|
178
|
-
data
|
|
179
|
-
}, {
|
|
180
|
-
F: __dxlog_file2,
|
|
181
|
-
L: 91,
|
|
182
|
-
S: void 0,
|
|
183
|
-
C: (f, a) => f(...a)
|
|
184
|
-
});
|
|
185
|
-
} else {
|
|
186
|
-
import_log2.log.info("handler", {
|
|
187
|
-
space: space?.key.truncate(),
|
|
188
|
-
objects: objects?.length
|
|
189
|
-
}, {
|
|
190
|
-
F: __dxlog_file2,
|
|
191
|
-
L: 93,
|
|
192
|
-
S: void 0,
|
|
193
|
-
C: (f, a) => f(...a)
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
return handler({
|
|
197
|
-
event: {
|
|
198
|
-
data: {
|
|
199
|
-
...data,
|
|
200
|
-
space,
|
|
201
|
-
objects
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
context,
|
|
205
|
-
...rest
|
|
206
|
-
});
|
|
207
|
-
};
|
|
244
|
+
var getNewDefinitions = (candidateList, existing) => {
|
|
245
|
+
return candidateList.filter((candidate) => existing.find((def) => def.uri === candidate.uri) == null);
|
|
208
246
|
};
|
|
209
|
-
var
|
|
247
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
|
|
210
248
|
var DevServer = class {
|
|
249
|
+
// prettier-ignore
|
|
211
250
|
constructor(_client, _functionsRegistry, _options) {
|
|
212
251
|
this._client = _client;
|
|
213
252
|
this._functionsRegistry = _functionsRegistry;
|
|
@@ -216,14 +255,14 @@ var DevServer = class {
|
|
|
216
255
|
this._handlers = {};
|
|
217
256
|
this._seq = 0;
|
|
218
257
|
this.update = new import_async2.Event();
|
|
219
|
-
this._functionsRegistry.
|
|
220
|
-
|
|
258
|
+
this._functionsRegistry.onFunctionsRegistered.on(async ({ newFunctions }) => {
|
|
259
|
+
newFunctions.forEach((def) => this._load(def));
|
|
221
260
|
await this._safeUpdateRegistration();
|
|
222
|
-
(0,
|
|
223
|
-
|
|
261
|
+
(0, import_log2.log)("new functions loaded", {
|
|
262
|
+
newFunctions
|
|
224
263
|
}, {
|
|
225
|
-
F:
|
|
226
|
-
L:
|
|
264
|
+
F: __dxlog_file2,
|
|
265
|
+
L: 53,
|
|
227
266
|
S: this,
|
|
228
267
|
C: (f, a) => f(...a)
|
|
229
268
|
});
|
|
@@ -236,8 +275,8 @@ var DevServer = class {
|
|
|
236
275
|
}
|
|
237
276
|
get endpoint() {
|
|
238
277
|
(0, import_invariant.invariant)(this._port, void 0, {
|
|
239
|
-
F:
|
|
240
|
-
L:
|
|
278
|
+
F: __dxlog_file2,
|
|
279
|
+
L: 64,
|
|
241
280
|
S: this,
|
|
242
281
|
A: [
|
|
243
282
|
"this._port",
|
|
@@ -254,17 +293,17 @@ var DevServer = class {
|
|
|
254
293
|
}
|
|
255
294
|
async start() {
|
|
256
295
|
(0, import_invariant.invariant)(!this._server, void 0, {
|
|
257
|
-
F:
|
|
258
|
-
L:
|
|
296
|
+
F: __dxlog_file2,
|
|
297
|
+
L: 77,
|
|
259
298
|
S: this,
|
|
260
299
|
A: [
|
|
261
300
|
"!this._server",
|
|
262
301
|
""
|
|
263
302
|
]
|
|
264
303
|
});
|
|
265
|
-
|
|
266
|
-
F:
|
|
267
|
-
L:
|
|
304
|
+
import_log2.log.info("starting...", void 0, {
|
|
305
|
+
F: __dxlog_file2,
|
|
306
|
+
L: 78,
|
|
268
307
|
S: this,
|
|
269
308
|
C: (f, a) => f(...a)
|
|
270
309
|
});
|
|
@@ -274,11 +313,11 @@ var DevServer = class {
|
|
|
274
313
|
app.post("/:path", async (req, res) => {
|
|
275
314
|
const { path: path2 } = req.params;
|
|
276
315
|
try {
|
|
277
|
-
|
|
316
|
+
import_log2.log.info("calling", {
|
|
278
317
|
path: path2
|
|
279
318
|
}, {
|
|
280
|
-
F:
|
|
281
|
-
L:
|
|
319
|
+
F: __dxlog_file2,
|
|
320
|
+
L: 88,
|
|
282
321
|
S: this,
|
|
283
322
|
C: (f, a) => f(...a)
|
|
284
323
|
});
|
|
@@ -289,9 +328,9 @@ var DevServer = class {
|
|
|
289
328
|
res.statusCode = await this.invoke("/" + path2, req.body);
|
|
290
329
|
res.end();
|
|
291
330
|
} catch (err) {
|
|
292
|
-
|
|
293
|
-
F:
|
|
294
|
-
L:
|
|
331
|
+
import_log2.log.catch(err, void 0, {
|
|
332
|
+
F: __dxlog_file2,
|
|
333
|
+
L: 98,
|
|
295
334
|
S: this,
|
|
296
335
|
C: (f, a) => f(...a)
|
|
297
336
|
});
|
|
@@ -312,11 +351,11 @@ var DevServer = class {
|
|
|
312
351
|
const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
|
|
313
352
|
endpoint: this.endpoint
|
|
314
353
|
});
|
|
315
|
-
|
|
354
|
+
import_log2.log.info("registered", {
|
|
316
355
|
endpoint
|
|
317
356
|
}, {
|
|
318
|
-
F:
|
|
319
|
-
L:
|
|
357
|
+
F: __dxlog_file2,
|
|
358
|
+
L: 113,
|
|
320
359
|
S: this,
|
|
321
360
|
C: (f, a) => f(...a)
|
|
322
361
|
});
|
|
@@ -327,44 +366,44 @@ var DevServer = class {
|
|
|
327
366
|
await this.stop();
|
|
328
367
|
throw new Error("FunctionRegistryService not available (check plugin is configured).");
|
|
329
368
|
}
|
|
330
|
-
|
|
369
|
+
import_log2.log.info("started", {
|
|
331
370
|
port: this._port
|
|
332
371
|
}, {
|
|
333
|
-
F:
|
|
334
|
-
L:
|
|
372
|
+
F: __dxlog_file2,
|
|
373
|
+
L: 124,
|
|
335
374
|
S: this,
|
|
336
375
|
C: (f, a) => f(...a)
|
|
337
376
|
});
|
|
338
377
|
}
|
|
339
378
|
async stop() {
|
|
340
379
|
(0, import_invariant.invariant)(this._server, void 0, {
|
|
341
|
-
F:
|
|
342
|
-
L:
|
|
380
|
+
F: __dxlog_file2,
|
|
381
|
+
L: 128,
|
|
343
382
|
S: this,
|
|
344
383
|
A: [
|
|
345
384
|
"this._server",
|
|
346
385
|
""
|
|
347
386
|
]
|
|
348
387
|
});
|
|
349
|
-
|
|
350
|
-
F:
|
|
351
|
-
L:
|
|
388
|
+
import_log2.log.info("stopping...", void 0, {
|
|
389
|
+
F: __dxlog_file2,
|
|
390
|
+
L: 129,
|
|
352
391
|
S: this,
|
|
353
392
|
C: (f, a) => f(...a)
|
|
354
393
|
});
|
|
355
394
|
const trigger = new import_async2.Trigger();
|
|
356
395
|
this._server.close(async () => {
|
|
357
|
-
|
|
358
|
-
F:
|
|
359
|
-
L:
|
|
396
|
+
import_log2.log.info("server stopped", void 0, {
|
|
397
|
+
F: __dxlog_file2,
|
|
398
|
+
L: 133,
|
|
360
399
|
S: this,
|
|
361
400
|
C: (f, a) => f(...a)
|
|
362
401
|
});
|
|
363
402
|
try {
|
|
364
403
|
if (this._functionServiceRegistration) {
|
|
365
404
|
(0, import_invariant.invariant)(this._client.services.services.FunctionRegistryService, void 0, {
|
|
366
|
-
F:
|
|
367
|
-
L:
|
|
405
|
+
F: __dxlog_file2,
|
|
406
|
+
L: 136,
|
|
368
407
|
S: this,
|
|
369
408
|
A: [
|
|
370
409
|
"this._client.services.services.FunctionRegistryService",
|
|
@@ -374,11 +413,11 @@ var DevServer = class {
|
|
|
374
413
|
await this._client.services.services.FunctionRegistryService.unregister({
|
|
375
414
|
registrationId: this._functionServiceRegistration
|
|
376
415
|
});
|
|
377
|
-
|
|
416
|
+
import_log2.log.info("unregistered", {
|
|
378
417
|
registrationId: this._functionServiceRegistration
|
|
379
418
|
}, {
|
|
380
|
-
F:
|
|
381
|
-
L:
|
|
419
|
+
F: __dxlog_file2,
|
|
420
|
+
L: 141,
|
|
382
421
|
S: this,
|
|
383
422
|
C: (f, a) => f(...a)
|
|
384
423
|
});
|
|
@@ -393,9 +432,9 @@ var DevServer = class {
|
|
|
393
432
|
await trigger.wait();
|
|
394
433
|
this._port = void 0;
|
|
395
434
|
this._server = void 0;
|
|
396
|
-
|
|
397
|
-
F:
|
|
398
|
-
L:
|
|
435
|
+
import_log2.log.info("stopped", void 0, {
|
|
436
|
+
F: __dxlog_file2,
|
|
437
|
+
L: 155,
|
|
399
438
|
S: this,
|
|
400
439
|
C: (f, a) => f(...a)
|
|
401
440
|
});
|
|
@@ -403,24 +442,24 @@ var DevServer = class {
|
|
|
403
442
|
/**
|
|
404
443
|
* Load function.
|
|
405
444
|
*/
|
|
406
|
-
async _load(def, force) {
|
|
445
|
+
async _load(def, force = false) {
|
|
407
446
|
const { uri, route, handler } = def;
|
|
408
447
|
const filePath = (0, import_node_path.join)(this._options.baseDir, handler);
|
|
409
|
-
|
|
448
|
+
import_log2.log.info("loading", {
|
|
410
449
|
uri,
|
|
411
450
|
force
|
|
412
451
|
}, {
|
|
413
|
-
F:
|
|
414
|
-
L:
|
|
452
|
+
F: __dxlog_file2,
|
|
453
|
+
L: 164,
|
|
415
454
|
S: this,
|
|
416
455
|
C: (f, a) => f(...a)
|
|
417
456
|
});
|
|
418
457
|
if (force) {
|
|
419
|
-
Object.keys(
|
|
420
|
-
delete
|
|
458
|
+
Object.keys(__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
|
|
459
|
+
delete __require.cache[key];
|
|
421
460
|
});
|
|
422
461
|
}
|
|
423
|
-
const module2 =
|
|
462
|
+
const module2 = __require(filePath);
|
|
424
463
|
if (typeof module2.default !== "function") {
|
|
425
464
|
throw new Error(`Handler must export default function: ${uri}`);
|
|
426
465
|
}
|
|
@@ -431,8 +470,8 @@ var DevServer = class {
|
|
|
431
470
|
}
|
|
432
471
|
async _safeUpdateRegistration() {
|
|
433
472
|
(0, import_invariant.invariant)(this._functionServiceRegistration, void 0, {
|
|
434
|
-
F:
|
|
435
|
-
L:
|
|
473
|
+
F: __dxlog_file2,
|
|
474
|
+
L: 186,
|
|
436
475
|
S: this,
|
|
437
476
|
A: [
|
|
438
477
|
"this._functionServiceRegistration",
|
|
@@ -448,9 +487,9 @@ var DevServer = class {
|
|
|
448
487
|
}))
|
|
449
488
|
});
|
|
450
489
|
} catch (e) {
|
|
451
|
-
|
|
452
|
-
F:
|
|
453
|
-
L:
|
|
490
|
+
import_log2.log.catch(e, void 0, {
|
|
491
|
+
F: __dxlog_file2,
|
|
492
|
+
L: 193,
|
|
454
493
|
S: this,
|
|
455
494
|
C: (f, a) => f(...a)
|
|
456
495
|
});
|
|
@@ -462,26 +501,26 @@ var DevServer = class {
|
|
|
462
501
|
async invoke(path2, data) {
|
|
463
502
|
const seq = ++this._seq;
|
|
464
503
|
const now = Date.now();
|
|
465
|
-
|
|
504
|
+
import_log2.log.info("req", {
|
|
466
505
|
seq,
|
|
467
506
|
path: path2
|
|
468
507
|
}, {
|
|
469
|
-
F:
|
|
470
|
-
L:
|
|
508
|
+
F: __dxlog_file2,
|
|
509
|
+
L: 204,
|
|
471
510
|
S: this,
|
|
472
511
|
C: (f, a) => f(...a)
|
|
473
512
|
});
|
|
474
513
|
const statusCode = await this._invoke(path2, {
|
|
475
514
|
data
|
|
476
515
|
});
|
|
477
|
-
|
|
516
|
+
import_log2.log.info("res", {
|
|
478
517
|
seq,
|
|
479
518
|
path: path2,
|
|
480
519
|
statusCode,
|
|
481
520
|
duration: Date.now() - now
|
|
482
521
|
}, {
|
|
483
|
-
F:
|
|
484
|
-
L:
|
|
522
|
+
F: __dxlog_file2,
|
|
523
|
+
L: 207,
|
|
485
524
|
S: this,
|
|
486
525
|
C: (f, a) => f(...a)
|
|
487
526
|
});
|
|
@@ -491,8 +530,8 @@ var DevServer = class {
|
|
|
491
530
|
async _invoke(path2, event) {
|
|
492
531
|
const { handler } = this._handlers[path2] ?? {};
|
|
493
532
|
(0, import_invariant.invariant)(handler, `invalid path: ${path2}`, {
|
|
494
|
-
F:
|
|
495
|
-
L:
|
|
533
|
+
F: __dxlog_file2,
|
|
534
|
+
L: 214,
|
|
496
535
|
S: this,
|
|
497
536
|
A: [
|
|
498
537
|
"handler",
|
|
@@ -521,16 +560,15 @@ var DevServer = class {
|
|
|
521
560
|
var createContext = () => new import_context2.Context({
|
|
522
561
|
name: "DevServer"
|
|
523
562
|
});
|
|
524
|
-
var
|
|
563
|
+
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
|
|
525
564
|
var Scheduler = class {
|
|
526
565
|
constructor(functions, triggers, _options = {}) {
|
|
527
566
|
this.functions = functions;
|
|
528
567
|
this.triggers = triggers;
|
|
529
568
|
this._options = _options;
|
|
530
569
|
this._ctx = createContext2();
|
|
531
|
-
this.
|
|
532
|
-
|
|
533
|
-
await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), added);
|
|
570
|
+
this.functions.onFunctionsRegistered.on(async ({ space, newFunctions }) => {
|
|
571
|
+
await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), newFunctions);
|
|
534
572
|
});
|
|
535
573
|
this.triggers.registered.on(async ({ space, triggers: triggers2 }) => {
|
|
536
574
|
await this._safeActivateTriggers(space, triggers2, this.functions.getFunctions(space));
|
|
@@ -547,25 +585,24 @@ var Scheduler = class {
|
|
|
547
585
|
await this.functions.close();
|
|
548
586
|
await this.triggers.close();
|
|
549
587
|
}
|
|
550
|
-
// TODO(burdon): Remove and update registries directly.
|
|
551
588
|
async register(space, manifest) {
|
|
552
|
-
await this.functions.register(space, manifest
|
|
589
|
+
await this.functions.register(space, manifest);
|
|
553
590
|
await this.triggers.register(space, manifest);
|
|
554
591
|
}
|
|
555
592
|
async _safeActivateTriggers(space, triggers, functions) {
|
|
556
593
|
const mountTasks = triggers.map((trigger) => {
|
|
557
594
|
return this.activate(space, functions, trigger);
|
|
558
595
|
});
|
|
559
|
-
await Promise.all(mountTasks).catch(
|
|
596
|
+
await Promise.all(mountTasks).catch(import_log3.log.catch);
|
|
560
597
|
}
|
|
561
598
|
async activate(space, functions, fnTrigger) {
|
|
562
599
|
const definition = functions.find((def) => def.uri === fnTrigger.function);
|
|
563
600
|
if (!definition) {
|
|
564
|
-
|
|
601
|
+
import_log3.log.info("function is not found for trigger", {
|
|
565
602
|
fnTrigger
|
|
566
603
|
}, {
|
|
567
|
-
F:
|
|
568
|
-
L:
|
|
604
|
+
F: __dxlog_file3,
|
|
605
|
+
L: 74,
|
|
569
606
|
S: this,
|
|
570
607
|
C: (f, a) => f(...a)
|
|
571
608
|
});
|
|
@@ -574,27 +611,25 @@ var Scheduler = class {
|
|
|
574
611
|
await this.triggers.activate({
|
|
575
612
|
space
|
|
576
613
|
}, fnTrigger, async (args) => {
|
|
577
|
-
return this.
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
}
|
|
584
|
-
});
|
|
614
|
+
return this._execFunction(definition, {
|
|
615
|
+
meta: fnTrigger.meta,
|
|
616
|
+
data: {
|
|
617
|
+
...args,
|
|
618
|
+
spaceKey: space.key
|
|
619
|
+
}
|
|
585
620
|
});
|
|
586
621
|
});
|
|
587
|
-
(0,
|
|
622
|
+
(0, import_log3.log)("activated trigger", {
|
|
588
623
|
space: space.key,
|
|
589
624
|
trigger: fnTrigger
|
|
590
625
|
}, {
|
|
591
|
-
F:
|
|
592
|
-
L:
|
|
626
|
+
F: __dxlog_file3,
|
|
627
|
+
L: 84,
|
|
593
628
|
S: this,
|
|
594
629
|
C: (f, a) => f(...a)
|
|
595
630
|
});
|
|
596
631
|
}
|
|
597
|
-
async _execFunction(def,
|
|
632
|
+
async _execFunction(def, { data, meta }) {
|
|
598
633
|
let status = 0;
|
|
599
634
|
try {
|
|
600
635
|
const payload = Object.assign({}, meta && {
|
|
@@ -603,13 +638,12 @@ var Scheduler = class {
|
|
|
603
638
|
const { endpoint, callback } = this._options;
|
|
604
639
|
if (endpoint) {
|
|
605
640
|
const url = import_node_path2.default.join(endpoint, def.route);
|
|
606
|
-
|
|
641
|
+
import_log3.log.info("exec", {
|
|
607
642
|
function: def.uri,
|
|
608
|
-
url
|
|
609
|
-
triggerType: trigger.spec.type
|
|
643
|
+
url
|
|
610
644
|
}, {
|
|
611
|
-
F:
|
|
612
|
-
L:
|
|
645
|
+
F: __dxlog_file3,
|
|
646
|
+
L: 100,
|
|
613
647
|
S: this,
|
|
614
648
|
C: (f, a) => f(...a)
|
|
615
649
|
});
|
|
@@ -622,11 +656,11 @@ var Scheduler = class {
|
|
|
622
656
|
});
|
|
623
657
|
status = response.status;
|
|
624
658
|
} else if (callback) {
|
|
625
|
-
|
|
659
|
+
import_log3.log.info("exec", {
|
|
626
660
|
function: def.uri
|
|
627
661
|
}, {
|
|
628
|
-
F:
|
|
629
|
-
L:
|
|
662
|
+
F: __dxlog_file3,
|
|
663
|
+
L: 111,
|
|
630
664
|
S: this,
|
|
631
665
|
C: (f, a) => f(...a)
|
|
632
666
|
});
|
|
@@ -635,22 +669,22 @@ var Scheduler = class {
|
|
|
635
669
|
if (status && status >= 400) {
|
|
636
670
|
throw new Error(`Response: ${status}`);
|
|
637
671
|
}
|
|
638
|
-
|
|
672
|
+
import_log3.log.info("done", {
|
|
639
673
|
function: def.uri,
|
|
640
674
|
status
|
|
641
675
|
}, {
|
|
642
|
-
F:
|
|
643
|
-
L:
|
|
676
|
+
F: __dxlog_file3,
|
|
677
|
+
L: 121,
|
|
644
678
|
S: this,
|
|
645
679
|
C: (f, a) => f(...a)
|
|
646
680
|
});
|
|
647
681
|
} catch (err) {
|
|
648
|
-
|
|
682
|
+
import_log3.log.error("error", {
|
|
649
683
|
function: def.uri,
|
|
650
684
|
error: err.message
|
|
651
685
|
}, {
|
|
652
|
-
F:
|
|
653
|
-
L:
|
|
686
|
+
F: __dxlog_file3,
|
|
687
|
+
L: 123,
|
|
654
688
|
S: this,
|
|
655
689
|
C: (f, a) => f(...a)
|
|
656
690
|
});
|
|
@@ -662,59 +696,53 @@ var Scheduler = class {
|
|
|
662
696
|
var createContext2 = () => new import_context3.Context({
|
|
663
697
|
name: "FunctionScheduler"
|
|
664
698
|
});
|
|
665
|
-
var
|
|
699
|
+
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/subscription-trigger.ts";
|
|
666
700
|
var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
|
|
667
701
|
const objectIds = /* @__PURE__ */ new Set();
|
|
668
|
-
const task = new
|
|
702
|
+
const task = new import_async4.DeferredTask(ctx, async () => {
|
|
669
703
|
if (objectIds.size > 0) {
|
|
670
|
-
const objects = Array.from(objectIds);
|
|
671
|
-
objectIds.clear();
|
|
672
704
|
await callback({
|
|
673
|
-
objects
|
|
705
|
+
objects: Array.from(objectIds)
|
|
674
706
|
});
|
|
707
|
+
objectIds.clear();
|
|
675
708
|
}
|
|
676
|
-
}, {
|
|
677
|
-
maxFrequency: 4
|
|
678
709
|
});
|
|
679
710
|
const subscriptions = [];
|
|
680
711
|
const subscription = (0, import_echo_db.createSubscription)(({ added, updated }) => {
|
|
681
|
-
|
|
712
|
+
import_log5.log.info("updated", {
|
|
713
|
+
added: added.length,
|
|
714
|
+
updated: updated.length
|
|
715
|
+
}, {
|
|
716
|
+
F: __dxlog_file4,
|
|
717
|
+
L: 32,
|
|
718
|
+
S: void 0,
|
|
719
|
+
C: (f, a) => f(...a)
|
|
720
|
+
});
|
|
682
721
|
for (const object of added) {
|
|
683
722
|
objectIds.add(object.id);
|
|
684
723
|
}
|
|
685
724
|
for (const object of updated) {
|
|
686
725
|
objectIds.add(object.id);
|
|
687
726
|
}
|
|
688
|
-
|
|
689
|
-
import_log6.log.info("updated", {
|
|
690
|
-
added: added.length,
|
|
691
|
-
updated: updated.length
|
|
692
|
-
}, {
|
|
693
|
-
F: __dxlog_file5,
|
|
694
|
-
L: 45,
|
|
695
|
-
S: void 0,
|
|
696
|
-
C: (f, a) => f(...a)
|
|
697
|
-
});
|
|
698
|
-
task.trigger();
|
|
699
|
-
}
|
|
727
|
+
task.schedule();
|
|
700
728
|
});
|
|
701
729
|
subscriptions.push(() => subscription.unsubscribe());
|
|
702
730
|
const { filter, options: { deep, delay } = {} } = spec;
|
|
703
731
|
const update = ({ objects }) => {
|
|
704
732
|
subscription.update(objects);
|
|
705
733
|
if (deep) {
|
|
706
|
-
|
|
734
|
+
import_log5.log.info("update", {
|
|
707
735
|
objects: objects.length
|
|
708
736
|
}, {
|
|
709
|
-
F:
|
|
710
|
-
L:
|
|
737
|
+
F: __dxlog_file4,
|
|
738
|
+
L: 52,
|
|
711
739
|
S: void 0,
|
|
712
740
|
C: (f, a) => f(...a)
|
|
713
741
|
});
|
|
714
742
|
for (const object of objects) {
|
|
715
743
|
const content = object.content;
|
|
716
744
|
if (content instanceof import_types.TextV0Type) {
|
|
717
|
-
subscriptions.push((0, import_echo_db.getAutomergeObjectCore)(content).updates.on((0,
|
|
745
|
+
subscriptions.push((0, import_echo_db.getAutomergeObjectCore)(content).updates.on((0, import_async4.debounce)(() => subscription.update([
|
|
718
746
|
object
|
|
719
747
|
]), 1e3)));
|
|
720
748
|
}
|
|
@@ -722,14 +750,14 @@ var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
|
|
|
722
750
|
}
|
|
723
751
|
};
|
|
724
752
|
const query = triggerCtx.space.db.query(import_echo_db.Filter.or(filter.map(({ type, props }) => import_echo_db.Filter.typename(type, props))));
|
|
725
|
-
subscriptions.push(query.subscribe(delay ? (0,
|
|
753
|
+
subscriptions.push(query.subscribe(delay ? (0, import_async4.debounce)(update, delay) : update));
|
|
726
754
|
ctx.onDispose(() => {
|
|
727
755
|
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
728
756
|
});
|
|
729
757
|
};
|
|
730
|
-
var
|
|
758
|
+
var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/timer-trigger.ts";
|
|
731
759
|
var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
|
|
732
|
-
const task = new
|
|
760
|
+
const task = new import_async5.DeferredTask(ctx, async () => {
|
|
733
761
|
await callback({});
|
|
734
762
|
});
|
|
735
763
|
let last = 0;
|
|
@@ -742,12 +770,12 @@ var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
|
|
|
742
770
|
const delta = last ? now - last : 0;
|
|
743
771
|
last = now;
|
|
744
772
|
run++;
|
|
745
|
-
|
|
773
|
+
import_log6.log.info("tick", {
|
|
746
774
|
space: triggerContext.space.key.truncate(),
|
|
747
775
|
count: run,
|
|
748
776
|
delta
|
|
749
777
|
}, {
|
|
750
|
-
F:
|
|
778
|
+
F: __dxlog_file5,
|
|
751
779
|
L: 37,
|
|
752
780
|
S: void 0,
|
|
753
781
|
C: (f, a) => f(...a)
|
|
@@ -758,7 +786,7 @@ var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
|
|
|
758
786
|
job.start();
|
|
759
787
|
ctx.onDispose(() => job.stop());
|
|
760
788
|
};
|
|
761
|
-
var
|
|
789
|
+
var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/webhook-trigger.ts";
|
|
762
790
|
var createWebhookTrigger = async (ctx, _, spec, callback) => {
|
|
763
791
|
const server = import_node_http.default.createServer(async (req, res) => {
|
|
764
792
|
if (req.method !== spec.method) {
|
|
@@ -772,10 +800,10 @@ var createWebhookTrigger = async (ctx, _, spec, callback) => {
|
|
|
772
800
|
random: true
|
|
773
801
|
});
|
|
774
802
|
server.listen(port, () => {
|
|
775
|
-
|
|
803
|
+
import_log7.log.info("started webhook", {
|
|
776
804
|
port
|
|
777
805
|
}, {
|
|
778
|
-
F:
|
|
806
|
+
F: __dxlog_file6,
|
|
779
807
|
L: 40,
|
|
780
808
|
S: void 0,
|
|
781
809
|
C: (f, a) => f(...a)
|
|
@@ -786,7 +814,7 @@ var createWebhookTrigger = async (ctx, _, spec, callback) => {
|
|
|
786
814
|
server.close();
|
|
787
815
|
});
|
|
788
816
|
};
|
|
789
|
-
var
|
|
817
|
+
var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/websocket-trigger.ts";
|
|
790
818
|
var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
791
819
|
retryDelay: 2,
|
|
792
820
|
maxAttempts: 5
|
|
@@ -794,14 +822,14 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
|
794
822
|
const { url, init } = spec;
|
|
795
823
|
let ws;
|
|
796
824
|
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
|
|
797
|
-
const open = new
|
|
825
|
+
const open = new import_async6.Trigger();
|
|
798
826
|
ws = new import_ws.default(url);
|
|
799
827
|
Object.assign(ws, {
|
|
800
828
|
onopen: () => {
|
|
801
|
-
|
|
829
|
+
import_log8.log.info("opened", {
|
|
802
830
|
url
|
|
803
831
|
}, {
|
|
804
|
-
F:
|
|
832
|
+
F: __dxlog_file7,
|
|
805
833
|
L: 39,
|
|
806
834
|
S: void 0,
|
|
807
835
|
C: (f, a) => f(...a)
|
|
@@ -812,21 +840,21 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
|
812
840
|
open.wake(true);
|
|
813
841
|
},
|
|
814
842
|
onclose: (event) => {
|
|
815
|
-
|
|
843
|
+
import_log8.log.info("closed", {
|
|
816
844
|
url,
|
|
817
845
|
code: event.code
|
|
818
846
|
}, {
|
|
819
|
-
F:
|
|
847
|
+
F: __dxlog_file7,
|
|
820
848
|
L: 48,
|
|
821
849
|
S: void 0,
|
|
822
850
|
C: (f, a) => f(...a)
|
|
823
851
|
});
|
|
824
852
|
if (event.code === 1006) {
|
|
825
853
|
setTimeout(async () => {
|
|
826
|
-
|
|
854
|
+
import_log8.log.info(`reconnecting in ${options.retryDelay}s...`, {
|
|
827
855
|
url
|
|
828
856
|
}, {
|
|
829
|
-
F:
|
|
857
|
+
F: __dxlog_file7,
|
|
830
858
|
L: 53,
|
|
831
859
|
S: void 0,
|
|
832
860
|
C: (f, a) => f(...a)
|
|
@@ -837,10 +865,10 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
|
837
865
|
open.wake(false);
|
|
838
866
|
},
|
|
839
867
|
onerror: (event) => {
|
|
840
|
-
|
|
868
|
+
import_log8.log.catch(event.error, {
|
|
841
869
|
url
|
|
842
870
|
}, {
|
|
843
|
-
F:
|
|
871
|
+
F: __dxlog_file7,
|
|
844
872
|
L: 62,
|
|
845
873
|
S: void 0,
|
|
846
874
|
C: (f, a) => f(...a)
|
|
@@ -848,8 +876,8 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
|
848
876
|
},
|
|
849
877
|
onmessage: async (event) => {
|
|
850
878
|
try {
|
|
851
|
-
|
|
852
|
-
F:
|
|
879
|
+
import_log8.log.info("message", void 0, {
|
|
880
|
+
F: __dxlog_file7,
|
|
853
881
|
L: 67,
|
|
854
882
|
S: void 0,
|
|
855
883
|
C: (f, a) => f(...a)
|
|
@@ -859,10 +887,10 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
|
859
887
|
data
|
|
860
888
|
});
|
|
861
889
|
} catch (err) {
|
|
862
|
-
|
|
890
|
+
import_log8.log.catch(err, {
|
|
863
891
|
url
|
|
864
892
|
}, {
|
|
865
|
-
F:
|
|
893
|
+
F: __dxlog_file7,
|
|
866
894
|
L: 71,
|
|
867
895
|
S: void 0,
|
|
868
896
|
C: (f, a) => f(...a)
|
|
@@ -876,15 +904,15 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
|
876
904
|
} else {
|
|
877
905
|
const wait = Math.pow(attempt, 2) * options.retryDelay;
|
|
878
906
|
if (attempt < options.maxAttempts) {
|
|
879
|
-
|
|
907
|
+
import_log8.log.warn(`failed to connect; trying again in ${wait}s`, {
|
|
880
908
|
attempt
|
|
881
909
|
}, {
|
|
882
|
-
F:
|
|
910
|
+
F: __dxlog_file7,
|
|
883
911
|
L: 82,
|
|
884
912
|
S: void 0,
|
|
885
913
|
C: (f, a) => f(...a)
|
|
886
914
|
});
|
|
887
|
-
await (0,
|
|
915
|
+
await (0, import_async6.sleep)(wait * 1e3);
|
|
888
916
|
}
|
|
889
917
|
}
|
|
890
918
|
}
|
|
@@ -892,7 +920,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
|
|
|
892
920
|
ws?.close();
|
|
893
921
|
});
|
|
894
922
|
};
|
|
895
|
-
var
|
|
923
|
+
var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/trigger-registry.ts";
|
|
896
924
|
var triggerHandlers = {
|
|
897
925
|
subscription: createSubscriptionTrigger,
|
|
898
926
|
timer: createTimerTrigger,
|
|
@@ -905,8 +933,8 @@ var TriggerRegistry = class extends import_context4.Resource {
|
|
|
905
933
|
this._client = _client;
|
|
906
934
|
this._options = _options;
|
|
907
935
|
this._triggersBySpaceKey = new import_util3.ComplexMap(import_keys2.PublicKey.hash);
|
|
908
|
-
this.registered = new
|
|
909
|
-
this.removed = new
|
|
936
|
+
this.registered = new import_async3.Event();
|
|
937
|
+
this.removed = new import_async3.Event();
|
|
910
938
|
}
|
|
911
939
|
getActiveTriggers(space) {
|
|
912
940
|
return this._getTriggers(space, (t) => t.activationCtx != null);
|
|
@@ -915,12 +943,12 @@ var TriggerRegistry = class extends import_context4.Resource {
|
|
|
915
943
|
return this._getTriggers(space, (t) => t.activationCtx == null);
|
|
916
944
|
}
|
|
917
945
|
async activate(triggerCtx, trigger, callback) {
|
|
918
|
-
(0,
|
|
946
|
+
(0, import_log4.log)("activate", {
|
|
919
947
|
space: triggerCtx.space.key,
|
|
920
948
|
trigger
|
|
921
949
|
}, {
|
|
922
|
-
F:
|
|
923
|
-
L:
|
|
950
|
+
F: __dxlog_file8,
|
|
951
|
+
L: 73,
|
|
924
952
|
S: this,
|
|
925
953
|
C: (f, a) => f(...a)
|
|
926
954
|
});
|
|
@@ -930,8 +958,8 @@ var TriggerRegistry = class extends import_context4.Resource {
|
|
|
930
958
|
this._ctx.onDispose(() => activationCtx.dispose());
|
|
931
959
|
const registeredTrigger = this._triggersBySpaceKey.get(triggerCtx.space.key)?.find((reg) => reg.trigger.id === trigger.id);
|
|
932
960
|
(0, import_invariant2.invariant)(registeredTrigger, `Trigger is not registered: ${trigger.function}`, {
|
|
933
|
-
F:
|
|
934
|
-
L:
|
|
961
|
+
F: __dxlog_file8,
|
|
962
|
+
L: 79,
|
|
935
963
|
S: this,
|
|
936
964
|
A: [
|
|
937
965
|
"registeredTrigger",
|
|
@@ -951,35 +979,24 @@ var TriggerRegistry = class extends import_context4.Resource {
|
|
|
951
979
|
* Loads triggers from the manifest into the space.
|
|
952
980
|
*/
|
|
953
981
|
async register(space, manifest) {
|
|
954
|
-
(0,
|
|
982
|
+
(0, import_log4.log)("register", {
|
|
955
983
|
space: space.key
|
|
956
984
|
}, {
|
|
957
|
-
F:
|
|
958
|
-
L:
|
|
985
|
+
F: __dxlog_file8,
|
|
986
|
+
L: 95,
|
|
959
987
|
S: this,
|
|
960
988
|
C: (f, a) => f(...a)
|
|
961
989
|
});
|
|
962
990
|
if (!manifest.triggers?.length) {
|
|
963
991
|
return;
|
|
964
992
|
}
|
|
965
|
-
if (!space.db.graph.runtimeSchemaRegistry.
|
|
966
|
-
space.db.graph.runtimeSchemaRegistry.registerSchema(
|
|
993
|
+
if (!space.db.graph.runtimeSchemaRegistry.isSchemaRegistered(FunctionTrigger)) {
|
|
994
|
+
space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionTrigger);
|
|
967
995
|
}
|
|
968
|
-
const
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
b.function,
|
|
973
|
-
b.spec.type
|
|
974
|
-
].join("-"))
|
|
975
|
-
];
|
|
976
|
-
return intersection((0, import_echo2.getMeta)(a)?.keys ?? [], keys, import_echo_schema.foreignKeyEquals).length > 0;
|
|
977
|
-
});
|
|
978
|
-
added.forEach((trigger) => {
|
|
979
|
-
const { meta, object } = (0, import_echo_schema.splitMeta)(trigger);
|
|
980
|
-
space.db.add((0, import_echo2.create)(import_chunk_3VSJ57ZZ.FunctionTrigger, object, meta));
|
|
981
|
-
});
|
|
982
|
-
removed.forEach((trigger) => space.db.remove(trigger));
|
|
996
|
+
const reactiveObjects = manifest.triggers.map((template) => (0, import_echo2.create)(FunctionTrigger, {
|
|
997
|
+
...template
|
|
998
|
+
}));
|
|
999
|
+
reactiveObjects.forEach((obj) => space.db.add(obj));
|
|
983
1000
|
}
|
|
984
1001
|
async _open() {
|
|
985
1002
|
const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
@@ -993,7 +1010,7 @@ var TriggerRegistry = class extends import_context4.Resource {
|
|
|
993
1010
|
if (this._ctx.disposed) {
|
|
994
1011
|
break;
|
|
995
1012
|
}
|
|
996
|
-
const functionsSubscription = space.db.query(import_echo2.Filter.schema(
|
|
1013
|
+
const functionsSubscription = space.db.query(import_echo2.Filter.schema(FunctionTrigger)).subscribe(async (triggers) => {
|
|
997
1014
|
await this._handleRemovedTriggers(space, triggers.objects, registered);
|
|
998
1015
|
this._handleNewTriggers(space, triggers.objects, registered);
|
|
999
1016
|
});
|
|
@@ -1014,12 +1031,12 @@ var TriggerRegistry = class extends import_context4.Resource {
|
|
|
1014
1031
|
trigger
|
|
1015
1032
|
}));
|
|
1016
1033
|
registered.push(...newRegisteredTriggers);
|
|
1017
|
-
(0,
|
|
1034
|
+
(0, import_log4.log)("registered new triggers", () => ({
|
|
1018
1035
|
spaceKey: space.key,
|
|
1019
1036
|
functions: newTriggers.map((t) => t.function)
|
|
1020
1037
|
}), {
|
|
1021
|
-
F:
|
|
1022
|
-
L:
|
|
1038
|
+
F: __dxlog_file8,
|
|
1039
|
+
L: 146,
|
|
1023
1040
|
S: this,
|
|
1024
1041
|
C: (f, a) => f(...a)
|
|
1025
1042
|
});
|