@dxos/functions-runtime-cloudflare 0.8.4-main.bc674ce → 0.8.4-main.bcb3aa67d6

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.
@@ -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";
@@ -248,83 +248,9 @@ var DataServiceImpl = class {
248
248
  };
249
249
 
250
250
  // src/internal/query-service-impl.ts
251
- import * as Schema from "effect/Schema";
252
251
  import { Stream as Stream2 } from "@dxos/codec-protobuf/stream";
253
- import { QueryAST } from "@dxos/echo-protocol";
254
252
  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
253
  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
328
254
  function _ts_add_disposable_resource2(env, value, async) {
329
255
  if (value !== null && value !== void 0) {
330
256
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -390,7 +316,7 @@ function _ts_dispose_resources2(env) {
390
316
  return next();
391
317
  })(env);
392
318
  }
393
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
319
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
394
320
  var QueryServiceImpl = class {
395
321
  _executionContext;
396
322
  _dataService;
@@ -403,23 +329,11 @@ var QueryServiceImpl = class {
403
329
  log2.info("execQuery", {
404
330
  request
405
331
  }, {
406
- F: __dxlog_file3,
407
- L: 34,
332
+ F: __dxlog_file2,
333
+ L: 20,
408
334
  S: this,
409
335
  C: (f, a) => f(...a)
410
336
  });
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];
423
337
  return Stream2.fromPromise((async () => {
424
338
  try {
425
339
  const env = {
@@ -430,37 +344,23 @@ var QueryServiceImpl = class {
430
344
  try {
431
345
  this._queryCount++;
432
346
  log2.info("begin query", {
433
- spaceId
347
+ request
434
348
  }, {
435
- F: __dxlog_file3,
436
- L: 44,
349
+ F: __dxlog_file2,
350
+ L: 26,
437
351
  S: this,
438
352
  C: (f, a) => f(...a)
439
353
  });
440
- const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.queryDocuments(this._executionContext, queryToDataServiceRequest(query)), false);
354
+ const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.execQuery(this._executionContext, request), false);
441
355
  log2.info("query response", {
442
- spaceId,
443
- filter: request.filter,
444
- resultCount: queryResponse.results.length
356
+ resultCount: queryResponse.results?.length
445
357
  }, {
446
- F: __dxlog_file3,
447
- L: 49,
358
+ F: __dxlog_file2,
359
+ L: 28,
448
360
  S: this,
449
361
  C: (f, a) => f(...a)
450
362
  });
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
- };
363
+ return structuredClone(queryResponse);
464
364
  } catch (e) {
465
365
  env.error = e;
466
366
  env.hasError = true;
@@ -471,16 +371,14 @@ var QueryServiceImpl = class {
471
371
  log2.error("query failed", {
472
372
  err: error
473
373
  }, {
474
- F: __dxlog_file3,
475
- L: 66,
374
+ F: __dxlog_file2,
375
+ L: 31,
476
376
  S: this,
477
377
  C: (f, a) => f(...a)
478
378
  });
479
379
  throw new RuntimeServiceError2({
480
380
  message: `Query execution failed (queryCount=${this._queryCount})`,
481
381
  context: {
482
- spaceId,
483
- filter: request.filter,
484
382
  queryCount: this._queryCount
485
383
  },
486
384
  cause: error
@@ -499,26 +397,9 @@ var QueryServiceImpl = class {
499
397
  });
500
398
  }
501
399
  };
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
400
 
519
401
  // 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";
402
+ import { RuntimeServiceError as RuntimeServiceError3 } from "@dxos/errors";
522
403
  function _ts_add_disposable_resource3(env, value, async) {
523
404
  if (value !== null && value !== void 0) {
524
405
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -584,7 +465,6 @@ function _ts_dispose_resources3(env) {
584
465
  return next();
585
466
  })(env);
586
467
  }
587
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/queue-service-impl.ts";
588
468
  var QueueServiceImpl = class {
589
469
  _ctx;
590
470
  _queueService;
@@ -593,19 +473,6 @@ var QueueServiceImpl = class {
593
473
  this._queueService = _queueService;
594
474
  }
595
475
  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: 26,
603
- S: this,
604
- A: [
605
- "request.query.queuesNamespace",
606
- ""
607
- ]
608
- });
609
476
  try {
610
477
  const env = {
611
478
  stack: [],
@@ -613,10 +480,8 @@ var QueueServiceImpl = class {
613
480
  hasError: false
614
481
  };
615
482
  try {
616
- const result = _ts_add_disposable_resource3(env, await this._queueService.query(this._ctx, `dxn:queue:${request.query.queuesNamespace}:${spaceId}:${queueId}`, filter), false);
483
+ const result = _ts_add_disposable_resource3(env, await this._queueService.queryQueue(this._ctx, request), false);
617
484
  return {
618
- // Copy returned object to avoid hanging RPC stub
619
- // See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
620
485
  objects: structuredClone(result.objects),
621
486
  nextCursor: result.nextCursor,
622
487
  prevCursor: result.prevCursor
@@ -628,22 +493,35 @@ var QueueServiceImpl = class {
628
493
  _ts_dispose_resources3(env);
629
494
  }
630
495
  } catch (error) {
496
+ const { query } = request;
631
497
  throw RuntimeServiceError3.wrap({
632
498
  message: "Queue query failed.",
633
499
  context: {
634
- subspaceTag: request.query.queuesNamespace,
635
- spaceId,
636
- queueId
500
+ subspaceTag: query?.queuesNamespace,
501
+ spaceId: query?.spaceId,
502
+ queueId: query?.queueIds?.[0]
637
503
  },
638
504
  ifTypeDiffers: true
639
505
  })(error);
640
506
  }
641
507
  }
642
508
  async insertIntoQueue(request) {
643
- const { subspaceTag, spaceId, queueId, objects } = request;
644
509
  try {
645
- await this._queueService.append(this._ctx, `dxn:queue:${subspaceTag}:${spaceId}:${queueId}`, objects ?? []);
510
+ const env = {
511
+ stack: [],
512
+ error: void 0,
513
+ hasError: false
514
+ };
515
+ try {
516
+ const _ = _ts_add_disposable_resource3(env, await this._queueService.insertIntoQueue(this._ctx, request), false);
517
+ } catch (e) {
518
+ env.error = e;
519
+ env.hasError = true;
520
+ } finally {
521
+ _ts_dispose_resources3(env);
522
+ }
646
523
  } catch (error) {
524
+ const { subspaceTag, spaceId, queueId } = request;
647
525
  throw RuntimeServiceError3.wrap({
648
526
  message: "Queue append failed.",
649
527
  context: {
@@ -655,16 +533,35 @@ var QueueServiceImpl = class {
655
533
  })(error);
656
534
  }
657
535
  }
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
536
+ async deleteFromQueue(request) {
537
+ try {
538
+ const env = {
539
+ stack: [],
540
+ error: void 0,
541
+ hasError: false
542
+ };
543
+ try {
544
+ const _ = _ts_add_disposable_resource3(env, await this._queueService.deleteFromQueue(this._ctx, request), false);
545
+ } catch (e) {
546
+ env.error = e;
547
+ env.hasError = true;
548
+ } finally {
549
+ _ts_dispose_resources3(env);
666
550
  }
667
- });
551
+ } catch (error) {
552
+ const { subspaceTag, spaceId, queueId } = request;
553
+ throw RuntimeServiceError3.wrap({
554
+ message: "Queue delete failed.",
555
+ context: {
556
+ subspaceTag,
557
+ spaceId,
558
+ queueId
559
+ },
560
+ ifTypeDiffers: true
561
+ })(error);
562
+ }
563
+ }
564
+ async syncQueue(_) {
668
565
  }
669
566
  };
670
567
 
@@ -776,37 +673,45 @@ var ServiceContainer = class {
776
673
  };
777
674
  }
778
675
  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);
676
+ const parts = queue.asQueueDXN();
677
+ if (!parts) {
678
+ throw new Error("Invalid queue DXN");
799
679
  }
680
+ const { subspaceTag, spaceId, queueId } = parts;
681
+ const result = await this._queueService.queryQueue(this._executionContext, {
682
+ query: {
683
+ spaceId,
684
+ queuesNamespace: subspaceTag,
685
+ queueIds: [
686
+ queueId
687
+ ]
688
+ }
689
+ });
690
+ return {
691
+ objects: structuredClone(result.objects),
692
+ nextCursor: result.nextCursor ?? null,
693
+ prevCursor: result.prevCursor ?? null
694
+ };
800
695
  }
801
696
  async insertIntoQueue(queue, objects) {
802
- await this._queueService.append({}, queue.toString(), objects);
697
+ const parts = queue.asQueueDXN();
698
+ if (!parts) {
699
+ throw new Error("Invalid queue DXN");
700
+ }
701
+ const { subspaceTag, spaceId, queueId } = parts;
702
+ await this._queueService.insertIntoQueue(this._executionContext, {
703
+ subspaceTag,
704
+ spaceId,
705
+ queueId,
706
+ objects
707
+ });
803
708
  }
804
709
  };
805
710
 
806
711
  // src/space-proxy.ts
807
712
  import { Resource } from "@dxos/context";
808
- import { invariant as invariant5 } from "@dxos/invariant";
809
- import { PublicKey as PublicKey2 } from "@dxos/keys";
713
+ import { invariant as invariant2 } from "@dxos/invariant";
714
+ import { PublicKey } from "@dxos/keys";
810
715
 
811
716
  // src/queues-api.ts
812
717
  var QueuesAPIImpl = class {
@@ -825,7 +730,7 @@ var QueuesAPIImpl = class {
825
730
  };
826
731
 
827
732
  // src/space-proxy.ts
828
- var __dxlog_file5 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/space-proxy.ts";
733
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/space-proxy.ts";
829
734
  var SpaceProxy = class extends Resource {
830
735
  _serviceContainer;
831
736
  _echoClient;
@@ -840,8 +745,8 @@ var SpaceProxy = class extends Resource {
840
745
  return this._id;
841
746
  }
842
747
  get db() {
843
- invariant5(this._db, void 0, {
844
- F: __dxlog_file5,
748
+ invariant2(this._db, void 0, {
749
+ F: __dxlog_file3,
845
750
  L: 35,
846
751
  S: this,
847
752
  A: [
@@ -855,8 +760,8 @@ var SpaceProxy = class extends Resource {
855
760
  * @deprecated Use db API.
856
761
  */
857
762
  get crud() {
858
- invariant5(this._db, void 0, {
859
- F: __dxlog_file5,
763
+ invariant2(this._db, void 0, {
764
+ F: __dxlog_file3,
860
765
  L: 43,
861
766
  S: this,
862
767
  A: [
@@ -876,18 +781,18 @@ var SpaceProxy = class extends Resource {
876
781
  }
877
782
  this._db = this._echoClient.constructDatabase({
878
783
  spaceId: this._id,
879
- spaceKey: PublicKey2.from(meta.spaceKey),
784
+ spaceKey: PublicKey.from(meta.spaceKey),
880
785
  reactiveSchemaQuery: false,
881
786
  owningObject: this
882
787
  });
883
- await this._db.coreDatabase.open({
788
+ await this._db.coreDatabase.open(this._ctx, {
884
789
  rootUrl: meta.rootDocumentId
885
790
  });
886
791
  }
887
792
  };
888
793
 
889
794
  // src/functions-client.ts
890
- var __dxlog_file6 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/functions-client.ts";
795
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/functions-client.ts";
891
796
  var FunctionsClient = class extends Resource2 {
892
797
  _serviceContainer;
893
798
  _echoClient;
@@ -895,8 +800,8 @@ var FunctionsClient = class extends Resource2 {
895
800
  _spaces = /* @__PURE__ */ new Map();
896
801
  constructor(services) {
897
802
  super();
898
- invariant6(typeof services.dataService !== "undefined", "DataService is required", {
899
- F: __dxlog_file6,
803
+ invariant3(typeof services.dataService !== "undefined", "DataService is required", {
804
+ F: __dxlog_file4,
900
805
  L: 33,
901
806
  S: this,
902
807
  A: [
@@ -904,8 +809,8 @@ var FunctionsClient = class extends Resource2 {
904
809
  "'DataService is required'"
905
810
  ]
906
811
  });
907
- invariant6(typeof services.queueService !== "undefined", "QueueService is required", {
908
- F: __dxlog_file6,
812
+ invariant3(typeof services.queueService !== "undefined", "QueueService is required", {
813
+ F: __dxlog_file4,
909
814
  L: 34,
910
815
  S: this,
911
816
  A: [
@@ -962,11 +867,11 @@ var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
962
867
  })({});
963
868
 
964
869
  // src/wrap-handler-for-cloudflare.ts
965
- import { invariant as invariant7 } from "@dxos/invariant";
966
- import { SpaceId as SpaceId4 } from "@dxos/keys";
870
+ import { invariant as invariant4 } from "@dxos/invariant";
871
+ import { SpaceId as SpaceId2 } from "@dxos/keys";
967
872
  import { log as log3 } from "@dxos/log";
968
873
  import { EdgeResponse } from "@dxos/protocols";
969
- var __dxlog_file7 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
874
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
970
875
  var wrapHandlerForCloudflare = (func) => {
971
876
  return async (request, env) => {
972
877
  if (request.headers.get(FUNCTION_ROUTE_HEADER) === FunctionRouteValue.Meta) {
@@ -975,7 +880,7 @@ var wrapHandlerForCloudflare = (func) => {
975
880
  try {
976
881
  const spaceId = new URL(request.url).searchParams.get("spaceId");
977
882
  if (spaceId) {
978
- if (!SpaceId4.isValid(spaceId)) {
883
+ if (!SpaceId2.isValid(spaceId)) {
979
884
  return new Response("Invalid spaceId", {
980
885
  status: 400
981
886
  });
@@ -992,7 +897,7 @@ var wrapHandlerForCloudflare = (func) => {
992
897
  error,
993
898
  stack: error.stack
994
899
  }, {
995
- F: __dxlog_file7,
900
+ F: __dxlog_file5,
996
901
  L: 44,
997
902
  S: void 0,
998
903
  C: (f, a) => f(...a)
@@ -1030,7 +935,7 @@ var decodeRequest = async (request) => {
1030
935
  };
1031
936
  } catch (err) {
1032
937
  log3.catch(err, void 0, {
1033
- F: __dxlog_file7,
938
+ F: __dxlog_file5,
1034
939
  L: 79,
1035
940
  S: void 0,
1036
941
  C: (f, a) => f(...a)
@@ -1067,8 +972,8 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
1067
972
  throw new Error(`Space not found: ${contextSpaceId}`);
1068
973
  }
1069
974
  spaceKey = meta.spaceKey;
1070
- invariant7(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
1071
- F: __dxlog_file7,
975
+ invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
976
+ F: __dxlog_file5,
1072
977
  L: 117,
1073
978
  S: void 0,
1074
979
  A: [