Skip to content

Commit

Permalink
chore: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Apr 3, 2024
1 parent 8213052 commit 5b6b988
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swtl",
"version": "0.3.4",
"version": "0.3.5",
"description": "",
"main": "index.js",
"type": "module",
Expand Down
74 changes: 41 additions & 33 deletions packages/core/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Router {
/**
* @param {{
* routes: Route[],
* fallback: (args: RouteArgs) => RouteResult,
* fallback?: (args: RouteArgs) => RouteResult,
* plugins?: Plugin[],
* baseHref?: string,
* customElementRenderers?: CustomElementRenderer[]
Expand Down Expand Up @@ -58,6 +58,7 @@ export class Router {

/**
* @param {Request} request
* @returns {Promise<Response | HtmlResponse | undefined>}
*/
async handleRequest(request) {
const url = new URL(request.url);
Expand Down Expand Up @@ -96,7 +97,7 @@ export class Router {
}
}

return new HtmlResponse(
return createResponse(
await route({url, query, params, request}),
matchedRoute?.options ?? {},
{
Expand All @@ -107,43 +108,50 @@ export class Router {
}
}

/**
* @param {*} template
* @param {*} routeOptions
* @param {*} renderOptions
* @returns
*/
function createResponse(template, routeOptions, renderOptions) {
const iterator = render(template, renderOptions.renderers);
const encoder = new TextEncoder();
const stream = new ReadableStream({
async pull(controller) {
try {
const { value, done } = await iterator.next();

if (done) {
controller.close();
} else {
controller.enqueue(encoder.encode(value));
}
} catch(e) {
console.error(/** @type {Error} */ (e).stack);
throw e;
}
}
});

return new Response(stream, {
status: 200,
headers: {
'Content-Type': 'text/html',
'Transfer-Encoding': 'chunked',
...(routeOptions?.headers ?? {})
},
...routeOptions
});
}

export class HtmlResponse {
/**
*
* @param {unknown} template
* @param {*} routeOptions
* @param {*} renderOptions
* @returns
*/
constructor(template, routeOptions = {}, renderOptions = {}) {
// @ts-expect-error
const iterator = render(template, renderOptions.renderers);
const encoder = new TextEncoder();
const stream = new ReadableStream({
async pull(controller) {
try {
const { value, done } = await iterator.next();

if (done) {
controller.close();
} else {
controller.enqueue(encoder.encode(value));
}
} catch(e) {
console.error(/** @type {Error} */ (e).stack);
throw e;
}
}
});

return new Response(stream, {
status: 200,
headers: {
'Content-Type': 'text/html',
'Transfer-Encoding': 'chunked',
...(routeOptions?.headers ?? {})
},
...routeOptions
});
return createResponse(template, routeOptions, renderOptions);
}
}

0 comments on commit 5b6b988

Please sign in to comment.