diff --git a/package.json b/package.json index bfc80c569..1fea1500e 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "browser-process-platform": "~0.1.1", "cross-env": "^6.0.0", "go-ipfs-dep": "^0.4.22", - "interface-ipfs-core": "^0.119.0", + "interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#fix/files-tests-tweaks", "ipfsd-ctl": "^0.47.1", "nock": "^11.4.0", "stream-equal": "^1.1.1" diff --git a/src/files/cp.js b/src/files/cp.js index de4e0954d..b779301a2 100644 --- a/src/files/cp.js +++ b/src/files/cp.js @@ -1,20 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') -const findSources = require('../utils/find-sources') +const configure = require('../lib/configure') +const { findSources } = require('./utils') -module.exports = (send) => { - return promisify(function () { - const { - callback, - sources, - opts - } = findSources(Array.prototype.slice.call(arguments)) +module.exports = configure(({ ky }) => { + return (...args) => { + const { sources, options } = findSources(args) - send({ - path: 'files/cp', - args: sources, - qs: opts - }, (error) => callback(error)) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + sources.forEach(src => searchParams.append('arg', src)) + if (options.format) searchParams.set('format', options.format) + if (options.flush != null) searchParams.set('flush', options.flush) + if (options.hashAlg) searchParams.set('hash', options.hashAlg) + if (options.parents != null) searchParams.set('parents', options.parents) + + return ky.post('files/cp', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).text() + } +}) diff --git a/src/files/flush.js b/src/files/flush.js index 6a131989b..b66568353 100644 --- a/src/files/flush.js +++ b/src/files/flush.js @@ -1,17 +1,27 @@ 'use strict' -const promisify = require('promisify-es6') +const CID = require('cids') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((args, callback) => { - if (typeof args === 'function') { - callback = args - args = '/' +module.exports = configure(({ ky }) => { + return async (path, options) => { + if (typeof path !== 'string') { + options = path + path = '/' } - return send({ - path: 'files/flush', - args: args - }, (error) => callback(error)) - }) -} + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', path) + + const { Cid } = await ky.post('files/flush', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return new CID(Cid) + } +}) diff --git a/src/files/index.js b/src/files/index.js index c543d9b1e..6c86c589a 100644 --- a/src/files/index.js +++ b/src/files/index.js @@ -1,20 +1,22 @@ 'use strict' const callbackify = require('callbackify') +const { collectify, streamify, pullify } = require('../lib/converters') const moduleConfig = require('../utils/module-config') module.exports = (arg, config) => { const send = moduleConfig(arg) + const ls = require('./ls')(config) return { - cp: require('./cp')(send), - mkdir: require('./mkdir')(send), - flush: require('./flush')(send), + cp: callbackify.variadic(require('./cp')(config)), + mkdir: callbackify.variadic(require('./mkdir')(config)), + flush: callbackify.variadic(require('./flush')(config)), stat: require('./stat')(send), rm: require('./rm')(send), - ls: require('./ls')(send), - lsReadableStream: require('./ls-readable-stream')(send), - lsPullStream: require('./ls-pull-stream')(send), + ls: callbackify.variadic(collectify(ls)), + lsReadableStream: streamify.readable(ls), + lsPullStream: pullify.source(ls), read: require('./read')(send), readReadableStream: require('./read-readable-stream')(send), readPullStream: require('./read-pull-stream')(send), diff --git a/src/files/ls-pull-stream.js b/src/files/ls-pull-stream.js deleted file mode 100644 index 111eba7ed..000000000 --- a/src/files/ls-pull-stream.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict' - -const toPull = require('stream-to-pull-stream') -const lsReadableStream = require('./ls-readable-stream') - -module.exports = (send) => { - return (args, opts) => { - opts = opts || {} - - return toPull.source(lsReadableStream(send)(args, opts)) - } -} diff --git a/src/files/ls-readable-stream.js b/src/files/ls-readable-stream.js deleted file mode 100644 index 404893e70..000000000 --- a/src/files/ls-readable-stream.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict' - -const { - Transform, - PassThrough -} = require('readable-stream') -const pump = require('pump') -const ndjson = require('ndjson') -const isStream = require('is-stream') - -const toEntry = (entry) => { - return { - name: entry.Name, - type: entry.Type, - size: entry.Size, - hash: entry.Hash - } -} - -module.exports = (send) => { - return (args, opts) => { - opts = opts || {} - - const transform = new Transform({ - objectMode: true, - - transform (entry, encoding, callback) { - callback(null, toEntry(entry)) - } - }) - - const output = new PassThrough({ - objectMode: true - }) - - send({ - path: 'files/ls', - args: args, - qs: Object.assign({}, opts, { stream: true }) - }, (err, res) => { - if (err) { - return output.destroy(err) - } - - if (isStream(res)) { - const parse = ndjson.parse() - - pump(res, parse, transform, output) - } else { - const entries = res.Entries || [] - - entries.forEach((entry) => { - output.write(toEntry(entry)) - }) - - output.end() - } - }) - - return output - } -} diff --git a/src/files/ls.js b/src/files/ls.js index ff22f5396..a36200d1d 100644 --- a/src/files/ls.js +++ b/src/files/ls.js @@ -1,37 +1,36 @@ 'use strict' -const promisify = require('promisify-es6') +const ndjson = require('iterable-ndjson') +const toIterable = require('../lib/stream-to-iterable') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - const entries = res.Entries || [] +module.exports = configure(({ ky }) => { + return async function * ls (path, options) { + options = options || {} - callback(null, entries.map((entry) => { - return { - name: entry.Name, - type: entry.Type, - size: entry.Size, - hash: entry.Hash - } - })) -} + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${path}`) + searchParams.set('stream', true) + if (options.cidBase) searchParams.set('cid-base', options.cidBase) + if (options.long != null) searchParams.set('long', options.long) -module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } + const res = await ky.post('files/ls', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }) - if (typeof (args) === 'function') { - callback = args - opts = {} - args = null + for await (const result of ndjson(toIterable(res.body))) { + // go-ipfs does not yet support the "stream" option + if ('Entries' in result) { + for (const entry of result.Entries || []) { + yield toCamel(entry) + } + return + } + yield toCamel(result) } - - return send.andTransform({ - path: 'files/ls', - args: args, - qs: opts - }, transform, callback) - }) -} + } +}) diff --git a/src/files/mkdir.js b/src/files/mkdir.js index b77fbf6dd..0fc3c238d 100644 --- a/src/files/mkdir.js +++ b/src/files/mkdir.js @@ -1,18 +1,24 @@ - 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') + +module.exports = configure(({ ky }) => { + return (path, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.append('arg', path) + if (options.cidVersion != null) searchParams.set('cid-version', options.cidVersion) + if (options.format) searchParams.set('format', options.format) + if (options.flush != null) searchParams.set('flush', options.flush) + if (options.hashAlg) searchParams.set('hash', options.hashAlg) + if (options.parents != null) searchParams.set('parents', options.parents) -module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/mkdir', - args: args, - qs: opts - }, (error) => callback(error)) - }) -} + return ky.post('files/mkdir', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).text() + } +}) diff --git a/src/files/mv.js b/src/files/mv.js index 234491b28..8546d8ab2 100644 --- a/src/files/mv.js +++ b/src/files/mv.js @@ -1,20 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') -const findSources = require('../utils/find-sources') +const configure = require('../lib/configure') +const { findSources } = require('./utils') -module.exports = (send) => { - return promisify(function () { - const { - callback, - sources, - opts - } = findSources(Array.prototype.slice.call(arguments)) +module.exports = configure(({ ky }) => { + return (...args) => { + const { sources, options } = findSources(args) - send({ - path: 'files/mv', - args: sources, - qs: opts - }, (error) => callback(error)) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + sources.forEach(src => searchParams.append('arg', src)) + if (options.format) searchParams.set('format', options.format) + if (options.flush != null) searchParams.set('flush', options.flush) + if (options.hashAlg) searchParams.set('hash', options.hashAlg) + if (options.parents != null) searchParams.set('parents', options.parents) + + return ky.post('files/mv', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).text() + } +}) diff --git a/src/files/utils.js b/src/files/utils.js new file mode 100644 index 000000000..67a04eb6e --- /dev/null +++ b/src/files/utils.js @@ -0,0 +1,23 @@ +'use strict' + +exports.findSources = (args) => { + let options = {} + let sources = [] + + if (!Array.isArray(args[args.length - 1]) && typeof args[args.length - 1] === 'object') { + options = args.pop() + } + + if (args.length === 1 && Array.isArray(args[0])) { + // support ipfs.file.cp([src, dest], opts) + sources = args[0] + } else { + // support ipfs.file.cp(src, dest, opts) and ipfs.file.cp(src1, src2, dest, opts) + sources = args + } + + return { + sources, + options + } +}