@dxos/functions-runtime-cloudflare 0.8.4-main.ef1bc66f44 → 0.8.4-main.fcc0d83b33

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 (35) hide show
  1. package/dist/lib/browser/index.mjs +152 -322
  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 +152 -322
  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.map +1 -1
  8. package/dist/types/src/internal/data-service-impl.d.ts +1 -1
  9. package/dist/types/src/internal/data-service-impl.d.ts.map +1 -1
  10. package/dist/types/src/internal/query-service-impl.d.ts +3 -9
  11. package/dist/types/src/internal/query-service-impl.d.ts.map +1 -1
  12. package/dist/types/src/internal/queue-service-impl.d.ts +3 -2
  13. package/dist/types/src/internal/queue-service-impl.d.ts.map +1 -1
  14. package/dist/types/src/internal/service-container.d.ts +1 -1
  15. package/dist/types/src/internal/service-container.d.ts.map +1 -1
  16. package/dist/types/src/internal/utils.d.ts.map +1 -1
  17. package/dist/types/src/queues-api.d.ts +7 -3
  18. package/dist/types/src/queues-api.d.ts.map +1 -1
  19. package/dist/types/src/space-proxy.d.ts.map +1 -1
  20. package/dist/types/src/wrap-handler-for-cloudflare.d.ts +6 -0
  21. package/dist/types/src/wrap-handler-for-cloudflare.d.ts.map +1 -1
  22. package/dist/types/tsconfig.tsbuildinfo +1 -1
  23. package/package.json +16 -16
  24. package/src/functions-client.ts +1 -1
  25. package/src/internal/data-service-impl.ts +3 -2
  26. package/src/internal/query-service-impl.ts +8 -62
  27. package/src/internal/queue-service-impl.ts +28 -24
  28. package/src/internal/service-container.ts +24 -6
  29. package/src/logger.ts +10 -8
  30. package/src/queues-api.ts +23 -4
  31. package/src/space-proxy.ts +1 -1
  32. package/src/wrap-handler-for-cloudflare.ts +1 -1
  33. package/dist/types/src/internal/adapter.d.ts +0 -12
  34. package/dist/types/src/internal/adapter.d.ts.map +0 -1
  35. package/src/internal/adapter.ts +0 -48
@@ -1,7 +1,7 @@
1
1
  // src/functions-client.ts
2
2
  import { Resource as Resource2 } from "@dxos/context";
3
3
  import { EchoClient } from "@dxos/echo-db";
4
- import { invariant as invariant6 } from "@dxos/invariant";
4
+ import { invariant as invariant3 } from "@dxos/invariant";
5
5
 
6
6
  // src/internal/data-service-impl.ts
7
7
  import { Stream } from "@dxos/codec-protobuf/stream";
@@ -15,6 +15,7 @@ import { log } from "@dxos/log";
15
15
  var copyUint8Array = (value) => new Uint8Array(value);
16
16
 
17
17
  // src/internal/data-service-impl.ts
18
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/data-service-impl.ts";
18
19
  function _ts_add_disposable_resource(env, value, async) {
19
20
  if (value !== null && value !== void 0) {
20
21
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -80,7 +81,6 @@ function _ts_dispose_resources(env) {
80
81
  return next();
81
82
  })(env);
82
83
  }
