@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.
@@ -3,7 +3,7 @@ import { createRequire } from 'node:module';const require = createRequire(import
3
3
  // src/functions-client.ts
4
4
  import { Resource as Resource2 } from "@dxos/context";
5
5
  import { EchoClient } from "@dxos/echo-db";
6
- import { invariant as invariant6 } from "@dxos/invariant";
6
+ import { invariant as invariant3 } from "@dxos/invariant";
7
7
 
8
8
  // src/internal/data-service-impl.ts
9
9
  import { Stream } from "@dxos/codec-protobuf/stream";
@@ -250,83 +250,9 @@ var DataServiceImpl = class {
250
250
  };
251
251
 
252
252
  // src/internal/query-service-impl.ts
253
- import * as Schema from "effect/Schema";
254
253
  import { Stream as Stream2 } from "@dxos/codec-protobuf/stream";
255
- import { QueryAST } from "@dxos/echo-protocol";
256
254
  import { NotImplementedError as NotImplementedError2, RuntimeServiceError as RuntimeServiceError2 } from "@dxos/errors";
257
- import { invariant as invariant3 } from "@dxos/invariant";
258
- import { PublicKey } from "@dxos/keys";
259
- import { SpaceId as SpaceId3 } from "@dxos/keys";
260
255
  import { log as log2 } from "@dxos/log";
261
-
262
- // src/internal/adapter.ts
263
- import { failUndefined } from "@dxos/debug";
264
- import { invariant as invariant2 } from "@dxos/invariant";
265
- import { SpaceId as SpaceId2 } from "@dxos/keys";
266
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/adapter.ts";
267
- var queryToDataServiceRequest = (query) => {
268
- const { filter, options } = isSimpleSelectionQuery(query) ?? failUndefined();
269
- invariant2(options?.spaceIds?.length === 1, "Only one space is supported", {
270
- F: __dxlog_file2,
271
- L: 13,
272
- S: void 0,
273
- A: [
274
- "options?.spaceIds?.length === 1",
275
- "'Only one space is supported'"
276
- ]
277
- });
278
- invariant2(filter.type === "object", "Only object filters are supported", {
279
- F: __dxlog_file2,
280
- L: 14,
281
- S: void 0,
282
- A: [
283
- "filter.type === 'object'",
284
- "'Only object filters are supported'"
285
- ]
286
- });
287
- const spaceId = options.spaceIds[0];
288
- invariant2(SpaceId2.isValid(spaceId), void 0, {
289
- F: __dxlog_file2,
290
- L: 17,
291
- S: void 0,
292
- A: [
293
- "SpaceId.isValid(spaceId)",
294
- ""
295
- ]
296
- });
297
- return {
298
- spaceId,
299
- type: filter.typename ?? void 0,
300
- objectIds: [
301
- ...filter.id ?? []
302
- ]
303
- };
304
- };
305
- var isSimpleSelectionQuery = (query) => {
306
- switch (query.type) {
307
- case "options": {
308
- const maybeFilter = isSimpleSelectionQuery(query.query);
309
- if (!maybeFilter) {
310
- return null;
311
- }
312
- return {
313
- filter: maybeFilter.filter,
314
- options: query.options
315
- };
316
- }
317
- case "select": {
318
- return {
319
- filter: query.filter,
320
- options: void 0
321
- };
322
- }
323
- default: {
324
- return null;
325
- }
326
- }
327
- };
328
-
329
- // src/internal/query-service-impl.ts
330
256
  function _ts_add_disposable_resource2(env, value, async) {
331
257
  if (value !== null && value !== void 0) {
332
258
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -392,7 +318,7 @@ function _ts_dispose_resources2(env) {
392
318
  return next();
393
319
  })(env);
394
320
  }
395
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
321
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
396
322
  var QueryServiceImpl = class {
397
323
  _executionContext;
398
324
  _dataService;
@@ -405,23 +331,11 @@ var QueryServiceImpl = class {
405
331
  log2.info("execQuery", {
406
332
  request
407
333
  }, {
408
- F: __dxlog_file3,
409
- L: 34,
334
+ F: __dxlog_file2,
335
+ L: 20,
410
336
  S: this,
411
337
  C: (f, a) => f(...a)
412
338
  });
413
- const query = QueryAST.Query.pipe(Schema.decodeUnknownSync)(JSON.parse(request.query));
414
- const requestedSpaceIds = getTargetSpacesForQuery(query);
415
- invariant3(requestedSpaceIds.length === 1, "Only one space is supported", {
416
- F: __dxlog_file3,
417
- L: 37,
418
- S: this,
419
- A: [
420
- "requestedSpaceIds.length === 1",
421
- "'Only one space is supported'"
422
- ]
423
- });
424
- const spaceId = requestedSpaceIds[0];
425
339
  return Stream2.fromPromise((async () => {
426
340
  try {
427
341
  const env = {
@@ -432,37 +346,23 @@ var QueryServiceImpl = class {
432
346
  try {
433
347
  this._queryCount++;
434
348
  log2.info("begin query", {
435
- spaceId
349
+ request
436
350
  }, {
437
- F: __dxlog_file3,
438
- L: 44,
351
+ F: __dxlog_file2,
352
+ L: 26,
439
353
  S: this,
440
354
  C: (f, a) => f(...a)
441
355
  });
442
- const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.queryDocuments(this._executionContext, queryToDataServiceRequest(query)), false);
356
+ const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.execQuery(this._executionContext, request), false);
443
357
  log2.info("query response", {
444
- spaceId,
445
- filter: request.filter,
446
- resultCount: queryResponse.results.length
358
+ resultCount: queryResponse.results?.length
447
359
  }, {
448
- F: __dxlog_file3,
449
- L: 49,
360
+ F: __dxlog_file2,
361
+ L: 28,
450
362
  S: this,
451
363
  C: (f, a) => f(...a)
452
364
  });
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
- };
365
+ return structuredClone(queryResponse);
466
366
  } catch (e) {
467
367
  env.error = e;
468
368
  env.hasError = true;
@@ -473,16 +373,14 @@ var QueryServiceImpl = class {
473
373
  log2.error("query failed", {
474
374
  err: error
475
375
  }, {
476
- F: __dxlog_file3,
477
- L: 66,
376
+ F: __dxlog_file2,
377
+ L: 31,
478
378
  S: this,
479
379
  C: (f, a) => f(...a)
480
380
  });
481
381
  throw new RuntimeServiceError2({
482
382
  message: `Query execution failed (queryCount=${this._queryCount})`,
483
383
  context: {
484
- spaceId,
485
- filter: request.filter,
486
384
  queryCount: this._queryCount
487
385
  },
488
386
  cause: error
@@ -501,26 +399,9 @@ var QueryServiceImpl = class {
501
399
  });
502
400
  }
503
401
  };
504
- var getTargetSpacesForQuery = (query) => {
505
- const spaces = /* @__PURE__ */ new Set();
506
- const visitor = (node) => {
507
- if (node.type === "options") {
508
- if (node.options.spaceIds) {
509
- for (const spaceId of node.options.spaceIds) {
510
- spaces.add(SpaceId3.make(spaceId));
511
- }
512
- }
513
- }
514
- };
515
- QueryAST.visit(query, visitor);
516
- return [
517
- ...spaces
518
- ];
519
- };
520
402
 
521
403
  // 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";
404
+ import { RuntimeServiceError as RuntimeServiceError3 } from "@dxos/errors";
524
405
  function _ts_add_disposable_resource3(env, value, async) {
525
406
  if (value !== null && value !== void 0) {
526
407
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -586,7 +467,6 @@ function _ts_dispose_resources3(env) {
586
467
  return next();
587
468
  })(env);
588
469
  }
589
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/queue-service-impl.ts";
590
470
  var QueueServiceImpl = class {
591
471
  _ctx;
592
472
  _queueService;
@@ -595,19 +475,6 @@ var QueueServiceImpl = class {
595
475
  this._queueService = _queueService;
596
476
  }
597
477
  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
478
  try {
612
479
  const env = {
613
480
  stack: [],
@@ -615,10 +482,8 @@ var QueueServiceImpl = class {
615
482
  hasError: false
616
483
  };
617
484
  try {
618
- const result = _ts_add_disposable_resource3(env, await this._queueService.query(this._ctx, `dxn:queue:${request.query.queuesNamespace}:${spaceId}:${queueId}`, filter), false);
485
+ const result = _ts_add_disposable_resource3(env, await this._queueService.queryQueue(this._ctx, request), false);
619
486
  return {
620
- // Copy returned object to avoid hanging RPC stub
621
- // See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
622
487
  objects: structuredClone(result.objects),
623
488
  nextCursor: result.nextCursor,
624
489
  prevCursor: result.prevCursor
@@ -630,22 +495,35 @@ var QueueServiceImpl = class {
630
495
  _ts_dispose_resources3(env);
631
496
  }
632
497
  } catch (error) {
498
+ const { query } = request;
633
499
  throw RuntimeServiceError3.wrap({
634
500
  message: "Queue query failed.",
635
501
  context: {
636
- subspaceTag: request.query.queuesNamespace,
637
- spaceId,
638
- queueId
502
+ subspaceTag: query?.queuesNamespace,
503
+ spaceId: query?.spaceId,
504
+ queueId: query?.queueIds?.[0]
639
505
  },
640
506
  ifTypeDiffers: true
641
507
  })(error);
642
508
  }
643
509
  }
644
510
  async insertIntoQueue(request) {
645
- const { subspaceTag, spaceId, queueId, objects } = request;
646
511
  try {
647
- await this._queueService.append(this._ctx, `dxn:queue:${subspaceTag}:${spaceId}:${queueId}`, objects ?? []);
512
+ const env = {
513
+ stack: [],
514
+ error: void 0,
515
+ hasError: false
516
+ };
517
+ try {
518
+ const _ = _ts_add_disposable_resource3(env, await this._queueService.insertIntoQueue(this._ctx, request), false);
519
+ } catch (e) {
520
+ env.error = e;
521
+ env.hasError = true;
522
+ } finally {
523
+ _ts_dispose_resources3(env);
524
+ }
648
525
  } catch (error) {
526
+ const { subspaceTag, spaceId, queueId } = request;
649
527
  throw RuntimeServiceError3.wrap({
650
528
  message: "Queue append failed.",
651
529
  context: {
@@ -657,16 +535,35 @@ var QueueServiceImpl = class {
657
535
  })(error);
658
536
  }
659
537
  }
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
538
+ async deleteFromQueue(request) {
539
+ try {
540
+ const env = {
541
+ stack: [],
542
+ error: void 0,
543
+ hasError: false
544
+ };
545
+ try {
546
+ const _ = _ts_add_disposable_resource3(env, await this._queueService.deleteFromQueue(this._ctx, request), false);
547
+ } catch (e) {
548
+ env.error = e;
549
+ env.hasError = true;
550
+ } finally {
551
+ _ts_dispose_resources3(env);
668
552
  }
669
- });
553
+ } catch (error) {
554
+ const { subspaceTag, spaceId, queueId } = request;
555
+ throw RuntimeServiceError3.wrap({
556
+ message: "Queue delete failed.",
557
+ context: {
558
+ subspaceTag,
559
+ spaceId,
560
+ queueId
561
+ },
562
+ ifTypeDiffers: true
563
+ })(error);
564
+ }
565
+ }
566
+ async syncQueue(_) {
670
567
  }
671
568
  };
672
569
 
@@ -778,37 +675,45 @@ var ServiceContainer = class {
778
675
  };
779
676
  }
780
677
  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);
678
+ const parts = queue.asQueueDXN();
679
+ if (!parts) {
680
+ throw new Error("Invalid queue DXN");
801
681
  }
682
+ const { subspaceTag, spaceId, queueId } = parts;
683
+ const result = await this._queueService.queryQueue(this._executionContext, {
684
+ query: {
685
+ spaceId,
686
+ queuesNamespace: subspaceTag,
687
+ queueIds: [
688
+ queueId
689
+ ]
690
+ }
691
+ });
692
+ return {
693
+ objects: structuredClone(result.objects),
694
+ nextCursor: result.nextCursor ?? null,
695
+ prevCursor: result.prevCursor ?? null
696
+ };
802
697
  }
803
698
  async insertIntoQueue(queue, objects) {
804
- await this._queueService.append({}, queue.toString(), objects);
699
+ const parts = queue.asQueueDXN();
700
+ if (!parts) {
701
+ throw new Error("Invalid queue DXN");
702
+ }
703
+ const { subspaceTag, spaceId, queueId } = parts;
704
+ await this._queueService.insertIntoQueue(this._executionContext, {
705
+ subspaceTag,
706
+ spaceId,
707
+ queueId,
708
+ objects
709
+ });
805
710
  }
806
711
  };
807
712
 
808
713
  // src/space-proxy.ts
809
714
  import { Resource } from "@dxos/context";
810
- import { invariant as invariant5 } from "@dxos/invariant";
811
- import { PublicKey as PublicKey2 } from "@dxos/keys";
715
+ import { invariant as invariant2 } from "@dxos/invariant";
716
+ import { PublicKey } from "@dxos/keys";
812
717
 
813
718
  // src/queues-api.ts
814
719
  var QueuesAPIImpl = class {
@@ -827,7 +732,7 @@ var QueuesAPIImpl = class {
827
732
  };
828
733
 
829
734
  // src/space-proxy.ts
830
- var __dxlog_file5 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/space-proxy.ts";
735
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/space-proxy.ts";
831
736
  var SpaceProxy = class extends Resource {
832
737
  _serviceContainer;
833
738
  _echoClient;
@@ -842,8 +747,8 @@ var SpaceProxy = class extends Resource {
842
747
  return this._id;
843
748
  }
844
749
  get db() {
845
- invariant5(this._db, void 0, {
846
- F: __dxlog_file5,
750
+ invariant2(this._db, void 0, {
751
+ F: __dxlog_file3,
847
752
  L: 35,
848
753
  S: this,
849
754
  A: [
@@ -857,8 +762,8 @@ var SpaceProxy = class extends Resource {
857
762
  * @deprecated Use db API.
858
763
  */
859
764
  get crud() {
860
- invariant5(this._db, void 0, {
861
- F: __dxlog_file5,
765
+ invariant2(this._db, void 0, {
766
+ F: __dxlog_file3,
862
767
  L: 43,
863
768
  S: this,
864
769
  A: [
@@ -878,18 +783,18 @@ var SpaceProxy = class extends Resource {
878
783
  }
879
784
  this._db = this._echoClient.constructDatabase({
880
785
  spaceId: this._id,
881
- spaceKey: PublicKey2.from(meta.spaceKey),
786
+ spaceKey: PublicKey.from(meta.spaceKey),
882
787
  reactiveSchemaQuery: false,
883
788
  owningObject: this
884
789
  });
885
- await this._db.coreDatabase.open({
790
+ await this._db.coreDatabase.open(this._ctx, {
886
791
  rootUrl: meta.rootDocumentId
887
792
  });
888
793
  }
889
794
  };
890
795
 
891
796
  // src/functions-client.ts
892
- var __dxlog_file6 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/functions-client.ts";
797
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/functions-client.ts";
893
798
  var FunctionsClient = class extends Resource2 {
894
799
  _serviceContainer;
895
800
  _echoClient;
@@ -897,8 +802,8 @@ var FunctionsClient = class extends Resource2 {
897
802
  _spaces = /* @__PURE__ */ new Map();
898
803
  constructor(services) {
899
804
  super();
900
- invariant6(typeof services.dataService !== "undefined", "DataService is required", {
901
- F: __dxlog_file6,
805
+ invariant3(typeof services.dataService !== "undefined", "DataService is required", {
806
+ F: __dxlog_file4,
902
807
  L: 33,
903
808
  S: this,
904
809
  A: [
@@ -906,8 +811,8 @@ var FunctionsClient = class extends Resource2 {
906
811
  "'DataService is required'"
907
812
  ]
908
813
  });
909
- invariant6(typeof services.queueService !== "undefined", "QueueService is required", {
910
- F: __dxlog_file6,
814
+ invariant3(typeof services.queueService !== "undefined", "QueueService is required", {
815
+ F: __dxlog_file4,
911
816
  L: 34,
912
817
  S: this,
913
818
  A: [
@@ -964,11 +869,11 @@ var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
964
869
  })({});
965
870
 
966
871
  // src/wrap-handler-for-cloudflare.ts
967
- import { invariant as invariant7 } from "@dxos/invariant";
968
- import { SpaceId as SpaceId4 } from "@dxos/keys";
872
+ import { invariant as invariant4 } from "@dxos/invariant";
873
+ import { SpaceId as SpaceId2 } from "@dxos/keys";
969
874
  import { log as log3 } from "@dxos/log";
970
875
  import { EdgeResponse } from "@dxos/protocols";
971
- var __dxlog_file7 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
876
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
972
877
  var wrapHandlerForCloudflare = (func) => {
973
878
  return async (request, env) => {
974
879
  if (request.headers.get(FUNCTION_ROUTE_HEADER) === FunctionRouteValue.Meta) {
@@ -977,7 +882,7 @@ var wrapHandlerForCloudflare = (func) => {
977
882
  try {
978
883
  const spaceId = new URL(request.url).searchParams.get("spaceId");
979
884
  if (spaceId) {
980
- if (!SpaceId4.isValid(spaceId)) {
885
+ if (!SpaceId2.isValid(spaceId)) {
981
886
  return new Response("Invalid spaceId", {
982
887
  status: 400
983
888
  });
@@ -994,7 +899,7 @@ var wrapHandlerForCloudflare = (func) => {
994
899
  error,
995
900
  stack: error.stack
996
901
  }, {
997
- F: __dxlog_file7,
902
+ F: __dxlog_file5,
998
903
  L: 44,
999
904
  S: void 0,
1000
905
  C: (f, a) => f(...a)
@@ -1032,7 +937,7 @@ var decodeRequest = async (request) => {
1032
937
  };
1033
938
  } catch (err) {
1034
939
  log3.catch(err, void 0, {
1035
- F: __dxlog_file7,
940
+ F: __dxlog_file5,
1036
941
  L: 79,
1037
942
  S: void 0,
1038
943
  C: (f, a) => f(...a)
@@ -1069,8 +974,8 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
1069
974
  throw new Error(`Space not found: ${contextSpaceId}`);
1070
975
  }
1071
976
  spaceKey = meta.spaceKey;
1072
- invariant7(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
1073
- F: __dxlog_file7,
977
+ invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
978
+ F: __dxlog_file5,
1074
979
  L: 117,
1075
980
  S: void 0,
1076
981
  A: [