Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use async-await #26

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions examples/mfs-example.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict'

var ipfsBlobStore = require('../index.js')
var store = ipfsBlobStore().mfs
var ipfsBlobStore = require('../src')

/*
process.stdin.pipe(store.createWriteStream('test/2/3/4', function (err, metadata) {
if (err) {
// console.log(err)
}
ipfsBlobStore().then(store => {
/*
process.stdin.pipe(store.createWriteStream('test/2/3/4', function (err, metadata) {
if (err) {
// console.log(err)
}

console.log(metadata)
}))
*/

store.createReadStream('test/2/3/4').pipe(process.stdout)
console.log(metadata)
}))
*/
store.createReadStream('test/2/3/4').pipe(process.stdout)
})
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@
"devDependencies": {
"abstract-blob-store": "^3.3.4",
"aegir": "^17.0.1",
"ipfs": "~0.33.0",
"ipfs": "~0.34.4",
"ipfsd-ctl": "~0.40.0",
"tape": "^4.9.1",
"which": "^1.3.1"
},
"dependencies": {
"debug": "^4.1.0",
"ipfs": "~0.33.0",
"ipfs-api": "^26.1.2",
"promisify-es6": "^1.0.3"
"ipfs": "~0.34.4",
"ipfs-http-client": "^30.1.0"
}
}
39 changes: 13 additions & 26 deletions src/create.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const promisify = require('promisify-es6')
const IPFS = require('ipfs')
const remote = require('ipfs-api')
const remote = require('ipfs-http-client')
const mfs = require('./mfs')
const log = require('debug')('ipfs:blob-store:create')
const defaultOptions = {
Expand All @@ -11,43 +10,31 @@ const defaultOptions = {
baseDir: '/'
}

module.exports = promisify((opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = defaultOptions
}

module.exports = async (opts) => {
const options = Object.assign({}, defaultOptions, opts)

if (options.ipfs) {
log('Using pre-configured IPFS instance')
return setImmediate(() => callback(null, mfs(options)))
return mfs(options)
}

if (options.host && options.port) {
log(`Connecting to remote IPFS at ${options.host}:${options.port}`)
options.ipfs = remote(options.host, options.port)

return setImmediate(() => callback(null, mfs(options)))
return mfs(options)
}

log(`Starting an IPFS instance`)
callback = once(callback)

options.ipfs = new IPFS()
options.ipfs.once('ready', () => callback(null, mfs(options)))
options.ipfs.once('error', (error) => callback(error))
})

function once (cb) {
let called = false

return function () {
if (called) {
return
}
options.ipfs = await getIPFSReadyNode()
return mfs(options)
}

called = true
cb.apply(null, arguments)
}
function getIPFSReadyNode () {
return new Promise((resolve, reject) => {
const ipfs = new IPFS()
ipfs.once('ready', () => resolve(ipfs))
ipfs.once('error', reject)
})
}
26 changes: 15 additions & 11 deletions src/mfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function (options) {
}

store.createWriteStream = function (opts, cb) {
if (typeof opts === 'string') opts = {key: opts}
if (typeof opts === 'string') opts = { key: opts }
if (opts.name) opts.key = opts.name
if (!cb) cb = noop

Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = function (options) {
}

store.createReadStream = function (opts) {
if (typeof opts === 'string') opts = {key: opts}
if (typeof opts === 'string') opts = { key: opts }
if (opts.name) opts.key = opts.name

const readPath = normalisePath(store.baseDir + opts.key)
Expand All @@ -63,7 +63,7 @@ module.exports = function (options) {
const readableStream = ipfs.files.readReadableStream(readPath)

readableStream.on('error', (error) => {
if (error.toString().indexOf('does not exist') > -1 || error.toString().indexOf('Not a directory') > -1) {
if (isNotFoundError(error)) {
error.notFound = true
}
})
Expand All @@ -72,28 +72,28 @@ module.exports = function (options) {
}

store.exists = function (opts, cb) {
if (typeof opts === 'string') opts = {key: opts}
if (typeof opts === 'string') opts = { key: opts }
if (opts.name) opts.key = opts.name
if (!cb) cb = noop

const statPath = normalisePath(store.baseDir + opts.key)

log(`stat ${statPath}`)
ipfs.files.stat(statPath, {}, (error) => {
if (error) {
if (error.toString().indexOf('does not exist') > -1 || error.toString().indexOf('Not a directory') > -1) {
return cb(null, false)
}
if (!error) {
return cb(null, true)
}

return cb(error)
if (isNotFoundError(error)) {
return cb(null, false)
}

cb(null, true)
return cb(error)
})
}

store.remove = function (opts, cb) {
if (typeof opts === 'string') opts = {key: opts}
if (typeof opts === 'string') opts = { key: opts }
if (opts.name) opts.key = opts.name
if (!cb) cb = noop

Expand All @@ -111,3 +111,7 @@ function noop () {}
function normalisePath (path) {
return path.replace(/\/(\/)+/g, '/')
}

function isNotFoundError (error) {
return error.toString().indexOf('does not exist') > -1 || error.toString().indexOf('Not a directory') > -1
}