Skip to content

Commit b493e4c

Browse files
committed
feat: forward non-get requests to the registry, fixes #58
1 parent 8065c2f commit b493e4c

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ build/Release
3030
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
3131
node_modules
3232
package-lock.json
33+
34+
docs

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"debug": "^3.1.0",
3737
"dnscache": "^1.0.1",
3838
"express": "^4.16.3",
39+
"express-http-proxy": "^1.4.0",
3940
"follow-registry": "achingbrain/follow-registry",
4041
"ipfs-blob-store": "^2.0.0",
4142
"once": "^1.4.0",

src/cli/bin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ yargs.command('$0', 'Starts a registry server that uses IPFS to fetch js depende
3131
default: 'http'
3232
})
3333
.option('mirror-registry', {
34-
describe: 'Where to download missing files from',
34+
describe: 'Where to download missing files from/proxy for non-get requests',
3535
default: 'https://registry.npmjs.com'
3636
})
37+
.option('mirror-upload-size-limit', {
38+
describe: 'How large a file upload to allow when proxying for the registry',
39+
default: '1024mb'
40+
})
3741
.option('ipfs-port', {
3842
describe: 'Which port the daemon is listening on',
3943
default: null

src/core/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ module.exports = (overrides = {}) => {
3232
host: option(process.env.MIRROR_HOST, overrides.mirrorHost),
3333
port: option(process.env.MIRROR_PORT, overrides.mirrorPort),
3434
protocol: option(process.env.MIRROR_PROTOCOL, overrides.mirrorProtocol),
35-
registry: option(process.env.MIRROR_REGISTRY, overrides.mirrorRegistry)
35+
registry: option(process.env.MIRROR_REGISTRY, overrides.mirrorRegistry),
36+
uploadSizeLimit: option(process.env.MIRROR_UPLOAD_SIZE_LIMIT, overrides.mirrorUploadSizeLimit)
3637
},
3738
external: {
3839
host: option(process.env.EXTERNAL_HOST, overrides.externalHost),

src/core/mirror/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const clone = require('../clone')
1111
const store = require('./store')
1212
const ipfsBlobStore = require('ipfs-blob-store')
1313
const getExternalUrl = require('../utils/get-external-url')
14+
const proxy = require('express-http-proxy')
1415

1516
module.exports = async (options) => {
1617
options = config(options)
@@ -35,9 +36,19 @@ module.exports = async (options) => {
3536
next()
3637
})
3738

39+
// intercept requests for tarballs and manifests
3840
app.get('/**/*.tgz', tarball)
3941
app.get('/*', json)
4042

43+
// everything else should just proxy for the registry
44+
const registry = proxy(options.mirror.registry, {
45+
limit: options.mirror.uploadSizeLimit
46+
})
47+
app.put('/*', registry)
48+
app.post('/*', registry)
49+
app.patch('/*', registry)
50+
app.delete('/*', registry)
51+
4152
app.use(function (error, request, response, next) {
4253
console.error(`💀 ${request.method} ${request.url} ${response.statusCode} - ${error.stack}`)
4354

test/mirror.spec.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ describe('mirror', () => {
3838
app = await mirror({
3939
mirrorProtocol: 'http',
4040
mirrorPort: 0,
41-
mirrorHost: '127.0.0.1'
41+
mirrorHost: '127.0.0.1',
42+
mirrorRegistry: 'http://127.0.0.1:1234'
4243
})
4344

4445
const content = 'manifest content'
@@ -54,7 +55,8 @@ describe('mirror', () => {
5455
app = await mirror({
5556
mirrorProtocol: 'http',
5657
mirrorPort: 0,
57-
mirrorHost: '127.0.0.1'
58+
mirrorHost: '127.0.0.1',
59+
mirrorRegistry: 'http://127.0.0.1:1234'
5860
})
5961

6062
const content = 'tarball content'
@@ -140,4 +142,25 @@ describe('mirror', () => {
140142

141143
expect(result.trim()).to.equal(data.trim())
142144
})
145+
146+
it('should proxy all other requests to the registry', async () => {
147+
let data = 'hello world'
148+
149+
const server = await createTestServer((server) => {
150+
return {
151+
'/-/user/org.couchdb.user:dave': data
152+
}
153+
})
154+
155+
app = await mirror({
156+
mirrorProtocol: 'http',
157+
mirrorPort: 0,
158+
mirrorHost: '127.0.0.1',
159+
mirrorRegistry: `http://127.0.0.1:${server.address().port}`
160+
})
161+
162+
const result = await request.put(`http://127.0.0.1:${app.address().port}/-/user/org.couchdb.user:dave`)
163+
164+
expect(result.trim()).to.equal(data.trim())
165+
})
143166
})

0 commit comments

Comments
 (0)