@stacksjs/router 0.70.257 → 0.70.259

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/api-shape.js CHANGED
@@ -1,11 +1 @@
1
- export const JSON_CONTENT_TYPE = /^application\/(?:json|.+\+json)(?:;|$)/i;
2
- export function isApiRequest(req) {
3
- const headers = req.headers, contentType = headers.get("content-type") || "";
4
- if (JSON_CONTENT_TYPE.test(contentType))
5
- return !0;
6
- if (headers.get("sec-fetch-dest") === "document")
7
- return !1;
8
- if ((headers.get("accept") || "").includes("text/html"))
9
- return !1;
10
- return !0;
11
- }
1
+ export const JSON_CONTENT_TYPE=/^application\/(?:json|.+\+json)(?:;|$)/i;export function isApiRequest(req){const headers=req.headers,contentType=headers.get("content-type")||"";if(JSON_CONTENT_TYPE.test(contentType))return!0;if(headers.get("sec-fetch-dest")==="document")return!1;if((headers.get("accept")||"").includes("text/html"))return!1;return!0}
@@ -1,73 +1 @@
1
- import { decrypt, encrypt } from "@stacksjs/security";
2
-
3
- export class EncryptedSessionStore {
4
- inner;
5
- opts;
6
- constructor(inner, opts = {}) {
7
- this.inner = inner;
8
- this.opts = opts;
9
- }
10
- async set(sid, session, ttl) {
11
- const envelope = await this.wrap(sid, session);
12
- await this.inner.set(sid, envelope, ttl);
13
- }
14
- async touch(sid, session, ttl) {
15
- const envelope = await this.wrap(sid, session);
16
- if (this.inner.touch)
17
- await this.inner.touch(sid, envelope, ttl);
18
- else
19
- await this.inner.set(sid, envelope, ttl);
20
- }
21
- async get(sid) {
22
- const stored = await this.inner.get(sid);
23
- if (!stored)
24
- return null;
25
- return this.unwrap(stored);
26
- }
27
- destroy(sid) {
28
- return this.inner.destroy(sid);
29
- }
30
- async all() {
31
- const wrapped = await this.inner.all?.() ?? {}, out = {};
32
- for (const [sid, envelope] of Object.entries(wrapped)) {
33
- const decrypted = await this.unwrap(envelope);
34
- if (decrypted)
35
- out[sid] = decrypted;
36
- }
37
- return out;
38
- }
39
- async length() {
40
- if (this.inner.length)
41
- return this.inner.length();
42
- return Object.keys(await this.all()).length;
43
- }
44
- async clear() {
45
- if (this.inner.clear)
46
- return this.inner.clear();
47
- const sessions = await this.inner.all?.() ?? {};
48
- await Promise.all(Object.keys(sessions).map((sid) => this.inner.destroy(sid)));
49
- }
50
- async wrap(sid, session) {
51
- const { id, ...rest } = session, ciphertext = await encrypt(JSON.stringify(rest), this.opts.appKey);
52
- return {
53
- _enc: !0,
54
- id: id ?? sid,
55
- data: ciphertext
56
- };
57
- }
58
- async unwrap(stored) {
59
- if (!stored || typeof stored !== "object")
60
- return null;
61
- const candidate = stored;
62
- if (candidate._enc === !0 && typeof candidate.data === "string")
63
- try {
64
- const decrypted = await decrypt(candidate.data, this.opts.appKey), parsed = JSON.parse(decrypted);
65
- if (candidate.id !== void 0)
66
- parsed.id = candidate.id;
67
- return parsed;
68
- } catch {
69
- return null;
70
- }
71
- return candidate;
72
- }
73
- }
1
+ import{decrypt,encrypt}from"@stacksjs/security";export class EncryptedSessionStore{inner;opts;constructor(inner,opts={}){this.inner=inner;this.opts=opts}async set(sid,session,ttl){const envelope=await this.wrap(sid,session);await this.inner.set(sid,envelope,ttl)}async touch(sid,session,ttl){const envelope=await this.wrap(sid,session);if(this.inner.touch)await this.inner.touch(sid,envelope,ttl);else await this.inner.set(sid,envelope,ttl)}async get(sid){const stored=await this.inner.get(sid);if(!stored)return null;return this.unwrap(stored)}destroy(sid){return this.inner.destroy(sid)}async all(){const wrapped=await this.inner.all?.()??{},out={};for(const[sid,envelope]of Object.entries(wrapped)){const decrypted=await this.unwrap(envelope);if(decrypted)out[sid]=decrypted}return out}async length(){if(this.inner.length)return this.inner.length();return Object.keys(await this.all()).length}async clear(){if(this.inner.clear)return this.inner.clear();const sessions=await this.inner.all?.()??{};await Promise.all(Object.keys(sessions).map((sid)=>this.inner.destroy(sid)))}async wrap(sid,session){const{id,...rest}=session,ciphertext=await encrypt(JSON.stringify(rest),this.opts.appKey);return{_enc:!0,id:id??sid,data:ciphertext}}async unwrap(stored){if(!stored||typeof stored!=="object")return null;const candidate=stored;if(candidate._enc===!0&&typeof candidate.data==="string")try{const decrypted=await decrypt(candidate.data,this.opts.appKey),parsed=JSON.parse(decrypted);if(candidate.id!==void 0)parsed.id=candidate.id;return parsed}catch{return null}return candidate}}
@@ -1,310 +1,13 @@
1
- import process from "node:process";
2
- import { log } from "@stacksjs/logging";
3
- import {
4
- createErrorHandler,
5
- renderProductionErrorPage
6
- } from "@stacksjs/error-handling";
7
- import { isApiRequest } from "./api-shape";
8
- import { getCurrentRequest } from "./request-context";
9
- function buildErrorJson(opts) {
10
- const body = {
11
- error: opts.error,
12
- message: opts.message,
13
- status: opts.status,
14
- timestamp: new Date().toISOString()
15
- };
16
- if (opts.details)
17
- body.details = opts.details;
18
- return JSON.stringify(body);
19
- }
20
- function isDebugAllowed() {
21
- const appEnv = (process.env.APP_ENV ?? "").toLowerCase();
22
- if (appEnv === "development")
23
- return !0;
24
- if (!appEnv && process.env.NODE_ENV === "development")
25
- return !0;
26
- return !1;
27
- }
28
- function getJsonHeaders() {
29
- return { "Content-Type": "application/json" };
30
- }
31
- function getJsonHeadersFull() {
32
- return getJsonHeaders();
33
- }
34
- const MAX_QUERIES = 50, N1_THRESHOLD = 5;
35
- function newQueryTrack() {
36
- return {
37
- buffer: Array(MAX_QUERIES).fill(null),
38
- writeIndex: 0,
39
- count: 0,
40
- shapeCounts: new Map,
41
- n1Warned: new Set
42
- };
43
- }
44
- const REQUEST_QUERY_TRACK_KEY = Symbol.for("stacks.queryTracking");
45
- let fallbackTrack = newQueryTrack();
46
- function getQueryTrack() {
47
- const req = getCurrentRequest();
48
- if (!req)
49
- return fallbackTrack;
50
- let track = req[REQUEST_QUERY_TRACK_KEY];
51
- if (!track) {
52
- track = newQueryTrack();
53
- req[REQUEST_QUERY_TRACK_KEY] = track;
54
- }
55
- return track;
56
- }
57
- function normalizeQueryShape(query) {
58
- return query.replace(/'(?:[^']|'')*'/g, "?").replace(/"(?:[^"]|"")*"/g, "?").replace(/\b\d+(?:\.\d+)?\b/g, "?").replace(/IN\s*\([^)]*\)/gi, "IN (?)").replace(/\s+/g, " ").trim().toUpperCase();
59
- }
60
- export function trackQuery(query, time, connection) {
61
- const track = getQueryTrack();
62
- track.buffer[track.writeIndex] = { query, time, connection };
63
- track.writeIndex = (track.writeIndex + 1) % MAX_QUERIES;
64
- if (track.count < MAX_QUERIES)
65
- track.count++;
66
- if (!isDebugAllowed())
67
- return;
68
- const shape = normalizeQueryShape(query);
69
- if (shape.startsWith("INSERT INTO QUERY_LOGS") || shape.startsWith("EXPLAIN"))
70
- return;
71
- const next = (track.shapeCounts.get(shape) ?? 0) + 1;
72
- track.shapeCounts.set(shape, next);
73
- if (next === N1_THRESHOLD + 1 && !track.n1Warned.has(shape)) {
74
- track.n1Warned.add(shape);
75
- import("@stacksjs/logging").then(({ log }) => {
76
- log.warn(`[orm] Possible N+1 \u2014 query shape ran ${next}\xD7 in this request:
1
+ import process from"node:process";import{log}from"@stacksjs/logging";import{createErrorHandler,renderProductionErrorPage}from"@stacksjs/error-handling";import{isApiRequest}from"./api-shape";import{getCurrentRequest}from"./request-context";function buildErrorJson(opts){const body={error:opts.error,message:opts.message,status:opts.status,timestamp:new Date().toISOString()};if(opts.details)body.details=opts.details;return JSON.stringify(body)}function isDebugAllowed(){const appEnv=(process.env.APP_ENV??"").toLowerCase();if(appEnv==="development")return!0;if(!appEnv&&process.env.NODE_ENV==="development")return!0;return!1}function getJsonHeaders(){return{"Content-Type":"application/json"}}function getJsonHeadersFull(){return getJsonHeaders()}const MAX_QUERIES=50,N1_THRESHOLD=5;function newQueryTrack(){return{buffer:Array(MAX_QUERIES).fill(null),writeIndex:0,count:0,shapeCounts:new Map,n1Warned:new Set}}const REQUEST_QUERY_TRACK_KEY=Symbol.for("stacks.queryTracking");let fallbackTrack=newQueryTrack();function getQueryTrack(){const req=getCurrentRequest();if(!req)return fallbackTrack;let track=req[REQUEST_QUERY_TRACK_KEY];if(!track){track=newQueryTrack();req[REQUEST_QUERY_TRACK_KEY]=track}return track}function normalizeQueryShape(query){return query.replace(/'(?:[^']|'')*'/g,"?").replace(/"(?:[^"]|"")*"/g,"?").replace(/\b\d+(?:\.\d+)?\b/g,"?").replace(/IN\s*\([^)]*\)/gi,"IN (?)").replace(/\s+/g," ").trim().toUpperCase()}export function trackQuery(query,time,connection){const track=getQueryTrack();track.buffer[track.writeIndex]={query,time,connection};track.writeIndex=(track.writeIndex+1)%MAX_QUERIES;if(track.count<MAX_QUERIES)track.count++;if(!isDebugAllowed())return;const shape=normalizeQueryShape(query);if(shape.startsWith("INSERT INTO QUERY_LOGS")||shape.startsWith("EXPLAIN"))return;const next=(track.shapeCounts.get(shape)??0)+1;track.shapeCounts.set(shape,next);if(next===N1_THRESHOLD+1&&!track.n1Warned.has(shape)){track.n1Warned.add(shape);import("@stacksjs/logging").then(({log})=>{log.warn(`[orm] Possible N+1 \u2014 query shape ran ${next}\xD7 in this request:
77
2
  ${shape}
78
- Hint: load related rows with .with('relation') or eager-load via includes() before iterating.`);
79
- }).catch(() => {});
80
- }
81
- }
82
- function getRecentQueries() {
83
- const track = getQueryTrack();
84
- if (track.count === 0)
85
- return [];
86
- const result = [], start = track.count < MAX_QUERIES ? 0 : track.writeIndex;
87
- for (let i = 0;i < track.count; i++) {
88
- const entry = track.buffer[(start + i) % MAX_QUERIES];
89
- if (entry)
90
- result.push(entry);
91
- }
92
- return result;
93
- }
94
- export function getQueryShapeCounts() {
95
- return new Map(getQueryTrack().shapeCounts);
96
- }
97
- export function clearTrackedQueries() {
98
- const req = getCurrentRequest();
99
- if (req && req[REQUEST_QUERY_TRACK_KEY]) {
100
- req[REQUEST_QUERY_TRACK_KEY] = newQueryTrack();
101
- return;
102
- }
103
- fallbackTrack = newQueryTrack();
104
- }
105
- function getErrorHandlerConfig() {
106
- return {
107
- appName: "Stacks",
108
- theme: "auto",
109
- showEnvironment: !0,
110
- showQueries: !0,
111
- showRequest: !0,
112
- enableCopyMarkdown: !0,
113
- snippetLines: 8,
114
- basePaths: [process.cwd()]
115
- };
116
- }
117
- const SENSITIVE_PATTERNS = [
118
- "password",
119
- "secret",
120
- "token",
121
- "api_key",
122
- "apikey",
123
- "access_key",
124
- "accesskey",
125
- "private_key",
126
- "privatekey",
127
- "credit_card",
128
- "creditcard",
129
- "card_number",
130
- "cardnumber",
131
- "cvv",
132
- "ssn",
133
- "authorization",
134
- "credential",
135
- "aws_secret",
136
- "aws_access",
137
- "database_password",
138
- "db_password",
139
- "encryption_key",
140
- "signing_key",
141
- "bearer",
142
- "session_id",
143
- "sessionid",
144
- "cookie"
145
- ], MAX_SANITIZE_DEPTH = 10, CIRCULAR_PLACEHOLDER = "[Circular]";
146
- function sanitizeData(data, depth = 0, seen = new WeakSet) {
147
- if (!data || typeof data !== "object" || depth >= MAX_SANITIZE_DEPTH)
148
- return data;
149
- if (seen.has(data))
150
- return CIRCULAR_PLACEHOLDER;
151
- seen.add(data);
152
- if (Array.isArray(data))
153
- return data.map((item) => sanitizeData(item, depth + 1, seen));
154
- const sanitized = {};
155
- for (const [key, value] of Object.entries(data)) {
156
- const lowerKey = key.toLowerCase();
157
- if (SENSITIVE_PATTERNS.some((pattern) => lowerKey.includes(pattern)))
158
- sanitized[key] = "********";
159
- else if (typeof value === "object" && value !== null)
160
- sanitized[key] = sanitizeData(value, depth + 1, seen);
161
- else
162
- sanitized[key] = value;
163
- }
164
- return sanitized;
165
- }
166
- function getRequestBody(request) {
167
- const req = request;
168
- if (req.jsonBody)
169
- return sanitizeData(req.jsonBody);
170
- if (req.formBody)
171
- return sanitizeData(req.formBody);
172
- return;
173
- }
174
- async function getUserContext(request) {
175
- const authed = request._authenticatedUser;
176
- if (authed)
177
- return {
178
- id: authed.id,
179
- email: authed.email,
180
- name: authed.name || authed.username
181
- };
182
- return;
183
- }
184
- export async function createErrorResponse(error, request, options) {
185
- const status = options?.status || 500;
186
- log.debug(`[error] ${status} ${error.message}`);
187
- if (!isDebugAllowed()) {
188
- if (isApiRequest(request)) {
189
- const isClientError = status >= 400 && status < 500, errDetails = error.details;
190
- return new Response(buildErrorJson({
191
- error: isClientError ? error.name || "Client Error" : "Internal Server Error",
192
- message: isClientError ? error.message : "An unexpected error occurred.",
193
- status,
194
- details: isClientError && errDetails && typeof errDetails === "object" ? errDetails : void 0
195
- }), { status, headers: getJsonHeaders() });
196
- }
197
- return new Response(renderProductionErrorPage(status), {
198
- status,
199
- headers: { "Content-Type": "text/html; charset=utf-8" }
200
- });
201
- }
202
- try {
203
- const handler = createErrorHandler(getErrorHandlerConfig());
204
- handler.setFramework("Stacks", "0.70.0");
205
- const requestBody = getRequestBody(request);
206
- if (requestBody) {
207
- const url = new URL(request.url);
208
- handler.setRequest({
209
- method: request.method,
210
- url: request.url,
211
- headers: Object.fromEntries(request.headers.entries()),
212
- queryParams: Object.fromEntries(url.searchParams.entries()),
213
- body: requestBody
214
- });
215
- } else
216
- handler.setRequest(request);
217
- const userContext = await getUserContext(request);
218
- if (userContext)
219
- handler.setUser(userContext);
220
- if (options?.routingContext)
221
- handler.setRouting(options.routingContext);
222
- else if (options?.handlerPath)
223
- handler.setRouting({
224
- controller: options.handlerPath
225
- });
226
- for (const query of getRecentQueries())
227
- handler.addQuery(query.query, query.time, query.connection);
228
- if (isApiRequest(request)) {
229
- const details = { handler: options?.handlerPath };
230
- if (isDebugAllowed()) {
231
- details.stack = error.stack?.split(`
232
- `).slice(0, 10);
233
- details.queries = getRecentQueries().slice(-10);
234
- }
235
- return new Response(buildErrorJson({
236
- error: error.name || "Error",
237
- message: error.message,
238
- status,
239
- details
240
- }), { status, headers: getJsonHeadersFull() });
241
- }
242
- const corsOrigin = process.env.APP_URL ? process.env.APP_URL.startsWith("http") ? process.env.APP_URL : `https://${process.env.APP_URL}` : isDebugAllowed() ? "*" : request.headers.get("origin") ?? "null", html = await handler.render(error, status);
243
- return new Response(html, {
244
- status,
245
- headers: {
246
- "Content-Type": "text/html; charset=utf-8",
247
- "Access-Control-Allow-Origin": corsOrigin
248
- }
249
- });
250
- } catch (renderError) {
251
- console.error("[Error Handler] Failed to render error page:", renderError);
252
- const escapeHtml = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
253
- return new Response(`
3
+ Hint: load related rows with .with('relation') or eager-load via includes() before iterating.`)}).catch(()=>{})}}function getRecentQueries(){const track=getQueryTrack();if(track.count===0)return[];const result=[],start=track.count<MAX_QUERIES?0:track.writeIndex;for(let i=0;i<track.count;i++){const entry=track.buffer[(start+i)%MAX_QUERIES];if(entry)result.push(entry)}return result}export function getQueryShapeCounts(){return new Map(getQueryTrack().shapeCounts)}export function clearTrackedQueries(){const req=getCurrentRequest();if(req&&req[REQUEST_QUERY_TRACK_KEY]){req[REQUEST_QUERY_TRACK_KEY]=newQueryTrack();return}fallbackTrack=newQueryTrack()}function getErrorHandlerConfig(){return{appName:"Stacks",theme:"auto",showEnvironment:!0,showQueries:!0,showRequest:!0,enableCopyMarkdown:!0,snippetLines:8,basePaths:[process.cwd()]}}const SENSITIVE_PATTERNS=["password","secret","token","api_key","apikey","access_key","accesskey","private_key","privatekey","credit_card","creditcard","card_number","cardnumber","cvv","ssn","authorization","credential","aws_secret","aws_access","database_password","db_password","encryption_key","signing_key","bearer","session_id","sessionid","cookie"],MAX_SANITIZE_DEPTH=10,CIRCULAR_PLACEHOLDER="[Circular]";function sanitizeData(data,depth=0,seen=new WeakSet){if(!data||typeof data!=="object"||depth>=MAX_SANITIZE_DEPTH)return data;if(seen.has(data))return CIRCULAR_PLACEHOLDER;seen.add(data);if(Array.isArray(data))return data.map((item)=>sanitizeData(item,depth+1,seen));const sanitized={};for(const[key,value]of Object.entries(data)){const lowerKey=key.toLowerCase();if(SENSITIVE_PATTERNS.some((pattern)=>lowerKey.includes(pattern)))sanitized[key]="********";else if(typeof value==="object"&&value!==null)sanitized[key]=sanitizeData(value,depth+1,seen);else sanitized[key]=value}return sanitized}function getRequestBody(request){const req=request;if(req.jsonBody)return sanitizeData(req.jsonBody);if(req.formBody)return sanitizeData(req.formBody);return}async function getUserContext(request){const authed=request._authenticatedUser;if(authed)return{id:authed.id,email:authed.email,name:authed.name||authed.username};return}export async function createErrorResponse(error,request,options){const status=options?.status||500;log.debug(`[error] ${status} ${error.message}`);if(!isDebugAllowed()){if(isApiRequest(request)){const isClientError=status>=400&&status<500,errDetails=error.details;return new Response(buildErrorJson({error:isClientError?error.name||"Client Error":"Internal Server Error",message:isClientError?error.message:"An unexpected error occurred.",status,details:isClientError&&errDetails&&typeof errDetails==="object"?errDetails:void 0}),{status,headers:getJsonHeaders()})}return new Response(renderProductionErrorPage(status),{status,headers:{"Content-Type":"text/html; charset=utf-8"}})}try{const handler=createErrorHandler(getErrorHandlerConfig());handler.setFramework("Stacks","0.70.0");const requestBody=getRequestBody(request);if(requestBody){const url=new URL(request.url);handler.setRequest({method:request.method,url:request.url,headers:Object.fromEntries(request.headers.entries()),queryParams:Object.fromEntries(url.searchParams.entries()),body:requestBody})}else handler.setRequest(request);const userContext=await getUserContext(request);if(userContext)handler.setUser(userContext);if(options?.routingContext)handler.setRouting(options.routingContext);else if(options?.handlerPath)handler.setRouting({controller:options.handlerPath});for(const query of getRecentQueries())handler.addQuery(query.query,query.time,query.connection);if(isApiRequest(request)){const details={handler:options?.handlerPath};if(isDebugAllowed()){details.stack=error.stack?.split(`
4
+ `).slice(0,10);details.queries=getRecentQueries().slice(-10)}return new Response(buildErrorJson({error:error.name||"Error",message:error.message,status,details}),{status,headers:getJsonHeadersFull()})}const corsOrigin=process.env.APP_URL?process.env.APP_URL.startsWith("http")?process.env.APP_URL:`https://${process.env.APP_URL}`:isDebugAllowed()?"*":request.headers.get("origin")??"null",html=await handler.render(error,status);return new Response(html,{status,headers:{"Content-Type":"text/html; charset=utf-8","Access-Control-Allow-Origin":corsOrigin}})}catch(renderError){console.error("[Error Handler] Failed to render error page:",renderError);const escapeHtml=(s)=>s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");return new Response(`
254
5
  <html>
255
6
  <head><title>Error</title></head>
256
7
  <body>
257
8
  <h1>Error</h1>
258
9
  <p>${escapeHtml(error.message)}</p>
259
- <pre>${escapeHtml(error.stack || "")}</pre>
10
+ <pre>${escapeHtml(error.stack||"")}</pre>
260
11
  </body>
261
12
  </html>
262
- `, {
263
- status,
264
- headers: { "Content-Type": "text/html; charset=utf-8" }
265
- });
266
- }
267
- }
268
- export async function createMiddlewareErrorResponse(error, request) {
269
- const status = error.statusCode ?? error.status ?? 500, isDevelopment = isDebugAllowed();
270
- if (status >= 400 && status < 500) {
271
- const headers = error.headers ? { ...error.headers, ...getJsonHeaders() } : getJsonHeaders();
272
- return new Response(buildErrorJson({
273
- error: error.name || "ClientError",
274
- message: error.message,
275
- status
276
- }), { status, headers });
277
- }
278
- if (isDevelopment)
279
- return await createErrorResponse(error, request, { status });
280
- return new Response(buildErrorJson({
281
- error: "Internal Server Error",
282
- message: "An unexpected error occurred.",
283
- status
284
- }), { status, headers: getJsonHeaders() });
285
- }
286
- export function createValidationErrorResponse(errors, _request) {
287
- return new Response(buildErrorJson({
288
- error: "ValidationError",
289
- message: "Validation failed",
290
- status: 422,
291
- details: { errors }
292
- }), { status: 422, headers: getJsonHeaders() });
293
- }
294
- export async function createNotFoundResponse(path, request) {
295
- if (isDebugAllowed()) {
296
- const error = Error(`Route not found: ${path}`);
297
- error.name = "NotFoundError";
298
- return await createErrorResponse(error, request, { status: 404 });
299
- }
300
- if (isApiRequest(request))
301
- return new Response(buildErrorJson({
302
- error: "NotFound",
303
- message: `Route not found: ${path}`,
304
- status: 404
305
- }), { status: 404, headers: getJsonHeaders() });
306
- return new Response(renderProductionErrorPage(404), {
307
- status: 404,
308
- headers: { "Content-Type": "text/html; charset=utf-8" }
309
- });
310
- }
13
+ `,{status,headers:{"Content-Type":"text/html; charset=utf-8"}})}}export async function createMiddlewareErrorResponse(error,request){const status=error.statusCode??error.status??500,isDevelopment=isDebugAllowed();if(status>=400&&status<500){const headers=error.headers?{...error.headers,...getJsonHeaders()}:getJsonHeaders();return new Response(buildErrorJson({error:error.name||"ClientError",message:error.message,status}),{status,headers})}if(isDevelopment)return await createErrorResponse(error,request,{status});return new Response(buildErrorJson({error:"Internal Server Error",message:"An unexpected error occurred.",status}),{status,headers:getJsonHeaders()})}export function createValidationErrorResponse(errors,_request){return new Response(buildErrorJson({error:"ValidationError",message:"Validation failed",status:422,details:{errors}}),{status:422,headers:getJsonHeaders()})}export async function createNotFoundResponse(path,request){if(isDebugAllowed()){const error=Error(`Route not found: ${path}`);error.name="NotFoundError";return await createErrorResponse(error,request,{status:404})}if(isApiRequest(request))return new Response(buildErrorJson({error:"NotFound",message:`Route not found: ${path}`,status:404}),{status:404,headers:getJsonHeaders()});return new Response(renderProductionErrorPage(404),{status:404,headers:{"Content-Type":"text/html; charset=utf-8"}})}
package/dist/health.js CHANGED
@@ -1,56 +1 @@
1
- export async function runHealthProbes(probes, options = {}) {
2
- const timeoutMs = options.timeoutMs ?? 1500, now = options.now ?? Date.now, entries = await Promise.all(probes.map(async ({ name, run }) => {
3
- const startedAt = now();
4
- let timeout;
5
- try {
6
- await Promise.race([
7
- run(),
8
- new Promise((_resolve, reject) => {
9
- timeout = setTimeout(() => reject(Error("timeout")), timeoutMs);
10
- })
11
- ]);
12
- return [name, { ok: !0, ms: Math.max(0, now() - startedAt) }];
13
- } catch (error) {
14
- return [name, {
15
- ok: !1,
16
- ms: Math.max(0, now() - startedAt),
17
- message: error instanceof Error ? error.message : String(error)
18
- }];
19
- } finally {
20
- if (timeout)
21
- clearTimeout(timeout);
22
- }
23
- })), checks = Object.fromEntries(entries);
24
- return {
25
- status: Object.values(checks).every((check) => check.ok) ? "healthy" : "degraded",
26
- checks,
27
- timestamp: now()
28
- };
29
- }
30
- export async function checkApplicationHealth(options = {}) {
31
- return runHealthProbes([
32
- {
33
- name: "database",
34
- async run() {
35
- const { db } = await import("@stacksjs/database"), unsafe = db.unsafe;
36
- if (typeof unsafe !== "function")
37
- throw Error("database driver does not expose a raw health probe");
38
- await unsafe.call(db, "SELECT 1");
39
- }
40
- },
41
- {
42
- name: "cache",
43
- async run() {
44
- const { cache } = await import("@stacksjs/cache"), key = `__health__:${Date.now()}`;
45
- let stored = !1;
46
- try {
47
- await cache.set(key, 1, 5);
48
- stored = !0;
49
- } finally {
50
- if (stored)
51
- await cache.del(key);
52
- }
53
- }
54
- }
55
- ], options);
56
- }
1
+ export async function runHealthProbes(probes,options={}){const timeoutMs=options.timeoutMs??1500,now=options.now??Date.now,entries=await Promise.all(probes.map(async({name,run})=>{const startedAt=now();let timeout;try{await Promise.race([run(),new Promise((_resolve,reject)=>{timeout=setTimeout(()=>reject(Error("timeout")),timeoutMs)})]);return[name,{ok:!0,ms:Math.max(0,now()-startedAt)}]}catch(error){return[name,{ok:!1,ms:Math.max(0,now()-startedAt),message:error instanceof Error?error.message:String(error)}]}finally{if(timeout)clearTimeout(timeout)}})),checks=Object.fromEntries(entries);return{status:Object.values(checks).every((check)=>check.ok)?"healthy":"degraded",checks,timestamp:now()}}export async function checkApplicationHealth(options={}){return runHealthProbes([{name:"database",async run(){const{db}=await import("@stacksjs/database"),unsafe=db.unsafe;if(typeof unsafe!=="function")throw Error("database driver does not expose a raw health probe");await unsafe.call(db,"SELECT 1")}},{name:"cache",async run(){const{cache}=await import("@stacksjs/cache"),key=`__health__:${Date.now()}`;let stored=!1;try{await cache.set(key,1,5);stored=!0}finally{if(stored)await cache.del(key)}}}],options)}
package/dist/index.js CHANGED
@@ -1,38 +1 @@
1
- var {require}=import.meta;import"./request-augmentation";
2
-
3
- export * from "@stacksjs/bun-router";
4
- export { assertRouteMiddlewareResolvable, clearMiddlewareCache, createStacksRouter, findUnresolvableRouteMiddleware, installMiddlewareHotReload, route, serve, serverResponse, url, warnOnMultipleRouterInstances } from "./stacks-router";
5
- export { cacheRequestQuery, getCurrentRequest, getTraceId, request, runWithRequest, setCurrentRequest, withTraceId } from "./request-context";
6
- export { Middleware } from "./middleware";
7
- export { loadRoutes } from "./route-loader";
8
- export {
9
- clearTrackedQueries,
10
- createErrorResponse,
11
- createMiddlewareErrorResponse,
12
- createNotFoundResponse,
13
- createValidationErrorResponse,
14
- getQueryShapeCounts,
15
- trackQuery
16
- } from "./error-handler";
17
- export { listRegisteredRoutes, routeParams } from "./stacks-router";
18
- export { isApiRequest, JSON_CONTENT_TYPE } from "./api-shape";
19
- export { rateLimit, rateLimitStatus, clearRateLimit } from "./rate-limit";
20
- export { PathParamError, safePathParam, sanitizePathParam } from "./path-sanitize";
21
- export { checkApplicationHealth, runHealthProbes } from "./health";
22
- export { stream } from "./stacks-router";
23
- export { signedUrl, signUrl, verifySignedUrl, verifySignedUrlMiddleware } from "./signed-url";
24
- export { EncryptedSessionStore } from "./encrypted-session-store";
25
- export {
26
- createSessionStore,
27
- createStacksSessionStore,
28
- DatabaseSessionStore,
29
- FileSessionStore,
30
- MemorySessionStore,
31
- RedisSessionStore
32
- } from "./session-factory";
33
- import("@stacksjs/database").then(({ setQueryTracker }) => {
34
- if (typeof setQueryTracker === "function") {
35
- const { trackQuery } = require("./error-handler");
36
- setQueryTracker(trackQuery);
37
- }
38
- }).catch(() => {});
1
+ var {require}=import.meta;import"./request-augmentation";export*from"@stacksjs/bun-router";export{assertRouteMiddlewareResolvable,clearMiddlewareCache,createStacksRouter,findUnresolvableRouteMiddleware,installMiddlewareHotReload,route,serve,serverResponse,url,warnOnMultipleRouterInstances}from"./stacks-router";export{cacheRequestQuery,getCurrentRequest,getTraceId,request,runWithRequest,setCurrentRequest,withTraceId}from"./request-context";export{Middleware}from"./middleware";export{loadRoutes}from"./route-loader";export{clearTrackedQueries,createErrorResponse,createMiddlewareErrorResponse,createNotFoundResponse,createValidationErrorResponse,getQueryShapeCounts,trackQuery}from"./error-handler";export{listRegisteredRoutes,routeParams}from"./stacks-router";export{isApiRequest,JSON_CONTENT_TYPE}from"./api-shape";export{rateLimit,rateLimitStatus,clearRateLimit}from"./rate-limit";export{PathParamError,safePathParam,sanitizePathParam}from"./path-sanitize";export{checkApplicationHealth,runHealthProbes}from"./health";export{stream}from"./stacks-router";export{signedUrl,signUrl,verifySignedUrl,verifySignedUrlMiddleware}from"./signed-url";export{EncryptedSessionStore}from"./encrypted-session-store";export{createSessionStore,createStacksSessionStore,DatabaseSessionStore,FileSessionStore,MemorySessionStore,RedisSessionStore}from"./session-factory";import("@stacksjs/database").then(({setQueryTracker})=>{if(typeof setQueryTracker==="function"){const{trackQuery}=require("./error-handler");setQueryTracker(trackQuery)}}).catch(()=>{});
@@ -1,23 +1 @@
1
- export class Middleware {
2
- name;
3
- priority;
4
- handle;
5
- constructor(config) {
6
- this.name = config.name;
7
- this.priority = config.priority ?? 10;
8
- this.handle = config.handle;
9
- }
10
- toRouterHandler() {
11
- const handle = this.handle.bind(this);
12
- return async (req, next) => {
13
- try {
14
- await handle(req);
15
- } catch (thrown) {
16
- if (thrown instanceof Response)
17
- return thrown;
18
- throw thrown;
19
- }
20
- return next();
21
- };
22
- }
23
- }
1
+ export class Middleware{name;priority;handle;constructor(config){this.name=config.name;this.priority=config.priority??10;this.handle=config.handle}toRouterHandler(){const handle=this.handle.bind(this);return async(req,next)=>{try{await handle(req)}catch(thrown){if(thrown instanceof Response)return thrown;throw thrown}return next()}}}
@@ -1,37 +1 @@
1
- export class PathParamError extends Error {
2
- reason;
3
- constructor(reason, value, context) {
4
- const ctx = context ? ` in ${context}` : "";
5
- super(`[router] Refusing to use ${JSON.stringify(value)} as a path parameter${ctx} \u2014 ${reason}`);
6
- this.name = "PathParamError";
7
- this.reason = reason;
8
- }
9
- }
10
- const CONTROL_CHARS = /[\u0000-\u001F\u007F]/;
11
- export function sanitizePathParam(value, options = {}) {
12
- if (typeof value !== "string")
13
- throw new PathParamError("not-string", value, options.context);
14
- if (value.length === 0)
15
- throw new PathParamError("empty", value, options.context);
16
- const maxLength = options.maxLength ?? 255;
17
- if (value.length > maxLength)
18
- throw new PathParamError("too-long", value, options.context);
19
- if (value.includes("\x00"))
20
- throw new PathParamError("null-byte", value, options.context);
21
- if (CONTROL_CHARS.test(value))
22
- throw new PathParamError("control-char", value, options.context);
23
- if (value.startsWith("/") || /^[A-Z]:[\\/]/i.test(value))
24
- throw new PathParamError("absolute-path", value, options.context);
25
- if (/(^|[\\/])\.\.([\\/]|$)/.test(value))
26
- throw new PathParamError("traversal", value, options.context);
27
- if (!options.allowSlashes && /[\\/]/.test(value))
28
- throw new PathParamError("traversal", value, options.context);
29
- return value;
30
- }
31
- export function safePathParam(value, options = {}) {
32
- try {
33
- return sanitizePathParam(value, options);
34
- } catch {
35
- return null;
36
- }
37
- }
1
+ export class PathParamError extends Error{reason;constructor(reason,value,context){const ctx=context?` in ${context}`:"";super(`[router] Refusing to use ${JSON.stringify(value)} as a path parameter${ctx} \u2014 ${reason}`);this.name="PathParamError";this.reason=reason}}const CONTROL_CHARS=/[\u0000-\u001F\u007F]/;export function sanitizePathParam(value,options={}){if(typeof value!=="string")throw new PathParamError("not-string",value,options.context);if(value.length===0)throw new PathParamError("empty",value,options.context);const maxLength=options.maxLength??255;if(value.length>maxLength)throw new PathParamError("too-long",value,options.context);if(value.includes("\x00"))throw new PathParamError("null-byte",value,options.context);if(CONTROL_CHARS.test(value))throw new PathParamError("control-char",value,options.context);if(value.startsWith("/")||/^[A-Z]:[\\/]/i.test(value))throw new PathParamError("absolute-path",value,options.context);if(/(^|[\\/])\.\.([\\/]|$)/.test(value))throw new PathParamError("traversal",value,options.context);if(!options.allowSlashes&&/[\\/]/.test(value))throw new PathParamError("traversal",value,options.context);return value}export function safePathParam(value,options={}){try{return sanitizePathParam(value,options)}catch{return null}}
@@ -1,73 +1 @@
1
- import { HttpError } from "@stacksjs/error-handling";
2
- import { RateLimitError, RateLimiter, defaultIdentity } from "ts-rate-limiter";
3
- import { getCurrentRequest } from "./request-context";
4
- const PERIOD_SECONDS = {
5
- second: 1,
6
- minute: 60,
7
- hour: 3600,
8
- day: 86400
9
- }, limiterCache = new Map;
10
- function getLimiter(max, windowMs) {
11
- const cacheKey = `${windowMs}:${max}`;
12
- let limiter = limiterCache.get(cacheKey);
13
- if (!limiter) {
14
- limiter = new RateLimiter({
15
- windowMs,
16
- maxRequests: max,
17
- algorithm: "fixed-window",
18
- standardHeaders: !1,
19
- legacyHeaders: !1
20
- });
21
- limiterCache.set(cacheKey, limiter);
22
- }
23
- return limiter;
24
- }
25
- function resolveIdentity(explicit) {
26
- if (explicit !== void 0)
27
- return explicit;
28
- const req = getCurrentRequest();
29
- return req ? defaultIdentity(req) : "anon";
30
- }
31
- export function rateLimit(key, max, options = {}) {
32
- const id = resolveIdentity(options.identity), bucketKey = `${key}:${id}`, run = async (windowMs) => {
33
- const limiter = getLimiter(max, windowMs);
34
- try {
35
- await limiter.enforce(bucketKey);
36
- } catch (err) {
37
- if (err instanceof RateLimitError)
38
- throw Object.assign(new HttpError(429, "Too many requests", {
39
- key,
40
- max,
41
- retryAfter: err.retryAfter
42
- }), { headers: err.toHeaders() });
43
- throw err;
44
- }
45
- };
46
- return {
47
- async per(period) {
48
- const seconds = PERIOD_SECONDS[period];
49
- if (!seconds)
50
- throw Error(`rateLimit().per: unknown period '${period}'`);
51
- await run(seconds * 1000);
52
- },
53
- async over(ttlSeconds) {
54
- if (!Number.isFinite(ttlSeconds) || ttlSeconds <= 0)
55
- throw Error(`rateLimit().over: ttl must be a positive number, got ${ttlSeconds}`);
56
- await run(ttlSeconds * 1000);
57
- }
58
- };
59
- }
60
- export async function rateLimitStatus(key, max, windowSeconds, options = {}) {
61
- const id = resolveIdentity(options.identity), bucketKey = `${key}:${id}`, result = await getLimiter(max, windowSeconds * 1000).peek(bucketKey);
62
- if (!result)
63
- return null;
64
- return {
65
- count: result.current,
66
- limit: result.limit,
67
- remaining: Math.max(0, result.limit - result.current)
68
- };
69
- }
70
- export async function clearRateLimit(key, max, windowSeconds, options = {}) {
71
- const id = resolveIdentity(options.identity), bucketKey = `${key}:${id}`;
72
- await getLimiter(max, windowSeconds * 1000).reset(bucketKey);
73
- }
1
+ import{HttpError}from"@stacksjs/error-handling";import{RateLimitError,RateLimiter,defaultIdentity}from"ts-rate-limiter";import{getCurrentRequest}from"./request-context";const PERIOD_SECONDS={second:1,minute:60,hour:3600,day:86400},limiterCache=new Map;function getLimiter(max,windowMs){const cacheKey=`${windowMs}:${max}`;let limiter=limiterCache.get(cacheKey);if(!limiter){limiter=new RateLimiter({windowMs,maxRequests:max,algorithm:"fixed-window",standardHeaders:!1,legacyHeaders:!1});limiterCache.set(cacheKey,limiter)}return limiter}function resolveIdentity(explicit){if(explicit!==void 0)return explicit;const req=getCurrentRequest();return req?defaultIdentity(req):"anon"}export function rateLimit(key,max,options={}){const id=resolveIdentity(options.identity),bucketKey=`${key}:${id}`,run=async(windowMs)=>{const limiter=getLimiter(max,windowMs);try{await limiter.enforce(bucketKey)}catch(err){if(err instanceof RateLimitError)throw Object.assign(new HttpError(429,"Too many requests",{key,max,retryAfter:err.retryAfter}),{headers:err.toHeaders()});throw err}};return{async per(period){const seconds=PERIOD_SECONDS[period];if(!seconds)throw Error(`rateLimit().per: unknown period '${period}'`);await run(seconds*1000)},async over(ttlSeconds){if(!Number.isFinite(ttlSeconds)||ttlSeconds<=0)throw Error(`rateLimit().over: ttl must be a positive number, got ${ttlSeconds}`);await run(ttlSeconds*1000)}}}export async function rateLimitStatus(key,max,windowSeconds,options={}){const id=resolveIdentity(options.identity),bucketKey=`${key}:${id}`,result=await getLimiter(max,windowSeconds*1000).peek(bucketKey);if(!result)return null;return{count:result.current,limit:result.limit,remaining:Math.max(0,result.limit-result.current)}}export async function clearRateLimit(key,max,windowSeconds,options={}){const id=resolveIdentity(options.identity),bucketKey=`${key}:${id}`;await getLimiter(max,windowSeconds*1000).reset(bucketKey)}