source: imaps-frontend/node_modules/fast-uri/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 9.3 KB
Line 
1'use strict'
2
3const { normalizeIPv6, normalizeIPv4, removeDotSegments, recomposeAuthority, normalizeComponentEncoding } = require('./lib/utils')
4const SCHEMES = require('./lib/schemes')
5
6function normalize (uri, options) {
7 if (typeof uri === 'string') {
8 uri = serialize(parse(uri, options), options)
9 } else if (typeof uri === 'object') {
10 uri = parse(serialize(uri, options), options)
11 }
12 return uri
13}
14
15function resolve (baseURI, relativeURI, options) {
16 const schemelessOptions = Object.assign({ scheme: 'null' }, options)
17 const resolved = resolveComponents(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true)
18 return serialize(resolved, { ...schemelessOptions, skipEscape: true })
19}
20
21function resolveComponents (base, relative, options, skipNormalization) {
22 const target = {}
23 if (!skipNormalization) {
24 base = parse(serialize(base, options), options) // normalize base components
25 relative = parse(serialize(relative, options), options) // normalize relative components
26 }
27 options = options || {}
28
29 if (!options.tolerant && relative.scheme) {
30 target.scheme = relative.scheme
31 // target.authority = relative.authority;
32 target.userinfo = relative.userinfo
33 target.host = relative.host
34 target.port = relative.port
35 target.path = removeDotSegments(relative.path || '')
36 target.query = relative.query
37 } else {
38 if (relative.userinfo !== undefined || relative.host !== undefined || relative.port !== undefined) {
39 // target.authority = relative.authority;
40 target.userinfo = relative.userinfo
41 target.host = relative.host
42 target.port = relative.port
43 target.path = removeDotSegments(relative.path || '')
44 target.query = relative.query
45 } else {
46 if (!relative.path) {
47 target.path = base.path
48 if (relative.query !== undefined) {
49 target.query = relative.query
50 } else {
51 target.query = base.query
52 }
53 } else {
54 if (relative.path.charAt(0) === '/') {
55 target.path = removeDotSegments(relative.path)
56 } else {
57 if ((base.userinfo !== undefined || base.host !== undefined || base.port !== undefined) && !base.path) {
58 target.path = '/' + relative.path
59 } else if (!base.path) {
60 target.path = relative.path
61 } else {
62 target.path = base.path.slice(0, base.path.lastIndexOf('/') + 1) + relative.path
63 }
64 target.path = removeDotSegments(target.path)
65 }
66 target.query = relative.query
67 }
68 // target.authority = base.authority;
69 target.userinfo = base.userinfo
70 target.host = base.host
71 target.port = base.port
72 }
73 target.scheme = base.scheme
74 }
75
76 target.fragment = relative.fragment
77
78 return target
79}
80
81function equal (uriA, uriB, options) {
82 if (typeof uriA === 'string') {
83 uriA = unescape(uriA)
84 uriA = serialize(normalizeComponentEncoding(parse(uriA, options), true), { ...options, skipEscape: true })
85 } else if (typeof uriA === 'object') {
86 uriA = serialize(normalizeComponentEncoding(uriA, true), { ...options, skipEscape: true })
87 }
88
89 if (typeof uriB === 'string') {
90 uriB = unescape(uriB)
91 uriB = serialize(normalizeComponentEncoding(parse(uriB, options), true), { ...options, skipEscape: true })
92 } else if (typeof uriB === 'object') {
93 uriB = serialize(normalizeComponentEncoding(uriB, true), { ...options, skipEscape: true })
94 }
95
96 return uriA.toLowerCase() === uriB.toLowerCase()
97}
98
99function serialize (cmpts, opts) {
100 const components = {
101 host: cmpts.host,
102 scheme: cmpts.scheme,
103 userinfo: cmpts.userinfo,
104 port: cmpts.port,
105 path: cmpts.path,
106 query: cmpts.query,
107 nid: cmpts.nid,
108 nss: cmpts.nss,
109 uuid: cmpts.uuid,
110 fragment: cmpts.fragment,
111 reference: cmpts.reference,
112 resourceName: cmpts.resourceName,
113 secure: cmpts.secure,
114 error: ''
115 }
116 const options = Object.assign({}, opts)
117 const uriTokens = []
118
119 // find scheme handler
120 const schemeHandler = SCHEMES[(options.scheme || components.scheme || '').toLowerCase()]
121
122 // perform scheme specific serialization
123 if (schemeHandler && schemeHandler.serialize) schemeHandler.serialize(components, options)
124
125 if (components.path !== undefined) {
126 if (!options.skipEscape) {
127 components.path = escape(components.path)
128
129 if (components.scheme !== undefined) {
130 components.path = components.path.split('%3A').join(':')
131 }
132 } else {
133 components.path = unescape(components.path)
134 }
135 }
136
137 if (options.reference !== 'suffix' && components.scheme) {
138 uriTokens.push(components.scheme, ':')
139 }
140
141 const authority = recomposeAuthority(components)
142 if (authority !== undefined) {
143 if (options.reference !== 'suffix') {
144 uriTokens.push('//')
145 }
146
147 uriTokens.push(authority)
148
149 if (components.path && components.path.charAt(0) !== '/') {
150 uriTokens.push('/')
151 }
152 }
153 if (components.path !== undefined) {
154 let s = components.path
155
156 if (!options.absolutePath && (!schemeHandler || !schemeHandler.absolutePath)) {
157 s = removeDotSegments(s)
158 }
159
160 if (authority === undefined) {
161 s = s.replace(/^\/\//u, '/%2F') // don't allow the path to start with "//"
162 }
163
164 uriTokens.push(s)
165 }
166
167 if (components.query !== undefined) {
168 uriTokens.push('?', components.query)
169 }
170
171 if (components.fragment !== undefined) {
172 uriTokens.push('#', components.fragment)
173 }
174 return uriTokens.join('')
175}
176
177const hexLookUp = Array.from({ length: 127 }, (_v, k) => /[^!"$&'()*+,\-.;=_`a-z{}~]/u.test(String.fromCharCode(k)))
178
179function nonSimpleDomain (value) {
180 let code = 0
181 for (let i = 0, len = value.length; i < len; ++i) {
182 code = value.charCodeAt(i)
183 if (code > 126 || hexLookUp[code]) {
184 return true
185 }
186 }
187 return false
188}
189
190const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
191
192function parse (uri, opts) {
193 const options = Object.assign({}, opts)
194 const parsed = {
195 scheme: undefined,
196 userinfo: undefined,
197 host: '',
198 port: undefined,
199 path: '',
200 query: undefined,
201 fragment: undefined
202 }
203 const gotEncoding = uri.indexOf('%') !== -1
204 let isIP = false
205 if (options.reference === 'suffix') uri = (options.scheme ? options.scheme + ':' : '') + '//' + uri
206
207 const matches = uri.match(URI_PARSE)
208
209 if (matches) {
210 // store each component
211 parsed.scheme = matches[1]
212 parsed.userinfo = matches[3]
213 parsed.host = matches[4]
214 parsed.port = parseInt(matches[5], 10)
215 parsed.path = matches[6] || ''
216 parsed.query = matches[7]
217 parsed.fragment = matches[8]
218
219 // fix port number
220 if (isNaN(parsed.port)) {
221 parsed.port = matches[5]
222 }
223 if (parsed.host) {
224 const ipv4result = normalizeIPv4(parsed.host)
225 if (ipv4result.isIPV4 === false) {
226 const ipv6result = normalizeIPv6(ipv4result.host)
227 parsed.host = ipv6result.host.toLowerCase()
228 isIP = ipv6result.isIPV6
229 } else {
230 parsed.host = ipv4result.host
231 isIP = true
232 }
233 }
234 if (parsed.scheme === undefined && parsed.userinfo === undefined && parsed.host === undefined && parsed.port === undefined && !parsed.path && parsed.query === undefined) {
235 parsed.reference = 'same-document'
236 } else if (parsed.scheme === undefined) {
237 parsed.reference = 'relative'
238 } else if (parsed.fragment === undefined) {
239 parsed.reference = 'absolute'
240 } else {
241 parsed.reference = 'uri'
242 }
243
244 // check for reference errors
245 if (options.reference && options.reference !== 'suffix' && options.reference !== parsed.reference) {
246 parsed.error = parsed.error || 'URI is not a ' + options.reference + ' reference.'
247 }
248
249 // find scheme handler
250 const schemeHandler = SCHEMES[(options.scheme || parsed.scheme || '').toLowerCase()]
251
252 // check if scheme can't handle IRIs
253 if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) {
254 // if host component is a domain name
255 if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && isIP === false && nonSimpleDomain(parsed.host)) {
256 // convert Unicode IDN -> ASCII IDN
257 try {
258 parsed.host = URL.domainToASCII(parsed.host.toLowerCase())
259 } catch (e) {
260 parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e
261 }
262 }
263 // convert IRI -> URI
264 }
265
266 if (!schemeHandler || (schemeHandler && !schemeHandler.skipNormalize)) {
267 if (gotEncoding && parsed.scheme !== undefined) {
268 parsed.scheme = unescape(parsed.scheme)
269 }
270 if (gotEncoding && parsed.host !== undefined) {
271 parsed.host = unescape(parsed.host)
272 }
273 if (parsed.path && parsed.path.length) {
274 parsed.path = escape(unescape(parsed.path))
275 }
276 if (parsed.fragment && parsed.fragment.length) {
277 parsed.fragment = encodeURI(decodeURIComponent(parsed.fragment))
278 }
279 }
280
281 // perform scheme specific parsing
282 if (schemeHandler && schemeHandler.parse) {
283 schemeHandler.parse(parsed, options)
284 }
285 } else {
286 parsed.error = parsed.error || 'URI can not be parsed.'
287 }
288 return parsed
289}
290
291const fastUri = {
292 SCHEMES,
293 normalize,
294 resolve,
295 resolveComponents,
296 equal,
297 serialize,
298 parse
299}
300
301module.exports = fastUri
302module.exports.default = fastUri
303module.exports.fastUri = fastUri
Note: See TracBrowser for help on using the repository browser.