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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement streaming mfs.read methods
License: MIT Signed-off-by: achingbrain <alex@achingbrain.net>
- Loading branch information
1 parent
ac213ee
commit 3e5620b
Showing
6 changed files
with
199 additions
and
102 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
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,54 @@ | ||
'use strict' | ||
|
||
const exporter = require('ipfs-unixfs-engine').exporter | ||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream/pull') | ||
const collect = require('pull-stream/sinks/collect') | ||
const waterfall = require('async/waterfall') | ||
const { | ||
validatePath, | ||
traverseTo | ||
} = require('./utils') | ||
const log = require('debug')('mfs:read-pull-stream') | ||
|
||
const defaultOptions = { | ||
offset: 0, | ||
length: undefined | ||
} | ||
|
||
module.exports = function mfsReadPullStream (ipfs) { | ||
return promisify((path, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
options = Object.assign({}, defaultOptions, options) | ||
|
||
try { | ||
path = validatePath(path) | ||
} catch (error) { | ||
return callback(error) | ||
} | ||
|
||
log(`Reading ${path}`) | ||
|
||
waterfall([ | ||
(done) => traverseTo(ipfs, path, { | ||
parents: false | ||
}, done), | ||
(result, done) => { | ||
waterfall([ | ||
(next) => pull( | ||
exporter(result.node.multihash, ipfs._ipld, { | ||
offset: options.offset, | ||
length: options.length | ||
}), | ||
collect(next) | ||
), | ||
(files, next) => next(null, files[0].content) | ||
], done) | ||
} | ||
], 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const waterfall = require('async/waterfall') | ||
const readPullStream = require('./read-pull-stream') | ||
const toStream = require('pull-stream-to-stream') | ||
|
||
module.exports = function mfsRead (ipfs) { | ||
return promisify((path, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
waterfall([ | ||
(cb) => readPullStream(ipfs)(path, options, cb), | ||
(stream, cb) => cb(null, toStream.source(stream)) | ||
], 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
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