diff options
author | Nathan Fritz <fritzy@github.com> | 2021-12-16 21:01:56 +0300 |
---|---|---|
committer | Nathan Fritz <fritzy@github.com> | 2021-12-16 21:05:19 +0300 |
commit | d7265045730555c03b3142c004c7438e9577028c (patch) | |
tree | 035d81b3124bdaa09c21854934bf2b2b50e52e44 /workspaces/libnpmexec/test/registry | |
parent | d8aac8448e983692cacb427e03f4688cd1b62e30 (diff) |
Bring in all libnpm modules + arborist as workspaces (#4166)
Added libnpm workspaces and arborist
Diffstat (limited to 'workspaces/libnpmexec/test/registry')
7 files changed, 494 insertions, 0 deletions
diff --git a/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index.json b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index.json new file mode 100644 index 000000000..1e85cbbab --- /dev/null +++ b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index.json @@ -0,0 +1,81 @@ +{ + "_id": "@ruyadorno/create-index", + "name": "@ruyadorno/create-index", + "dist-tags": { + "latest": "1.0.0" + }, + "versions": { + "1.0.0": { + "name": "@ruyadorno/create-index", + "version": "1.0.0", + "description": "Create an empty index.js file", + "bin": { + "create-index": "create-index.js" + }, + "keywords": [ + "init", + "create", + "index" + ], + "author": { + "name": "Ruy Adorno", + "url": "https://ruyadorno.com" + }, + "license": "MIT", + "gitHead": "0c1b6a4c503d8565439b2b194b4691824a1bc902", + "_id": "@ruyadorno/create-index@1.0.0", + "_nodeVersion": "15.13.0", + "_npmVersion": "7.9.0", + "dist": { + "integrity": "sha512-2T2JRYWtB9/wN8Vr/SRDcjIbKD5IjR5joO8iCCCYjXfDRZ2lYBSnZQ2kGp34F+T8OEavzJfj9sxNt9Y7QT7Oaw==", + "shasum": "a7d15d2ca78c496685b7b2bc24599d4e0983783c", + "tarball": "https://registry.npmjs.org/@ruyadorno/create-index/-/create-index-1.0.0.tgz", + "fileCount": 3, + "unpackedSize": 565, + "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgd0fRCRA9TVsSAnZWagAA9ysP/i22HySX0+RYcHUldWcv\neDgd24/wQqNEsQiTrGpIRSSorqbrC5+xoZfFzbvbUA24JaFChgQE1rRtYDab\ntjo5asfUqCspru1X05D3T3lmy3NyBCShqzwsZo88stj8L1w8DcnmU83als4h\n6DqxmwQbPMn+hd5gKtr6ZsUwHZRc/9dXWjn6GI3ztAla73RKXQ4D9Gs/ULyo\nNwS6a/CqThqu4atlA6ZGXum72XsFYSRB712N3Q1l0+8T9L3lAWuitGx/K8L/\n95gxU0e6ME+Wiin62SxH6QYWuVIKD04UNkz14dzfI2RIjT2NDbX6l308uSza\nbWz6aro4w9kUJviDX/hk/o469d+EQ87L+vpFrLDbSfZg8RtvSptHCDdM6mNw\n05xNFji33ujMX54HyGxplioAgnE5X2ZTQuBymsiINHq5gxCn8MSaUxiX45yB\n7Bhf1rWbp5KgiUa0kGXV4eoAutP6HWs1avzkHi9q2xS61wMBdPPHX5GsTTqe\nI+4mdgpNOdQLQjLyCp+ydvSqTHtVHkHDrBJzgkOjDWC7YzDcbzFQt6Fn6uc/\nA4kTlU1yTD2lPz9ICNI6BwqM7aOa9qCVkBL7vWaUUpxblRpzfbKmCtEi704h\nIJ6YZ3z6xwTl59aMXiInOLFsb7upEwtTXTAWqDlsJmTYS7hsVi3gY7wqYp1p\nMwwm\r\n=rrJ8\r\n-----END PGP SIGNATURE-----\r\n" + }, + "_npmUser": { + "name": "ruyadorno", + "email": "ruyadorno@hotmail.com" + }, + "directories": {}, + "maintainers": [ + { + "name": "ruyadorno", + "email": "ruyadorno@hotmail.com" + } + ], + "_npmOperationalInternal": { + "host": "s3://npm-registry-packages", + "tmp": "tmp/create-index_1.0.0_1618429905498_0.11104270815832784" + }, + "_hasShrinkwrap": false + } + }, + "time": { + "created": "2021-04-14T19:51:45.442Z", + "1.0.0": "2021-04-14T19:51:45.650Z", + "modified": "2021-04-14T19:51:47.833Z" + }, + "maintainers": [ + { + "name": "ruyadorno", + "email": "ruyadorno@hotmail.com" + } + ], + "description": "Create an empty index.js file", + "keywords": [ + "init", + "create", + "index" + ], + "author": { + "name": "Ruy Adorno", + "url": "https://ruyadorno.com" + }, + "license": "MIT", + "readme": "# create-index\n\nPuts an empty `index.js` into current working dir. Meant for testing only.\n\n## Usage\n\n`npm exec @ruyadorno/create-index`\n\n", + "readmeFilename": "README.md", + "_cached": false, + "_contentLength": 0 +}
\ No newline at end of file diff --git a/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index.min.json b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index.min.json new file mode 100644 index 000000000..e4e998317 --- /dev/null +++ b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index.min.json @@ -0,0 +1,26 @@ +{ + "name": "@ruyadorno/create-index", + "dist-tags": { + "latest": "1.0.0" + }, + "versions": { + "1.0.0": { + "name": "@ruyadorno/create-index", + "version": "1.0.0", + "bin": { + "create-index": "create-index.js" + }, + "dist": { + "integrity": "sha512-2T2JRYWtB9/wN8Vr/SRDcjIbKD5IjR5joO8iCCCYjXfDRZ2lYBSnZQ2kGp34F+T8OEavzJfj9sxNt9Y7QT7Oaw==", + "shasum": "a7d15d2ca78c496685b7b2bc24599d4e0983783c", + "tarball": "https://registry.npmjs.org/@ruyadorno/create-index/-/create-index-1.0.0.tgz", + "fileCount": 3, + "unpackedSize": 565, + "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgd0fRCRA9TVsSAnZWagAA9ysP/i22HySX0+RYcHUldWcv\neDgd24/wQqNEsQiTrGpIRSSorqbrC5+xoZfFzbvbUA24JaFChgQE1rRtYDab\ntjo5asfUqCspru1X05D3T3lmy3NyBCShqzwsZo88stj8L1w8DcnmU83als4h\n6DqxmwQbPMn+hd5gKtr6ZsUwHZRc/9dXWjn6GI3ztAla73RKXQ4D9Gs/ULyo\nNwS6a/CqThqu4atlA6ZGXum72XsFYSRB712N3Q1l0+8T9L3lAWuitGx/K8L/\n95gxU0e6ME+Wiin62SxH6QYWuVIKD04UNkz14dzfI2RIjT2NDbX6l308uSza\nbWz6aro4w9kUJviDX/hk/o469d+EQ87L+vpFrLDbSfZg8RtvSptHCDdM6mNw\n05xNFji33ujMX54HyGxplioAgnE5X2ZTQuBymsiINHq5gxCn8MSaUxiX45yB\n7Bhf1rWbp5KgiUa0kGXV4eoAutP6HWs1avzkHi9q2xS61wMBdPPHX5GsTTqe\nI+4mdgpNOdQLQjLyCp+ydvSqTHtVHkHDrBJzgkOjDWC7YzDcbzFQt6Fn6uc/\nA4kTlU1yTD2lPz9ICNI6BwqM7aOa9qCVkBL7vWaUUpxblRpzfbKmCtEi704h\nIJ6YZ3z6xwTl59aMXiInOLFsb7upEwtTXTAWqDlsJmTYS7hsVi3gY7wqYp1p\nMwwm\r\n=rrJ8\r\n-----END PGP SIGNATURE-----\r\n" + } + } + }, + "modified": "2021-04-14T19:51:47.833Z", + "_cached": false, + "_contentLength": 1423 +}
\ No newline at end of file diff --git a/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index/-/create-index-1.0.0.tgz b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index/-/create-index-1.0.0.tgz Binary files differnew file mode 100644 index 000000000..d6ddf7570 --- /dev/null +++ b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-index/-/create-index-1.0.0.tgz diff --git a/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test.json b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test.json new file mode 100644 index 000000000..d193b83cf --- /dev/null +++ b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test.json @@ -0,0 +1,81 @@ +{ + "_id": "@ruyadorno/create-test", + "name": "@ruyadorno/create-test", + "dist-tags": { + "latest": "1.0.0" + }, + "versions": { + "1.0.0": { + "name": "@ruyadorno/create-test", + "version": "1.0.0", + "description": "Creates an empty test.js file", + "bin": { + "create-test": "create-test.js" + }, + "keywords": [ + "init", + "create", + "test" + ], + "author": { + "name": "Ruy Adorno", + "url": "https://ruyadorno.com" + }, + "license": "MIT", + "gitHead": "707aa293e34f48dcf9cb6b4b452cb1fc8e484c8b", + "_id": "@ruyadorno/create-test@1.0.0", + "_nodeVersion": "15.13.0", + "_npmVersion": "7.9.0", + "dist": { + "integrity": "sha512-WOifELHCU8nmg0yHsPbSETPaNO1orDPhTSflJsomqGFNwVS44qvkWwMPbDE3L2aAglXLf5AxUznyFkxsXgDF2w==", + "shasum": "f0f393449fe5205c54a4ca2181d8355d2372da93", + "tarball": "https://registry.npmjs.org/@ruyadorno/create-test/-/create-test-1.0.0.tgz", + "fileCount": 3, + "unpackedSize": 557, + "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgd1SICRA9TVsSAnZWagAAOa4P/jknjgmFaBWz6PCng8qV\nsdfa23GbE4MdmvpB72v6DvNjGQ+51Vgd7PBAJRo+d3LmQ0c2DE/e6PZEgam/\nOtuVbgimxPy85V1MTA66bgML4nFtEOKS/R/Z5s7wMMCrhYqKdMp6ELMUEO07\n7cDIzAmc7WeSLyzhTBC661T0nKPPAf2nKKYXLI+6RisQoXnEgZmgNyNlIt6D\nNDNTRZjaR6s1QvHgWN9h/hLAgKvgaAnSy+JOzcB+SGaClLow3svbvW+klQpA\n8afOTLV4D/pgPDGXvvwDDInH6yccYSOSNiAZgd45hsmo82xIR3n+Cod2qk9Y\njCye36nXzdQTz9A7a3SgH++DV7fA5n87GoahkpGEnKu8gjgMuE6ncDEypbTi\nM4R8JikZrScR2wWXtO+jK4f/5XHVh19ZpqdOrlxzXutkUy0/bMoHFNxcGrXB\n5D1Qk/lOpNO4rd0NoURk6OkpueHOlBHlBNxqrEsltzY2IWs+JICcFaz385H0\nKYyNQrmltEqWVgW+LeFvm3B1sLL5wySqplX/396lC6kCHZyofeeqZFcC1G+m\ntkp0iho63tlm6WjIzw6ddHWu8olNohCk4xFpvNkkZ0u9GR4BaDBRXS60AcoD\nNIYwMuUlqmXAc7ey+xNZCqXokgbtjD7aI2uIDLNUMHELxrRzBccHe76sIQit\nBeBy\r\n=u7P4\r\n-----END PGP SIGNATURE-----\r\n" + }, + "_npmUser": { + "name": "ruyadorno", + "email": "ruyadorno@hotmail.com" + }, + "directories": {}, + "maintainers": [ + { + "name": "ruyadorno", + "email": "ruyadorno@hotmail.com" + } + ], + "_npmOperationalInternal": { + "host": "s3://npm-registry-packages", + "tmp": "tmp/create-test_1.0.0_1618433159830_0.5969279363908722" + }, + "_hasShrinkwrap": false + } + }, + "time": { + "created": "2021-04-14T20:45:59.789Z", + "1.0.0": "2021-04-14T20:45:59.974Z", + "modified": "2021-04-14T20:46:02.139Z" + }, + "maintainers": [ + { + "name": "ruyadorno", + "email": "ruyadorno@hotmail.com" + } + ], + "description": "Creates an empty test.js file", + "keywords": [ + "init", + "create", + "test" + ], + "author": { + "name": "Ruy Adorno", + "url": "https://ruyadorno.com" + }, + "license": "MIT", + "readme": "# create-test\n\nPuts an empty `test.js` into current working dir. Meant for testing only.\n\n## Usage\n\n`npm exec @ruyadorno/create-test`\n\n", + "readmeFilename": "README.md", + "_cached": false, + "_contentLength": 0 +}
\ No newline at end of file diff --git a/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test.min.json b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test.min.json new file mode 100644 index 000000000..cc1508415 --- /dev/null +++ b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test.min.json @@ -0,0 +1,26 @@ +{ + "name": "@ruyadorno/create-test", + "dist-tags": { + "latest": "1.0.0" + }, + "versions": { + "1.0.0": { + "name": "@ruyadorno/create-test", + "version": "1.0.0", + "bin": { + "create-test": "create-test.js" + }, + "dist": { + "integrity": "sha512-WOifELHCU8nmg0yHsPbSETPaNO1orDPhTSflJsomqGFNwVS44qvkWwMPbDE3L2aAglXLf5AxUznyFkxsXgDF2w==", + "shasum": "f0f393449fe5205c54a4ca2181d8355d2372da93", + "tarball": "https://registry.npmjs.org/@ruyadorno/create-test/-/create-test-1.0.0.tgz", + "fileCount": 3, + "unpackedSize": 557, + "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgd1SICRA9TVsSAnZWagAAOa4P/jknjgmFaBWz6PCng8qV\nsdfa23GbE4MdmvpB72v6DvNjGQ+51Vgd7PBAJRo+d3LmQ0c2DE/e6PZEgam/\nOtuVbgimxPy85V1MTA66bgML4nFtEOKS/R/Z5s7wMMCrhYqKdMp6ELMUEO07\n7cDIzAmc7WeSLyzhTBC661T0nKPPAf2nKKYXLI+6RisQoXnEgZmgNyNlIt6D\nNDNTRZjaR6s1QvHgWN9h/hLAgKvgaAnSy+JOzcB+SGaClLow3svbvW+klQpA\n8afOTLV4D/pgPDGXvvwDDInH6yccYSOSNiAZgd45hsmo82xIR3n+Cod2qk9Y\njCye36nXzdQTz9A7a3SgH++DV7fA5n87GoahkpGEnKu8gjgMuE6ncDEypbTi\nM4R8JikZrScR2wWXtO+jK4f/5XHVh19ZpqdOrlxzXutkUy0/bMoHFNxcGrXB\n5D1Qk/lOpNO4rd0NoURk6OkpueHOlBHlBNxqrEsltzY2IWs+JICcFaz385H0\nKYyNQrmltEqWVgW+LeFvm3B1sLL5wySqplX/396lC6kCHZyofeeqZFcC1G+m\ntkp0iho63tlm6WjIzw6ddHWu8olNohCk4xFpvNkkZ0u9GR4BaDBRXS60AcoD\nNIYwMuUlqmXAc7ey+xNZCqXokgbtjD7aI2uIDLNUMHELxrRzBccHe76sIQit\nBeBy\r\n=u7P4\r\n-----END PGP SIGNATURE-----\r\n" + } + } + }, + "modified": "2021-04-14T20:46:02.139Z", + "_cached": false, + "_contentLength": 1417 +}
\ No newline at end of file diff --git a/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test/-/create-test-1.0.0.tgz b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test/-/create-test-1.0.0.tgz Binary files differnew file mode 100644 index 000000000..34a857020 --- /dev/null +++ b/workspaces/libnpmexec/test/registry/content/ruyadorno/create-test/-/create-test-1.0.0.tgz diff --git a/workspaces/libnpmexec/test/registry/server.js b/workspaces/libnpmexec/test/registry/server.js new file mode 100644 index 000000000..1db583a93 --- /dev/null +++ b/workspaces/libnpmexec/test/registry/server.js @@ -0,0 +1,280 @@ +const { join, dirname } = require('path') +const { existsSync, readFileSync, writeFileSync } = require('fs') +const PORT = 12345 + (+process.env.TAP_CHILD_ID || 0) +const http = require('http') +const https = require('https') + +const mkdirp = require('mkdirp') +const doProxy = process.env.ARBORIST_TEST_PROXY +const missing = /\/@isaacs(\/|%2[fF])(this-does-not-exist-at-all|testing-missing-tgz\/-\/)/ +const corgiDoc = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*' +const { gzipSync, unzipSync } = require('zlib') + +let advisoryBulkResponse = null +let failAdvisoryBulk = false +let auditResponse = null +let failAudit = false +const startServer = cb => { + const server = module.exports.server = http.createServer((req, res) => { + res.setHeader('connection', 'close') + + if (req.url === '/-/npm/v1/security/advisories/bulk') { + const body = [] + req.on('data', c => body.push(c)) + req.on('end', () => { + res.setHeader('connection', 'close') + if (failAdvisoryBulk) { + res.statusCode = 503 + return res.end('no advisory bulk for you') + } + if (!advisoryBulkResponse) { + if (auditResponse && !failAudit) { + // simulate what the registry does when quick audits are allowed, + // but advisory bulk requests are not + res.statusCode = 405 + return res.end(JSON.stringify({ + code: 'MethodNotAllowedError', + message: 'POST is not allowed', + })) + } else { + res.statusCode = 404 + return res.end('not found') + } + } + if (doProxy && !existsSync(advisoryBulkResponse)) { + // hit the main registry, then fall back to staging for now + // XXX: remove this when bulk advisory endpoint pushed to production! + const opts = { + host: 'registry.npmjs.org', + method: req.method, + path: req.url, + headers: { + ...req.headers, + accept: '*', + host: 'registry.npmjs.org', + connection: 'close', + 'if-none-match': '', + }, + } + const handleUpstream = upstream => { + res.statusCode = upstream.statusCode + if (upstream.statusCode >= 300 || upstream.statusCode < 200) { + console.error('UPSTREAM ERROR', upstream.statusCode) + return upstream.pipe(res) + } + res.setHeader('content-encoding', upstream.headers['content-encoding']) + const file = advisoryBulkResponse + console.error('PROXY', `${req.url} -> ${file} ${upstream.statusCode}`) + mkdirp.sync(dirname(file)) + const data = [] + upstream.on('end', () => { + const out = Buffer.concat(data) + const obj = JSON.parse(unzipSync(out).toString()) + writeFileSync(file, JSON.stringify(obj, 0, 2) + '\n') + res.end(out) + }) + upstream.on('data', c => data.push(c)) + } + return https.request(opts).on('response', upstream => { + if (upstream.statusCode !== 200) { + console.error('ATTEMPTING TO PROXY FROM STAGING') + console.error('NOTE: THIS WILL FAIL WHEN NOT ON VPN!') + opts.host = 'security-microservice-3-west.npm.red' + opts.headers.host = opts.host + opts.path = '/v1/advisories/bulk' + https.request(opts) + .on('response', upstream => handleUpstream(upstream)) + .end(Buffer.concat(body)) + } else { + handleUpstream(upstream) + } + }).end(Buffer.concat(body)) + } else { + res.setHeader('content-encoding', 'gzip') + res.end(gzipSync(readFileSync(advisoryBulkResponse))) + } + }) + return + } else if (req.url === '/-/npm/v1/security/audits/quick') { + const body = [] + req.on('data', c => body.push(c)) + req.on('end', () => { + res.setHeader('connection', 'close') + if (failAudit) { + res.statusCode = 503 + return res.end('no audit for you') + } + if (!auditResponse) { + res.statusCode = 404 + return res.end('not found') + } + if (doProxy && !existsSync(auditResponse)) { + return https.request({ + host: 'registry.npmjs.org', + method: req.method, + path: req.url, + headers: { + ...req.headers, + accept: '*', + host: 'registry.npmjs.org', + connection: 'close', + 'if-none-match': '', + }, + }).on('response', upstream => { + res.statusCode = upstream.statusCode + if (upstream.statusCode >= 300 || upstream.statusCode < 200) { + console.error('UPSTREAM ERROR', upstream.statusCode) + // don't save if it's not a valid response + return upstream.pipe(res) + } + res.setHeader('content-encoding', upstream.headers['content-encoding']) + const file = auditResponse + console.error('PROXY', `${req.url} -> ${file} ${upstream.statusCode}`) + mkdirp.sync(dirname(file)) + const data = [] + upstream.on('end', () => { + const out = Buffer.concat(data) + // make it a bit prettier to read later + const obj = JSON.parse(unzipSync(out).toString()) + writeFileSync(file, JSON.stringify(obj, 0, 2) + '\n') + res.end(out) + }) + upstream.on('data', c => data.push(c)) + }).end(Buffer.concat(body)) + } else { + res.setHeader('content-encoding', 'gzip') + res.end(gzipSync(readFileSync(auditResponse))) + } + }) + return + } + + const f = join(__dirname, 'content', join('/', req.url.replace(/@/, '').replace(/%2f/i, '/'))) + const isCorgi = req.headers.accept.includes('application/vnd.npm.install-v1+json') + const file = f + ( + isCorgi && existsSync(`${f}.min.json`) ? '.min.json' + : existsSync(`${f}.json`) ? '.json' + : existsSync(`${f}/index.json`) ? 'index.json' + : '' + ) + + try { + const body = readFileSync(file) + res.setHeader('content-length', body.length) + res.setHeader('content-type', /\.min\.json$/.test(file) ? corgiDoc + : /\.json$/.test(file) ? 'application/json' + : 'application/octet-stream') + res.end(body) + } catch (er) { + // testing things going missing from the registry somehow + if (missing.test(req.url)) { + res.statusCode = 404 + res.end('{"error": "not found"}') + return + } + + if (doProxy) { + return https.get({ + host: 'registry.npmjs.org', + path: req.url, + headers: { + ...req.headers, + accept: '*', + 'accept-encoding': 'identity', + host: 'registry.npmjs.org', + connection: 'close', + 'if-none-match': '', + }, + }).on('response', upstream => { + const errorStatus = + upstream.statusCode >= 300 || upstream.statusCode < 200 + + if (errorStatus) { + console.error('UPSTREAM ERROR', upstream.statusCode) + } + + const ct = upstream.headers['content-type'] + const isJson = ct.includes('application/json') + const file = isJson ? f + '.json' : f + console.error('PROXY', `${req.url} -> ${file} ${ct}`) + mkdirp.sync(dirname(file)) + const data = [] + res.statusCode = upstream.statusCode + res.setHeader('content-type', ct) + upstream.on('end', () => { + console.error('ENDING', req.url) + const out = Buffer.concat(data) + if (!errorStatus) { + if (isJson) { + const obj = JSON.parse(out.toString()) + writeFileSync(file, JSON.stringify(obj, 0, 2) + '\n') + const mrm = require('minify-registry-metadata') + const minFile = file.replace(/\.json$/, '.min.json') + writeFileSync(minFile, JSON.stringify(mrm(obj), 0, 2) + '\n') + console.error('WROTE JSONS', [file, minFile]) + } else { + writeFileSync(file, out) + } + } + res.end(out) + }) + upstream.on('data', c => data.push(c)) + }).end() + } + + res.statusCode = er.code === 'ENOENT' ? 404 : 500 + if (res.method === 'GET') { + console.error(er) + } + res.setHeader('content-type', 'text/plain') + res.end(er.stack) + } + }) + server.listen(PORT, cb) +} + +module.exports = t => startServer(() => { + t.parent.teardown(() => module.exports.server.close()) + t.end() +}) + +module.exports.auditResponse = value => { + if (auditResponse && auditResponse !== value) { + throw new Error('setting audit response, but already set\n' + + '(did you forget to call the returned function on teardown?)') + } + auditResponse = value + return () => auditResponse = null +} +module.exports.failAudit = () => { + failAudit = true + return () => failAudit = false +} + +module.exports.advisoryBulkResponse = value => { + if (advisoryBulkResponse && advisoryBulkResponse !== value) { + throw new Error('setting advisory bulk response, but already set\n' + + '(did you forget to call the returned function on teardown?)') + } + advisoryBulkResponse = value + return () => advisoryBulkResponse = null +} +module.exports.failAdvisoryBulk = () => { + failAdvisoryBulk = true + return () => failAdvisoryBulk = false +} + +module.exports.registry = `http://localhost:${PORT}/` + +module.exports.start = startServer +module.exports.stop = () => module.exports.server.close() + +if (require.main === module) { + startServer(() => { + console.log(`Mock registry live at: + ${module.exports.registry} +Press ^D to close gracefully.`) + }) + process.openStdin() + process.stdin.on('end', () => module.exports.server.close()) +} |