diff --git a/src/core/commands/start-ipfs.js b/src/core/commands/start-ipfs.js index 2069ee5f..79768c74 100644 --- a/src/core/commands/start-ipfs.js +++ b/src/core/commands/start-ipfs.js @@ -4,6 +4,22 @@ const IpfsApi = require('ipfs-http-client') const ipfsdCtrl = require('ipfsd-ctl') const which = require('which-promise') const promisify = require('util').promisify +const request = require('ipfs-registry-mirror-common/utils/retry-request') +const timeout = require('ipfs-registry-mirror-common/utils/timeout-promise') + +const cleanUpOps = [] + +const cleanUp = () => { + Promise.all( + cleanUpOps.map(op => op()) + ) + .then(() => { + process.exit(0) + }) +} + +process.on('SIGTERM', cleanUp) +process.on('SIGINT', cleanUp) const spawn = (createArgs, spawnArgs = { init: true }) => { return new Promise((resolve, reject) => { @@ -72,4 +88,50 @@ const startIpfs = async (config) => { } } -module.exports = startIpfs +const createIpfs = options => { + return async () => { + const ipfs = await startIpfs(options) + + cleanUpOps.push(() => { + return new Promise((resolve) => { + if (options.ipfs.node !== 'proc') { + return resolve() + } + + ipfs.stop(() => { + console.info('😈 IPFS node stopped') // eslint-disable-line no-console + + resolve() + }) + }) + }) + + console.info('🗂️ Loading registry index from', options.registry) // eslint-disable-line no-console + + try { + const mirror = await request(Object.assign({}, options.request, { + uri: options.registry, + json: true + })) + + console.info('☎️ Dialling registry mirror', mirror.ipfs.addresses.join(',')) // eslint-disable-line no-console + + await timeout( + Promise.race( + mirror.ipfs.addresses.map(addr => { + return ipfs.api.swarm.connect(mirror.ipfs.addresses[0]) + }) + ), + options.registryConnectTimeout + ) + + console.info('📱️ Connected to registry') // eslint-disable-line no-console + } catch (error) { + console.info('📴 Not connected to registry') // eslint-disable-line no-console + } + + return ipfs + } +} + +module.exports = createIpfs diff --git a/src/core/commands/start-server.js b/src/core/commands/start-server.js index 8bb1f58e..87b996be 100644 --- a/src/core/commands/start-server.js +++ b/src/core/commands/start-server.js @@ -9,7 +9,7 @@ const favicon = require('ipfs-registry-mirror-common/handlers/favicon') const root = require('../handlers/root') const tarball = require('../handlers/tarball') const manifest = require('../handlers/manifest') -const getIpfs = require('../middleware/get-ipfs') +const startIpfs = require('./start-ipfs') const startServer = (config) => { const app = express() @@ -19,11 +19,11 @@ const startServer = (config) => { app.get('/favicon.ico', favicon(config, app)) app.get('/favicon.png', favicon(config, app)) - app.get('/', getIpfs(config), root(config, app)) + app.get('/', root(config, app)) // intercept requests for tarballs and manifests - app.get('/*.tgz', getIpfs(config), tarball(config, app)) - app.get('/*', getIpfs(config), manifest(config, app)) + app.get('/*.tgz', tarball(config, app)) + app.get('/*', manifest(config, app)) // everything else should just proxy for the registry const registry = proxy(config.registry, { @@ -36,6 +36,8 @@ const startServer = (config) => { app.use(errorLog) + app.locals.ipfs = startIpfs(config) + return new Promise((resolve, reject) => { const callback = once((error) => { if (error) { diff --git a/src/core/commands/update.js b/src/core/commands/update.js index 93c827fe..0dd0b863 100644 --- a/src/core/commands/update.js +++ b/src/core/commands/update.js @@ -1,97 +1,43 @@ 'use strict' +const request = require('ipfs-registry-mirror-common/utils/retry-request') const config = require('../config') const startIpfs = require('./start-ipfs') -const request = require('ipfs-registry-mirror-common/utils/retry-request') -const timeout = require('ipfs-registry-mirror-common/utils/timeout-promise') - -const cleanUpOps = [] - -const cleanUp = () => { - Promise.all( - cleanUpOps.map(op => op()) - ) - .then(() => { - process.exit(0) - }) -} - -process.on('SIGTERM', cleanUp) -process.on('SIGINT', cleanUp) module.exports = async (options) => { options = config(options) - const ipfs = await startIpfs(options) - - cleanUpOps.push(() => { - return new Promise((resolve) => { - if (options.ipfs.node !== 'proc') { - return resolve() - } - - ipfs.stop(() => { - console.info('👿 IPFS node stopped') // eslint-disable-line no-console - resolve() - }) - }) - }) - - console.info('🗂️ Loading registry index from', options.registry) // eslint-disable-line no-console + const ipfs = await startIpfs(options)() const mirror = await request(Object.assign({}, options.request, { uri: options.registry, json: true })) - console.info('☎️ Dialling registry mirror', mirror.ipfs.addresses.join(',')) // eslint-disable-line no-console + const tempPath = `${options.ipfs.prefix}-${Date.now()}` - let connected + console.info('📠 Copying registry index', mirror.root, 'to', tempPath) // eslint-disable-line no-console - await timeout( - Promise.race( - mirror.ipfs.addresses.map(addr => { - return ipfs.api.swarm.connect(mirror.ipfs.addresses[0]) - }) - ), - options.registryConnectTimeout - ) - .then(() => { - connected = true - }) - .catch(() => { - connected = false + try { + await ipfs.api.files.rm(tempPath, { + recursive: true }) + } catch (e) { + // ignore + } - if (connected) { - const tempPath = `${options.ipfs.prefix}-${Date.now()}` - - console.info('📠 Copying registry index', mirror.root, 'to', tempPath) // eslint-disable-line no-console - - try { - await ipfs.api.files.rm(tempPath, { - recursive: true - }) - } catch (e) { - // ignore - } - - await ipfs.api.files.cp(mirror.root, tempPath) - - console.info('💌 Copied registry index', mirror.root, 'to', tempPath) // eslint-disable-line no-console - - console.info('🗑️ Replacing old registry index if it exists') // eslint-disable-line no-console + await ipfs.api.files.cp(mirror.root, tempPath) - try { - await ipfs.api.files.rm(options.ipfs.prefix, { - recursive: true - }) - } catch (e) { - // ignore - } + console.info('💌 Copied registry index', mirror.root, 'to', tempPath) // eslint-disable-line no-console + console.info('🗑️ Replacing old registry index if it exists') // eslint-disable-line no-console - await ipfs.api.files.mv(tempPath, options.ipfs.prefix) + try { + await ipfs.api.files.rm(options.ipfs.prefix, { + recursive: true + }) + } catch (e) { + // ignore } - await cleanUp() + await ipfs.api.files.mv(tempPath, options.ipfs.prefix) } diff --git a/src/core/handlers/manifest.js b/src/core/handlers/manifest.js index c9eafcc5..71a5455e 100644 --- a/src/core/handlers/manifest.js +++ b/src/core/handlers/manifest.js @@ -30,7 +30,7 @@ module.exports = (config, app) => { log(`Loading manifest for ${moduleName}`) - const ipfs = await request.app.locals.ipfs + const ipfs = await request.app.locals.ipfs() try { const manifest = await loadManifest(config, ipfs.api, moduleName) diff --git a/src/core/handlers/tarball.js b/src/core/handlers/tarball.js index cf1e21a1..9d3ec7ff 100644 --- a/src/core/handlers/tarball.js +++ b/src/core/handlers/tarball.js @@ -13,7 +13,7 @@ module.exports = (config, app) => { log(`Loading ${file}`) - const ipfs = await request.app.locals.ipfs + const ipfs = await request.app.locals.ipfs() try { const readStream = await loadTarball(config, ipfs.api, file) diff --git a/src/core/middleware/get-ipfs.js b/src/core/middleware/get-ipfs.js deleted file mode 100644 index d3ef41ca..00000000 --- a/src/core/middleware/get-ipfs.js +++ /dev/null @@ -1,71 +0,0 @@ -'use strict' - -const startIpfs = require('../commands/start-ipfs') -const request = require('ipfs-registry-mirror-common/utils/retry-request') -const timeout = require('ipfs-registry-mirror-common/utils/timeout-promise') - -const cleanUpOps = [] - -const cleanUp = () => { - Promise.all( - cleanUpOps.map(op => op()) - ) - .then(() => { - process.exit(0) - }) -} - -process.on('SIGTERM', cleanUp) -process.on('SIGINT', cleanUp) - -module.exports = (options) => { - return (req, res, next) => { - if (req.app.locals.ipfs) { - return next() - } - - const createIpfs = async () => { - const ipfs = await startIpfs(options) - - cleanUpOps.push(() => { - return new Promise((resolve) => { - if (options.ipfs.node !== 'proc') { - return resolve() - } - - ipfs.stop(() => { - console.info('😈 IPFS node stopped') // eslint-disable-line no-console - - resolve() - }) - }) - }) - - console.info('🗂️ Loading registry index from', options.registry) // eslint-disable-line no-console - - try { - const mirror = await request(Object.assign({}, options.request, { - uri: options.registry, - json: true - })) - - console.info('☎️ Dialling registry mirror', mirror.ipfs.addresses.join(',')) // eslint-disable-line no-console - - await timeout( - ipfs.api.swarm.connect(mirror.ipfs.addresses[0]), - options.registryConnectTimeout - ) - - console.info('📱️ Connected to registry') // eslint-disable-line no-console - } catch (error) { - console.info('📴 Not connected to registry') // eslint-disable-line no-console - } - - return ipfs - } - - req.app.locals.ipfs = createIpfs() - - next() - } -}