spacetimedb 2.3.0 → 2.4.1

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 (61) hide show
  1. package/LICENSE.txt +2 -2
  2. package/dist/index.browser.mjs +64 -4
  3. package/dist/index.browser.mjs.map +1 -1
  4. package/dist/index.cjs +64 -4
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.mjs +64 -4
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/lib/autogen/types.d.ts +675 -2
  9. package/dist/lib/autogen/types.d.ts.map +1 -1
  10. package/dist/lib/schema.d.ts.map +1 -1
  11. package/dist/min/index.browser.mjs +1 -1
  12. package/dist/min/index.browser.mjs.map +1 -1
  13. package/dist/min/sdk/index.browser.mjs +1 -1
  14. package/dist/min/sdk/index.browser.mjs.map +1 -1
  15. package/dist/sdk/decompress.d.ts.map +1 -1
  16. package/dist/sdk/index.browser.mjs +64 -4
  17. package/dist/sdk/index.browser.mjs.map +1 -1
  18. package/dist/sdk/index.cjs +64 -4
  19. package/dist/sdk/index.cjs.map +1 -1
  20. package/dist/sdk/index.mjs +64 -4
  21. package/dist/sdk/index.mjs.map +1 -1
  22. package/dist/server/http.d.ts +1 -2
  23. package/dist/server/http.d.ts.map +1 -1
  24. package/dist/server/http.test-d.d.ts +2 -0
  25. package/dist/server/http.test-d.d.ts.map +1 -0
  26. package/dist/server/http_handlers.d.ts +82 -0
  27. package/dist/server/http_handlers.d.ts.map +1 -0
  28. package/dist/server/http_internal.d.ts +1 -32
  29. package/dist/server/http_internal.d.ts.map +1 -1
  30. package/dist/server/http_shared.d.ts +44 -0
  31. package/dist/server/http_shared.d.ts.map +1 -0
  32. package/dist/server/index.d.ts +2 -0
  33. package/dist/server/index.d.ts.map +1 -1
  34. package/dist/server/index.mjs +628 -136
  35. package/dist/server/index.mjs.map +1 -1
  36. package/dist/server/runtime.d.ts +1 -0
  37. package/dist/server/runtime.d.ts.map +1 -1
  38. package/dist/server/schema.d.ts +19 -4
  39. package/dist/server/schema.d.ts.map +1 -1
  40. package/dist/server/views.d.ts +17 -1
  41. package/dist/server/views.d.ts.map +1 -1
  42. package/package.json +1 -1
  43. package/src/lib/autogen/types.ts +38 -0
  44. package/src/lib/schema.ts +21 -0
  45. package/src/sdk/decompress.ts +19 -4
  46. package/src/sdk/logger.ts +1 -1
  47. package/src/server/http.test-d.ts +80 -0
  48. package/src/server/http.ts +14 -2
  49. package/src/server/http_handlers.ts +413 -0
  50. package/src/server/http_internal.ts +15 -142
  51. package/src/server/http_shared.ts +186 -0
  52. package/src/server/index.ts +11 -0
  53. package/src/server/procedures.ts +8 -30
  54. package/src/server/runtime.ts +137 -1
  55. package/src/server/schema.ts +86 -4
  56. package/src/server/sys.d.ts +7 -0
  57. package/src/server/view.test-d.ts +22 -0
  58. package/src/server/views.ts +127 -0
  59. package/dist/lib/http_types.d.ts +0 -2
  60. package/dist/lib/http_types.d.ts.map +0 -1
  61. package/src/lib/http_types.ts +0 -8
@@ -1,6 +1,7 @@
1
1
  import * as _syscalls2_0 from 'spacetime:sys@2.0';
2
2
  import { moduleHooks } from 'spacetime:sys@2.0';
3
- import { headersToList, Headers } from 'headers-polyfill';
3
+ import { Headers, headersToList } from 'headers-polyfill';
4
+ export { Headers } from 'headers-polyfill';
4
5
  import * as _syscalls2_1 from 'spacetime:sys@2.1';
5
6
 
6
7
  typeof globalThis!=="undefined"&&((globalThis.global=globalThis.global||globalThis),(globalThis.window=globalThis.window||globalThis));
@@ -4606,6 +4607,12 @@ var Lifecycle = t.enum("Lifecycle", {
4606
4607
  OnConnect: t.unit(),
4607
4608
  OnDisconnect: t.unit()
4608
4609
  });
