blob: cd011b73909407de425474caeabf226583dd3f4b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
'use strict'
var parseURI = require('parse-uri')
var encode = require('punycode2/encode')
// Illegal characters (anything which is not in between the square brackets):
var ILLEGALS = /[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i
// Incomplete HEX escapes:
var HEX1 = /%[^0-9a-f]/i
var HEX2 = /%[0-9a-f](:?[^0-9a-f]|$)/i
// Scheme must begin with a letter, then consist of letters, digits, '+', '.', or '-' => e.g., 'http', 'https', 'ftp'
var PROTOCOL = /^[a-z][a-z0-9\+\-\.]*$/
// If authority is not present, path must not begin with '//'
var PATH = /^\/\//
module.exports = function isURI (uri, opts) {
if (!uri) return false
if (typeof uri !== 'object') {
uri = encode(uri)
if (ILLEGALS.test(uri)) return false
if (HEX1.test(uri) || HEX2.test(uri)) return false
uri = parseURI(uri, opts)
}
if (!uri.protocol || !PROTOCOL.test(uri.protocol.toLowerCase())) return false
if (!uri.authority && PATH.test(uri.path)) return false
return true
}
|