@dxos/functions 0.8.1 → 0.8.2-main.2f9c567

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.
Files changed (168) hide show
  1. package/dist/lib/browser/bundler/index.mjs +0 -3
  2. package/dist/lib/browser/bundler/index.mjs.map +1 -1
  3. package/dist/lib/browser/edge/index.mjs +63 -7
  4. package/dist/lib/browser/edge/index.mjs.map +4 -4
  5. package/dist/lib/browser/index.mjs +368 -98
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/node/bundler/index.cjs +0 -1
  9. package/dist/lib/node/bundler/index.cjs.map +1 -1
  10. package/dist/lib/node/edge/index.cjs +65 -5
  11. package/dist/lib/node/edge/index.cjs.map +4 -4
  12. package/dist/lib/node/index.cjs +383 -93
  13. package/dist/lib/node/index.cjs.map +4 -4
  14. package/dist/lib/node/meta.json +1 -1
  15. package/dist/lib/node-esm/bundler/index.mjs +0 -1
  16. package/dist/lib/node-esm/bundler/index.mjs.map +1 -1
  17. package/dist/lib/node-esm/edge/index.mjs +64 -6
  18. package/dist/lib/node-esm/edge/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/index.mjs +368 -96
  20. package/dist/lib/node-esm/index.mjs.map +4 -4
  21. package/dist/lib/node-esm/meta.json +1 -1
  22. package/dist/types/src/edge/index.d.ts.map +1 -1
  23. package/dist/types/src/handler.d.ts +15 -63
  24. package/dist/types/src/handler.d.ts.map +1 -1
  25. package/dist/types/src/index.d.ts +3 -3
  26. package/dist/types/src/index.d.ts.map +1 -1
  27. package/dist/types/src/schema.d.ts +57 -0
  28. package/dist/types/src/schema.d.ts.map +1 -0
  29. package/dist/types/src/trace.d.ts +148 -0
  30. package/dist/types/src/trace.d.ts.map +1 -0
  31. package/dist/types/src/{types/types.d.ts → types.d.ts} +191 -49
  32. package/dist/types/src/types.d.ts.map +1 -0
  33. package/dist/types/src/{types/url.d.ts → url.d.ts} +6 -0
  34. package/dist/types/src/url.d.ts.map +1 -0
  35. package/package.json +20 -35
  36. package/src/edge/index.ts +4 -0
  37. package/src/handler.ts +17 -120
  38. package/src/index.ts +4 -5
  39. package/src/schema.ts +53 -0
  40. package/src/{types/trace.ts → trace.ts} +33 -31
  41. package/src/translations.ts +1 -1
  42. package/src/types.ts +214 -0
  43. package/src/{types/url.ts → url.ts} +5 -0
  44. package/dist/lib/browser/chunk-HI7YZO2K.mjs +0 -482
  45. package/dist/lib/browser/chunk-HI7YZO2K.mjs.map +0 -7
  46. package/dist/lib/browser/chunk-LT4LR4VU.mjs +0 -72
  47. package/dist/lib/browser/chunk-LT4LR4VU.mjs.map +0 -7
  48. package/dist/lib/browser/chunk-RVSG6WTL.mjs +0 -358
  49. package/dist/lib/browser/chunk-RVSG6WTL.mjs.map +0 -7
  50. package/dist/lib/browser/chunk-XRCXIG74.mjs +0 -12
  51. package/dist/lib/browser/chunk-XRCXIG74.mjs.map +0 -7
  52. package/dist/lib/browser/testing/index.mjs +0 -670
  53. package/dist/lib/browser/testing/index.mjs.map +0 -7
  54. package/dist/lib/browser/types/index.mjs +0 -49
  55. package/dist/lib/browser/types/index.mjs.map +0 -7
  56. package/dist/lib/node/chunk-DSUGRAAL.cjs +0 -392
  57. package/dist/lib/node/chunk-DSUGRAAL.cjs.map +0 -7
  58. package/dist/lib/node/chunk-JEQ2X3Z6.cjs +0 -34
  59. package/dist/lib/node/chunk-JEQ2X3Z6.cjs.map +0 -7
  60. package/dist/lib/node/chunk-NXZNXVT3.cjs +0 -94
  61. package/dist/lib/node/chunk-NXZNXVT3.cjs.map +0 -7
  62. package/dist/lib/node/chunk-RXMCVAMJ.cjs +0 -496
  63. package/dist/lib/node/chunk-RXMCVAMJ.cjs.map +0 -7
  64. package/dist/lib/node/testing/index.cjs +0 -687
  65. package/dist/lib/node/testing/index.cjs.map +0 -7
  66. package/dist/lib/node/types/index.cjs +0 -70
  67. package/dist/lib/node/types/index.cjs.map +0 -7
  68. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs +0 -12
  69. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs.map +0 -7
  70. package/dist/lib/node-esm/chunk-HBD2FZXO.mjs +0 -358
  71. package/dist/lib/node-esm/chunk-HBD2FZXO.mjs.map +0 -7
  72. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs +0 -72
  73. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs.map +0 -7
  74. package/dist/lib/node-esm/chunk-SQSJO5HI.mjs +0 -482
  75. package/dist/lib/node-esm/chunk-SQSJO5HI.mjs.map +0 -7
  76. package/dist/lib/node-esm/testing/index.mjs +0 -670
  77. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  78. package/dist/lib/node-esm/types/index.mjs +0 -49
  79. package/dist/lib/node-esm/types/index.mjs.map +0 -7
  80. package/dist/types/src/browser/index.d.ts +0 -2
  81. package/dist/types/src/browser/index.d.ts.map +0 -1
  82. package/dist/types/src/function/function-registry.d.ts +0 -25
  83. package/dist/types/src/function/function-registry.d.ts.map +0 -1
  84. package/dist/types/src/function/function-registry.test.d.ts +0 -2
  85. package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
  86. package/dist/types/src/function/index.d.ts +0 -2
  87. package/dist/types/src/function/index.d.ts.map +0 -1
  88. package/dist/types/src/runtime/dev-server.d.ts +0 -52
  89. package/dist/types/src/runtime/dev-server.d.ts.map +0 -1
  90. package/dist/types/src/runtime/dev-server.test.d.ts +0 -2
  91. package/dist/types/src/runtime/dev-server.test.d.ts.map +0 -1
  92. package/dist/types/src/runtime/index.d.ts +0 -3
  93. package/dist/types/src/runtime/index.d.ts.map +0 -1
  94. package/dist/types/src/runtime/scheduler.d.ts +0 -34
  95. package/dist/types/src/runtime/scheduler.d.ts.map +0 -1
  96. package/dist/types/src/runtime/scheduler.test.d.ts +0 -2
  97. package/dist/types/src/runtime/scheduler.test.d.ts.map +0 -1
  98. package/dist/types/src/testing/functions-integration.test.d.ts +0 -2
  99. package/dist/types/src/testing/functions-integration.test.d.ts.map +0 -1
  100. package/dist/types/src/testing/index.d.ts +0 -5
  101. package/dist/types/src/testing/index.d.ts.map +0 -1
  102. package/dist/types/src/testing/manifest.d.ts +0 -3
  103. package/dist/types/src/testing/manifest.d.ts.map +0 -1
  104. package/dist/types/src/testing/plugin-init.d.ts +0 -6
  105. package/dist/types/src/testing/plugin-init.d.ts.map +0 -1
  106. package/dist/types/src/testing/setup.d.ts +0 -15
  107. package/dist/types/src/testing/setup.d.ts.map +0 -1
  108. package/dist/types/src/testing/test/handler.d.ts +0 -4
  109. package/dist/types/src/testing/test/handler.d.ts.map +0 -1
  110. package/dist/types/src/testing/test/index.d.ts +0 -3
  111. package/dist/types/src/testing/test/index.d.ts.map +0 -1
  112. package/dist/types/src/testing/types.d.ts +0 -10
  113. package/dist/types/src/testing/types.d.ts.map +0 -1
  114. package/dist/types/src/testing/util.d.ts +0 -5
  115. package/dist/types/src/testing/util.d.ts.map +0 -1
  116. package/dist/types/src/trigger/index.d.ts +0 -3
  117. package/dist/types/src/trigger/index.d.ts.map +0 -1
  118. package/dist/types/src/trigger/trigger-registry.d.ts +0 -38
  119. package/dist/types/src/trigger/trigger-registry.d.ts.map +0 -1
  120. package/dist/types/src/trigger/trigger-registry.test.d.ts +0 -2
  121. package/dist/types/src/trigger/trigger-registry.test.d.ts.map +0 -1
  122. package/dist/types/src/trigger/type/index.d.ts +0 -3
  123. package/dist/types/src/trigger/type/index.d.ts.map +0 -1
  124. package/dist/types/src/trigger/type/subscription-trigger.d.ts +0 -4
  125. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +0 -1
  126. package/dist/types/src/trigger/type/timer-trigger.d.ts +0 -4
  127. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +0 -1
  128. package/dist/types/src/trigger/type/webhook-trigger.d.ts +0 -4
  129. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +0 -1
  130. package/dist/types/src/types/index.d.ts +0 -5
  131. package/dist/types/src/types/index.d.ts.map +0 -1
  132. package/dist/types/src/types/schema.d.ts +0 -53
  133. package/dist/types/src/types/schema.d.ts.map +0 -1
  134. package/dist/types/src/types/trace.d.ts +0 -146
  135. package/dist/types/src/types/trace.d.ts.map +0 -1
  136. package/dist/types/src/types/types.d.ts.map +0 -1
  137. package/dist/types/src/types/url.d.ts.map +0 -1
  138. package/dist/types/tools/schema.d.ts +0 -2
  139. package/dist/types/tools/schema.d.ts.map +0 -1
  140. package/schema/functions.json +0 -211
  141. package/src/browser/index.ts +0 -5
  142. package/src/function/function-registry.test.ts +0 -118
  143. package/src/function/function-registry.ts +0 -104
  144. package/src/function/index.ts +0 -5
  145. package/src/runtime/dev-server.test.ts +0 -79
  146. package/src/runtime/dev-server.ts +0 -240
  147. package/src/runtime/index.ts +0 -6
  148. package/src/runtime/scheduler.test.ts +0 -152
  149. package/src/runtime/scheduler.ts +0 -170
  150. package/src/testing/functions-integration.test.ts +0 -65
  151. package/src/testing/index.ts +0 -8
  152. package/src/testing/manifest.ts +0 -15
  153. package/src/testing/plugin-init.ts +0 -20
  154. package/src/testing/setup.ts +0 -109
  155. package/src/testing/test/handler.ts +0 -15
  156. package/src/testing/test/index.ts +0 -7
  157. package/src/testing/types.ts +0 -9
  158. package/src/testing/util.ts +0 -26
  159. package/src/trigger/index.ts +0 -6
  160. package/src/trigger/trigger-registry.test.ts +0 -278
  161. package/src/trigger/trigger-registry.ts +0 -218
  162. package/src/trigger/type/index.ts +0 -7
  163. package/src/trigger/type/subscription-trigger.ts +0 -84
  164. package/src/trigger/type/timer-trigger.ts +0 -48
  165. package/src/trigger/type/webhook-trigger.ts +0 -48
  166. package/src/types/index.ts +0 -8
  167. package/src/types/schema.ts +0 -46
  168. package/src/types/types.ts +0 -163
