cpeak 1.1.0 → 1.2.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/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const http = require("node:http");
2
2
  const fs = require("node:fs/promises");
3
3
 
4
- const { parseJSON } = require("./util");
4
+ const { parseJSON, serveStatic } = require("./util");
5
5
  class CPeak {
6
6
  constructor() {
7
7
  this.server = http.createServer();
@@ -71,5 +71,6 @@ class CPeak {
71
71
  }
72
72
 
73
73
  CPeak.parseJSON = parseJSON;
74
+ CPeak.serveStatic = serveStatic;
74
75
 
75
76
  module.exports = CPeak;
package/lib/util.js CHANGED
@@ -1,3 +1,79 @@
1
+ const fs = require("node:fs");
2
+ const path = require("node:path");
3
+
4
+ const MIME_TYPES = {
5
+ html: "text/html",
6
+ css: "text/css",
7
+ js: "application/javascript",
8
+ jpg: "image/jpeg",
9
+ jpeg: "image/jpeg",
10
+ png: "image/png",
11
+ svg: "image/svg+xml",
12
+ txt: "text/plain",
13
+ eot: "application/vnd.ms-fontobject",
14
+ otf: "font/otf",
15
+ ttf: "font/ttf",
16
+ woff: "font/woff",
17
+ woff2: "font/woff2",
18
+ };
19
+
20
+ exports.serveStatic = (folderPath, newMimeTypes) => {
21
+ // For new user defined mime types
22
+ if (newMimeTypes) {
23
+ Object.assign(MIME_TYPES, newMimeTypes);
24
+ }
25
+
26
+ function processFolder(folderPath, parentFolder) {
27
+ const staticFiles = [];
28
+
29
+ // Read the contents of the folder
30
+ const files = fs.readdirSync(folderPath);
31
+
32
+ // Loop through the files and subfolders
33
+ for (const file of files) {
34
+ const fullPath = path.join(folderPath, file);
35
+
36
+ // Check if it's a directory
37
+ if (fs.statSync(fullPath).isDirectory()) {
38
+ // If it's a directory, recursively process it
39
+ const subfolderFiles = processFolder(fullPath, parentFolder);
40
+ staticFiles.push(...subfolderFiles);
41
+ } else {
42
+ // If it's a file, add it to the array
43
+ const relativePath = path.relative(parentFolder, fullPath);
44
+ const fileExtension = path.extname(file).slice(1);
45
+ if (MIME_TYPES[fileExtension]) staticFiles.push("/" + relativePath);
46
+ }
47
+ }
48
+
49
+ return staticFiles;
50
+ }
51
+
52
+ const filesArrayToFilesMap = (filesArray) => {
53
+ const filesMap = {};
54
+ for (const file of filesArray) {
55
+ const fileExtension = path.extname(file).slice(1);
56
+ filesMap[file] = {
57
+ path: folderPath + file,
58
+ mime: MIME_TYPES[fileExtension],
59
+ };
60
+ }
61
+ return filesMap;
62
+ };
63
+
64
+ // Start processing the folder
65
+ const filesMap = filesArrayToFilesMap(processFolder(folderPath, folderPath));
66
+
67
+ return function (req, res, next) {
68
+ if (filesMap.hasOwnProperty(req.url)) {
69
+ const fileRoute = filesMap[req.url];
70
+ return res.sendFile(fileRoute.path, fileRoute.mime);
71
+ } else {
72
+ next();
73
+ }
74
+ };
75
+ };
76
+
1
77
  // Parsing JSON
2
78
  exports.parseJSON = (req, res, next) => {
3
79
  // This is only good for bodies that their size is less than the highWaterMark value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cpeak",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "scripts": {