@dxos/functions-runtime-cloudflare 0.8.4-main.66e292d → 0.8.4-main.69d29f4

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 +769 -113
  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 +769 -113
  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 +4 -1
  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 +4 -1
  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,17 +1,87 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
- // src/wrap-handler-for-cloudflare.ts
4
- import { invariant as invariant4 } from "@dxos/invariant";
5
- import { SpaceId as SpaceId4 } from "@dxos/keys";
6
- import { log as log3 } from "@dxos/log";
7
- import { EdgeResponse } from "@dxos/protocols";
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";
8
7
 
9
8
  // src/internal/data-service-impl.ts
10
9
  import { Stream } from "@dxos/codec-protobuf/stream";
11
10
  import { raise } from "@dxos/debug";
11
+ import { NotImplementedError, RuntimeServiceError } from "@dxos/errors";
12
12
  import { invariant } from "@dxos/invariant";
13
13
  import { SpaceId } from "@dxos/keys";
14
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
+ }
15
85
  var __dxlog_file = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/data-service-impl.ts";
16
86
  var DataServiceImpl = class {
17
87
  _executionContext;
@@ -25,7 +95,7 @@ var DataServiceImpl = class {
25
95
  return new Stream(({ next }) => {
26
96
  invariant(SpaceId.isValid(spaceId), void 0, {
27
97
  F: __dxlog_file,
28
- L: 34,
98
+ L: 39,
29
99
  S: this,
30
100
  A: [
31
101
  "SpaceId.isValid(spaceId)",
@@ -41,74 +111,141 @@ var DataServiceImpl = class {
41
111
  };
42
112
  });
43
113
  }
44
- async updateSubscription({ subscriptionId, addIds, removeIds }) {
45
- 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
+ }));
46
121
  if (addIds) {
47
122
  log.info("request documents", {
48
123
  count: addIds.length
49
124
  }, {
50
125
  F: __dxlog_file,
51
- L: 47,
126
+ L: 59,
52
127
  S: this,
53
128
  C: (f, a) => f(...a)
54
129
  });
55
130
  for (const documentId of addIds) {
56
- const document = await this._dataService.getDocument(this._executionContext, sub.spaceId, documentId);
57
- log.info("document loaded", {
58
- documentId,
59
- spaceId: sub.spaceId,
60
- found: !!document
61
- }, {
62
- F: __dxlog_file,
63
- L: 51,
64
- S: this,
65
- C: (f, a) => f(...a)
66
- });
67
- if (!document) {
68
- log.warn("not found", {
69
- 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
70
142
  }, {
71
143
  F: __dxlog_file,
72
- L: 53,
144
+ L: 63,
73
145
  S: this,
74
146
  C: (f, a) => f(...a)
75
147
  });
76
- 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);
77
174
  }
78
- sub.next({
79
- updates: [
80
- {
81
- documentId,
82
- mutation: document.data
83
- }
84
- ]
85
- });
86
175
  }
87
176
  }
88
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
+ }
89
199
  async update({ updates, subscriptionId }) {
90
- const sub = this.dataSubscriptions.get(subscriptionId) ?? raise(new Error("Subscription not found"));
91
- for (const update of updates ?? []) {
92
- 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);
93
218
  }
94
- throw new Error("Method not implemented.");
95
219
  }
96
220
  async flush() {
97
221
  }
