-
Notifications
You must be signed in to change notification settings - Fork 1k
TypeScript HTTP handlers #4931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: phoebe/http-handlers-webhooks
Are you sure you want to change the base?
TypeScript HTTP handlers #4931
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import { | ||
| HttpRequest, | ||
| HttpResponse, | ||
| type MethodOrAny, | ||
| type HttpMethod, | ||
| } from '../lib/autogen/types'; | ||
| import BinaryReader from '../lib/binary_reader'; | ||
| import BinaryWriter from '../lib/binary_writer'; | ||
| import { bsatnBaseSize } from '../lib/util'; | ||
| import { | ||
| Headers, | ||
| SyncResponse, | ||
| deserializeHeaders, | ||
| serializeHeaders, | ||
| } from './http_internal'; | ||
| import { | ||
| exportContext, | ||
| registerExport, | ||
| type ModuleExport, | ||
| type SchemaInner, | ||
| } from './schema'; | ||
|
|
||
| export interface Request { | ||
| readonly method: string; | ||
| readonly url: string; | ||
| readonly headers: Headers; | ||
| readonly body: Uint8Array; | ||
| } | ||
|
|
||
| export type HttpHandler = (request: Request) => SyncResponse; | ||
| export type HttpHandlerExport = HttpHandler & ModuleExport; | ||
|
|
||
| type HttpMethodName = 'GET' | 'POST'; | ||
|
|
||
| type Route = { | ||
| method: HttpMethodName; | ||
| path: string; | ||
| handler: HttpHandlerExport; | ||
| }; | ||
|
|
||
| export type HttpHandlers = HttpHandler[]; | ||
|
|
||
| const responseBaseSize = bsatnBaseSize( | ||
| { types: [] }, | ||
| HttpResponse.algebraicType | ||
| ); | ||
|
|
||
| const routesSymbol = Symbol('SpacetimeDB.http.routes'); | ||
| const httpHandlerSymbol = Symbol('SpacetimeDB.http.handler'); | ||
|
|
||
| type RouterWithRoutes = Router & { | ||
| [routesSymbol]: Route[]; | ||
| }; | ||
|
Comment on lines
+51
to
+53
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this redundant? |
||
|
|
||
| const METHODS: Record<HttpMethodName, MethodOrAny> = { | ||
| GET: { tag: 'Method', value: { tag: 'Get' } }, | ||
| POST: { tag: 'Method', value: { tag: 'Post' } }, | ||
| }; | ||
|
|
||
| function methodToString(method: HttpMethod): string { | ||
| switch (method.tag) { | ||
| case 'Get': | ||
| return 'GET'; | ||
| case 'Head': | ||
| return 'HEAD'; | ||
| case 'Post': | ||
| return 'POST'; | ||
| case 'Put': | ||
| return 'PUT'; | ||
| case 'Delete': | ||
| return 'DELETE'; | ||
| case 'Connect': | ||
| return 'CONNECT'; | ||
| case 'Options': | ||
| return 'OPTIONS'; | ||
| case 'Trace': | ||
| return 'TRACE'; | ||
| case 'Patch': | ||
| return 'PATCH'; | ||
| case 'Extension': | ||
| return method.value; | ||
| } | ||
| } | ||
|
|
||
| export class Router { | ||
| [routesSymbol]: Route[] = []; | ||
|
|
||
| get(path: string, handler: HttpHandlerExport): this { | ||
| // TODO(v8-http-handlers): Validate route path and duplicate registrations. | ||
| this[routesSymbol].push({ method: 'GET', path, handler }); | ||
| return this; | ||
| } | ||
|
|
||
| post(path: string, handler: HttpHandlerExport): this { | ||
| // TODO(v8-http-handlers): Validate route path and duplicate registrations. | ||
| this[routesSymbol].push({ method: 'POST', path, handler }); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| function registerHttpHandler( | ||
| ctx: SchemaInner, | ||
| exportName: string, | ||
| fn: HttpHandler | ||
| ): void { | ||
| ctx.defineFunction(exportName); | ||
| ctx.moduleDef.httpHandlers.push({ sourceName: exportName }); | ||
| ctx.httpHandlers.push(fn); | ||
| } | ||
|
|
||
| function makeHttpHandlerExport( | ||
| ctx: SchemaInner, | ||
| fn: HttpHandler | ||
| ): HttpHandlerExport { | ||
| const handlerExport = fn as HttpHandlerExport & { | ||
| [httpHandlerSymbol]?: true; | ||
| }; | ||
|
|
||
| handlerExport[exportContext] = ctx; | ||
| handlerExport[httpHandlerSymbol] = true; | ||
| handlerExport[registerExport] = (ctx, exportName) => { | ||
| // TODO(v8-http-handlers): Reject duplicate registration of the same function object. | ||
| registerHttpHandler(ctx, exportName, fn); | ||
| ctx.functionExports.set(handlerExport, exportName); | ||
| }; | ||
|
|
||
| return handlerExport; | ||
| } | ||
|
|
||
| function makeHttpRouterExport(ctx: SchemaInner, router: Router): ModuleExport { | ||
| return { | ||
| [exportContext]: ctx, | ||
| [registerExport](ctx, _exportName) { | ||
| for (const route of (router as RouterWithRoutes)[routesSymbol]) { | ||
| // TODO(v8-http-handlers): Verify that handlers referenced by routers come from the same schema. | ||
| const handlerName = ctx.functionExports.get(route.handler); | ||
| if (handlerName === undefined) { | ||
| throw new TypeError( | ||
| 'HTTP router references a handler that was not exported as an HTTP handler' | ||
| ); | ||
| } | ||
| ctx.moduleDef.httpRoutes.push({ | ||
| handlerFunction: handlerName, | ||
| method: METHODS[route.method], | ||
| path: route.path, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export function makeHttpNamespace(ctx: SchemaInner) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice for the return value of this function (and thus also the |
||
| return Object.freeze({ | ||
| Router, | ||
| handler(fn: HttpHandler): HttpHandlerExport { | ||
| return makeHttpHandlerExport(ctx, fn); | ||
| }, | ||
| router(router: Router): ModuleExport { | ||
| if (!(router instanceof Router)) { | ||
| throw new TypeError('spacetime.http.router expects a Router instance'); | ||
| } | ||
| return makeHttpRouterExport(ctx, router); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| export function deserializeHttpHandlerRequest( | ||
| requestBuf: Uint8Array, | ||
| requestBody: Uint8Array | ||
| ): Request { | ||
| const request = HttpRequest.deserialize(new BinaryReader(requestBuf)); | ||
| return Object.freeze({ | ||
| method: methodToString(request.method), | ||
| url: request.uri, | ||
| headers: deserializeHeaders(request.headers), | ||
| body: requestBody, | ||
| }); | ||
| } | ||
|
|
||
| export function serializeHttpHandlerResponse( | ||
| response: SyncResponse | ||
| ): [Uint8Array, Uint8Array] { | ||
| const responseWire: HttpResponse = { | ||
| code: response.status, | ||
| headers: serializeHeaders(response.headers), | ||
| version: { tag: 'Http11' }, | ||
| }; | ||
|
|
||
| const responseBuf = new BinaryWriter(responseBaseSize); | ||
| HttpResponse.serialize(responseBuf, responseWire); | ||
| return [responseBuf.getBuffer(), response.bytes()]; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What purpose do these serve?
httpHandlerSymbolseems unused androutesSymbolcould just be a private field onRouterwith a static method to access it.