@dxos/functions-runtime-cloudflare 0.8.4-main.70d3990 → 0.8.4-main.937b3ca

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