Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally wait in readdir() and list() #377

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,22 @@ Returns a stream of all entries in the drive at paths prefixed with `folder`.
```js
{
recursive: true | false // Whether to descend into all subfolders or not,
ignore: String || Array // Ignore files and folders by name.
ignore: String || Array // Ignore files and folders by name,
wait: true, // Wait for block to be downloaded.
}
```

#### `const stream = drive.readdir(folder)`
#### `const stream = drive.readdir(folder, [options])`

Returns a stream of all subpaths of entries in drive stored at paths prefixed by `folder`.

`options` include:
```js
{
wait: true, // Wait for block to be downloaded
}
```

#### `const stream = await drive.entries([range], [options])`

Returns a read stream of entries in the drive.
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,13 @@ module.exports = class Hyperdrive extends ReadyResource {
folder = std(folder || '/', true)

const ignore = opts.ignore ? normalizeIgnore(opts.ignore) : null
const stream = opts && opts.recursive === false ? shallowReadStream(this.db, folder, false, ignore) : this.entries(prefixRange(folder), { ignore })
const stream = opts && opts.recursive === false ? shallowReadStream(this.db, folder, false, ignore, opts) : this.entries(prefixRange(folder), { ...opts, ignore })
return stream
}

readdir (folder) {
readdir (folder, opts) {
folder = std(folder || '/', true)
return shallowReadStream(this.db, folder, true, null)
return shallowReadStream(this.db, folder, true, null, opts)
}

mirror (out, opts) {
Expand Down Expand Up @@ -622,7 +622,7 @@ module.exports = class Hyperdrive extends ReadyResource {
}
}

function shallowReadStream (files, folder, keys, ignore) {
function shallowReadStream (files, folder, keys, ignore, opts) {
let prev = '/'
let prevName = ''

Expand All @@ -631,7 +631,7 @@ function shallowReadStream (files, folder, keys, ignore) {
let node = null

try {
node = await files.peek(prefixRange(folder, prev), { keyEncoding })
node = await files.peek(prefixRange(folder, prev), { ...opts, keyEncoding })
} catch (err) {
return cb(err)
}
Expand Down
Loading