Skip to content

Commit 2b3ebbe

Browse files
Gozalaachingbrain
andauthored
feat: make urlSource compatible with new ipfs.add (#53)
Changes the 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 file. fixes: ipfs/js-ipfs#3195 Co-authored-by: achingbrain <[email protected]>
1 parent 707174c commit 2b3ebbe

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/files/url-source.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
'use strict'
22

33
const HTTP = require('../http')
4+
45
/**
6+
*
57
* @param {string} url
68
* @param {import("../types").HTTPOptions} [options]
7-
* @returns {AsyncIterable<{
8-
path: string;
9-
content?: AsyncIterable<Uint8Array>;
10-
}>}
9+
* @returns {{
10+
* path: string;
11+
* content?: AsyncIterable<Uint8Array>;
12+
* }}
1113
*/
12-
async function * urlSource (url, options) {
14+
const urlSource = (url, options) => {
15+
return {
16+
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
17+
content: readURLContent(url, options)
18+
}
19+
}
20+
21+
/**
22+
*
23+
* @param {string} url
24+
* @param {import("../types").HTTPOptions} [options]
25+
* @returns {AsyncIterable<Uint8Array>}
26+
*/
27+
async function * readURLContent (url, options) {
1328
const http = new HTTP()
1429
const response = await http.get(url, options)
1530

16-
yield {
17-
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
18-
content: response.iterator()
19-
}
31+
yield * response.iterator()
2032
}
2133

2234
module.exports = urlSource

test/files/url-source.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
const { expect } = require('aegir/utils/chai')
66
const all = require('it-all')
77
const urlSource = require('../../src/files/url-source')
8-
const last = require('it-last')
98
const { Buffer } = require('buffer')
109

1110
describe('url-source', function () {
1211
it('can get url content', async function () {
1312
const content = 'foo'
14-
const file = await last(urlSource(`${process.env.ECHO_SERVER}/download?data=${content}`))
13+
const file = urlSource(`${process.env.ECHO_SERVER}/download?data=${content}`)
14+
15+
expect(file).to.have.property('path', 'download')
1516

1617
if (file && file.content) {
1718
await expect(all(file.content)).to.eventually.deep.equal([Buffer.from(content)])

0 commit comments

Comments
 (0)