Skip to content

Commit

Permalink
fix: remove redundant ipfs configing code
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jun 6, 2019
1 parent e825a0b commit 55978ab
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 152 deletions.
64 changes: 63 additions & 1 deletion src/core/commands/start-ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
10 changes: 6 additions & 4 deletions src/core/commands/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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, {
Expand All @@ -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) {
Expand Down
94 changes: 20 additions & 74 deletions src/core/commands/update.js
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion src/core/handlers/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/handlers/tarball.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
71 changes: 0 additions & 71 deletions src/core/middleware/get-ipfs.js

This file was deleted.

0 comments on commit 55978ab

Please sign in to comment.