Skip to content

Commit 614746b

Browse files
author
abluchet
committed
fix #1
1 parent 4e168eb commit 614746b

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var axios = require('axios')
22
var randomAccess = require('random-access-storage')
33
var logger = require('./lib/logger')
44
var isNode = require('./lib/is-node')
5-
var validUrl = require('./lib/valid-url')
5+
var {validUrl, prependUrlProtocol} = require('./lib/url')
66

77
var defaultOptions = {
88
responseType: 'arraybuffer',
@@ -12,10 +12,15 @@ var defaultOptions = {
1212
}
1313

1414
var randomAccessHttp = function (filename, options) {
15-
var url = options && options.url
15+
if (!options) options = {}
16+
17+
var url = prependUrlProtocol(options.url)
18+
19+
if (!url) filename = prependUrlProtocol(filename)
1620
if (!filename || (!validUrl(filename) && !validUrl(url))) {
1721
throw new Error('Expect first argument to be a valid URL or a relative path, with url set in options')
1822
}
23+
1924
var axiosConfig = Object.assign({}, defaultOptions)
2025
if (isNode) {
2126
var http = require('http')

lib/url.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var url = require('url')
2+
3+
module.exports.validUrl = function (str) {
4+
if (typeof str !== 'string') return false
5+
var parsed = url.parse(str)
6+
return ~['http:', 'https:'].indexOf(parsed.protocol)
7+
}
8+
9+
module.exports.prependUrlProtocol = function (str) {
10+
if (typeof str !== 'string') return false
11+
var parsed = url.parse(str)
12+
13+
if (parsed.protocol === null) {
14+
parsed.protocol = 'http:'
15+
var parts = parsed.href.split('/')
16+
parsed.slashes = true
17+
parsed.hostname = parsed.host = parts[0]
18+
parsed.pathname = parsed.path = parts.slice(1).join('/')
19+
}
20+
21+
return url.format(parsed)
22+
}

lib/valid-url.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/valid-url.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
var test = require('tape')
2-
var validUrl = require('../lib/valid-url')
2+
var {validUrl, prependUrlProtocol} = require('../lib/url')
3+
4+
test('prependUrlProtocol assumes http without path', (t) => {
5+
t.same(prependUrlProtocol('example.com'), 'http://example.com')
6+
t.end()
7+
})
8+
9+
test('prependUrlProtocol assumes http with path', (t) => {
10+
t.same(prependUrlProtocol('example.com/foo/bar.html'), 'http://example.com/foo/bar.html')
11+
t.end()
12+
})
13+
14+
test('prependUrlProtocol doesn\'t change protocol if given', (t) => {
15+
t.same(prependUrlProtocol('https://example.com/foo/bar.html'), 'https://example.com/foo/bar.html')
16+
t.same(prependUrlProtocol('ftp://example.com/foo/bar.html'), 'ftp://example.com/foo/bar.html')
17+
t.end()
18+
})
319

420
test('validUrl returns false if url is not a string', (t) => {
521
t.notOk(validUrl())

0 commit comments

Comments
 (0)