Skip to content
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
30 changes: 21 additions & 9 deletions src/files/url-source.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
'use strict'

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

/**
*
* @param {string} url
* @param {import("../types").HTTPOptions} [options]
* @returns {AsyncIterable<{
path: string;
content?: AsyncIterable<Uint8Array>;
}>}
* @returns {{
* path: string;
* content?: AsyncIterable<Uint8Array>;
* }}
*/
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<Uint8Array>}
*/
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
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')

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