spacetimedb 2.3.0 → 2.4.0

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 (59) hide show
  1. package/LICENSE.txt +2 -2
  2. package/dist/index.browser.mjs +50 -4
  3. package/dist/index.browser.mjs.map +1 -1
  4. package/dist/index.cjs +50 -4
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.mjs +50 -4
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/lib/autogen/types.d.ts +674 -18
  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 +50 -4
  17. package/dist/sdk/index.browser.mjs.map +1 -1
  18. package/dist/sdk/index.cjs +50 -4
  19. package/dist/sdk/index.cjs.map +1 -1
  20. package/dist/sdk/index.mjs +50 -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 +582 -134
  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 +16 -1
  39. package/dist/server/schema.d.ts.map +1 -1
  40. package/dist/server/views.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/src/lib/autogen/types.ts +29 -0
  43. package/src/lib/schema.ts +14 -0
  44. package/src/sdk/decompress.ts +19 -4
  45. package/src/sdk/logger.ts +1 -1
  46. package/src/server/http.test-d.ts +80 -0
  47. package/src/server/http.ts +14 -2
  48. package/src/server/http_handlers.ts +413 -0
  49. package/src/server/http_internal.ts +15 -142
  50. package/src/server/http_shared.ts +186 -0
  51. package/src/server/index.ts +11 -0
  52. package/src/server/procedures.ts +8 -30
  53. package/src/server/runtime.ts +137 -1
  54. package/src/server/schema.ts +71 -2
  55. package/src/server/sys.d.ts +7 -0
  56. package/src/server/views.ts +1 -0
  57. package/dist/lib/http_types.d.ts +0 -2
  58. package/dist/lib/http_types.d.ts.map +0 -1
  59. package/src/lib/http_types.ts +0 -8