98
- subscribeSpaceSyncState(request, options) {
99
- throw new Error("Method not implemented.");
222
+ subscribeSpaceSyncState(_request, _options) {
223
+ throw new NotImplementedError({
224
+ message: "subscribeSpaceSyncState is not implemented."
225
+ });
100
226
  }
101
- async getDocumentHeads({ documentIds }) {
102
- throw new Error("Method not implemented.");
227
+ async getDocumentHeads({ documentIds: _documentIds }) {
228
+ throw new NotImplementedError({
229
+ message: "getDocumentHeads is not implemented."
230
+ });
103
231
  }
104
- async reIndexHeads({ documentIds }) {
105
- throw new Error("Method not implemented.");
232
+ async reIndexHeads({ documentIds: _documentIds }) {
233
+ throw new NotImplementedError({
234
+ message: "reIndexHeads is not implemented."
235
+ });
106
236
  }
107
237
  async updateIndexes() {
108
- 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
+ });
109
244
  }
110
- async waitUntilHeadsReplicated({ heads }) {
111
- throw new Error("Method not implemented.");
245
+ async waitUntilHeadsReplicated({ heads: _heads }) {
246
+ throw new NotImplementedError({
247
+ message: "waitUntilHeadsReplicated is not implemented."
248
+ });
112
249
  }
113
250
  };
114
251
 
@@ -116,6 +253,7 @@ var DataServiceImpl = class {
116
253
  import * as Schema from "effect/Schema";
117
254
  import { Stream as Stream2 } from "@dxos/codec-protobuf/stream";
118
255
  import { QueryAST } from "@dxos/echo-protocol";
256
+ import { NotImplementedError as NotImplementedError2, RuntimeServiceError as RuntimeServiceError2 } from "@dxos/errors";
119
257
  import { invariant as invariant3 } from "@dxos/invariant";
120
258
  import { PublicKey } from "@dxos/keys";
121
259
  import { SpaceId as SpaceId3 } from "@dxos/keys";
@@ -189,10 +327,76 @@ var isSimpleSelectionQuery = (query) => {
189
327
  };
190
328
 
191
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
+ }
192
395
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
193
396
  var QueryServiceImpl = class {
194
397
  _executionContext;
195
398
  _dataService;
399
+ _queryCount = 0;
196
400
  constructor(_executionContext, _dataService) {
197
401
  this._executionContext = _executionContext;
198
402
  this._dataService = _dataService;
@@ -202,7 +406,7 @@ var QueryServiceImpl = class {
202
406
  request
203
407
  }, {
204
408
  F: __dxlog_file3,
205
- L: 30,
409
+ L: 34,
206
410
  S: this,
207
411
  C: (f, a) => f(...a)
208
412
  });
@@ -210,7 +414,7 @@ var QueryServiceImpl = class {
210
414
  const requestedSpaceIds = getTargetSpacesForQuery(query);
211
415
  invariant3(requestedSpaceIds.length === 1, "Only one space is supported", {
212
416
  F: __dxlog_file3,
213
- L: 33,
417
+ L: 37,
214
418
  S: this,
215
419
  A: [
216
420
  "requestedSpaceIds.length === 1",
@@ -220,53 +424,81 @@ var QueryServiceImpl = class {
220
424
  const spaceId = requestedSpaceIds[0];
221
425
  return Stream2.fromPromise((async () => {
222
426
  try {
223
- log2.info("begin query", {
224
- spaceId
225
- }, {
226
- F: __dxlog_file3,
227
- L: 39,
228
- S: this,
229
- C: (f, a) => f(...a)
230
- });
231
- const queryResponse = await this._dataService.queryDocuments(this._executionContext, queryToDataServiceRequest(query));
232
- log2.info("query response", {
233
- spaceId,
234
- filter: request.filter,
235
- resultCount: queryResponse.results.length
236
- }, {
237
- F: __dxlog_file3,
238
- L: 44,
239
- S: this,
240
- C: (f, a) => f(...a)
241
- });
242
- return {
243
- results: queryResponse.results.map((object) => ({
244
- id: object.objectId,
245
- spaceId,
246
- spaceKey: PublicKey.ZERO,
247
- documentId: object.document.documentId,
248
- rank: 0,
249
- documentAutomerge: object.document.data
250
- }))
427
+ const env = {
428
+ stack: [],
429
+ error: void 0,
430
+ hasError: false
251
431
  };
252
- } 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) {
253
473
  log2.error("query failed", {
254
- err
474
+ err: error
255
475
  }, {
256
476
  F: __dxlog_file3,
257
- L: 58,
477
+ L: 66,
258
478
  S: this,
259
479
  C: (f, a) => f(...a)
260
480
  });
261
- 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
+ });
262
490
  }
263
491
  })());
264
492
  }
265
493
  async reindex() {
266
- throw new Error("Method not implemented.");
494
+ throw new NotImplementedError2({
495
+ message: "Reindex is not implemented."
496
+ });
267
497
  }
268
498
  async setConfig() {
269
- throw new Error("Method not implemented.");
499
+ throw new NotImplementedError2({
500
+ message: "SetConfig is not implemented."
501
+ });
270
502
  }
271
503
  };
272
504
  var getTargetSpacesForQuery = (query) => {
@@ -287,6 +519,74 @@ var getTargetSpacesForQuery = (query) => {
287
519
  };
288
520
 
289
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";
290
590
  var QueueServiceImpl = class {
291
591
  _ctx;
292
592
  _queueService;
@@ -294,29 +594,177 @@ var QueueServiceImpl = class {
294
594
  this._ctx = _ctx;
295
595
  this._queueService = _queueService;
296
596
  }
297
- queryQueue(subspaceTag, spaceId, { queueId, ...query }) {
298
- 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
+ }
299
643
  }
300
- insertIntoQueue(subspaceTag, spaceId, queueId, objects) {
301
- 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
+ }
302
659
  }
303
- deleteFromQueue(subspaceTag, spaceId, queueId, objectIds) {
304
- 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
+ });
305
670
  }
306
671
  };
307
672
 
