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
feat: add from fs #1777
Merged
Merged
feat: add from fs #1777
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4ef3fa8
feat: wip `addFromFs` method
a154cb5
feat: add `addFromFs` method
59a3fab
docs: added jsdocs for options for glob-source.js
976497b
chore: appease linter
db3c183
refactor: use typeof instead of lodash/isString
384a9e6
fix: path is always posix
158d37d
fix: fixes for windows
4258046
fix: remove unnecessary realpath call
43fc573
fix: do not rely on external has being available
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict' | ||
|
||
module.exports = (self) => require('../../runtime/add-from-fs-nodejs')(self) |
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,10 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
|
||
module.exports = self => { | ||
return promisify((...args) => { | ||
const callback = args.pop() | ||
callback(new Error('not available in the browser')) | ||
}) | ||
} |
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,19 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream') | ||
const globSource = require('../../utils/files/glob-source') | ||
|
||
module.exports = self => { | ||
return promisify((...args) => { | ||
const callback = args.pop() | ||
const options = typeof args[args.length - 1] === 'string' ? {} : args.pop() | ||
const paths = args | ||
|
||
pull( | ||
globSource(...paths, options), | ||
self.addPullStream(options), | ||
pull.collect(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,109 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const Path = require('path') | ||
const pull = require('pull-stream') | ||
const glob = require('glob') | ||
const cat = require('pull-cat') | ||
const defer = require('pull-defer') | ||
const pushable = require('pull-pushable') | ||
const map = require('async/map') | ||
const errCode = require('err-code') | ||
|
||
/** | ||
* Create a pull stream source that can be piped to ipfs.addPullStream for the | ||
* provided file paths. | ||
* | ||
* @param {String} ...paths File system path(s) to glob from | ||
* @param {Object} [options] Optional options | ||
* @param {Boolean} [options.recursive] Recursively glob all paths in directories | ||
* @param {Boolean} [options.hidden] Include .dot files in matched paths | ||
* @param {Array<String>} [options.ignore] Glob paths to ignore | ||
* @param {Boolean} [options.followSymlinks] follow symlinks | ||
* @returns {Function} pull stream source | ||
*/ | ||
module.exports = (...args) => { | ||
const options = typeof args[args.length - 1] === 'string' ? {} : args.pop() | ||
const paths = args | ||
const deferred = defer.source() | ||
|
||
const globSourceOptions = { | ||
recursive: options.recursive, | ||
glob: { | ||
dot: Boolean(options.hidden), | ||
ignore: Array.isArray(options.ignore) ? options.ignore : [], | ||
follow: options.followSymlinks != null ? options.followSymlinks : true | ||
} | ||
} | ||
|
||
// Check the input paths comply with options.recursive and convert to glob sources | ||
map(paths, pathAndType, (err, results) => { | ||
if (err) return deferred.abort(err) | ||
|
||
try { | ||
const sources = results.map(res => toGlobSource(res, globSourceOptions)) | ||
deferred.resolve(cat(sources)) | ||
} catch (err) { | ||
deferred.abort(err) | ||
} | ||
}) | ||
|
||
return pull( | ||
deferred, | ||
pull.map(({ path, contentPath }) => ({ | ||
path, | ||
content: fs.createReadStream(contentPath) | ||
})) | ||
) | ||
} | ||
|
||
function toGlobSource ({ path, type }, options) { | ||
options = options || {} | ||
|
||
const baseName = Path.basename(path) | ||
|
||
if (type === 'file') { | ||
return pull.values([{ path: baseName, contentPath: path }]) | ||
} | ||
|
||
if (type === 'dir' && !options.recursive) { | ||
throw errCode( | ||
new Error(`'${path}' is a directory and recursive option not set`), | ||
'ERR_DIR_NON_RECURSIVE', | ||
{ path } | ||
) | ||
} | ||
|
||
const globOptions = Object.assign({}, options.glob, { | ||
cwd: path, | ||
nodir: true, | ||
realpath: false, | ||
absolute: false | ||
}) | ||
|
||
// TODO: want to use pull-glob but it doesn't have the features... | ||
const pusher = pushable() | ||
|
||
glob('**/*', globOptions) | ||
.on('match', m => pusher.push(m)) | ||
.on('end', () => pusher.end()) | ||
.on('abort', () => pusher.end()) | ||
.on('error', err => pusher.end(err)) | ||
|
||
return pull( | ||
pusher, | ||
pull.map(p => ({ | ||
path: `${baseName}/${toPosix(p)}`, | ||
contentPath: Path.join(path, p) | ||
})) | ||
) | ||
} | ||
|
||
function pathAndType (path, cb) { | ||
fs.stat(path, (err, stat) => { | ||
if (err) return cb(err) | ||
cb(null, { path, type: stat.isDirectory() ? 'dir' : 'file' }) | ||
}) | ||
} | ||
|
||
const toPosix = path => path.replace(/\\/g, '/') |
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.
can't we just use https://www.npmjs.com/package/fast-glob ? it's faster and supports streams so we can go directly to pull-stream
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.
I tried it, and it's not significantly faster than
glob
, in fact in most tests it's slower (left isglob
, right isfast-glob
).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.
even using streams instead of glob events ? seems weird but if its faster like this im good with it
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.
I know weird, maybe it is the stream to pull stream conversion? Anyway even weirder is that the benchmarks for
fast-glob
do not run againstglob
😱 They benchmarkglobby
,bash-glob
,tiny-glob
andglob-stream
...but not regularglob
🙄.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.
lol that kinda wtf and globby is based on fast-glob with some sindre sugar on top
anyway this looks fine and if your benchs show better numbers lets merge it