@dxos/functions-runtime-cloudflare 0.8.4-main.3eb6e50203 → 0.8.4-main.422d1c7879

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 invariant5 } 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";
@@ -180,10 +180,16 @@ var DataServiceImpl = class {
180
180
  hasError: false
181
181
  };
182
182
  try {
183
- const response = _ts_add_disposable_resource(env, await this._dataService.createDocument(this._executionContext, {
184
- spaceId,
185
- initialValue
186
- }), false);
183
+ invariant(SpaceId.isValid(spaceId), void 0, {
184
+ F: __dxlog_file,
185
+ L: 83,
186
+ S: this,
187
+ A: [
188
+ "SpaceId.isValid(spaceId)",
189
+ ""
190
+ ]
191
+ });
192
+ const response = _ts_add_disposable_resource(env, await this._dataService.createDocument(this._executionContext, spaceId, initialValue), false);
187
193
  return {
188
194
  documentId: response.documentId
189
195
  };
@@ -235,7 +241,7 @@ var DataServiceImpl = class {
235
241
  async updateIndexes() {
236
242
  log.error("updateIndexes is not available in EDGE env.", void 0, {
237
243
  F: __dxlog_file,
238
- L: 133,
244
+ L: 134,
239
245
  S: this,
240
246
  C: (f, a) => f(...a)
241
247
  });
@@ -248,83 +254,9 @@ var DataServiceImpl = class {
248
254
  };
249
255
 
250
256
  // src/internal/query-service-impl.ts
251
- import * as Schema from "effect/Schema";
252
257
  import { Stream as Stream2 } from "@dxos/codec-protobuf/stream";
253
- import { QueryAST } from "@dxos/echo-protocol";
254
258
  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
259
  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
260
  function _ts_add_disposable_resource2(env, value, async) {
329
261
  if (value !== null && value !== void 0) {
330
262
  if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
@@ -390,7 +322,7 @@ function _ts_dispose_resources2(env) {
390
322
  return next();
391
323
  })(env);
392
324
  }
393
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
325
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
394
326
  var QueryServiceImpl = class {
395
327
  _executionContext;
396
328
  _dataService;
@@ -403,23 +335,11 @@ var QueryServiceImpl = class {
403
335
  log2.info("execQuery", {
404
336
  request
405
337
  }, {
406
- F: __dxlog_file3,
407
- L: 34,
338
+ F: __dxlog_file2,
339
+ L: 20,
408
340
  S: this,
409
341
  C: (f, a) => f(...a)
410
342
  });
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
343
  return Stream2.fromPromise((async () => {
424
344
  try {
425
345
  const env = {
@@ -430,37 +350,23 @@ var QueryServiceImpl = class {
430
350
  try {
431
351
  this._queryCount++;
432
352
  log2.info("begin query", {
433
- spaceId
353
+ request
434
354
  }, {
435
- F: __dxlog_file3,
436
- L: 44,
355
+ F: __dxlog_file2,
356
+ L: 26,
437
357
  S: this,
438
358
  C: (f, a) => f(...a)
439
359
  });
440
- const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.queryDocuments(this._executionContext, queryToDataServiceRequest(query)), false);
360
+ const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.execQuery(this._executionContext, request), false);
441
361
  log2.info("query response", {
442
- spaceId,
443
- filter: request.filter,
444
- resultCount: queryResponse.results.length
362
+ resultCount: queryResponse.results?.length
445
363
  }, {
446
- F: __dxlog_file3,
447
- L: 49,
364
+ F: __dxlog_file2,
365
+ L: 28,
448
366
  S: this,
449
367
  C: (f, a) => f(...a)
450
368
  });
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
- };
369
+ return structuredClone(queryResponse);
464
370
  } catch (e) {
465
371
  env.error = e;
466
372
  env.hasError = true;
@@ -471,16 +377,14 @@ var QueryServiceImpl = class {
471
377
  log2.error("query failed", {
472
378
  err: error
473
379
  }, {
474
- F: __dxlog_file3,
475
- L: 66,
380
+ F: __dxlog_file2,
381
+ L: 31,
476
382
  S: this,
477
383
  C: (f, a) => f(...a)
478
384
  });
479
385
  throw new RuntimeServiceError2({
480
386
  message: `Query execution failed (queryCount=${this._queryCount})`,
481
387
  context: {
482
- spaceId,
483
- filter: request.filter,
484
388
  queryCount: this._queryCount
485
389
  },
486
390
  cause: error
@@ -499,22 +403,6 @@ var QueryServiceImpl = class {
499
403
  });
500
404
  }
501
405
  };
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
406
 
519
407
  // src/internal/queue-service-impl.ts
520
408
  import { RuntimeServiceError as RuntimeServiceError3 } from "@dxos/errors";
@@ -679,6 +567,8 @@ var QueueServiceImpl = class {
679
567
  })(error);
680
568
  }
681
569
  }
570
+ async syncQueue(_) {
571
+ }
682
572
  };
683
573
 
684
574
  // src/internal/service-container.ts
@@ -819,17 +709,19 @@ var ServiceContainer = class {
819
709
  subspaceTag,
820
710
  spaceId,
821
711
  queueId,
822
- objects
712
+ objects: objects.map((obj) => JSON.stringify(obj))
823
713
  });
824
714
  }
825
715
  };
826
716
 
827
717
  // src/space-proxy.ts
828
718
  import { Resource } from "@dxos/context";
829
- import { invariant as invariant4 } from "@dxos/invariant";
830
- import { PublicKey as PublicKey2 } from "@dxos/keys";
719
+ import { invariant as invariant2 } from "@dxos/invariant";
720
+ import { PublicKey } from "@dxos/keys";
831
721
 
832
722
  // src/queues-api.ts
723
+ import { log as log3 } from "@dxos/log";
724
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/queues-api.ts";
833
725
  var QueuesAPIImpl = class {
834
726
  _serviceContainer;
835
727
  _spaceId;
@@ -837,8 +729,31 @@ var QueuesAPIImpl = class {
837
729
  this._serviceContainer = _serviceContainer;
838
730
  this._spaceId = _spaceId;
839
731
  }
840
- queryQueue(queue, options) {
841
- return this._serviceContainer.queryQueue(queue);
732
+ async queryQueue(queue, options) {
733
+ const result = await this._serviceContainer.queryQueue(queue);
734
+ const objects = (result.objects ?? []).flatMap((encoded) => {
735
+ try {
736
+ return [
737
+ JSON.parse(encoded)
738
+ ];
739
+ } catch (err) {
740
+ log3.verbose("queue object JSON parse failed; object ignored", {
741
+ encoded,
742
+ error: err
743
+ }, {
744
+ F: __dxlog_file3,
745
+ L: 42,
746
+ S: this,
747
+ C: (f, a) => f(...a)
748
+ });
749
+ return [];
750
+ }
751
+ });
752
+ return {
753
+ objects,
754
+ nextCursor: result.nextCursor ?? null,
755
+ prevCursor: result.prevCursor ?? null
756
+ };
842
757
  }
843
758
  insertIntoQueue(queue, objects) {
844
759
  return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
@@ -861,7 +776,7 @@ var SpaceProxy = class extends Resource {
861
776
  return this._id;
862
777
  }
863
778
  get db() {
864
- invariant4(this._db, void 0, {
779
+ invariant2(this._db, void 0, {
865
780
  F: __dxlog_file4,
866
781
  L: 35,
867
782
  S: this,
@@ -876,7 +791,7 @@ var SpaceProxy = class extends Resource {
876
791
  * @deprecated Use db API.
877
792
  */
878
793
  get crud() {
879
- invariant4(this._db, void 0, {
794
+ invariant2(this._db, void 0, {
880
795
  F: __dxlog_file4,
881
796
  L: 43,
882
797
  S: this,
@@ -897,11 +812,11 @@ var SpaceProxy = class extends Resource {
897
812
  }
898
813
  this._db = this._echoClient.constructDatabase({
899
814
  spaceId: this._id,
900
- spaceKey: PublicKey2.from(meta.spaceKey),
815
+ spaceKey: PublicKey.from(meta.spaceKey),
901
816
  reactiveSchemaQuery: false,
902
817
  owningObject: this
903
818
  });
904
- await this._db.coreDatabase.open({
819
+ await this._db.coreDatabase.open(this._ctx, {
905
820
  rootUrl: meta.rootDocumentId
906
821
  });
907
822
  }
@@ -916,7 +831,7 @@ var FunctionsClient = class extends Resource2 {
916
831
  _spaces = /* @__PURE__ */ new Map();
917
832
  constructor(services) {
918
833
  super();
919
- invariant5(typeof services.dataService !== "undefined", "DataService is required", {
834
+ invariant3(typeof services.dataService !== "undefined", "DataService is required", {
920
835
  F: __dxlog_file5,
921
836
  L: 33,
922
837
  S: this,
@@ -925,7 +840,7 @@ var FunctionsClient = class extends Resource2 {
925
840
  "'DataService is required'"
926
841
  ]
927
842
  });
928
- invariant5(typeof services.queueService !== "undefined", "QueueService is required", {
843
+ invariant3(typeof services.queueService !== "undefined", "QueueService is required", {
929
844
  F: __dxlog_file5,
930
845
  L: 34,
931
846
  S: this,
@@ -983,9 +898,9 @@ var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
983
898
  })({});
984
899
 
985
900
  // src/wrap-handler-for-cloudflare.ts
986
- import { invariant as invariant6 } from "@dxos/invariant";
987
- import { SpaceId as SpaceId4 } from "@dxos/keys";
988
- import { log as log3 } from "@dxos/log";
901
+ import { invariant as invariant4 } from "@dxos/invariant";
902
+ import { SpaceId as SpaceId2 } from "@dxos/keys";
903
+ import { log as log4 } from "@dxos/log";
989
904
  import { EdgeResponse } from "@dxos/protocols";
990
905
  var __dxlog_file6 = "/__w/dxos/dxos/packages/core/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
991
906
  var wrapHandlerForCloudflare = (func) => {
@@ -996,7 +911,7 @@ var wrapHandlerForCloudflare = (func) => {
996
911
  try {
997
912
  const spaceId = new URL(request.url).searchParams.get("spaceId");
998
913
  if (spaceId) {
999
- if (!SpaceId4.isValid(spaceId)) {
914
+ if (!SpaceId2.isValid(spaceId)) {
1000
915
  return new Response("Invalid spaceId", {
1001
916
  status: 400
1002
917
  });
@@ -1009,7 +924,7 @@ var wrapHandlerForCloudflare = (func) => {
1009
924
  });
1010
925
  return EdgeResponse.success(await invokeFunction(func, context, request));
1011
926
  } catch (error) {
1012
- log3.error("error invoking function", {
927
+ log4.error("error invoking function", {
1013
928
  error,
1014
929
  stack: error.stack
1015
930
  }, {
@@ -1050,7 +965,7 @@ var decodeRequest = async (request) => {
1050
965
  }
1051
966
  };
1052
967
  } catch (err) {
1053
- log3.catch(err, void 0, {
968
+ log4.catch(err, void 0, {
1054
969
  F: __dxlog_file6,
1055
970
  L: 79,
1056
971
  S: void 0,
@@ -1088,7 +1003,7 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
1088
1003
  throw new Error(`Space not found: ${contextSpaceId}`);
1089
1004
  }
1090
1005
  spaceKey = meta.spaceKey;
1091
- invariant6(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
1006
+ invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
1092
1007
  F: __dxlog_file6,
1093
1008
  L: 117,
1094
1009
  S: void 0,
@@ -1113,36 +1028,40 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
1113
1028
  };
1114
1029
 
1115
1030
  // src/logger.ts
1116
- import { LogLevel, log as log4, shouldLog } from "@dxos/log";
1031
+ import { LogLevel, log as log5, shouldLog } from "@dxos/log";
1117
1032
  var setupFunctionsLogger = () => {
1118
- log4.runtimeConfig.processors.length = 0;
1119
- log4.runtimeConfig.processors.push(functionLogProcessor);
1033
+ log5.runtimeConfig.processors.length = 0;
1034
+ log5.runtimeConfig.processors.push(functionLogProcessor);
1120
1035
  };
1121
1036
  var functionLogProcessor = (config, entry) => {
1122
1037
  if (!shouldLog(entry, config.filters)) {
1123
1038
  return;
1124
1039
  }
1040
+ const context = entry.computedContext;
1041
+ const error = entry.computedError;
1042
+ const extras = [
1043
+ Object.keys(context).length > 0 ? context : void 0,
1044
+ error
1045
+ ].filter((value) => value !== void 0);
1125
1046
  switch (entry.level) {
1126
1047
  case LogLevel.DEBUG:
1127
- console.debug(entry.message, entry.context);
1128
- break;
1129
1048
  case LogLevel.TRACE:
1130
- console.debug(entry.message, entry.context);
1049
+ console.debug(entry.message, ...extras);
1131
1050
  break;
1132
1051
  case LogLevel.VERBOSE:
1133
- console.log(entry.message, entry.context);
1052
+ console.log(entry.message, ...extras);
1134
1053
  break;
1135
1054
  case LogLevel.INFO:
1136
- console.info(entry.message, entry.context);
1055
+ console.info(entry.message, ...extras);
1137
1056
  break;
1138
1057
  case LogLevel.WARN:
1139
- console.warn(entry.message, entry.context);
1058
+ console.warn(entry.message, ...extras);
1140
1059
  break;
1141
1060
  case LogLevel.ERROR:
1142
- console.error(entry.message, entry.context);
1061
+ console.error(entry.message, ...extras);
1143
1062
  break;
1144
1063
  default:
1145
- console.log(entry.message, entry.context);
1064
+ console.log(entry.message, ...extras);
1146
1065
  break;
1147
1066
  }
1148
1067
  };