cpeak 2.5.0 → 2.7.0
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/README.md +300 -2
- package/dist/index.d.ts +88 -16
- package/dist/index.js +653 -165
- package/dist/index.js.map +1 -1
- package/lib/index.ts +229 -158
- package/lib/internal/compression.ts +180 -0
- package/lib/internal/types.ts +10 -0
- package/lib/types.ts +20 -5
- package/lib/utils/auth.ts +148 -0
- package/lib/utils/cookieParser.ts +179 -0
- package/lib/utils/cors.ts +109 -0
- package/lib/utils/index.ts +15 -1
- package/lib/utils/render.ts +7 -0
- package/lib/utils/serveStatic.ts +16 -5
- package/lib/utils/swagger.ts +31 -0
- package/lib/utils/types.ts +51 -0
- package/package.json +4 -4
- /package/lib/utils/{paseJSON.ts → parseJSON.ts} +0 -0
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../lib/index.ts","../lib/utils/serveStatic.ts","../lib/utils/paseJSON.ts","../lib/utils/render.ts"],"sourcesContent":["import http from \"node:http\";\nimport fs from \"node:fs/promises\";\nimport { createReadStream } from \"node:fs\";\nimport { pipeline } from \"node:stream/promises\";\n\nimport type {\n StringMap,\n CpeakRequest,\n CpeakResponse,\n Middleware,\n RouteMiddleware,\n Handler,\n RoutesMap\n} from \"./types\";\n\n// A utility function to create an error with a custom stack trace\nexport function frameworkError(\n message: string,\n skipFn: Function,\n code?: string,\n status?: number\n) {\n const err = new Error(message) as Error & {\n code?: string;\n cpeak_err?: boolean;\n };\n Error.captureStackTrace(err, skipFn);\n\n err.cpeak_err = true;\n\n if (code) err.code = code;\n if (status) (err as any).status = status;\n\n return err;\n}\n\nexport enum ErrorCode {\n MISSING_MIME = \"CPEAK_ERR_MISSING_MIME\",\n FILE_NOT_FOUND = \"CPEAK_ERR_FILE_NOT_FOUND\",\n NOT_A_FILE = \"CPEAK_ERR_NOT_A_FILE\",\n SEND_FILE_FAIL = \"CPEAK_ERR_SEND_FILE_FAIL\",\n INVALID_JSON = \"CPEAK_ERR_INVALID_JSON\",\n PAYLOAD_TOO_LARGE = \"CPEAK_ERR_PAYLOAD_TOO_LARGE\"\n}\n\nclass CpeakIncomingMessage extends http.IncomingMessage {\n // We define body and params here for better V8 optimization (not changing the shape of the object at runtime)\n public body: any = undefined;\n public params: StringMap = {};\n\n private _query?: StringMap;\n\n // Parse the URL parameters (like /users?key1=value1&key2=value2)\n // We will call this query to be more familiar with other node.js frameworks.\n // This is a getter method (accessed like a property)\n get query(): StringMap {\n // This way if a developer writes req.query multiple times, we don't parse it multiple times\n if (this._query) return this._query;\n\n const url = this.url || \"\";\n const qIndex = url.indexOf(\"?\");\n\n if (qIndex === -1) {\n this._query = {};\n } else {\n const searchParams = new URLSearchParams(url.substring(qIndex + 1));\n this._query = Object.fromEntries(searchParams.entries());\n }\n\n return this._query;\n }\n}\n\nclass CpeakServerResponse extends http.ServerResponse<CpeakIncomingMessage> {\n // Send a file back to the client\n async sendFile(path: string, mime: string) {\n if (!mime) {\n throw frameworkError(\n 'MIME type is missing. Use res.sendFile(path, \"mime-type\").',\n this.sendFile,\n ErrorCode.MISSING_MIME\n );\n }\n\n try {\n const stat = await fs.stat(path);\n if (!stat.isFile()) {\n throw frameworkError(\n `Not a file: ${path}`,\n this.sendFile,\n ErrorCode.NOT_A_FILE\n );\n }\n\n this.setHeader(\"Content-Type\", mime);\n this.setHeader(\"Content-Length\", String(stat.size));\n\n // Properly propagate stream errors and respect backpressure\n await pipeline(createReadStream(path), this);\n } catch (err: any) {\n if (err?.code === \"ENOENT\") {\n throw frameworkError(\n `File not found: ${path}`,\n this.sendFile,\n ErrorCode.FILE_NOT_FOUND\n );\n }\n\n throw frameworkError(\n `Failed to send file: ${path}`,\n this.sendFile,\n ErrorCode.SEND_FILE_FAIL\n );\n }\n }\n\n // Set the status code of the response\n status(code: number) {\n this.statusCode = code;\n return this;\n }\n\n // Redirects to a new URL\n redirect(location: string) {\n this.writeHead(302, { Location: location });\n this.end();\n return this;\n }\n\n // Send a json data back to the client (for small json data, less than the highWaterMark)\n json(data: any) {\n // This is only good for bodies that their size is less than the highWaterMark value\n this.setHeader(\"Content-Type\", \"application/json\");\n this.end(JSON.stringify(data));\n }\n}\n\nclass Cpeak {\n private server: http.Server<\n typeof CpeakIncomingMessage,\n typeof CpeakServerResponse\n >;\n private routes: RoutesMap;\n private middleware: Middleware[];\n private _handleErr?: (\n err: unknown,\n req: CpeakRequest,\n res: CpeakResponse\n ) => void;\n\n constructor() {\n this.server = http.createServer({\n IncomingMessage: CpeakIncomingMessage,\n ServerResponse: CpeakServerResponse\n });\n this.routes = {};\n this.middleware = [];\n\n this.server.on(\"request\", async (req: CpeakRequest, res: CpeakResponse) => {\n // Get the url without the URL parameters (query strings)\n const qIndex = req.url?.indexOf(\"?\");\n const urlWithoutQueries =\n qIndex === -1 ? req.url || \"\" : req.url?.substring(0, qIndex);\n\n // Run all the specific middleware functions for that router only and then run the handler\n const runHandler = async (\n req: CpeakRequest,\n res: CpeakResponse,\n middleware: RouteMiddleware[],\n cb: Handler,\n index: number\n ) => {\n // Our exit point...\n if (index === middleware.length) {\n // Call the route handler with the modified req and res objects.\n // Also handle the promise errors by passing them to the handleErr to save developers from having to manually wrap every handler in try catch.\n try {\n await cb(req, res, (error) => {\n res.setHeader(\"Connection\", \"close\");\n this._handleErr?.(error, req, res);\n });\n } catch (error) {\n res.setHeader(\"Connection\", \"close\");\n this._handleErr?.(error, req, res);\n }\n } else {\n // Handle the promise errors by passing them to the handleErr to save developers from having to manually wrap every handler middleware in try catch.\n try {\n await middleware[index](\n req,\n res,\n // The next function\n async (error) => {\n // this function only accepts an error argument to be more compatible with NPM modules that are built for express\n if (error) {\n res.setHeader(\"Connection\", \"close\");\n return this._handleErr?.(error, req, res);\n }\n await runHandler(req, res, middleware, cb, index + 1);\n },\n // Error handler for a route middleware\n (error) => {\n res.setHeader(\"Connection\", \"close\");\n this._handleErr?.(error, req, res);\n }\n );\n } catch (error) {\n res.setHeader(\"Connection\", \"close\");\n this._handleErr?.(error, req, res);\n }\n }\n };\n\n // Run all the middleware functions (beforeEach functions) before we run the corresponding route\n const runMiddleware = async (\n req: CpeakRequest,\n res: CpeakResponse,\n middleware: Middleware[],\n index: number\n ) => {\n // Our exit point...\n if (index === middleware.length) {\n const routes = this.routes[req.method?.toLowerCase() || \"\"];\n if (routes && typeof routes[Symbol.iterator] === \"function\")\n for (const route of routes) {\n const match = urlWithoutQueries?.match(route.regex);\n\n if (match) {\n // Parse the URL path variables from the matched route (like /users/:id)\n const pathVariables = this.#extractPathVariables(\n route.path,\n match\n );\n\n // We will call this params to be more familiar with other node.js frameworks.\n req.params = pathVariables;\n\n return await runHandler(\n req,\n res,\n route.middleware,\n route.cb,\n 0\n );\n }\n }\n\n // If the requested route dose not exist, return 404\n return res\n .status(404)\n .json({ error: `Cannot ${req.method} ${urlWithoutQueries}` });\n } else {\n try {\n await middleware[index](req, res, async (err?: unknown) => {\n if (err) {\n res.setHeader(\"Connection\", \"close\");\n return this._handleErr?.(err, req, res);\n }\n await runMiddleware(req, res, middleware, index + 1);\n });\n } catch (error) {\n res.setHeader(\"Connection\", \"close\");\n this._handleErr?.(error, req, res);\n }\n }\n };\n\n await runMiddleware(req, res, this.middleware, 0);\n });\n }\n\n route(method: string, path: string, ...args: (RouteMiddleware | Handler)[]) {\n if (!this.routes[method]) this.routes[method] = [];\n\n // The last argument should always be our handler\n const cb = args.pop() as Handler;\n\n if (!cb || typeof cb !== \"function\") {\n throw new Error(\"Route definition must include a handler\");\n }\n\n // Rest will be our middleware functions\n const middleware = args.flat() as RouteMiddleware[];\n\n const regex = this.#pathToRegex(path);\n this.routes[method].push({ path, regex, middleware, cb });\n }\n\n beforeEach(cb: Middleware) {\n this.middleware.push(cb);\n }\n\n handleErr(cb: (err: unknown, req: CpeakRequest, res: CpeakResponse) => void) {\n this._handleErr = cb;\n }\n\n listen(port: number, cb?: () => void) {\n return this.server.listen(port, cb);\n }\n\n close(cb?: (err?: Error) => void) {\n this.server.close(cb);\n }\n\n // ------------------------------\n // PRIVATE METHODS:\n // ------------------------------\n #pathToRegex(path: string) {\n const paramNames: string[] = [];\n const regexString =\n \"^\" +\n path.replace(/:\\w+/g, (match, offset) => {\n paramNames.push(match.slice(1));\n return \"([^/]+)\";\n }) +\n \"$\";\n\n const regex = new RegExp(regexString);\n return regex;\n }\n\n #extractPathVariables(path: string, match: RegExpMatchArray) {\n // Extract path url variable values from the matched route\n const paramNames = (path.match(/:\\w+/g) || []).map((param) =>\n param.slice(1)\n );\n const params: StringMap = {};\n paramNames.forEach((name, index) => {\n params[name] = match[index + 1];\n });\n return params;\n }\n}\n\n// Util functions\nexport { serveStatic } from \"./utils/serveStatic.js\";\nexport { parseJSON } from \"./utils/paseJSON.js\";\nexport { render } from \"./utils/render.js\";\n\nexport type {\n Cpeak,\n CpeakRequest,\n CpeakResponse,\n Next,\n HandleErr,\n Middleware,\n RouteMiddleware,\n Handler,\n RoutesMap\n} from \"./types\";\n\nexport default function cpeak() {\n return new Cpeak();\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\n\nimport type { StringMap, CpeakRequest, CpeakResponse, Next } from \"../types\";\n\nconst MIME_TYPES: StringMap = {\n html: \"text/html\",\n css: \"text/css\",\n js: \"application/javascript\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n png: \"image/png\",\n svg: \"image/svg+xml\",\n txt: \"text/plain\",\n eot: \"application/vnd.ms-fontobject\",\n otf: \"font/otf\",\n ttf: \"font/ttf\",\n woff: \"font/woff\",\n woff2: \"font/woff2\",\n};\n\nconst serveStatic = (folderPath: string, newMimeTypes?: StringMap) => {\n // For new user defined mime types\n if (newMimeTypes) {\n Object.assign(MIME_TYPES, newMimeTypes);\n }\n\n function processFolder(folderPath: string, parentFolder: string) {\n const staticFiles: string[] = [];\n\n // Read the contents of the folder\n const files = fs.readdirSync(folderPath);\n\n // Loop through the files and subfolders\n for (const file of files) {\n const fullPath = path.join(folderPath, file);\n\n // Check if it's a directory\n if (fs.statSync(fullPath).isDirectory()) {\n // If it's a directory, recursively process it\n const subfolderFiles = processFolder(fullPath, parentFolder);\n staticFiles.push(...subfolderFiles);\n } else {\n // If it's a file, add it to the array\n const relativePath = path.relative(parentFolder, fullPath);\n const fileExtension = path.extname(file).slice(1);\n if (MIME_TYPES[fileExtension]) staticFiles.push(\"/\" + relativePath);\n }\n }\n\n return staticFiles;\n }\n\n const filesArrayToFilesMap = (filesArray: string[]) => {\n const filesMap: Record<string, { path: string; mime: string }> = {};\n for (const file of filesArray) {\n const fileExtension = path.extname(file).slice(1);\n filesMap[file] = {\n path: folderPath + file,\n mime: MIME_TYPES[fileExtension],\n };\n }\n return filesMap;\n };\n\n // Start processing the folder\n const filesMap = filesArrayToFilesMap(processFolder(folderPath, folderPath));\n\n return function (req: CpeakRequest, res: CpeakResponse, next: Next) {\n const url = req.url;\n if (typeof url !== \"string\") return next();\n\n if (Object.prototype.hasOwnProperty.call(filesMap, url)) {\n const fileRoute = filesMap[url];\n return res.sendFile(fileRoute.path, fileRoute.mime);\n }\n\n next();\n };\n};\n\nexport { serveStatic };\n","import type { CpeakRequest, CpeakResponse, Next } from \"../types\";\nimport { Buffer } from \"node:buffer\";\nimport { frameworkError, ErrorCode } from \"../index\";\n\n// Check if Content-Type is JSON\nfunction isJSON(contentType: string | undefined) {\n if (!contentType) return false;\n if (contentType === \"application/json\") return true;\n return (\n contentType.startsWith(\"application/json\") || contentType.includes(\"+json\")\n );\n}\n\n// Parsing JSON\nconst parseJSON = (options: { limit?: number } = {}) => {\n // Default limit to 1MB\n const limit = options.limit || 1024 * 1024;\n\n return (req: CpeakRequest, res: CpeakResponse, next: Next) => {\n if (!isJSON(req.headers[\"content-type\"])) return next();\n\n const chunks: Buffer[] = [];\n let bytesReceived = 0;\n\n const onData = (chunk: Buffer) => {\n bytesReceived += chunk.length;\n\n // To prevent Denial of Service (DoS) attacks, enforce a maximum body size\n if (bytesReceived > limit) {\n // Stop listening to data\n req.pause();\n\n // Remove listeners so we don't trigger 'end' or more 'data'\n req.removeListener(\"data\", onData);\n req.removeListener(\"end\", onEnd);\n\n next(\n frameworkError(\n \"JSON body too large\",\n onData,\n ErrorCode.PAYLOAD_TOO_LARGE,\n 413 // HTTP 413 Payload Too Large\n )\n );\n\n return;\n }\n\n chunks.push(chunk);\n };\n\n const onEnd = () => {\n try {\n // For better performance, we concat buffers once, then convert to string\n // Optimization: If only one chunk exists, avoid the memory copy of concat\n const rawBody =\n chunks.length === 1\n ? chunks[0].toString(\"utf-8\")\n : Buffer.concat(chunks).toString(\"utf-8\");\n\n // Handle empty body case\n req.body = rawBody ? JSON.parse(rawBody) : {};\n\n next();\n } catch (err) {\n // Handle Invalid JSON without crashing\n next(\n frameworkError(\n \"Invalid JSON format\",\n onEnd,\n ErrorCode.INVALID_JSON,\n 400 // HTTP 400 Bad Request\n )\n );\n }\n };\n\n req.on(\"data\", onData);\n req.on(\"end\", onEnd);\n };\n};\n\nexport { parseJSON };\n","import fs from \"node:fs/promises\";\nimport { frameworkError } from \"../\";\nimport type { CpeakRequest, CpeakResponse, Next } from \"../types\";\n\nfunction renderTemplate(\n templateStr: string,\n data: Record<string, unknown>\n): string {\n // Initialize variables\n let result: (string | unknown)[] = [];\n\n let currentIndex = 0;\n\n while (currentIndex < templateStr.length) {\n // Find the next opening placeholder\n const startIdx = templateStr.indexOf(\"{{\", currentIndex);\n if (startIdx === -1) {\n // No more placeholders, push the remaining string\n result.push(templateStr.slice(currentIndex));\n break;\n }\n\n // Push the part before the placeholder\n result.push(templateStr.slice(currentIndex, startIdx));\n\n // Find the closing placeholder\n const endIdx = templateStr.indexOf(\"}}\", startIdx);\n if (endIdx === -1) {\n // No closing brace found, treat the rest as plain text\n result.push(templateStr.slice(startIdx));\n break;\n }\n\n // Extract the variable name\n const varName = templateStr.slice(startIdx + 2, endIdx).trim();\n\n // Replace the variable with its value from the data, or use an empty string if not found\n const replacement = data[varName] !== undefined ? data[varName] : \"\";\n\n // Push the replacement to the result array\n result.push(replacement);\n\n // Move the index past the current closing placeholder\n currentIndex = endIdx + 2;\n }\n\n // Join all parts into a final string\n return result.join(\"\");\n}\n\n// Errors to return: recommend to not render files larger than 100KB\n// To Explore: Doing the operation in C++ and return the data as stream back to the client\n// @TODO: remove the file from static map\n// @TODO: escape the string to prevent XSS\n// @TODO: add another {{{ }}} option to not escape the string\nconst render = () => {\n return function (req: CpeakRequest, res: CpeakResponse, next: Next): void {\n res.render = async (\n path: string,\n data: Record<string, unknown>,\n mime: string\n ) => {\n // check if mime is specified, if not return an error\n if (!mime) {\n throw frameworkError(\n `MIME type is missing. You called res.render(\"${path}\", ...) but forgot to provide the third \"mime\" argument.`,\n res.render\n );\n }\n\n let fileStr = await fs.readFile(path, \"utf-8\");\n const finalStr = renderTemplate(fileStr, data);\n res.setHeader(\"Content-Type\", mime);\n res.end(finalStr);\n };\n\n next();\n };\n};\n\nexport { render };\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,OAAOA,SAAQ;AACf,SAAS,wBAAwB;AACjC,SAAS,gBAAgB;;;ACHzB,OAAO,QAAQ;AACf,OAAO,UAAU;AAIjB,IAAM,aAAwB;AAAA,EAC5B,MAAM;AAAA,EACN,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,KAAK;AAAA,EACL,MAAM;AAAA,EACN,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,MAAM;AAAA,EACN,OAAO;AACT;AAEA,IAAM,cAAc,CAAC,YAAoB,iBAA6B;AAEpE,MAAI,cAAc;AAChB,WAAO,OAAO,YAAY,YAAY;AAAA,EACxC;AAEA,WAAS,cAAcC,aAAoB,cAAsB;AAC/D,UAAM,cAAwB,CAAC;AAG/B,UAAM,QAAQ,GAAG,YAAYA,WAAU;AAGvC,eAAW,QAAQ,OAAO;AACxB,YAAM,WAAW,KAAK,KAAKA,aAAY,IAAI;AAG3C,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UAAU;AAClC,aAAO,IAAI,IAAI,MAAM,QAAQ,CAAC;AAAA,IAChC,CAAC;AACD,WAAO;AAAA,EACT;AACF;AAmBe,SAAR,QAAyB;AAC9B,SAAO,IAAI,MAAM;AACnB;","names":["fs","folderPath","filesMap","fs","path","fs","ErrorCode","path","fs","req","res"]}
|
|
1
|
+
{"version":3,"sources":["../lib/index.ts","../lib/utils/compression.ts","../lib/utils/parseJSON.ts","../lib/utils/serveStatic.ts","../lib/utils/render.ts","../lib/utils/swagger.ts","../lib/utils/auth.ts","../lib/utils/cookieParser.ts","../lib/utils/cors.ts"],"sourcesContent":["import http from \"node:http\";\nimport fs from \"node:fs/promises\";\nimport { createReadStream } from \"node:fs\";\nimport { pipeline } from \"node:stream/promises\";\nimport type { Readable } from \"node:stream\";\nimport type { Buffer } from \"node:buffer\";\n\nimport type {\n StringMap,\n CpeakOptions,\n CpeakRequest,\n CpeakResponse,\n Middleware,\n RouteMiddleware,\n Handler,\n RoutesMap\n} from \"./types\";\nimport {\n resolveCompressionOptions,\n compressAndSend,\n type ResolvedCompressionConfig\n} from \"./utils/compression\";\n\n// A utility function to create an error with a custom stack trace\nexport function frameworkError(\n message: string,\n skipFn: Function,\n code?: string,\n status?: number\n) {\n const err = new Error(message) as Error & {\n code?: string;\n cpeak_err?: boolean;\n };\n Error.captureStackTrace(err, skipFn);\n\n err.cpeak_err = true;\n\n if (code) err.code = code;\n if (status) (err as any).status = status;\n\n return err;\n}\n\nexport enum ErrorCode {\n MISSING_MIME = \"CPEAK_ERR_MISSING_MIME\",\n FILE_NOT_FOUND = \"CPEAK_ERR_FILE_NOT_FOUND\",\n NOT_A_FILE = \"CPEAK_ERR_NOT_A_FILE\",\n SEND_FILE_FAIL = \"CPEAK_ERR_SEND_FILE_FAIL\",\n INVALID_JSON = \"CPEAK_ERR_INVALID_JSON\",\n PAYLOAD_TOO_LARGE = \"CPEAK_ERR_PAYLOAD_TOO_LARGE\",\n WEAK_SECRET = \"CPEAK_ERR_WEAK_SECRET\",\n COMPRESSION_NOT_ENABLED = \"CPEAK_ERR_COMPRESSION_NOT_ENABLED\"\n}\n\n// Resolve the compression config attached at construction time. Reaches the\n// http.Server through the response's socket — same Server object every\n// request, so no per-response state to read.\nfunction compressionConfigFor(\n res: http.ServerResponse\n): ResolvedCompressionConfig | undefined {\n return (res.socket as any)?.server?._cpeakCompression;\n}\n\nexport class CpeakIncomingMessage extends http.IncomingMessage {\n // We define body and params here for better V8 optimization (not changing the shape of the object at runtime)\n public body: any = undefined;\n public params: StringMap = {};\n\n #query?: StringMap;\n\n // Parse the URL parameters (like /users?key1=value1&key2=value2)\n // We will call this query to be more familiar with other node.js frameworks.\n // This is a getter method (accessed like a property)\n get query(): StringMap {\n // This way if a developer writes req.query multiple times, we don't parse it multiple times\n if (this.#query) return this.#query;\n\n const url = this.url || \"\";\n const qIndex = url.indexOf(\"?\");\n\n if (qIndex === -1) {\n this.#query = {};\n } else {\n const searchParams = new URLSearchParams(url.substring(qIndex + 1));\n this.#query = Object.fromEntries(searchParams.entries());\n }\n\n return this.#query;\n }\n}\n\nexport class CpeakServerResponse extends http.ServerResponse<CpeakIncomingMessage> {\n // Send a file back to the client\n async sendFile(path: string, mime: string) {\n if (!mime) {\n throw frameworkError(\n 'MIME type is missing. Use res.sendFile(path, \"mime-type\").',\n this.sendFile,\n ErrorCode.MISSING_MIME\n );\n }\n\n try {\n const stat = await fs.stat(path);\n if (!stat.isFile()) {\n throw frameworkError(\n `Not a file: ${path}`,\n this.sendFile,\n ErrorCode.NOT_A_FILE\n );\n }\n\n const config = compressionConfigFor(this);\n if (config) {\n await compressAndSend(this, mime, createReadStream(path), config, stat.size);\n return;\n }\n\n this.setHeader(\"Content-Type\", mime);\n this.setHeader(\"Content-Length\", String(stat.size));\n\n // Properly propagate stream errors and respect backpressure\n await pipeline(createReadStream(path), this);\n } catch (err: any) {\n if (err?.code === \"ENOENT\") {\n throw frameworkError(\n `File not found: ${path}`,\n this.sendFile,\n ErrorCode.FILE_NOT_FOUND\n );\n }\n\n throw frameworkError(\n `Failed to send file: ${path}`,\n this.sendFile,\n ErrorCode.SEND_FILE_FAIL\n );\n }\n }\n\n // Set the status code of the response\n status(code: number) {\n this.statusCode = code;\n return this;\n }\n\n // Set the Content-Disposition header to prompt the user to download a file\n attachment(filename?: string) {\n const contentDisposition = filename\n ? `attachment; filename=\"${filename}\"`\n : \"attachment\";\n this.setHeader(\"Content-Disposition\", contentDisposition);\n return this;\n }\n\n // Redirects to a new URL\n redirect(location: string) {\n this.writeHead(302, { Location: location });\n this.end();\n }\n\n // Send a json data back to the client. Sync hot path when compression is\n // off — no Promise allocation, no microtask. Branches into compressAndSend\n // (async) when compression was enabled at cpeak() construction.\n json(data: any): void | Promise<void> {\n const body = JSON.stringify(data);\n const config = compressionConfigFor(this);\n if (config) {\n return compressAndSend(this, \"application/json\", body, config);\n }\n this.setHeader(\"Content-Type\", \"application/json\");\n this.end(body);\n }\n\n // Explicit compression entry point. Throws if compression wasn't configured —\n // the developer asked to compress but the framework was never told to.\n compress(\n mime: string,\n body: Buffer | string | Readable,\n size?: number\n ): Promise<void> {\n const config = compressionConfigFor(this);\n if (!config) {\n throw frameworkError(\n \"compression is not enabled. Pass `compression` to cpeak({ compression: true | { ... } }) to use res.compress.\",\n this.compress,\n ErrorCode.COMPRESSION_NOT_ENABLED\n );\n }\n return compressAndSend(this, mime, body, config, size);\n }\n}\n\nexport class Cpeak {\n #server: http.Server<typeof CpeakIncomingMessage, typeof CpeakServerResponse>;\n #routes: RoutesMap;\n #middleware: Middleware[];\n #handleErr?: (err: unknown, req: CpeakRequest, res: CpeakResponse) => void;\n\n constructor(options: CpeakOptions = {}) {\n this.#server = http.createServer({\n IncomingMessage: CpeakIncomingMessage,\n ServerResponse: CpeakServerResponse\n });\n this.#routes = {};\n this.#middleware = [];\n\n // Stash resolved compression config on the http.Server. Set once at\n // construction; responses read it via res.socket.server. No per-request\n // mutation, no middleware traversal.\n if (options.compression) {\n (this.#server as any)._cpeakCompression = resolveCompressionOptions(\n options.compression\n );\n }\n\n this.#server.on(\n \"request\",\n async (req: CpeakRequest, res: CpeakResponse) => {\n // Get the url without the URL parameters (query strings)\n const qIndex = req.url?.indexOf(\"?\");\n const urlWithoutQueries =\n qIndex === -1 ? req.url || \"\" : req.url?.substring(0, qIndex);\n\n const dispatchError = (error: unknown) => {\n if (res.headersSent) {\n req.socket?.destroy();\n return;\n }\n res.setHeader(\"Connection\", \"close\");\n this.#handleErr?.(error, req, res);\n };\n\n // Run all the specific middleware functions for that router only and then run the handler\n const runHandler = async (\n req: CpeakRequest,\n res: CpeakResponse,\n middleware: RouteMiddleware[],\n cb: Handler,\n index: number\n ) => {\n // Our exit point...\n if (index === middleware.length) {\n // Call the route handler with the modified req and res objects.\n // Also handle the promise errors by passing them to the handleErr to save developers from having to manually wrap every handler in try catch.\n try {\n await cb(req, res, dispatchError);\n } catch (error) {\n dispatchError(error);\n }\n } else {\n // Handle the promise errors by passing them to the handleErr to save developers from having to manually wrap every handler middleware in try catch.\n try {\n await middleware[index](\n req,\n res,\n // The next function\n async (error) => {\n // this function only accepts an error argument to be more compatible with NPM modules that are built for express\n if (error) {\n return dispatchError(error);\n }\n await runHandler(req, res, middleware, cb, index + 1);\n },\n // Error handler for a route middleware\n dispatchError\n );\n } catch (error) {\n dispatchError(error);\n }\n }\n };\n\n // Run all the middleware functions (beforeEach functions) before we run the corresponding route\n const runMiddleware = async (\n req: CpeakRequest,\n res: CpeakResponse,\n middleware: Middleware[],\n index: number\n ) => {\n // Our exit point...\n if (index === middleware.length) {\n const routes = this.#routes[req.method?.toLowerCase() || \"\"];\n if (routes && typeof routes[Symbol.iterator] === \"function\")\n for (const route of routes) {\n const match = urlWithoutQueries?.match(route.regex);\n\n if (match) {\n // Parse the URL path variables from the matched route (like /users/:id)\n const pathVariables = this.#extractPathVariables(\n route.path,\n match\n );\n\n // We will call this params to be more familiar with other node.js frameworks.\n req.params = pathVariables;\n\n return await runHandler(\n req,\n res,\n route.middleware,\n route.cb,\n 0\n );\n }\n }\n\n // If the requested route dose not exist, return 404\n return res\n .status(404)\n .json({ error: `Cannot ${req.method} ${urlWithoutQueries}` });\n } else {\n try {\n await middleware[index](req, res, async (err?: unknown) => {\n if (err) {\n return dispatchError(err);\n }\n await runMiddleware(req, res, middleware, index + 1);\n });\n } catch (error) {\n dispatchError(error);\n }\n }\n };\n\n await runMiddleware(req, res, this.#middleware, 0);\n }\n );\n }\n\n route(method: string, path: string, ...args: (RouteMiddleware | Handler)[]) {\n if (!this.#routes[method]) this.#routes[method] = [];\n\n // The last argument should always be our handler\n const cb = args.pop() as Handler;\n\n if (!cb || typeof cb !== \"function\") {\n throw new Error(\"Route definition must include a handler\");\n }\n\n // Rest will be our middleware functions\n const middleware = args.flat() as RouteMiddleware[];\n\n const regex = this.#pathToRegex(path);\n this.#routes[method].push({ path, regex, middleware, cb });\n }\n\n beforeEach(cb: Middleware) {\n this.#middleware.push(cb);\n }\n\n handleErr(cb: (err: unknown, req: CpeakRequest, res: CpeakResponse) => void) {\n this.#handleErr = cb;\n }\n\n listen(port: number, cb?: () => void) {\n return this.#server.listen(port, cb);\n }\n\n address() {\n return this.#server.address();\n }\n\n close(cb?: (err?: Error) => void) {\n this.#server.close(cb);\n }\n\n // ------------------------------\n // PRIVATE METHODS:\n // ------------------------------\n #pathToRegex(path: string) {\n const regexString =\n \"^\" + path.replace(/:\\w+/g, \"([^/]+)\").replace(/\\*/g, \".*\") + \"$\";\n\n return new RegExp(regexString);\n }\n\n #extractPathVariables(path: string, match: RegExpMatchArray) {\n // Extract path url variable values from the matched route\n const paramNames = (path.match(/:\\w+/g) || []).map((param) =>\n param.slice(1)\n );\n const params: StringMap = {};\n paramNames.forEach((name, index) => {\n params[name] = match[index + 1];\n });\n return params;\n }\n}\n\n// Util functions\nexport {\n serveStatic,\n parseJSON,\n render,\n swagger,\n auth,\n hashPassword,\n verifyPassword,\n cookieParser,\n cors\n} from \"./utils\";\nexport type {\n AuthOptions,\n PbkdfOptions,\n CookieOptions,\n CorsOptions,\n CompressionOptions\n} from \"./utils/types\";\n\nexport type {\n CpeakOptions,\n CpeakRequest,\n CpeakResponse,\n Next,\n HandleErr,\n Middleware,\n RouteMiddleware,\n Handler,\n RoutesMap\n} from \"./types\";\n\nexport default function cpeak(options?: CpeakOptions): Cpeak {\n return new Cpeak(options);\n}\n","import zlib from \"node:zlib\";\nimport { Readable } from \"node:stream\";\nimport { Buffer } from \"node:buffer\";\nimport { pipeline } from \"node:stream/promises\";\nimport type { Transform } from \"node:stream\";\nimport type { ServerResponse } from \"node:http\";\nimport type { CompressionOptions } from \"./types\";\n\ntype Encoding = \"br\" | \"gzip\" | \"deflate\";\n\nexport type ResolvedCompressionConfig = Required<CompressionOptions>;\n\nconst COMPRESSIBLE_TYPE = /text|json|javascript|css|xml|svg/i;\nconst NO_TRANSFORM = /(?:^|,)\\s*no-transform\\s*(?:,|$)/i;\n\n// Parse Accept-Encoding and pick a compression algorithm the server supports.\n// Handles q=0 to explicitly disable an algorithm. Server preference is fixed: br > gzip > deflate.\nfunction pickEncoding(header: string): Encoding | null {\n if (!header) return null;\n\n const accepted: Record<string, number> = {};\n let wildcard: number | undefined;\n\n for (const part of header.split(\",\")) {\n const [rawName, ...params] = part.trim().split(\";\");\n const name = rawName.trim().toLowerCase();\n if (!name) continue;\n\n let q = 1;\n for (const p of params) {\n const m = p.trim().match(/^q=([\\d.]+)$/i);\n if (m) q = Number(m[1]);\n }\n if (Number.isNaN(q)) q = 0;\n\n if (name === \"*\") wildcard = q;\n else accepted[name] = q;\n }\n\n const tryPick = (enc: Encoding): boolean => {\n const q = enc in accepted ? accepted[enc] : wildcard;\n return q !== undefined && q > 0;\n };\n\n if (tryPick(\"br\")) return \"br\";\n if (tryPick(\"gzip\")) return \"gzip\";\n if (tryPick(\"deflate\")) return \"deflate\";\n return null;\n}\n\n// Handling the Vary HTTP header\nfunction appendVary(res: ServerResponse, value: string) {\n const existing = res.getHeader(\"Vary\");\n if (!existing) return res.setHeader(\"Vary\", value);\n const current = String(existing)\n .split(\",\")\n .map((s) => s.trim())\n .filter(Boolean);\n if (\n current.includes(\"*\") ||\n current.some((v) => v.toLowerCase() === value.toLowerCase())\n )\n return;\n res.setHeader(\"Vary\", [...current, value].join(\", \"));\n}\n\n// Brotli options. Zlib uses 11 (max), which is really slow for live\n// responses. We go with 4 unless the developer specifies otherwise.\nfunction brotliOptsFor(config: ResolvedCompressionConfig): zlib.BrotliOptions {\n const userBrotli = config.brotli || {};\n return {\n ...userBrotli,\n params: {\n [zlib.constants.BROTLI_PARAM_QUALITY]: 4,\n ...(userBrotli.params || {})\n }\n };\n}\n\nfunction createCompressorStream(\n encoding: Encoding,\n config: ResolvedCompressionConfig\n): Transform {\n if (encoding === \"br\")\n return zlib.createBrotliCompress(brotliOptsFor(config));\n if (encoding === \"gzip\") return zlib.createGzip(config.gzip);\n return zlib.createDeflate(config.deflate);\n}\n\n// Decides what to do with this response\nfunction negotiate(\n res: ServerResponse,\n mime: string,\n size: number,\n config: ResolvedCompressionConfig\n): { encoding: Encoding | null; eligible: boolean } {\n // Whether this content type is worth trying to compress at all.\n // Some types are already compressed and don't compress well.\n if (!COMPRESSIBLE_TYPE.test(mime)) return { encoding: null, eligible: false };\n\n if (res.req?.method === \"HEAD\") return { encoding: null, eligible: false };\n\n // RFC specification: don't transform responses that ask not to be transformed.\n const cc = res.getHeader(\"Cache-Control\");\n if (cc && NO_TRANSFORM.test(String(cc)))\n return { encoding: null, eligible: false };\n\n const existing = res.getHeader(\"Content-Encoding\");\n if (existing && existing !== \"identity\")\n return { encoding: null, eligible: false };\n\n if (size < config.threshold) return { encoding: null, eligible: true };\n\n const encoding = pickEncoding(\n String(res.req?.headers[\"accept-encoding\"] || \"\")\n );\n return { encoding, eligible: true };\n}\n\n// Converts into a Readable stream\nfunction bodyAsReadable(body: Buffer | string | Readable): Readable {\n if (Buffer.isBuffer(body)) return Readable.from([body]);\n if (typeof body === \"string\") return Readable.from([Buffer.from(body)]);\n return body;\n}\n\n// Resolve user-provided compression options (or `true` for defaults) into a\n// complete config. Called once at Cpeak construction.\nexport function resolveCompressionOptions(\n input: true | CompressionOptions\n): ResolvedCompressionConfig {\n const options: CompressionOptions = input === true ? {} : input;\n return {\n threshold: options.threshold ?? 1024,\n brotli: options.brotli ?? {},\n gzip: options.gzip ?? {},\n deflate: options.deflate ?? {}\n };\n}\n\n// The final point used by res.compress / res.json / res.sendFile /\n// res.render when compression is enabled by the developer.\n//\n// Sets Content-Type, negotiates encoding, applies compression when warranted,\n// and writes the body. When compression doesn't apply (HEAD, no-transform,\n// below threshold, no acceptable encoding, non-compressible mime, already\n// encoded), it sends identity bytes with the proper Content-Length.\n//\n// Compression always goes through createGzip/createBrotliCompress/createDeflate\n// streams — these are async and run on libuv's thread pool. The sync zlib\n// forms (gzipSync etc.) would block the event loop on every response, so we\n// never use them here.\nexport async function compressAndSend(\n res: ServerResponse,\n mime: string,\n body: Buffer | string | Readable,\n config: ResolvedCompressionConfig,\n size?: number\n): Promise<void> {\n res.setHeader(\"Content-Type\", mime);\n\n // Resolve a known size for the threshold check. For streams without a\n // size, use Infinity — every other criterion is evaluated, and we assume\n // the body is \"big enough\".\n const knownSize: number = Buffer.isBuffer(body)\n ? body.length\n : typeof body === \"string\"\n ? Buffer.byteLength(body)\n : (size ?? Infinity);\n\n const { encoding, eligible } = negotiate(res, mime, knownSize, config);\n\n // Identity path.\n if (!encoding) {\n if (eligible) appendVary(res, \"Accept-Encoding\");\n if (Buffer.isBuffer(body) || typeof body === \"string\") {\n res.setHeader(\"Content-Length\", String(knownSize));\n res.end(body);\n return;\n }\n if (size !== undefined) res.setHeader(\"Content-Length\", String(size));\n await pipeline(body, res);\n return;\n }\n\n // Compression path. No Content-Length — the compressed size is only known\n // after the stream finishes, so the response goes out chunked.\n res.setHeader(\"Content-Encoding\", encoding);\n appendVary(res, \"Accept-Encoding\");\n await pipeline(\n bodyAsReadable(body),\n createCompressorStream(encoding, config),\n res\n );\n}\n","import type { CpeakRequest, CpeakResponse, Next } from \"../types\";\nimport { Buffer } from \"node:buffer\";\nimport { frameworkError, ErrorCode } from \"../index\";\n\n// Check if Content-Type is JSON\nfunction isJSON(contentType: string | undefined) {\n if (!contentType) return false;\n if (contentType === \"application/json\") return true;\n return (\n contentType.startsWith(\"application/json\") || contentType.includes(\"+json\")\n );\n}\n\n// Parsing JSON\nconst parseJSON = (options: { limit?: number } = {}) => {\n // Default limit to 1MB\n const limit = options.limit || 1024 * 1024;\n\n return (req: CpeakRequest, res: CpeakResponse, next: Next) => {\n if (!isJSON(req.headers[\"content-type\"])) return next();\n\n const chunks: Buffer[] = [];\n let bytesReceived = 0;\n\n const onData = (chunk: Buffer) => {\n bytesReceived += chunk.length;\n\n // To prevent Denial of Service (DoS) attacks, enforce a maximum body size\n if (bytesReceived > limit) {\n // Stop listening to data\n req.pause();\n\n // Remove listeners so we don't trigger 'end' or more 'data'\n req.removeListener(\"data\", onData);\n req.removeListener(\"end\", onEnd);\n\n next(\n frameworkError(\n \"JSON body too large\",\n onData,\n ErrorCode.PAYLOAD_TOO_LARGE,\n 413 // HTTP 413 Payload Too Large\n )\n );\n\n return;\n }\n\n chunks.push(chunk);\n };\n\n const onEnd = () => {\n try {\n // For better performance, we concat buffers once, then convert to string\n // Optimization: If only one chunk exists, avoid the memory copy of concat\n const rawBody =\n chunks.length === 1\n ? chunks[0].toString(\"utf-8\")\n : Buffer.concat(chunks).toString(\"utf-8\");\n\n // Handle empty body case\n req.body = rawBody ? JSON.parse(rawBody) : {};\n\n next();\n } catch (err) {\n // Handle Invalid JSON without crashing\n next(\n frameworkError(\n \"Invalid JSON format\",\n onEnd,\n ErrorCode.INVALID_JSON,\n 400 // HTTP 400 Bad Request\n )\n );\n }\n };\n\n req.on(\"data\", onData);\n req.on(\"end\", onEnd);\n };\n};\n\nexport { parseJSON };\n","import fs from \"node:fs\";\nimport path from \"node:path\";\n\nimport type { StringMap, CpeakRequest, CpeakResponse, Next } from \"../types\";\n\nconst MIME_TYPES: StringMap = {\n html: \"text/html\",\n css: \"text/css\",\n js: \"application/javascript\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n png: \"image/png\",\n svg: \"image/svg+xml\",\n txt: \"text/plain\",\n eot: \"application/vnd.ms-fontobject\",\n otf: \"font/otf\",\n ttf: \"font/ttf\",\n woff: \"font/woff\",\n woff2: \"font/woff2\",\n gif: \"image/gif\",\n ico: \"image/x-icon\",\n json: \"application/json\",\n webmanifest: \"application/manifest+json\"\n};\n\nconst serveStatic = (\n folderPath: string,\n newMimeTypes?: StringMap,\n options?: { prefix?: string }\n) => {\n // For new user defined mime types\n if (newMimeTypes) {\n Object.assign(MIME_TYPES, newMimeTypes);\n }\n\n const prefix = options?.prefix ?? \"\";\n\n function processFolder(folderPath: string, parentFolder: string) {\n const staticFiles: string[] = [];\n\n // Read the contents of the folder\n const files = fs.readdirSync(folderPath);\n\n // Loop through the files and subfolders\n for (const file of files) {\n const fullPath = path.join(folderPath, file);\n\n // Check if it's a directory\n if (fs.statSync(fullPath).isDirectory()) {\n // If it's a directory, recursively process it\n const subfolderFiles = processFolder(fullPath, parentFolder);\n staticFiles.push(...subfolderFiles);\n } else {\n // If it's a file, add it to the array\n const relativePath = path.relative(parentFolder, fullPath);\n const fileExtension = path.extname(file).slice(1);\n if (MIME_TYPES[fileExtension]) staticFiles.push(\"/\" + relativePath);\n }\n }\n\n return staticFiles;\n }\n\n const filesArrayToFilesMap = (filesArray: string[]) => {\n const filesMap: Record<string, { path: string; mime: string }> = {};\n for (const file of filesArray) {\n const fileExtension = path.extname(file).slice(1);\n filesMap[prefix + file] = {\n path: folderPath + file,\n mime: MIME_TYPES[fileExtension]\n };\n }\n return filesMap;\n };\n\n // Start processing the folder\n const filesMap = filesArrayToFilesMap(processFolder(folderPath, folderPath));\n\n return function (req: CpeakRequest, res: CpeakResponse, next: Next) {\n const url = req.url;\n if (typeof url !== \"string\") return next();\n\n const pathname = url.split(\"?\")[0];\n if (Object.prototype.hasOwnProperty.call(filesMap, pathname)) {\n const fileRoute = filesMap[pathname];\n return res.sendFile(fileRoute.path, fileRoute.mime);\n }\n\n next();\n };\n};\n\nexport { serveStatic };\n","import fs from \"node:fs/promises\";\nimport { frameworkError } from \"../\";\nimport { compressAndSend } from \"./compression\";\nimport type { CpeakRequest, CpeakResponse, Next } from \"../types\";\n\nfunction renderTemplate(\n templateStr: string,\n data: Record<string, unknown>\n): string {\n // Initialize variables\n let result: (string | unknown)[] = [];\n\n let currentIndex = 0;\n\n while (currentIndex < templateStr.length) {\n // Find the next opening placeholder\n const startIdx = templateStr.indexOf(\"{{\", currentIndex);\n if (startIdx === -1) {\n // No more placeholders, push the remaining string\n result.push(templateStr.slice(currentIndex));\n break;\n }\n\n // Push the part before the placeholder\n result.push(templateStr.slice(currentIndex, startIdx));\n\n // Find the closing placeholder\n const endIdx = templateStr.indexOf(\"}}\", startIdx);\n if (endIdx === -1) {\n // No closing brace found, treat the rest as plain text\n result.push(templateStr.slice(startIdx));\n break;\n }\n\n // Extract the variable name\n const varName = templateStr.slice(startIdx + 2, endIdx).trim();\n\n // Replace the variable with its value from the data, or use an empty string if not found\n const replacement = data[varName] !== undefined ? data[varName] : \"\";\n\n // Push the replacement to the result array\n result.push(replacement);\n\n // Move the index past the current closing placeholder\n currentIndex = endIdx + 2;\n }\n\n // Join all parts into a final string\n return result.join(\"\");\n}\n\n// Errors to return: recommend to not render files larger than 100KB\n// To Explore: Doing the operation in C++ and return the data as stream back to the client\n// @TODO: remove the file from static map\n// @TODO: escape the string to prevent XSS\n// @TODO: add another {{{ }}} option to not escape the string\nconst render = () => {\n return function (req: CpeakRequest, res: CpeakResponse, next: Next): void {\n res.render = async (\n path: string,\n data: Record<string, unknown>,\n mime: string\n ) => {\n // check if mime is specified, if not return an error\n if (!mime) {\n throw frameworkError(\n `MIME type is missing. You called res.render(\"${path}\", ...) but forgot to provide the third \"mime\" argument.`,\n res.render\n );\n }\n\n let fileStr = await fs.readFile(path, \"utf-8\");\n const finalStr = renderTemplate(fileStr, data);\n\n const config = (res.socket as any)?.server?._cpeakCompression;\n if (config) {\n await compressAndSend(res, mime, finalStr, config);\n return;\n }\n\n res.setHeader(\"Content-Type\", mime);\n res.end(finalStr);\n };\n\n next();\n };\n};\n\nexport { render };\n","import type { CpeakRequest, CpeakResponse, Next } from \"../types\";\n\nconst swagger = (spec: object, prefix = \"/api-docs\") => {\n const initializerJs = `window.onload = function() {\n SwaggerUIBundle({\n url: \"${prefix}/spec.json\",\n dom_id: '#swagger-ui',\n presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],\n layout: \"StandaloneLayout\"\n });\n};`;\n\n return (req: CpeakRequest, res: CpeakResponse, next: Next) => {\n if (req.url === prefix || req.url === `${prefix}/`) {\n res.writeHead(302, { Location: `${prefix}/index.html` });\n res.end();\n return;\n }\n if (req.url === `${prefix}/spec.json`) {\n return res.json(spec);\n }\n if (req.url === `${prefix}/swagger-initializer.js`) {\n res.setHeader(\"Content-Type\", \"application/javascript\");\n res.end(initializerJs);\n return;\n }\n next();\n };\n};\n\nexport { swagger };\n","import { randomBytes, pbkdf2, createHmac, timingSafeEqual } from \"node:crypto\";\nimport { promisify } from \"node:util\";\nimport type { Middleware } from \"../types\";\nimport { frameworkError, ErrorCode } from \"../index\";\nimport type { AuthOptions, PbkdfOptions } from \"./types\";\n\nconst pbkdf2Async = promisify(pbkdf2);\n\nconst DEFAULTS = {\n iterations: 210_000,\n keylen: 64,\n digest: \"sha512\",\n saltSize: 32,\n hmacAlgorithm: \"sha256\",\n tokenIdSize: 20,\n tokenExpiry: 7 * 24 * 60 * 60 * 1000 // 7 days in ms\n} as const;\n\nexport async function hashPassword(\n password: string,\n options?: PbkdfOptions\n): Promise<string> {\n const iterations = options?.iterations ?? DEFAULTS.iterations;\n const keylen = options?.keylen ?? DEFAULTS.keylen;\n const digest = options?.digest ?? DEFAULTS.digest;\n const saltSize = options?.saltSize ?? DEFAULTS.saltSize;\n const salt = randomBytes(saltSize);\n const hash = await pbkdf2Async(password, salt, iterations, keylen, digest);\n return `pbkdf2:${iterations}:${keylen}:${digest}:${salt.toString(\"hex\")}:${hash.toString(\"hex\")}`;\n}\n\nexport async function verifyPassword(\n password: string,\n stored: string\n): Promise<boolean> {\n // When argon2 is added, dispatch on the prefix here.\n const withoutPrefix = stored.slice(stored.indexOf(\":\") + 1);\n const parts = withoutPrefix.split(\":\");\n if (parts.length !== 5) return false;\n const [itersStr, keylenStr, digest, saltHex, hashHex] = parts;\n const iterations = parseInt(itersStr, 10);\n const keylen = parseInt(keylenStr, 10);\n if (!digest || !saltHex || !hashHex || isNaN(iterations) || isNaN(keylen))\n return false;\n const salt = Buffer.from(saltHex, \"hex\");\n const hash = await pbkdf2Async(password, salt, iterations, keylen, digest);\n const storedHash = Buffer.from(hashHex, \"hex\");\n if (storedHash.length !== hash.length) return false;\n return timingSafeEqual(hash, storedHash);\n}\n\nfunction signToken(tokenId: string, secret: string, algorithm: string): string {\n const sig = createHmac(algorithm, secret).update(tokenId).digest(\"hex\");\n return `${tokenId}.${sig}`;\n}\n\nfunction extractTokenId(\n token: string,\n secret: string,\n algorithm: string\n): string | null {\n const dot = token.indexOf(\".\");\n if (dot === -1) return null;\n const tokenId = token.slice(0, dot);\n const sig = token.slice(dot + 1);\n const expected = createHmac(algorithm, secret).update(tokenId).digest(\"hex\");\n const expectedBuf = Buffer.from(expected, \"hex\");\n const actualBuf = Buffer.from(sig, \"hex\");\n if (expectedBuf.length !== actualBuf.length) return null;\n if (!timingSafeEqual(expectedBuf, actualBuf)) return null;\n return tokenId;\n}\n\nexport function auth(options: AuthOptions): Middleware {\n if (!options.secret || options.secret.length < 32) {\n throw frameworkError(\n \"Secret must be at least 32 characters. HMAC security is only as strong as the key.\",\n auth,\n ErrorCode.WEAK_SECRET\n );\n }\n\n const {\n secret,\n saveToken,\n findToken,\n revokeToken,\n tokenExpiry = DEFAULTS.tokenExpiry,\n hmacAlgorithm = DEFAULTS.hmacAlgorithm,\n tokenIdSize = DEFAULTS.tokenIdSize\n } = options;\n\n const pbkdfOpts: PbkdfOptions = {\n iterations: options.iterations,\n keylen: options.keylen,\n digest: options.digest,\n saltSize: options.saltSize\n };\n\n const _hashPassword = ({ password }: { password: string }) =>\n hashPassword(password, pbkdfOpts);\n\n const login = async ({\n password,\n hashedPassword,\n userId\n }: {\n password: string;\n hashedPassword: string;\n userId: string;\n }): Promise<string | null> => {\n const isMatch = await verifyPassword(password, hashedPassword);\n if (!isMatch) return null;\n const tokenId = randomBytes(tokenIdSize).toString(\"hex\");\n const token = signToken(tokenId, secret, hmacAlgorithm);\n await saveToken(tokenId, userId, new Date(Date.now() + tokenExpiry));\n return token;\n };\n\n const verifyToken = async (\n token: string\n ): Promise<{ userId: string } | null> => {\n if (!token) return null;\n const tokenId = extractTokenId(token, secret, hmacAlgorithm);\n if (!tokenId) return null;\n const record = await findToken(tokenId);\n if (!record) return null;\n if (new Date(record.expiresAt) < new Date()) return null;\n return { userId: record.userId };\n };\n\n const logout = revokeToken\n ? async (token: string): Promise<boolean> => {\n const tokenId = extractTokenId(token, secret, hmacAlgorithm);\n if (!tokenId) return false;\n await revokeToken(tokenId);\n return true;\n }\n : undefined;\n\n return (req, _res, next) => {\n req.hashPassword = _hashPassword;\n req.login = login;\n req.verifyToken = verifyToken;\n if (logout) req.logout = logout;\n next();\n };\n}\n","import { createHmac, timingSafeEqual } from \"node:crypto\";\nimport type { CpeakRequest, CpeakResponse, Next } from \"../types\";\nimport { frameworkError, ErrorCode } from \"../index\";\nimport type { CookieOptions } from \"./types\";\n\n// This will sign the cookie value with HMAC with the secret.\n// Ideal for data like user IDs or session IDs, where you want to ensure the integrity of the cookie value without encryption.\n// So this way, imagine you save a user ID in the cookie. By signing it, you can detect if the client has tampered with the cookie value\n// (e.g., changing the user ID to impersonate another user).\n// However, since it's not encrypted, the actual user ID is still visible to the client.\n// This is a common approach for session cookies where you want to prevent tampering but don't mind if the value is visible.\nfunction sign(value: string, secret: string): string {\n const sig = createHmac(\"sha256\", secret).update(value).digest(\"base64url\");\n return `s:${value}.${sig}`;\n}\n\nfunction unsign(signed: string, secret: string): string | false {\n if (!signed.startsWith(\"s:\")) return false;\n const withoutPrefix = signed.slice(2);\n const lastDot = withoutPrefix.lastIndexOf(\".\");\n if (lastDot === -1) return false;\n const value = withoutPrefix.slice(0, lastDot);\n const sig = withoutPrefix.slice(lastDot + 1);\n const expected = createHmac(\"sha256\", secret)\n .update(value)\n .digest(\"base64url\");\n const expectedBuf = Buffer.from(expected);\n const actualBuf = Buffer.from(sig);\n if (expectedBuf.length !== actualBuf.length) return false;\n if (!timingSafeEqual(expectedBuf, actualBuf)) return false;\n return value;\n}\n\n// Parses the raw value of an HTTP `Cookie` request header into a name->value\n// This should be compatible with the RFC 6265 HTTP specification\nfunction parseRawCookies(header: string): Record<string, string> {\n // Use a null-prototype object to prevent prototype pollution attacks when assigning cookie names like \"__proto__\" or \"constructor\".\n const cookies: Record<string, string> = Object.create(null);\n if (!header) return cookies;\n\n const pairs = header.split(\";\");\n\n for (let i = 0; i < pairs.length; i++) {\n const pair = pairs[i];\n const equalSignIndex = pair.indexOf(\"=\");\n\n // RFC 6265: cookie-pair requires '='. Pairs without one (e.g. a\n // bare flag like `Cookie: foo`) are not valid cookie-pairs and we skip them.\n // Note we use the FIRST '=' only. So values like base64 padding (`token=YWJjPT0=`) must keep trailing '='s.\n if (equalSignIndex === -1) continue;\n\n const key = pair.slice(0, equalSignIndex).trim();\n // Drop empty names and honour the FIRST occurrence on duplicates (Specs say servers SHOULD NOT rely on order.\n // We pick first-wins for stability).\n if (!key || cookies[key] !== undefined) continue;\n\n let val = pair.slice(equalSignIndex + 1).trim();\n\n // Cookie values are sometimes sent wrapped in double quotes (like name=\"hello world\"), so we strip the outer \"\n // characters to get the actual value hello world.\n // The val.length > 1 guard handles the edge case where the value is literally just a single \" — without it, that one\n // character would match both the \"starts with quote\" and \"ends with quote\" checks, and slice(1, -1) would wipe it out\n // to an empty string.\n if (val.length > 1 && val[0] === '\"' && val[val.length - 1] === '\"') {\n val = val.slice(1, -1);\n }\n\n // Percent-decoding cookie values is a server-side convention (not part of\n // RFC 6265 itself), but it's what Express and most ecosystem libraries do,\n // so we follow suit for compatibility. Skip the decode entirely when there's no\n // '%' to save work on the common case, and fall back to the raw value if\n // decodeURIComponent throws on malformed input rather than crashing the\n // whole request.\n try {\n cookies[key] = val.indexOf(\"%\") !== -1 ? decodeURIComponent(val) : val;\n } catch (e) {\n cookies[key] = val;\n }\n }\n return cookies;\n}\n\n// One example output: \"session=abc123; Path=/dashboard; Domain=example.com; Max-Age=86400; Expires=Thu, 31 Dec 2026 00:00:00 GMT; HttpOnly; Secure; SameSite=Strict\"\nfunction buildSetCookieHeader(\n name: string,\n value: string,\n options: CookieOptions\n): string {\n const parts: string[] = [`${name}=${encodeURIComponent(value)}`];\n const path = options.path ?? \"/\";\n parts.push(`Path=${path}`);\n if (options.domain) parts.push(`Domain=${options.domain}`);\n if (options.maxAge !== undefined)\n parts.push(`Max-Age=${Math.floor(options.maxAge / 1000)}`);\n if (options.expires) parts.push(`Expires=${options.expires.toUTCString()}`);\n if (options.httpOnly) parts.push(\"HttpOnly\");\n if (options.secure) parts.push(\"Secure\");\n if (options.sameSite) parts.push(`SameSite=${options.sameSite}`);\n return parts.join(\"; \");\n}\n\n// Without this helper, calling res.cookie(\"a\", \"1\") then res.cookie(\"b\", \"2\") would overwrite the first cookie instead\n// of sending both.\nfunction appendSetCookie(res: CpeakResponse, header: string) {\n const existing = res.getHeader(\"Set-Cookie\");\n if (!existing) {\n res.setHeader(\"Set-Cookie\", [header]);\n } else if (Array.isArray(existing)) {\n res.setHeader(\"Set-Cookie\", [...existing, header]);\n } else {\n res.setHeader(\"Set-Cookie\", [String(existing), header]);\n }\n}\n\nexport function cookieParser(options: { secret?: string } = {}) {\n const { secret } = options;\n\n if (secret !== undefined && secret.length < 32) {\n throw frameworkError(\n \"Secret must be at least 32 characters. HMAC security is only as strong as the key.\",\n cookieParser,\n ErrorCode.WEAK_SECRET\n );\n }\n\n return (req: CpeakRequest, res: CpeakResponse, next: Next) => {\n const rawHeader = req.headers[\"cookie\"] || \"\";\n const raw = parseRawCookies(rawHeader);\n\n // Mirror parseRawCookies and use null-prototype maps here too. If we used\n // a regular `{}`, the assignment below would invoke Object.prototype's\n // __proto__ setter (no-op for string values), silently dropping any\n // cookie literally named __proto__ — undoing the fix in parseRawCookies.\n const cookies: Record<string, string> = Object.create(null);\n const signedCookies: Record<string, string | false> = Object.create(null);\n\n for (const [key, val] of Object.entries(raw)) {\n // The \"s:\" prefix is the marker we add in `sign()` for HMAC-signed\n // cookies. Route those through unsign so the handler sees the original\n // value (or `false` if the signature didn't verify).\n if (val.startsWith(\"s:\") && secret) {\n signedCookies[key] = unsign(val, secret);\n } else {\n cookies[key] = val;\n }\n }\n\n // The separation is intentional signal: \"these were signed and verified, trust them more.\"\n req.cookies = cookies;\n req.signedCookies = signedCookies;\n\n res.cookie = (name: string, value: string, options: CookieOptions = {}) => {\n let finalValue = value;\n if (options.signed) {\n if (!secret)\n throw new Error(\n \"cookieParser: secret is required to use signed cookies\"\n );\n finalValue = sign(value, secret);\n }\n appendSetCookie(res, buildSetCookieHeader(name, finalValue, options));\n return res;\n };\n\n res.clearCookie = (name: string, options: CookieOptions = {}) => {\n appendSetCookie(\n res,\n buildSetCookieHeader(name, \"\", {\n ...options,\n maxAge: 0,\n expires: new Date(0)\n })\n );\n return res;\n };\n\n next();\n };\n}\n","import type { CpeakRequest, CpeakResponse, Next } from \"../types\";\nimport type { CorsOptions, OriginInput } from \"./types\";\n\n// Append a value to an existing header without overwriting prior entries\n// (e.g. compression already sets `Vary: Accept-Encoding`).\nfunction appendVary(res: CpeakResponse, value: string) {\n const existing = res.getHeader(\"Vary\");\n if (!existing) return res.setHeader(\"Vary\", value);\n const current = String(existing)\n .split(\",\")\n .map((s) => s.trim())\n .filter(Boolean);\n if (current.includes(\"*\") || current.includes(value)) return;\n res.setHeader(\"Vary\", [...current, value].join(\", \"));\n}\n\n// Determine if the given origin is allowed based on the rule.\n// Examples of what developers can specify for the rule:\n// - `true` or `*`: allow all origins\n// - `false`: disallow all origins\n// - `\"https://example.com\"`: allow only this origin\n// - `[\"https://example.com\", \"https://foo.com\"]`: allow these origins\n// - `/\\.example\\.com$/`: allow origins that match this regex\n// - `(origin) => origin === \"https://example.com\"`: custom function to determine if the origin is allowed\nasync function isAllowed(\n origin: string | undefined,\n rule: OriginInput\n): Promise<boolean> {\n if (rule === true || rule === \"*\") return true;\n if (rule === false || !origin) return false;\n if (typeof rule === \"string\") return rule === origin;\n if (Array.isArray(rule)) return rule.includes(origin);\n if (rule instanceof RegExp) return rule.test(origin);\n if (typeof rule === \"function\") return await rule(origin);\n return false;\n}\n\nconst cors = (options: CorsOptions = {}) => {\n const {\n origin = \"*\",\n methods = \"GET,HEAD,PUT,PATCH,POST,DELETE\",\n allowedHeaders,\n exposedHeaders,\n credentials = false,\n maxAge = 86400,\n preflightContinue = false,\n optionsSuccessStatus = 204\n } = options;\n\n const methodsStr = Array.isArray(methods) ? methods.join(\",\") : methods;\n const allowedHeadersStr = Array.isArray(allowedHeaders)\n ? allowedHeaders.join(\",\")\n : allowedHeaders;\n const exposedHeadersStr = Array.isArray(exposedHeaders)\n ? exposedHeaders.join(\",\")\n : exposedHeaders;\n\n return async (req: CpeakRequest, res: CpeakResponse, next: Next) => {\n const requestOrigin = req.headers.origin;\n\n // Not a CORS request, nothing to do.\n if (!requestOrigin) return next();\n\n const allowed = await isAllowed(requestOrigin, origin);\n if (!allowed) return next();\n\n // We cannot combine Access-Control-Allow-Origin: * with\n // Access-Control-Allow-Credentials: true. Browsers will flat-out reject it.\n // Instead we'll reflect the origin.\n const allowOriginValue =\n origin === \"*\" && !credentials ? \"*\" : requestOrigin;\n res.setHeader(\"Access-Control-Allow-Origin\", allowOriginValue);\n if (allowOriginValue !== \"*\") appendVary(res, \"Origin\");\n\n if (credentials) res.setHeader(\"Access-Control-Allow-Credentials\", \"true\");\n if (exposedHeadersStr)\n res.setHeader(\"Access-Control-Expose-Headers\", exposedHeadersStr);\n\n const isPreflight =\n req.method === \"OPTIONS\" &&\n req.headers[\"access-control-request-method\"] !== undefined;\n\n if (!isPreflight) return next();\n\n res.setHeader(\"Access-Control-Allow-Methods\", methodsStr);\n\n if (allowedHeadersStr) {\n res.setHeader(\"Access-Control-Allow-Headers\", allowedHeadersStr);\n } else if (origin === \"*\") {\n // If origin is *, just act like an echo chamber for requested headers. Give back whatever the browser asks for.\n const requested = req.headers[\"access-control-request-headers\"];\n if (requested) res.setHeader(\"Access-Control-Allow-Headers\", requested);\n } else {\n res.setHeader(\n \"Access-Control-Allow-Headers\",\n \"Content-Type, Authorization\"\n );\n }\n\n res.setHeader(\"Access-Control-Max-Age\", String(maxAge));\n\n if (preflightContinue) return next();\n\n res.statusCode = optionsSuccessStatus;\n res.end();\n };\n};\n\nexport { cors };\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,OAAOA,SAAQ;AACf,SAAS,wBAAwB;AACjC,SAAS,YAAAC,iBAAgB;;;ACHzB,OAAO,UAAU;AACjB,SAAS,gBAAgB;AACzB,SAAS,UAAAC,eAAc;AACvB,SAAS,gBAAgB;AASzB,IAAM,oBAAoB;AAC1B,IAAM,eAAe;AAIrB,SAAS,aAAa,QAAiC;AACrD,MAAI,CAAC,OAAQ,QAAO;AAEpB,QAAM,WAAmC,CAAC;AAC1C,MAAI;AAEJ,aAAW,QAAQ,OAAO,MAAM,GAAG,GAAG;AACpC,UAAM,CAAC,SAAS,GAAG,MAAM,IAAI,KAAK,KAAK,EAAE,MAAM,GAAG;AAClD,UAAM,OAAO,QAAQ,KAAK,EAAE,YAAY;AACxC,QAAI,CAAC,KAAM;AAEX,QAAI,IAAI;AACR,eAAW,KAAK,QAAQ;AACtB,YAAM,IAAI,EAAE,KAAK,EAAE,MAAM,eAAe;AACxC,UAAI,EAAG,KAAI,OAAO,EAAE,CAAC,CAAC;AAAA,IACxB;AACA,QAAI,OAAO,MAAM,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vB;AAAA;AAAA;AAAA;AAAA,EAKA,aAAaA,OAAc;AACzB,UAAM,cACJ,MAAMA,MAAK,QAAQ,SAAS,SAAS,EAAE,QAAQ,OAAO,IAAI,IAAI;AAEhE,WAAO,IAAI,OAAO,WAAW;AAAA,EAC/B;AAAA,EAEA,sBAAsBA,OAAc,OAAyB;AAE3D,UAAM,cAAcA,MAAK,MAAM,OAAO,KAAK,CAAC,GAAG;AAAA,MAAI,CAAC,UAClD,MAAM,MAAM,CAAC;AAAA,IACf;AACA,UAAM,SAAoB,CAAC;AAC3B,eAAW,QAAQ,CAAC,MAAM,UAAU;AAClC,aAAO,IAAI,IAAI,MAAM,QAAQ,CAAC;AAAA,IAChC,CAAC;AACD,WAAO;AAAA,EACT;AACF;AAkCe,SAAR,MAAuB,SAA+B;AAC3D,SAAO,IAAI,MAAM,OAAO;AAC1B;","names":["fs","pipeline","Buffer","Buffer","Buffer","folderPath","filesMap","fs","path","fs","createHmac","timingSafeEqual","createHmac","timingSafeEqual","path","options","appendVary","ErrorCode","path","fs","pipeline","req","res"]}
|