From 3a7de0537f7be9431f2999edbf72d7bae86e950d Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 15 Nov 2019 17:02:05 +0000 Subject: [PATCH 1/7] refactor: convert some object and files API methods to async/await License: MIT Signed-off-by: Alan Shaw --- src/files/index.js | 5 +-- src/files/write.js | 70 +++++++++++++++++----------------------- src/object/appendData.js | 45 ++++++++++---------------- src/object/index.js | 9 +++--- src/object/put.js | 56 +++++++++++++------------------- src/object/setData.js | 44 ++++++++++--------------- 6 files changed, 95 insertions(+), 134 deletions(-) diff --git a/src/files/index.js b/src/files/index.js index d35e82ea7..c543d9b1e 100644 --- a/src/files/index.js +++ b/src/files/index.js @@ -1,8 +1,9 @@ 'use strict' +const callbackify = require('callbackify') const moduleConfig = require('../utils/module-config') -module.exports = (arg) => { +module.exports = (arg, config) => { const send = moduleConfig(arg) return { @@ -17,7 +18,7 @@ module.exports = (arg) => { read: require('./read')(send), readReadableStream: require('./read-readable-stream')(send), readPullStream: require('./read-pull-stream')(send), - write: require('./write')(send), + write: callbackify.variadic(require('./write')(config)), mv: require('./mv')(send) } } diff --git a/src/files/write.js b/src/files/write.js index 33f1ec973..77a772ea6 100644 --- a/src/files/write.js +++ b/src/files/write.js @@ -1,42 +1,32 @@ 'use strict' -const promisify = require('promisify-es6') -const concatStream = require('concat-stream') -const once = require('once') -const SendFilesStream = require('../utils/send-files-stream') - -module.exports = (send) => { - const sendFilesStream = SendFilesStream(send, 'files/write') - - return promisify((pathDst, _files, opts, _callback) => { - if (typeof opts === 'function' && - !_callback) { - _callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof _callback === 'function') { - _callback = opts - opts = {} - } - - const files = [].concat(_files) - const callback = once(_callback) - - const options = { - args: pathDst, - qs: opts - } - - const stream = sendFilesStream({ qs: options }) - const concat = concatStream((result) => callback(null, result)) - stream.once('error', callback) - stream.pipe(concat) - - files.forEach((file) => stream.write(file)) - stream.end() - }) -} +const configure = require('../lib/configure') +const toFormData = require('../lib/buffer-to-form-data') + +module.exports = configure(({ ky }) => { + return async (path, input, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', path) + searchParams.set('stream-channels', true) + if (options.cidVersion) searchParams.set('cid-version', options.cidVersion) + if (options.create != null) searchParams.set('create', options.create) + if (options.hashAlg) searchParams.set('hash', options.hashAlg) + if (options.length != null) searchParams.set('length', options.length) + if (options.offset != null) searchParams.set('offset', options.offset) + if (options.parents != null) searchParams.set('parents', options.parents) + if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves) + if (options.truncate != null) searchParams.set('truncate', options.truncate) + + const res = await ky.post('files/write', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams, + body: toFormData(input) // TODO: support inputs other than buffer as per spec + }) + + return res.text() + } +}) diff --git a/src/object/appendData.js b/src/object/appendData.js index 6c6cdb722..1a82a611c 100644 --- a/src/object/appendData.js +++ b/src/object/appendData.js @@ -1,35 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') -const once = require('once') const CID = require('cids') -const SendOneFile = require('../utils/send-one-file') +const configure = require('../lib/configure') +const toFormData = require('../lib/buffer-to-form-data') -module.exports = (send) => { - const sendOneFile = SendOneFile(send, 'object/patch/append-data') +module.exports = configure(({ ky }) => { + return async (cid, data, options) => { + options = options || {} - return promisify((cid, data, opts, _callback) => { - if (typeof opts === 'function') { - _callback = opts - opts = {} - } - const callback = once(_callback) - if (!opts) { - opts = {} - } + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${cid}`) - try { - cid = new CID(cid) - } catch (err) { - return callback(err) - } + const { Hash } = await ky.post('object/patch/append-data', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams, + body: toFormData(data) + }).json() - sendOneFile(data, { args: [cid.toString()] }, (err, result) => { - if (err) { - return callback(err) - } - - callback(null, new CID(result.Hash)) - }) - }) -} + return new CID(Hash) + } +}) diff --git a/src/object/index.js b/src/object/index.js index 2966232c4..9b365e49b 100644 --- a/src/object/index.js +++ b/src/object/index.js @@ -1,13 +1,14 @@ 'use strict' +const callbackify = require('callbackify') const moduleConfig = require('../utils/module-config') -module.exports = (arg) => { +module.exports = (arg, config) => { const send = moduleConfig(arg) return { get: require('./get')(send), - put: require('./put')(send), + put: callbackify.variadic(require('./put')(config)), data: require('./data')(send), links: require('./links')(send), stat: require('./stat')(send), @@ -15,8 +16,8 @@ module.exports = (arg) => { patch: { addLink: require('./addLink')(send), rmLink: require('./rmLink')(send), - setData: require('./setData')(send), - appendData: require('./appendData')(send) + setData: callbackify.variadic(require('./setData')(config)), + appendData: callbackify.variadic(require('./appendData')(config)) } } } diff --git a/src/object/put.js b/src/object/put.js index 40b9ffac4..be8660a00 100644 --- a/src/object/put.js +++ b/src/object/put.js @@ -1,26 +1,14 @@ 'use strict' -const promisify = require('promisify-es6') const CID = require('cids') const { DAGNode } = require('ipld-dag-pb') +const { Buffer } = require('buffer') +const configure = require('../lib/configure') +const toFormData = require('../lib/buffer-to-form-data') -const SendOneFile = require('../utils/send-one-file') -const once = require('once') - -module.exports = (send) => { - const sendOneFile = SendOneFile(send, 'object/put') - - return promisify((obj, options, _callback) => { - if (typeof options === 'function') { - _callback = options - options = {} - } - - const callback = once(_callback) - - if (!options) { - options = {} - } +module.exports = configure(({ ky }) => { + return async (obj, options) => { + options = options || {} let tmpObj = { Data: null, @@ -47,7 +35,7 @@ module.exports = (send) => { tmpObj.Data = obj.Data.toString() tmpObj.Links = obj.Links } else { - return callback(new Error('obj not recognized')) + throw new Error('obj not recognized') } let buf @@ -56,18 +44,20 @@ module.exports = (send) => { } else { buf = Buffer.from(JSON.stringify(tmpObj)) } - const enc = options.enc || 'json' - - const sendOptions = { - qs: { inputenc: enc } - } - - sendOneFile(buf, sendOptions, (err, result) => { - if (err) { - return callback(err) // early - } - callback(null, new CID(result.Hash)) - }) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + if (options.enc) searchParams.set('inputenc', options.enc) + if (options.pin != null) searchParams.set('pin', options.pin) + if (options.quiet != null) searchParams.set('quiet', options.quiet) + + const { Hash } = await ky.post('object/put', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams, + body: toFormData(buf) + }).json() + + return new CID(Hash) + } +}) diff --git a/src/object/setData.js b/src/object/setData.js index 5eb014474..2ca50fc2b 100644 --- a/src/object/setData.js +++ b/src/object/setData.js @@ -1,34 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') -const once = require('once') const CID = require('cids') -const SendOneFile = require('../utils/send-one-file') +const configure = require('../lib/configure') +const toFormData = require('../lib/buffer-to-form-data') -module.exports = (send) => { - const sendOneFile = SendOneFile(send, 'object/patch/set-data') +module.exports = configure(({ ky }) => { + return async (cid, data, options) => { + options = options || {} - return promisify((cid, data, opts, _callback) => { - if (typeof opts === 'function') { - _callback = opts - opts = {} - } - const callback = once(_callback) - if (!opts) { - opts = {} - } + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${cid}`) - try { - cid = new CID(cid) - } catch (err) { - return callback(err) - } + const { Hash } = await ky.post('object/patch/set-data', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams, + body: toFormData(data) + }).json() - sendOneFile(data, { args: [cid.toString()] }, (err, result) => { - if (err) { - return callback(err) - } - callback(null, new CID(result.Hash)) - }) - }) -} + return new CID(Hash) + } +}) From b7b4a91c56050bbfcad031b4bc7fd3f7e03a7d9b Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Nov 2019 15:56:04 +0000 Subject: [PATCH 2/7] refactor: convert files cp, flush, ls, mkdir and mv to async/await --- package.json | 2 +- src/files/cp.js | 36 ++++++++++--------- src/files/flush.js | 34 +++++++++++------- src/files/index.js | 14 ++++---- src/files/ls-pull-stream.js | 12 ------- src/files/ls-readable-stream.js | 62 --------------------------------- src/files/ls.js | 59 +++++++++++++++---------------- src/files/mkdir.js | 36 +++++++++++-------- src/files/mv.js | 36 ++++++++++--------- src/files/utils.js | 23 ++++++++++++ 10 files changed, 144 insertions(+), 170 deletions(-) delete mode 100644 src/files/ls-pull-stream.js delete mode 100644 src/files/ls-readable-stream.js create mode 100644 src/files/utils.js 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 + } +} From 537ee57a0f215c7b65442f8914be11c0a95182d9 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Nov 2019 16:58:26 +0000 Subject: [PATCH 3/7] refactor: convert remaining files API to async/await --- package.json | 10 +----- src/files/cp.js | 3 +- src/files/index.js | 19 +++++------ src/files/ls.js | 3 +- src/files/mv.js | 3 +- src/files/read-pull-stream.js | 26 --------------- src/files/read-readable-stream.js | 26 --------------- src/files/read.js | 38 ++++++++++++--------- src/files/rm.js | 38 +++++++++------------ src/files/stat.js | 55 +++++++++++++++---------------- src/files/utils.js | 4 +-- src/utils/find-sources.js | 25 -------------- src/utils/load-commands.js | 6 ++-- 13 files changed, 85 insertions(+), 171 deletions(-) delete mode 100644 src/files/read-pull-stream.js delete mode 100644 src/files/read-readable-stream.js delete mode 100644 src/utils/find-sources.js diff --git a/package.json b/package.json index 1fea1500e..cefc1f849 100644 --- a/package.json +++ b/package.json @@ -53,14 +53,11 @@ "cids": "~0.7.1", "concat-stream": "github:hugomrdias/concat-stream#feat/smaller", "debug": "^4.1.0", - "delay": "^4.3.0", "detect-node": "^2.0.4", - "end-of-stream": "^1.4.1", "err-code": "^2.0.0", "explain-error": "^1.0.4", "flatmap": "0.0.3", "form-data": "^3.0.0", - "fs-extra": "^8.1.0", "glob": "^7.1.3", "ipfs-block": "~0.8.1", "ipfs-utils": "^0.4.0", @@ -71,13 +68,10 @@ "is-pull-stream": "0.0.0", "is-stream": "^2.0.0", "iso-stream-http": "~0.1.2", - "iso-url": "~0.4.6", "it-glob": "0.0.6", "it-tar": "^1.1.0", "it-to-stream": "^0.1.1", "iterable-ndjson": "^1.1.0", - "just-kebab-case": "^1.1.0", - "just-map-keys": "^1.1.0", "kind-of": "^6.0.2", "ky": "^0.15.0", "ky-universal": "^0.3.0", @@ -95,13 +89,11 @@ "promisify-es6": "^1.0.3", "pull-defer": "~0.2.3", "pull-stream": "^3.6.9", - "pull-stream-to-async-iterator": "^1.0.2", "pull-to-stream": "~0.1.1", "pump": "^3.0.0", "qs": "^6.5.2", "readable-stream": "^3.1.1", - "stream-to-pull-stream": "^1.7.2", - "through2": "^3.0.1" + "stream-to-pull-stream": "^1.7.2" }, "devDependencies": { "aegir": "^20.4.1", diff --git a/src/files/cp.js b/src/files/cp.js index b779301a2..812c8b7d7 100644 --- a/src/files/cp.js +++ b/src/files/cp.js @@ -1,5 +1,6 @@ 'use strict' +const CID = require('cids') const configure = require('../lib/configure') const { findSources } = require('./utils') @@ -8,7 +9,7 @@ module.exports = configure(({ ky }) => { const { sources, options } = findSources(args) const searchParams = new URLSearchParams(options.searchParams) - sources.forEach(src => searchParams.append('arg', src)) + sources.forEach(src => searchParams.append('arg', CID.isCID(src) ? `/ipfs/${src}` : 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) diff --git a/src/files/index.js b/src/files/index.js index 6c86c589a..25e79fcab 100644 --- a/src/files/index.js +++ b/src/files/index.js @@ -1,26 +1,25 @@ 'use strict' const callbackify = require('callbackify') -const { collectify, streamify, pullify } = require('../lib/converters') -const moduleConfig = require('../utils/module-config') +const { collectify, streamify, pullify, concatify } = require('../lib/converters') -module.exports = (arg, config) => { - const send = moduleConfig(arg) +module.exports = config => { const ls = require('./ls')(config) + const read = require('./read')(config) return { 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), + stat: callbackify.variadic(require('./stat')(config)), + rm: callbackify.variadic(require('./rm')(config)), 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), + read: callbackify.variadic(concatify(read)), + readReadableStream: streamify.readable(read), + readPullStream: pullify.source(read), write: callbackify.variadic(require('./write')(config)), - mv: require('./mv')(send) + mv: callbackify.variadic(require('./mv')(config)) } } diff --git a/src/files/ls.js b/src/files/ls.js index a36200d1d..c60ff172e 100644 --- a/src/files/ls.js +++ b/src/files/ls.js @@ -1,5 +1,6 @@ 'use strict' +const CID = require('cids') const ndjson = require('iterable-ndjson') const toIterable = require('../lib/stream-to-iterable') const configure = require('../lib/configure') @@ -10,7 +11,7 @@ module.exports = configure(({ ky }) => { options = options || {} const searchParams = new URLSearchParams(options.searchParams) - searchParams.set('arg', `${path}`) + searchParams.set('arg', CID.isCID(path) ? `/ipfs/${path}` : path) searchParams.set('stream', true) if (options.cidBase) searchParams.set('cid-base', options.cidBase) if (options.long != null) searchParams.set('long', options.long) diff --git a/src/files/mv.js b/src/files/mv.js index 8546d8ab2..fadf0c34c 100644 --- a/src/files/mv.js +++ b/src/files/mv.js @@ -1,5 +1,6 @@ 'use strict' +const CID = require('cids') const configure = require('../lib/configure') const { findSources } = require('./utils') @@ -8,7 +9,7 @@ module.exports = configure(({ ky }) => { const { sources, options } = findSources(args) const searchParams = new URLSearchParams(options.searchParams) - sources.forEach(src => searchParams.append('arg', src)) + sources.forEach(src => searchParams.append('arg', CID.isCID(src) ? `/ipfs/${src}` : 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) diff --git a/src/files/read-pull-stream.js b/src/files/read-pull-stream.js deleted file mode 100644 index 3e9ab8c3d..000000000 --- a/src/files/read-pull-stream.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict' - -const toPull = require('stream-to-pull-stream') -const deferred = require('pull-defer') - -module.exports = (send) => { - return (args, opts) => { - opts = opts || {} - - const p = deferred.source() - - send({ - path: 'files/read', - args: args, - qs: opts - }, (err, stream) => { - if (err) { - return p.abort(err) - } - - p.resolve(toPull(stream)) - }) - - return p - } -} diff --git a/src/files/read-readable-stream.js b/src/files/read-readable-stream.js deleted file mode 100644 index df8b92fbf..000000000 --- a/src/files/read-readable-stream.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict' - -const Stream = require('readable-stream') -const pump = require('pump') - -module.exports = (send) => { - return (args, opts) => { - opts = opts || {} - - const pt = new Stream.PassThrough() - - send({ - path: 'files/read', - args: args, - qs: opts - }, (err, stream) => { - if (err) { - return pt.destroy(err) - } - - pump(stream, pt) - }) - - return pt - } -} diff --git a/src/files/read.js b/src/files/read.js index 378d53dfa..5a6a14acb 100644 --- a/src/files/read.js +++ b/src/files/read.js @@ -1,19 +1,27 @@ 'use strict' -const promisify = require('promisify-es6') -const streamToValue = require('../utils/stream-to-value') +const { Buffer } = require('buffer') +const configure = require('../lib/configure') +const toIterable = require('../lib/stream-to-iterable') -module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } +module.exports = configure(({ ky }) => { + return async function * read (path, options) { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.append('arg', `${path}`) + if (options.length != null) searchParams.set('length', options.length) + if (options.offset != null) searchParams.set('offset', options.offset) - send.andTransform({ - path: 'files/read', - args: args, - qs: opts - }, streamToValue, callback) - }) -} + const res = await ky.post('files/read', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }) + + for await (const chunk of toIterable(res.body)) { + yield Buffer.from(chunk) + } + } +}) diff --git a/src/files/rm.js b/src/files/rm.js index 404ceed10..c0a5e7eab 100644 --- a/src/files/rm.js +++ b/src/files/rm.js @@ -1,27 +1,21 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((path, opts, callback) => { - if (typeof opts === 'function' && - !callback) { - callback = opts - opts = {} - } +module.exports = configure(({ ky }) => { + return (path, options) => { + options = options || {} - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof callback === 'function') { - callback = opts - opts = {} - } + const searchParams = new URLSearchParams(options.searchParams) + searchParams.append('arg', path) + if (options.recursive != null) searchParams.set('recursive', options.recursive) + if (options.force != null) searchParams.set('force', options.force) - send({ - path: 'files/rm', - args: path, - qs: opts - }, (error) => callback(error)) - }) -} + return ky.post('files/rm', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).text() + } +}) diff --git a/src/files/stat.js b/src/files/stat.js index 7274e32a1..98026283e 100644 --- a/src/files/stat.js +++ b/src/files/stat.js @@ -1,35 +1,32 @@ 'use strict' -const promisify = require('promisify-es6') -const mapKeys = require('just-map-keys') -const kebabCase = require('just-kebab-case') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (data, callback) { - callback(null, { - type: data.Type, - blocks: data.Blocks, - size: data.Size, - hash: data.Hash, - cumulativeSize: data.CumulativeSize, - withLocality: data.WithLocality || false, - local: data.Local || undefined, - sizeLocal: data.SizeLocal || undefined - }) -} - -module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} +module.exports = configure(({ ky }) => { + return async (path, options) => { + if (typeof path !== 'string') { + options = path + path = '/' } - opts = mapKeys(opts, (v, k) => kebabCase(k)) + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', path) + if (options.cidBase) searchParams.set('cid-base', options.cidBase) + if (options.hash != null) searchParams.set('hash', options.hash) + if (options.size != null) searchParams.set('size', options.size) + if (options.withLocal != null) searchParams.set('with-local', options.withLocal) + + const res = await ky.post('files/stat', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() - send.andTransform({ - path: 'files/stat', - args: args, - qs: opts - }, transform, callback) - }) -} + res.WithLocality = res.WithLocality || false + return toCamel(res) + } +}) diff --git a/src/files/utils.js b/src/files/utils.js index 67a04eb6e..6dc2c499d 100644 --- a/src/files/utils.js +++ b/src/files/utils.js @@ -9,10 +9,10 @@ exports.findSources = (args) => { } if (args.length === 1 && Array.isArray(args[0])) { - // support ipfs.file.cp([src, dest], opts) + // support ipfs.files.cp([src, dest], opts) sources = args[0] } else { - // support ipfs.file.cp(src, dest, opts) and ipfs.file.cp(src1, src2, dest, opts) + // support ipfs.files.cp(src, dest, opts) and ipfs.files.cp(src1, src2, dest, opts) sources = args } diff --git a/src/utils/find-sources.js b/src/utils/find-sources.js deleted file mode 100644 index 2a862514a..000000000 --- a/src/utils/find-sources.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' - -module.exports = (args) => { - const callback = args.pop() - let opts = {} - let sources = [] - - if (!Array.isArray(args[args.length - 1]) && typeof args[args.length - 1] === 'object') { - opts = args.pop() - } - - if (args.length === 1 && Array.isArray(args[0])) { - // support ipfs.file.cp([src, dest], opts, cb) - sources = args[0] - } else { - // support ipfs.file.cp(src, dest, opts, cb) and ipfs.file.cp(src1, src2, dest, opts, cb) - sources = args - } - - return { - callback, - sources, - opts - } -} diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 3e51acdb2..041ca4492 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -113,7 +113,8 @@ function requireCommands (send, config) { config: require('../config')(config), dag: require('../dag')(config), dht: require('../dht')(config), - diag: require('../diag')(config) + diag: require('../diag')(config), + files: require('../files')(config) } Object.assign(cmds.refs, { @@ -124,9 +125,6 @@ function requireCommands (send, config) { }) const subCmds = { - // Files MFS (Mutable Filesystem) - files: require('../files'), - // Graph object: require('../object'), pin: require('../pin'), From 198e7a9c59a5d18a03fad12406cc8780059cd72e Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Nov 2019 17:02:35 +0000 Subject: [PATCH 4/7] fix: revert change to files/flush --- src/files/flush.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/files/flush.js b/src/files/flush.js index b66568353..ab2062387 100644 --- a/src/files/flush.js +++ b/src/files/flush.js @@ -1,6 +1,5 @@ 'use strict' -const CID = require('cids') const configure = require('../lib/configure') module.exports = configure(({ ky }) => { @@ -15,13 +14,11 @@ module.exports = configure(({ ky }) => { const searchParams = new URLSearchParams(options.searchParams) searchParams.set('arg', path) - const { Cid } = await ky.post('files/flush', { + await ky.post('files/flush', { timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams - }).json() - - return new CID(Cid) + }).text() } }) From 59772c58580595ac39b809cf9bdf19536de488d3 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Nov 2019 17:17:01 +0000 Subject: [PATCH 5/7] fix: tests --- src/files/ls.js | 5 +++++ test/files-mfs.spec.js | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/files/ls.js b/src/files/ls.js index c60ff172e..1baa3f656 100644 --- a/src/files/ls.js +++ b/src/files/ls.js @@ -8,6 +8,11 @@ const toCamel = require('../lib/object-to-camel') module.exports = configure(({ ky }) => { return async function * ls (path, options) { + if (typeof path !== 'string') { + options = path + path = '/' + } + options = options || {} const searchParams = new URLSearchParams(options.searchParams) diff --git a/test/files-mfs.spec.js b/test/files-mfs.spec.js index 203fd6eb5..6ae508d1d 100644 --- a/test/files-mfs.spec.js +++ b/test/files-mfs.spec.js @@ -376,9 +376,7 @@ describe('.files (the MFS API part)', function () { cumulativeSize: 70, blocks: 1, type: 'file', - withLocality: false, - local: undefined, - sizeLocal: undefined + withLocality: false }) }) From 8b2423e6440f5cf5d42d657e7aac0e62b2a29ce2 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Nov 2019 17:24:29 +0000 Subject: [PATCH 6/7] chore: update interface-ipfs-core dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cefc1f849..0b12957b7 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "browser-process-platform": "~0.1.1", "cross-env": "^6.0.0", "go-ipfs-dep": "^0.4.22", - "interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#fix/files-tests-tweaks", + "interface-ipfs-core": "^0.120.0", "ipfsd-ctl": "^0.47.1", "nock": "^11.4.0", "stream-equal": "^1.1.1" From f447ea790e3ee95b8999712471cbb50c0b358752 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 20 Nov 2019 13:09:29 +0000 Subject: [PATCH 7/7] refactor: convert remaining object API to async/await License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- src/lib/configure.js | 8 +- src/object/addLink.js | 35 -------- src/object/data.js | 63 +++++---------- src/object/get.js | 79 ++++++------------- src/object/index.js | 29 +++---- src/object/links.js | 63 ++++----------- src/object/new.js | 38 +++++---- src/object/patch/add-link.js | 25 ++++++ .../{appendData.js => patch/append-data.js} | 7 +- src/object/patch/index.js | 10 +++ src/object/patch/rm-link.js | 24 ++++++ src/object/{setData.js => patch/set-data.js} | 7 +- src/object/rmLink.js | 35 -------- src/object/stat.js | 41 +++++----- src/utils/load-commands.js | 4 +- 16 files changed, 189 insertions(+), 281 deletions(-) delete mode 100644 src/object/addLink.js create mode 100644 src/object/patch/add-link.js rename src/object/{appendData.js => patch/append-data.js} (68%) create mode 100644 src/object/patch/index.js create mode 100644 src/object/patch/rm-link.js rename src/object/{setData.js => patch/set-data.js} (68%) delete mode 100644 src/object/rmLink.js diff --git a/package.json b/package.json index 0b12957b7..4b305550a 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,6 @@ "kind-of": "^6.0.2", "ky": "^0.15.0", "ky-universal": "^0.3.0", - "lru-cache": "^5.1.1", "merge-options": "^2.0.0", "multiaddr": "^6.0.6", "multibase": "~0.6.0", @@ -83,6 +82,7 @@ "multihashes": "~0.4.14", "ndjson": "github:hugomrdias/ndjson#feat/readable-stream3", "once": "^1.4.0", + "parse-duration": "^0.1.1", "peer-id": "~0.12.3", "peer-info": "~0.15.1", "promise-nodeify": "^3.0.1", diff --git a/src/lib/configure.js b/src/lib/configure.js index 8401683f9..b1a1f8948 100644 --- a/src/lib/configure.js +++ b/src/lib/configure.js @@ -6,6 +6,7 @@ const { isBrowser, isWebWorker } = require('ipfs-utils/src/env') const { toUri } = require('./multiaddr') const errorHandler = require('./error-handler') const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) +const parseDuration = require('parse-duration') // Set default configuration and call create function with them module.exports = create => config => { @@ -27,7 +28,7 @@ module.exports = create => config => { // https://github.com/sindresorhus/ky/pull/153 const defaults = { prefixUrl: config.apiAddr + config.apiPath, - timeout: config.timeout || 60000 * 20, + timeout: parseTimeout(config.timeout) || 60000 * 20, headers: config.headers, hooks: { afterResponse: [errorHandler] @@ -72,6 +73,11 @@ function getDefaultApiAddr ({ protocol, host, port }) { // undefined values in the passed `options` object function wrap (fn, defaults) { return (input, options) => { + if (options.timeout) options.timeout = parseTimeout(options.timeout) return fn(input, mergeOptions(defaults, options)) } } + +function parseTimeout (value) { + return typeof value === 'string' ? parseDuration(value) : value +} diff --git a/src/object/addLink.js b/src/object/addLink.js deleted file mode 100644 index bda202bce..000000000 --- a/src/object/addLink.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') -const CID = require('cids') - -module.exports = (send) => { - return promisify((cid, dLink, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } - - try { - cid = new CID(cid) - } catch (err) { - return callback(err) - } - - const args = [ - cid.toString(), - dLink.Name || dLink.name || null, - (dLink.Hash || dLink.cid || '').toString() || null - ] - - send({ path: 'object/patch/add-link', args }, (err, result) => { - if (err) { - return callback(err) - } - callback(null, new CID(result.Hash)) - }) - }) -} diff --git a/src/object/data.js b/src/object/data.js index bbc43ebd6..8f1e42b7a 100644 --- a/src/object/data.js +++ b/src/object/data.js @@ -1,53 +1,26 @@ 'use strict' -const promisify = require('promisify-es6') -const streamToValue = require('../utils/stream-to-value') +const { Buffer } = require('buffer') const CID = require('cids') -const LRU = require('lru-cache') -const lruOptions = { - max: 128 -} +const configure = require('../lib/configure') +const toIterable = require('../lib/stream-to-iterable') -const cache = new LRU(lruOptions) +module.exports = configure(({ ky }) => { + return async function * (cid, options) { + options = options || {} -module.exports = (send) => { - return promisify((cid, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } - if (!options) { - options = {} - } - - let cidB58Str - - try { - cid = new CID(cid) - cidB58Str = cid.toBaseEncodedString() - } catch (err) { - return callback(err) - } + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) - const node = cache.get(cidB58Str) + const res = await ky.get('object/data', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }) - if (node) { - return callback(null, node.data) + for await (const chunk of toIterable(res.body)) { + yield Buffer.from(chunk) } - - send({ - path: 'object/data', - args: cidB58Str - }, (err, result) => { - if (err) { - return callback(err) - } - - if (typeof result.pipe === 'function') { - streamToValue(result, callback) - } else { - callback(null, result) - } - }) - }) -} + } +}) diff --git a/src/object/get.js b/src/object/get.js index 5052c8af3..5ed7468dc 100644 --- a/src/object/get.js +++ b/src/object/get.js @@ -1,57 +1,28 @@ 'use strict' -const promisify = require('promisify-es6') -const { DAGNode, DAGLink } = require('ipld-dag-pb') +const { Buffer } = require('buffer') const CID = require('cids') -const LRU = require('lru-cache') -const lruOptions = { - max: 128 -} - -const cache = new LRU(lruOptions) - -module.exports = (send) => { - return promisify((cid, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } - - if (!options) { - options = {} - } - - let cidB58Str - - try { - cid = new CID(cid) - cidB58Str = cid.toBaseEncodedString() - } catch (err) { - return callback(err) - } - - const node = cache.get(cidB58Str) - - if (node) { - return callback(null, node) - } - - send({ - path: 'object/get', - args: cidB58Str, - qs: { - 'data-encoding': 'base64' - } - }, (err, result) => { - if (err) { - return callback(err) - } - - const links = result.Links.map(l => new DAGLink(l.Name, l.Size, l.Hash)) - const node = new DAGNode(Buffer.from(result.Data, 'base64'), links) - - cache.set(cidB58Str, node) - callback(null, node) - }) - }) -} +const { DAGNode, DAGLink } = require('ipld-dag-pb') +const configure = require('../lib/configure') + +module.exports = configure(({ ky }) => { + return async (cid, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) + searchParams.set('data-encoding', 'base64') + + const res = await ky.get('object/get', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return new DAGNode( + Buffer.from(res.Data, 'base64'), + (res.Links || []).map(l => new DAGLink(l.Name, l.Size, l.Hash)) + ) + } +}) diff --git a/src/object/index.js b/src/object/index.js index 9b365e49b..dac85598c 100644 --- a/src/object/index.js +++ b/src/object/index.js @@ -1,23 +1,14 @@ 'use strict' const callbackify = require('callbackify') -const moduleConfig = require('../utils/module-config') +const { concatify } = require('../lib/converters') -module.exports = (arg, config) => { - const send = moduleConfig(arg) - - return { - get: require('./get')(send), - put: callbackify.variadic(require('./put')(config)), - data: require('./data')(send), - links: require('./links')(send), - stat: require('./stat')(send), - new: require('./new')(send), - patch: { - addLink: require('./addLink')(send), - rmLink: require('./rmLink')(send), - setData: callbackify.variadic(require('./setData')(config)), - appendData: callbackify.variadic(require('./appendData')(config)) - } - } -} +module.exports = config => ({ + data: callbackify.variadic(concatify(require('./data')(config))), + get: callbackify.variadic(require('./get')(config)), + links: callbackify.variadic(require('./links')(config)), + new: callbackify.variadic(require('./new')(config)), + patch: require('./patch')(config), + put: callbackify.variadic(require('./put')(config)), + stat: callbackify.variadic(require('./stat')(config)) +}) diff --git a/src/object/links.js b/src/object/links.js index 0b0366c81..49b088254 100644 --- a/src/object/links.js +++ b/src/object/links.js @@ -1,53 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') -const { DAGLink } = require('ipld-dag-pb') +const { Buffer } = require('buffer') const CID = require('cids') -const LRU = require('lru-cache') -const lruOptions = { - max: 128 -} - -const cache = new LRU(lruOptions) - -module.exports = (send) => { - return promisify((cid, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } - if (!options) { - options = {} - } - - try { - cid = new CID(cid) - } catch (err) { - return callback(err) - } - - const node = cache.get(cid.toString()) +const { DAGLink } = require('ipld-dag-pb') +const configure = require('../lib/configure') - if (node) { - return callback(null, node.links) - } +module.exports = configure(({ ky }) => { + return async (cid, options) => { + options = options || {} - send({ - path: 'object/links', - args: cid.toString() - }, (err, result) => { - if (err) { - return callback(err) - } + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) - let links = [] + const res = await ky.get('object/links', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() - if (result.Links) { - links = result.Links.map((l) => { - return new DAGLink(l.Name, l.Size, l.Hash) - }) - } - callback(null, links) - }) - }) -} + return (res.Links || []).map(l => new DAGLink(l.Name, l.Size, l.Hash)) + } +}) diff --git a/src/object/new.js b/src/object/new.js index f9d8ddb35..d372812a5 100644 --- a/src/object/new.js +++ b/src/object/new.js @@ -1,23 +1,27 @@ 'use strict' -const promisify = require('promisify-es6') const CID = require('cids') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((template, callback) => { - if (typeof template === 'function') { - callback = template - template = undefined +module.exports = configure(({ ky }) => { + return async (template, options) => { + if (typeof template !== 'string') { + options = template + template = null } - send({ - path: 'object/new', - args: template - }, (err, result) => { - if (err) { - return callback(err) - } - callback(null, new CID(result.Hash)) - }) - }) -} + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + if (template) searchParams.set('arg', template) + + const { Hash } = await ky.post('object/new', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return new CID(Hash) + } +}) diff --git a/src/object/patch/add-link.js b/src/object/patch/add-link.js new file mode 100644 index 000000000..55821751d --- /dev/null +++ b/src/object/patch/add-link.js @@ -0,0 +1,25 @@ +'use strict' + +const { Buffer } = require('buffer') +const CID = require('cids') +const configure = require('../../lib/configure') + +module.exports = configure(({ ky }) => { + return async (cid, dLink, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) + searchParams.append('arg', dLink.Name || dLink.name || null) + searchParams.append('arg', (dLink.Hash || dLink.cid || '').toString() || null) + + const { Hash } = await ky.post('object/patch/add-link', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return new CID(Hash) + } +}) diff --git a/src/object/appendData.js b/src/object/patch/append-data.js similarity index 68% rename from src/object/appendData.js rename to src/object/patch/append-data.js index 1a82a611c..1ab8d1381 100644 --- a/src/object/appendData.js +++ b/src/object/patch/append-data.js @@ -1,15 +1,16 @@ 'use strict' +const { Buffer } = require('buffer') const CID = require('cids') -const configure = require('../lib/configure') -const toFormData = require('../lib/buffer-to-form-data') +const configure = require('../../lib/configure') +const toFormData = require('../../lib/buffer-to-form-data') module.exports = configure(({ ky }) => { return async (cid, data, options) => { options = options || {} const searchParams = new URLSearchParams(options.searchParams) - searchParams.set('arg', `${cid}`) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) const { Hash } = await ky.post('object/patch/append-data', { timeout: options.timeout, diff --git a/src/object/patch/index.js b/src/object/patch/index.js new file mode 100644 index 000000000..5711200ec --- /dev/null +++ b/src/object/patch/index.js @@ -0,0 +1,10 @@ +'use strict' + +const callbackify = require('callbackify') + +module.exports = config => ({ + addLink: callbackify.variadic(require('./add-link')(config)), + appendData: callbackify.variadic(require('./append-data')(config)), + rmLink: callbackify.variadic(require('./rm-link')(config)), + setData: callbackify.variadic(require('./set-data')(config)) +}) diff --git a/src/object/patch/rm-link.js b/src/object/patch/rm-link.js new file mode 100644 index 000000000..39ea0ea3b --- /dev/null +++ b/src/object/patch/rm-link.js @@ -0,0 +1,24 @@ +'use strict' + +const { Buffer } = require('buffer') +const CID = require('cids') +const configure = require('../../lib/configure') + +module.exports = configure(({ ky }) => { + return async (cid, dLink, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) + searchParams.append('arg', dLink.Name || dLink.name || null) + + const { Hash } = await ky.post('object/patch/rm-link', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return new CID(Hash) + } +}) diff --git a/src/object/setData.js b/src/object/patch/set-data.js similarity index 68% rename from src/object/setData.js rename to src/object/patch/set-data.js index 2ca50fc2b..7bb131fb7 100644 --- a/src/object/setData.js +++ b/src/object/patch/set-data.js @@ -1,15 +1,16 @@ 'use strict' +const { Buffer } = require('buffer') const CID = require('cids') -const configure = require('../lib/configure') -const toFormData = require('../lib/buffer-to-form-data') +const configure = require('../../lib/configure') +const toFormData = require('../../lib/buffer-to-form-data') module.exports = configure(({ ky }) => { return async (cid, data, options) => { options = options || {} const searchParams = new URLSearchParams(options.searchParams) - searchParams.set('arg', `${cid}`) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) const { Hash } = await ky.post('object/patch/set-data', { timeout: options.timeout, diff --git a/src/object/rmLink.js b/src/object/rmLink.js deleted file mode 100644 index db699feeb..000000000 --- a/src/object/rmLink.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') -const CID = require('cids') - -module.exports = (send) => { - return promisify((cid, dLink, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } - - try { - cid = new CID(cid) - } catch (err) { - return callback(err) - } - - send({ - path: 'object/patch/rm-link', - args: [ - cid.toString(), - dLink.Name || dLink.name || null - ] - }, (err, result) => { - if (err) { - return callback(err) - } - callback(null, new CID(result.Hash)) - }) - }) -} diff --git a/src/object/stat.js b/src/object/stat.js index 6488a297e..c3d2d8edc 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -1,28 +1,31 @@ 'use strict' -const promisify = require('promisify-es6') +const { Buffer } = require('buffer') const CID = require('cids') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((cid, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } +module.exports = configure(({ ky }) => { + return async (cid, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`) + let res try { - cid = new CID(cid) + res = await ky.post('object/stat', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() } catch (err) { - return callback(err) + if (err.name === 'TimeoutError') { + err.message = `failed to get block for ${Buffer.isBuffer(cid) ? new CID(cid) : cid}: context deadline exceeded` + } + throw err } - send({ - path: 'object/stat', - args: cid.toString(), - qs: opts - }, callback) - }) -} + return res + } +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 9ae23462d..a4a6dcd07 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -102,6 +102,7 @@ function requireCommands (send, config) { lsReadableStream: streamify.readable(ls), lsPullStream: pullify.source(ls), _lsAsyncIterator: ls, + object: require('../object')(config), refs: callbackify.variadic((path, options) => collectify(refs)(path, options)), refsReadableStream: streamify.readable(refs), refsPullStream: pullify.source(refs), @@ -126,9 +127,6 @@ function requireCommands (send, config) { }) const subCmds = { - // Graph - object: require('../object'), - // Network name: require('../name'), ping: require('../ping'),