This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
chore: converts remaining file api methods to async iterators #2517
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
494235d
chore: converts remaining file api methods to async iterators
achingbrain 2dea4c9
chore: wip
achingbrain 2d4c571
chore: fix linting
achingbrain d9986ee
chore: start stream flowing in test
achingbrain 06c42aa
Merge remote-tracking branch 'origin/master' into add-remaining-file-…
achingbrain 3ea666f
chore: update deps
achingbrain b6505c1
fix: fix up linting
achingbrain 5e112b8
fix: expose .all on returned commands
achingbrain 9152f6d
refactor: apply suggestions from code review
achingbrain cc5c293
Merge branch 'master' into add-remaining-file-async-iterator-methods
achingbrain 0878ef2
chore: fix linting
achingbrain d912b8d
chore: rev http client version
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
|
||
const exporter = require('ipfs-unixfs-exporter') | ||
const { normalizePath } = require('./utils') | ||
|
||
module.exports = function (self) { | ||
return async function * catAsyncIterator (ipfsPath, options) { | ||
options = options || {} | ||
|
||
ipfsPath = normalizePath(ipfsPath) | ||
|
||
if (options.preload !== false) { | ||
const pathComponents = ipfsPath.split('/') | ||
self._preload(pathComponents[0]) | ||
} | ||
|
||
const file = await exporter(ipfsPath, self._ipld, options) | ||
|
||
// File may not have unixfs prop if small & imported with rawLeaves true | ||
if (file.unixfs && file.unixfs.type.includes('dir')) { | ||
throw new Error('this dag node is a directory') | ||
} | ||
|
||
if (!file.content) { | ||
throw new Error('this dag node has no content') | ||
} | ||
|
||
yield * file.content(options) | ||
} | ||
} |
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,43 +1,9 @@ | ||
'use strict' | ||
|
||
const exporter = require('ipfs-unixfs-exporter') | ||
const deferred = require('pull-defer') | ||
const toPullStream = require('async-iterator-to-pull-stream') | ||
const { normalizePath } = require('./utils') | ||
|
||
module.exports = function (self) { | ||
return function catPullStream (ipfsPath, options) { | ||
if (typeof ipfsPath === 'function') { | ||
throw new Error('You must supply an ipfsPath') | ||
} | ||
|
||
options = options || {} | ||
|
||
ipfsPath = normalizePath(ipfsPath) | ||
const pathComponents = ipfsPath.split('/') | ||
|
||
if (options.preload !== false) { | ||
self._preload(pathComponents[0]) | ||
} | ||
|
||
const d = deferred.source() | ||
|
||
exporter(ipfsPath, self._ipld, options) | ||
.then(file => { | ||
// File may not have unixfs prop if small & imported with rawLeaves true | ||
if (file.unixfs && file.unixfs.type.includes('dir')) { | ||
return d.abort(new Error('this dag node is a directory')) | ||
} | ||
|
||
if (!file.content) { | ||
return d.abort(new Error('this dag node has no content')) | ||
} | ||
|
||
d.resolve(toPullStream.source(file.content(options))) | ||
}, err => { | ||
d.abort(err) | ||
}) | ||
|
||
return d | ||
return toPullStream.source(self._catAsyncIterator(ipfsPath, options)) | ||
} | ||
} |
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,7 +1,11 @@ | ||
'use strict' | ||
|
||
const toStream = require('pull-stream-to-stream') | ||
const toStream = require('it-to-stream') | ||
|
||
module.exports = function (self) { | ||
return (ipfsPath, options) => toStream.source(self.catPullStream(ipfsPath, options)) | ||
return function catReadableStream (ipfsPath, options) { | ||
return toStream.readable(self._catAsyncIterator(ipfsPath, options), { | ||
objectMode: true | ||
}) | ||
} | ||
} |
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,21 +1,10 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream') | ||
const callbackify = require('callbackify') | ||
const all = require('async-iterator-all') | ||
|
||
module.exports = function (self) { | ||
return promisify((ipfsPath, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
pull( | ||
self.catPullStream(ipfsPath, options), | ||
pull.collect((err, buffers) => { | ||
if (err) { return callback(err) } | ||
callback(null, Buffer.concat(buffers)) | ||
}) | ||
) | ||
return callbackify.variadic(async function cat (ipfsPath, options) { | ||
return Buffer.concat(await all(self._catAsyncIterator(ipfsPath, options))) | ||
}) | ||
} |
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,30 @@ | ||
'use strict' | ||
|
||
const exporter = require('ipfs-unixfs-exporter') | ||
const errCode = require('err-code') | ||
const { normalizePath, mapFile } = require('./utils') | ||
|
||
module.exports = function (self) { | ||
return async function * getAsyncIterator (ipfsPath, options) { | ||
options = options || {} | ||
|
||
if (options.preload !== false) { | ||
let pathComponents | ||
|
||
try { | ||
pathComponents = normalizePath(ipfsPath).split('/') | ||
} catch (err) { | ||
throw errCode(err, 'ERR_INVALID_PATH') | ||
} | ||
|
||
self._preload(pathComponents[0]) | ||
} | ||
|
||
for await (const file of exporter.recursive(ipfsPath, self._ipld, options)) { | ||
yield mapFile(file, { | ||
...options, | ||
includeContent: true | ||
}) | ||
} | ||
} | ||
} |
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,35 +1,20 @@ | ||
'use strict' | ||
|
||
const exporter = require('ipfs-unixfs-exporter') | ||
const toPullStream = require('async-iterator-to-pull-stream') | ||
const errCode = require('err-code') | ||
const pull = require('pull-stream/pull') | ||
const pullError = require('pull-stream/sources/error') | ||
const map = require('pull-stream/throughs/map') | ||
const { normalizePath, mapFile } = require('./utils') | ||
|
||
module.exports = function (self) { | ||
return (ipfsPath, options) => { | ||
options = options || {} | ||
|
||
if (options.preload !== false) { | ||
let pathComponents | ||
|
||
try { | ||
pathComponents = normalizePath(ipfsPath).split('/') | ||
} catch (err) { | ||
return pullError(errCode(err, 'ERR_INVALID_PATH')) | ||
} | ||
|
||
self._preload(pathComponents[0]) | ||
} | ||
|
||
return function getPullStream (ipfsPath, options) { | ||
return pull( | ||
toPullStream.source(exporter.recursive(ipfsPath, self._ipld, options)), | ||
map(mapFile({ | ||
...options, | ||
includeContent: true | ||
})) | ||
toPullStream.source(self._getAsyncIterator(ipfsPath, options)), | ||
map(file => { | ||
if (file.content) { | ||
file.content = toPullStream.source(file.content()) | ||
} | ||
|
||
return file | ||
}) | ||
) | ||
} | ||
} |
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,24 +1,19 @@ | ||
'use strict' | ||
|
||
const pull = require('pull-stream') | ||
const toStream = require('pull-stream-to-stream') | ||
const toStream = require('it-to-stream') | ||
|
||
module.exports = function (self) { | ||
return (ipfsPath, options) => { | ||
options = options || {} | ||
return function getReadableStream (ipfsPath, options) { | ||
return toStream.readable((async function * mapStreamFileContents () { | ||
for await (const file of self._getAsyncIterator(ipfsPath, options)) { | ||
if (file.content) { | ||
file.content = toStream.readable(file.content()) | ||
} | ||
|
||
return toStream.source( | ||
pull( | ||
self.getPullStream(ipfsPath, options), | ||
pull.map((file) => { | ||
if (file.content) { | ||
file.content = toStream.source(file.content) | ||
file.content.pause() | ||
} | ||
|
||
return file | ||
}) | ||
) | ||
) | ||
yield file | ||
} | ||
})(), { | ||
objectMode: true | ||
}) | ||
} | ||
} |
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,34 +1,18 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream') | ||
const callbackify = require('callbackify') | ||
const all = require('async-iterator-all') | ||
|
||
module.exports = function (self) { | ||
return promisify((ipfsPath, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
options = options || {} | ||
|
||
pull( | ||
self.getPullStream(ipfsPath, options), | ||
pull.asyncMap((file, cb) => { | ||
return callbackify.variadic(async function get (ipfsPath, options) { // eslint-disable-line require-await | ||
return all(async function * () { | ||
for await (const file of self._getAsyncIterator(ipfsPath, options)) { | ||
if (file.content) { | ||
pull( | ||
file.content, | ||
pull.collect((err, buffers) => { | ||
if (err) { return cb(err) } | ||
file.content = Buffer.concat(buffers) | ||
cb(null, file) | ||
}) | ||
) | ||
} else { | ||
cb(null, file) | ||
file.content = Buffer.concat(await all(file.content())) | ||
} | ||
}), | ||
pull.collect(callback) | ||
) | ||
|
||
yield file | ||
} | ||
}()) | ||
}) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why move from arrow func to named func here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To have function names in any stack traces emitted making them a teensy bit easier to read.