This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert object.* and files.* API methods to async/await (#1160
- Loading branch information
Alan Shaw
authored
Nov 20, 2019
1 parent
8b48d57
commit fc73da7
Showing
35 changed files
with
495 additions
and
738 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const findSources = require('../utils/find-sources') | ||
const CID = require('cids') | ||
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', 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) | ||
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() | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
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) | ||
|
||
await ky.post('files/flush', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}).text() | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
'use strict' | ||
|
||
const moduleConfig = require('../utils/module-config') | ||
const callbackify = require('callbackify') | ||
const { collectify, streamify, pullify, concatify } = require('../lib/converters') | ||
|
||
module.exports = (arg) => { | ||
const send = moduleConfig(arg) | ||
module.exports = config => { | ||
const ls = require('./ls')(config) | ||
const read = require('./read')(config) | ||
|
||
return { | ||
cp: require('./cp')(send), | ||
mkdir: require('./mkdir')(send), | ||
flush: require('./flush')(send), | ||
stat: require('./stat')(send), | ||
rm: require('./rm')(send), | ||
ls: require('./ls')(send), | ||
lsReadableStream: require('./ls-readable-stream')(send), | ||
lsPullStream: require('./ls-pull-stream')(send), | ||
read: require('./read')(send), | ||
readReadableStream: require('./read-readable-stream')(send), | ||
readPullStream: require('./read-pull-stream')(send), | ||
write: require('./write')(send), | ||
mv: require('./mv')(send) | ||
cp: callbackify.variadic(require('./cp')(config)), | ||
mkdir: callbackify.variadic(require('./mkdir')(config)), | ||
flush: callbackify.variadic(require('./flush')(config)), | ||
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: callbackify.variadic(concatify(read)), | ||
readReadableStream: streamify.readable(read), | ||
readPullStream: pullify.source(read), | ||
write: callbackify.variadic(require('./write')(config)), | ||
mv: callbackify.variadic(require('./mv')(config)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,42 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
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 || [] | ||
|
||
callback(null, entries.map((entry) => { | ||
return { | ||
name: entry.Name, | ||
type: entry.Type, | ||
size: entry.Size, | ||
hash: entry.Hash | ||
module.exports = configure(({ ky }) => { | ||
return async function * ls (path, options) { | ||
if (typeof path !== 'string') { | ||
options = path | ||
path = '/' | ||
} | ||
})) | ||
} | ||
|
||
module.exports = (send) => { | ||
return promisify((args, opts, callback) => { | ||
if (typeof (opts) === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
options = options || {} | ||
|
||
if (typeof (args) === 'function') { | ||
callback = args | ||
opts = {} | ||
args = null | ||
} | ||
const searchParams = new URLSearchParams(options.searchParams) | ||
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) | ||
|
||
return send.andTransform({ | ||
path: 'files/ls', | ||
args: args, | ||
qs: opts | ||
}, transform, callback) | ||
}) | ||
} | ||
const res = await ky.post('files/ls', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}) | ||
|
||
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) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
}) |
Oops, something went wrong.