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')
22var randomAccess = require ( 'random-access-storage' )
33var logger = require ( './lib/logger' )
44var isNode = require ( './lib/is-node' )
5- var validUrl = require ( './lib/valid- url' )
5+ var { validUrl, prependUrlProtocol } = require ( './lib/url' )
66
77var defaultOptions = {
88 responseType : 'arraybuffer' ,
@@ -12,10 +12,15 @@ var defaultOptions = {
1212}
1313
1414var 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' )
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 11var 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
420test ( 'validUrl returns false if url is not a string' , ( t ) => {
521 t . notOk ( validUrl ( ) )
You can’t perform that action at this time.
0 commit comments