UNPKG

abi.js

Version:

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

61 lines (58 loc) 1.77 kB
// src/defaults.ts var defaultPort = 3e3; var defaultHost = "0.0.0.0"; // src/helpers.ts function isPort(port) { return typeof port === "number"; } function isHostname(host) { return typeof host === "string"; } function isAddress(address) { return typeof address === "object" && "port" in address && "hostname" in address && isPort(address.port) && isHostname(address.hostname); } function isHandler(handler) { return typeof handler === "function"; } function hasHandler(options) { return "handler" in options && isHandler(options.handler); } 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; } // src/serve.bun.ts function serve(arg1, arg2, arg3) { const { port, hostname, handler } = toServeOptions(arg1, arg2, arg3); const server = Bun.serve({ port, hostname, fetch: handler }); return { port: server.port, hostname: server.hostname }; } export { serve };