UNPKG

abi.js

Version:

[![typescript-icon]][typescript-link] [![license-icon]][license-link] [![status-icon]][status-link] [![ci-icon]][ci-link] [![twitter-icon]][twitter-link]

54 lines (53 loc) 1.66 kB
import { defaultHost, defaultPort } from './defaults.js'; export function isPort(port) { return typeof port === 'number'; } export function isHostname(host) { return typeof host === 'string'; } export function isAddress(address) { return (typeof address === 'object' && 'port' in address && 'hostname' in address && isPort(address.port) && isHostname(address.hostname)); } export function isHandler(handler) { return typeof handler === 'function'; } export function hasHandler(options) { return 'handler' in options && isHandler(options.handler); } export function isServeOptions(options) { return isAddress(options) && hasHandler(options); } function toServeOptions(arg1, arg2, arg3) { let options; if (typeof arg1 === 'function') { options = { port: defaultPort, hostname: defaultHost, handler: arg1 }; } else if (isPort(arg1) && isHandler(arg2)) { options = { port: arg1, hostname: defaultHost, handler: arg2 }; } else if (isHostname(arg1) && isHandler(arg2)) { options = { port: defaultPort, hostname: arg1, handler: arg2 }; } else if (isPort(arg1) && isHostname(arg2) && isHandler(arg3)) { options = { port: arg1, hostname: arg2, handler: arg3 }; } else if (isAddress(arg1) && isHandler(arg2)) { options = { port: arg1.port, hostname: arg1.hostname, handler: arg2, }; } else if (isServeOptions(arg1)) { options = arg1; } else { throw new Error('Invalid arguments for serve function'); } return options; } export { toServeOptions };