83
- var __dxlog_file = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/data-service-impl.ts";
84
84
  var DataServiceImpl = class {
85
85
  _executionContext;
86
86
  _dataService;
@@ -91,15 +91,7 @@ var DataServiceImpl = class {
91
91
  }
92
92
  subscribe({ subscriptionId, spaceId }) {
93
93
  return new Stream(({ next }) => {
94
- invariant(SpaceId.isValid(spaceId), void 0, {
95
- F: __dxlog_file,
96
- L: 39,
97
- S: this,
98
- A: [
99
- "SpaceId.isValid(spaceId)",
100
- ""
101
- ]
102
- });
94
+ invariant(SpaceId.isValid(spaceId), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 86, S: this, A: ["SpaceId.isValid(spaceId)", ""] });
103
95
  this.dataSubscriptions.set(subscriptionId, {
104
96
  spaceId,
105
97
  next
@@ -119,12 +111,7 @@ var DataServiceImpl = class {
119
111
  if (addIds) {
120
112
  log.info("request documents", {
121
113
  count: addIds.length
122
- }, {
123
- F: __dxlog_file,
124
- L: 59,
125
- S: this,
126
- C: (f, a) => f(...a)
127
- });
114
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 104, S: this });
128
115
  for (const documentId of addIds) {
129
116
  const env = {
130
117
  stack: [],
@@ -137,21 +124,11 @@ var DataServiceImpl = class {
137
124
  documentId,
138
125
  spaceId: sub.spaceId,
139
126
  found: !!document
140
- }, {
141
- F: __dxlog_file,
142
- L: 63,
143
- S: this,
144
- C: (f, a) => f(...a)
145
- });
127
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 116, S: this });
146
128
  if (!document) {
147
129
  log.warn("not found", {
148
130
  documentId
149
- }, {
150
- F: __dxlog_file,
151
- L: 65,
152
- S: this,
153
- C: (f, a) => f(...a)
154
- });
131
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 122, S: this });
155
132
  continue;
156
133
  }
157
134
  sub.next({
@@ -180,10 +157,8 @@ var DataServiceImpl = class {
180
157
  hasError: false
181
158
  };
182
159
  try {
183
- const response = _ts_add_disposable_resource(env, await this._dataService.createDocument(this._executionContext, {
184
- spaceId,
185
- initialValue
186
- }), false);
160
+ invariant(SpaceId.isValid(spaceId), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 153, S: this, A: ["SpaceId.isValid(spaceId)", ""] });
161
+ const response = _ts_add_disposable_resource(env, await this._dataService.createDocument(this._executionContext, spaceId, initialValue), false);
187
162
  return {
188
163
  documentId: response.documentId
189
164
  };
@@ -233,12 +208,7 @@ var DataServiceImpl = class {
233
208
  });
234
209
  }
235
210
  async updateIndexes() {
236
- log.error("updateIndexes is not available in EDGE env.", void 0, {
237
- F: __dxlog_file,
238
- L: 133,
239
- S: this,
240
- C: (f, a) => f(...a)
241
- });
211
+ log.error("updateIndexes is not available in EDGE env.", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 206, S: this });
242
212
  }
243
213
  async waitUntilHeadsReplicated({ heads: _heads }) {
244
214
  throw new NotImplementedError({
@@ -248,83 +218,10 @@ var DataServiceImpl = class {
248
218
  };
249
219
 
250
220
  // src/internal/query-service-impl.ts
251
- import * as Schema from "effect/Schema";
252
221
  import { Stream as Stream2 } from "@dxos/codec-protobuf/stream";
253
- import { QueryAST } from "@dxos/echo-protocol";
254
222
  import { NotImplementedError as NotImplementedError2, RuntimeServiceError as RuntimeServiceError2 } from "@dxos/errors";
255
- import { invariant as invariant3 } from "@dxos/invariant";
256
- import { PublicKey } from "@dxos/keys";
257
- import { SpaceId as SpaceId3 } from "@dxos/keys";
258
223
  import { log as log2 } from "@dxos/log";
259
-
260
- // src/internal/adapter.ts
261
- import { failUndefined } from "@dxos/debug";
262
- import { invariant as invariant2 } from "@dxos/invariant";
263
- import { SpaceId as SpaceId2 } from "@dxos/keys";
264
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/adapter.ts";
265
- var queryToDataServiceRequest = (query) => {
266
- const { filter, options } = isSimpleSelectionQuery(query) ?? failUndefined();
267
- invariant2(options?.spaceIds?.length === 1, "Only one space is supported", {
268
- F: __dxlog_file2,
269
- L: 13,
270
- S: void 0,
271
- A: [
272
- "options?.spaceIds?.length === 1",
273
- "'Only one space is supported'"
274
- ]
275
- });
276
- invariant2(filter.type === "object", "Only object filters are supported", {
277
- F: __dxlog_file2,
278
- L: 14,
279
- S: void 0,
280
- A: [
281
- "filter.type === 'object'",
282
- "'Only object filters are supported'"
283
- ]
284
- });
285
- const spaceId = options.spaceIds[0];
286
- invariant2(SpaceId2.isValid(spaceId), void 0, {
287
- F: __dxlog_file2,
288
- L: 17,
289
- S: void 0,
290
- A: [
291
- "SpaceId.isValid(spaceId)",
292
- ""
293
- ]
294
- });
295
- return {
296
- spaceId,
297
- type: filter.typename ?? void 0,
298
- objectIds: [
299
- ...filter.id ?? []
300
- ]
301
- };
302
- };
303
- var isSimpleSelectionQuery = (query) => {
304
- switch (query.type) {
305
- case "options": {
306
- const maybeFilter = isSimpleSelectionQuery(query.query);
307
- if (!maybeFilter) {
308
- return null;
309
- }
310
- return {
311
- filter: maybeFilter.filter,
312
- options: query.options
313
- };
314
- }
315
- case "select": {
316
- return {
317
- filter: query.filter,
318
- options: void 0
319
- };
320
- }
321
- default: {
322
- return null;
323
- }
324
- }
325
- };
326
-
327
- // src/internal/query-service-impl.ts
224
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
328
225
  function _ts_add_disposable_resource2(env, value, async) {
329
226
  if (value !== null && value !== void 0) {
330
227
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -390,7 +287,6 @@ function _ts_dispose_resources2(env) {
390
287
  return next();
391
288
  })(env);
392
289
  }
393
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
394
290
  var QueryServiceImpl = class {
395
291
  _executionContext;
396
292
  _dataService;
@@ -402,24 +298,7 @@ var QueryServiceImpl = class {
402
298
  execQuery(request) {
403
299
  log2.info("execQuery", {
404
300
  request
405
- }, {
406
- F: __dxlog_file3,
407
- L: 34,
408
- S: this,
409
- C: (f, a) => f(...a)
410
- });
411
- const query = QueryAST.Query.pipe(Schema.decodeUnknownSync)(JSON.parse(request.query));
412
- const requestedSpaceIds = getTargetSpacesForQuery(query);
413
- invariant3(requestedSpaceIds.length === 1, "Only one space is supported", {
414
- F: __dxlog_file3,
415
- L: 37,
416
- S: this,
417
- A: [
418
- "requestedSpaceIds.length === 1",
419
- "'Only one space is supported'"
420
- ]
421
- });
422
- const spaceId = requestedSpaceIds[0];
301
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 81, S: this });
423
302
  return Stream2.fromPromise((async () => {
424
303
  try {
425
304
  const env = {
@@ -430,37 +309,13 @@ var QueryServiceImpl = class {
430
309
  try {
431
310
  this._queryCount++;
432
311
  log2.info("begin query", {
433
- spaceId
434
- }, {
435
- F: __dxlog_file3,
436
- L: 44,
437
- S: this,
438
- C: (f, a) => f(...a)
439
- });
440
- const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.queryDocuments(this._executionContext, queryToDataServiceRequest(query)), false);
312
+ request
313
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 93, S: this });
314
+ const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.execQuery(this._executionContext, request), false);
441
315
  log2.info("query response", {
442
- spaceId,
443
- filter: request.filter,
444
- resultCount: queryResponse.results.length
445
- }, {
446
- F: __dxlog_file3,
447
- L: 49,
448
- S: this,
449
- C: (f, a) => f(...a)
450
- });
451
- return {
452
- results: queryResponse.results.map((object) => ({
453
- id: object.objectId,
454
- spaceId,
455
- spaceKey: PublicKey.ZERO,
456
- documentId: object.document.documentId,
457
- // Rank 1 for predicate matches where ranking is not determined.
458
- rank: 1,
459
- // Copy returned object to avoid hanging RPC stub.
460
- // See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
461
- documentAutomerge: copyUint8Array(object.document.data)
462
- }))
463
- };
316
+ resultCount: queryResponse.results?.length
317
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 97, S: this });
318
+ return structuredClone(queryResponse);
464
319
  } catch (e) {
465
320
  env.error = e;
466
321
  env.hasError = true;
@@ -470,17 +325,10 @@ var QueryServiceImpl = class {
470
325
  } catch (error) {
471
326
  log2.error("query failed", {
472
327
  err: error
473
- }, {
474
- F: __dxlog_file3,
475
- L: 66,
476
- S: this,
477
- C: (f, a) => f(...a)
478
- });
328
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 108, S: this });
479
329
  throw new RuntimeServiceError2({
480
330
  message: `Query execution failed (queryCount=${this._queryCount})`,
481
331
  context: {
482
- spaceId,
483
- filter: request.filter,
484
332
  queryCount: this._queryCount
485
333
  },
486
334
  cause: error
@@ -499,26 +347,9 @@ var QueryServiceImpl = class {
499
347
  });
500
348
  }
501
349
  };
502
- var getTargetSpacesForQuery = (query) => {
503
- const spaces = /* @__PURE__ */ new Set();
504
- const visitor = (node) => {
505
- if (node.type === "options") {
506
- if (node.options.spaceIds) {
507
- for (const spaceId of node.options.spaceIds) {
508
- spaces.add(SpaceId3.make(spaceId));
509
- }
510
- }
511
- }
512
- };
513
- QueryAST.visit(query, visitor);
514
- return [
515
- ...spaces
516
- ];
517
- };
518
350
 
519
351
  // src/internal/queue-service-impl.ts
520
- import { NotImplementedError as NotImplementedError3, RuntimeServiceError as RuntimeServiceError3 } from "@dxos/errors";
521
- import { invariant as invariant4 } from "@dxos/invariant";
352
+ import { RuntimeServiceError as RuntimeServiceError3 } from "@dxos/errors";
522
353
  function _ts_add_disposable_resource3(env, value, async) {
523
354
  if (value !== null && value !== void 0) {
524
355
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -584,7 +415,6 @@ function _ts_dispose_resources3(env) {
584
415
  return next();
585
416
  })(env);
586
417
  }
587
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/queue-service-impl.ts";
588
418
  var QueueServiceImpl = class {
589
419
  _ctx;
590
420
  _queueService;
@@ -593,19 +423,6 @@ var QueueServiceImpl = class {
593
423
  this._queueService = _queueService;
594
424
  }
595
425
  async queryQueue(request) {
596
- const { query } = request;
597
- const { queueIds, ...filter } = query;
598
- const spaceId = query.spaceId;
599
- const queueId = queueIds?.[0];
600
- invariant4(request.query.queuesNamespace, void 0, {
601
- F: __dxlog_file4,
602
- L: 19,
603
- S: this,
604
- A: [
605
- "request.query.queuesNamespace",
606
- ""
607
- ]
608
- });
609
426
  try {
610
427
  const env = {
611
428
  stack: [],
@@ -613,10 +430,8 @@ var QueueServiceImpl = class {
613
430
  hasError: false
614
431
  };
615
432
  try {
616
- const result = _ts_add_disposable_resource3(env, await this._queueService.query(this._ctx, `dxn:queue:${request.query.queuesNamespace}:${spaceId}:${queueId}`, filter), false);
433
+ const result = _ts_add_disposable_resource3(env, await this._queueService.queryQueue(this._ctx, request), false);
617
434
  return {
618
- // Copy returned object to avoid hanging RPC stub
619
- // See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
620
435
  objects: structuredClone(result.objects),
621
436
  nextCursor: result.nextCursor,
622
437
  prevCursor: result.prevCursor
@@ -628,22 +443,35 @@ var QueueServiceImpl = class {
628
443
  _ts_dispose_resources3(env);
629
444
  }
630
445
  } catch (error) {
446
+ const { query } = request;
631
447
  throw RuntimeServiceError3.wrap({
632
448
  message: "Queue query failed.",
633
449
  context: {
634
- subspaceTag: request.query.queuesNamespace,
635
- spaceId,
636
- queueId
450
+ subspaceTag: query?.queuesNamespace,
451
+ spaceId: query?.spaceId,
452
+ queueId: query?.queueIds?.[0]
637
453
  },
638
454
  ifTypeDiffers: true
639
455
  })(error);
640
456
  }
641
457
  }
642
458
  async insertIntoQueue(request) {
643
- const { subspaceTag, spaceId, queueId, objects } = request;
644
459
  try {
645
- await this._queueService.append(this._ctx, `dxn:queue:${subspaceTag}:${spaceId}:${queueId}`, objects ?? []);
460
+ const env = {
461
+ stack: [],
462
+ error: void 0,
463
+ hasError: false
464
+ };
465
+ try {
466
+ const _ = _ts_add_disposable_resource3(env, await this._queueService.insertIntoQueue(this._ctx, request), false);
467
+ } catch (e) {
468
+ env.error = e;
469
+ env.hasError = true;
470
+ } finally {
471
+ _ts_dispose_resources3(env);
472
+ }
646
473
  } catch (error) {
474
+ const { subspaceTag, spaceId, queueId } = request;
647
475
  throw RuntimeServiceError3.wrap({
648
476
  message: "Queue append failed.",
649
477
  context: {
@@ -655,16 +483,35 @@ var QueueServiceImpl = class {
655
483
  })(error);
656
484
  }
657
485
  }
658
- deleteFromQueue(request) {
659
- const { subspaceTag, spaceId, queueId } = request;
660
- throw new NotImplementedError3({
661
- message: "Deleting from queue is not supported.",
662
- context: {
663
- subspaceTag,
664
- spaceId,
665
- queueId
486
+ async deleteFromQueue(request) {
487
+ try {
488
+ const env = {
489
+ stack: [],
490
+ error: void 0,
491
+ hasError: false
492
+ };
493
+ try {
494
+ const _ = _ts_add_disposable_resource3(env, await this._queueService.deleteFromQueue(this._ctx, request), false);
495
+ } catch (e) {
496
+ env.error = e;
497
+ env.hasError = true;
498
+ } finally {
499
+ _ts_dispose_resources3(env);
666
500
  }
667
- });
501
+ } catch (error) {
502
+ const { subspaceTag, spaceId, queueId } = request;
503
+ throw RuntimeServiceError3.wrap({
504
+ message: "Queue delete failed.",
505
+ context: {
506
+ subspaceTag,
507
+ spaceId,
508
+ queueId
509
+ },
510
+ ifTypeDiffers: true
511
+ })(error);
512
+ }
513
+ }
514
+ async syncQueue(_) {
668
515
  }
669
516
  };
670
517
 
@@ -776,39 +623,49 @@ var ServiceContainer = class {
776
623
  };
777
624
  }
778
625
  async queryQueue(queue) {
779
- const env = {
780
- stack: [],
781
- error: void 0,
782
- hasError: false
783
- };
784
- try {
785
- const { spaceId } = queue.asQueueDXN() ?? {};
786
- const result = _ts_add_disposable_resource4(env, await this._queueService.query({}, queue.toString(), {
787
- spaceId
788
- }), false);
789
- return {
790
- objects: structuredClone(result.objects),
791
- nextCursor: result.nextCursor ?? null,
792
- prevCursor: result.prevCursor ?? null
793
- };
794
- } catch (e) {
795
- env.error = e;
796
- env.hasError = true;
797
- } finally {
798
- _ts_dispose_resources4(env);
626
+ const parts = queue.asQueueDXN();
627
+ if (!parts) {
628
+ throw new Error("Invalid queue DXN");
799
629
  }
630
+ const { subspaceTag, spaceId, queueId } = parts;
631
+ const result = await this._queueService.queryQueue(this._executionContext, {
632
+ query: {
633
+ spaceId,
634
+ queuesNamespace: subspaceTag,
635
+ queueIds: [
636
+ queueId
637
+ ]
638
+ }
639
+ });
640
+ return {
641
+ objects: structuredClone(result.objects),
642
+ nextCursor: result.nextCursor ?? null,
643
+ prevCursor: result.prevCursor ?? null
644
+ };
800
645
  }
801
646
  async insertIntoQueue(queue, objects) {
802
- await this._queueService.append({}, queue.toString(), objects);
647
+ const parts = queue.asQueueDXN();
648
+ if (!parts) {
649
+ throw new Error("Invalid queue DXN");
650
+ }
651
+ const { subspaceTag, spaceId, queueId } = parts;
652
+ await this._queueService.insertIntoQueue(this._executionContext, {
653
+ subspaceTag,
654
+ spaceId,
655
+ queueId,
656
+ objects: objects.map((obj) => JSON.stringify(obj))
657
+ });
803
658
  }
804
659
  };
805
660
 
806
661
  // src/space-proxy.ts
807
662
  import { Resource } from "@dxos/context";
808
- import { invariant as invariant5 } from "@dxos/invariant";
809
- import { PublicKey as PublicKey2 } from "@dxos/keys";
663
+ import { invariant as invariant2 } from "@dxos/invariant";
664
+ import { PublicKey } from "@dxos/keys";
810
665
 
811
666
  // src/queues-api.ts
667
+ import { log as log3 } from "@dxos/log";
668
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/queues-api.ts";
812
669
  var QueuesAPIImpl = class {
813
670
  _serviceContainer;
814
671
  _spaceId;
@@ -816,8 +673,26 @@ var QueuesAPIImpl = class {
816
673
  this._serviceContainer = _serviceContainer;
817
674
  this._spaceId = _spaceId;
818
675
  }
819
- queryQueue(queue, options) {
820
- return this._serviceContainer.queryQueue(queue);
676
+ async queryQueue(queue, options) {
677
+ const result = await this._serviceContainer.queryQueue(queue);
678
+ const objects = (result.objects ?? []).flatMap((encoded) => {
679
+ try {
680
+ return [
681
+ JSON.parse(encoded)
682
+ ];
683
+ } catch (err) {
684
+ log3.verbose("queue object JSON parse failed; object ignored", {
685
+ encoded,
686
+ error: err
687
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 22, S: this });
688
+ return [];
689
+ }
690
+ });
691
+ return {
692
+ objects,
693
+ nextCursor: result.nextCursor ?? null,
694
+ prevCursor: result.prevCursor ?? null
695
+ };
821
696
  }
822
697
  insertIntoQueue(queue, objects) {
823
698
  return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
@@ -825,7 +700,7 @@ var QueuesAPIImpl = class {
825
700
  };
826
701
 
827
702
  // src/space-proxy.ts
828
- var __dxlog_file5 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/space-proxy.ts";
703
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/space-proxy.ts";
829
704
  var SpaceProxy = class extends Resource {
830
705
  _serviceContainer;
831
706
  _echoClient;
@@ -840,30 +715,14 @@ var SpaceProxy = class extends Resource {
840
715
  return this._id;
841
716
  }
842
717
  get db() {
843
- invariant5(this._db, void 0, {
844
- F: __dxlog_file5,
845
- L: 35,
846
- S: this,
847
- A: [
848
- "this._db",
849
- ""
850
- ]
851
- });
718
+ invariant2(this._db, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 24, S: this, A: ["this._db", ""] });
852
719
  return this._db;
853
720
  }
854
721
  /**
855
722
  * @deprecated Use db API.
856
723
  */
857
724
  get crud() {
858
- invariant5(this._db, void 0, {
859
- F: __dxlog_file5,
860
- L: 43,
861
- S: this,
862
- A: [
863
- "this._db",
864
- ""
865
- ]
866
- });
725
+ invariant2(this._db, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 30, S: this, A: ["this._db", ""] });
867
726
  return this._db.coreDatabase;
868
727
  }
869
728
  get queues() {
@@ -876,18 +735,18 @@ var SpaceProxy = class extends Resource {
876
735
  }
877
736
  this._db = this._echoClient.constructDatabase({
878
737
  spaceId: this._id,
879
- spaceKey: PublicKey2.from(meta.spaceKey),
738
+ spaceKey: PublicKey.from(meta.spaceKey),
880
739
  reactiveSchemaQuery: false,
881
740
  owningObject: this
882
741
  });
883
- await this._db.coreDatabase.open({
742
+ await this._db.coreDatabase.open(this._ctx, {
884
743
  rootUrl: meta.rootDocumentId
885
744
  });
886
745
  }
887
746
  };
888
747
 
889
748
  // src/functions-client.ts
890
- var __dxlog_file6 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/functions-client.ts";
749
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/functions-client.ts";
891
750
  var FunctionsClient = class extends Resource2 {
892
751
  _serviceContainer;
893
752
  _echoClient;
@@ -895,24 +754,8 @@ var FunctionsClient = class extends Resource2 {
895
754
  _spaces = /* @__PURE__ */ new Map();
896
755
  constructor(services) {
897
756
  super();
898
- invariant6(typeof services.dataService !== "undefined", "DataService is required", {
899
- F: __dxlog_file6,
900
- L: 33,
901
- S: this,
902
- A: [
903
- "typeof services.dataService !== 'undefined'",
904
- "'DataService is required'"
905
- ]
906
- });
907
- invariant6(typeof services.queueService !== "undefined", "QueueService is required", {
908
- F: __dxlog_file6,
909
- L: 34,
910
- S: this,
911
- A: [
912
- "typeof services.queueService !== 'undefined'",
913
- "'QueueService is required'"
914
- ]
915
- });
757
+ 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'"] });
758
+ 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'"] });
916
759
  this._serviceContainer = new ServiceContainer(this._executionContext, services.dataService, services.queueService, services.functionsAiService);
917
760
  this._echoClient = new EchoClient({});
918
761
  }
@@ -962,11 +805,11 @@ var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
962
805
  })({});
963
806
 
964
807
  // src/wrap-handler-for-cloudflare.ts
965
- import { invariant as invariant7 } from "@dxos/invariant";
966
- import { SpaceId as SpaceId4 } from "@dxos/keys";
967
- import { log as log3 } from "@dxos/log";
808
+ import { invariant as invariant4 } from "@dxos/invariant";
809
+ import { SpaceId as SpaceId2 } from "@dxos/keys";
810
+ import { log as log4 } from "@dxos/log";
968
811
  import { EdgeResponse } from "@dxos/protocols";
969
- var __dxlog_file7 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
812
+ var __dxlog_file6 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
970
813
  var wrapHandlerForCloudflare = (func) => {
971
814
  return async (request, env) => {
972
815
  if (request.headers.get(FUNCTION_ROUTE_HEADER) === FunctionRouteValue.Meta) {
@@ -975,7 +818,7 @@ var wrapHandlerForCloudflare = (func) => {
975
818
  try {
976
819
  const spaceId = new URL(request.url).searchParams.get("spaceId");
977
820
  if (spaceId) {
978
- if (!SpaceId4.isValid(spaceId)) {
821
+ if (!SpaceId2.isValid(spaceId)) {
979
822
  return new Response("Invalid spaceId", {
980
823
  status: 400
981
824
  });
@@ -988,15 +831,10 @@ var wrapHandlerForCloudflare = (func) => {
988
831
  });
989
832
  return EdgeResponse.success(await invokeFunction(func, context, request));
990
833
  } catch (error) {
991
- log3.error("error invoking function", {
834
+ log4.error("error invoking function", {
992
835
  error,
993
836
  stack: error.stack
994
- }, {
995
- F: __dxlog_file7,
996
- L: 44,
997
- S: void 0,
998
- C: (f, a) => f(...a)
999
- });
837
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 37, S: void 0 });
1000
838
  return EdgeResponse.failure({
1001
839
  message: error?.message ?? "Internal error",
1002
840
  error
@@ -1029,12 +867,7 @@ var decodeRequest = async (request) => {
1029
867
  }
1030
868
  };
1031
869
  } catch (err) {
1032
- log3.catch(err, void 0, {
1033
- F: __dxlog_file7,
1034
- L: 79,
1035
- S: void 0,
1036
- C: (f, a) => f(...a)
1037
- });
870
+ log4.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 76, S: void 0 });
1038
871
  return {
1039
872
  data: {
1040
873
  bodyText,
@@ -1067,15 +900,7 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
1067
900
  throw new Error(`Space not found: ${contextSpaceId}`);
1068
901
  }
1069
902
  spaceKey = meta.spaceKey;
1070
- invariant7(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
1071
- F: __dxlog_file7,
1072
- L: 117,
1073
- S: void 0,
1074
- A: [
1075
- "!meta.rootDocumentId.startsWith('automerge:')",
1076
- ""
1077
- ]
1078
- });
903
+ invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 109, S: void 0, A: ["!meta.rootDocumentId.startsWith('automerge:')", ""] });
1079
904
  rootUrl = `automerge:${meta.rootDocumentId}`;
1080
905
  }
1081
906
  return {
@@ -1092,36 +917,40 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
1092
917
  };
1093
918
 
1094
919
  // src/logger.ts
1095
- import { LogLevel, log as log4, shouldLog } from "@dxos/log";
920
+ import { LogLevel, log as log5, shouldLog } from "@dxos/log";
1096
921
  var setupFunctionsLogger = () => {
1097
- log4.runtimeConfig.processors.length = 0;
1098
- log4.runtimeConfig.processors.push(functionLogProcessor);
922
+ log5.runtimeConfig.processors.length = 0;
923
+ log5.runtimeConfig.processors.push(functionLogProcessor);
1099
924
  };
1100
925
  var functionLogProcessor = (config, entry) => {
1101
926
  if (!shouldLog(entry, config.filters)) {
1102
927
  return;
1103
928
  }
929
+ const context = entry.computedContext;
930
+ const error = entry.computedError;
931
+ const extras = [
932
+ Object.keys(context).length > 0 ? context : void 0,
933
+ error
934
+ ].filter((value) => value !== void 0);
1104
935
  switch (entry.level) {
1105
936
  case LogLevel.DEBUG:
1106
- console.debug(entry.message, entry.context);
1107
- break;
1108
937
  case LogLevel.TRACE:
1109
- console.debug(entry.message, entry.context);
938
+ console.debug(entry.message, ...extras);
1110
939
  break;
1111
940
  case LogLevel.VERBOSE:
1112
- console.log(entry.message, entry.context);
941
+ console.log(entry.message, ...extras);
1113
942
  break;
1114
943
  case LogLevel.INFO:
1115
- console.info(entry.message, entry.context);
944
+ console.info(entry.message, ...extras);
1116
945
  break;
1117
946
  case LogLevel.WARN:
1118
- console.warn(entry.message, entry.context);
947
+ console.warn(entry.message, ...extras);
1119
948
  break;
1120
949
  case LogLevel.ERROR:
1121
- console.error(entry.message, entry.context);
950
+ console.error(entry.message, ...extras);
1122
951
  break;
1123
952
  default:
1124
- console.log(entry.message, entry.context);
953
+ console.log(entry.message, ...extras);
1125
954
  break;
1126
955
  }
1127
956
  };
@@ -1131,6 +960,7 @@ export {
1131
960
  FunctionsClient,
1132
961
  ServiceContainer,
1133
962
  createClientFromEnv,
963
+ createFunctionContext,
1134
964
  setupFunctionsLogger,
1135
965
  wrapHandlerForCloudflare
1136
966
  };