mirror of
https://github.com/Wessel/kirbe.git
synced 2026-07-26 10:00:59 +02:00
general code improvements part 3
This commit is contained in:
@@ -1,85 +1,85 @@
|
|||||||
const { join } = require( 'path' );
|
const { join } = require('path');
|
||||||
const { createServer } = require( 'http' );
|
const { createServer } = require('http');
|
||||||
|
|
||||||
const KirbeRequest = require( join( __dirname, 'KirbeRequest.js' ) );
|
const KirbeRequest = require(join( __dirname, 'KirbeRequest.js'));
|
||||||
const KirbeResponse = require( join( __dirname, 'KirbeResponse.js' ) );
|
const KirbeResponse = require(join( __dirname, 'KirbeResponse.js'));
|
||||||
|
|
||||||
const Collection = require( '../fake_node_modules/Collection' );
|
const Collection = require( '../fake_node_modules/Collection' );
|
||||||
|
|
||||||
const methods = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
|
const methods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'];
|
||||||
const isUrl = ( c ) => (typeof c === 'string' && !methods.includes( c ) ) || c instanceof RegExp;
|
const isUrl = (c) => (typeof c === 'string' && !methods.includes(c)) || c instanceof RegExp;
|
||||||
|
|
||||||
module.exports = class KirbeServer {
|
module.exports = class KirbeServer {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.internalServer = createServer( ( req, res ) => this.handler.apply( this, [ req, res ] ) );
|
this.internalServer = createServer(( req, res) => this.handler.apply(this, [req, res]));
|
||||||
this.externalHandler = ( req, res ) => this.handler( req, res );
|
this.externalHandler = (req, res) => this.handler(req, res);
|
||||||
|
|
||||||
this.stack = new Collection();
|
this.stack = new Collection();
|
||||||
this.routes = [];
|
this.routes = [];
|
||||||
this.extensions = [];
|
this.extensions = [];
|
||||||
|
|
||||||
methods.forEach( ( v ) => this[ v.toLowerCase() ] = ( a, b ) => this.route( v, a, b ) );
|
methods.forEach((v) => this[v.toLowerCase()] = (a, b) => this.route(v, a, b ));
|
||||||
}
|
}
|
||||||
|
|
||||||
use( middleware ) {
|
use(middleware) {
|
||||||
const o = typeof middleware === 'object';
|
const o = typeof middleware === 'object';
|
||||||
const m = {
|
const m = {
|
||||||
name : this.stack.size,
|
name : this.stack.size,
|
||||||
args : o && typeof Array.isArray( o.args ) ? o.args : [],
|
args : o && typeof Array.isArray(o.args) ? o.args : [],
|
||||||
function : o && typeof o.function === 'function' ? o.function : middleware,
|
function : o && typeof o.function === 'function' ? o.function : middleware,
|
||||||
constructor: !!( o && o.constructor )
|
constructor: !!(o && o.constructor)
|
||||||
};
|
};
|
||||||
|
|
||||||
this.stack.set( m.name, m );
|
this.stack.set(m.name, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
route( a, b, c ) {
|
route(a, b, c) {
|
||||||
this.routes.push({
|
this.routes.push({
|
||||||
'target': {
|
'target': {
|
||||||
'path' : isUrl( a ) ? a : ( isUrl( b ) ? b : null ),
|
'path' : isUrl(a) ? a : (isUrl(b) ? b : null),
|
||||||
'method': methods.includes( a ) ? a : null
|
'method': methods.includes(a) ? a : null
|
||||||
},
|
},
|
||||||
'handler': c || b || a
|
'handler': c || b || a
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handler( req, res ) {
|
handler(req, res) {
|
||||||
let body = Buffer.alloc( 0 );
|
let body = Buffer.alloc(0);
|
||||||
|
|
||||||
req.on( 'data', ( c ) => body = Buffer.concat([ body, c ]) );
|
req.on('data', (c) => body = Buffer.concat([body, c]));
|
||||||
req.on( 'end', () => {
|
req.on('end', () => {
|
||||||
const request = new KirbeRequest( req, body );
|
const request = new KirbeRequest(req, body);
|
||||||
const response = new KirbeResponse( res );
|
const response = new KirbeResponse(res);
|
||||||
|
|
||||||
const start = () => {
|
const start = () => {
|
||||||
for( let i = 0; i < this.routes.length; i++ ) {
|
for(let i = 0; i < this.routes.length; i++) {
|
||||||
const current = this.routes[ i ];
|
const current = this.routes[i];
|
||||||
|
|
||||||
if ( current.target.method && request.method !== current.target.method ) continue;
|
if (current.target.method && request.method !== current.target.method) continue;
|
||||||
if ( current.target.path ) {
|
if (current.target.path) {
|
||||||
if ( current.target.path instanceof RegExp && !request.parsedUrl.pathname.match( current.target.path ) ) continue;
|
if (current.target.path instanceof RegExp && !request.parsedUrl.pathname.match(current.target.path)) continue;
|
||||||
else if ( current.target.path !== request.parsedUrl.pathname ) continue;
|
else if (current.target.path !== request.parsedUrl.pathname) continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
current.handler( request, response );
|
current.handler( request, response );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let currentMiddleware = 0;
|
let currentMiddleware = 0;
|
||||||
const renderMiddleware = () => {
|
const renderMiddleware = () => {
|
||||||
if( this.stack.size >= currentMiddleware +1 ) {
|
if (this.stack.size >= currentMiddleware +1) {
|
||||||
currentMiddleware++;
|
currentMiddleware++;
|
||||||
const middleware = this.stack.get( currentMiddleware - 1 );
|
const middleware = this.stack.get(currentMiddleware - 1);
|
||||||
if ( middleware.constructor ) new middleware.function( request, response, renderMiddleware );
|
if (middleware.constructor) new middleware.function(request, response, renderMiddleware);
|
||||||
else middleware.function( request, response, renderMiddleware );
|
else middleware.function(request, response, renderMiddleware);
|
||||||
}else start();
|
} else start();
|
||||||
};
|
};
|
||||||
renderMiddleware();
|
renderMiddleware();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
listen( a, b, c ) {
|
listen(a, b, c) {
|
||||||
this.internalServer.listen( typeof a === 'number' ? a : 80, typeof b === 'string' ? b : null, typeof b === 'function' ? b : null );
|
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