Skip to content

Commit

Permalink
refactor: use URL object to parse urls
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Jan 24, 2021
1 parent 726f31e commit c601b0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';


Expand Down Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion test/request-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -878,7 +889,7 @@ describe('Make a request', () => {
});
});

describe('Importing the module', () => {
describe('Import the module', () => {
it('Exposes default options', () => {
assert.ok(miniget.defaultOptions);
});
Expand Down

0 comments on commit c601b0e

Please sign in to comment.