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

feat: make urlSource compatible with new ipfs.add #53

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions src/files/url-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const Http = require('../http')

module.exports = async function * urlSource (url, options) {
const http = new Http()
const response = await http.get(url, options)

yield {
const urlSource = (url, options) => {
return {
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
content: response.iterator()
content: readURLContent(url, options)
}
}

const readURLContent = async function * (url, options) {
const http = new Http()
const response = await http.get(url, options)
yield * response.iterator()
}
Copy link
Member

Choose a reason for hiding this comment

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

i dont understand this change, why the wrapper ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hugomrdias so previously we had ipfs.add that could imported bunch of files or import a single one. Which later we have split into ipfs.add that imports a single file and takes single file like thing, and ipfs.addAll which imports multilpe files.

This change just makes return of urlSource compatible with ipfs.add as opposed to ipfs.addAll.

P.S.: ipfs.add implementation (but not the typings) will still accept many files, but plan is to make that not a case in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To answer your specific question, this does not add a wrapper but rather moves code doing the content fetching into content field which is expected to be AsyncIterable<Uint8Array>. effectively changing return type of this urlSource from AsyncIterable<{ path: string, content: AsyncIterable<Uint8Array> }> to { path: string, content: AsyncIterable<Uint8Array> } removing one layer of asynchronicity and making return be a single file as opposed to collection of files that always happen to contain one.


module.exports = urlSource
5 changes: 3 additions & 2 deletions test/files/url-source.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
const { expect } = require('aegir/utils/chai')
const all = require('it-all')
const urlSource = require('../../src/files/url-source')
const last = require('it-last')
const { Buffer } = require('buffer')

describe('url-source', function () {
it('can get url content', async function () {
const content = 'foo'
const file = await last(urlSource(`${process.env.ECHO_SERVER}/download?data=${content}`))
const file = urlSource(`${process.env.ECHO_SERVER}/download?data=${content}`)

expect(file).to.have.property('path', 'download')

await expect(all(file.content)).to.eventually.deep.equal([Buffer.from(content)])
})
Expand Down