Skip to content

Commit 8741a66

Browse files
authored
fix: only spawn node once (#111)
1 parent 36f4e8f commit 8741a66

File tree

5 files changed

+74
-80
lines changed

5 files changed

+74
-80
lines changed

src/core/commands/start-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const favicon = require('ipfs-registry-mirror-common/handlers/favicon')
99
const root = require('../handlers/root')
1010
const tarball = require('../handlers/tarball')
1111
const manifest = require('../handlers/manifest')
12-
const getIpfs = require('../middlewares/getIpfs')
12+
const getIpfs = require('../middleware/get-ipfs')
1313

1414
const startServer = (config) => {
1515
const app = express()

src/core/handlers/manifest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = (config, app) => {
3030

3131
log(`Loading manifest for ${moduleName}`)
3232

33-
const ipfs = response.locals.ipfs
33+
const ipfs = await request.app.locals.ipfs
3434

3535
try {
3636
const manifest = await loadManifest(config, ipfs.api, moduleName)

src/core/handlers/tarball.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (config, app) => {
1313

1414
log(`Loading ${file}`)
1515

16-
const ipfs = response.locals.ipfs
16+
const ipfs = await request.app.locals.ipfs
1717

1818
try {
1919
const readStream = await loadTarball(config, ipfs.api, file)

src/core/middleware/get-ipfs.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict'
2+
3+
const startIpfs = require('../commands/start-ipfs')
4+
const request = require('ipfs-registry-mirror-common/utils/retry-request')
5+
const timeout = require('ipfs-registry-mirror-common/utils/timeout-promise')
6+
7+
const cleanUpOps = []
8+
9+
const cleanUp = () => {
10+
Promise.all(
11+
cleanUpOps.map(op => op())
12+
)
13+
.then(() => {
14+
process.exit(0)
15+
})
16+
}
17+
18+
process.on('SIGTERM', cleanUp)
19+
process.on('SIGINT', cleanUp)
20+
21+
module.exports = (options) => {
22+
return async (req, res, next) => {
23+
if (req.app.locals.ipfs) {
24+
return next()
25+
}
26+
27+
const createIpfs = async () => {
28+
const ipfs = await startIpfs(options)
29+
30+
cleanUpOps.push(() => {
31+
return new Promise((resolve) => {
32+
if (options.ipfs.node !== 'proc') {
33+
return resolve()
34+
}
35+
36+
ipfs.stop(() => {
37+
console.info('😈 IPFS node stopped') // eslint-disable-line no-console
38+
39+
resolve()
40+
})
41+
})
42+
})
43+
44+
console.info('🗂️ Loading registry index from', options.registry) // eslint-disable-line no-console
45+
46+
try {
47+
const mirror = await request(Object.assign({}, options.request, {
48+
uri: options.registry,
49+
json: true
50+
}))
51+
52+
console.info('☎️ Dialling registry mirror', mirror.ipfs.addresses.join(',')) // eslint-disable-line no-console
53+
54+
await timeout(
55+
ipfs.api.swarm.connect(mirror.ipfs.addresses[0]),
56+
options.registryConnectTimeout
57+
)
58+
59+
console.info('📱️ Connected to registry') // eslint-disable-line no-console
60+
} catch (error) {
61+
console.info('📴 Not connected to registry') // eslint-disable-line no-console
62+
}
63+
64+
return ipfs
65+
}
66+
67+
req.app.locals.ipfs = createIpfs()
68+
69+
next()
70+
}
71+
}

src/core/middlewares/getIpfs.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)