@whook/whook
Version:
Build strong and efficient REST web services.
33 lines • 1.26 kB
JavaScript
import { autoService, name, location } from 'knifecycle';
import { noop } from '../libs/utils.js';
const DEFAULT_ENV = {};
export default location(name('PORT', autoService(initPort)), import.meta.url);
/**
* Initialize the PORT service from ENV or auto-detection if
* none specified in ENV
* @param {Object} services
* The service dependencies
* @param {Object} [services.ENV={}]
* An optional environment object
* @param {Object} [services.log=noop]
* An optional logging service
* @param {Object} services.importer
* A service allowing to dynamically import ES modules
* @return {Promise<Number>}
* A promise of a number representing the actual port.
*/
async function initPort({ ENV = DEFAULT_ENV, log = noop, importer, }) {
log('debug', `🏭 - Initializing the PORT service.`);
if ('undefined' !== typeof ENV.PORT) {
log('warning', `♻️ - Using ENV port "${ENV.PORT}"`);
return parseInt(ENV.PORT, 10);
}
const port = await (await importer('portfinder')).getPortPromise();
if (!port) {
log('warning', `🚫 - Could not detect any free port.`);
return 8080;
}
log('warning', `✔ - Found a free port "${port}"`);
return port;
}
//# sourceMappingURL=PORT.js.map