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