bin/server/sans-server.js
function SansServer(configuration) {
    if (!(this instanceof SansServer)) return new SansServer(configuration);

    const config = configuration && typeof configuration === 'object' ? Object.assign(configuration) : {};
    config.logs = config.hasOwnProperty('logs') ? config.logs : true;
    config.rejectable = config.hasOwnProperty('rejectable') ? config.rejectable : false;
    config.timeout = config.hasOwnProperty('timeout') && !isNaN(config.timeout) && config.timeout >= 0 ? config.timeout : 30;
    config.useBuiltInHooks = config.hasOwnProperty('useBuiltInHooks') ? config.useBuiltInHooks : true;

    const hooks = {};
    const keys = {};
    const runners = {
        symbols: {},
        types: {}
    };
    const server = this;

    /**
     * Define a hook that is applied to all requests.
     * @param {string} type
     * @param {number} [weight=0]
     * @param {...function} hook
     * @returns {SansServer}
     */
    this.hook = addHook.bind(server, hooks);

    /**
     * Define a hook runner and get back a Symbol that is used to execute the hooks.
     * @name SansServer#hook.define
     * @function
     * @param {string} type
     * @returns {Symbol}
     */
    this.hook.define = type => defineHookRunner(runners, type);

    /**
     * Get the hook type for a specific symbol.
     * @name SansServer#hook.type
     * @function
     * @param {Symbol} key The symbol to use to get they type.
     * @returns {string}
     */
    this.hook.type = key => runners.symbols[key];

    /**
     * Have the server execute a request.
     * @param {object|string} [req={}] An object that has request details or a string that is a GET endpoint.
     * @param {function} [callback] The function to call once the request has been processed.
     * @returns {Request}
     * @listens Request#log
     */
    this.request = (req, callback) => request(server, config, hooks, keys, req, callback);

    /**
     * Specify a middleware to use.
     * @param {...Function} middleware
     * @throws {Error}
     * @returns {SansServer}
     */
    this.use = this.hook.bind(this, 'request', 0);



    // define the request and response hooks
    keys.request = this.hook.define('request');
    keys.response = this.hook.define('response');

    // set request hooks
    if (config.timeout) this.hook('request', Number.MIN_SAFE_INTEGER + 10, timeout(config.timeout));
    if (config.useBuiltInHooks) this.hook('request', -100000, validMethod);

    // set response hooks
    if (config.useBuiltInHooks) this.hook('response', -100000, transform);
}
function SansServer(configuration) {
    if (!(this instanceof SansServer)) return new SansServer(configuration);

    const config = configuration && typeof configuration === 'object' ? Object.assign(configuration) : {};
    config.logs = config.hasOwnProperty('logs') ? config.logs : true;
    config.rejectable = config.hasOwnProperty('rejectable') ? config.rejectable : false;
    config.timeout = config.hasOwnProperty('timeout') && !isNaN(config.timeout) && config.timeout >= 0 ? config.timeout : 30;
    config.useBuiltInHooks = config.hasOwnProperty('useBuiltInHooks') ? config.useBuiltInHooks : true;

    const hooks = {};
    const keys = {};
    const runners = {
        symbols: {},
        types: {}
    };
    const server = this;

    /**
     * Define a hook that is applied to all requests.
     * @param {string} type
     * @param {number} [weight=0]
     * @param {...function} hook
     * @returns {SansServer}
     */
    this.hook = addHook.bind(server, hooks);

    /**
     * Define a hook runner and get back a Symbol that is used to execute the hooks.
     * @name SansServer#hook.define
     * @function
     * @param {string} type
     * @returns {Symbol}
     */
    this.hook.define = type => defineHookRunner(runners, type);

    /**
     * Get the hook type for a specific symbol.
     * @name SansServer#hook.type
     * @function
     * @param {Symbol} key The symbol to use to get they type.
     * @returns {string}
     */
    this.hook.type = key => runners.symbols[key];

    /**
     * Have the server execute a request.
     * @param {object|string} [req={}] An object that has request details or a string that is a GET endpoint.
     * @param {function} [callback] The function to call once the request has been processed.
     * @returns {Request}
     * @listens Request#log
     */
    this.request = (req, callback) => request(server, config, hooks, keys, req, callback);

    /**
     * Specify a middleware to use.
     * @param {...Function} middleware
     * @throws {Error}
     * @returns {SansServer}
     */
    this.use = this.hook.bind(this, 'request', 0);



    // define the request and response hooks
    keys.request = this.hook.define('request');
    keys.response = this.hook.define('response');

    // set request hooks
    if (config.timeout) this.hook('request', Number.MIN_SAFE_INTEGER + 10, timeout(config.timeout));
    if (config.useBuiltInHooks) this.hook('request', -100000, validMethod);

    // set response hooks
    if (config.useBuiltInHooks) this.hook('response', -100000, transform);
}
function SansServer ( configuration ) { if ( ! ( this instanceof SansServer ) ) return new SansServer ( configuration ) ; const config = configuration && typeof configuration === 'object' ? Object . assign ( configuration ) : { } ; config . logs = config . hasOwnProperty ( 'logs' ) ? config . logs : true ; config . rejectable = config . hasOwnProperty ( 'rejectable' ) ? config . rejectable : false ; config . timeout = config . hasOwnProperty ( 'timeout' ) && ! isNaN ( config . timeout ) && config . timeout >= 0 ? config . timeout : 30 ; config . useBuiltInHooks = config . hasOwnProperty ( 'useBuiltInHooks' ) ? config . useBuiltInHooks : true ; const hooks = { } ; const keys = { } ; const runners = { symbols : { } , types : { } } ; const server = this ; /**
     * Define a hook that is applied to all requests.
     * @param {string} type
     * @param {number} [weight=0]
     * @param {...function} hook
     * @returns {SansServer}
     */ this . hook = addHook . bind ( server , hooks ) ; /**
     * Define a hook runner and get back a Symbol that is used to execute the hooks.
     * @name SansServer#hook.define
     * @function
     * @param {string} type
     * @returns {Symbol}
     */ this . hook . define = type => defineHookRunner ( runners , type ) ; /**
     * Get the hook type for a specific symbol.
     * @name SansServer#hook.type
     * @function
     * @param {Symbol} key The symbol to use to get they type.
     * @returns {string}
     */ this . hook . type = key => runners . symbols [ key ] ; /**
     * Have the server execute a request.
     * @param {object|string} [req={}] An object that has request details or a string that is a GET endpoint.
     * @param {function} [callback] The function to call once the request has been processed.
     * @returns {Request}
     * @listens Request#log
     */ this . request = ( req , callback ) => request ( server , config , hooks , keys , req , callback ) ; /**
     * Specify a middleware to use.
     * @param {...Function} middleware
     * @throws {Error}
     * @returns {SansServer}
     */ this . use = this . hook . bind ( this , 'request' , 0 ) ; // define the request and response hooks keys . request = this . hook . define ( 'request' ) ; keys . response = this . hook . define ( 'response' ) ; // set request hooks if ( config . timeout ) this . hook ( 'request' , Number . MIN_SAFE_INTEGER + 10 , timeout ( config . timeout ) ) ; if ( config . useBuiltInHooks ) this . hook ( 'request' , - 100000 , validMethod ) ; // set response hooks if ( config . useBuiltInHooks ) this . hook ( 'response' , - 100000 , transform ) ; }
Create a san-server instance.
@param {object} [configuration] Configuration options.
@param {boolean} [configuration.logs=true] Whether to output grouped logs at the end of a request.
@param {boolean} [configuration.rejectable=false] Whether an error while processing the request should cause a failure or return a 500 response.
@param {number} [configuration.timeout=30] The number of seconds to wait before timeout for a request.
@param {boolean} [configuration.useBuiltInHooks=true] Whether to use built in middleware.
@returns {SansServer}
@constructor
Create a san - server instance .