@dxos/functions 0.1.53-main.e7b89d1 → 0.1.53-main.e9dc2bc
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/README.md +4 -5
- package/dist/lib/browser/index.mjs +52 -54
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +51 -53
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/runtime/trigger-manager.d.ts.map +1 -1
- package/package.json +8 -8
- package/src/runtime/trigger-manager.ts +21 -26
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ runtime:
|
|
|
50
50
|
Start functions in dev mode (from the related package):
|
|
51
51
|
|
|
52
52
|
```bash
|
|
53
|
-
dx function dev-server -r ts-node/register
|
|
53
|
+
dx function dev-server -r ts-node/register --verbose
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
> NOTE: `-r ts-node/register` configures native TypesScript support.
|
|
@@ -59,9 +59,9 @@ Install `nodemon` to support live reloading:
|
|
|
59
59
|
|
|
60
60
|
```bash
|
|
61
61
|
npm i -g nodemon
|
|
62
|
-
export DXOS_ROOT=$(git rev-parse --show-toplevel)
|
|
63
62
|
|
|
64
|
-
nodemon -w ./src -e ts --exec $
|
|
63
|
+
nodemon -w ./src -e ts --exec $(git rev-parse --show-toplevel)/packages/devtools/cli/bin/dev \
|
|
64
|
+
function dev-server -r ts-node/register --verbose
|
|
65
65
|
```
|
|
66
66
|
|
|
67
67
|
## Invoking functions
|
|
@@ -69,8 +69,7 @@ nodemon -w ./src -e ts --exec $DXOS_ROOT/packages/devtools/cli/bin/dev function
|
|
|
69
69
|
> NOTE: The port (7001) must match the one in config.
|
|
70
70
|
|
|
71
71
|
```bash
|
|
72
|
-
curl -X POST -H 'Content-Type: application/json' -w '\n'
|
|
73
|
-
http://localhost:7001/dev/hello --data '{ "message": "Hello World!" }'
|
|
72
|
+
curl http://localhost:7001/dev/hello -X POST -H 'Content-Type: application/json' -w '\n' --data '{ "message": "Hello World!" }'
|
|
74
73
|
```
|
|
75
74
|
|
|
76
75
|
## DXOS Resources
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "@dxos/node-std/globals"
|
|
1
|
+
import "@dxos/node-std/globals";
|
|
2
2
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
3
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
4
|
}) : x)(function(x) {
|
|
@@ -13,6 +13,7 @@ import { join } from "@dxos/node-std/path";
|
|
|
13
13
|
import { getPortPromise } from "portfinder";
|
|
14
14
|
import { Trigger } from "@dxos/async";
|
|
15
15
|
import { log } from "@dxos/log";
|
|
16
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
|
|
16
17
|
var DEFAULT_PORT = 7e3;
|
|
17
18
|
var DevServer = class {
|
|
18
19
|
// prettier-ignore
|
|
@@ -41,10 +42,10 @@ var DevServer = class {
|
|
|
41
42
|
this._functionHandlers[name] = handler;
|
|
42
43
|
} catch (err) {
|
|
43
44
|
log.error("parsing function (check functions.yml manifest)", err, {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
F: __dxlog_file,
|
|
46
|
+
L: 63,
|
|
47
|
+
S: this,
|
|
48
|
+
C: (f, a) => f(...a)
|
|
48
49
|
});
|
|
49
50
|
}
|
|
50
51
|
}
|
|
@@ -58,10 +59,10 @@ var DevServer = class {
|
|
|
58
59
|
function: functionName,
|
|
59
60
|
data: req.body
|
|
60
61
|
}, {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
F: __dxlog_file,
|
|
63
|
+
L: 74,
|
|
64
|
+
S: this,
|
|
65
|
+
C: (f, a) => f(...a)
|
|
65
66
|
});
|
|
66
67
|
const builder = {
|
|
67
68
|
status: (code) => {
|
|
@@ -123,6 +124,7 @@ import { Context } from "@dxos/context";
|
|
|
123
124
|
import { createSubscription } from "@dxos/echo-schema";
|
|
124
125
|
import { log as log2 } from "@dxos/log";
|
|
125
126
|
import { ComplexMap } from "@dxos/util";
|
|
127
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/trigger-manager.ts";
|
|
126
128
|
var TriggerManager = class {
|
|
127
129
|
constructor(_client, _triggers, _invokeOptions) {
|
|
128
130
|
this._client = _client;
|
|
@@ -135,7 +137,7 @@ var TriggerManager = class {
|
|
|
135
137
|
for (const space of spaces) {
|
|
136
138
|
await space.waitUntilReady();
|
|
137
139
|
for (const trigger of this._triggers) {
|
|
138
|
-
await this.mount(new Context(),
|
|
140
|
+
await this.mount(new Context(), space, trigger);
|
|
139
141
|
}
|
|
140
142
|
}
|
|
141
143
|
});
|
|
@@ -145,7 +147,7 @@ var TriggerManager = class {
|
|
|
145
147
|
await this.unmount(name, spaceKey);
|
|
146
148
|
}
|
|
147
149
|
}
|
|
148
|
-
async mount(ctx,
|
|
150
|
+
async mount(ctx, space, trigger) {
|
|
149
151
|
const key = {
|
|
150
152
|
name: trigger.function,
|
|
151
153
|
spaceKey: space.key
|
|
@@ -156,60 +158,56 @@ var TriggerManager = class {
|
|
|
156
158
|
ctx,
|
|
157
159
|
trigger
|
|
158
160
|
});
|
|
161
|
+
log2("mount", {
|
|
162
|
+
space: space.key,
|
|
163
|
+
trigger
|
|
164
|
+
}, {
|
|
165
|
+
F: __dxlog_file2,
|
|
166
|
+
L: 59,
|
|
167
|
+
S: this,
|
|
168
|
+
C: (f, a) => f(...a)
|
|
169
|
+
});
|
|
159
170
|
if (ctx.disposed) {
|
|
160
171
|
return;
|
|
161
172
|
}
|
|
162
|
-
let count = 0;
|
|
163
173
|
const objectIds = /* @__PURE__ */ new Set();
|
|
164
174
|
const task = new DeferredTask(ctx, async () => {
|
|
165
|
-
const updatedObjects = Array.from(objectIds);
|
|
166
|
-
objectIds.clear();
|
|
167
175
|
await this.invokeFunction(this._invokeOptions, trigger.function, {
|
|
168
176
|
space: space.key,
|
|
169
|
-
objects:
|
|
177
|
+
objects: Array.from(objectIds)
|
|
170
178
|
});
|
|
171
179
|
});
|
|
172
|
-
|
|
180
|
+
let count = 0;
|
|
181
|
+
const subscription = createSubscription(({ added, updated }) => {
|
|
173
182
|
for (const object of added) {
|
|
174
183
|
objectIds.add(object.id);
|
|
175
184
|
}
|
|
176
|
-
for (const
|
|
177
|
-
objectIds.add(
|
|
185
|
+
for (const object of updated) {
|
|
186
|
+
objectIds.add(object.id);
|
|
178
187
|
}
|
|
179
|
-
log2
|
|
188
|
+
log2("updated", {
|
|
189
|
+
trigger,
|
|
180
190
|
space: space.key,
|
|
181
191
|
objects: objectIds.size,
|
|
182
|
-
added: added.length,
|
|
183
|
-
updated: updated.length,
|
|
184
192
|
count
|
|
185
193
|
}, {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
194
|
+
F: __dxlog_file2,
|
|
195
|
+
L: 82,
|
|
196
|
+
S: this,
|
|
197
|
+
C: (f, a) => f(...a)
|
|
190
198
|
});
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
199
|
+
task.schedule();
|
|
200
|
+
count++;
|
|
194
201
|
});
|
|
195
|
-
ctx.onDispose(() =>
|
|
202
|
+
ctx.onDispose(() => subscription.unsubscribe());
|
|
196
203
|
const query = space.db.query({
|
|
197
|
-
|
|
198
|
-
|
|
204
|
+
"@type": trigger.subscription.type,
|
|
205
|
+
...trigger.subscription.props
|
|
199
206
|
});
|
|
200
207
|
const unsubscribe = query.subscribe(({ objects }) => {
|
|
201
|
-
|
|
202
|
-
});
|
|
203
|
-
ctx.onDispose(unsubscribe);
|
|
204
|
-
log2.info("mounted", {
|
|
205
|
-
space: space.key,
|
|
206
|
-
trigger
|
|
207
|
-
}, {
|
|
208
|
-
file: "trigger-manager.ts",
|
|
209
|
-
line: 109,
|
|
210
|
-
scope: this,
|
|
211
|
-
callSite: (f, a) => f(...a)
|
|
208
|
+
subscription.update(objects);
|
|
212
209
|
});
|
|
210
|
+
ctx.onDispose(() => unsubscribe());
|
|
213
211
|
}
|
|
214
212
|
}
|
|
215
213
|
async unmount(name, spaceKey) {
|
|
@@ -232,10 +230,10 @@ var TriggerManager = class {
|
|
|
232
230
|
log2("invoke", {
|
|
233
231
|
function: functionName
|
|
234
232
|
}, {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
233
|
+
F: __dxlog_file2,
|
|
234
|
+
L: 123,
|
|
235
|
+
S: this,
|
|
236
|
+
C: (f, a) => f(...a)
|
|
239
237
|
});
|
|
240
238
|
const url = `${endpoint}/${runtime}/${functionName}`;
|
|
241
239
|
const res = await fetch(url, {
|
|
@@ -249,20 +247,20 @@ var TriggerManager = class {
|
|
|
249
247
|
function: functionName,
|
|
250
248
|
result: await res.json()
|
|
251
249
|
}, {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
250
|
+
F: __dxlog_file2,
|
|
251
|
+
L: 133,
|
|
252
|
+
S: this,
|
|
253
|
+
C: (f, a) => f(...a)
|
|
256
254
|
});
|
|
257
255
|
} catch (err) {
|
|
258
256
|
log2.error("error", {
|
|
259
257
|
function: functionName,
|
|
260
258
|
error: err.message
|
|
261
259
|
}, {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
260
|
+
F: __dxlog_file2,
|
|
261
|
+
L: 135,
|
|
262
|
+
S: this,
|
|
263
|
+
C: (f, a) => f(...a)
|
|
266
264
|
});
|
|
267
265
|
}
|
|
268
266
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/runtime/dev-server.ts", "../../../src/runtime/trigger-manager.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport express from 'express';\nimport http from 'http';\nimport { join } from 'node:path';\nimport { getPortPromise } from 'portfinder';\n\nimport { Trigger } from '@dxos/async';\nimport { Client } from '@dxos/client';\nimport { log } from '@dxos/log';\n\nimport { FunctionContext, FunctionHandler, FunctionsManifest, Response } from '../function';\n\nconst DEFAULT_PORT = 7000;\n\nexport type DevServerOptions = {\n directory: string;\n manifest: FunctionsManifest;\n};\n\n/**\n * Functions dev server provides a local HTTP server for testing functions.\n */\nexport class DevServer {\n private readonly _functionHandlers: Record<string, FunctionHandler> = {};\n\n private _server?: http.Server;\n private _port?: number;\n private _registrationId?: string;\n\n // prettier-ignore\n constructor(\n private readonly _client: Client,\n private readonly _options: DevServerOptions\n ) {}\n\n get port() {\n return this._port;\n }\n\n get endpoint() {\n return this._port ? `http://localhost:${this._port}` : undefined;\n }\n\n get functions() {\n return Object.keys(this._functionHandlers);\n }\n\n async initialize() {\n for (const [name, _] of Object.entries(this._options.manifest.functions)) {\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const module = require(join(this._options.directory, name));\n const handler = module.default;\n if (typeof handler !== 'function') {\n throw new Error(`Handler must export default function: ${name}`);\n }\n\n this._functionHandlers[name] = handler;\n } catch (err) {\n log.error('parsing function (check functions.yml manifest)', err);\n }\n }\n }\n\n async start() {\n const app = express();\n app.use(express.json());\n\n app.post('/:functionName', async (req, res) => {\n const functionName = req.params.functionName;\n log('invoke', { function: functionName, data: req.body });\n\n const builder: Response = {\n status: (code: number) => {\n res.statusCode = code;\n return builder;\n },\n succeed: (result = {}) => {\n res.end(JSON.stringify(result));\n return builder;\n },\n };\n\n const context: FunctionContext = {\n client: this._client,\n status: builder.status.bind(builder),\n };\n\n void (async () => {\n try {\n await this._functionHandlers[functionName](req.body, context);\n } catch (err: any) {\n res.statusCode = 500;\n res.end(err.message);\n }\n })();\n });\n\n this._port = await getPortPromise({ startPort: DEFAULT_PORT });\n this._server = app.listen(this._port);\n\n // TODO(burdon): Check plugin is registered.\n // TypeError: Cannot read properties of undefined (reading 'register')\n const { registrationId } = await this._client.services.services.FunctionRegistryService!.register({\n endpoint: this.endpoint!,\n functions: this.functions.map((name) => ({ name })),\n });\n this._registrationId = registrationId;\n }\n\n async stop() {\n const trigger = new Trigger();\n this._server?.close(async () => {\n if (this._registrationId) {\n await this._client.services.services.FunctionRegistryService!.unregister({\n registrationId: this._registrationId,\n });\n this._registrationId = undefined;\n }\n\n trigger.wake();\n });\n\n await trigger.wait();\n this._server = undefined;\n this._port = undefined;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport assert from 'node:assert';\n\nimport { DeferredTask } from '@dxos/async';\nimport { Client, PublicKey
|
|
5
|
-
"mappings": ";;;;;;;;;;AAIA,OAAOA,aAAa;AAEpB,SAASC,YAAY;AACrB,SAASC,sBAAsB;AAE/B,SAASC,eAAe;AAExB,SAASC,WAAW
|
|
6
|
-
"names": ["express", "join", "getPortPromise", "Trigger", "log", "DEFAULT_PORT", "DevServer", "constructor", "_client", "_options", "_functionHandlers", "port", "_port", "endpoint", "undefined", "functions", "Object", "keys", "initialize", "name", "_", "entries", "manifest", "module", "require", "join", "directory", "handler", "default", "Error", "err", "log", "error", "start", "app", "express", "use", "json", "post", "req", "res", "functionName", "params", "function", "data", "body", "builder", "status", "code", "statusCode", "succeed", "result", "end", "JSON", "stringify", "context", "client", "bind", "message", "getPortPromise", "startPort", "_server", "listen", "registrationId", "services", "FunctionRegistryService", "register", "map", "_registrationId", "stop", "trigger", "Trigger", "close", "unregister", "wake", "wait", "assert", "DeferredTask", "Context", "createSubscription", "log", "ComplexMap", "TriggerManager", "constructor", "_client", "_triggers", "_invokeOptions", "_mounts", "name", "spaceKey", "toHex", "start", "spaces", "subscribe", "space", "waitUntilReady", "trigger", "mount", "stop", "keys", "unmount", "ctx", "key", "function", "exists", "get", "set", "disposed", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport express from 'express';\nimport http from 'http';\nimport { join } from 'node:path';\nimport { getPortPromise } from 'portfinder';\n\nimport { Trigger } from '@dxos/async';\nimport { Client } from '@dxos/client';\nimport { log } from '@dxos/log';\n\nimport { FunctionContext, FunctionHandler, FunctionsManifest, Response } from '../function';\n\nconst DEFAULT_PORT = 7000;\n\nexport type DevServerOptions = {\n directory: string;\n manifest: FunctionsManifest;\n};\n\n/**\n * Functions dev server provides a local HTTP server for testing functions.\n */\nexport class DevServer {\n private readonly _functionHandlers: Record<string, FunctionHandler> = {};\n\n private _server?: http.Server;\n private _port?: number;\n private _registrationId?: string;\n\n // prettier-ignore\n constructor(\n private readonly _client: Client,\n private readonly _options: DevServerOptions\n ) {}\n\n get port() {\n return this._port;\n }\n\n get endpoint() {\n return this._port ? `http://localhost:${this._port}` : undefined;\n }\n\n get functions() {\n return Object.keys(this._functionHandlers);\n }\n\n async initialize() {\n for (const [name, _] of Object.entries(this._options.manifest.functions)) {\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const module = require(join(this._options.directory, name));\n const handler = module.default;\n if (typeof handler !== 'function') {\n throw new Error(`Handler must export default function: ${name}`);\n }\n\n this._functionHandlers[name] = handler;\n } catch (err) {\n log.error('parsing function (check functions.yml manifest)', err);\n }\n }\n }\n\n async start() {\n const app = express();\n app.use(express.json());\n\n app.post('/:functionName', async (req, res) => {\n const functionName = req.params.functionName;\n log('invoke', { function: functionName, data: req.body });\n\n const builder: Response = {\n status: (code: number) => {\n res.statusCode = code;\n return builder;\n },\n succeed: (result = {}) => {\n res.end(JSON.stringify(result));\n return builder;\n },\n };\n\n const context: FunctionContext = {\n client: this._client,\n status: builder.status.bind(builder),\n };\n\n void (async () => {\n try {\n await this._functionHandlers[functionName](req.body, context);\n } catch (err: any) {\n res.statusCode = 500;\n res.end(err.message);\n }\n })();\n });\n\n this._port = await getPortPromise({ startPort: DEFAULT_PORT });\n this._server = app.listen(this._port);\n\n // TODO(burdon): Check plugin is registered.\n // TypeError: Cannot read properties of undefined (reading 'register')\n const { registrationId } = await this._client.services.services.FunctionRegistryService!.register({\n endpoint: this.endpoint!,\n functions: this.functions.map((name) => ({ name })),\n });\n this._registrationId = registrationId;\n }\n\n async stop() {\n const trigger = new Trigger();\n this._server?.close(async () => {\n if (this._registrationId) {\n await this._client.services.services.FunctionRegistryService!.unregister({\n registrationId: this._registrationId,\n });\n this._registrationId = undefined;\n }\n\n trigger.wake();\n });\n\n await trigger.wait();\n this._server = undefined;\n this._port = undefined;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport assert from 'node:assert';\n\nimport { DeferredTask } from '@dxos/async';\nimport { Client, PublicKey } from '@dxos/client';\nimport type { Space } from '@dxos/client/echo';\nimport { Context } from '@dxos/context';\nimport { createSubscription } from '@dxos/echo-schema';\nimport { log } from '@dxos/log';\nimport { ComplexMap } from '@dxos/util';\n\nimport { FunctionTrigger } from '../function';\n\n// TODO(burdon): Rename.\nexport type InvokeOptions = {\n endpoint: string;\n runtime: string;\n};\n\nexport class TriggerManager {\n private readonly _mounts = new ComplexMap<\n { name: string; spaceKey: PublicKey },\n { ctx: Context; trigger: FunctionTrigger }\n >(({ name, spaceKey }) => `${spaceKey.toHex()}:${name}`);\n\n constructor(\n private readonly _client: Client,\n private readonly _triggers: FunctionTrigger[],\n private readonly _invokeOptions: InvokeOptions,\n ) {}\n\n async start() {\n // TODO(burdon): Make runtime configurable (via CLI)?\n this._client.spaces.subscribe(async (spaces) => {\n for (const space of spaces) {\n await space.waitUntilReady();\n for (const trigger of this._triggers) {\n // TODO(burdon): New context? Shared?\n await this.mount(new Context(), space, trigger);\n }\n }\n });\n }\n\n async stop() {\n for (const { name, spaceKey } of this._mounts.keys()) {\n await this.unmount(name, spaceKey);\n }\n }\n\n private async mount(ctx: Context, space: Space, trigger: FunctionTrigger) {\n const key = { name: trigger.function, spaceKey: space.key };\n const exists = this._mounts.get(key);\n if (!exists) {\n this._mounts.set(key, { ctx, trigger });\n log('mount', { space: space.key, trigger });\n if (ctx.disposed) {\n return;\n }\n\n // TODO(burdon): Why DeferredTask? How to pass objectIds to function?\n const objectIds = new Set<string>();\n const task = new DeferredTask(ctx, async () => {\n await this.invokeFunction(this._invokeOptions, trigger.function, {\n space: space.key,\n objects: Array.from(objectIds),\n });\n });\n\n let count = 0;\n const subscription = createSubscription(({ added, updated }) => {\n for (const object of added) {\n objectIds.add(object.id);\n }\n for (const object of updated) {\n objectIds.add(object.id);\n }\n\n log('updated', {\n trigger,\n space: space.key,\n objects: objectIds.size,\n count,\n });\n\n task.schedule();\n count++;\n });\n\n ctx.onDispose(() => subscription.unsubscribe());\n\n // TODO(burdon): DSL for query (replace props).\n const query = space.db.query({ '@type': trigger.subscription.type, ...trigger.subscription.props });\n const unsubscribe = query.subscribe(({ objects }) => {\n subscription.update(objects);\n });\n\n // TODO(burdon): Option to trigger on first subscription.\n // TODO(burdon): After restart not triggered.\n\n ctx.onDispose(() => unsubscribe());\n }\n }\n\n private async unmount(name: string, spaceKey: PublicKey) {\n const key = { name, spaceKey };\n const { ctx } = this._mounts.get(key) ?? {};\n if (ctx) {\n this._mounts.delete(key);\n await ctx.dispose();\n }\n }\n\n private async invokeFunction(options: InvokeOptions, functionName: string, data: any) {\n const { endpoint, runtime } = options;\n assert(endpoint, 'Missing endpoint');\n assert(runtime, 'Missing runtime');\n\n try {\n log('invoke', { function: functionName });\n const url = `${endpoint}/${runtime}/${functionName}`;\n const res = await fetch(url, {\n method: 'POST',\n body: JSON.stringify(data),\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n\n log('result', { function: functionName, result: await res.json() });\n } catch (err: any) {\n log.error('error', { function: functionName, error: err.message });\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;AAIA,OAAOA,aAAa;AAEpB,SAASC,YAAY;AACrB,SAASC,sBAAsB;AAE/B,SAASC,eAAe;AAExB,SAASC,WAAW;;AAIpB,IAAMC,eAAe;AAUd,IAAMC,YAAN,MAAMA;;EAQXC,YACmBC,SACAC,UACjB;mBAFiBD;oBACAC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names": ["express", "join", "getPortPromise", "Trigger", "log", "DEFAULT_PORT", "DevServer", "constructor", "_client", "_options", "_functionHandlers", "port", "_port", "endpoint", "undefined", "functions", "Object", "keys", "initialize", "name", "_", "entries", "manifest", "module", "require", "join", "directory", "handler", "default", "Error", "err", "log", "error", "start", "app", "express", "use", "json", "post", "req", "res", "functionName", "params", "function", "data", "body", "builder", "status", "code", "statusCode", "succeed", "result", "end", "JSON", "stringify", "context", "client", "bind", "message", "getPortPromise", "startPort", "_server", "listen", "registrationId", "services", "FunctionRegistryService", "register", "map", "_registrationId", "stop", "trigger", "Trigger", "close", "unregister", "wake", "wait", "assert", "DeferredTask", "Context", "createSubscription", "log", "ComplexMap", "TriggerManager", "constructor", "_client", "_triggers", "_invokeOptions", "_mounts", "name", "spaceKey", "toHex", "start", "spaces", "subscribe", "space", "waitUntilReady", "trigger", "mount", "stop", "keys", "unmount", "ctx", "key", "function", "exists", "get", "set", "disposed", "objectIds", "Set", "task", "invokeFunction", "objects", "Array", "from", "count", "subscription", "added", "updated", "object", "add", "id", "size", "schedule", "onDispose", "unsubscribe", "query", "db", "type", "props", "update", "delete", "dispose", "options", "functionName", "data", "endpoint", "runtime", "url", "res", "fetch", "method", "body", "JSON", "stringify", "headers", "result", "json", "err", "error", "message"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/core/functions/src/function.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/core/functions/src/function.ts":{"bytes":1422,"imports":[]},"packages/core/functions/src/runtime/dev-server.ts":{"bytes":13335,"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"@dxos/node-std/path","kind":"import-statement","external":true},{"path":"portfinder","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"<runtime>","kind":"import-statement","external":true}]},"packages/core/functions/src/runtime/trigger-manager.ts":{"bytes":15730,"imports":[{"path":"@dxos/node-std/assert","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/core/functions/src/runtime/index.ts":{"bytes":584,"imports":[{"path":"packages/core/functions/src/runtime/dev-server.ts","kind":"import-statement","original":"./dev-server"},{"path":"packages/core/functions/src/runtime/trigger-manager.ts","kind":"import-statement","original":"./trigger-manager"}]},"packages/core/functions/src/index.ts":{"bytes":550,"imports":[{"path":"packages/core/functions/src/function.ts","kind":"import-statement","original":"./function"},{"path":"packages/core/functions/src/runtime/index.ts","kind":"import-statement","original":"./runtime"}]}},"outputs":{"packages/core/functions/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":13972},"packages/core/functions/dist/lib/browser/index.mjs":{"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"@dxos/node-std/path","kind":"import-statement","external":true},{"path":"portfinder","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/node-std/assert","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["DevServer","TriggerManager"],"entryPoint":"packages/core/functions/src/index.ts","inputs":{"packages/core/functions/src/index.ts":{"bytesInOutput":0},"packages/core/functions/src/runtime/dev-server.ts":{"bytesInOutput":3119},"packages/core/functions/src/runtime/index.ts":{"bytesInOutput":0},"packages/core/functions/src/runtime/trigger-manager.ts":{"bytesInOutput":3977}},"bytes":7692}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -41,6 +41,7 @@ var import_node_path = require("node:path");
|
|
|
41
41
|
var import_portfinder = require("portfinder");
|
|
42
42
|
var import_async = require("@dxos/async");
|
|
43
43
|
var import_log = require("@dxos/log");
|
|
44
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
|
|
44
45
|
var DEFAULT_PORT = 7e3;
|
|
45
46
|
var DevServer = class {
|
|
46
47
|
// prettier-ignore
|
|
@@ -69,10 +70,10 @@ var DevServer = class {
|
|
|
69
70
|
this._functionHandlers[name] = handler;
|
|
70
71
|
} catch (err) {
|
|
71
72
|
import_log.log.error("parsing function (check functions.yml manifest)", err, {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
F: __dxlog_file,
|
|
74
|
+
L: 63,
|
|
75
|
+
S: this,
|
|
76
|
+
C: (f, a) => f(...a)
|
|
76
77
|
});
|
|
77
78
|
}
|
|
78
79
|
}
|
|
@@ -86,10 +87,10 @@ var DevServer = class {
|
|
|
86
87
|
function: functionName,
|
|
87
88
|
data: req.body
|
|
88
89
|
}, {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
F: __dxlog_file,
|
|
91
|
+
L: 74,
|
|
92
|
+
S: this,
|
|
93
|
+
C: (f, a) => f(...a)
|
|
93
94
|
});
|
|
94
95
|
const builder = {
|
|
95
96
|
status: (code) => {
|
|
@@ -151,6 +152,7 @@ var import_context = require("@dxos/context");
|
|
|
151
152
|
var import_echo_schema = require("@dxos/echo-schema");
|
|
152
153
|
var import_log2 = require("@dxos/log");
|
|
153
154
|
var import_util = require("@dxos/util");
|
|
155
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/trigger-manager.ts";
|
|
154
156
|
var TriggerManager = class {
|
|
155
157
|
constructor(_client, _triggers, _invokeOptions) {
|
|
156
158
|
this._client = _client;
|
|
@@ -163,7 +165,7 @@ var TriggerManager = class {
|
|
|
163
165
|
for (const space of spaces) {
|
|
164
166
|
await space.waitUntilReady();
|
|
165
167
|
for (const trigger of this._triggers) {
|
|
166
|
-
await this.mount(new import_context.Context(),
|
|
168
|
+
await this.mount(new import_context.Context(), space, trigger);
|
|
167
169
|
}
|
|
168
170
|
}
|
|
169
171
|
});
|
|
@@ -173,7 +175,7 @@ var TriggerManager = class {
|
|
|
173
175
|
await this.unmount(name, spaceKey);
|
|
174
176
|
}
|
|
175
177
|
}
|
|
176
|
-
async mount(ctx,
|
|
178
|
+
async mount(ctx, space, trigger) {
|
|
177
179
|
const key = {
|
|
178
180
|
name: trigger.function,
|
|
179
181
|
spaceKey: space.key
|
|
@@ -184,60 +186,56 @@ var TriggerManager = class {
|
|
|
184
186
|
ctx,
|
|
185
187
|
trigger
|
|
186
188
|
});
|
|
189
|
+
(0, import_log2.log)("mount", {
|
|
190
|
+
space: space.key,
|
|
191
|
+
trigger
|
|
192
|
+
}, {
|
|
193
|
+
F: __dxlog_file2,
|
|
194
|
+
L: 59,
|
|
195
|
+
S: this,
|
|
196
|
+
C: (f, a) => f(...a)
|
|
197
|
+
});
|
|
187
198
|
if (ctx.disposed) {
|
|
188
199
|
return;
|
|
189
200
|
}
|
|
190
|
-
let count = 0;
|
|
191
201
|
const objectIds = /* @__PURE__ */ new Set();
|
|
192
202
|
const task = new import_async2.DeferredTask(ctx, async () => {
|
|
193
|
-
const updatedObjects = Array.from(objectIds);
|
|
194
|
-
objectIds.clear();
|
|
195
203
|
await this.invokeFunction(this._invokeOptions, trigger.function, {
|
|
196
204
|
space: space.key,
|
|
197
|
-
objects:
|
|
205
|
+
objects: Array.from(objectIds)
|
|
198
206
|
});
|
|
199
207
|
});
|
|
200
|
-
|
|
208
|
+
let count = 0;
|
|
209
|
+
const subscription = (0, import_echo_schema.createSubscription)(({ added, updated }) => {
|
|
201
210
|
for (const object of added) {
|
|
202
211
|
objectIds.add(object.id);
|
|
203
212
|
}
|
|
204
|
-
for (const
|
|
205
|
-
objectIds.add(
|
|
213
|
+
for (const object of updated) {
|
|
214
|
+
objectIds.add(object.id);
|
|
206
215
|
}
|
|
207
|
-
import_log2.log
|
|
216
|
+
(0, import_log2.log)("updated", {
|
|
217
|
+
trigger,
|
|
208
218
|
space: space.key,
|
|
209
219
|
objects: objectIds.size,
|
|
210
|
-
added: added.length,
|
|
211
|
-
updated: updated.length,
|
|
212
220
|
count
|
|
213
221
|
}, {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
222
|
+
F: __dxlog_file2,
|
|
223
|
+
L: 82,
|
|
224
|
+
S: this,
|
|
225
|
+
C: (f, a) => f(...a)
|
|
218
226
|
});
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
227
|
+
task.schedule();
|
|
228
|
+
count++;
|
|
222
229
|
});
|
|
223
|
-
ctx.onDispose(() =>
|
|
230
|
+
ctx.onDispose(() => subscription.unsubscribe());
|
|
224
231
|
const query = space.db.query({
|
|
225
|
-
|
|
226
|
-
|
|
232
|
+
"@type": trigger.subscription.type,
|
|
233
|
+
...trigger.subscription.props
|
|
227
234
|
});
|
|
228
235
|
const unsubscribe = query.subscribe(({ objects }) => {
|
|
229
|
-
|
|
230
|
-
});
|
|
231
|
-
ctx.onDispose(unsubscribe);
|
|
232
|
-
import_log2.log.info("mounted", {
|
|
233
|
-
space: space.key,
|
|
234
|
-
trigger
|
|
235
|
-
}, {
|
|
236
|
-
file: "trigger-manager.ts",
|
|
237
|
-
line: 109,
|
|
238
|
-
scope: this,
|
|
239
|
-
callSite: (f, a) => f(...a)
|
|
236
|
+
subscription.update(objects);
|
|
240
237
|
});
|
|
238
|
+
ctx.onDispose(() => unsubscribe());
|
|
241
239
|
}
|
|
242
240
|
}
|
|
243
241
|
async unmount(name, spaceKey) {
|
|
@@ -260,10 +258,10 @@ var TriggerManager = class {
|
|
|
260
258
|
(0, import_log2.log)("invoke", {
|
|
261
259
|
function: functionName
|
|
262
260
|
}, {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
261
|
+
F: __dxlog_file2,
|
|
262
|
+
L: 123,
|
|
263
|
+
S: this,
|
|
264
|
+
C: (f, a) => f(...a)
|
|
267
265
|
});
|
|
268
266
|
const url = `${endpoint}/${runtime}/${functionName}`;
|
|
269
267
|
const res = await fetch(url, {
|
|
@@ -277,20 +275,20 @@ var TriggerManager = class {
|
|
|
277
275
|
function: functionName,
|
|
278
276
|
result: await res.json()
|
|
279
277
|
}, {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
278
|
+
F: __dxlog_file2,
|
|
279
|
+
L: 133,
|
|
280
|
+
S: this,
|
|
281
|
+
C: (f, a) => f(...a)
|
|
284
282
|
});
|
|
285
283
|
} catch (err) {
|
|
286
284
|
import_log2.log.error("error", {
|
|
287
285
|
function: functionName,
|
|
288
286
|
error: err.message
|
|
289
287
|
}, {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
288
|
+
F: __dxlog_file2,
|
|
289
|
+
L: 135,
|
|
290
|
+
S: this,
|
|
291
|
+
C: (f, a) => f(...a)
|
|
294
292
|
});
|
|
295
293
|
}
|
|
296
294
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/index.ts", "../../../src/runtime/dev-server.ts", "../../../src/runtime/trigger-manager.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nexport * from './function';\nexport * from './runtime';\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport express from 'express';\nimport http from 'http';\nimport { join } from 'node:path';\nimport { getPortPromise } from 'portfinder';\n\nimport { Trigger } from '@dxos/async';\nimport { Client } from '@dxos/client';\nimport { log } from '@dxos/log';\n\nimport { FunctionContext, FunctionHandler, FunctionsManifest, Response } from '../function';\n\nconst DEFAULT_PORT = 7000;\n\nexport type DevServerOptions = {\n directory: string;\n manifest: FunctionsManifest;\n};\n\n/**\n * Functions dev server provides a local HTTP server for testing functions.\n */\nexport class DevServer {\n private readonly _functionHandlers: Record<string, FunctionHandler> = {};\n\n private _server?: http.Server;\n private _port?: number;\n private _registrationId?: string;\n\n // prettier-ignore\n constructor(\n private readonly _client: Client,\n private readonly _options: DevServerOptions\n ) {}\n\n get port() {\n return this._port;\n }\n\n get endpoint() {\n return this._port ? `http://localhost:${this._port}` : undefined;\n }\n\n get functions() {\n return Object.keys(this._functionHandlers);\n }\n\n async initialize() {\n for (const [name, _] of Object.entries(this._options.manifest.functions)) {\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const module = require(join(this._options.directory, name));\n const handler = module.default;\n if (typeof handler !== 'function') {\n throw new Error(`Handler must export default function: ${name}`);\n }\n\n this._functionHandlers[name] = handler;\n } catch (err) {\n log.error('parsing function (check functions.yml manifest)', err);\n }\n }\n }\n\n async start() {\n const app = express();\n app.use(express.json());\n\n app.post('/:functionName', async (req, res) => {\n const functionName = req.params.functionName;\n log('invoke', { function: functionName, data: req.body });\n\n const builder: Response = {\n status: (code: number) => {\n res.statusCode = code;\n return builder;\n },\n succeed: (result = {}) => {\n res.end(JSON.stringify(result));\n return builder;\n },\n };\n\n const context: FunctionContext = {\n client: this._client,\n status: builder.status.bind(builder),\n };\n\n void (async () => {\n try {\n await this._functionHandlers[functionName](req.body, context);\n } catch (err: any) {\n res.statusCode = 500;\n res.end(err.message);\n }\n })();\n });\n\n this._port = await getPortPromise({ startPort: DEFAULT_PORT });\n this._server = app.listen(this._port);\n\n // TODO(burdon): Check plugin is registered.\n // TypeError: Cannot read properties of undefined (reading 'register')\n const { registrationId } = await this._client.services.services.FunctionRegistryService!.register({\n endpoint: this.endpoint!,\n functions: this.functions.map((name) => ({ name })),\n });\n this._registrationId = registrationId;\n }\n\n async stop() {\n const trigger = new Trigger();\n this._server?.close(async () => {\n if (this._registrationId) {\n await this._client.services.services.FunctionRegistryService!.unregister({\n registrationId: this._registrationId,\n });\n this._registrationId = undefined;\n }\n\n trigger.wake();\n });\n\n await trigger.wait();\n this._server = undefined;\n this._port = undefined;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport assert from 'node:assert';\n\nimport { DeferredTask } from '@dxos/async';\nimport { Client, PublicKey
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACIA,qBAAoB;AAEpB,uBAAqB;AACrB,wBAA+B;AAE/B,mBAAwB;AAExB,iBAAoB
|
|
6
|
-
"names": ["DEFAULT_PORT", "DevServer", "constructor", "_client", "_options", "_functionHandlers", "port", "_port", "endpoint", "undefined", "functions", "Object", "keys", "initialize", "name", "_", "entries", "manifest", "module", "require", "join", "directory", "handler", "default", "Error", "err", "log", "error", "start", "app", "express", "use", "json", "post", "req", "res", "functionName", "params", "function", "data", "body", "builder", "status", "code", "statusCode", "succeed", "result", "end", "JSON", "stringify", "context", "client", "bind", "message", "getPortPromise", "startPort", "_server", "listen", "registrationId", "services", "FunctionRegistryService", "register", "map", "_registrationId", "stop", "trigger", "Trigger", "close", "unregister", "wake", "wait", "import_async", "import_log", "TriggerManager", "constructor", "_client", "_triggers", "_invokeOptions", "_mounts", "ComplexMap", "name", "spaceKey", "toHex", "start", "spaces", "subscribe", "space", "waitUntilReady", "trigger", "mount", "Context", "stop", "keys", "unmount", "ctx", "key", "function", "exists", "get", "set", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nexport * from './function';\nexport * from './runtime';\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport express from 'express';\nimport http from 'http';\nimport { join } from 'node:path';\nimport { getPortPromise } from 'portfinder';\n\nimport { Trigger } from '@dxos/async';\nimport { Client } from '@dxos/client';\nimport { log } from '@dxos/log';\n\nimport { FunctionContext, FunctionHandler, FunctionsManifest, Response } from '../function';\n\nconst DEFAULT_PORT = 7000;\n\nexport type DevServerOptions = {\n directory: string;\n manifest: FunctionsManifest;\n};\n\n/**\n * Functions dev server provides a local HTTP server for testing functions.\n */\nexport class DevServer {\n private readonly _functionHandlers: Record<string, FunctionHandler> = {};\n\n private _server?: http.Server;\n private _port?: number;\n private _registrationId?: string;\n\n // prettier-ignore\n constructor(\n private readonly _client: Client,\n private readonly _options: DevServerOptions\n ) {}\n\n get port() {\n return this._port;\n }\n\n get endpoint() {\n return this._port ? `http://localhost:${this._port}` : undefined;\n }\n\n get functions() {\n return Object.keys(this._functionHandlers);\n }\n\n async initialize() {\n for (const [name, _] of Object.entries(this._options.manifest.functions)) {\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const module = require(join(this._options.directory, name));\n const handler = module.default;\n if (typeof handler !== 'function') {\n throw new Error(`Handler must export default function: ${name}`);\n }\n\n this._functionHandlers[name] = handler;\n } catch (err) {\n log.error('parsing function (check functions.yml manifest)', err);\n }\n }\n }\n\n async start() {\n const app = express();\n app.use(express.json());\n\n app.post('/:functionName', async (req, res) => {\n const functionName = req.params.functionName;\n log('invoke', { function: functionName, data: req.body });\n\n const builder: Response = {\n status: (code: number) => {\n res.statusCode = code;\n return builder;\n },\n succeed: (result = {}) => {\n res.end(JSON.stringify(result));\n return builder;\n },\n };\n\n const context: FunctionContext = {\n client: this._client,\n status: builder.status.bind(builder),\n };\n\n void (async () => {\n try {\n await this._functionHandlers[functionName](req.body, context);\n } catch (err: any) {\n res.statusCode = 500;\n res.end(err.message);\n }\n })();\n });\n\n this._port = await getPortPromise({ startPort: DEFAULT_PORT });\n this._server = app.listen(this._port);\n\n // TODO(burdon): Check plugin is registered.\n // TypeError: Cannot read properties of undefined (reading 'register')\n const { registrationId } = await this._client.services.services.FunctionRegistryService!.register({\n endpoint: this.endpoint!,\n functions: this.functions.map((name) => ({ name })),\n });\n this._registrationId = registrationId;\n }\n\n async stop() {\n const trigger = new Trigger();\n this._server?.close(async () => {\n if (this._registrationId) {\n await this._client.services.services.FunctionRegistryService!.unregister({\n registrationId: this._registrationId,\n });\n this._registrationId = undefined;\n }\n\n trigger.wake();\n });\n\n await trigger.wait();\n this._server = undefined;\n this._port = undefined;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport assert from 'node:assert';\n\nimport { DeferredTask } from '@dxos/async';\nimport { Client, PublicKey } from '@dxos/client';\nimport type { Space } from '@dxos/client/echo';\nimport { Context } from '@dxos/context';\nimport { createSubscription } from '@dxos/echo-schema';\nimport { log } from '@dxos/log';\nimport { ComplexMap } from '@dxos/util';\n\nimport { FunctionTrigger } from '../function';\n\n// TODO(burdon): Rename.\nexport type InvokeOptions = {\n endpoint: string;\n runtime: string;\n};\n\nexport class TriggerManager {\n private readonly _mounts = new ComplexMap<\n { name: string; spaceKey: PublicKey },\n { ctx: Context; trigger: FunctionTrigger }\n >(({ name, spaceKey }) => `${spaceKey.toHex()}:${name}`);\n\n constructor(\n private readonly _client: Client,\n private readonly _triggers: FunctionTrigger[],\n private readonly _invokeOptions: InvokeOptions,\n ) {}\n\n async start() {\n // TODO(burdon): Make runtime configurable (via CLI)?\n this._client.spaces.subscribe(async (spaces) => {\n for (const space of spaces) {\n await space.waitUntilReady();\n for (const trigger of this._triggers) {\n // TODO(burdon): New context? Shared?\n await this.mount(new Context(), space, trigger);\n }\n }\n });\n }\n\n async stop() {\n for (const { name, spaceKey } of this._mounts.keys()) {\n await this.unmount(name, spaceKey);\n }\n }\n\n private async mount(ctx: Context, space: Space, trigger: FunctionTrigger) {\n const key = { name: trigger.function, spaceKey: space.key };\n const exists = this._mounts.get(key);\n if (!exists) {\n this._mounts.set(key, { ctx, trigger });\n log('mount', { space: space.key, trigger });\n if (ctx.disposed) {\n return;\n }\n\n // TODO(burdon): Why DeferredTask? How to pass objectIds to function?\n const objectIds = new Set<string>();\n const task = new DeferredTask(ctx, async () => {\n await this.invokeFunction(this._invokeOptions, trigger.function, {\n space: space.key,\n objects: Array.from(objectIds),\n });\n });\n\n let count = 0;\n const subscription = createSubscription(({ added, updated }) => {\n for (const object of added) {\n objectIds.add(object.id);\n }\n for (const object of updated) {\n objectIds.add(object.id);\n }\n\n log('updated', {\n trigger,\n space: space.key,\n objects: objectIds.size,\n count,\n });\n\n task.schedule();\n count++;\n });\n\n ctx.onDispose(() => subscription.unsubscribe());\n\n // TODO(burdon): DSL for query (replace props).\n const query = space.db.query({ '@type': trigger.subscription.type, ...trigger.subscription.props });\n const unsubscribe = query.subscribe(({ objects }) => {\n subscription.update(objects);\n });\n\n // TODO(burdon): Option to trigger on first subscription.\n // TODO(burdon): After restart not triggered.\n\n ctx.onDispose(() => unsubscribe());\n }\n }\n\n private async unmount(name: string, spaceKey: PublicKey) {\n const key = { name, spaceKey };\n const { ctx } = this._mounts.get(key) ?? {};\n if (ctx) {\n this._mounts.delete(key);\n await ctx.dispose();\n }\n }\n\n private async invokeFunction(options: InvokeOptions, functionName: string, data: any) {\n const { endpoint, runtime } = options;\n assert(endpoint, 'Missing endpoint');\n assert(runtime, 'Missing runtime');\n\n try {\n log('invoke', { function: functionName });\n const url = `${endpoint}/${runtime}/${functionName}`;\n const res = await fetch(url, {\n method: 'POST',\n body: JSON.stringify(data),\n headers: {\n 'Content-Type': 'application/json',\n },\n });\n\n log('result', { function: functionName, result: await res.json() });\n } catch (err: any) {\n log.error('error', { function: functionName, error: err.message });\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACIA,qBAAoB;AAEpB,uBAAqB;AACrB,wBAA+B;AAE/B,mBAAwB;AAExB,iBAAoB;;AAIpB,IAAMA,eAAe;AAUd,IAAMC,YAAN,MAAMA;;EAQXC,YACmBC,SACAC,UACjB;mBAFiBD;oBACAC;SATFC,oBAAqD,CAAC;EAUpE;EAEH,IAAIC,OAAO;AACT,WAAO,KAAKC;EACd;EAEA,IAAIC,WAAW;AACb,WAAO,KAAKD,QAAQ,oBAAoB,KAAKA,UAAUE;EACzD;EAEA,IAAIC,YAAY;AACd,WAAOC,OAAOC,KAAK,KAAKP,iBAAiB;EAC3C;EAEA,MAAMQ,aAAa;AACjB,eAAW,CAACC,MAAMC,CA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CAAA;AAKAb,UAAI0B,UAAU,MAAMC,YAAAA,CAAAA;IACtB;EACF;EAEA,MAAc5B,QAAQb,MAAcC,UAAqB;AA3G3D;AA4GI,UAAMc,MAAM;MAAEf;MAAMC;IAAS;AAC7B,UAAM,EAAEa,IAAG,KAAK,UAAKhB,QAAQoB,IAAIH,GAAAA,MAAjB,YAAyB,CAAC;AAC1C,QAAID,KAAK;AACP,WAAKhB,QAAQiD,OAAOhC,GAAAA;AACpB,YAAMD,IAAIkC,QAAO;IACnB;EACF;EAEA,MAActB,eAAeuB,SAAwBC,cAAsBC,MAAW;AACpF,UAAM,EAAEC,UAAUC,QAAO,IAAKJ;AAC9BK,2BAAAA,SAAOF,UAAU,kBAAA;AACjBE,2BAAAA,SAAOD,SAAS,iBAAA;AAEhB,QAAI;AACFjC,2BAAI,UAAU;QAAEJ,UAAUkC;MAAa,GAAA;;;;;;AACvC,YAAMK,MAAM,GAAGH,YAAYC,WAAWH;AACtC,YAAMM,MAAM,MAAMC,MAAMF,KAAK;QAC3BG,QAAQ;QACRC,MAAMC,KAAKC,UAAUV,IAAAA;QACrBW,SAAS;UACP,gBAAgB;QAClB;MACF,CAAA;AAEA1C,2BAAI,UAAU;QAAEJ,UAAUkC;QAAca,QAAQ,MAAMP,IAAIQ,KAAI;MAAG,GAAA;;;;;;IACnE,SAASC,KAAP;AACA7C,sBAAI8C,MAAM,SAAS;QAAElD,UAAUkC;QAAcgB,OAAOD,IAAIE;MAAQ,GAAA;;;;;;IAClE;EACF;AACF;",
|
|
6
|
+
"names": ["DEFAULT_PORT", "DevServer", "constructor", "_client", "_options", "_functionHandlers", "port", "_port", "endpoint", "undefined", "functions", "Object", "keys", "initialize", "name", "_", "entries", "manifest", "module", "require", "join", "directory", "handler", "default", "Error", "err", "log", "error", "start", "app", "express", "use", "json", "post", "req", "res", "functionName", "params", "function", "data", "body", "builder", "status", "code", "statusCode", "succeed", "result", "end", "JSON", "stringify", "context", "client", "bind", "message", "getPortPromise", "startPort", "_server", "listen", "registrationId", "services", "FunctionRegistryService", "register", "map", "_registrationId", "stop", "trigger", "Trigger", "close", "unregister", "wake", "wait", "import_async", "import_log", "TriggerManager", "constructor", "_client", "_triggers", "_invokeOptions", "_mounts", "ComplexMap", "name", "spaceKey", "toHex", "start", "spaces", "subscribe", "space", "waitUntilReady", "trigger", "mount", "Context", "stop", "keys", "unmount", "ctx", "key", "function", "exists", "get", "set", "log", "disposed", "objectIds", "Set", "task", "DeferredTask", "invokeFunction", "objects", "Array", "from", "count", "subscription", "createSubscription", "added", "updated", "object", "add", "id", "size", "schedule", "onDispose", "unsubscribe", "query", "db", "type", "props", "update", "delete", "dispose", "options", "functionName", "data", "endpoint", "runtime", "assert", "url", "res", "fetch", "method", "body", "JSON", "stringify", "headers", "result", "json", "err", "error", "message"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/core/functions/src/function.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/core/functions/src/function.ts":{"bytes":1422,"imports":[]},"packages/core/functions/src/runtime/dev-server.ts":{"bytes":13335,"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"node:path","kind":"import-statement","external":true},{"path":"portfinder","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}]},"packages/core/functions/src/runtime/trigger-manager.ts":{"bytes":15730,"imports":[{"path":"node:assert","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/core/functions/src/runtime/index.ts":{"bytes":584,"imports":[{"path":"packages/core/functions/src/runtime/dev-server.ts","kind":"import-statement","original":"./dev-server"},{"path":"packages/core/functions/src/runtime/trigger-manager.ts","kind":"import-statement","original":"./trigger-manager"}]},"packages/core/functions/src/index.ts":{"bytes":550,"imports":[{"path":"packages/core/functions/src/function.ts","kind":"import-statement","original":"./function"},{"path":"packages/core/functions/src/runtime/index.ts","kind":"import-statement","original":"./runtime"}]}},"outputs":{"packages/core/functions/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":14057},"packages/core/functions/dist/lib/node/index.cjs":{"imports":[{"path":"express","kind":"require-call","external":true},{"path":"node:path","kind":"require-call","external":true},{"path":"portfinder","kind":"require-call","external":true},{"path":"@dxos/async","kind":"require-call","external":true},{"path":"@dxos/log","kind":"require-call","external":true},{"path":"node:assert","kind":"require-call","external":true},{"path":"@dxos/async","kind":"require-call","external":true},{"path":"@dxos/context","kind":"require-call","external":true},{"path":"@dxos/echo-schema","kind":"require-call","external":true},{"path":"@dxos/log","kind":"require-call","external":true},{"path":"@dxos/util","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/core/functions/src/index.ts","inputs":{"packages/core/functions/src/index.ts":{"bytesInOutput":163},"packages/core/functions/src/runtime/dev-server.ts":{"bytesInOutput":3271},"packages/core/functions/src/runtime/index.ts":{"bytesInOutput":0},"packages/core/functions/src/runtime/trigger-manager.ts":{"bytesInOutput":4185}},"bytes":9325}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trigger-manager.d.ts","sourceRoot":"","sources":["../../../../src/runtime/trigger-manager.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"trigger-manager.d.ts","sourceRoot":"","sources":["../../../../src/runtime/trigger-manager.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,MAAM,EAAa,MAAM,cAAc,CAAC;AAOjD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,qBAAa,cAAc;IAOvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,cAAc;IARjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAGiC;gBAGtC,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,eAAe,EAAE,EAC5B,cAAc,EAAE,aAAa;IAG1C,KAAK;IAaL,IAAI;YAMI,KAAK;YAsDL,OAAO;YASP,cAAc;CAqB7B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.1.53-main.
|
|
3
|
+
"version": "0.1.53-main.e9dc2bc",
|
|
4
4
|
"description": "Functions SDK and runtime.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"express": "4.17.1",
|
|
23
23
|
"portfinder": "^1.0.32",
|
|
24
|
-
"@dxos/async": "0.1.53-main.
|
|
25
|
-
"@dxos/client": "0.1.53-main.
|
|
26
|
-
"@dxos/context": "0.1.53-main.
|
|
27
|
-
"@dxos/echo-schema": "0.1.53-main.
|
|
28
|
-
"@dxos/log": "0.1.53-main.
|
|
29
|
-
"@dxos/node-std": "0.1.53-main.
|
|
30
|
-
"@dxos/util": "0.1.53-main.
|
|
24
|
+
"@dxos/async": "0.1.53-main.e9dc2bc",
|
|
25
|
+
"@dxos/client": "0.1.53-main.e9dc2bc",
|
|
26
|
+
"@dxos/context": "0.1.53-main.e9dc2bc",
|
|
27
|
+
"@dxos/echo-schema": "0.1.53-main.e9dc2bc",
|
|
28
|
+
"@dxos/log": "0.1.53-main.e9dc2bc",
|
|
29
|
+
"@dxos/node-std": "0.1.53-main.e9dc2bc",
|
|
30
|
+
"@dxos/util": "0.1.53-main.e9dc2bc"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/express": "^4.17.17"
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
import assert from 'node:assert';
|
|
6
6
|
|
|
7
7
|
import { DeferredTask } from '@dxos/async';
|
|
8
|
-
import { Client, PublicKey
|
|
8
|
+
import { Client, PublicKey } from '@dxos/client';
|
|
9
|
+
import type { Space } from '@dxos/client/echo';
|
|
9
10
|
import { Context } from '@dxos/context';
|
|
10
11
|
import { createSubscription } from '@dxos/echo-schema';
|
|
11
12
|
import { log } from '@dxos/log';
|
|
@@ -38,7 +39,7 @@ export class TriggerManager {
|
|
|
38
39
|
await space.waitUntilReady();
|
|
39
40
|
for (const trigger of this._triggers) {
|
|
40
41
|
// TODO(burdon): New context? Shared?
|
|
41
|
-
await this.mount(new Context(),
|
|
42
|
+
await this.mount(new Context(), space, trigger);
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
});
|
|
@@ -50,31 +51,27 @@ export class TriggerManager {
|
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
private async mount(ctx: Context,
|
|
54
|
+
private async mount(ctx: Context, space: Space, trigger: FunctionTrigger) {
|
|
54
55
|
const key = { name: trigger.function, spaceKey: space.key };
|
|
55
56
|
const exists = this._mounts.get(key);
|
|
56
57
|
if (!exists) {
|
|
57
58
|
this._mounts.set(key, { ctx, trigger });
|
|
59
|
+
log('mount', { space: space.key, trigger });
|
|
58
60
|
if (ctx.disposed) {
|
|
59
61
|
return;
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
// TODO(burdon):
|
|
63
|
-
|
|
64
|
-
let count = 0;
|
|
64
|
+
// TODO(burdon): Why DeferredTask? How to pass objectIds to function?
|
|
65
65
|
const objectIds = new Set<string>();
|
|
66
66
|
const task = new DeferredTask(ctx, async () => {
|
|
67
|
-
const updatedObjects = Array.from(objectIds);
|
|
68
|
-
objectIds.clear();
|
|
69
|
-
|
|
70
67
|
await this.invokeFunction(this._invokeOptions, trigger.function, {
|
|
71
68
|
space: space.key,
|
|
72
|
-
objects:
|
|
69
|
+
objects: Array.from(objectIds),
|
|
73
70
|
});
|
|
74
71
|
});
|
|
75
72
|
|
|
76
|
-
|
|
77
|
-
const
|
|
73
|
+
let count = 0;
|
|
74
|
+
const subscription = createSubscription(({ added, updated }) => {
|
|
78
75
|
for (const object of added) {
|
|
79
76
|
objectIds.add(object.id);
|
|
80
77
|
}
|
|
@@ -82,31 +79,29 @@ export class TriggerManager {
|
|
|
82
79
|
objectIds.add(object.id);
|
|
83
80
|
}
|
|
84
81
|
|
|
85
|
-
log
|
|
82
|
+
log('updated', {
|
|
83
|
+
trigger,
|
|
86
84
|
space: space.key,
|
|
87
85
|
objects: objectIds.size,
|
|
88
|
-
added: added.length,
|
|
89
|
-
updated: updated.length,
|
|
90
86
|
count,
|
|
91
87
|
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
88
|
+
|
|
89
|
+
task.schedule();
|
|
90
|
+
count++;
|
|
95
91
|
});
|
|
96
92
|
|
|
97
|
-
ctx.onDispose(() =>
|
|
93
|
+
ctx.onDispose(() => subscription.unsubscribe());
|
|
98
94
|
|
|
99
|
-
|
|
95
|
+
// TODO(burdon): DSL for query (replace props).
|
|
96
|
+
const query = space.db.query({ '@type': trigger.subscription.type, ...trigger.subscription.props });
|
|
100
97
|
const unsubscribe = query.subscribe(({ objects }) => {
|
|
101
|
-
|
|
98
|
+
subscription.update(objects);
|
|
102
99
|
});
|
|
103
100
|
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
ctx.onDispose(unsubscribe);
|
|
101
|
+
// TODO(burdon): Option to trigger on first subscription.
|
|
102
|
+
// TODO(burdon): After restart not triggered.
|
|
108
103
|
|
|
109
|
-
|
|
104
|
+
ctx.onDispose(() => unsubscribe());
|
|
110
105
|
}
|
|
111
106
|
}
|
|
112
107
|
|