@dxos/functions 0.8.1 → 0.8.2-main.10c050d

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