@@ -0,0 +1,413 @@
1
+ import type { Identity } from '../lib/identity';
2
+ import type {
3
+ HttpMethod,
4
+ HttpVersion,
5
+ MethodOrAny,
6
+ } from '../lib/autogen/types';
7
+ import type { UntypedSchemaDef } from '../lib/schema';
8
+ import type { Timestamp } from '../lib/timestamp';
9
+ import type { Uuid } from '../lib/uuid';
10
+ import type { TransactionCtx } from './procedures';
11
+ import type { HttpClient } from './http_internal';
12
+ import type { Random } from './rng';
13
+ import {
14
+ exportContext,
15
+ registerExport,
16
+ type ModuleExport,
17
+ type SchemaInner,
18
+ } from './schema';
19
+ import {
20
+ Headers,
21
+ makeResponse,
22
+ SyncResponse,
23
+ textDecoder,
24
+ textEncoder,
25
+ type BodyInit,
26
+ type HeadersInit,
27
+ type ResponseInit,
28
+ } from './http_shared';
29
+
30
+ export { Headers };
31
+ export { SyncResponse };
32
+ export type { BodyInit, HeadersInit, ResponseInit };
33
+ export { makeResponse };
34
+ export const httpHandlerFn = Symbol('SpacetimeDB.httpHandlerFn');
35
+
36
+ export interface RequestInit {
37
+ body?: BodyInit | null;
38
+ headers?: HeadersInit;
39
+ method?: string;
40
+ version?: HttpVersion;
41
+ }
42
+
43
+ type RequestInner = {
44
+ headers: Headers;
45
+ method: string;
46
+ uri: string;
47
+ version: HttpVersion;
48
+ };
49
+
50
+ type RouteSpec = {
51
+ handler: HttpHandlerExport<any>;
52
+ method: MethodOrAny;
53
+ path: string;
54
+ };
55
+
56
+ const ACCEPTABLE_ROUTE_PATH_CHARS_HUMAN_DESCRIPTION =
57
+ 'ASCII lowercase letters, digits and `-_~/`';
58
+
59
+ export const makeRequest = Symbol('makeRequest');
60
+
61
+ function coerceRequestBody(body?: BodyInit | null): string | Uint8Array | null {
62
+ if (body == null) {
63
+ return null;
64
+ }
65
+ if (typeof body === 'string') {
66
+ return body;
67
+ }
68
+ return new Uint8Array(body as any);
69
+ }
70
+
71
+ function requestBodyToBytes(body: string | Uint8Array | null): Uint8Array {
72
+ if (body == null) {
73
+ return new Uint8Array();
74
+ }
75
+ if (typeof body === 'string') {
76
+ return textEncoder.encode(body);
77
+ }
78
+ return body;
79
+ }
80
+
81
+ function requestBodyToText(body: string | Uint8Array | null): string {
82
+ if (body == null) {
83
+ return '';
84
+ }
85
+ if (typeof body === 'string') {
86
+ return body;
87
+ }
88
+ return textDecoder.decode(body);
89
+ }
90
+
91
+ function characterIsAcceptableForRoutePath(c: string) {
92
+ return (
93
+ (c >= 'a' && c <= 'z') ||
94
+ (c >= '0' && c <= '9') ||
95
+ c === '-' ||
96
+ c === '_' ||
97
+ c === '~' ||
98
+ c === '/'
99
+ );
100
+ }
101
+
102
+ function assertValidPath(path: string) {
103
+ if (path !== '' && !path.startsWith('/')) {
104
+ throw new TypeError(`Route paths must start with \`/\`: ${path}`);
105
+ }
106
+ if (![...path].every(characterIsAcceptableForRoutePath)) {
107
+ throw new TypeError(
108
+ `Route paths may contain only ${ACCEPTABLE_ROUTE_PATH_CHARS_HUMAN_DESCRIPTION}: ${path}`
109
+ );
110
+ }
111
+ }
112
+
113
+ function routesOverlap(a: RouteSpec, b: RouteSpec) {
114
+ const methodsMatch = (left: HttpMethod, right: HttpMethod) => {
115
+ if (left.tag !== right.tag) {
116
+ return false;
117
+ }
118
+ if (left.tag === 'Extension' && right.tag === 'Extension') {
119
+ return left.value === right.value;
120
+ }
121
+ return true;
122
+ };
123
+
124
+ return (
125
+ a.path === b.path &&
126
+ (a.method.tag === 'Any' ||
127
+ b.method.tag === 'Any' ||
128
+ (a.method.tag === 'Method' &&
129
+ b.method.tag === 'Method' &&
130
+ methodsMatch(a.method.value, b.method.value)))
131
+ );
132
+ }
133
+
134
+ function joinPaths(prefix: string, suffix: string) {
135
+ if (prefix === '/') {
136
+ return suffix;
137
+ }
138
+ if (suffix === '/') {
139
+ return prefix;
140
+ }
141
+ let prefixEnd = prefix.length;
142
+ while (prefixEnd > 0 && prefix[prefixEnd - 1] === '/') {
143
+ prefixEnd--;
144
+ }
145
+
146
+ let suffixStart = 0;
147
+ while (suffixStart < suffix.length && suffix[suffixStart] === '/') {
148
+ suffixStart++;
149
+ }
150
+
151
+ const joinedPrefix = prefix.slice(0, prefixEnd);
152
+ const joinedSuffix = suffix.slice(suffixStart);
153
+ return `${joinedPrefix}/${joinedSuffix}`;
154
+ }
155
+
156
+ export class Request {
157
+ #body: string | Uint8Array | null;
158
+ #inner: RequestInner;
159
+
160
+ constructor(url: URL | string, init: RequestInit = {}) {
161
+ this.#body = coerceRequestBody(init.body);
162
+ this.#inner = {
163
+ headers: new Headers(init.headers as any),
164
+ method: init.method ?? 'GET',
165
+ uri: '' + url,
166
+ version: init.version ?? { tag: 'Http11' },
167
+ };
168
+ }
169
+
170
+ static [makeRequest](body: BodyInit | null, inner: RequestInner) {
171
+ const me = new Request(inner.uri);
172
+ me.#body = coerceRequestBody(body);
173
+ me.#inner = inner;
174
+ return me;
175
+ }
176
+
177
+ get headers(): Headers {
178
+ return this.#inner.headers;
179
+ }
180
+
181
+ get method(): string {
182
+ return this.#inner.method;
183
+ }
184
+
185
+ get uri(): string {
186
+ return this.#inner.uri;
187
+ }
188
+
189
+ get url(): string {
190
+ return this.#inner.uri;
191
+ }
192
+
193
+ get version(): HttpVersion {
194
+ return this.#inner.version;
195
+ }
196
+
197
+ arrayBuffer(): ArrayBuffer {
198
+ return this.bytes().buffer as ArrayBuffer;
199
+ }
200
+
201
+ bytes(): Uint8Array {
202
+ return requestBodyToBytes(this.#body);
203
+ }
204
+
205
+ json(): any {
206
+ return JSON.parse(this.text());
207
+ }
208
+
209
+ text(): string {
210
+ return requestBodyToText(this.#body);
211
+ }
212
+ }
213
+
214
+ export interface HandlerContext<S extends UntypedSchemaDef = UntypedSchemaDef> {
215
+ readonly timestamp: Timestamp;
216
+ readonly http: HttpClient;
217
+ readonly identity: Identity;
218
+ readonly random: Random;
219
+ withTx<T>(body: (ctx: TransactionCtx<S>) => T): T;
220
+ newUuidV4(): Uuid;
221
+ newUuidV7(): Uuid;
222
+ }
223
+
224
+ export type HandlerFn<S extends UntypedSchemaDef = UntypedSchemaDef> = (
225
+ ctx: HandlerContext<S>,
226
+ req: Request
227
+ ) => SyncResponse;
228
+
229
+ export interface HttpHandlerExport<
230
+ S extends UntypedSchemaDef = UntypedSchemaDef,
231
+ > extends ModuleExport {
232
+ [httpHandlerFn]: HandlerFn<S>;
233
+ }
234
+
235
+ const exportedHttpHandlerObjects = new WeakSet<object>();
236
+
237
+ export interface HttpHandlerOpts {
238
+ name: string;
239
+ }
240
+
241
+ export class Router {
242
+ #routes: RouteSpec[];
243
+
244
+ constructor(routes: RouteSpec[] = []) {
245
+ this.#routes = routes;
246
+ }
247
+
248
+ get(path: string, handler: HttpHandlerExport<any>) {
249
+ return this.addRoute(
250
+ { tag: 'Method', value: { tag: 'Get' } },
251
+ path,
252
+ handler
253
+ );
254
+ }
255
+
256
+ head(path: string, handler: HttpHandlerExport<any>) {
257
+ return this.addRoute(
258
+ { tag: 'Method', value: { tag: 'Head' } },
259
+ path,
260
+ handler
261
+ );
262
+ }
263
+
264
+ options(path: string, handler: HttpHandlerExport<any>) {
265
+ return this.addRoute(
266
+ { tag: 'Method', value: { tag: 'Options' } },
267
+ path,
268
+ handler
269
+ );
270
+ }
271
+
272
+ put(path: string, handler: HttpHandlerExport<any>) {
273
+ return this.addRoute(
274
+ { tag: 'Method', value: { tag: 'Put' } },
275
+ path,
276
+ handler
277
+ );
278
+ }
279
+
280
+ delete(path: string, handler: HttpHandlerExport<any>) {
281
+ return this.addRoute(
282
+ { tag: 'Method', value: { tag: 'Delete' } },
283
+ path,
284
+ handler
285
+ );
286
+ }
287
+
288
+ post(path: string, handler: HttpHandlerExport<any>) {
289
+ return this.addRoute(
290
+ { tag: 'Method', value: { tag: 'Post' } },
291
+ path,
292
+ handler
293
+ );
294
+ }
295
+
296
+ patch(path: string, handler: HttpHandlerExport<any>) {
297
+ return this.addRoute(
298
+ { tag: 'Method', value: { tag: 'Patch' } },
299
+ path,
300
+ handler
301
+ );
302
+ }
303
+
304
+ any(path: string, handler: HttpHandlerExport<any>) {
305
+ return this.addRoute({ tag: 'Any' }, path, handler);
306
+ }
307
+
308
+ nest(path: string, subRouter: Router) {
309
+ assertValidPath(path);
310
+ if (this.#routes.some(route => route.path.startsWith(path))) {
311
+ throw new TypeError(
312
+ `Cannot nest router at \`${path}\`; existing routes overlap with nested path`
313
+ );
314
+ }
315
+ let merged = new Router(this.#routes);
316
+ for (const route of subRouter.#routes) {
317
+ merged = merged.addRoute(
318
+ route.method,
319
+ joinPaths(path, route.path),
320
+ route.handler
321
+ );
322
+ }
323
+ return merged;
324
+ }
325
+
326
+ merge(otherRouter: Router) {
327
+ let merged = new Router(this.#routes);
328
+ for (const route of otherRouter.#routes) {
329
+ merged = merged.addRoute(route.method, route.path, route.handler);
330
+ }
331
+ return merged;
332
+ }
333
+
334
+ intoRoutes() {
335
+ return this.#routes.slice();
336
+ }
337
+
338
+ private addRoute(
339
+ method: MethodOrAny,
340
+ path: string,
341
+ handler: HttpHandlerExport<any>
342
+ ) {
343
+ assertValidPath(path);
344
+ const candidate = { method, path, handler };
345
+ if (this.#routes.some(route => routesOverlap(route, candidate))) {
346
+ throw new TypeError(`Route conflict for \`${path}\``);
347
+ }
348
+ return new Router([...this.#routes, candidate]);
349
+ }
350
+ }
351
+
352
+ export function makeHttpHandlerExport<S extends UntypedSchemaDef>(
353
+ ctx: SchemaInner,
354
+ opts: HttpHandlerOpts | undefined,
355
+ fn: HandlerFn<S>
356
+ ): HttpHandlerExport<S> {
357
+ const handlerExport = {
358
+ [httpHandlerFn]: fn,
359
+ [exportContext]: ctx,
360
+ [registerExport](ctx: SchemaInner, exportName: string) {
361
+ if (exportedHttpHandlerObjects.has(handlerExport)) {
362
+ throw new TypeError(
363
+ `HTTP handler '${exportName}' was exported more than once`
364
+ );
365
+ }
366
+ exportedHttpHandlerObjects.add(handlerExport);
367
+ registerHttpHandler(ctx, exportName, fn, opts);
368
+ ctx.httpHandlerExports.set(
369
+ handlerExport as HttpHandlerExport<UntypedSchemaDef>,
370
+ exportName
371
+ );
372
+ },
373
+ };
374
+ return handlerExport as HttpHandlerExport<S>;
375
+ }
376
+
377
+ export function makeHttpRouterExport(
378
+ ctx: SchemaInner,
379
+ router: Router
380
+ ): ModuleExport {
381
+ return {
382
+ [exportContext]: ctx,
383
+ [registerExport](ctx: SchemaInner) {
384
+ ctx.pendingHttpRoutes.push(...router.intoRoutes());
385
+ },
386
+ };
387
+ }
388
+
389
+ function registerHttpHandler<S extends UntypedSchemaDef>(
390
+ ctx: SchemaInner,
391
+ exportName: string,
392
+ fn: HandlerFn<S>,
393
+ opts?: HttpHandlerOpts
394
+ ) {
395
+ ctx.defineHttpHandler(exportName);
396
+ ctx.moduleDef.httpHandlers.push({ sourceName: exportName });
397
+
398
+ if (opts?.name != null) {
399
+ ctx.moduleDef.explicitNames.entries.push({
400
+ tag: 'Function',
401
+ value: {
402
+ sourceName: exportName,
403
+ canonicalName: opts.name,
404
+ },
405
+ });
406
+ }
407
+
408
+ if (!fn.name) {
409
+ Object.defineProperty(fn, 'name', { value: exportName, writable: false });
410
+ }
411
+
412
+ ctx.httpHandlers.push(fn as HandlerFn<UntypedSchemaDef>);
413
+ }
@@ -1,133 +1,25 @@
1
- import { Headers, headersToList } from 'headers-polyfill';
2
- import status from 'statuses';
3
1
  import BinaryReader from '../lib/binary_reader';
4
2
  import BinaryWriter from '../lib/binary_writer';
5
- import {
6
- HttpHeaders,
7
- HttpMethod,
8
- HttpRequest,
9
- HttpResponse,
10
- } from '../lib/http_types';
3
+ import status from 'statuses';
4
+ import { HttpRequest, HttpResponse } from '../lib/autogen/types';
11
5
  import type { TimeDuration } from '../lib/time_duration';
12
6
  import { bsatnBaseSize } from '../lib/util';
7
+ import {
8
+ type BodyInit,
9
+ type HeadersInit,
10
+ deserializeHeaders,
11
+ Headers,
12
+ makeResponse,
13
+ serializeHeaders,
14
+ serializeMethod,
15
+ SyncResponse,
16
+ } from './http_shared';
13
17
  import { sys } from './runtime';
14
18
 
15
19
  export { Headers };
16
20
 
17
21
  const { freeze } = Object;
18
22
 
19
- export type BodyInit = ArrayBuffer | ArrayBufferView | string;
20
- export type HeadersInit = [string, string][] | Record<string, string> | Headers;
21
- export interface ResponseInit {
22
- headers?: HeadersInit;
23
- status?: number;
24
- statusText?: string;
25
- }
26
-
27
- const textEncoder = new TextEncoder();
28
- const textDecoder = new TextDecoder('utf-8' /* { fatal: true } */);
29
-
30
- function deserializeHeaders(headers: HttpHeaders): Headers {
31
- return new Headers(
32
- headers.entries.map(({ name, value }): [string, string] => [
33
- name,
34
- textDecoder.decode(value),
35
- ])
36
- );
37
- }
38
-
39
- const makeResponse = Symbol('makeResponse');
40
-
41
- // based on deno's type of the same name
42
- interface InnerResponse {
43
- type: 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect';
44
- url: string | null;
45
- status: number;
46
- statusText: string;
47
- headers: Headers;
48
- aborted: boolean;
49
- }
50
-
51
- export class SyncResponse {
52
- #body: string | ArrayBuffer | null;
53
- #inner: InnerResponse;
54
-
55
- constructor(body?: BodyInit | null, init?: ResponseInit) {
56
- if (body == null) {
57
- this.#body = null;
58
- } else if (typeof body === 'string') {
59
- this.#body = body;
60
- } else {
61
- // this call is fine, the typings are just weird
62
- this.#body = new Uint8Array<ArrayBuffer>(body as any).buffer;
63
- }
64
-
65
- // there's a type mismatch - headers-polyfill's typing doesn't expect its
66
- // own `Headers` type, even though the actual code handles it correctly.
67
- this.#inner = {
68
- headers: new Headers(init?.headers as any),
69
- status: init?.status ?? 200,
70
- statusText: init?.statusText ?? '',
71
- type: 'default',
72
- url: null,
73
- aborted: false,
74
- };
75
- }
76
-
77
- static [makeResponse](body: BodyInit | null, inner: InnerResponse) {
78
- const me = new SyncResponse(body);
79
- me.#inner = inner;
80
- return me;
81
- }
82
-
83
- get headers(): Headers {
84
- return this.#inner.headers;
85
- }
86
- get status(): number {
87
- return this.#inner.status;
88
- }
89
- get statusText() {
90
- return this.#inner.statusText;
91
- }
92
- get ok(): boolean {
93
- return 200 <= this.#inner.status && this.#inner.status <= 299;
94
- }
95
- get url(): string {
96
- return this.#inner.url ?? '';
97
- }
98
- get type() {
99
- return this.#inner.type;
100
- }
101
-
102
- arrayBuffer(): ArrayBuffer {
103
- return this.bytes().buffer;
104
- }
105
-
106
- bytes(): Uint8Array<ArrayBuffer> {
107
- if (this.#body == null) {
108
- return new Uint8Array();
109
- } else if (typeof this.#body === 'string') {
110
- return textEncoder.encode(this.#body);
111
- } else {
112
- return new Uint8Array(this.#body);
113
- }
114
- }
115
-
116
- json(): any {
117
- return JSON.parse(this.text());
118
- }
119
-
120
- text(): string {
121
- if (this.#body == null) {
122
- return '';
123
- } else if (typeof this.#body === 'string') {
124
- return this.#body;
125
- } else {
126
- return textDecoder.decode(this.#body);
127
- }
128
- }
129
- }
130
-
131
23
  export interface RequestOptions {
132
24
  /** A BodyInit object or null to set request's body. */
133
25
  body?: BodyInit | null;
@@ -147,29 +39,9 @@ export interface HttpClient {
147
39
 
148
40
  const requestBaseSize = bsatnBaseSize({ types: [] }, HttpRequest.algebraicType);
149
41
 
150
- const methods = new Map<string, HttpMethod>([
151
- ['GET', { tag: 'Get' }],
152
- ['HEAD', { tag: 'Head' }],
153
- ['POST', { tag: 'Post' }],
154
- ['PUT', { tag: 'Put' }],
155
- ['DELETE', { tag: 'Delete' }],
156
- ['CONNECT', { tag: 'Connect' }],
157
- ['OPTIONS', { tag: 'Options' }],
158
- ['TRACE', { tag: 'Trace' }],
159
- ['PATCH', { tag: 'Patch' }],
160
- ]);
161
-
162
42
  function fetch(url: URL | string, init: RequestOptions = {}) {
163
- const method = methods.get(init.method?.toUpperCase() ?? 'GET') ?? {
164
- tag: 'Extension',
165
- value: init.method!,
166
- };
167
- const headers: HttpHeaders = {
168
- // anys because the typings are wonky - see comment in SyncResponse.constructor
169
- entries: headersToList(new Headers(init.headers as any) as any)
170
- .flatMap(([k, v]) => (Array.isArray(v) ? v.map(v => [k, v]) : [[k, v]]))
171
- .map(([name, value]) => ({ name, value: textEncoder.encode(value) })),
172
- };
43
+ const method = serializeMethod(init.method);
44
+ const headers = serializeHeaders(new Headers(init.headers as any));
173
45
  const uri = '' + url;
174
46
  const request: HttpRequest = freeze({
175
47
  method,
@@ -198,6 +70,7 @@ function fetch(url: URL | string, init: RequestOptions = {}) {
198
70
  statusText: status(response.code),
199
71
  headers: deserializeHeaders(response.headers),
200
72
  aborted: false,
73
+ version: response.version,
201
74
  });
202
75
  }
203
76