-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Assaf Sapir
committed
Mar 30, 2021
1 parent
2101986
commit 3aa0d72
Showing
5 changed files
with
110 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"standard.enable": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "yahf", | ||
"version": "1.0.0", | ||
"description": "Yet Another HTTP Framework", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Assaf Sapir <me@ass.af>", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// import string decoder module | ||
import { StringDecoder } from 'string_decoder'; | ||
|
||
const YAHF = {} | ||
|
||
// create empty middlewares array | ||
const middlewares = []; | ||
|
||
// create empty routes object | ||
const routes = {}; | ||
|
||
YAHF.useMiddleware = middleware => { | ||
middlewares.push(middleware); | ||
} | ||
|
||
YAHF.useRoute = (path, handler) => { | ||
routes[path] = handler; | ||
} | ||
|
||
YAHF.requestInit = (req, res) => { | ||
return new Promise((resolve, reject) => { | ||
// get the url and parse it | ||
const parsedUrl = new URL(req.url, 'http://localhost:3000'); | ||
// get the path so we can get the route, remove the leading '/' and empty string | ||
const path = parsedUrl.pathname.split('/').filter(Boolean); | ||
// get the query string as an object | ||
const query = parsedUrl.query; | ||
// get the method | ||
const method = req.method; | ||
// get the headers as an object | ||
const headers = req.headers; | ||
// get the payload if any | ||
const decoder = new StringDecoder('utf-8'); | ||
let buffer = ''; | ||
req.on('data', (data) => { | ||
buffer += decoder.write(data); | ||
}); | ||
|
||
req.on('end', () => { | ||
buffer += decoder.end(); | ||
const data = { | ||
path, | ||
query, | ||
method, | ||
headers, | ||
payload: buffer | ||
}; | ||
|
||
resolve(data); | ||
}); | ||
|
||
req.on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
YAHF.serverHandler = async (req, res) => { | ||
try { | ||
const data = await requestInit(req, res); | ||
for (const middleware of middlewares) { | ||
await middleware(data); | ||
} | ||
|
||
const handler = routes[data.path]; | ||
if (!handler) { | ||
res.statusCode = 404; | ||
res.end(); | ||
return; | ||
} | ||
// handle the request | ||
const handlerResult = await handler(data); | ||
// send the response | ||
res.statusCode = handlerResult.statusCode || 200; | ||
res.setHeader('Content-Type', handlerResult.contentType || 'text/html'); | ||
res.end(handlerResult.payload); | ||
} catch (err) { | ||
res.statusCode = 500; | ||
res.end(err.message); | ||
} | ||
} | ||
|
||
export default YAHF; |