Skip to content

Commit

Permalink
ws as handler instead of as route verb
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Feb 7, 2024
1 parent a327bd2 commit 9eabc05
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
6 changes: 5 additions & 1 deletion packages/primate/src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const json = handle(MediaType.APPLICATION_JSON, JSON.stringify);
// {{{ stream
const stream = handle(MediaType.APPLICATION_OCTET_STREAM);
// }}}
// {{{ ws
const ws = implementation => ({ server }, _, { original }) =>
server.upgrade(original, implementation);
// }}}
// {{{ sse
const sse = handle(MediaType.TEXT_EVENT_STREAM, implementation =>
new ReadableStream({
Expand Down Expand Up @@ -70,4 +74,4 @@ const view = (name, props, options) => async (app, ...rest) => {
};
// }}}

export { text, json, stream, redirect, error, html, view, sse };
export { text, json, stream, redirect, error, html, view, ws, sse };
9 changes: 2 additions & 7 deletions packages/primate/src/hooks/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export default app => {
let error_handler = app.error.default;

return tryreturn(async _ => {
const { path, guards, errors, layouts, handler, method } =
await route(request);
const { path, guards, errors, layouts, handler } = await route(request);

error_handler = errors?.at(-1);

Expand All @@ -55,12 +54,8 @@ export default app => {
// handle request
const response = await (await cascade(hooks, handler))(pathed);

if (method === "ws") {
return app.server.upgrade(request.original, response);
}

const $layouts = { layouts: await get_layouts(layouts, request) };
return (await respond(response, method))(app, $layouts, pathed);
return (await respond(response))(app, $layouts, pathed);
}).orelse(async error => {
app.log.auto(error);

Expand Down
5 changes: 1 addition & 4 deletions packages/primate/src/hooks/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const ieq = (left, right) => left.toLowerCase() === right.toLowerCase();
const deroot = pathname => pathname.endsWith("/") && pathname !== "/"
? pathname.slice(0, -1) : pathname;

const is_ws = headers => headers.get("upgrade") === "websocket";

export default app => {
const { types, routes, config: { types: { explicit }, location } } = app;

Expand Down Expand Up @@ -42,8 +40,7 @@ export default app => {

const index = path => `${location.routes}${path === "/" ? "/index" : path}`;

return ({ original, url, headers }) => {
const method = is_ws(headers) ? "ws" : original.method;
return ({ original: { method }, url }) => {
const pathname = deroot(url.pathname);
const route = find(method, pathname) ?? errors.NoRouteToPath
.throw(method.toLowerCase(), pathname, index(pathname));
Expand Down

0 comments on commit 9eabc05

Please sign in to comment.