diff --git a/src/files/url-source.js b/src/files/url-source.js index a779aeb..2880c7d 100644 --- a/src/files/url-source.js +++ b/src/files/url-source.js @@ -1,22 +1,34 @@ 'use strict' const HTTP = require('../http') + /** + * * @param {string} url * @param {import("../types").HTTPOptions} [options] - * @returns {AsyncIterable<{ - path: string; - content?: AsyncIterable; -}>} + * @returns {{ + * path: string; + * content?: AsyncIterable; + * }} */ -async function * urlSource (url, options) { +const urlSource = (url, options) => { + return { + path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''), + content: readURLContent(url, options) + } +} + +/** + * + * @param {string} url + * @param {import("../types").HTTPOptions} [options] + * @returns {AsyncIterable} + */ +async function * readURLContent (url, options) { const http = new HTTP() const response = await http.get(url, options) - yield { - path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''), - content: response.iterator() - } + yield * response.iterator() } module.exports = urlSource diff --git a/test/files/url-source.spec.js b/test/files/url-source.spec.js index e20aea9..79a3635 100644 --- a/test/files/url-source.spec.js +++ b/test/files/url-source.spec.js @@ -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') if (file && file.content) { await expect(all(file.content)).to.eventually.deep.equal([Buffer.from(content)])