Skip to content

Commit

Permalink
Start request life cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Assaf Sapir committed Mar 30, 2021
1 parent 2101986 commit 3aa0d72
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 40 deletions.
40 changes: 0 additions & 40 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ build/Release
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

Expand All @@ -65,40 +59,6 @@ typings/
# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"standard.enable": false
}
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions package.json
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"
}
83 changes: 83 additions & 0 deletions requestLifeCycleHanlder.js
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;

0 comments on commit 3aa0d72

Please sign in to comment.