From c601b0ecc48b86b172898fbb63d99941bb77e017 Mon Sep 17 00:00:00 2001 From: fent <933490+fent@users.noreply.github.com> Date: Sun, 24 Jan 2021 14:15:27 -0700 Subject: [PATCH] refactor: use URL object to parse urls --- src/index.ts | 14 ++++++++++++-- test/request-test.ts | 13 ++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 432ba94..b8180ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { RequestOptions, IncomingMessage, ClientRequest, default as http } from 'http'; import { EventEmitter } from 'events'; import https from 'https'; -import { parse as urlParse } from 'url'; +import { URL } from 'url'; import { PassThrough, Transform } from 'stream'; @@ -148,7 +148,17 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream { const doDownload = () => { let parsed: RequestOptions = {}, httpLib; try { - parsed = urlParse(url); + let urlObj = new URL(url); + parsed = Object.assign({}, { + host: urlObj.host, + hostname: urlObj.hostname, + path: urlObj.pathname + urlObj.search + urlObj.hash, + port: urlObj.port, + protocol: urlObj.protocol, + }); + if (urlObj.username) { + parsed.auth = `${urlObj.username}:${urlObj.password}`; + } httpLib = httpLibs[parsed.protocol]; } catch (err) { // Let the error be caught by the if statement below. diff --git a/test/request-test.ts b/test/request-test.ts index eb8a5d3..1785ad9 100644 --- a/test/request-test.ts +++ b/test/request-test.ts @@ -144,6 +144,17 @@ describe('Make a request', () => { }); }); + describe('with auth', () => { + it('Passes auth to request', async() => { + const scope = nock('https://lockbox.com') + .get('/vault') + .basicAuth({ user: 'john', pass: 'pass' }) + .reply(200); + await miniget('https://john:pass@lockbox.com/vault'); + scope.done(); + }); + }); + describe('with an incorrect URL', () => { it('Emits error', done => { miniget('file:///path/to/file/').on('error', err => { @@ -878,7 +889,7 @@ describe('Make a request', () => { }); }); -describe('Importing the module', () => { +describe('Import the module', () => { it('Exposes default options', () => { assert.ok(miniget.defaultOptions); });