From 24bcb3260af79ba876184f3a48ec812d170f871e Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:10:54 -0400
Subject: [PATCH 01/29] Update package.json
---
package.json | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/package.json b/package.json
index 9a9461f..a764497 100644
--- a/package.json
+++ b/package.json
@@ -1,12 +1,17 @@
{
"name": "serverfire",
- "version": "1.0.2",
+ "version": "2.0.0",
"description": "ServerFire is a new lightweight server library to help you create your website, light and powerful!",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
- "keywords": ["server", "web", "express-alternative", "dependency-free"],
+ "keywords": [
+ "server",
+ "web",
+ "express-alternative",
+ "dependency-free"
+ ],
"author": "AmazingMech2418",
"license": "MIT"
}
From 4e2194c4068c33f246196a7b205214928a82da8c Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:11:08 -0400
Subject: [PATCH 02/29] Update package-lock.json
---
package-lock.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package-lock.json b/package-lock.json
index f5b4cb7..0cdb6bd 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,5 +1,5 @@
{
"name": "serverfire",
- "version": "1.0.0",
+ "version": "2.0.0",
"lockfileVersion": 1
}
From 0a54ac89f1eeadf57a1a877de76330cc2484bcd0 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:11:28 -0400
Subject: [PATCH 03/29] Update index.js
---
index.js | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/index.js b/index.js
index 34b8487..6ff4eb1 100644
--- a/index.js
+++ b/index.js
@@ -1,16 +1,18 @@
-const Server = require('./src/server/index.js');
-const bodyparser = require('./src/server/body-parser/middleware.js');
-const Router = require('./src/server/router/router.js');
-const route = require('./src/server/router/route.js');
-const {generate} = require('./src/server/middleware-tools/gen.js');
+const Server = require("./src/server.js");
+const bp = require("./src/utils/middleware/body-parser.js");
+const Router = require("./src/utils/router/router.js");
+const route = require("./src/utils/router/route.js");
+const {generate, genAsync} = require("./src/utils/middleware/middleware.js");
+const cors = require("./tools/cors.js");
module.exports = {
Server: Server,
- bodyparser: bodyparser,
+ bodyparser: bp,
Router: Router,
route: route,
generateMiddleware: generate,
+ genMiddlewareAsync: genAsync,
tools: {
- cors: require('./tools/cors.js')
+ cors: cors
}
-};
\ No newline at end of file
+};
From e82e58b210f99d0afa52efa2d8deccb2b5934f6b Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:12:23 -0400
Subject: [PATCH 04/29] Update cors.js
---
tools/cors.js | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/tools/cors.js b/tools/cors.js
index a52dcd4..121a443 100644
--- a/tools/cors.js
+++ b/tools/cors.js
@@ -1,11 +1,11 @@
-const {generate} = require('../src/server/middleware-tools/gen.js');
+const {generate} = require("../src/utils/middleware/middleware.js");
-module.exports = generate((req, _res, head) => {
+module.exports = generate((req, _res) => {
_res.setHeader("Access-Control-Allow-Origin", "*");
_res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
+
return {
req: req,
- res: _res,
- head: head
- }
-});
\ No newline at end of file
+ res: _res
+ };
+});
From e0f79e144a680703e7b116858aba768c5b3a8db3 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:12:34 -0400
Subject: [PATCH 05/29] Delete file-type.js
---
lib/file-type.js | 660 -----------------------------------------------
1 file changed, 660 deletions(-)
delete mode 100644 lib/file-type.js
diff --git a/lib/file-type.js b/lib/file-type.js
deleted file mode 100644
index ef3711b..0000000
--- a/lib/file-type.js
+++ /dev/null
@@ -1,660 +0,0 @@
-// https://en.wikipedia.org/wiki/List_of_file_signatures
-// https://github.com/sindresorhus/file-type/blob/master/core.js
-
-function fromBuffer(buff) {
- const arr = Array.from(buff);
- function check(testArr, options = {}) {
- let offset = 0;
- if ("offset" in options)
- offset = options.offset;
- for (let i in testArr)
- if (testArr[i] != arr[+i + offset] || testArr[i] == '?')
- return false;
- return true;
- }
- function checkString(str) {
- return check(Array.from(Buffer.from(str)));
- }
- if (check([0x42, 0x4D])) {
- return {
- ext: 'bmp',
- mime: 'image/bmp'
- };
- }
-
- if (check([0x0B, 0x77])) {
- return {
- ext: 'ac3',
- mime: 'audio/vnd.dolby.dd-raw'
- };
- }
-
- if (check([0x78, 0x01])) {
- return {
- ext: 'dmg',
- mime: 'application/x-apple-diskimage'
- };
- }
-
- if (check([0x4D, 0x5A])) {
- return {
- ext: 'exe',
- mime: 'application/x-msdownload'
- };
- }
-
- if (check([0x1F, 0xA0]) || check([0x1F, 0x9D])) {
- return {
- ext: 'Z',
- mime: 'application/x-compress'
- };
- }
-
- if (check([0xFF, 0xD8, 0xFF])) {
- return {
- ext: 'jpg',
- mime: 'image/jpeg'
- };
- }
-
- if (check([0x49, 0x49, 0xBC])) {
- return {
- ext: 'jxr',
- mime: 'image/vnd.ms-photo'
- };
- }
-
- if (check([0x1F, 0x8B, 0x8])) {
- return {
- ext: 'gz',
- mime: 'application/gzip'
- };
- }
-
- if (check([0x42, 0x5A, 0x68])) {
- return {
- ext: 'bz2',
- mime: 'application/x-bzip2'
- };
- }
-
- if (checkString('ID3')) {
- return {
- ext: 'mp3',
- mime: 'audio/mpeg'
- };
- }
- if (checkString('MP+')) {
- return {
- ext: 'mpc',
- mime: 'audio/x-musepack'
- };
- }
-
- if ((arr[0] == 0x43 || arr[0] == 0x46) && check([0x57, 0x53], { offset: 1 })) {
- return {
- ext: 'swf',
- mime: 'application/x-shockwave-flash'
- };
- }
-
- if (check([0x47, 0x49, 0x46])) {
- return {
- ext: 'gif',
- mime: 'image/gif'
- };
- }
-
- if (checkString('FLIF')) {
- return {
- ext: 'flif',
- mime: 'image/flif'
- };
- }
-
- if (checkString('8BPS')) {
- return {
- ext: 'psd',
- mime: 'image/vnd.adobe.photoshop'
- };
- }
-
- if (checkString('WEBP', { offset: 8 })) {
- return {
- ext: 'webp',
- mime: 'image/webp'
- };
- }
-
- if (checkString('MPCK')) {
- return {
- ext: 'mpc',
- mime: 'audio/x-musepack'
- };
- }
-
- if (checkString('FORM')) {
- return {
- ext: 'aif',
- mime: 'audio/aiff'
- };
- }
-
- if (checkString('icns')) {
- return {
- ext: 'icns',
- mime: 'image/icns'
- };
- }
-
- if (check([0x50, 0x4B, 0x3, 0x4])) {
- return {
- ext: 'zip',
- mime: 'application/zip'
- };
- }
-
- if (checkString('OggS')) {
- return {
- ext: 'ogx',
- mime: 'application/ogg'
- };
- }
-
- if (check([0x50, 0x4B]) && (arr[2] == 0x3 || arr[2] == 0x5 || arr[2] == 0x7) && (arr[3] == 0x4 || arr[3] == 0x6 || arr[3] == 0x8)) {
- return {
- ext: 'zip',
- mime: 'application/zip'
- };
- }
-
- if (
- checkString('ftyp', { offset: 4 }) &&
- (arr[8] & 0x60) != 0x00
- ) {
- const brandMajor = Buffer.from(arr).toString('binary', 8, 12).replace('\0', ' ').trim();
- switch (brandMajor) {
- case 'avif':
- return { ext: 'avif', mime: 'image/avif' };
- case 'mif1':
- return { ext: 'heic', mime: 'image/heif' };
- case 'msf1':
- return { ext: 'heic', mime: 'image/heif-sequence' };
- case 'heic':
- case 'heix':
- return { ext: 'heic', mime: 'image/heic' };
- case 'hevc':
- case 'hevx':
- return { ext: 'heic', mime: 'image/heic-sequence' };
- case 'qt':
- return { ext: 'mov', mime: 'video/quicktime' };
- case 'M4V':
- case 'M4VH':
- case 'M4VP':
- return { ext: 'm4v', mime: 'video/x-m4v' };
- case 'M4P':
- return { ext: 'm4p', mime: 'video/mp4' };
- case 'M4B':
- return { ext: 'm4b', mime: 'audio/mp4' };
- case 'M4A':
- return { ext: 'm4a', mime: 'audio/x-m4a' };
- case 'F4V':
- return { ext: 'f4v', mime: 'video/mp4' };
- case 'F4P':
- return { ext: 'f4p', mime: 'video/mp4' };
- case 'F4A':
- return { ext: 'f4a', mime: 'audio/mp4' };
- case 'F4B':
- return { ext: 'f4b', mime: 'audio/mp4' };
- case 'crx':
- return { ext: 'cr3', mime: 'image/x-canon-cr3' };
- default:
- if (brandMajor.startsWith('3g')) {
- if (brandMajor.startsWith('3g2')) {
- return { ext: '3g2', mime: 'video/3gpp2' };
- }
-
- return { ext: '3gp', mime: 'video/3gpp' };
- }
-
- return { ext: 'mp4', mime: 'video/mp4' };
- }
- }
-
- if (checkString('MThd')) {
- return {
- ext: 'mid',
- mime: 'audio/midi'
- };
- }
-
- if (
- checkString('wOFF') &&
- (
- check([0x00, 0x01, 0x00, 0x00], { offset: 4 }) ||
- checkString('OTTO', { offset: 4 })
- )
- ) {
- return {
- ext: 'woff',
- mime: 'font/woff'
- };
- }
-
- if (
- checkString('wOF2') &&
- (
- check([0x00, 0x01, 0x00, 0x00], { offset: 4 }) ||
- checkString('OTTO', { offset: 4 })
- )
- ) {
- return {
- ext: 'woff2',
- mime: 'font/woff2'
- };
- }
-
- if (check([0xD4, 0xC3, 0xB2, 0xA1]) || check([0xA1, 0xB2, 0xC3, 0xD4])) {
- return {
- ext: 'pcap',
- mime: 'application/vnd.tcpdump.pcap'
- };
- }
-
- if (checkString('DSD ')) {
- return {
- ext: 'dsf',
- mime: 'audio/x-dsf'
- };
- }
-
- if (checkString('LZIP')) {
- return {
- ext: 'lz',
- mime: 'application/x-lzip'
- };
- }
-
- if (checkString('fLaC')) {
- return {
- ext: 'flac',
- mime: 'audio/x-flac'
- };
- }
-
- if (check([0x42, 0x50, 0x47, 0xFB])) {
- return {
- ext: 'bpg',
- mime: 'image/bpg'
- };
- }
-
- if (checkString('wvpk')) {
- return {
- ext: 'wv',
- mime: 'audio/wavpack'
- };
- }
-
- if (checkString('%PDF')) {
- return {
- ext: 'pdf',
- mime: 'application/pdf'
- };
- }
-
- if (check([0x00, 0x61, 0x73, 0x6D])) {
- return {
- ext: 'wasm',
- mime: 'application/wasm'
- };
- }
-
- if (check([0x49, 0x49, 0x2A, 0x0])) {
- if (checkString('CR', { offset: 8 })) {
- return {
- ext: 'cr2',
- mime: 'image/x-canon-cr2'
- };
- }
-
- if (check([0x1C, 0x00, 0xFE, 0x00], { offset: 8 }) || check([0x1F, 0x00, 0x0B, 0x00], { offset: 8 })) {
- return {
- ext: 'nef',
- mime: 'image/x-nikon-nef'
- };
- }
-
- if (
- check([0x08, 0x00, 0x00, 0x00], { offset: 4 }) &&
- (check([0x2D, 0x00, 0xFE, 0x00], { offset: 8 }) ||
- check([0x27, 0x00, 0xFE, 0x00], { offset: 8 }))
- ) {
- return {
- ext: 'dng',
- mime: 'image/x-adobe-dng'
- };
- }
-
- return {
- ext: 'tif',
- mime: 'image/tiff'
- };
- }
-
- if (check([0x4D, 0x4D, 0x0, 0x2A])) {
- return {
- ext: 'tif',
- mime: 'image/tiff'
- };
- }
-
- if (checkString('MAC ')) {
- return {
- ext: 'ape',
- mime: 'audio/ape'
- };
- }
-
- if (check([0x1A, 0x45, 0xDF, 0xA3])) {
- return {
- ext: 'webm',
- mime: 'video/webm'
- };
- }
-
- if (check([0x52, 0x49, 0x46, 0x46])) {
- if (check([0x41, 0x56, 0x49], { offset: 8 })) {
- return {
- ext: 'avi',
- mime: 'video/vnd.avi'
- };
- }
-
- if (check([0x57, 0x41, 0x56, 0x45], { offset: 8 })) {
- return {
- ext: 'wav',
- mime: 'audio/vnd.wave'
- };
- }
-
- if (check([0x51, 0x4C, 0x43, 0x4D], { offset: 8 })) {
- return {
- ext: 'qcp',
- mime: 'audio/qcelp'
- };
- }
- }
-
- if (checkString('SQLi')) {
- return {
- ext: 'sqlite',
- mime: 'application/x-sqlite3'
- };
- }
-
- if (check([0x4E, 0x45, 0x53, 0x1A])) {
- return {
- ext: 'nes',
- mime: 'application/x-nintendo-nes-rom'
- };
- }
-
- if (checkString('Cr24')) {
- return {
- ext: 'crx',
- mime: 'application/x-google-chrome-extension'
- };
- }
-
- if (
- checkString('MSCF') ||
- checkString('ISc(')
- ) {
- return {
- ext: 'cab',
- mime: 'application/vnd.ms-cab-compressed'
- };
- }
-
- if (check([0xED, 0xAB, 0xEE, 0xDB])) {
- return {
- ext: 'rpm',
- mime: 'application/x-rpm'
- };
- }
-
- if (check([0xC5, 0xD0, 0xD3, 0xC6])) {
- return {
- ext: 'eps',
- mime: 'application/eps'
- };
- }
-
-
- if (check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
- return {
- ext: 'otf',
- mime: 'font/otf'
- };
- }
-
- if (checkString('#!AMR')) {
- return {
- ext: 'amr',
- mime: 'audio/amr'
- };
- }
-
- if (checkString('{\\rtf')) {
- return {
- ext: 'rtf',
- mime: 'application/rtf'
- };
- }
-
- if (check([0x46, 0x4C, 0x56, 0x01])) {
- return {
- ext: 'flv',
- mime: 'video/x-flv'
- };
- }
-
- if (checkString('IMPM')) {
- return {
- ext: 'it',
- mime: 'audio/x-it'
- };
- }
-
- if (
- checkString('-lh0-', { offset: 2 }) ||
- checkString('-lh1-', { offset: 2 }) ||
- checkString('-lh2-', { offset: 2 }) ||
- checkString('-lh3-', { offset: 2 }) ||
- checkString('-lh4-', { offset: 2 }) ||
- checkString('-lh5-', { offset: 2 }) ||
- checkString('-lh6-', { offset: 2 }) ||
- checkString('-lh7-', { offset: 2 }) ||
- checkString('-lzs-', { offset: 2 }) ||
- checkString('-lz4-', { offset: 2 }) ||
- checkString('-lz5-', { offset: 2 }) ||
- checkString('-lhd-', { offset: 2 })
- ) {
- return {
- ext: 'lzh',
- mime: 'application/x-lzh-compressed'
- };
- }
-
-
-
- if (check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
- return {
- ext: 'xz',
- mime: 'application/x-xz'
- };
- }
-
- if (checkString('')) {
- return {
- ext: 'ar',
- mime: 'application/x-unix-archive'
- };
- }
-
- if (check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
- return {
- ext: 'png',
- mime: 'image/png'
- };
- }
- if (check([0x41, 0x52, 0x52, 0x4F, 0x57, 0x31, 0x00, 0x00])) {
- return {
- ext: 'arrow',
- mime: 'application/x-apache-arrow'
- };
- }
-
- if (check([0x67, 0x6C, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00])) {
- return {
- ext: 'glb',
- mime: 'model/gltf-binary'
- };
- }
- if (
- check([0x66, 0x72, 0x65, 0x65], { offset: 4 }) ||
- check([0x6D, 0x64, 0x61, 0x74], { offset: 4 }) ||
- check([0x6D, 0x6F, 0x6F, 0x76], { offset: 4 }) ||
- check([0x77, 0x69, 0x64, 0x65], { offset: 4 })
- ) {
- return {
- ext: 'mov',
- mime: 'video/quicktime'
- };
- }
-
-
- if (check([0x49, 0x49, 0x52, 0x4F, 0x08, 0x00, 0x00, 0x00, 0x18])) {
- return {
- ext: 'orf',
- mime: 'image/x-olympus-orf'
- };
- }
- if (check([0x49, 0x49, 0x55, 0x00, 0x18, 0x00, 0x00, 0x00, 0x88, 0xE7, 0x74, 0xD8])) {
- return {
- ext: 'rw2',
- mime: 'image/x-panasonic-rw2'
- };
- }
-
-
- if (check([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) {
- return {
- ext: 'ktx',
- mime: 'image/ktx'
- };
- }
-
- if ((check([0x7E, 0x10, 0x04]) || check([0x7E, 0x18, 0x04])) && check([0x30, 0x4D, 0x49, 0x45], { offset: 4 })) {
- return {
- ext: 'mie',
- mime: 'application/x-mie'
- };
- }
-
- if (check([0x27, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], { offset: 2 })) {
- return {
- ext: 'shp',
- mime: 'application/x-esri-shape'
- };
- }
-
- if (
- check([0x0, 0x0, 0x1, 0xBA]) ||
- check([0x0, 0x0, 0x1, 0xB3])
- ) {
- return {
- ext: 'mpg',
- mime: 'video/mpeg'
- };
- }
-
- if (check([0x00, 0x01, 0x00, 0x00, 0x00])) {
- return {
- ext: 'ttf',
- mime: 'font/ttf'
- };
- }
-
- if (check([0x00, 0x00, 0x01, 0x00])) {
- return {
- ext: 'ico',
- mime: 'image/x-icon'
- };
- }
-
- if (check([0x00, 0x00, 0x02, 0x00])) {
- return {
- ext: 'cur',
- mime: 'image/x-icon'
- };
- }
-
- if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
- return {
- ext: 'cfb',
- mime: 'application/x-cfb'
- };
- }
-
- return undefined;
-}
-
-function fromFile(file) {
- return fromBuffer(require('fs').readFileSync(file));
-}
-
-module.exports = {
- fromBuffer: fromBuffer,
- fromFile: fromFile
-}
\ No newline at end of file
From eb7b3566b9547302d8e12b7f57cd5ac575ed3e1c Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:12:39 -0400
Subject: [PATCH 06/29] Delete html-tags.json
---
lib/html-tags.json | 119 ---------------------------------------------
1 file changed, 119 deletions(-)
delete mode 100644 lib/html-tags.json
diff --git a/lib/html-tags.json b/lib/html-tags.json
deleted file mode 100644
index 5c58945..0000000
--- a/lib/html-tags.json
+++ /dev/null
@@ -1,119 +0,0 @@
-[
- "a",
- "abbr",
- "address",
- "area",
- "article",
- "aside",
- "audio",
- "b",
- "base",
- "bdi",
- "bdo",
- "blockquote",
- "body",
- "br",
- "button",
- "canvas",
- "caption",
- "cite",
- "code",
- "col",
- "colgroup",
- "data",
- "datalist",
- "dd",
- "del",
- "details",
- "dfn",
- "dialog",
- "div",
- "dl",
- "dt",
- "em",
- "embed",
- "fieldset",
- "figcaption",
- "figure",
- "footer",
- "form",
- "h1",
- "h2",
- "h3",
- "h4",
- "h5",
- "h6",
- "head",
- "header",
- "hgroup",
- "hr",
- "html",
- "i",
- "iframe",
- "img",
- "input",
- "ins",
- "kbd",
- "label",
- "legend",
- "li",
- "link",
- "main",
- "map",
- "mark",
- "math",
- "menu",
- "menuitem",
- "meta",
- "meter",
- "nav",
- "noscript",
- "object",
- "ol",
- "optgroup",
- "option",
- "output",
- "p",
- "param",
- "picture",
- "pre",
- "progress",
- "q",
- "rb",
- "rp",
- "rt",
- "rtc",
- "ruby",
- "s",
- "samp",
- "script",
- "section",
- "select",
- "slot",
- "small",
- "source",
- "span",
- "strong",
- "style",
- "sub",
- "summary",
- "sup",
- "svg",
- "table",
- "tbody",
- "td",
- "template",
- "textarea",
- "tfoot",
- "th",
- "thead",
- "time",
- "title",
- "tr",
- "track",
- "u",
- "ul",
- "var",
- "video",
- "wbr"
-]
\ No newline at end of file
From 0278307fe20593b82a1a288edaff2fc7ae462982 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:12:46 -0400
Subject: [PATCH 07/29] Delete is-html.js
---
lib/is-html.js | 5 -----
1 file changed, 5 deletions(-)
delete mode 100644 lib/is-html.js
diff --git a/lib/is-html.js b/lib/is-html.js
deleted file mode 100644
index 03acbcb..0000000
--- a/lib/is-html.js
+++ /dev/null
@@ -1,5 +0,0 @@
-const tags = require('./html-tags.json');
-
-const tester = new RegExp('(\\s*|(|
|]+>)+)|'+tags.map(t=>`<${t}[/\\s]*>`).join('|'));
-
-module.exports = str => tester.test(str.toString().toLowerCase());
\ No newline at end of file
From 2a7bb79ef636c924bb005ca7988c855446688227 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:13:03 -0400
Subject: [PATCH 08/29] Update qs.js
---
lib/qs.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lib/qs.js b/lib/qs.js
index 20b2d4e..26fe269 100644
--- a/lib/qs.js
+++ b/lib/qs.js
@@ -5,7 +5,7 @@ function processData(__data){
let d = {}, keys = __data.map(n=>n[0]), data = __data.map(n=>n[1]);
for (let i = 0; i < keys.length; i++) {
let current = d;
- let keyParts = keys[i].split('[').join('.').split(']').join('').split('.');
+ let keyParts = keys[i].split('[').join('.').split(']').join('').split('.');
for (let j = 0; j < keyParts.length; j++){
let k = keyParts[j];
@@ -32,4 +32,4 @@ function parse(data) {
return processData(d);
}
-module.exports = parse;
\ No newline at end of file
+module.exports = parse;
From 478824397ff644025ef67460de24a997df575013 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:13:22 -0400
Subject: [PATCH 09/29] Delete index.js
---
src/server/index.js | 38 --------------------------------------
1 file changed, 38 deletions(-)
delete mode 100644 src/server/index.js
diff --git a/src/server/index.js b/src/server/index.js
deleted file mode 100644
index 8ff1e7a..0000000
--- a/src/server/index.js
+++ /dev/null
@@ -1,38 +0,0 @@
-const http = require('http');
-const Response = require('./response/index.js');
-
-class Server {
- constructor() {
- this.utils = [];
- this.server;
- this.code = (req, res) => {
- let body = '';
- req.on('data', chunk => {
- //console.log('chunk');
- body += chunk;
- });
- req.on('end', () => {
- const _req = Object.assign({}, req);
- let data = {
- req: Object.assign(_req, {body: body}),
- res: res,
- head: {}
- };
- this.utils.forEach(util => {
- data = util(data);
- });
- });
- };
- }
- use(fn) {
- this.utils.push(fn);
- }
- create() {
- this.server = http.createServer(this.code);
- }
- listen(port = 3000) {
- this.server.listen(port);
- }
-}
-
-module.exports = Server;
\ No newline at end of file
From 142c3066e8073e99b7594ce03e866729e9e46eed Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:13:37 -0400
Subject: [PATCH 10/29] Delete compare.js
---
src/server/router/url-tools/compare.js | 62 --------------------------
1 file changed, 62 deletions(-)
delete mode 100644 src/server/router/url-tools/compare.js
diff --git a/src/server/router/url-tools/compare.js b/src/server/router/url-tools/compare.js
deleted file mode 100644
index d93f5df..0000000
--- a/src/server/router/url-tools/compare.js
+++ /dev/null
@@ -1,62 +0,0 @@
-const {normalize} = require('path');
-
-
-const getPathArray = path => path.split("?")[0].split("#")[0].split('/').filter(n=>n.length>0);
-
-const compare = (path1, path2) => getPathArray(path1).join('/') == getPathArray(path2).join('/');
-
-function compareWithParams(path1, path2, req) {
- let path = getPathArray(path1);
- let params = [];
- for(let i in path) {
- if(path[i].includes('$')) {
- params.push(path[i].slice(path[i].indexOf('$')+1));
- path[i] = path[i].slice(0, path[i].indexOf('$'))+'$';
- }
- }
- let cmpPath = getPathArray(path2);
- if(cmpPath.length != path.length)
- return false;
- let paramID = 0;
- let results = {};
- for(let i in cmpPath) {
- if(path[i].includes('$')) {
- let before = cmpPath[i].slice(0, path[i].indexOf('$'))+'$';
- if(before != path[i])
- return false
- let after = cmpPath[i].slice(path[i].indexOf('$'));
- results[params[paramID]] = after;
- paramID++;
- } else {
- if(path[i] != cmpPath[i])
- return false;
- }
- }
- if(!req.urlParams) {
- req.urlParams = {};
- }
- for(let i in results) {
- req.urlParams[i] = results[i];
- }
- return true;
-}
-
-function compareBases(baseDir, fullDir) {
- let base = getPathArray(normalize(baseDir));
- let full = getPathArray(normalize(fullDir));
- if(full.length < base.length)
- return false;
- for(let i in base) {
- if(full[i] != base[i])
- return false;
- }
- return true;
-}
-
-
-/*
-let testReq = {};
-console.log(compareWithParams("/users/$username/projects/$projectID/", "/users/Test/projects/2123", testReq), testReq);*/
-
-
-module.exports = {compare: compare, compareWithParams: compareWithParams, compareBases: compareBases};
From 186e7f180d8737d45f2eddb7001de841278b8d6f Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:13:44 -0400
Subject: [PATCH 11/29] Delete route.js
---
src/server/router/route.js | 75 --------------------------------------
1 file changed, 75 deletions(-)
delete mode 100644 src/server/router/route.js
diff --git a/src/server/router/route.js b/src/server/router/route.js
deleted file mode 100644
index 7d5e4ff..0000000
--- a/src/server/router/route.js
+++ /dev/null
@@ -1,75 +0,0 @@
-const Response = require('../response/index.js');
-const Request = require('../request/index.js');
-const {generate, package} = require('../middleware-tools/gen.js');
-const {compare, compareWithParams, compareBases} = require('./url-tools/compare.js');
-
-function generateRoute(router) {
- const route = generate((req, res, head) => {
- const response = new Response(res)
- const request = new Request(req)
- const paths = router.paths;
- const statics = router.statics;
- let asynchronous = false;
- let found = false;
- paths.forEach(path => {
- if(request.method == path.method) {
- if(compare(request.req.url, path.path)) {
- found = true;
- if(path.asynchronous) {
- asynchronous = true;
- }
- if(path.fn(request, response) == "ERR")
- found = false;
- } else if(compareWithParams(path.path, request.req.url, request)) {
- found = true;
- if(path.asynchronous) {
- asynchronous = true;
- }
- if(path.fn(request, response) == "ERR")
- found = false;
- };
- }
- });
- if(!found) {
- statics.forEach(path => {
- //path.path
- if(compareBases(path.path, request.req.url)) {
- let part = decodeURIComponent(require('path').normalize(request.req.url).replace(path.path, ''));
- if(path.file[path.file.length - 1] != '/' && part[0] != '/')
- part = '/' + part;
- try {
- if(require('fs').lstatSync(path.file + part).isFile()) {
- response.sendFile(path.file + part);
- found = true;
- } else if(require('fs').existsSync(path.file + part + 'index.html') && require('fs').lstatSync(path.file + part + 'index.html').isFile()) {
- response.sendFile(path.file + part + 'index.html');
- found = true;
- } else if(require('fs').existsSync(path.file + part + '/index.html') && require('fs').lstatSync(path.file + part + '/index.html').isFile()) {
- response.send(``);
- found = true;
- } else {
- response.send("Cannot find file " + path.file + part);
- found = true;
- }
- } catch(e) {
- // Nothing...
- }
- }
- });
- }
- if(!found) {
- response.send(`Cannot ${request.method} ${request.req.url}`);
- }
- if(!asynchronous) {
- response.finish();
- }
- return {
- req: req,
- res: res,
- head: head
- };
- });
- return route;
-}
-
-module.exports = generateRoute;
From b05e6ea4fe77ad17ec1723fce823c85d41885875 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:13:49 -0400
Subject: [PATCH 12/29] Delete router.js
---
src/server/router/router.js | 39 -------------------------------------
1 file changed, 39 deletions(-)
delete mode 100644 src/server/router/router.js
diff --git a/src/server/router/router.js b/src/server/router/router.js
deleted file mode 100644
index 46d6df7..0000000
--- a/src/server/router/router.js
+++ /dev/null
@@ -1,39 +0,0 @@
-const {normalize} = require('path');
-
-class Router {
- constructor() {
- this.paths = [];
- this.statics = [];
- }
- _addPath(method, path, fn, asynchronous) {
- this.paths.push({
- method: method,
- path: path,
- fn: fn,
- asynchronous: asynchronous
- });
- }
- get(path, fn, asynchronous=false) {
- this._addPath('GET', path, fn, asynchronous);
- }
- post(path, fn, asynchronous=false) {
- this._addPath('POST', path, fn, asynchronous);
- }
- all(path, fn, asynchronous=false) {
- this.get(path, fn, asynchronous);
- this.post(path, fn, asynchronous);
- }
- static(path, file) {
- if(normalize(path)[0] != '/' || normalize(file)[0] != '/') {
- console.error(new Error("Static path must be absolute"));
- return false;
- }
- this.statics.push({
- path: normalize(path),
- file: normalize(file)
- });
- return true;
- }
-}
-
-module.exports = Router;
\ No newline at end of file
From ddd76de6d2e83d8aea6c644c45f151941f13762c Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:14:00 -0400
Subject: [PATCH 13/29] Delete middleware.js
---
src/server/body-parser/middleware.js | 12 ------------
1 file changed, 12 deletions(-)
delete mode 100644 src/server/body-parser/middleware.js
diff --git a/src/server/body-parser/middleware.js b/src/server/body-parser/middleware.js
deleted file mode 100644
index f4b51a4..0000000
--- a/src/server/body-parser/middleware.js
+++ /dev/null
@@ -1,12 +0,0 @@
-const {generate, package} = require('../middleware-tools/gen.js');
-const parse = require('./parser.js');
-
-module.exports = generate((req, res, head) => {
- const _req = req;
- _req.body = parse(_req.body);
- return {
- req: _req,
- res: res,
- head: head
- }
-});
\ No newline at end of file
From 473f801ea4b158f5a9e263045158619881930e38 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:14:05 -0400
Subject: [PATCH 14/29] Delete parser.js
---
src/server/body-parser/parser.js | 44 --------------------------------
1 file changed, 44 deletions(-)
delete mode 100644 src/server/body-parser/parser.js
diff --git a/src/server/body-parser/parser.js b/src/server/body-parser/parser.js
deleted file mode 100644
index fdf6c98..0000000
--- a/src/server/body-parser/parser.js
+++ /dev/null
@@ -1,44 +0,0 @@
-let qs = {};//require('qs');
-//let qs = require('qs');
-qs.parse = require('../../../lib/qs.js');//require('qs');
-
-function parse(data,log=false) {
- try {
- return JSON.parse(data);
- } catch (err) {
- if(log) {
- console.log('Not JSON');
- }
- }
- try {
- return qs.parse(data);
- } catch (err) {
- if(log) {
- console.log('Not urlencoded');
- }
- }
- try {
- return qs.parse(data.toString());
- } catch (err) {
- if(log) {
- console.log('Not buffer urlencoded');
- }
- }
- try {
- return data.toString();
- } catch (err) {
- if(log) {
- console.log('Not buffer');
- }
- }
- try {
- return data;
- } catch (err) {
- if(log) {
- console.log('Not txt');
- }
- }
- return {};
-}
-
-module.exports = parse;
\ No newline at end of file
From a45aad55ed523729663ebeb2b7af67b71fb6ad10 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:14:14 -0400
Subject: [PATCH 15/29] Delete index.js
---
src/server/response/mime/index.js | 46 -------------------------------
1 file changed, 46 deletions(-)
delete mode 100644 src/server/response/mime/index.js
diff --git a/src/server/response/mime/index.js b/src/server/response/mime/index.js
deleted file mode 100644
index 089fa98..0000000
--- a/src/server/response/mime/index.js
+++ /dev/null
@@ -1,46 +0,0 @@
-const FileType = require('../../../../lib/file-type.js');
-const fs = require('fs');
-const isHTML = require('../../../../lib/is-html.js');
-
-
-const extensions = {
- js: 'application/javascript',
- css: 'text/css',
- html: 'text/html',
- xml: 'text/xml'
-};
-
-function mimeTypeFromFile(filePath, callback){
- (async () => {
- //let data = await FileType.fromFile(filePath);
- let data = FileType.fromFile(filePath);
- if(data == undefined) {
- const ext = filePath.split('.')[filePath.split('.').length - 1];
- if(ext in extensions) {
- callback(extensions[ext]);
- } else {
- callback(isHTML(fs.readFileSync(filePath))?'text/html':'text/plain');
- }
- } else {
- callback(data.mime);
- }
- })();
-}
-
-function mimeTypeFromString(str, callback){
- const buffer = Buffer.from(str);
- (async () => {
- //let data = await FileType.fromBuffer(buffer);
- let data = FileType.fromBuffer(buffer);
- if(data == undefined) {
- callback(isHTML(str)?'text/html':'text/plain');
- } else {
- callback(data.mime);
- }
- })();
-}
-
-module.exports = {
- file: mimeTypeFromFile,
- string: mimeTypeFromString
-};
\ No newline at end of file
From e4833c84c71092d38b9d58a8e3075e5f54c2a338 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:14:19 -0400
Subject: [PATCH 16/29] Delete index.js
---
src/server/response/index.js | 61 ------------------------------------
1 file changed, 61 deletions(-)
delete mode 100644 src/server/response/index.js
diff --git a/src/server/response/index.js b/src/server/response/index.js
deleted file mode 100644
index 749edbf..0000000
--- a/src/server/response/index.js
+++ /dev/null
@@ -1,61 +0,0 @@
-const fs = require('fs');
-
-const mime = require('./mime/index.js');
-
-
-class Response {
- constructor(res) {
- this.res = res;
- this.content = '';
- this.mime = 'text/plain';
- this.fileTemp = '';
- this.check = true;
- }
- send(data) {
- this.content += String(data);
- this.mime = {lookup: 'content'};
- }
- sendFile(path) {
- this.content = fs.readFileSync(path);
- this.mime = {lookup: 'file'};
- this.fileTemp = path;
- }
- sendJSON(data) {
- this.content = JSON.stringify(data);
- this.mime = 'application/json';
- }
- setHeader(header, value) {
- this.res.setHeader(header, value);
- if(header == 'Content-Type') {
- this.check = false;
- }
- }
- finish() {
- if(this.check){
- if(typeof(this.mime) == 'object' && "lookup" in this.mime) {
- if(this.mime.lookup == 'content') {
- mime.string(this.content, (data) => {
- this.res.setHeader('Content-Type', data);
- this.res.write(this.content);
- this.res.end();
- })
- } else {
- mime.file(this.fileTemp, (data) => {
- this.res.setHeader('Content-Type', data);
- this.res.write(this.content);
- this.res.end();
- })
- }
- } else {
- this.res.setHeader('Content-Type', this.mime);
- this.res.write(this.content);
- this.res.end();
- }
- } else {
- this.res.write(this.content);
- this.res.end();
- }
- }
-}
-
-module.exports = Response;
\ No newline at end of file
From 6fa2da6be6c6e6f7c345c987cdd0c7ac1bcf59ee Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:14:26 -0400
Subject: [PATCH 17/29] Delete index.js
---
src/server/request/index.js | 27 ---------------------------
1 file changed, 27 deletions(-)
delete mode 100644 src/server/request/index.js
diff --git a/src/server/request/index.js b/src/server/request/index.js
deleted file mode 100644
index 64e0034..0000000
--- a/src/server/request/index.js
+++ /dev/null
@@ -1,27 +0,0 @@
-class Request {
- constructor(req) {
- this.req = req;
- }
- get body() {
- return this.req.body;
- }
- getHeader(header) {
- return this.req.headers[header];
- }
- get method() {
- return this.req.method;
- }
- get url() {
- return new URL(this.req.url, `http://${this.req.headers.host}`);
- }
- get status() {
- return {
- code: this.req.statusCode,
- message: this.req.statusMessage
- };
- }
- get headers() {
- return this.req.headers;
- }
-}
-module.exports = Request;
\ No newline at end of file
From 50ae93af0bbb1c2ec8f6ec4bff56b7cf9897ef44 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:14:32 -0400
Subject: [PATCH 18/29] Delete gen.js
---
src/server/middleware-tools/gen.js | 14 --------------
1 file changed, 14 deletions(-)
delete mode 100644 src/server/middleware-tools/gen.js
diff --git a/src/server/middleware-tools/gen.js b/src/server/middleware-tools/gen.js
deleted file mode 100644
index bf749c1..0000000
--- a/src/server/middleware-tools/gen.js
+++ /dev/null
@@ -1,14 +0,0 @@
-function genMiddleware(fn) {
- return data => fn(data.req, data.res, data.head);
-}
-
-const package = (req, res, head) => {return {
- req: req,
- res: res,
- head: head
-}};
-
-module.exports = {
- generate: genMiddleware,
- package: package
-};
\ No newline at end of file
From bdf02e45769941953e3f3ba7cb8fd7a2fa90c305 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:15:00 -0400
Subject: [PATCH 19/29] Create server.js
---
src/server.js | 43 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
create mode 100644 src/server.js
diff --git a/src/server.js b/src/server.js
new file mode 100644
index 0000000..da9d38a
--- /dev/null
+++ b/src/server.js
@@ -0,0 +1,43 @@
+const http = require("http");
+const addDefaults = require("./defaults.js");
+
+class Server {
+ constructor() {
+ this.utils = [];
+ this.server;
+ this.code = (req, res) => {
+ (async() => {
+ let data = {req: req, res: res};
+ for(let util of this.utils) {
+ let utilRes;
+ if(util.constructor.name == "AsyncFunction") {
+ utilRes = await util(data);
+ } else {
+ utilRes = util(data);
+ }
+ data = Object.assign(Object.assign({}, data), utilRes);
+ }
+ })();
+ }
+
+ addDefaults(this);
+ }
+
+ use(util) {
+ this.utils.push(util);
+ }
+
+ create() {
+ this.server = http.createServer(this.code);
+ }
+
+ getCode() {
+ return this.code;
+ }
+
+ listen(port) {
+ this.server.listen(port);
+ }
+}
+
+module.exports = Server;
From d7cc3ec75bdac1bdbb486c59389418016ebc932f Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:15:26 -0400
Subject: [PATCH 20/29] Create defaults.js
---
src/defaults.js | 9 +++++++++
1 file changed, 9 insertions(+)
create mode 100644 src/defaults.js
diff --git a/src/defaults.js b/src/defaults.js
new file mode 100644
index 0000000..61d25f2
--- /dev/null
+++ b/src/defaults.js
@@ -0,0 +1,9 @@
+const wrap = require("./utils/wrapper.js");
+const bp = require("./utils/middleware/body-parser.js");
+
+function addDefaults(server) {
+ server.use(wrap);
+ server.use(bp);
+}
+
+module.exports = addDefaults;
From 6718c9954496e522f1e122bbf0c4f6089384d615 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:16:11 -0400
Subject: [PATCH 21/29] Create wrapper.js
---
src/utils/wrapper.js | 8 ++++++++
1 file changed, 8 insertions(+)
create mode 100644 src/utils/wrapper.js
diff --git a/src/utils/wrapper.js b/src/utils/wrapper.js
new file mode 100644
index 0000000..b3ce0a4
--- /dev/null
+++ b/src/utils/wrapper.js
@@ -0,0 +1,8 @@
+const Request = require("./wrappers/req.js");
+const Response = require("./wrappers/res.js");
+
+function wrap(data) {
+ return {res: new Response(data.res), req: new Request(data.req)};
+}
+
+module.exports = wrap;
From 6cadd1a156d9b6dd2a1dad73cc6f668c2dccf1eb Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:16:32 -0400
Subject: [PATCH 22/29] Create body-parser.js
---
src/utils/body-parser.js | 44 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
create mode 100644 src/utils/body-parser.js
diff --git a/src/utils/body-parser.js b/src/utils/body-parser.js
new file mode 100644
index 0000000..b5c5215
--- /dev/null
+++ b/src/utils/body-parser.js
@@ -0,0 +1,44 @@
+let qs = {};//require('qs');
+//let qs = require('qs');
+qs.parse = require('../../lib/qs.js');//require('qs');
+
+function parse(data,log=false) {
+ try {
+ return JSON.parse(data);
+ } catch (err) {
+ if(log) {
+ console.log('Not JSON');
+ }
+ }
+ try {
+ return qs.parse(data);
+ } catch (err) {
+ if(log) {
+ console.log('Not urlencoded');
+ }
+ }
+ try {
+ return qs.parse(data.toString());
+ } catch (err) {
+ if(log) {
+ console.log('Not buffer urlencoded');
+ }
+ }
+ try {
+ return data.toString();
+ } catch (err) {
+ if(log) {
+ console.log('Not buffer');
+ }
+ }
+ try {
+ return data;
+ } catch (err) {
+ if(log) {
+ console.log('Not txt');
+ }
+ }
+ return {};
+}
+
+module.exports = parse;
From 73140cffaae00dc8578a64db1b101d9a8d64b3cf Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:16:52 -0400
Subject: [PATCH 23/29] Create req.js
---
src/utils/wrappers/req.js | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
create mode 100644 src/utils/wrappers/req.js
diff --git a/src/utils/wrappers/req.js b/src/utils/wrappers/req.js
new file mode 100644
index 0000000..db76463
--- /dev/null
+++ b/src/utils/wrappers/req.js
@@ -0,0 +1,35 @@
+class Request {
+ constructor(req) {
+ this.raw = req;
+ }
+
+ getHeader(header) {
+ return this.raw.headers[header];
+ }
+
+ getHeaders() {
+ return this.raw.headers;
+ }
+
+ get method() {
+ return this.raw.method;
+ }
+
+ get url() {
+ return new URL(this.raw.url, `http://${this.raw.headers.host}`);
+ }
+
+ get status() {
+ return {
+ code: this.raw.statusCode,
+ message: this.raw.statusMessage
+ };
+ }
+
+ // Backwards Compatibility
+ get headers() {
+ return this.raw.headers;
+ }
+}
+
+module.exports = Request;
From 4c6666675bfbd1ef301c73c02f6b8599218bf16a Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:17:15 -0400
Subject: [PATCH 24/29] Create res.js
---
src/utils/wrappers/res.js | 44 +++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
create mode 100644 src/utils/wrappers/res.js
diff --git a/src/utils/wrappers/res.js b/src/utils/wrappers/res.js
new file mode 100644
index 0000000..afe6c02
--- /dev/null
+++ b/src/utils/wrappers/res.js
@@ -0,0 +1,44 @@
+const fs = require("fs");
+
+class Response {
+ constructor(res) {
+ this.raw = res;
+ this.content = "";
+ }
+
+ send(data) {
+ this.content += data;
+ }
+
+ sendFile(path) {
+ this.content = fs.readFileSync(path);
+ }
+
+ sendJSON(data) {
+ this.content = JSON.stringify(data);
+ this.raw.setHeader("Content-Type", "application/json");
+ }
+
+ setHeader(header, value) {
+ this.raw.setHeader(header, value);
+ }
+
+ setMime(type) {
+ this.raw.setHeader("Content-Type", type);
+ }
+
+ getHeaders() {
+ return this.raw.getHeaders();
+ }
+
+ setStatusCode(code) {
+ this.raw.writeHead(code);
+ }
+
+ finish() {
+ this.raw.write(this.content);
+ this.raw.end();
+ }
+}
+
+module.exports = Response;
From 3c5f775c114eb8f3678d1aa646ebed808f99109a Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:17:33 -0400
Subject: [PATCH 25/29] Create body-parser.js
---
src/utils/middleware/body-parser.js | 11 +++++++++++
1 file changed, 11 insertions(+)
create mode 100644 src/utils/middleware/body-parser.js
diff --git a/src/utils/middleware/body-parser.js b/src/utils/middleware/body-parser.js
new file mode 100644
index 0000000..72d3474
--- /dev/null
+++ b/src/utils/middleware/body-parser.js
@@ -0,0 +1,11 @@
+const {generate, package} = require('./middleware.js');
+const parse = require('../body-parser.js');
+
+module.exports = generate((req, res) => {
+ const _req = req;
+ _req.body = parse(_req.body);
+ return {
+ req: _req,
+ res: res
+ }
+});
From 64b49ca5b56d34a42a3af1dcf91ca3616ef2e870 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:17:53 -0400
Subject: [PATCH 26/29] Create middleware.js
---
src/utils/middleware/middleware.js | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 src/utils/middleware/middleware.js
diff --git a/src/utils/middleware/middleware.js b/src/utils/middleware/middleware.js
new file mode 100644
index 0000000..0eea353
--- /dev/null
+++ b/src/utils/middleware/middleware.js
@@ -0,0 +1,18 @@
+function genMiddleware(fn) {
+ return data => fn(data.req, data.res);
+}
+
+function genMiddlewareAsync(fn) {
+ return async data => await fn(data.req, data.res);
+}
+
+const package = (req, res, head) => {return {
+ req: req,
+ res: res
+}};
+
+module.exports = {
+ generate: genMiddleware,
+ genAsync: genMiddlewareAsync,
+ package: package
+};
From 74ea2cb77829b14c14a786cc7ebb293306bc4eeb Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:18:26 -0400
Subject: [PATCH 27/29] Create compare.js
---
src/utils/router/url-tools/compare.js | 62 +++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
create mode 100644 src/utils/router/url-tools/compare.js
diff --git a/src/utils/router/url-tools/compare.js b/src/utils/router/url-tools/compare.js
new file mode 100644
index 0000000..d93f5df
--- /dev/null
+++ b/src/utils/router/url-tools/compare.js
@@ -0,0 +1,62 @@
+const {normalize} = require('path');
+
+
+const getPathArray = path => path.split("?")[0].split("#")[0].split('/').filter(n=>n.length>0);
+
+const compare = (path1, path2) => getPathArray(path1).join('/') == getPathArray(path2).join('/');
+
+function compareWithParams(path1, path2, req) {
+ let path = getPathArray(path1);
+ let params = [];
+ for(let i in path) {
+ if(path[i].includes('$')) {
+ params.push(path[i].slice(path[i].indexOf('$')+1));
+ path[i] = path[i].slice(0, path[i].indexOf('$'))+'$';
+ }
+ }
+ let cmpPath = getPathArray(path2);
+ if(cmpPath.length != path.length)
+ return false;
+ let paramID = 0;
+ let results = {};
+ for(let i in cmpPath) {
+ if(path[i].includes('$')) {
+ let before = cmpPath[i].slice(0, path[i].indexOf('$'))+'$';
+ if(before != path[i])
+ return false
+ let after = cmpPath[i].slice(path[i].indexOf('$'));
+ results[params[paramID]] = after;
+ paramID++;
+ } else {
+ if(path[i] != cmpPath[i])
+ return false;
+ }
+ }
+ if(!req.urlParams) {
+ req.urlParams = {};
+ }
+ for(let i in results) {
+ req.urlParams[i] = results[i];
+ }
+ return true;
+}
+
+function compareBases(baseDir, fullDir) {
+ let base = getPathArray(normalize(baseDir));
+ let full = getPathArray(normalize(fullDir));
+ if(full.length < base.length)
+ return false;
+ for(let i in base) {
+ if(full[i] != base[i])
+ return false;
+ }
+ return true;
+}
+
+
+/*
+let testReq = {};
+console.log(compareWithParams("/users/$username/projects/$projectID/", "/users/Test/projects/2123", testReq), testReq);*/
+
+
+module.exports = {compare: compare, compareWithParams: compareWithParams, compareBases: compareBases};
From a0a138290176e48fcba70e50377d65a5bf890d94 Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:18:41 -0400
Subject: [PATCH 28/29] Create route.js
---
src/utils/router/route.js | 84 +++++++++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
create mode 100644 src/utils/router/route.js
diff --git a/src/utils/router/route.js b/src/utils/router/route.js
new file mode 100644
index 0000000..b13da21
--- /dev/null
+++ b/src/utils/router/route.js
@@ -0,0 +1,84 @@
+const fs = require("fs");
+
+const Response = require('../wrappers/res.js');
+const Request = require('../wrappers/req.js');
+const {genAsync, package} = require('../middleware/middleware.js');
+const {compare, compareWithParams, compareBases} = require('./url-tools/compare.js');
+
+function generateRoute(router) {
+ const route = genAsync(async (req, res) => {
+ const paths = router.paths;
+ const statics = router.statics;
+
+ let found = false;
+ for(const path of paths) {
+ if(req.method == path.method) {
+ if(compare(req.raw.url, path.path)) {
+ found = true;
+
+ try {
+ if(path.fn.constructor.name == "AsyncFunction") {
+ await path.fn(req, res);
+ } else {
+ path.fn(req, res);
+ }
+ } catch(e) {
+ found = false;
+ }
+ } else if(compareWithParams(path.path, req.raw.url, req)) {
+ found = true;
+
+ try {
+ if(path.fn.constructor.name == "AsyncFunction") {
+ await path.fn(req, res);
+ } else {
+ path.fn(req, res);
+ }
+ } catch(e) {
+ found = false;
+ }
+ }
+ }
+ }
+ if(!found) {
+ statics.forEach(path => {
+ //path.path
+ if(compareBases(path.path, req.raw.url)) {
+ let part = decodeURIComponent(require('path').normalize(req.raw.url).replace(path.path, ''));
+ if(path.file[path.file.length - 1] != '/' && part[0] != '/')
+ part = '/' + part;
+ try {
+ if(fs.lstatSync(path.file + part).isFile()) {
+ res.sendFile(path.file + part);
+ found = true;
+ } else if(fs.existsSync(path.file + part + 'index.html') && fs.lstatSync(path.file + part + 'index.html').isFile()) {
+ res.sendFile(path.file + part + 'index.html');
+ found = true;
+ } else if(fs.existsSync(path.file + part + '/index.html') && fs.lstatSync(path.file + part + '/index.html').isFile()) {
+ res.send(``);
+ found = true;
+ } else {
+ res.send("Cannot find file " + path.file + part);
+ found = true;
+ }
+ } catch(e) {
+ // Nothing...
+ }
+ }
+ });
+ }
+ if(!found) {
+ res.send(`Cannot ${req.method} ${req.raw.url}`);
+ }
+
+
+ res.finish();
+ return {
+ req: req,
+ res: res
+ };
+ });
+ return route;
+}
+
+module.exports = generateRoute;
From 36da46e487e969ea8ccc032f52ee5e88e05b176a Mon Sep 17 00:00:00 2001
From: AmazingMech2418 <33498997+AmazingMech2418@users.noreply.github.com>
Date: Wed, 13 Jul 2022 16:18:53 -0400
Subject: [PATCH 29/29] Create router.js
---
src/utils/router/router.js | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
create mode 100644 src/utils/router/router.js
diff --git a/src/utils/router/router.js b/src/utils/router/router.js
new file mode 100644
index 0000000..3a1fc43
--- /dev/null
+++ b/src/utils/router/router.js
@@ -0,0 +1,38 @@
+const {normalize} = require('path');
+
+class Router {
+ constructor() {
+ this.paths = [];
+ this.statics = [];
+ }
+ _addPath(method, path, fn, asynchronous) {
+ this.paths.push({
+ method: method,
+ path: path,
+ fn: fn
+ });
+ }
+ get(path, fn) {
+ this._addPath('GET', path, fn);
+ }
+ post(path, fn) {
+ this._addPath('POST', path, fn);
+ }
+ all(path, fn) {
+ this.get(path, fn);
+ this.post(path, fn);
+ }
+ static(path, file) {
+ if(normalize(path)[0] != '/' || normalize(file)[0] != '/') {
+ console.error(new Error("Static path must be absolute"));
+ return false;
+ }
+ this.statics.push({
+ path: normalize(path),
+ file: normalize(file)
+ });
+ return true;
+ }
+}
+
+module.exports = Router;