@stacksjs/bun-router 0.0.22 → 0.0.23

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.
package/dist/cli.js CHANGED
@@ -2047,7 +2047,7 @@ function getPrefix() {
2047
2047
  }
2048
2048
  var prefix = getPrefix();
2049
2049
  // package.json
2050
- var version = "0.0.22";
2050
+ var version = "0.0.23";
2051
2051
 
2052
2052
  // src/cli/middleware.ts
2053
2053
  import fs2 from "fs/promises";
package/dist/index.js CHANGED
@@ -3074,7 +3074,19 @@ function findPrebuiltView(stxFilePath, viewsDir) {
3074
3074
  }
3075
3075
  return null;
3076
3076
  }
3077
- async function renderStxFile(filePath, viewsDir, data, routingConfig) {
3077
+ function parseCookies(header) {
3078
+ const jar = {};
3079
+ for (const part of String(header ?? "").split(";")) {
3080
+ const cut = part.indexOf("=");
3081
+ if (cut < 0)
3082
+ continue;
3083
+ try {
3084
+ jar[part.slice(0, cut).trim()] = decodeURIComponent(part.slice(cut + 1).trim());
3085
+ } catch {}
3086
+ }
3087
+ return jar;
3088
+ }
3089
+ async function renderStxFile(filePath, viewsDir, data, routingConfig, request2) {
3078
3090
  const stxModule = "@stacksjs/stx";
3079
3091
  const stx = await import(stxModule);
3080
3092
  if (!stx.processDirectives || !stx.extractVariables) {
@@ -3085,11 +3097,42 @@ async function renderStxFile(filePath, viewsDir, data, routingConfig) {
3085
3097
  const scriptContent = scriptMatch ? scriptMatch[1] : "";
3086
3098
  let templateContent = scriptMatch ? content.replace(/<script\s+server\s*>[\s\S]*?<\/script>/i, "") : content;
3087
3099
  templateContent = templateContent.replace(/<script\s+client\s*>/gi, "<script>");
3100
+ const asked = { status: 200, headers: {} };
3101
+ const headerBag = data.headers ?? {};
3102
+ const cookieHeader = String(headerBag.cookie ?? headerBag.Cookie ?? "");
3103
+ const requestUrl = String(data.url ?? "");
3104
+ const search = requestUrl.includes("?") ? requestUrl.slice(requestUrl.indexOf("?")) : "";
3105
+ const serveContext = {
3106
+ url: requestUrl,
3107
+ path: String(data.path ?? ""),
3108
+ search,
3109
+ host: String(headerBag.host ?? ""),
3110
+ cookieHeader,
3111
+ cookies: parseCookies(cookieHeader),
3112
+ ip: "",
3113
+ locale: null,
3114
+ params: data.params ?? {},
3115
+ method: String(data.method ?? "GET"),
3116
+ request: request2
3117
+ };
3088
3118
  const context = {
3089
3119
  __filename: filePath,
3090
3120
  __dirname: dirname(filePath),
3091
3121
  props: data,
3092
- ...data
3122
+ ...data,
3123
+ __stxServeContext: serveContext,
3124
+ setResponseStatus: (status) => {
3125
+ if (Number.isInteger(status) && status >= 100 && status <= 599)
3126
+ asked.status = status;
3127
+ },
3128
+ notFound: (status = 404) => {
3129
+ asked.status = Number.isInteger(status) && status >= 400 && status <= 599 ? status : 404;
3130
+ },
3131
+ setResponseHeader: (name, value) => {
3132
+ if (name)
3133
+ asked.headers[String(name)] = String(value);
3134
+ },
3135
+ definePageMeta: () => {}
3093
3136
  };
3094
3137
  if (scriptContent) {
3095
3138
  await stx.extractVariables(scriptContent, context, filePath);
@@ -3103,7 +3146,14 @@ async function renderStxFile(filePath, viewsDir, data, routingConfig) {
3103
3146
  layoutsDir: resolvedLayoutsDir,
3104
3147
  partialsDir: resolvedPartialsDir
3105
3148
  };
3106
- return stx.processDirectives(templateContent, context, filePath, config2, new Set);
3149
+ const html = await stx.processDirectives(templateContent, context, filePath, config2, new Set);
3150
+ const recordedStatus = Number(context.__stxResponseStatus);
3151
+ const recordedHeaders = context.__stxResponseHeaders;
3152
+ return {
3153
+ html,
3154
+ status: Number.isInteger(recordedStatus) && recordedStatus >= 100 && recordedStatus <= 599 ? recordedStatus : asked.status,
3155
+ headers: { ...asked.headers, ...recordedHeaders ?? {} }
3156
+ };
3107
3157
  }
3108
3158
  function createViewHandler(filePath, viewsDir, config2, queryPreservationConfig) {
3109
3159
  return async (req) => {
@@ -3133,11 +3183,11 @@ function createViewHandler(filePath, viewsDir, config2, queryPreservationConfig)
3133
3183
  });
3134
3184
  }
3135
3185
  if (filePath.endsWith(".stx")) {
3136
- let html = await renderStxFile(filePath, viewsDir, data, config2);
3137
- html = applyQueryPreservation(html);
3186
+ const rendered = await renderStxFile(filePath, viewsDir, data, config2, req);
3187
+ const html = applyQueryPreservation(rendered.html);
3138
3188
  return new Response(html, {
3139
- status: 200,
3140
- headers: { "Content-Type": "text/html; charset=utf-8" }
3189
+ status: rendered.status,
3190
+ headers: { "Content-Type": "text/html; charset=utf-8", ...rendered.headers }
3141
3191
  });
3142
3192
  }
3143
3193
  let content = await Bun.file(filePath).text();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/bun-router",
3
3
  "type": "module",
4
- "version": "0.0.22",
4
+ "version": "0.0.23",
5
5
  "description": "A fast, type-safe router for Bun.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",