4610
+ var MethodOrAny = t.enum("MethodOrAny", {
4611
+ Any: t.unit(),
4612
+ get Method() {
4613
+ return HttpMethod;
4614
+ }
4615
+ });
4609
4616
  var MiscModuleExport = t.enum("MiscModuleExport", {
4610
4617
  get TypeAlias() {
4611
4618
  return TypeAlias;
@@ -4663,6 +4670,16 @@ var RawConstraintDefV9 = t.object("RawConstraintDefV9", {
4663
4670
  return RawConstraintDataV9;
4664
4671
  }
4665
4672
  });
4673
+ var RawHttpHandlerDefV10 = t.object("RawHttpHandlerDefV10", {
4674
+ sourceName: t.string()
4675
+ });
4676
+ var RawHttpRouteDefV10 = t.object("RawHttpRouteDefV10", {
4677
+ handlerFunction: t.string(),
4678
+ get method() {
4679
+ return MethodOrAny;
4680
+ },
4681
+ path: t.string()
4682
+ });
4666
4683
  var RawIndexAlgorithm = t.enum("RawIndexAlgorithm", {
4667
4684
  BTree: t.array(t.u16()),
4668
4685
  Hash: t.array(t.u16()),
@@ -4759,6 +4776,15 @@ var RawModuleDefV10Section = t.enum("RawModuleDefV10Section", {
4759
4776
  },
4760
4777
  get ExplicitNames() {
4761
4778
  return ExplicitNames;
4779
+ },
4780
+ get HttpHandlers() {
4781
+ return t.array(RawHttpHandlerDefV10);
4782
+ },
4783
+ get HttpRoutes() {
4784
+ return t.array(RawHttpRouteDefV10);
4785
+ },
4786
+ get ViewPrimaryKeys() {
4787
+ return t.array(RawViewPrimaryKeyDefV10);
4762
4788
  }
4763
4789
  });
4764
4790
  var RawModuleDefV8 = t.object("RawModuleDefV8", {
@@ -4996,6 +5022,10 @@ var RawViewDefV9 = t.object("RawViewDefV9", {
4996
5022
  return AlgebraicType2;
4997
5023
  }
4998
5024
  });
5025
+ var RawViewPrimaryKeyDefV10 = t.object("RawViewPrimaryKeyDefV10", {
5026
+ viewSourceName: t.string(),
5027
+ columns: t.array(t.string())
5028
+ });
4999
5029
  var ReducerDef = t.object("ReducerDef", {
5000
5030
  name: t.string(),
5001
5031
  get args() {
@@ -5106,7 +5136,10 @@ var ModuleContext = class {
5106
5136
  schedules: [],
5107
5137
  procedures: [],
5108
5138
  views: [],
5139
+ viewPrimaryKeys: [],
5109
5140
  lifeCycleReducers: [],
5141
+ httpHandlers: [],
5142
+ httpRoutes: [],
5110
5143
  caseConversionPolicy: { tag: "SnakeCase" },
5111
5144
  explicitNames: {
5112
5145
  entries: []
@@ -5127,6 +5160,12 @@ var ModuleContext = class {
5127
5160
  push(module.reducers && { tag: "Reducers", value: module.reducers });
5128
5161
  push(module.procedures && { tag: "Procedures", value: module.procedures });
5129
5162
  push(module.views && { tag: "Views", value: module.views });
5163
+ push(
5164
+ module.viewPrimaryKeys && {
5165
+ tag: "ViewPrimaryKeys",
5166
+ value: module.viewPrimaryKeys
5167
+ }
5168
+ );
5130
5169
  push(module.schedules && { tag: "Schedules", value: module.schedules });
5131
5170
  push(
5132
5171
  module.lifeCycleReducers && {
@@ -5134,6 +5173,18 @@ var ModuleContext = class {
5134
5173
  value: module.lifeCycleReducers
5135
5174
  }
5136
5175
  );
5176
+ push(
5177
+ module.httpHandlers && {
5178
+ tag: "HttpHandlers",
5179
+ value: module.httpHandlers
5180
+ }
5181
+ );
5182
+ push(
5183
+ module.httpRoutes && {
5184
+ tag: "HttpRoutes",
5185
+ value: module.httpRoutes
5186
+ }
5187
+ );
5137
5188
  push(
5138
5189
  module.rowLevelSecurity && {
5139
5190
  tag: "RowLevelSecurity",
@@ -5263,6 +5314,396 @@ function splitName(name) {
5263
5314
  const scope = name.split(".");
5264
5315
  return { sourceName: scope.pop(), scope };
5265
5316
  }
5317
+ var textEncoder = new TextEncoder();
5318
+ var textDecoder = new TextDecoder("utf-8");
5319
+ function deserializeMethod(method) {
5320
+ switch (method.tag) {
5321
+ case "Get":
5322
+ return "GET";
5323
+ case "Head":
5324
+ return "HEAD";
5325
+ case "Post":
5326
+ return "POST";
5327
+ case "Put":
5328
+ return "PUT";
5329
+ case "Delete":
5330
+ return "DELETE";
5331
+ case "Connect":
5332
+ return "CONNECT";
5333
+ case "Options":
5334
+ return "OPTIONS";
5335
+ case "Trace":
5336
+ return "TRACE";
5337
+ case "Patch":
5338
+ return "PATCH";
5339
+ case "Extension":
5340
+ return method.value;
5341
+ }
5342
+ }
5343
+ var methods = /* @__PURE__ */ new Map([
5344
+ ["GET", { tag: "Get" }],
5345
+ ["HEAD", { tag: "Head" }],
5346
+ ["POST", { tag: "Post" }],
5347
+ ["PUT", { tag: "Put" }],
5348
+ ["DELETE", { tag: "Delete" }],
5349
+ ["CONNECT", { tag: "Connect" }],
5350
+ ["OPTIONS", { tag: "Options" }],
5351
+ ["TRACE", { tag: "Trace" }],
5352
+ ["PATCH", { tag: "Patch" }]
5353
+ ]);
5354
+ function serializeMethod(method) {
5355
+ return methods.get(method?.toUpperCase() ?? "GET") ?? {
5356
+ tag: "Extension",
5357
+ value: method
5358
+ };
5359
+ }
5360
+ function serializeHeaders(headers) {
5361
+ return {
5362
+ entries: headersToList(headers).flatMap(([k, v]) => Array.isArray(v) ? v.map((v2) => [k, v2]) : [[k, v]]).map(([name, value]) => ({ name, value: textEncoder.encode(value) }))
5363
+ };
5364
+ }
5365
+ function deserializeHeaders(headers) {
5366
+ return new Headers(
5367
+ headers.entries.map(({ name, value }) => [
5368
+ name,
5369
+ textDecoder.decode(value)
5370
+ ])
5371
+ );
5372
+ }
5373
+ var makeResponse = Symbol("makeResponse");
5374
+ var SyncResponse = class _SyncResponse {
5375
+ #body;
5376
+ #inner;
5377
+ constructor(body, init) {
5378
+ if (body == null) {
5379
+ this.#body = null;
5380
+ } else if (typeof body === "string") {
5381
+ this.#body = body;
5382
+ } else {
5383
+ this.#body = new Uint8Array(body).buffer;
5384
+ }
5385
+ this.#inner = {
5386
+ headers: new Headers(init?.headers),
5387
+ status: init?.status ?? 200,
5388
+ statusText: init?.statusText ?? "",
5389
+ type: "default",
5390
+ url: null,
5391
+ aborted: false,
5392
+ version: init?.version ?? { tag: "Http11" }
5393
+ };
5394
+ }
5395
+ static [makeResponse](body, inner) {
5396
+ const me = new _SyncResponse(body);
5397
+ me.#inner = inner;
5398
+ return me;
5399
+ }
5400
+ get headers() {
5401
+ return this.#inner.headers;
5402
+ }
5403
+ get status() {
5404
+ return this.#inner.status;
5405
+ }
5406
+ get statusText() {
5407
+ return this.#inner.statusText;
5408
+ }
5409
+ get ok() {
5410
+ return 200 <= this.#inner.status && this.#inner.status <= 299;
5411
+ }
5412
+ get url() {
5413
+ return this.#inner.url ?? "";
5414
+ }
5415
+ get type() {
5416
+ return this.#inner.type;
5417
+ }
5418
+ get version() {
5419
+ return this.#inner.version;
5420
+ }
5421
+ arrayBuffer() {
5422
+ return this.bytes().buffer;
5423
+ }
5424
+ bytes() {
5425
+ if (this.#body == null) {
5426
+ return new Uint8Array();
5427
+ } else if (typeof this.#body === "string") {
5428
+ return textEncoder.encode(this.#body);
5429
+ } else {
5430
+ return new Uint8Array(this.#body);
5431
+ }
5432
+ }
5433
+ json() {
5434
+ return JSON.parse(this.text());
5435
+ }
5436
+ text() {
5437
+ if (this.#body == null) {
5438
+ return "";
5439
+ } else if (typeof this.#body === "string") {
5440
+ return this.#body;
5441
+ } else {
5442
+ return textDecoder.decode(this.#body);
5443
+ }
5444
+ }
5445
+ };
5446
+
5447
+ // src/server/http_handlers.ts
5448
+ var httpHandlerFn = Symbol("SpacetimeDB.httpHandlerFn");
5449
+ var ACCEPTABLE_ROUTE_PATH_CHARS_HUMAN_DESCRIPTION = "ASCII lowercase letters, digits and `-_~/`";
5450
+ var makeRequest = Symbol("makeRequest");
5451
+ function coerceRequestBody(body) {
5452
+ if (body == null) {
5453
+ return null;
5454
+ }
5455
+ if (typeof body === "string") {
5456
+ return body;
5457
+ }
5458
+ return new Uint8Array(body);
5459
+ }
5460
+ function requestBodyToBytes(body) {
5461
+ if (body == null) {
5462
+ return new Uint8Array();
5463
+ }
5464
+ if (typeof body === "string") {
5465
+ return textEncoder.encode(body);
5466
+ }
5467
+ return body;
5468
+ }
5469
+ function requestBodyToText(body) {
5470
+ if (body == null) {
5471
+ return "";
5472
+ }
5473
+ if (typeof body === "string") {
5474
+ return body;
5475
+ }
5476
+ return textDecoder.decode(body);
5477
+ }
5478
+ function characterIsAcceptableForRoutePath(c) {
5479
+ return c >= "a" && c <= "z" || c >= "0" && c <= "9" || c === "-" || c === "_" || c === "~" || c === "/";
5480
+ }
5481
+ function assertValidPath(path) {
5482
+ if (path !== "" && !path.startsWith("/")) {
5483
+ throw new TypeError(`Route paths must start with \`/\`: ${path}`);
5484
+ }
5485
+ if (![...path].every(characterIsAcceptableForRoutePath)) {
5486
+ throw new TypeError(
5487
+ `Route paths may contain only ${ACCEPTABLE_ROUTE_PATH_CHARS_HUMAN_DESCRIPTION}: ${path}`
5488
+ );
5489
+ }
5490
+ }
5491
+ function routesOverlap(a, b) {
5492
+ const methodsMatch = (left, right) => {
5493
+ if (left.tag !== right.tag) {
5494
+ return false;
5495
+ }
5496
+ if (left.tag === "Extension" && right.tag === "Extension") {
5497
+ return left.value === right.value;
5498
+ }
5499
+ return true;
5500
+ };
5501
+ return a.path === b.path && (a.method.tag === "Any" || b.method.tag === "Any" || a.method.tag === "Method" && b.method.tag === "Method" && methodsMatch(a.method.value, b.method.value));
5502
+ }
5503
+ function joinPaths(prefix, suffix) {
5504
+ if (prefix === "/") {
5505
+ return suffix;
5506
+ }
5507
+ if (suffix === "/") {
5508
+ return prefix;
5509
+ }
5510
+ let prefixEnd = prefix.length;
5511
+ while (prefixEnd > 0 && prefix[prefixEnd - 1] === "/") {
5512
+ prefixEnd--;
5513
+ }
5514
+ let suffixStart = 0;
5515
+ while (suffixStart < suffix.length && suffix[suffixStart] === "/") {
5516
+ suffixStart++;
5517
+ }
5518
+ const joinedPrefix = prefix.slice(0, prefixEnd);
5519
+ const joinedSuffix = suffix.slice(suffixStart);
5520
+ return `${joinedPrefix}/${joinedSuffix}`;
5521
+ }
5522
+ var Request = class _Request {
5523
+ #body;
5524
+ #inner;
5525
+ constructor(url, init = {}) {
5526
+ this.#body = coerceRequestBody(init.body);
5527
+ this.#inner = {
5528
+ headers: new Headers(init.headers),
5529
+ method: init.method ?? "GET",
5530
+ uri: "" + url,
5531
+ version: init.version ?? { tag: "Http11" }
5532
+ };
5533
+ }
5534
+ static [makeRequest](body, inner) {
5535
+ const me = new _Request(inner.uri);
5536
+ me.#body = coerceRequestBody(body);
5537
+ me.#inner = inner;
5538
+ return me;
5539
+ }
5540
+ get headers() {
5541
+ return this.#inner.headers;
5542
+ }
5543
+ get method() {
5544
+ return this.#inner.method;
5545
+ }
5546
+ get uri() {
5547
+ return this.#inner.uri;
5548
+ }
5549
+ get url() {
5550
+ return this.#inner.uri;
5551
+ }
5552
+ get version() {
5553
+ return this.#inner.version;
5554
+ }
5555
+ arrayBuffer() {
5556
+ return this.bytes().buffer;
5557
+ }
5558
+ bytes() {
5559
+ return requestBodyToBytes(this.#body);
5560
+ }
5561
+ json() {
5562
+ return JSON.parse(this.text());
5563
+ }
5564
+ text() {
5565
+ return requestBodyToText(this.#body);
5566
+ }
5567
+ };
5568
+ var exportedHttpHandlerObjects = /* @__PURE__ */ new WeakSet();
5569
+ var Router = class _Router {
5570
+ #routes;
5571
+ constructor(routes = []) {
5572
+ this.#routes = routes;
5573
+ }
5574
+ get(path, handler) {
5575
+ return this.addRoute(
5576
+ { tag: "Method", value: { tag: "Get" } },
5577
+ path,
5578
+ handler
5579
+ );
5580
+ }
5581
+ head(path, handler) {
5582
+ return this.addRoute(
5583
+ { tag: "Method", value: { tag: "Head" } },
5584
+ path,
5585
+ handler
5586
+ );
5587
+ }
5588
+ options(path, handler) {
5589
+ return this.addRoute(
5590
+ { tag: "Method", value: { tag: "Options" } },
5591
+ path,
5592
+ handler
5593
+ );
5594
+ }
5595
+ put(path, handler) {
5596
+ return this.addRoute(
5597
+ { tag: "Method", value: { tag: "Put" } },
5598
+ path,
5599
+ handler
5600
+ );
5601
+ }
5602
+ delete(path, handler) {
5603
+ return this.addRoute(
5604
+ { tag: "Method", value: { tag: "Delete" } },
5605
+ path,
5606
+ handler
5607
+ );
5608
+ }
5609
+ post(path, handler) {
5610
+ return this.addRoute(
5611
+ { tag: "Method", value: { tag: "Post" } },
5612
+ path,
5613
+ handler
5614
+ );
5615
+ }
5616
+ patch(path, handler) {
5617
+ return this.addRoute(
5618
+ { tag: "Method", value: { tag: "Patch" } },
5619
+ path,
5620
+ handler
5621
+ );
5622
+ }
5623
+ any(path, handler) {
5624
+ return this.addRoute({ tag: "Any" }, path, handler);
5625
+ }
5626
+ nest(path, subRouter) {
5627
+ assertValidPath(path);
5628
+ if (this.#routes.some((route) => route.path.startsWith(path))) {
5629
+ throw new TypeError(
5630
+ `Cannot nest router at \`${path}\`; existing routes overlap with nested path`
5631
+ );
5632
+ }
5633
+ let merged = new _Router(this.#routes);
5634
+ for (const route of subRouter.#routes) {
5635
+ merged = merged.addRoute(
5636
+ route.method,
5637
+ joinPaths(path, route.path),
5638
+ route.handler
5639
+ );
5640
+ }
5641
+ return merged;
5642
+ }
5643
+ merge(otherRouter) {
5644
+ let merged = new _Router(this.#routes);
5645
+ for (const route of otherRouter.#routes) {
5646
+ merged = merged.addRoute(route.method, route.path, route.handler);
5647
+ }
5648
+ return merged;
5649
+ }
5650
+ intoRoutes() {
5651
+ return this.#routes.slice();
5652
+ }
5653
+ addRoute(method, path, handler) {
5654
+ assertValidPath(path);
5655
+ const candidate = { method, path, handler };
5656
+ if (this.#routes.some((route) => routesOverlap(route, candidate))) {
5657
+ throw new TypeError(`Route conflict for \`${path}\``);
5658
+ }
5659
+ return new _Router([...this.#routes, candidate]);
5660
+ }
5661
+ };
5662
+ function makeHttpHandlerExport(ctx, opts, fn) {
5663
+ const handlerExport = {
5664
+ [httpHandlerFn]: fn,
5665
+ [exportContext]: ctx,
5666
+ [registerExport](ctx2, exportName) {
5667
+ if (exportedHttpHandlerObjects.has(handlerExport)) {
5668
+ throw new TypeError(
5669
+ `HTTP handler '${exportName}' was exported more than once`
5670
+ );
5671
+ }
5672
+ exportedHttpHandlerObjects.add(handlerExport);
5673
+ registerHttpHandler(ctx2, exportName, fn, opts);
5674
+ ctx2.httpHandlerExports.set(
5675
+ handlerExport,
5676
+ exportName
5677
+ );
5678
+ }
5679
+ };
5680
+ return handlerExport;
5681
+ }
5682
+ function makeHttpRouterExport(ctx, router) {
5683
+ return {
5684
+ [exportContext]: ctx,
5685
+ [registerExport](ctx2) {
5686
+ ctx2.pendingHttpRoutes.push(...router.intoRoutes());
5687
+ }
5688
+ };
5689
+ }
5690
+ function registerHttpHandler(ctx, exportName, fn, opts) {
5691
+ ctx.defineHttpHandler(exportName);
5692
+ ctx.moduleDef.httpHandlers.push({ sourceName: exportName });
5693
+ if (opts?.name != null) {
5694
+ ctx.moduleDef.explicitNames.entries.push({
5695
+ tag: "Function",
5696
+ value: {
5697
+ sourceName: exportName,
5698
+ canonicalName: opts.name
5699
+ }
5700
+ });
5701
+ }
5702
+ if (!fn.name) {
5703
+ Object.defineProperty(fn, "name", { value: exportName, writable: false });
5704
+ }
5705
+ ctx.httpHandlers.push(fn);
5706
+ }
5266
5707
 
5267
5708
  // src/server/http_internal.ts
5268
5709
  var import_statuses = __toESM(require_statuses());
@@ -5894,6 +6335,7 @@ function makeAnonViewExport(ctx, opts, params, ret, fn) {
5894
6335
  return viewExport;
5895
6336
  }
5896
6337
  function registerView(ctx, opts, exportName, anon, params, ret, fn) {
6338
+ ctx.defineFunction(exportName);
5897
6339
  const paramsBuilder = new RowBuilder(params, toPascalCase(exportName));
5898
6340
  let returnType = ctx.registerTypesRecursively(ret).algebraicType;
5899
6341
  const { typespace } = ctx;
@@ -5908,6 +6350,18 @@ function registerView(ctx, opts, exportName, anon, params, ret, fn) {
5908
6350
  params: paramType,
5909
6351
  returnType
5910
6352
  });
6353
+ const primaryKeyColumns = viewPrimaryKeyColumns(ret);
6354
+ if (primaryKeyColumns.length > 1) {
6355
+ throw new TypeError(
6356
+ `View '${exportName}' can have at most one primaryKey() column on its returned row type; found ${primaryKeyColumns.join(", ")}`
6357
+ );
6358
+ }
6359
+ if (primaryKeyColumns.length === 1) {
6360
+ ctx.moduleDef.viewPrimaryKeys.push({
6361
+ viewSourceName: exportName,
6362
+ columns: primaryKeyColumns
6363
+ });
6364
+ }
5911
6365
  if (opts.name != null) {
5912
6366
  ctx.moduleDef.explicitNames.entries.push({
5913
6367
  tag: "Function",
@@ -5934,6 +6388,24 @@ function registerView(ctx, opts, exportName, anon, params, ret, fn) {
5934
6388
  returnTypeBaseSize: bsatnBaseSize(typespace, returnType)
5935
6389
  });
5936
6390
  }
6391
+ function viewPrimaryKeyColumns(ret) {
6392
+ const row = viewReturnRow(ret);
6393
+ if (row == null) {
6394
+ return [];
6395
+ }
6396
+ return Object.entries(row.row).filter(
6397
+ (entry) => entry[1].columnMetadata.isPrimaryKey === true
6398
+ ).map(([name]) => name);
6399
+ }
6400
+ function viewReturnRow(ret) {
6401
+ if (ret instanceof ArrayBuilder && ret.element instanceof RowBuilder) {
6402
+ return ret.element;
6403
+ }
6404
+ if (ret instanceof OptionBuilder && ret.value instanceof RowBuilder) {
6405
+ return ret.value;
6406
+ }
6407
+ return void 0;
6408
+ }
5937
6409
 
5938
6410
  // src/lib/errors.ts
5939
6411
  var SenderError = class extends Error {
@@ -6303,6 +6775,24 @@ function makeRandom(seed) {
6303
6775
  // src/server/runtime.ts
6304
6776
  var { freeze } = Object;
6305
6777
  var sys = { ..._syscalls2_0, ..._syscalls2_1 };
6778
+ function requestFromWire(request, body) {
6779
+ return Request[makeRequest](body, {
6780
+ headers: deserializeHeaders(request.headers),
6781
+ method: deserializeMethod(request.method),
6782
+ uri: request.uri,
6783
+ version: request.version
6784
+ });
6785
+ }
6786
+ function responseIntoWire(response) {
6787
+ return [
6788
+ {
6789
+ headers: serializeHeaders(response.headers),
6790
+ version: response.version,
6791
+ code: response.status
6792
+ },
6793
+ response.bytes()
6794
+ ];
6795
+ }
6306
6796
  function parseJsonObject(json) {
6307
6797
  let value;
6308
6798
  try {
@@ -6467,6 +6957,31 @@ var ReducerCtxImpl = class ReducerCtx {
6467
6957
  var callUserFunction = function __spacetimedb_end_short_backtrace(fn, ...args) {
6468
6958
  return fn(...args);
6469
6959
  };
6960
+ function runWithTx(makeCtx, body) {
6961
+ const run = () => {
6962
+ const timestamp = sys.procedure_start_mut_tx();
6963
+ try {
6964
+ return body(makeCtx(new Timestamp(timestamp)));
6965
+ } catch (e) {
6966
+ sys.procedure_abort_mut_tx();
6967
+ throw e;
6968
+ }
6969
+ };
6970
+ let res = run();
6971
+ try {
6972
+ sys.procedure_commit_mut_tx();
6973
+ return res;
6974
+ } catch {
6975
+ }
6976
+ console.warn("committing anonymous transaction failed");
6977
+ res = run();
6978
+ try {
6979
+ sys.procedure_commit_mut_tx();
6980
+ return res;
6981
+ } catch (e) {
6982
+ throw new Error("transaction retry failed again", { cause: e });
6983
+ }
6984
+ }
6470
6985
  var makeHooks = (schema2) => new ModuleHooksImpl(schema2);
6471
6986
  var ModuleHooksImpl = class {
6472
6987
  #schema;
@@ -6583,9 +7098,61 @@ var ModuleHooksImpl = class {
6583
7098
  () => this.#dbView
6584
7099
  );
6585
7100
  }
7101
+ __call_http_handler__(id, timestamp, request, body) {
7102
+ const moduleCtx = this.#schema;
7103
+ const handler = moduleCtx.httpHandlers[id];
7104
+ const ctx = new HandlerContextImpl(
7105
+ new Timestamp(timestamp),
7106
+ () => this.#dbView
7107
+ );
7108
+ const requestMetadata = HttpRequest.deserialize(new BinaryReader(request));
7109
+ const response = callUserFunction(
7110
+ handler,
7111
+ ctx,
7112
+ requestFromWire(requestMetadata, body)
7113
+ );
7114
+ const [responseMetadata, responseBody] = responseIntoWire(response);
7115
+ const responseBuf = new BinaryWriter(
7116
+ bsatnBaseSize(moduleCtx.typespace, HttpResponse.algebraicType)
7117
+ );
7118
+ HttpResponse.serialize(responseBuf, responseMetadata);
7119
+ return [responseBuf.getBuffer(), responseBody];
7120
+ }
6586
7121
  };
6587
7122
  var BINARY_WRITER = new BinaryWriter(0);
6588
7123
  var BINARY_READER = new BinaryReader(new Uint8Array());
7124
+ var HandlerContextImpl = class {
7125
+ constructor(timestamp, dbView) {
7126
+ this.timestamp = timestamp;
7127
+ this.#dbView = dbView;
7128
+ }
7129
+ #identity;
7130
+ #uuidCounter;
7131
+ #random;
7132
+ #dbView;
7133
+ http = httpClient;
7134
+ get identity() {
7135
+ return this.#identity ??= new Identity(sys.identity());
7136
+ }
7137
+ get random() {
7138
+ return this.#random ??= makeRandom(this.timestamp);
7139
+ }
7140
+ withTx(body) {
7141
+ return runWithTx(
7142
+ (timestamp) => new ReducerCtxImpl(Identity.zero(), timestamp, null, this.#dbView()),
7143
+ body
7144
+ );
7145
+ }
7146
+ newUuidV4() {
7147
+ const bytes = this.random.fill(new Uint8Array(16));
7148
+ return Uuid.fromRandomBytesV4(bytes);
7149
+ }
7150
+ newUuidV7() {
7151
+ const bytes = this.random.fill(new Uint8Array(4));
7152
+ const counter = this.#uuidCounter ??= { value: 0 };
7153
+ return Uuid.fromCounterV7(counter, this.timestamp, bytes);
7154
+ }
7155
+ };
6589
7156
  function makeTableView(typespace, table2) {
6590
7157
  const table_id = sys.table_id_from_name(table2.sourceName);
6591
7158
  const rowType = typespace.types[table2.productTypeRef];
@@ -7034,109 +7601,10 @@ var IteratorHandle = class _IteratorHandle {
7034
7601
 
7035
7602
  // src/server/http_internal.ts
7036
7603
  var { freeze: freeze2 } = Object;
7037
- var textEncoder = new TextEncoder();
7038
- var textDecoder = new TextDecoder(
7039
- "utf-8"
7040
- /* { fatal: true } */
7041
- );
7042
- function deserializeHeaders(headers) {
7043
- return new Headers(
7044
- headers.entries.map(({ name, value }) => [
7045
- name,
7046
- textDecoder.decode(value)
7047
- ])
7048
- );
7049
- }
7050
- var makeResponse = Symbol("makeResponse");
7051
- var SyncResponse = class _SyncResponse {
7052
- #body;
7053
- #inner;
7054
- constructor(body, init) {
7055
- if (body == null) {
7056
- this.#body = null;
7057
- } else if (typeof body === "string") {
7058
- this.#body = body;
7059
- } else {
7060
- this.#body = new Uint8Array(body).buffer;
7061
- }
7062
- this.#inner = {
7063
- headers: new Headers(init?.headers),
7064
- status: init?.status ?? 200,
7065
- statusText: init?.statusText ?? "",
7066
- type: "default",
7067
- url: null,
7068
- aborted: false
7069
- };
7070
- }
7071
- static [makeResponse](body, inner) {
7072
- const me = new _SyncResponse(body);
7073
- me.#inner = inner;
7074
- return me;
7075
- }
7076
- get headers() {
7077
- return this.#inner.headers;
7078
- }
7079
- get status() {
7080
- return this.#inner.status;
7081
- }
7082
- get statusText() {
7083
- return this.#inner.statusText;
7084
- }
7085
- get ok() {
7086
- return 200 <= this.#inner.status && this.#inner.status <= 299;
7087
- }
7088
- get url() {
7089
- return this.#inner.url ?? "";
7090
- }
7091
- get type() {
7092
- return this.#inner.type;
7093
- }
7094
- arrayBuffer() {
7095
- return this.bytes().buffer;
7096
- }
7097
- bytes() {
7098
- if (this.#body == null) {
7099
- return new Uint8Array();
7100
- } else if (typeof this.#body === "string") {
7101
- return textEncoder.encode(this.#body);
7102
- } else {
7103
- return new Uint8Array(this.#body);
7104
- }
7105
- }
7106
- json() {
7107
- return JSON.parse(this.text());
7108
- }
7109
- text() {
7110
- if (this.#body == null) {
7111
- return "";
7112
- } else if (typeof this.#body === "string") {
7113
- return this.#body;
7114
- } else {
7115
- return textDecoder.decode(this.#body);
7116
- }
7117
- }
7118
- };
7119
7604
  var requestBaseSize = bsatnBaseSize({ types: [] }, HttpRequest.algebraicType);
7120
- var methods = /* @__PURE__ */ new Map([
7121
- ["GET", { tag: "Get" }],
7122
- ["HEAD", { tag: "Head" }],
7123
- ["POST", { tag: "Post" }],
7124
- ["PUT", { tag: "Put" }],
7125
- ["DELETE", { tag: "Delete" }],
7126
- ["CONNECT", { tag: "Connect" }],
7127
- ["OPTIONS", { tag: "Options" }],
7128
- ["TRACE", { tag: "Trace" }],
7129
- ["PATCH", { tag: "Patch" }]
7130
- ]);
7131
7605
  function fetch(url, init = {}) {
7132
- const method = methods.get(init.method?.toUpperCase() ?? "GET") ?? {
7133
- tag: "Extension",
7134
- value: init.method
7135
- };
7136
- const headers = {
7137
- // anys because the typings are wonky - see comment in SyncResponse.constructor
7138
- entries: headersToList(new Headers(init.headers)).flatMap(([k, v]) => Array.isArray(v) ? v.map((v2) => [k, v2]) : [[k, v]]).map(([name, value]) => ({ name, value: textEncoder.encode(value) }))
7139
- };
7606
+ const method = serializeMethod(init.method);
7607
+ const headers = serializeHeaders(new Headers(init.headers));
7140
7608
  const uri = "" + url;
7141
7609
  const request = freeze2({
7142
7610
  method,
@@ -7159,7 +7627,8 @@ function fetch(url, init = {}) {
7159
7627
  status: response.code,
7160
7628
  statusText: (0, import_statuses.default)(response.code),
7161
7629
  headers: deserializeHeaders(response.headers),
7162
- aborted: false
7630
+ aborted: false,
7631
+ version: response.version
7163
7632
  });
7164
7633
  }
7165
7634
  freeze2(fetch);
@@ -7244,35 +7713,15 @@ var ProcedureCtxImpl = class ProcedureCtx {
7244
7713
  return httpClient;
7245
7714
  }
7246
7715
  withTx(body) {
7247
- const run = () => {
7248
- const timestamp = sys.procedure_start_mut_tx();
7249
- try {
7250
- const ctx = new TransactionCtxImpl(
7251
- this.sender,
7252
- new Timestamp(timestamp),
7253
- this.connectionId,
7254
- this.#dbView()
7255
- );
7256
- return body(ctx);
7257
- } catch (e) {
7258
- sys.procedure_abort_mut_tx();
7259
- throw e;
7260
- }
7261
- };
7262
- let res = run();
7263
- try {
7264
- sys.procedure_commit_mut_tx();
7265
- return res;
7266
- } catch {
7267
- }
7268
- console.warn("committing anonymous transaction failed");
7269
- res = run();
7270
- try {
7271
- sys.procedure_commit_mut_tx();
7272
- return res;
7273
- } catch (e) {
7274
- throw new Error("transaction retry failed again", { cause: e });
7275
- }
7716
+ return runWithTx(
7717
+ (timestamp) => new TransactionCtxImpl(
7718
+ this.sender,
7719
+ timestamp,
7720
+ this.connectionId,
7721
+ this.#dbView()
7722
+ ),
7723
+ body
7724
+ );
7276
7725
  }
7277
7726
  newUuidV4() {
7278
7727
  const bytes = this.random.fill(new Uint8Array(16));
@@ -7343,16 +7792,20 @@ function registerReducer(ctx, exportName, params, fn, opts, lifecycle) {
7343
7792
  var SchemaInner = class extends ModuleContext {
7344
7793
  schemaType;
7345
7794
  existingFunctions = /* @__PURE__ */ new Set();
7795
+ existingHttpHandlers = /* @__PURE__ */ new Set();
7346
7796
  reducers = [];
7347
7797
  procedures = [];
7348
7798
  views = [];
7349
7799
  anonViews = [];
7800
+ httpHandlers = [];
7350
7801
  /**
7351
7802
  * Maps ReducerExport objects to the name of the reducer.
7352
7803
  * Used for resolving the reducers of scheduled tables.
7353
7804
  */
7354
7805
  functionExports = /* @__PURE__ */ new Map();
7806
+ httpHandlerExports = /* @__PURE__ */ new Map();
7355
7807
  pendingSchedules = [];
7808
+ pendingHttpRoutes = [];
7356
7809
  constructor(getSchemaType) {
7357
7810
  super();
7358
7811
  this.schemaType = getSchemaType(this);
@@ -7360,11 +7813,19 @@ var SchemaInner = class extends ModuleContext {
7360
7813
  defineFunction(name) {
7361
7814
  if (this.existingFunctions.has(name)) {
7362
7815
  throw new TypeError(
7363
- `There is already a reducer or procedure with the name '${name}'`
7816
+ `There is already a reducer, procedure, or view with the name '${name}'`
7364
7817
  );
7365
7818
  }
7366
7819
  this.existingFunctions.add(name);
7367
7820
  }
7821
+ defineHttpHandler(name) {
7822
+ if (this.existingHttpHandlers.has(name)) {
7823
+ throw new TypeError(
7824
+ `There is already an HTTP handler with the name '${name}'`
7825
+ );
7826
+ }
7827
+ this.existingHttpHandlers.add(name);
7828
+ }
7368
7829
  resolveSchedules() {
7369
7830
  for (const { reducer, scheduleAtCol, tableName } of this.pendingSchedules) {
7370
7831
  const functionName = this.functionExports.get(reducer());
@@ -7380,6 +7841,21 @@ var SchemaInner = class extends ModuleContext {
7380
7841
  });
7381
7842
  }
7382
7843
  }
7844
+ resolveHttpRoutes() {
7845
+ for (const route of this.pendingHttpRoutes) {
7846
+ const handlerFunction = this.httpHandlerExports.get(route.handler);
7847
+ if (handlerFunction === void 0) {
7848
+ throw new TypeError(
7849
+ `HTTP route for path '${route.path}' refers to a handler that was not exported.`
7850
+ );
7851
+ }
7852
+ this.moduleDef.httpRoutes.push({
7853
+ handlerFunction,
7854
+ method: route.method,
7855
+ path: route.path
7856
+ });
7857
+ }
7858
+ }
7383
7859
  };
7384
7860
  var Schema = class {
7385
7861
  #ctx;
@@ -7399,6 +7875,7 @@ var Schema = class {
7399
7875
  moduleExport[registerExport](registeredSchema, name);
7400
7876
  }
7401
7877
  registeredSchema.resolveSchedules();
7878
+ registeredSchema.resolveHttpRoutes();
7402
7879
  return makeHooks(registeredSchema);
7403
7880
  }
7404
7881
  get schemaType() {
@@ -7465,7 +7942,7 @@ var Schema = class {
7465
7942
  }
7466
7943
  return makeReducerExport(this.#ctx, opts, {}, fn, Lifecycle.OnDisconnect);
7467
7944
  }
7468
- view(opts, ret, fn) {
7945
+ view(opts, ret, fn, ..._) {
7469
7946
  return makeViewExport(this.#ctx, opts, {}, ret, fn);
7470
7947
  }
7471
7948
  // TODO: re-enable once parameterized views are supported in SQL
@@ -7492,7 +7969,7 @@ var Schema = class {
7492
7969
  // defineView(name, false, paramsOrRet as Params, retOrFn, maybeFn!);
7493
7970
  // }
7494
7971
  // }
7495
- anonymousView(opts, ret, fn) {
7972
+ anonymousView(opts, ret, fn, ..._) {
7496
7973
  return makeAnonViewExport(this.#ctx, opts, {}, ret, fn);
7497
7974
  }
7498
7975
  procedure(...args) {
@@ -7514,6 +7991,21 @@ var Schema = class {
7514
7991
  }
7515
7992
  return makeProcedureExport(this.#ctx, opts, params, ret, fn);
7516
7993
  }
7994
+ httpHandler(...args) {
7995
+ let opts, fn;
7996
+ switch (args.length) {
7997
+ case 1:
7998
+ [fn] = args;
7999
+ break;
8000
+ case 2:
8001
+ [opts, fn] = args;
8002
+ break;
8003
+ }
8004
+ return makeHttpHandlerExport(this.#ctx, opts, fn);
8005
+ }
8006
+ httpRouter(router) {
8007
+ return makeHttpRouterExport(this.#ctx, router);
8008
+ }
7517
8009
  /**
7518
8010
  * Bundle multiple reducers, procedures, etc into one value to export.
7519
8011
  * The name they will be exported with is their corresponding key in the `exports` argument.
@@ -7678,6 +8170,6 @@ statuses/index.js:
7678
8170
  *)
7679
8171
  */
7680
8172
 
7681
- export { ArrayBuilder, ArrayColumnBuilder, BoolBuilder, BoolColumnBuilder, BooleanExpr, ByteArrayBuilder, ByteArrayColumnBuilder, CaseConversionPolicy, ColumnBuilder, ColumnExpression, ConnectionIdBuilder, ConnectionIdColumnBuilder, F32Builder, F32ColumnBuilder, F64Builder, F64ColumnBuilder, I128Builder, I128ColumnBuilder, I16Builder, I16ColumnBuilder, I256Builder, I256ColumnBuilder, I32Builder, I32ColumnBuilder, I64Builder, I64ColumnBuilder, I8Builder, I8ColumnBuilder, IdentityBuilder, IdentityColumnBuilder, OptionBuilder, OptionColumnBuilder, ProductBuilder, ProductColumnBuilder, Range, RefBuilder, ResultBuilder, ResultColumnBuilder, RowBuilder, ScheduleAtBuilder, ScheduleAtColumnBuilder, SenderError, SimpleSumBuilder, SimpleSumColumnBuilder, SpacetimeHostError, StringBuilder, StringColumnBuilder, SumBuilder, SumColumnBuilder, TimeDurationBuilder, TimeDurationColumnBuilder, TimestampBuilder, TimestampColumnBuilder, TypeBuilder, U128Builder, U128ColumnBuilder, U16Builder, U16ColumnBuilder, U256Builder, U256ColumnBuilder, U32Builder, U32ColumnBuilder, U64Builder, U64ColumnBuilder, U8Builder, U8ColumnBuilder, UuidBuilder, UuidColumnBuilder, and, createTableRefFromDef, errors, evaluateBooleanExpr, getQueryAccessorName, getQueryTableName, getQueryWhereClause, isRowTypedQuery, isTypedQuery, literal, makeQueryBuilder, not, or, schema, t, table, toCamelCase, toComparableValue, toSql };
8173
+ export { ArrayBuilder, ArrayColumnBuilder, BoolBuilder, BoolColumnBuilder, BooleanExpr, ByteArrayBuilder, ByteArrayColumnBuilder, CaseConversionPolicy, ColumnBuilder, ColumnExpression, ConnectionIdBuilder, ConnectionIdColumnBuilder, F32Builder, F32ColumnBuilder, F64Builder, F64ColumnBuilder, I128Builder, I128ColumnBuilder, I16Builder, I16ColumnBuilder, I256Builder, I256ColumnBuilder, I32Builder, I32ColumnBuilder, I64Builder, I64ColumnBuilder, I8Builder, I8ColumnBuilder, IdentityBuilder, IdentityColumnBuilder, OptionBuilder, OptionColumnBuilder, ProductBuilder, ProductColumnBuilder, Range, RefBuilder, Request, ResultBuilder, ResultColumnBuilder, Router, RowBuilder, ScheduleAtBuilder, ScheduleAtColumnBuilder, SenderError, SimpleSumBuilder, SimpleSumColumnBuilder, SpacetimeHostError, StringBuilder, StringColumnBuilder, SumBuilder, SumColumnBuilder, SyncResponse, TimeDurationBuilder, TimeDurationColumnBuilder, TimestampBuilder, TimestampColumnBuilder, TypeBuilder, U128Builder, U128ColumnBuilder, U16Builder, U16ColumnBuilder, U256Builder, U256ColumnBuilder, U32Builder, U32ColumnBuilder, U64Builder, U64ColumnBuilder, U8Builder, U8ColumnBuilder, UuidBuilder, UuidColumnBuilder, and, createTableRefFromDef, errors, evaluateBooleanExpr, getQueryAccessorName, getQueryTableName, getQueryWhereClause, isRowTypedQuery, isTypedQuery, literal, makeQueryBuilder, not, or, schema, t, table, toCamelCase, toComparableValue, toSql };
7682
8174
  //# sourceMappingURL=index.mjs.map
7683
8175
  //# sourceMappingURL=index.mjs.map