File tree Expand file tree Collapse file tree 4 files changed +46
-10
lines changed Expand file tree Collapse file tree 4 files changed +46
-10
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ var axios = require('axios')
2
2
var randomAccess = require ( 'random-access-storage' )
3
3
var logger = require ( './lib/logger' )
4
4
var isNode = require ( './lib/is-node' )
5
- var validUrl = require ( './lib/valid- url' )
5
+ var { validUrl, prependUrlProtocol } = require ( './lib/url' )
6
6
7
7
var defaultOptions = {
8
8
responseType : 'arraybuffer' ,
@@ -12,10 +12,15 @@ var defaultOptions = {
12
12
}
13
13
14
14
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 )
16
20
if ( ! filename || ( ! validUrl ( filename ) && ! validUrl ( url ) ) ) {
17
21
throw new Error ( 'Expect first argument to be a valid URL or a relative path, with url set in options' )
18
22
}
23
+
19
24
var axiosConfig = Object . assign ( { } , defaultOptions )
20
25
if ( isNode ) {
21
26
var http = require ( 'http' )
Original file line number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
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
+ } )
3
19
4
20
test ( 'validUrl returns false if url is not a string' , ( t ) => {
5
21
t . notOk ( validUrl ( ) )
You can’t perform that action at this time.
0 commit comments