Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

feat: support adding async iterators #2379

Merged
merged 2 commits into from
Sep 9, 2019
Merged

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Aug 21, 2019

Adds a ipfs._addAsyncIterator method for adding async iterators and refactors all add methods to call this, as when the Great Async Iterator Migration is complete this will become the one true method to add files to IPFS.

Purposefully does not add symmetrical ipfs._catAsyncIterator methods as this PR is big enough already.

Also fixes up the jsipfs add progress indicator so it works more like the go-ipfs one and prints out hashes as it goes instead of buffering them all up in one go, at the cost of not sorting the outout.

Also, also allows you to control preload behaviour from the command line.

Depends on:

@achingbrain achingbrain added the status/in-progress In progress label Aug 21, 2019
const shouldPin = pin && isRootDir && !opts.onlyHash && !opts.hashAlg

if (shouldPin) {
await ipfs.pin.add(file.hash, { preload: false })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably do this asynchronously as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably needs to be done in the gc read lock for add though

src/cli/commands/add.js Outdated Show resolved Hide resolved
yield {
path: file.path === undefined ? b58Hash : (file.path || ''),
hash: b58Hash,
size: file.unixfs.fileSize()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be different to when you don't specify onlyHash as the node has been serialized so includes the protobuf overhead. Typically we only show the user the hash when they've said onlyHash so I don't know how much of a problem this is. I think we want to switch this over to only show file sizes eventually anyway so it might not be a big deal.

package.json Outdated Show resolved Hide resolved

const retStream = new AddHelper(s, p)
const output = new AddHelper(stream, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it-to-stream or stream-to-it help here to avoid buffering and respect backpressure?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout, I'll pull those in.

const iterator = pipe(
source,
validateInput(),
normalizeInput(opts),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a comprehensive normalise in https://github.com/ipfs/js-ipfs-http-client/pull/1063/files#diff-7cec0363d7e0ecfb1f2d5e04b55e2cac and https://github.com/ipfs/js-ipfs-http-client/pull/1063/files#diff-8913549ebf1a95e3151ad16513fa9a6f - it would be good to check this is supporting the same inputs (or come to an agreement on what inputs we support).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, maybe we could move that into ipfs-utils?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will do 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to have a go at moving those utility functions over to ipfs-utils so they can be shared between the http client & core as it's going to simplify this a little.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to refactor some of it though, because it's full of Buffer.from which involves a buffer copy which is slow.

@achingbrain
Copy link
Member Author

achingbrain commented Aug 22, 2019

I might actually break this up into three PRs - one for ipfs.addAsyncIterator, one for parameterising preload and one for fixing the progress bar. Should make it easier to digest.

@achingbrain
Copy link
Member Author

See #2384 and #2386. I'll rebase this once those are in.

alanshaw pushed a commit that referenced this pull request Aug 27, 2019
Pulled out of #2379.

BREAKING CHANGE: Order of output from the CLI command `ipfs add` may change between invocations given the same files since it is not longer buffered and sorted.

Previously, js-ipfs buffered all hashes of added files and sorted them before outputting to the terminal, this might be because the `glob` module does not return stable results.  This gives a poor user experience as you see nothing then everything, compared to `go-ipfs` which prints out the hashes as they become available.  The tradeoff is the order might be slightly different between invocations, though the hashes are always the same as we sort DAGNode links before serializing so it doesn't matter what order you import them in.
@alanshaw
Copy link
Member

@achingbrain those PRs are now merged 😇

@alanshaw alanshaw mentioned this pull request Aug 27, 2019
66 tasks
@achingbrain achingbrain force-pushed the add-async-iterators branch 2 times, most recently from 0dfd370 to dd4fd43 Compare August 27, 2019 14:49
Copy link
Member

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove glob from dependencies?

Copy link
Member

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, pending dependency updates and CI passing

@achingbrain
Copy link
Member Author

Can we remove glob from dependencies?

Not yet - it's still used in src/cli/commands/commands.js and src/core/components/init-assets.js.

lidel added a commit to lidel/js-ipfs that referenced this pull request Sep 5, 2019
Some ipfs.add() tests need to be skipped until
ipfs#2379 is merged

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
lidel added a commit to lidel/js-ipfs that referenced this pull request Sep 5, 2019
Some ipfs.add() tests need to be skipped until
ipfs#2379 is merged

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
@achingbrain achingbrain force-pushed the add-async-iterators branch 3 times, most recently from 6d990a7 to 687d8d0 Compare September 6, 2019 16:28
@achingbrain achingbrain force-pushed the add-async-iterators branch 4 times, most recently from 376ae40 to e0f4e9b Compare September 6, 2019 17:59
@achingbrain
Copy link
Member Author

There are a few addFromUrl interface tests new in v0.113.0 failing on Windows but they will be fixed by ipfs/js-datastore-fs#27

Adds a `ipfs._addAsyncIterator` method for adding async iterators and
refactors all add methods to call this, as when the Great Async Iteator
Migration is complete this will become the one, true method to add
files to IPFS.
@achingbrain
Copy link
Member Author

fireworks

@achingbrain achingbrain merged commit 3878f0f into master Sep 9, 2019
@achingbrain achingbrain deleted the add-async-iterators branch September 9, 2019 09:45
lidel added a commit to ipfs/ipfs-companion that referenced this pull request Sep 12, 2019
This switch to js-ipfs before PR-2379

ipfs/js-ipfs#2379 switched ipfs.add
to ipfs._addAsyncIterator and it broke /api/v0/add exposed by embedded
js-ipfs in Brave.

It seems old polyfills are no longer enough. Real fix requires more time
to investigate, so for now we switch to version before js-ipfs/PR-2379.

Used js-ipfs commit is from ipfs/js-ipfs#2304 before it
was rebased on top of master after PR-2379, making it the last safe
version.

Real fix will be tracked in
#757
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants