mirror of
https://github.com/Wessel/kirbe.git
synced 2026-07-18 06:13:59 +02:00
Upload main code
This commit is contained in:
16
model/KirbeRequest.js
Normal file
16
model/KirbeRequest.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const { parse } = require( 'url' );
|
||||
|
||||
module.exports = class KirbeRequest {
|
||||
constructor( req, body ) {
|
||||
this.url = req.url;
|
||||
this.req = req;
|
||||
this.body = body;
|
||||
this.from = req.connection.remoteAddress;
|
||||
this.method = req.method;
|
||||
this.headers = req.headers;
|
||||
this.parsedUrl = parse( this.url, true );
|
||||
}
|
||||
|
||||
json() { return JSON.parse( this.body ); }
|
||||
query ( name ) { return this.parsedUrl.query[ name ]; }
|
||||
};
|
||||
50
model/KirbeResponse.js
Normal file
50
model/KirbeResponse.js
Normal file
@@ -0,0 +1,50 @@
|
||||
module.exports = class KirbeResponse {
|
||||
constructor( res ) {
|
||||
this.coreRes = res;
|
||||
this.headers = {};
|
||||
|
||||
this.statusCode = 200;
|
||||
this.statusMessage = null;
|
||||
|
||||
this.data = Buffer.alloc( 0 );
|
||||
}
|
||||
|
||||
body( body ) {
|
||||
if ( typeof body === 'object' && !Buffer.isBuffer( body ) ) {
|
||||
if ( !this.headers[ 'content-type' ] ) this.headers[ 'content-type' ] = 'application/json';
|
||||
|
||||
this.data = JSON.stringify( body );
|
||||
} else this.data = body;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
header( a, b ) {
|
||||
if ( typeof a === 'object' ) Object.keys( a ).forEach( ( v ) => this.headers[ v.toLowerCase() ] = a[ v ] );
|
||||
else this.headers[ a.toLowerCase() ] = b;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
status( code, message ) {
|
||||
this.statusCode = code;
|
||||
this.statusMessage = message;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
writeHead( status, headers ) {
|
||||
if ( status ) this.status( status );
|
||||
if ( headers ) this.header( headers );
|
||||
}
|
||||
|
||||
end( data ) {
|
||||
if ( data ) this.body( data );
|
||||
|
||||
if (this.statusMessage) this.coreRes.writeHead( this.statusCode, this.statusMessage, this.headers );
|
||||
else this.coreRes.writeHead( this.statusCode, this.headers );
|
||||
this.coreRes.end( this.data );
|
||||
|
||||
return this;
|
||||
}
|
||||
};
|
||||
71
model/KirbeServer.js
Normal file
71
model/KirbeServer.js
Normal file
@@ -0,0 +1,71 @@
|
||||
const { join } = require( 'path' );
|
||||
const { createServer } = require( 'http' );
|
||||
|
||||
const KirbeRequest = require( join( __dirname, 'KirbeRequest.js' ) );
|
||||
const KirbeResponse = require( join( __dirname, 'KirbeResponse.js' ) );
|
||||
|
||||
const methods = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
|
||||
const isUrl = ( c ) => (typeof c === 'string' && !methods.includes( c ) ) || c instanceof RegExp;
|
||||
|
||||
module.exports = class KirbeServer {
|
||||
constructor() {
|
||||
this.internalServer = createServer( ( req, res ) => this.handler.apply( this, [ req, res ] ) );
|
||||
this.externalHandler = ( req, res ) => this.handler( req, res );
|
||||
|
||||
this.routes = [];
|
||||
this.extensions = [];
|
||||
|
||||
methods.forEach( ( v ) => this[ v.toLowerCase() ] = ( a, b ) => this.route( v, a, b ) );
|
||||
}
|
||||
|
||||
use( extension ) { this.extensions.push( extension ); }
|
||||
|
||||
route( a, b, c ) {
|
||||
this.routes.push({
|
||||
'target': {
|
||||
'path' : isUrl( a ) ? a : ( isUrl( b ) ? b : null ),
|
||||
'method': methods.includes( a ) ? a : null
|
||||
},
|
||||
'handler': c || b || a
|
||||
});
|
||||
}
|
||||
|
||||
handler( req, res ) {
|
||||
let body = Buffer.alloc( 0 );
|
||||
|
||||
req.on( 'data', ( c ) => body = Buffer.concat([ body, c ]) );
|
||||
req.on( 'end', () => {
|
||||
const request = new KirbeRequest( req, body );
|
||||
const response = new KirbeResponse( res );
|
||||
|
||||
const start = () => {
|
||||
for( let i = 0; i < this.routes.length; i++ ) {
|
||||
const current = this.routes[ i ];
|
||||
|
||||
if ( current.target.method && request.method !== current.target.method ) continue;
|
||||
if ( current.target.path ) {
|
||||
if ( current.target.path instanceof RegExp && !request.parsedUrl.pathname.match( current.target.path ) ) continue;
|
||||
else if ( current.target.path !== request.parsedUrl.pathname ) continue;
|
||||
}
|
||||
|
||||
current.handler( request, response );
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
let currentExt = 0;
|
||||
const nextExt = () => {
|
||||
if ( this.extensions.length >= currentExt + 1 ) {
|
||||
currentExt++;
|
||||
this.extensions[ currentExt -1 ]( request, response, nextExt );
|
||||
} else start();
|
||||
};
|
||||
|
||||
nextExt();
|
||||
});
|
||||
}
|
||||
|
||||
listen( a, b, c ) {
|
||||
this.internalServer.listen( typeof a === 'number' ? a : 80, typeof b === 'string' ? b : null, typeof b === 'function' ? b : null );
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user