@@ -1,670 +0,0 @@
1
- import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
- import {
3
- FunctionRegistry,
4
- TriggerRegistry
5
- } from "../chunk-SQSJO5HI.mjs";
6
- import {
7
- FunctionDef,
8
- FunctionTrigger
9
- } from "../chunk-HBD2FZXO.mjs";
10
- import {
11
- __require
12
- } from "../chunk-DHGBFXSZ.mjs";
13
-
14
- // packages/core/functions/src/testing/setup.ts
15
- import { getRandomPort } from "get-port-please";
16
- import path2 from "node:path";
17
- import { waitForCondition } from "@dxos/async";
18
- import { Client, Config } from "@dxos/client";
19
- import { range } from "@dxos/util";
20
-
21
- // packages/core/functions/src/testing/types.ts
22
- import { S, TypedObject } from "@dxos/echo-schema";
23
- var TestType = class extends TypedObject({
24
- typename: "example.com/type/Test",
25
- version: "0.1.0"
26
- })({
27
- title: S.String
28
- }) {
29
- };
30
-
31
- // packages/core/functions/src/runtime/dev-server.ts
32
- import express from "express";
33
- import { getPort } from "get-port-please";
34
- import { join } from "node:path";
35
- import { asyncTimeout, Event, Trigger } from "@dxos/async";
36
- import { Context } from "@dxos/context";
37
- import { invariant } from "@dxos/invariant";
38
- import { log } from "@dxos/log";
39
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
40
- var FN_TIMEOUT = 2e4;
41
- var DevServer = class {
42
- constructor(_client, _functionsRegistry, _options) {
43
- this._client = _client;
44
- this._functionsRegistry = _functionsRegistry;
45
- this._options = _options;
46
- this._ctx = createContext();
47
- this._handlers = {};
48
- this._seq = 0;
49
- this.update = new Event();
50
- }
51
- get stats() {
52
- return {
53
- seq: this._seq
54
- };
55
- }
56
- get endpoint() {
57
- invariant(this._port, void 0, {
58
- F: __dxlog_file,
59
- L: 60,
60
- S: this,
61
- A: [
62
- "this._port",
63
- ""
64
- ]
65
- });
66
- return `http://localhost:${this._port}`;
67
- }
68
- get proxy() {
69
- return this._proxy;
70
- }
71
- get functions() {
72
- return Object.values(this._handlers);
73
- }
74
- async start() {
75
- invariant(!this._server, void 0, {
76
- F: __dxlog_file,
77
- L: 73,
78
- S: this,
79
- A: [
80
- "!this._server",
81
- ""
82
- ]
83
- });
84
- log.info("starting...", void 0, {
85
- F: __dxlog_file,
86
- L: 74,
87
- S: this,
88
- C: (f, a) => f(...a)
89
- });
90
- this._ctx = createContext();
91
- const app = express();
92
- app.use(express.json());
93
- app.post("/:path", async (req, res) => {
94
- const { path: path3 } = req.params;
95
- try {
96
- log.info("calling", {
97
- path: path3
98
- }, {
99
- F: __dxlog_file,
100
- L: 84,
101
- S: this,
102
- C: (f, a) => f(...a)
103
- });
104
- if (this._options.reload) {
105
- const { def } = this._handlers["/" + path3];
106
- await this._load(def, true);
107
- }
108
- res.statusCode = await asyncTimeout(this.invoke("/" + path3, req.body), FN_TIMEOUT);
109
- res.end();
110
- } catch (err) {
111
- log.catch(err, void 0, {
112
- F: __dxlog_file,
113
- L: 94,
114
- S: this,
115
- C: (f, a) => f(...a)
116
- });
117
- res.statusCode = 500;
118
- res.end();
119
- }
120
- });
121
- this._port = this._options.port ?? await getPort({
122
- host: "localhost",
123
- port: 7200,
124
- portRange: [
125
- 7200,
126
- 7299
127
- ]
128
- });
129
- this._server = app.listen(this._port);
130
- try {
131
- const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
132
- endpoint: this.endpoint
133
- });
134
- log.info("registered", {
135
- endpoint
136
- }, {
137
- F: __dxlog_file,
138
- L: 109,
139
- S: this,
140
- C: (f, a) => f(...a)
141
- });
142
- this._proxy = endpoint;
143
- this._functionServiceRegistration = registrationId;
144
- await this._handleNewFunctions(this._functionsRegistry.getUniqueByUri());
145
- this._ctx.onDispose(this._functionsRegistry.registered.on(({ added }) => this._handleNewFunctions(added)));
146
- } catch (err) {
147
- await this.stop();
148
- throw new Error("FunctionRegistryService not available (check plugin is configured).");
149
- }
150
- log.info("started", {
151
- port: this._port
152
- }, {
153
- F: __dxlog_file,
154
- L: 121,
155
- S: this,
156
- C: (f, a) => f(...a)
157
- });
158
- }
159
- async stop() {
160
- if (!this._server) {
161
- return;
162
- }
163
- log.info("stopping...", void 0, {
164
- F: __dxlog_file,
165
- L: 129,
166
- S: this,
167
- C: (f, a) => f(...a)
168
- });
169
- await this._ctx.dispose();
170
- const trigger = new Trigger();
171
- this._server.close(async () => {
172
- log.info("server stopped", void 0, {
173
- F: __dxlog_file,
174
- L: 134,
175
- S: this,
176
- C: (f, a) => f(...a)
177
- });
178
- try {
179
- if (this._functionServiceRegistration) {
180
- invariant(this._client.services.services.FunctionRegistryService, void 0, {
181
- F: __dxlog_file,
182
- L: 137,
183
- S: this,
184
- A: [
185
- "this._client.services.services.FunctionRegistryService",
186
- ""
187
- ]
188
- });
189
- await this._client.services.services.FunctionRegistryService.unregister({
190
- registrationId: this._functionServiceRegistration
191
- });
192
- log.info("unregistered", {
193
- registrationId: this._functionServiceRegistration
194
- }, {
195
- F: __dxlog_file,
196
- L: 142,
197
- S: this,
198
- C: (f, a) => f(...a)
199
- });
200
- this._functionServiceRegistration = void 0;
201
- this._proxy = void 0;
202
- }
203
- trigger.wake();
204
- } catch (err) {
205
- trigger.throw(err);
206
- }
207
- });
208
- await trigger.wait();
209
- this._port = void 0;
210
- this._server = void 0;
211
- log.info("stopped", void 0, {
212
- F: __dxlog_file,
213
- L: 156,
214
- S: this,
215
- C: (f, a) => f(...a)
216
- });
217
- }
218
- async _handleNewFunctions(newFunctions) {
219
- newFunctions.forEach((def) => this._load(def));
220
- await this._safeUpdateRegistration();
221
- log("new functions loaded", {
222
- newFunctions
223
- }, {
224
- F: __dxlog_file,
225
- L: 162,
226
- S: this,
227
- C: (f, a) => f(...a)
228
- });
229
- }
230
- /**
231
- * Load function.
232
- */
233
- async _load(def, force) {
234
- const { uri, route, handler } = def;
235
- const filePath = join(this._options.baseDir, handler);
236
- log.info("loading", {
237
- uri,
238
- force
239
- }, {
240
- F: __dxlog_file,
241
- L: 171,
242
- S: this,
243
- C: (f, a) => f(...a)
244
- });
245
- if (force) {
246
- Object.keys(__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
247
- delete __require.cache[key];
248
- });
249
- }
250
- const module = __require(filePath);
251
- if (typeof module.default !== "function") {
252
- throw new Error(`Handler must export default function: ${uri}`);
253
- }
254
- this._handlers[route] = {
255
- def,
256
- handler: module.default
257
- };
258
- }
259
- async _safeUpdateRegistration() {
260
- invariant(this._functionServiceRegistration, void 0, {
261
- F: __dxlog_file,
262
- L: 193,
263
- S: this,
264
- A: [
265
- "this._functionServiceRegistration",
266
- ""
267
- ]
268
- });
269
- try {
270
- await this._client.services.services.FunctionRegistryService.updateRegistration({
271
- registrationId: this._functionServiceRegistration,
272
- functions: this.functions.map(({ def: { id, route } }) => ({
273
- id,
274
- route
275
- }))
276
- });
277
- } catch (err) {
278
- log.catch(err, void 0, {
279
- F: __dxlog_file,
280
- L: 200,
281
- S: this,
282
- C: (f, a) => f(...a)
283
- });
284
- }
285
- }
286
- /**
287
- * Invoke function.
288
- */
289
- async invoke(path3, data) {
290
- const seq = ++this._seq;
291
- const now = Date.now();
292
- log.info("req", {
293
- seq,
294
- path: path3
295
- }, {
296
- F: __dxlog_file,
297
- L: 211,
298
- S: this,
299
- C: (f, a) => f(...a)
300
- });
301
- const statusCode = await this._invoke(path3, {
302
- data
303
- });
304
- log.info("res", {
305
- seq,
306
- path: path3,
307
- statusCode,
308
- duration: Date.now() - now
309
- }, {
310
- F: __dxlog_file,
311
- L: 214,
312
- S: this,
313
- C: (f, a) => f(...a)
314
- });
315
- this.update.emit(statusCode);
316
- return statusCode;
317
- }
318
- async _invoke(path3, event) {
319
- const { handler } = this._handlers[path3] ?? {};
320
- invariant(handler, `invalid path: ${path3}`, {
321
- F: __dxlog_file,
322
- L: 221,
323
- S: this,
324
- A: [
325
- "handler",
326
- "`invalid path: ${path}`"
327
- ]
328
- });
329
- const context = {
330
- client: this._client,
331
- dataDir: this._options.dataDir
332
- };
333
- let statusCode = 200;
334
- const response = {
335
- status: (code) => {
336
- statusCode = code;
337
- return response;
338
- }
339
- };
340
- await handler({
341
- context,
342
- event,
343
- response
344
- });
345
- return statusCode;
346
- }
347
- };
348
- var createContext = () => new Context({
349
- name: "DevServer"
350
- }, {
351
- F: __dxlog_file,
352
- L: 240
353
- });
354
-
355
- // packages/core/functions/src/runtime/scheduler.ts
356
- import path from "node:path";
357
- import { Mutex } from "@dxos/async";
358
- import { loadObjectReferences } from "@dxos/client/echo";
359
- import { Context as Context2 } from "@dxos/context";
360
- import { Reference } from "@dxos/echo-protocol";
361
- import { log as log2 } from "@dxos/log";
362
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
363
- var Scheduler = class {
364
- constructor(functions, triggers, _options = {}) {
365
- this.functions = functions;
366
- this.triggers = triggers;
367
- this._options = _options;
368
- this._ctx = createContext2();
369
- this._functionUriToCallMutex = /* @__PURE__ */ new Map();
370
- this.functions.registered.on(async ({ space, added }) => {
371
- await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), added);
372
- });
373
- this.triggers.registered.on(async ({ space, triggers: triggers2 }) => {
374
- await this._safeActivateTriggers(space, triggers2, this.functions.getFunctions(space));
375
- });
376
- }
377
- async start() {
378
- await this._ctx.dispose();
379
- this._ctx = createContext2();
380
- await this.functions.open(this._ctx);
381
- await this.triggers.open(this._ctx);
382
- }
383
- async stop() {
384
- await this._ctx.dispose();
385
- await this.functions.close();
386
- await this.triggers.close();
387
- }
388
- // TODO(burdon): Remove and update registries directly?
389
- async register(space, manifest) {
390
- await this.functions.register(space, manifest.functions);
391
- await this.triggers.register(space, manifest);
392
- }
393
- async _safeActivateTriggers(space, triggers, functions) {
394
- const mountTasks = triggers.map((trigger) => {
395
- return this.activate(space, functions, trigger);
396
- });
397
- await Promise.all(mountTasks).catch(log2.catch);
398
- }
399
- /**
400
- * Activate trigger.
401
- */
402
- // TODO(burdon): How are triggers deactivated?
403
- async activate(space, functions, trigger) {
404
- const definition = functions.find((def) => def.uri === trigger.function);
405
- if (!definition) {
406
- log2.info("function is not found for trigger", {
407
- trigger
408
- }, {
409
- F: __dxlog_file2,
410
- L: 85,
411
- S: this,
412
- C: (f, a) => f(...a)
413
- });
414
- return;
415
- }
416
- const execFunction = async (args) => {
417
- const mutex = this._functionUriToCallMutex.get(definition.uri) ?? new Mutex();
418
- this._functionUriToCallMutex.set(definition.uri, mutex);
419
- log2.info("function triggered, waiting for mutex", {
420
- uri: definition.uri
421
- }, {
422
- F: __dxlog_file2,
423
- L: 93,
424
- S: this,
425
- C: (f, a) => f(...a)
426
- });
427
- return mutex.executeSynchronized(async () => {
428
- log2.info("mutex acquired", {
429
- uri: definition.uri
430
- }, {
431
- F: __dxlog_file2,
432
- L: 95,
433
- S: this,
434
- C: (f, a) => f(...a)
435
- });
436
- await loadObjectReferences(trigger, (t) => Object.values(t.meta ?? {}));
437
- const meta = {};
438
- for (const [key, value] of Object.entries(trigger.meta ?? {})) {
439
- if (value instanceof Reference) {
440
- const object = await space.db.query({
441
- id: value.objectId
442
- }).first();
443
- if (object) {
444
- meta[key] = object;
445
- }
446
- } else {
447
- meta[key] = value;
448
- }
449
- }
450
- return this._execFunction(definition, trigger, {
451
- meta,
452
- data: {
453
- ...args,
454
- spaceKey: space.key
455
- }
456
- });
457
- });
458
- };
459
- await this.triggers.activate(space, trigger, execFunction);
460
- log2("activated trigger", {
461
- space: space.key,
462
- trigger
463
- }, {
464
- F: __dxlog_file2,
465
- L: 119,
466
- S: this,
467
- C: (f, a) => f(...a)
468
- });
469
- }
470
- /**
471
- * Invoke function RPC.
472
- */
473
- async _execFunction(def, trigger, { meta, data }) {
474
- let status = 0;
475
- try {
476
- const payload = Object.assign({}, meta && {
477
- meta
478
- }, data);
479
- const { endpoint, callback } = this._options;
480
- if (endpoint) {
481
- const url = path.join(endpoint, def.route);
482
- log2.info("exec", {
483
- function: def.uri,
484
- url,
485
- triggerType: trigger.spec?.type
486
- }, {
487
- F: __dxlog_file2,
488
- L: 139,
489
- S: this,
490
- C: (f, a) => f(...a)
491
- });
492
- const response = await fetch(url, {
493
- method: "POST",
494
- headers: {
495
- "Content-Type": "application/json"
496
- },
497
- body: JSON.stringify(payload)
498
- });
499
- status = response.status;
500
- } else if (callback) {
501
- log2.info("exec", {
502
- function: def.uri
503
- }, {
504
- F: __dxlog_file2,
505
- L: 150,
506
- S: this,
507
- C: (f, a) => f(...a)
508
- });
509
- status = await callback(payload) ?? 200;
510
- }
511
- if (status && status >= 400) {
512
- throw new Error(`Response: ${status}`);
513
- }
514
- log2.info("done", {
515
- function: def.uri,
516
- status
517
- }, {
518
- F: __dxlog_file2,
519
- L: 160,
520
- S: this,
521
- C: (f, a) => f(...a)
522
- });
523
- } catch (err) {
524
- log2.error("error", {
525
- function: def.uri,
526
- error: err.message
527
- }, {
528
- F: __dxlog_file2,
529
- L: 162,
530
- S: this,
531
- C: (f, a) => f(...a)
532
- });
533
- status = 500;
534
- }
535
- return status;
536
- }
537
- };
538
- var createContext2 = () => new Context2({
539
- name: "FunctionScheduler"
540
- }, {
541
- F: __dxlog_file2,
542
- L: 170
543
- });
544
-
545
- // packages/core/functions/src/testing/setup.ts
546
- var createInitializedClients = async (testBuilder, count = 1, config) => {
547
- const clients = range(count).map(() => new Client({
548
- config,
549
- services: testBuilder.createLocalClientServices(),
550
- types: [
551
- FunctionDef,
552
- FunctionTrigger,
553
- TestType
554
- ]
555
- }));
556
- testBuilder.ctx.onDispose(() => Promise.all(clients.map((client) => client.destroy())));
557
- return Promise.all(clients.map(async (client, index) => {
558
- await client.initialize();
559
- await client.halo.createIdentity({
560
- displayName: `Peer ${index}`
561
- });
562
- await client.spaces.waitUntilReady();
563
- return client;
564
- }));
565
- };
566
- var createFunctionRuntime = async (testBuilder, pluginInitializer) => {
567
- const functionsPort = await getRandomPort("127.0.0.1");
568
- const config = new Config({
569
- runtime: {
570
- agent: {
571
- plugins: [
572
- {
573
- id: "dxos.org/agent/plugin/functions",
574
- config: {
575
- port: functionsPort
576
- }
577
- }
578
- ]
579
- }
580
- }
581
- });
582
- const [client] = await createInitializedClients(testBuilder, 1, config);
583
- const plugin = await pluginInitializer(client);
584
- testBuilder.ctx.onDispose(() => plugin.close());
585
- return client;
586
- };
587
- var startFunctionsHost = async (testBuilder, pluginInitializer, options) => {
588
- const functionRuntime = await createFunctionRuntime(testBuilder, pluginInitializer);
589
- const functionsRegistry = new FunctionRegistry(functionRuntime);
590
- const devServer = await startDevServer(testBuilder, functionRuntime, functionsRegistry, options);
591
- const scheduler = await startScheduler(testBuilder, functionRuntime, devServer, functionsRegistry);
592
- return {
593
- scheduler,
594
- client: functionRuntime,
595
- waitForActiveTriggers: async (space) => {
596
- await waitForCondition({
597
- condition: () => scheduler.triggers.getActiveTriggers(space).length > 0
598
- });
599
- }
600
- };
601
- };
602
- var startScheduler = async (testBuilder, client, devServer, functionRegistry) => {
603
- const triggerRegistry = new TriggerRegistry(client);
604
- const scheduler = new Scheduler(functionRegistry, triggerRegistry, {
605
- endpoint: devServer.endpoint
606
- });
607
- await scheduler.start();
608
- testBuilder.ctx.onDispose(() => scheduler.stop());
609
- return scheduler;
610
- };
611
- var startDevServer = async (testBuilder, client, functionRegistry, options) => {
612
- const server = new DevServer(client, functionRegistry, {
613
- baseDir: path2.join(__dirname, "../testing"),
614
- port: await getRandomPort("127.0.0.1"),
615
- ...options
616
- });
617
- await server.start();
618
- testBuilder.ctx.onDispose(() => server.stop());
619
- return server;
620
- };
621
-
622
- // packages/core/functions/src/testing/util.ts
623
- import { Filter } from "@dxos/client/echo";
624
- import { performInvitation } from "@dxos/client/testing";
625
- import { invariant as invariant2 } from "@dxos/invariant";
626
- import { Invitation } from "@dxos/protocols/proto/dxos/client/services";
627
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/functions/src/testing/util.ts";
628
- var triggerWebhook = async (space, uri) => {
629
- const trigger = (await space.db.query(Filter.schema(FunctionTrigger, (trigger2) => trigger2.function === uri)).run()).objects[0];
630
- invariant2(trigger.spec?.type === "webhook", void 0, {
631
- F: __dxlog_file3,
632
- L: 17,
633
- S: void 0,
634
- A: [
635
- "trigger.spec?.type === 'webhook'",
636
- ""
637
- ]
638
- });
639
- void fetch(`http://localhost:${trigger.spec.port}`);
640
- };
641
- var inviteMember = async (host, guest) => {
642
- const [{ invitation: hostInvitation }] = await Promise.all(performInvitation({
643
- host,
644
- guest: guest.spaces
645
- }));
646
- if (hostInvitation?.state !== Invitation.State.SUCCESS) {
647
- throw new Error(`Expected ${hostInvitation?.state} to be ${Invitation.State.SUCCESS}.`);
648
- }
649
- };
650
-
651
- // packages/core/functions/src/testing/manifest.ts
652
- var testFunctionManifest = {
653
- functions: [
654
- {
655
- uri: "example.com/function/test",
656
- route: "test",
657
- handler: "test"
658
- }
659
- ]
660
- };
661
- export {
662
- TestType,
663
- createFunctionRuntime,
664
- createInitializedClients,
665
- inviteMember,
666
- startFunctionsHost,
667
- testFunctionManifest,
668
- triggerWebhook
669
- };
670
- //# sourceMappingURL=index.mjs.map