UNPKG

verdaccio

Version:

A lightweight private npm proxy registry

1 lines 5.23 kB
{"version":3,"file":"run-server.mjs","names":[],"sources":["../../src/lib/run-server.ts"],"sourcesContent":["import constants from 'constants';\nimport buildDebug from 'debug';\nimport fs from 'fs';\nimport http from 'http';\nimport https from 'https';\nimport _ from 'lodash';\n\nimport { getConfigParsed, getListenAddress } from '@verdaccio/config';\nimport { ConfigYaml, HttpsConfKeyCert, HttpsConfPfx } from '@verdaccio/types';\n\nimport endPointAPI from '../api/index';\nimport { displayExperimentsInfoBox } from './experiments';\nimport { logger } from './logger';\nimport { initLogger } from './utils';\n\nconst debug = buildDebug('verdaccio:run-server');\n\n/**\n * Exposes a server factory to be instantiated programmatically.\n *\n const app = await runServer(); // default configuration\n const app = await runServer('./config/config.yaml');\n const app = await runServer({ configuration });\n app.listen(4000, (event) => {\n // do something\n });\n * @param config\n */\nexport async function runServer(\n config?: string | ConfigYaml,\n options?: { listenArg?: string }\n): Promise<any> {\n const configurationParsed = getConfigParsed(config);\n\n initLogger(configurationParsed);\n // merge flags and experiments for backward compatibility\n const flags = {\n ...(configurationParsed?.flags || {}),\n ...(configurationParsed?.experiments || {}),\n };\n displayExperimentsInfoBox(flags);\n\n const listen = options?.listenArg ?? configurationParsed?.listen;\n const address = getListenAddress(listen, logger);\n\n const app = await endPointAPI(configurationParsed);\n return createServerFactory(configurationParsed, address, app);\n}\n\n/**\n * Return a native HTTP/HTTPS server instance\n * @param config\n * @param addr\n * @param app\n */\nexport function createServerFactory(config: ConfigYaml, addr, app) {\n let serverFactory;\n if (addr.proto === 'https') {\n debug('https enabled');\n try {\n let httpsOptions = {\n // disable insecure SSLv2 and SSLv3\n secureOptions: constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3,\n };\n\n const keyCertConfig = config.https as HttpsConfKeyCert;\n const pfxConfig = config.https as HttpsConfPfx;\n\n // https must either have key and cert or a pfx and (optionally) a passphrase\n if (!((keyCertConfig.key && keyCertConfig.cert) || pfxConfig.pfx)) {\n throw Error('bad format https configuration');\n }\n\n if (pfxConfig.pfx) {\n const { pfx, passphrase } = pfxConfig;\n httpsOptions = _.assign(httpsOptions, {\n pfx: fs.readFileSync(pfx),\n passphrase: passphrase || '',\n });\n } else {\n const { key, cert, ca } = keyCertConfig;\n httpsOptions = _.assign(httpsOptions, {\n key: fs.readFileSync(key),\n cert: fs.readFileSync(cert),\n ...(ca && {\n ca: fs.readFileSync(ca),\n }),\n });\n }\n // TODO: enable http2 as feature\n // if (config.server.http2) <-- check if force http2\n serverFactory = https.createServer(httpsOptions, app);\n } catch (err: any) {\n logger.fatal({ err: err }, 'cannot create server: @{err.message}');\n process.exit(2);\n }\n } else {\n // http\n debug('http enabled');\n serverFactory = http.createServer(app);\n }\n\n if (\n config.server &&\n typeof config.server.keepAliveTimeout !== 'undefined' &&\n // @ts-ignore\n config.server.keepAliveTimeout !== 'null'\n ) {\n // library definition for node is not up to date (doesn't contain recent 8.0 changes)\n serverFactory.keepAliveTimeout = config.server.keepAliveTimeout * 1000;\n }\n // FIXE: I could not find the reason of this code.\n unlinkAddressPath(addr);\n\n return serverFactory;\n}\n\nfunction unlinkAddressPath(addr) {\n if (addr.path && fs.existsSync(addr.path)) {\n fs.unlinkSync(addr.path);\n }\n}\n"],"mappings":";;;;;;;;;;;;AAeA,IAAM,QAAQ,YAAW,sBAAsB;;;;;;;;;;;;AAa/C,eAAsB,UACpB,QACA,SACc;CACd,MAAM,sBAAsB,gBAAgB,MAAM;CAElD,WAAW,mBAAmB;CAM9B,0BAA0B;EAHxB,GAAI,qBAAqB,SAAS,CAAC;EACnC,GAAI,qBAAqB,eAAe,CAAC;CAEjB,CAAK;CAM/B,OAAO,oBAAoB,qBAHX,iBADD,SAAS,aAAa,qBAAqB,QACjB,MAGO,GAAS,MADvC,YAAY,mBAAmB,CACW;AAC9D;;;;;;;AAQA,SAAgB,oBAAoB,QAAoB,MAAM,KAAK;CACjE,IAAI;CACJ,IAAI,KAAK,UAAU,SAAS;EAC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