@dxos/functions-runtime-cloudflare 0.8.4-main.03d5cd7b56
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/LICENSE +8 -0
- package/README.md +47 -0
- package/dist/lib/browser/index.mjs +967 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +969 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/functions-client.d.ts +33 -0
- package/dist/types/src/functions-client.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/internal/data-service-impl.d.ts +26 -0
- package/dist/types/src/internal/data-service-impl.d.ts.map +1 -0
- package/dist/types/src/internal/index.d.ts +2 -0
- package/dist/types/src/internal/index.d.ts.map +1 -0
- package/dist/types/src/internal/query-service-impl.d.ts +13 -0
- package/dist/types/src/internal/query-service-impl.d.ts.map +1 -0
- package/dist/types/src/internal/queue-service-impl.d.ts +11 -0
- package/dist/types/src/internal/queue-service-impl.d.ts.map +1 -0
- package/dist/types/src/internal/service-container.d.ts +25 -0
- package/dist/types/src/internal/service-container.d.ts.map +1 -0
- package/dist/types/src/internal/utils.d.ts +2 -0
- package/dist/types/src/internal/utils.d.ts.map +1 -0
- package/dist/types/src/logger.d.ts +2 -0
- package/dist/types/src/logger.d.ts.map +1 -0
- package/dist/types/src/queues-api.d.ts +26 -0
- package/dist/types/src/queues-api.d.ts.map +1 -0
- package/dist/types/src/space-proxy.d.ts +26 -0
- package/dist/types/src/space-proxy.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +31 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/wrap-handler-for-cloudflare.d.ts +12 -0
- package/dist/types/src/wrap-handler-for-cloudflare.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +52 -0
- package/src/functions-client.ts +88 -0
- package/src/index.ts +9 -0
- package/src/internal/data-service-impl.ts +143 -0
- package/src/internal/index.ts +5 -0
- package/src/internal/query-service-impl.ts +53 -0
- package/src/internal/queue-service-impl.ts +66 -0
- package/src/internal/service-container.ts +89 -0
- package/src/internal/utils.ts +5 -0
- package/src/logger.ts +44 -0
- package/src/queues-api.ts +57 -0
- package/src/space-proxy.ts +66 -0
- package/src/types.ts +40 -0
- package/src/wrap-handler-for-cloudflare.ts +132 -0
|
@@ -0,0 +1,969 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// src/functions-client.ts
|
|
4
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
5
|
+
import { EchoClient } from "@dxos/echo-db";
|
|
6
|
+
import { invariant as invariant3 } from "@dxos/invariant";
|
|
7
|
+
|
|
8
|
+
// src/internal/data-service-impl.ts
|
|
9
|
+
import { Stream } from "@dxos/codec-protobuf/stream";
|
|
10
|
+
import { raise } from "@dxos/debug";
|
|
11
|
+
import { NotImplementedError, RuntimeServiceError } from "@dxos/errors";
|
|
12
|
+
import { invariant } from "@dxos/invariant";
|
|
13
|
+
import { SpaceId } from "@dxos/keys";
|
|
14
|
+
import { log } from "@dxos/log";
|
|
15
|
+
|
|
16
|
+
// src/internal/utils.ts
|
|
17
|
+
var copyUint8Array = (value) => new Uint8Array(value);
|
|
18
|
+
|
|
19
|
+
// src/internal/data-service-impl.ts
|
|
20
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/internal/data-service-impl.ts";
|
|
21
|
+
function _ts_add_disposable_resource(env, value, async) {
|
|
22
|
+
if (value !== null && value !== void 0) {
|
|
23
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
24
|
+
var dispose, inner;
|
|
25
|
+
if (async) {
|
|
26
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
27
|
+
dispose = value[Symbol.asyncDispose];
|
|
28
|
+
}
|
|
29
|
+
if (dispose === void 0) {
|
|
30
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
31
|
+
dispose = value[Symbol.dispose];
|
|
32
|
+
if (async) inner = dispose;
|
|
33
|
+
}
|
|
34
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
35
|
+
if (inner) dispose = function() {
|
|
36
|
+
try {
|
|
37
|
+
inner.call(this);
|
|
38
|
+
} catch (e) {
|
|
39
|
+
return Promise.reject(e);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
env.stack.push({
|
|
43
|
+
value,
|
|
44
|
+
dispose,
|
|
45
|
+
async
|
|
46
|
+
});
|
|
47
|
+
} else if (async) {
|
|
48
|
+
env.stack.push({
|
|
49
|
+
async: true
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
function _ts_dispose_resources(env) {
|
|
55
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
56
|
+
var e = new Error(message);
|
|
57
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
58
|
+
};
|
|
59
|
+
return (_ts_dispose_resources = function _ts_dispose_resources5(env2) {
|
|
60
|
+
function fail(e) {
|
|
61
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
62
|
+
env2.hasError = true;
|
|
63
|
+
}
|
|
64
|
+
var r, s = 0;
|
|
65
|
+
function next() {
|
|
66
|
+
while (r = env2.stack.pop()) {
|
|
67
|
+
try {
|
|
68
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
69
|
+
if (r.dispose) {
|
|
70
|
+
var result = r.dispose.call(r.value);
|
|
71
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
72
|
+
fail(e);
|
|
73
|
+
return next();
|
|
74
|
+
});
|
|
75
|
+
} else s |= 1;
|
|
76
|
+
} catch (e) {
|
|
77
|
+
fail(e);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
81
|
+
if (env2.hasError) throw env2.error;
|
|
82
|
+
}
|
|
83
|
+
return next();
|
|
84
|
+
})(env);
|
|
85
|
+
}
|
|
86
|
+
var DataServiceImpl = class {
|
|
87
|
+
_executionContext;
|
|
88
|
+
_dataService;
|
|
89
|
+
dataSubscriptions = /* @__PURE__ */ new Map();
|
|
90
|
+
constructor(_executionContext, _dataService) {
|
|
91
|
+
this._executionContext = _executionContext;
|
|
92
|
+
this._dataService = _dataService;
|
|
93
|
+
}
|
|
94
|
+
subscribe({ subscriptionId, spaceId }) {
|
|
95
|
+
return new Stream(({ next }) => {
|
|
96
|
+
invariant(SpaceId.isValid(spaceId), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 86, S: this, A: ["SpaceId.isValid(spaceId)", ""] });
|
|
97
|
+
this.dataSubscriptions.set(subscriptionId, {
|
|
98
|
+
spaceId,
|
|
99
|
+
next
|
|
100
|
+
});
|
|
101
|
+
return () => {
|
|
102
|
+
this.dataSubscriptions.delete(subscriptionId);
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async updateSubscription({ subscriptionId, addIds }) {
|
|
107
|
+
const sub = this.dataSubscriptions.get(subscriptionId) ?? raise(new RuntimeServiceError({
|
|
108
|
+
message: "Subscription not found.",
|
|
109
|
+
context: {
|
|
110
|
+
subscriptionId
|
|
111
|
+
}
|
|
112
|
+
}));
|
|
113
|
+
if (addIds) {
|
|
114
|
+
log.info("request documents", {
|
|
115
|
+
count: addIds.length
|
|
116
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 104, S: this });
|
|
117
|
+
for (const documentId of addIds) {
|
|
118
|
+
const env = {
|
|
119
|
+
stack: [],
|
|
120
|
+
error: void 0,
|
|
121
|
+
hasError: false
|
|
122
|
+
};
|
|
123
|
+
try {
|
|
124
|
+
const document = _ts_add_disposable_resource(env, await this._dataService.getDocument(this._executionContext, sub.spaceId, documentId), false);
|
|
125
|
+
log.info("document loaded", {
|
|
126
|
+
documentId,
|
|
127
|
+
spaceId: sub.spaceId,
|
|
128
|
+
found: !!document
|
|
129
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 116, S: this });
|
|
130
|
+
if (!document) {
|
|
131
|
+
log.warn("not found", {
|
|
132
|
+
documentId
|
|
133
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 122, S: this });
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
sub.next({
|
|
137
|
+
updates: [
|
|
138
|
+
{
|
|
139
|
+
documentId,
|
|
140
|
+
// Copy returned object to avoid hanging RPC stub
|
|
141
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
142
|
+
mutation: copyUint8Array(document.data)
|
|
143
|
+
}
|
|
144
|
+
]
|
|
145
|
+
});
|
|
146
|
+
} catch (e) {
|
|
147
|
+
env.error = e;
|
|
148
|
+
env.hasError = true;
|
|
149
|
+
} finally {
|
|
150
|
+
_ts_dispose_resources(env);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async createDocument({ spaceId, initialValue }) {
|
|
156
|
+
const env = {
|
|
157
|
+
stack: [],
|
|
158
|
+
error: void 0,
|
|
159
|
+
hasError: false
|
|
160
|
+
};
|
|
161
|
+
try {
|
|
162
|
+
invariant(SpaceId.isValid(spaceId), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 153, S: this, A: ["SpaceId.isValid(spaceId)", ""] });
|
|
163
|
+
const response = _ts_add_disposable_resource(env, await this._dataService.createDocument(this._executionContext, spaceId, initialValue), false);
|
|
164
|
+
return {
|
|
165
|
+
documentId: response.documentId
|
|
166
|
+
};
|
|
167
|
+
} catch (e) {
|
|
168
|
+
env.error = e;
|
|
169
|
+
env.hasError = true;
|
|
170
|
+
} finally {
|
|
171
|
+
_ts_dispose_resources(env);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async update({ updates, subscriptionId }) {
|
|
175
|
+
const sub = this.dataSubscriptions.get(subscriptionId) ?? raise(new RuntimeServiceError({
|
|
176
|
+
message: "Subscription not found.",
|
|
177
|
+
context: {
|
|
178
|
+
subscriptionId
|
|
179
|
+
}
|
|
180
|
+
}));
|
|
181
|
+
try {
|
|
182
|
+
for (const update of updates ?? []) {
|
|
183
|
+
await this._dataService.changeDocument(this._executionContext, sub.spaceId, update.documentId, update.mutation);
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
throw RuntimeServiceError.wrap({
|
|
187
|
+
message: "Failed to apply document updates.",
|
|
188
|
+
context: {
|
|
189
|
+
subscriptionId
|
|
190
|
+
},
|
|
191
|
+
ifTypeDiffers: true
|
|
192
|
+
})(error);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async flush() {
|
|
196
|
+
}
|
|
197
|
+
subscribeSpaceSyncState(_request, _options) {
|
|
198
|
+
throw new NotImplementedError({
|
|
199
|
+
message: "subscribeSpaceSyncState is not implemented."
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
async getDocumentHeads({ documentIds: _documentIds }) {
|
|
203
|
+
throw new NotImplementedError({
|
|
204
|
+
message: "getDocumentHeads is not implemented."
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
async reIndexHeads({ documentIds: _documentIds }) {
|
|
208
|
+
throw new NotImplementedError({
|
|
209
|
+
message: "reIndexHeads is not implemented."
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
async updateIndexes() {
|
|
213
|
+
log.error("updateIndexes is not available in EDGE env.", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 206, S: this });
|
|
214
|
+
}
|
|
215
|
+
async waitUntilHeadsReplicated({ heads: _heads }) {
|
|
216
|
+
throw new NotImplementedError({
|
|
217
|
+
message: "waitUntilHeadsReplicated is not implemented."
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// src/internal/query-service-impl.ts
|
|
223
|
+
import { Stream as Stream2 } from "@dxos/codec-protobuf/stream";
|
|
224
|
+
import { NotImplementedError as NotImplementedError2, RuntimeServiceError as RuntimeServiceError2 } from "@dxos/errors";
|
|
225
|
+
import { log as log2 } from "@dxos/log";
|
|
226
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
|
|
227
|
+
function _ts_add_disposable_resource2(env, value, async) {
|
|
228
|
+
if (value !== null && value !== void 0) {
|
|
229
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
230
|
+
var dispose, inner;
|
|
231
|
+
if (async) {
|
|
232
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
233
|
+
dispose = value[Symbol.asyncDispose];
|
|
234
|
+
}
|
|
235
|
+
if (dispose === void 0) {
|
|
236
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
237
|
+
dispose = value[Symbol.dispose];
|
|
238
|
+
if (async) inner = dispose;
|
|
239
|
+
}
|
|
240
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
241
|
+
if (inner) dispose = function() {
|
|
242
|
+
try {
|
|
243
|
+
inner.call(this);
|
|
244
|
+
} catch (e) {
|
|
245
|
+
return Promise.reject(e);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
env.stack.push({
|
|
249
|
+
value,
|
|
250
|
+
dispose,
|
|
251
|
+
async
|
|
252
|
+
});
|
|
253
|
+
} else if (async) {
|
|
254
|
+
env.stack.push({
|
|
255
|
+
async: true
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return value;
|
|
259
|
+
}
|
|
260
|
+
function _ts_dispose_resources2(env) {
|
|
261
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
262
|
+
var e = new Error(message);
|
|
263
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
264
|
+
};
|
|
265
|
+
return (_ts_dispose_resources2 = function _ts_dispose_resources5(env2) {
|
|
266
|
+
function fail(e) {
|
|
267
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
268
|
+
env2.hasError = true;
|
|
269
|
+
}
|
|
270
|
+
var r, s = 0;
|
|
271
|
+
function next() {
|
|
272
|
+
while (r = env2.stack.pop()) {
|
|
273
|
+
try {
|
|
274
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
275
|
+
if (r.dispose) {
|
|
276
|
+
var result = r.dispose.call(r.value);
|
|
277
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
278
|
+
fail(e);
|
|
279
|
+
return next();
|
|
280
|
+
});
|
|
281
|
+
} else s |= 1;
|
|
282
|
+
} catch (e) {
|
|
283
|
+
fail(e);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
287
|
+
if (env2.hasError) throw env2.error;
|
|
288
|
+
}
|
|
289
|
+
return next();
|
|
290
|
+
})(env);
|
|
291
|
+
}
|
|
292
|
+
var QueryServiceImpl = class {
|
|
293
|
+
_executionContext;
|
|
294
|
+
_dataService;
|
|
295
|
+
_queryCount = 0;
|
|
296
|
+
constructor(_executionContext, _dataService) {
|
|
297
|
+
this._executionContext = _executionContext;
|
|
298
|
+
this._dataService = _dataService;
|
|
299
|
+
}
|
|
300
|
+
execQuery(request) {
|
|
301
|
+
log2.info("execQuery", {
|
|
302
|
+
request
|
|
303
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 81, S: this });
|
|
304
|
+
return Stream2.fromPromise((async () => {
|
|
305
|
+
try {
|
|
306
|
+
const env = {
|
|
307
|
+
stack: [],
|
|
308
|
+
error: void 0,
|
|
309
|
+
hasError: false
|
|
310
|
+
};
|
|
311
|
+
try {
|
|
312
|
+
this._queryCount++;
|
|
313
|
+
log2.info("begin query", {
|
|
314
|
+
request
|
|
315
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 93, S: this });
|
|
316
|
+
const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.execQuery(this._executionContext, request), false);
|
|
317
|
+
log2.info("query response", {
|
|
318
|
+
resultCount: queryResponse.results?.length
|
|
319
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 97, S: this });
|
|
320
|
+
return structuredClone(queryResponse);
|
|
321
|
+
} catch (e) {
|
|
322
|
+
env.error = e;
|
|
323
|
+
env.hasError = true;
|
|
324
|
+
} finally {
|
|
325
|
+
_ts_dispose_resources2(env);
|
|
326
|
+
}
|
|
327
|
+
} catch (error) {
|
|
328
|
+
log2.error("query failed", {
|
|
329
|
+
err: error
|
|
330
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 108, S: this });
|
|
331
|
+
throw new RuntimeServiceError2({
|
|
332
|
+
message: `Query execution failed (queryCount=${this._queryCount})`,
|
|
333
|
+
context: {
|
|
334
|
+
queryCount: this._queryCount
|
|
335
|
+
},
|
|
336
|
+
cause: error
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
})());
|
|
340
|
+
}
|
|
341
|
+
async reindex() {
|
|
342
|
+
throw new NotImplementedError2({
|
|
343
|
+
message: "Reindex is not implemented."
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
async setConfig() {
|
|
347
|
+
throw new NotImplementedError2({
|
|
348
|
+
message: "SetConfig is not implemented."
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
// src/internal/queue-service-impl.ts
|
|
354
|
+
import { RuntimeServiceError as RuntimeServiceError3 } from "@dxos/errors";
|
|
355
|
+
function _ts_add_disposable_resource3(env, value, async) {
|
|
356
|
+
if (value !== null && value !== void 0) {
|
|
357
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
358
|
+
var dispose, inner;
|
|
359
|
+
if (async) {
|
|
360
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
361
|
+
dispose = value[Symbol.asyncDispose];
|
|
362
|
+
}
|
|
363
|
+
if (dispose === void 0) {
|
|
364
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
365
|
+
dispose = value[Symbol.dispose];
|
|
366
|
+
if (async) inner = dispose;
|
|
367
|
+
}
|
|
368
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
369
|
+
if (inner) dispose = function() {
|
|
370
|
+
try {
|
|
371
|
+
inner.call(this);
|
|
372
|
+
} catch (e) {
|
|
373
|
+
return Promise.reject(e);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
env.stack.push({
|
|
377
|
+
value,
|
|
378
|
+
dispose,
|
|
379
|
+
async
|
|
380
|
+
});
|
|
381
|
+
} else if (async) {
|
|
382
|
+
env.stack.push({
|
|
383
|
+
async: true
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
return value;
|
|
387
|
+
}
|
|
388
|
+
function _ts_dispose_resources3(env) {
|
|
389
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
390
|
+
var e = new Error(message);
|
|
391
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
392
|
+
};
|
|
393
|
+
return (_ts_dispose_resources3 = function _ts_dispose_resources5(env2) {
|
|
394
|
+
function fail(e) {
|
|
395
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
396
|
+
env2.hasError = true;
|
|
397
|
+
}
|
|
398
|
+
var r, s = 0;
|
|
399
|
+
function next() {
|
|
400
|
+
while (r = env2.stack.pop()) {
|
|
401
|
+
try {
|
|
402
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
403
|
+
if (r.dispose) {
|
|
404
|
+
var result = r.dispose.call(r.value);
|
|
405
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
406
|
+
fail(e);
|
|
407
|
+
return next();
|
|
408
|
+
});
|
|
409
|
+
} else s |= 1;
|
|
410
|
+
} catch (e) {
|
|
411
|
+
fail(e);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
415
|
+
if (env2.hasError) throw env2.error;
|
|
416
|
+
}
|
|
417
|
+
return next();
|
|
418
|
+
})(env);
|
|
419
|
+
}
|
|
420
|
+
var QueueServiceImpl = class {
|
|
421
|
+
_ctx;
|
|
422
|
+
_queueService;
|
|
423
|
+
constructor(_ctx, _queueService) {
|
|
424
|
+
this._ctx = _ctx;
|
|
425
|
+
this._queueService = _queueService;
|
|
426
|
+
}
|
|
427
|
+
async queryQueue(request) {
|
|
428
|
+
try {
|
|
429
|
+
const env = {
|
|
430
|
+
stack: [],
|
|
431
|
+
error: void 0,
|
|
432
|
+
hasError: false
|
|
433
|
+
};
|
|
434
|
+
try {
|
|
435
|
+
const result = _ts_add_disposable_resource3(env, await this._queueService.queryQueue(this._ctx, request), false);
|
|
436
|
+
return {
|
|
437
|
+
objects: structuredClone(result.objects),
|
|
438
|
+
nextCursor: result.nextCursor,
|
|
439
|
+
prevCursor: result.prevCursor
|
|
440
|
+
};
|
|
441
|
+
} catch (e) {
|
|
442
|
+
env.error = e;
|
|
443
|
+
env.hasError = true;
|
|
444
|
+
} finally {
|
|
445
|
+
_ts_dispose_resources3(env);
|
|
446
|
+
}
|
|
447
|
+
} catch (error) {
|
|
448
|
+
const { query } = request;
|
|
449
|
+
throw RuntimeServiceError3.wrap({
|
|
450
|
+
message: "Queue query failed.",
|
|
451
|
+
context: {
|
|
452
|
+
subspaceTag: query?.queuesNamespace,
|
|
453
|
+
spaceId: query?.spaceId,
|
|
454
|
+
queueId: query?.queueIds?.[0]
|
|
455
|
+
},
|
|
456
|
+
ifTypeDiffers: true
|
|
457
|
+
})(error);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
async insertIntoQueue(request) {
|
|
461
|
+
try {
|
|
462
|
+
const env = {
|
|
463
|
+
stack: [],
|
|
464
|
+
error: void 0,
|
|
465
|
+
hasError: false
|
|
466
|
+
};
|
|
467
|
+
try {
|
|
468
|
+
const _ = _ts_add_disposable_resource3(env, await this._queueService.insertIntoQueue(this._ctx, request), false);
|
|
469
|
+
} catch (e) {
|
|
470
|
+
env.error = e;
|
|
471
|
+
env.hasError = true;
|
|
472
|
+
} finally {
|
|
473
|
+
_ts_dispose_resources3(env);
|
|
474
|
+
}
|
|
475
|
+
} catch (error) {
|
|
476
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
477
|
+
throw RuntimeServiceError3.wrap({
|
|
478
|
+
message: "Queue append failed.",
|
|
479
|
+
context: {
|
|
480
|
+
subspaceTag,
|
|
481
|
+
spaceId,
|
|
482
|
+
queueId
|
|
483
|
+
},
|
|
484
|
+
ifTypeDiffers: true
|
|
485
|
+
})(error);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
async deleteFromQueue(request) {
|
|
489
|
+
try {
|
|
490
|
+
const env = {
|
|
491
|
+
stack: [],
|
|
492
|
+
error: void 0,
|
|
493
|
+
hasError: false
|
|
494
|
+
};
|
|
495
|
+
try {
|
|
496
|
+
const _ = _ts_add_disposable_resource3(env, await this._queueService.deleteFromQueue(this._ctx, request), false);
|
|
497
|
+
} catch (e) {
|
|
498
|
+
env.error = e;
|
|
499
|
+
env.hasError = true;
|
|
500
|
+
} finally {
|
|
501
|
+
_ts_dispose_resources3(env);
|
|
502
|
+
}
|
|
503
|
+
} catch (error) {
|
|
504
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
505
|
+
throw RuntimeServiceError3.wrap({
|
|
506
|
+
message: "Queue delete failed.",
|
|
507
|
+
context: {
|
|
508
|
+
subspaceTag,
|
|
509
|
+
spaceId,
|
|
510
|
+
queueId
|
|
511
|
+
},
|
|
512
|
+
ifTypeDiffers: true
|
|
513
|
+
})(error);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
async syncQueue(_) {
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// src/internal/service-container.ts
|
|
521
|
+
function _ts_add_disposable_resource4(env, value, async) {
|
|
522
|
+
if (value !== null && value !== void 0) {
|
|
523
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
524
|
+
var dispose, inner;
|
|
525
|
+
if (async) {
|
|
526
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
527
|
+
dispose = value[Symbol.asyncDispose];
|
|
528
|
+
}
|
|
529
|
+
if (dispose === void 0) {
|
|
530
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
531
|
+
dispose = value[Symbol.dispose];
|
|
532
|
+
if (async) inner = dispose;
|
|
533
|
+
}
|
|
534
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
535
|
+
if (inner) dispose = function() {
|
|
536
|
+
try {
|
|
537
|
+
inner.call(this);
|
|
538
|
+
} catch (e) {
|
|
539
|
+
return Promise.reject(e);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
env.stack.push({
|
|
543
|
+
value,
|
|
544
|
+
dispose,
|
|
545
|
+
async
|
|
546
|
+
});
|
|
547
|
+
} else if (async) {
|
|
548
|
+
env.stack.push({
|
|
549
|
+
async: true
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
return value;
|
|
553
|
+
}
|
|
554
|
+
function _ts_dispose_resources4(env) {
|
|
555
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
556
|
+
var e = new Error(message);
|
|
557
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
558
|
+
};
|
|
559
|
+
return (_ts_dispose_resources4 = function _ts_dispose_resources5(env2) {
|
|
560
|
+
function fail(e) {
|
|
561
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
562
|
+
env2.hasError = true;
|
|
563
|
+
}
|
|
564
|
+
var r, s = 0;
|
|
565
|
+
function next() {
|
|
566
|
+
while (r = env2.stack.pop()) {
|
|
567
|
+
try {
|
|
568
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
569
|
+
if (r.dispose) {
|
|
570
|
+
var result = r.dispose.call(r.value);
|
|
571
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
572
|
+
fail(e);
|
|
573
|
+
return next();
|
|
574
|
+
});
|
|
575
|
+
} else s |= 1;
|
|
576
|
+
} catch (e) {
|
|
577
|
+
fail(e);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
581
|
+
if (env2.hasError) throw env2.error;
|
|
582
|
+
}
|
|
583
|
+
return next();
|
|
584
|
+
})(env);
|
|
585
|
+
}
|
|
586
|
+
var ServiceContainer = class {
|
|
587
|
+
_executionContext;
|
|
588
|
+
_dataService;
|
|
589
|
+
_queueService;
|
|
590
|
+
_functionsService;
|
|
591
|
+
constructor(_executionContext, _dataService, _queueService, _functionsService) {
|
|
592
|
+
this._executionContext = _executionContext;
|
|
593
|
+
this._dataService = _dataService;
|
|
594
|
+
this._queueService = _queueService;
|
|
595
|
+
this._functionsService = _functionsService;
|
|
596
|
+
}
|
|
597
|
+
async getSpaceMeta(spaceId) {
|
|
598
|
+
const env = {
|
|
599
|
+
stack: [],
|
|
600
|
+
error: void 0,
|
|
601
|
+
hasError: false
|
|
602
|
+
};
|
|
603
|
+
try {
|
|
604
|
+
const result = _ts_add_disposable_resource4(env, await this._dataService.getSpaceMeta(this._executionContext, spaceId), false);
|
|
605
|
+
return result ? {
|
|
606
|
+
spaceKey: result.spaceKey,
|
|
607
|
+
rootDocumentId: result.rootDocumentId
|
|
608
|
+
} : void 0;
|
|
609
|
+
} catch (e) {
|
|
610
|
+
env.error = e;
|
|
611
|
+
env.hasError = true;
|
|
612
|
+
} finally {
|
|
613
|
+
_ts_dispose_resources4(env);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
async createServices() {
|
|
617
|
+
const dataService = new DataServiceImpl(this._executionContext, this._dataService);
|
|
618
|
+
const queryService = new QueryServiceImpl(this._executionContext, this._dataService);
|
|
619
|
+
const queueService = new QueueServiceImpl(this._executionContext, this._queueService);
|
|
620
|
+
return {
|
|
621
|
+
dataService,
|
|
622
|
+
queryService,
|
|
623
|
+
queueService,
|
|
624
|
+
functionsAiService: this._functionsService
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
async queryQueue(queue) {
|
|
628
|
+
const parts = queue.asQueueDXN();
|
|
629
|
+
if (!parts) {
|
|
630
|
+
throw new Error("Invalid queue DXN");
|
|
631
|
+
}
|
|
632
|
+
const { subspaceTag, spaceId, queueId } = parts;
|
|
633
|
+
const result = await this._queueService.queryQueue(this._executionContext, {
|
|
634
|
+
query: {
|
|
635
|
+
spaceId,
|
|
636
|
+
queuesNamespace: subspaceTag,
|
|
637
|
+
queueIds: [
|
|
638
|
+
queueId
|
|
639
|
+
]
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
return {
|
|
643
|
+
objects: structuredClone(result.objects),
|
|
644
|
+
nextCursor: result.nextCursor ?? null,
|
|
645
|
+
prevCursor: result.prevCursor ?? null
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
async insertIntoQueue(queue, objects) {
|
|
649
|
+
const parts = queue.asQueueDXN();
|
|
650
|
+
if (!parts) {
|
|
651
|
+
throw new Error("Invalid queue DXN");
|
|
652
|
+
}
|
|
653
|
+
const { subspaceTag, spaceId, queueId } = parts;
|
|
654
|
+
await this._queueService.insertIntoQueue(this._executionContext, {
|
|
655
|
+
subspaceTag,
|
|
656
|
+
spaceId,
|
|
657
|
+
queueId,
|
|
658
|
+
objects: objects.map((obj) => JSON.stringify(obj))
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
// src/space-proxy.ts
|
|
664
|
+
import { Resource } from "@dxos/context";
|
|
665
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
666
|
+
import { PublicKey } from "@dxos/keys";
|
|
667
|
+
|
|
668
|
+
// src/queues-api.ts
|
|
669
|
+
import { log as log3 } from "@dxos/log";
|
|
670
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/queues-api.ts";
|
|
671
|
+
var QueuesAPIImpl = class {
|
|
672
|
+
_serviceContainer;
|
|
673
|
+
_spaceId;
|
|
674
|
+
constructor(_serviceContainer, _spaceId) {
|
|
675
|
+
this._serviceContainer = _serviceContainer;
|
|
676
|
+
this._spaceId = _spaceId;
|
|
677
|
+
}
|
|
678
|
+
async queryQueue(queue, options) {
|
|
679
|
+
const result = await this._serviceContainer.queryQueue(queue);
|
|
680
|
+
const objects = (result.objects ?? []).flatMap((encoded) => {
|
|
681
|
+
try {
|
|
682
|
+
return [
|
|
683
|
+
JSON.parse(encoded)
|
|
684
|
+
];
|
|
685
|
+
} catch (err) {
|
|
686
|
+
log3.verbose("queue object JSON parse failed; object ignored", {
|
|
687
|
+
encoded,
|
|
688
|
+
error: err
|
|
689
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 22, S: this });
|
|
690
|
+
return [];
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
return {
|
|
694
|
+
objects,
|
|
695
|
+
nextCursor: result.nextCursor ?? null,
|
|
696
|
+
prevCursor: result.prevCursor ?? null
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
insertIntoQueue(queue, objects) {
|
|
700
|
+
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
// src/space-proxy.ts
|
|
705
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/space-proxy.ts";
|
|
706
|
+
var SpaceProxy = class extends Resource {
|
|
707
|
+
_serviceContainer;
|
|
708
|
+
_echoClient;
|
|
709
|
+
_id;
|
|
710
|
+
_db = void 0;
|
|
711
|
+
_queuesApi;
|
|
712
|
+
constructor(_serviceContainer, _echoClient, _id) {
|
|
713
|
+
super(), this._serviceContainer = _serviceContainer, this._echoClient = _echoClient, this._id = _id;
|
|
714
|
+
this._queuesApi = new QueuesAPIImpl(this._serviceContainer, this._id);
|
|
715
|
+
}
|
|
716
|
+
get id() {
|
|
717
|
+
return this._id;
|
|
718
|
+
}
|
|
719
|
+
get db() {
|
|
720
|
+
invariant2(this._db, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 24, S: this, A: ["this._db", ""] });
|
|
721
|
+
return this._db;
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* @deprecated Use db API.
|
|
725
|
+
*/
|
|
726
|
+
get crud() {
|
|
727
|
+
invariant2(this._db, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 30, S: this, A: ["this._db", ""] });
|
|
728
|
+
return this._db.coreDatabase;
|
|
729
|
+
}
|
|
730
|
+
get queues() {
|
|
731
|
+
return this._queuesApi;
|
|
732
|
+
}
|
|
733
|
+
async _open() {
|
|
734
|
+
const meta = await this._serviceContainer.getSpaceMeta(this._id);
|
|
735
|
+
if (!meta) {
|
|
736
|
+
throw new Error(`Space not found: ${this._id}`);
|
|
737
|
+
}
|
|
738
|
+
this._db = this._echoClient.constructDatabase({
|
|
739
|
+
spaceId: this._id,
|
|
740
|
+
spaceKey: PublicKey.from(meta.spaceKey),
|
|
741
|
+
reactiveSchemaQuery: false,
|
|
742
|
+
owningObject: this
|
|
743
|
+
});
|
|
744
|
+
await this._db.coreDatabase.open(this._ctx, {
|
|
745
|
+
rootUrl: meta.rootDocumentId
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
|
|
750
|
+
// src/functions-client.ts
|
|
751
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/functions-client.ts";
|
|
752
|
+
var FunctionsClient = class extends Resource2 {
|
|
753
|
+
_serviceContainer;
|
|
754
|
+
_echoClient;
|
|
755
|
+
_executionContext = {};
|
|
756
|
+
_spaces = /* @__PURE__ */ new Map();
|
|
757
|
+
constructor(services) {
|
|
758
|
+
super();
|
|
759
|
+
invariant3(typeof services.dataService !== "undefined", "DataService is required", { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 19, S: this, A: ["typeof services.dataService !== 'undefined'", "'DataService is required'"] });
|
|
760
|
+
invariant3(typeof services.queueService !== "undefined", "QueueService is required", { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 20, S: this, A: ["typeof services.queueService !== 'undefined'", "'QueueService is required'"] });
|
|
761
|
+
this._serviceContainer = new ServiceContainer(this._executionContext, services.dataService, services.queueService, services.functionsAiService);
|
|
762
|
+
this._echoClient = new EchoClient({});
|
|
763
|
+
}
|
|
764
|
+
get echo() {
|
|
765
|
+
return this._echoClient;
|
|
766
|
+
}
|
|
767
|
+
async _open() {
|
|
768
|
+
const { dataService, queryService } = await this._serviceContainer.createServices();
|
|
769
|
+
this._echoClient.connectToService({
|
|
770
|
+
dataService,
|
|
771
|
+
queryService
|
|
772
|
+
});
|
|
773
|
+
await this._echoClient.open();
|
|
774
|
+
}
|
|
775
|
+
async _close() {
|
|
776
|
+
for (const space of this._spaces.values()) {
|
|
777
|
+
await space.close();
|
|
778
|
+
}
|
|
779
|
+
this._spaces.clear();
|
|
780
|
+
await this._echoClient.close();
|
|
781
|
+
}
|
|
782
|
+
async getSpace(spaceId) {
|
|
783
|
+
if (!this._spaces.has(spaceId)) {
|
|
784
|
+
const space2 = new SpaceProxy(this._serviceContainer, this._echoClient, spaceId);
|
|
785
|
+
this._spaces.set(spaceId, space2);
|
|
786
|
+
}
|
|
787
|
+
const space = this._spaces.get(spaceId);
|
|
788
|
+
await space.open();
|
|
789
|
+
return space;
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
var createClientFromEnv = async (env) => {
|
|
793
|
+
const client = new FunctionsClient({
|
|
794
|
+
dataService: env.DATA_SERVICE,
|
|
795
|
+
queueService: env.QUEUE_SERVICE,
|
|
796
|
+
functionsAiService: env.FUNCTIONS_AI_SERVICE
|
|
797
|
+
});
|
|
798
|
+
await client.open();
|
|
799
|
+
return client;
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
// src/types.ts
|
|
803
|
+
var FUNCTION_ROUTE_HEADER = "X-DXOS-Function-Route";
|
|
804
|
+
var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
|
|
805
|
+
FunctionRouteValue2["Meta"] = "meta";
|
|
806
|
+
return FunctionRouteValue2;
|
|
807
|
+
})({});
|
|
808
|
+
|
|
809
|
+
// src/wrap-handler-for-cloudflare.ts
|
|
810
|
+
import { invariant as invariant4 } from "@dxos/invariant";
|
|
811
|
+
import { SpaceId as SpaceId2 } from "@dxos/keys";
|
|
812
|
+
import { log as log4 } from "@dxos/log";
|
|
813
|
+
import { EdgeResponse } from "@dxos/protocols";
|
|
814
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
|
|
815
|
+
var wrapHandlerForCloudflare = (func) => {
|
|
816
|
+
return async (request, env) => {
|
|
817
|
+
if (request.headers.get(FUNCTION_ROUTE_HEADER) === FunctionRouteValue.Meta) {
|
|
818
|
+
return handleFunctionMetaCall(func, request);
|
|
819
|
+
}
|
|
820
|
+
try {
|
|
821
|
+
const spaceId = new URL(request.url).searchParams.get("spaceId");
|
|
822
|
+
if (spaceId) {
|
|
823
|
+
if (!SpaceId2.isValid(spaceId)) {
|
|
824
|
+
return new Response("Invalid spaceId", {
|
|
825
|
+
status: 400
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
const serviceContainer = new ServiceContainer({}, env.DATA_SERVICE, env.QUEUE_SERVICE, env.FUNCTIONS_AI_SERVICE);
|
|
830
|
+
const context = await createFunctionContext({
|
|
831
|
+
serviceContainer,
|
|
832
|
+
contextSpaceId: spaceId
|
|
833
|
+
});
|
|
834
|
+
return EdgeResponse.success(await invokeFunction(func, context, request));
|
|
835
|
+
} catch (error) {
|
|
836
|
+
log4.error("error invoking function", {
|
|
837
|
+
error,
|
|
838
|
+
stack: error.stack
|
|
839
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 37, S: void 0 });
|
|
840
|
+
return EdgeResponse.failure({
|
|
841
|
+
message: error?.message ?? "Internal error",
|
|
842
|
+
error
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
};
|
|
847
|
+
var invokeFunction = async (func, context, request) => {
|
|
848
|
+
const { data } = await decodeRequest(request);
|
|
849
|
+
return func.handler({
|
|
850
|
+
context,
|
|
851
|
+
data
|
|
852
|
+
});
|
|
853
|
+
};
|
|
854
|
+
var decodeRequest = async (request) => {
|
|
855
|
+
const { data: { bodyText, ...rest }, trigger } = await request.json();
|
|
856
|
+
if (!bodyText) {
|
|
857
|
+
return {
|
|
858
|
+
data: rest,
|
|
859
|
+
trigger
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
try {
|
|
863
|
+
const data = JSON.parse(bodyText);
|
|
864
|
+
return {
|
|
865
|
+
data,
|
|
866
|
+
trigger: {
|
|
867
|
+
...trigger,
|
|
868
|
+
...rest
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
} catch (err) {
|
|
872
|
+
log4.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 76, S: void 0 });
|
|
873
|
+
return {
|
|
874
|
+
data: {
|
|
875
|
+
bodyText,
|
|
876
|
+
...rest
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
};
|
|
881
|
+
var handleFunctionMetaCall = (functionDefinition, request) => {
|
|
882
|
+
const response = {
|
|
883
|
+
key: functionDefinition.meta.key,
|
|
884
|
+
name: functionDefinition.meta.name,
|
|
885
|
+
description: functionDefinition.meta.description,
|
|
886
|
+
inputSchema: functionDefinition.meta.inputSchema,
|
|
887
|
+
outputSchema: functionDefinition.meta.outputSchema
|
|
888
|
+
};
|
|
889
|
+
return new Response(JSON.stringify(response), {
|
|
890
|
+
headers: {
|
|
891
|
+
"Content-Type": "application/json"
|
|
892
|
+
}
|
|
893
|
+
});
|
|
894
|
+
};
|
|
895
|
+
var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
|
|
896
|
+
const { dataService, queryService, queueService, functionsAiService } = await serviceContainer.createServices();
|
|
897
|
+
let spaceKey;
|
|
898
|
+
let rootUrl;
|
|
899
|
+
if (contextSpaceId) {
|
|
900
|
+
const meta = await serviceContainer.getSpaceMeta(contextSpaceId);
|
|
901
|
+
if (!meta) {
|
|
902
|
+
throw new Error(`Space not found: ${contextSpaceId}`);
|
|
903
|
+
}
|
|
904
|
+
spaceKey = meta.spaceKey;
|
|
905
|
+
invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 109, S: void 0, A: ["!meta.rootDocumentId.startsWith('automerge:')", ""] });
|
|
906
|
+
rootUrl = `automerge:${meta.rootDocumentId}`;
|
|
907
|
+
}
|
|
908
|
+
return {
|
|
909
|
+
services: {
|
|
910
|
+
dataService,
|
|
911
|
+
queryService,
|
|
912
|
+
queueService,
|
|
913
|
+
functionsAiService
|
|
914
|
+
},
|
|
915
|
+
spaceId: contextSpaceId,
|
|
916
|
+
spaceKey,
|
|
917
|
+
spaceRootUrl: rootUrl
|
|
918
|
+
};
|
|
919
|
+
};
|
|
920
|
+
|
|
921
|
+
// src/logger.ts
|
|
922
|
+
import { LogLevel, log as log5, shouldLog } from "@dxos/log";
|
|
923
|
+
var setupFunctionsLogger = () => {
|
|
924
|
+
log5.runtimeConfig.processors.length = 0;
|
|
925
|
+
log5.runtimeConfig.processors.push(functionLogProcessor);
|
|
926
|
+
};
|
|
927
|
+
var functionLogProcessor = (config, entry) => {
|
|
928
|
+
if (!shouldLog(entry, config.filters)) {
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
const context = entry.computedContext;
|
|
932
|
+
const error = entry.computedError;
|
|
933
|
+
const extras = [
|
|
934
|
+
Object.keys(context).length > 0 ? context : void 0,
|
|
935
|
+
error
|
|
936
|
+
].filter((value) => value !== void 0);
|
|
937
|
+
switch (entry.level) {
|
|
938
|
+
case LogLevel.DEBUG:
|
|
939
|
+
case LogLevel.TRACE:
|
|
940
|
+
console.debug(entry.message, ...extras);
|
|
941
|
+
break;
|
|
942
|
+
case LogLevel.VERBOSE:
|
|
943
|
+
console.log(entry.message, ...extras);
|
|
944
|
+
break;
|
|
945
|
+
case LogLevel.INFO:
|
|
946
|
+
console.info(entry.message, ...extras);
|
|
947
|
+
break;
|
|
948
|
+
case LogLevel.WARN:
|
|
949
|
+
console.warn(entry.message, ...extras);
|
|
950
|
+
break;
|
|
951
|
+
case LogLevel.ERROR:
|
|
952
|
+
console.error(entry.message, ...extras);
|
|
953
|
+
break;
|
|
954
|
+
default:
|
|
955
|
+
console.log(entry.message, ...extras);
|
|
956
|
+
break;
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
export {
|
|
960
|
+
FUNCTION_ROUTE_HEADER,
|
|
961
|
+
FunctionRouteValue,
|
|
962
|
+
FunctionsClient,
|
|
963
|
+
ServiceContainer,
|
|
964
|
+
createClientFromEnv,
|
|
965
|
+
createFunctionContext,
|
|
966
|
+
setupFunctionsLogger,
|
|
967
|
+
wrapHandlerForCloudflare
|
|
968
|
+
};
|
|
969
|
+
//# sourceMappingURL=index.mjs.map
|