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 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert files cp, flush, ls, mkdir and mv to async/await
- Loading branch information
Alan Shaw
committed
Nov 18, 2019
1 parent
3a7de05
commit b7b4a91
Showing
10 changed files
with
144 additions
and
170 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,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() | ||
} | ||
}) |
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,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) | ||
} | ||
}) |
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 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,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) | ||
}) | ||
} | ||
} | ||
}) |
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() | ||
} | ||
}) |
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,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() | ||
} | ||
}) |
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 |
---|---|---|
@@ -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 | ||
} | ||
} |