From 102b15c3cd8a785c8514108bb64b90af79290876 Mon Sep 17 00:00:00 2001 From: Christian Georgi Date: Tue, 26 Oct 2021 18:48:54 +0200 Subject: [PATCH 1/2] NPM registry: more robust, work on Windows - Run `npm rm` in correct dir - Encode args in `npm conf rm` command for Powershell - Don't cleanup in exit handler, this might not complete. Instead, do it on startup. - Different cleanup command for Windows and Linux - Explicitly close server, instead of relying on process.exit() - Make scope configurable --- .registry/server.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/.registry/server.js b/.registry/server.js index 2696d948..2a9b8161 100644 --- a/.registry/server.js +++ b/.registry/server.js @@ -1,17 +1,22 @@ const { exec } = require ('child_process') +const isWin = process.platform === 'win32' const express = require ('express') const fs = require ('fs') const app = express() const { PORT=4444 } = process.env -const [,,port=PORT] = process.argv +const [,,port=PORT,scope='@capire'] = process.argv const cwd = __dirname +// clean up on start (exit handler might not complete on Windows) +exec(isWin ? 'del *.tgz' : 'rm *.tgz', {cwd}) + + app.use('/-/:tarball', (req,res,next) => { console.debug ('GET', req.params) try { const { tarball } = req.params - const [, pkg ] = /^capire-(\w+)/.exec(tarball) + const [, pkg ] = /^\w+-(\w+)/.exec(tarball) fs.lstat(tarball,(err => { if (err) exec(`npm pack ../${pkg}`,{cwd},next) else next() @@ -28,9 +33,9 @@ app.get('/*', (req,res)=>{ const url = decodeURIComponent(req.url) console.debug ('GET',url) try { - const [, capire, pkg ] = /^\/(@capire)\/(\w+)/.exec(url) - const package = require (`${capire}/${pkg}/package.json`) - const tarball = `capire-${pkg}-${package.version}.tgz` + const [, scpe, pkg ] = /^\/(@\w+)\/(\w+)/.exec(url) + const package = require (`${scpe}/${pkg}/package.json`) + const tarball = `${scpe.slice(1)}-${pkg}-${package.version}.tgz` // https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md res.json({ "name": package.name, @@ -53,18 +58,18 @@ app.get('/*', (req,res)=>{ } }) -app.listen(port, ()=>{ - console.log (`npm set @capire:registry=http://localhost:${port}`) - console.log (`@capire registry listening on http://localhost:${port}`) - exec(`npm set @capire:registry=http://localhost:${port}`) +const server = app.listen(port, ()=>{ + const url = `http://localhost:${port}` + console.log (`npm set ${scope}:registry=${url}`) + exec(`npm set ${scope}:registry=${url}`) + console.log (`${scope} registry listening on ${url}`) }) const _exit = ()=>{ - console.log ('\nnpm conf rm @capire:registry') - exec('npm conf rm @capire:registry') - exec('rm *.tgz') - process.exit() + server.close() + exec(`npm conf rm "${scope}:registry"`, ()=> { process.exit() }) } + process.on ('SIGTERM',_exit) process.on ('SIGHUP',_exit) process.on ('SIGINT',_exit) From 21e74bbbfb73b9e042995877efef10da03034ef7 Mon Sep 17 00:00:00 2001 From: Christian Georgi Date: Tue, 26 Oct 2021 22:10:23 +0200 Subject: [PATCH 2/2] Add test for NPM registry --- .registry/server.js | 13 +++++++---- test/registry.test.js | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 test/registry.test.js diff --git a/.registry/server.js b/.registry/server.js index 2a9b8161..90fe17d9 100644 --- a/.registry/server.js +++ b/.registry/server.js @@ -30,10 +30,12 @@ app.use('/-/:tarball', (req,res,next) => { app.use('/-', express.static(__dirname)) app.get('/*', (req,res)=>{ + const urlRegex = /^\/(@\w+)\/(\w+)/ const url = decodeURIComponent(req.url) console.debug ('GET',url) try { - const [, scpe, pkg ] = /^\/(@\w+)\/(\w+)/.exec(url) + if (!urlRegex.test(url)) return res.sendStatus(404) + const [, scpe, pkg ] = urlRegex.exec(url) const package = require (`${scpe}/${pkg}/package.json`) const tarball = `${scpe.slice(1)}-${pkg}-${package.version}.tgz` // https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md @@ -47,24 +49,25 @@ app.get('/*', (req,res)=>{ "name": package.name, "version": package.version, "dist": { - "tarball": `http://localhost:${port}/-/${tarball}` + "tarball": `/-/${tarball}` }, } }, }) } catch (e) { - console.error(e) - res.sendStatus(404) + if (e.code === 'MODULE_NOT_FOUND') return res.sendStatus(404) + console.error(e); throw e } }) const server = app.listen(port, ()=>{ - const url = `http://localhost:${port}` + const url = `http://localhost:${server.address().port}` console.log (`npm set ${scope}:registry=${url}`) exec(`npm set ${scope}:registry=${url}`) console.log (`${scope} registry listening on ${url}`) }) + const _exit = ()=>{ server.close() exec(`npm conf rm "${scope}:registry"`, ()=> { process.exit() }) diff --git a/test/registry.test.js b/test/registry.test.js new file mode 100644 index 00000000..beb98f87 --- /dev/null +++ b/test/registry.test.js @@ -0,0 +1,54 @@ + +const { fork } = require('child_process') +const { resolve } = require('path') +const Axios = require('axios') +const verbose = process.env.CDS_TEST_VERBOSE +// ||true + +describe('Local NPM registry', () => { + let registry + let axios + const cwd = resolve(__dirname, '..') + + beforeAll(async ()=> { + const env = Object.assign(process.env, {PORT:'0'}) + const res = await exec (resolve(cwd, '.registry/server.js'), {cwd, stdio: 'pipe', env}) + registry = res.cp + axios = Axios.default.create ({ baseURL: res.url, validateStatus: (status)=>status<500 }) + }) + + afterAll(() => { registry.kill() }) + + for (const mod of ['bookshop','fiori','orders','reviews']) { + it(`should serve ${mod}`, async () => { + const resp = await axios.get(`/@capire/${mod}`) + expect(resp.data).toMatchObject({name: `@capire/${mod}`, versions:{}}) + const versions = Object.values(resp.data.versions) + await axios.get(versions[0].dist.tarball) + }) + } + it(`should return 404 for unknown packages`, async () => { + let resp = await axios.get(`/@capire/foo`) + expect(resp.status).toEqual(404) + resp = await axios.get(`/foo`) + expect(resp.status).toEqual(404) + }) + +}) + +function exec (script, opts) { + return new Promise((resolve, reject)=> { + const cp = fork (script, [], opts) + .on('error', err => reject(new Error(err))) + cp.stdout.on('data', chunk => { + if (verbose) console.log(chunk.toString()) + if (chunk.toString().match(/listening.*(http:.*:\d+)/i)) { + resolve({cp, url:RegExp.$1}) + } + }) + cp.stderr.on('data', chunk => { + if (verbose) console.error(chunk.toString()) + }) + }) + +}