308
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
+ }
309
739
  var ServiceContainer = class {
310
740
  _executionContext;
311
741
  _dataService;
312
742
  _queueService;
313
- constructor(_executionContext, _dataService, _queueService) {
743
+ _functionsService;
744
+ constructor(_executionContext, _dataService, _queueService, _functionsService) {
314
745
  this._executionContext = _executionContext;
315
746
  this._dataService = _dataService;
316
747
  this._queueService = _queueService;
748
+ this._functionsService = _functionsService;
317
749
  }
318
750
  async getSpaceMeta(spaceId) {
319
- 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
+ }
320
768
  }
321
769
  async createServices() {
322
770
  const dataService = new DataServiceImpl(this._executionContext, this._dataService);
@@ -325,16 +773,188 @@ var ServiceContainer = class {
325
773
  return {
326
774
  dataService,
327
775
  queryService,
328
- queueService
776
+ queueService,
777
+ functionsAiService: this._functionsService
778
+ };
779
+ }
780
+ async queryQueue(queue) {
781
+ const env = {
782
+ stack: [],
783
+ error: void 0,
784
+ hasError: false
329
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;
330
820
  }
331
- queryQueue(queue) {
332
- return this._queueService.query({}, queue.toString(), {});
821
+ queryQueue(queue, options) {
822
+ return this._serviceContainer.queryQueue(queue);
333
823
  }
334
824
  insertIntoQueue(queue, objects) {
335
- return this._queueService.append({}, queue.toString(), objects);
825
+ return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
826
+ }
827
+ };
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;
336
947
  }
337
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
+ };
338
958
 
339
959
  // src/types.ts
340
960
  var FUNCTION_ROUTE_HEADER = "X-DXOS-Function-Route";
@@ -344,18 +964,14 @@ var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
344
964
  })({});
345
965
 
346
966
  // src/wrap-handler-for-cloudflare.ts
347
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
967
+ import { invariant as invariant7 } from "@dxos/invariant";
968
+ import { SpaceId as SpaceId4 } from "@dxos/keys";
969
+ import { log as log3 } from "@dxos/log";
970
+ import { EdgeResponse } from "@dxos/protocols";
971
+ var __dxlog_file7 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
348
972
  var wrapHandlerForCloudflare = (func) => {
349
973
  return async (request, env) => {
350
974
  if (request.headers.get(FUNCTION_ROUTE_HEADER) === FunctionRouteValue.Meta) {
351
- log3.info(">>> meta", {
352
- func
353
- }, {
354
- F: __dxlog_file4,
355
- L: 25,
356
- S: void 0,
357
- C: (f, a) => f(...a)
358
- });
359
975
  return handleFunctionMetaCall(func, request);
360
976
  }
361
977
  try {
@@ -367,7 +983,7 @@ var wrapHandlerForCloudflare = (func) => {
367
983
  });
368
984
  }
369
985
  }
370
- 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);
371
987
  const context = await createFunctionContext({
372
988
  serviceContainer,
373
989
  contextSpaceId: spaceId
@@ -378,8 +994,8 @@ var wrapHandlerForCloudflare = (func) => {
378
994
  error,
379
995
  stack: error.stack
380
996
  }, {
381
- F: __dxlog_file4,
382
- L: 45,
997
+ F: __dxlog_file7,
998
+ L: 44,
383
999
  S: void 0,
384
1000
  C: (f, a) => f(...a)
385
1001
  });
@@ -416,8 +1032,8 @@ var decodeRequest = async (request) => {
416
1032
  };
417
1033
  } catch (err) {
418
1034
  log3.catch(err, void 0, {
419
- F: __dxlog_file4,
420
- L: 80,
1035
+ F: __dxlog_file7,
1036
+ L: 79,
421
1037
  S: void 0,
422
1038
  C: (f, a) => f(...a)
423
1039
  });
@@ -444,7 +1060,7 @@ var handleFunctionMetaCall = (functionDefinition, request) => {
444
1060
  });
445
1061
  };
446
1062
  var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
447
- const { dataService, queryService, queueService } = await serviceContainer.createServices();
1063
+ const { dataService, queryService, queueService, functionsAiService } = await serviceContainer.createServices();
448
1064
  let spaceKey;
449
1065
  let rootUrl;
450
1066
  if (contextSpaceId) {
@@ -453,9 +1069,9 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
453
1069
  throw new Error(`Space not found: ${contextSpaceId}`);
454
1070
  }
455
1071
  spaceKey = meta.spaceKey;
456
- invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
457
- F: __dxlog_file4,
458
- L: 118,
1072
+ invariant7(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
1073
+ F: __dxlog_file7,
1074
+ L: 117,
459
1075
  S: void 0,
460
1076
  A: [
461
1077
  "!meta.rootDocumentId.startsWith('automerge:')",
@@ -468,16 +1084,56 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
468
1084
  services: {
469
1085
  dataService,
470
1086
  queryService,
471
- queueService
1087
+ queueService,
1088
+ functionsAiService
472
1089
  },
473
1090
  spaceId: contextSpaceId,
474
1091
  spaceKey,
475
1092
  spaceRootUrl: rootUrl
476
1093
  };
477
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
+ };
478
1130
  export {
479
1131
  FUNCTION_ROUTE_HEADER,
480
1132
  FunctionRouteValue,
1133
+ FunctionsClient,
1134
+ ServiceContainer,
1135
+ createClientFromEnv,
1136
+ setupFunctionsLogger,
481
1137
  wrapHandlerForCloudflare
482
1138
  };
483
1139
  //# sourceMappingURL=index.mjs.map