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.
@@ -1,5 +1,6 @@
1
1
  import fs from "node:fs/promises";
2
2
  import { frameworkError } from "../";
3
+ import { compressAndSend } from "../internal/compression";
3
4
  import type { CpeakRequest, CpeakResponse, Next } from "../types";
4
5
 
5
6
  function renderTemplate(
@@ -70,6 +71,12 @@ const render = () => {
70
71
 
71
72
  let fileStr = await fs.readFile(path, "utf-8");
72
73
  const finalStr = renderTemplate(fileStr, data);
74
+
75
+ if (res._compression) {
76
+ await compressAndSend(res, mime, finalStr, res._compression);
77
+ return;
78
+ }
79
+
73
80
  res.setHeader("Content-Type", mime);
74
81
  res.end(finalStr);
75
82
  };
@@ -17,14 +17,24 @@ const MIME_TYPES: StringMap = {
17
17
  ttf: "font/ttf",
18
18
  woff: "font/woff",
19
19
  woff2: "font/woff2",
20
+ gif: "image/gif",
21
+ ico: "image/x-icon",
22
+ json: "application/json",
23
+ webmanifest: "application/manifest+json"
20
24
  };
21
25
 
22
- const serveStatic = (folderPath: string, newMimeTypes?: StringMap) => {
26
+ const serveStatic = (
27
+ folderPath: string,
28
+ newMimeTypes?: StringMap,
29
+ options?: { prefix?: string }
30
+ ) => {
23
31
  // For new user defined mime types
24
32
  if (newMimeTypes) {
25
33
  Object.assign(MIME_TYPES, newMimeTypes);
26
34
  }
27
35
 
36
+ const prefix = options?.prefix ?? "";
37
+
28
38
  function processFolder(folderPath: string, parentFolder: string) {
29
39
  const staticFiles: string[] = [];
30
40
 
@@ -55,9 +65,9 @@ const serveStatic = (folderPath: string, newMimeTypes?: StringMap) => {
55
65
  const filesMap: Record<string, { path: string; mime: string }> = {};
56
66
  for (const file of filesArray) {
57
67
  const fileExtension = path.extname(file).slice(1);
58
- filesMap[file] = {
68
+ filesMap[prefix + file] = {
59
69
  path: folderPath + file,
60
- mime: MIME_TYPES[fileExtension],
70
+ mime: MIME_TYPES[fileExtension]
61
71
  };
62
72
  }
63
73
  return filesMap;
@@ -70,8 +80,9 @@ const serveStatic = (folderPath: string, newMimeTypes?: StringMap) => {
70
80
  const url = req.url;
71
81
  if (typeof url !== "string") return next();
72
82
 
73
- if (Object.prototype.hasOwnProperty.call(filesMap, url)) {
74
- const fileRoute = filesMap[url];
83
+ const pathname = url.split("?")[0];
84
+ if (Object.prototype.hasOwnProperty.call(filesMap, pathname)) {
85
+ const fileRoute = filesMap[pathname];
75
86
  return res.sendFile(fileRoute.path, fileRoute.mime);
76
87
  }
77
88
 
@@ -0,0 +1,31 @@
1
+ import type { CpeakRequest, CpeakResponse, Next } from "../types";
2
+
3
+ const swagger = (spec: object, prefix = "/api-docs") => {
4
+ const initializerJs = `window.onload = function() {
5
+ SwaggerUIBundle({
6
+ url: "${prefix}/spec.json",
7
+ dom_id: '#swagger-ui',
8
+ presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
9
+ layout: "StandaloneLayout"
10
+ });
11
+ };`;
12
+
13
+ return (req: CpeakRequest, res: CpeakResponse, next: Next) => {
14
+ if (req.url === prefix || req.url === `${prefix}/`) {
15
+ res.writeHead(302, { Location: `${prefix}/index.html` });
16
+ res.end();
17
+ return;
18
+ }
19
+ if (req.url === `${prefix}/spec.json`) {
20
+ return res.json(spec);
21
+ }
22
+ if (req.url === `${prefix}/swagger-initializer.js`) {
23
+ res.setHeader("Content-Type", "application/javascript");
24
+ res.end(initializerJs);
25
+ return;
26
+ }
27
+ next();
28
+ };
29
+ };
30
+
31
+ export { swagger };
@@ -0,0 +1,51 @@
1
+ export interface PbkdfOptions {
2
+ iterations?: number;
3
+ keylen?: number;
4
+ digest?: string;
5
+ saltSize?: number;
6
+ }
7
+
8
+ export interface AuthOptions extends PbkdfOptions {
9
+ secret: string;
10
+ saveToken: (
11
+ tokenId: string,
12
+ userId: string,
13
+ expiresAt: Date
14
+ ) => Promise<void>;
15
+ findToken: (
16
+ tokenId: string
17
+ ) => Promise<{ userId: string; expiresAt: Date } | null>;
18
+ tokenExpiry?: number;
19
+ hmacAlgorithm?: string;
20
+ tokenIdSize?: number;
21
+ revokeToken?: (tokenId: string) => Promise<void>;
22
+ }
23
+
24
+ export interface CookieOptions {
25
+ signed?: boolean;
26
+ httpOnly?: boolean;
27
+ secure?: boolean;
28
+ sameSite?: "strict" | "lax" | "none";
29
+ maxAge?: number; // ms
30
+ expires?: Date;
31
+ path?: string;
32
+ domain?: string;
33
+ }
34
+
35
+ export type OriginInput =
36
+ | string
37
+ | string[]
38
+ | RegExp
39
+ | boolean
40
+ | ((origin: string | undefined) => boolean | Promise<boolean>);
41
+
42
+ export interface CorsOptions {
43
+ origin?: OriginInput;
44
+ methods?: string | string[];
45
+ allowedHeaders?: string | string[];
46
+ exposedHeaders?: string | string[];
47
+ credentials?: boolean;
48
+ maxAge?: number;
49
+ preflightContinue?: boolean;
50
+ optionsSuccessStatus?: number;
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cpeak",
3
- "version": "2.5.0",
3
+ "version": "2.7.0",
4
4
  "description": "A minimal and fast Node.js HTTP framework.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -10,12 +10,12 @@
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "https://github.com/agile8118/cpeak.git"
13
+ "url": "https://github.com/Cododev-Technology/cpeak.git"
14
14
  },
15
15
  "bugs": {
16
- "url": "https://github.com/agile8118/cpeak/issues"
16
+ "url": "https://github.com/Cododev-Technology/cpeak/issues"
17
17
  },
18
- "homepage": "https://github.com/agile8118/cpeak#readme",
18
+ "homepage": "https://github.com/Cododev-Technology/cpeak#readme",
19
19
  "main": "./dist/index.js",
20
20
  "module": "./dist/index.js",
21
21
  "types": "./dist/index.d.ts",
File without changes