1 |
- {"version":3,"file":"index.mjs","sources":["../src/format.js","../third_party/format.js","../src/constants.js","../src/parse.js","../src/resolve.js"],"sourcesContent":["/*\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the 'License');\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an 'AS IS' BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport qs from 'querystring';\nimport parse from './parse';\nimport format from '../third_party/format';\n\nconst slashedProtocols = /https?|ftp|gopher|file/;\n\nexport default function(urlObj) {\n if (typeof urlObj === 'string') {\n urlObj = parse(urlObj);\n }\n\n const { protocol, host, pathname, search, hash } = format(\n urlObj,\n qs,\n slashedProtocols\n );\n\n return `${protocol}${host}${pathname}${search}${hash}`;\n}\n","// Format function modified from nodejs\n// Copyright Joyent, Inc. and other Node contributors.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a\n// copy of this software and associated documentation files (the\n// \"Software\"), to deal in the Software without restriction, including\n// without limitation the rights to use, copy, modify, merge, publish,\n// distribute, sublicense, and/or sell copies of the Software, and to permit\n// persons to whom the Software is furnished to do so, subject to the\n// following conditions:\n//\n// The above copyright notice and this permission notice shall be included\n// in all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN\n// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE\n// USE OR OTHER DEALINGS IN THE SOFTWARE.\n\nexport default function(urlObj, qs, slashedProtocols) {\n let { auth, hostname } = urlObj;\n let protocol = urlObj.protocol || '';\n let pathname = urlObj.pathname || '';\n let hash = urlObj.hash || '';\n let query = urlObj.query || '';\n let host = false;\n\n auth = auth ? encodeURIComponent(auth).replace(/%3A/i, ':') + '@' : '';\n\n if (urlObj.host) {\n host = auth + urlObj.host;\n } else if (hostname) {\n host = auth + (~hostname.indexOf(':') ? `[${hostname}]` : hostname);\n if (urlObj.port) {\n host += ':' + urlObj.port;\n }\n }\n\n if (query && typeof query === 'object') {\n // query = '' + new URLSearchParams(query);\n query = qs.encode(query);\n }\n\n let search = urlObj.search || (query && `?${query}`) || '';\n\n if (protocol && protocol.substr(-1) !== ':') protocol += ':';\n\n if (\n urlObj.slashes ||\n ((!protocol || slashedProtocols.test(protocol)) && host !== false)\n ) {\n host = '//' + (host || '');\n if (pathname && pathname[0] !== '/') pathname = '/' + pathname;\n } else if (!host) {\n host = '';\n }\n\n if (hash && hash[0] !== '#') hash = '#' + hash;\n if (search && search[0] !== '?') search = '?' + search;\n\n pathname = pathname.replace(/[?#]/g, encodeURIComponent);\n search = search.replace('#', '%23');\n\n return {\n protocol,\n host,\n pathname,\n search,\n hash\n };\n}\n","/*\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the 'License');\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an 'AS IS' BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport const PROTOCOL = 'http://';\nexport const HOST = 'w.w';\nexport const BASE_URL = PROTOCOL + HOST;\n","/*\n * Copyright 2019 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the 'License');\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an 'AS IS' BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport qs from 'querystring';\nimport format from './format';\nimport { BASE_URL, HOST } from './constants';\n\nconst slashedProtocols = /^https?|ftp|gopher|file/;\nconst urlRegex = /^(.*?)([#?].*)/;\nconst protocolRegex = /^([a-z0-9.+-]*:)(\\/{0,3})(.*)/i;\nconst slashesRegex = /^([a-z0-9.+-]*:)?\\/\\/\\/*/i;\nconst ipv6Regex = /^([a-z0-9.+-]*:)(\\/{0,2})\\[(.*)\\]$/i;\n\nfunction safeDecode(url) {\n try {\n return decodeURI(url);\n } catch (_) {\n return url;\n }\n}\n\nexport default function(urlStr, parseQs = false, slashesDenoteHost = false) {\n urlStr = urlStr.trim();\n\n const slashesMatch = urlStr.match(urlRegex);\n if (slashesMatch) {\n urlStr = safeDecode(slashesMatch[1]).replace(/\\\\/g, '/') + slashesMatch[2];\n } else {\n urlStr = safeDecode(urlStr).replace(/\\\\/g, '/');\n }\n\n // IPv6 check\n if (ipv6Regex.test(urlStr)) {\n // Add trailing slash to IPV6 urls to match parsing\n if (urlStr.slice(-1) !== '/') urlStr += '/';\n }\n\n const protocolMatch =\n !/(^javascript)/.test(urlStr) && urlStr.match(protocolRegex);\n let slashes = slashesRegex.test(urlStr);\n let protocolPrefix = '';\n\n if (protocolMatch) {\n if (!slashedProtocols.test(protocolMatch[1])) {\n // Replace invalid protocol with a valid one for correct parsing\n protocolPrefix = protocolMatch[1].toLowerCase();\n urlStr = `${protocolMatch[2]}${protocolMatch[3]}`;\n }\n\n if (!protocolMatch[2]) {\n slashes = false;\n if (slashedProtocols.test(protocolMatch[1])) {\n protocolPrefix = protocolMatch[1];\n urlStr = `${protocolMatch[3]}`;\n } else {\n urlStr = `//${protocolMatch[3]}`;\n }\n }\n\n // Handle '///' in url Eg: http:///s//a/b/c\n // TODO: file:/some/dir/# should become file:///some/dir/# according to the url module in node\n if (protocolMatch[2].length === 3 || protocolMatch[2].length === 1) {\n protocolPrefix = protocolMatch[1];\n urlStr = `/${protocolMatch[3]}`;\n }\n }\n\n // If port is 80 we change it to 8000 and undo it later\n let portMatch = urlStr.match(/(:[0-9]+)/);\n let portSuffix = '';\n\n if (portMatch && portMatch[1] && portMatch[1].length === 3) {\n portSuffix = portMatch[1];\n urlStr = urlStr.replace(portSuffix, `${portSuffix}00`);\n }\n\n let url;\n let res = {};\n let err = '';\n let preSlash = '';\n\n try {\n url = new URL(urlStr);\n } catch (e) {\n err = e;\n\n // Handle url with slashes - Eg: //some_url\n if (\n !protocolPrefix &&\n !slashesDenoteHost &&\n /^\\/\\//.test(urlStr) &&\n !/^\\/\\/.+[@.]/.test(urlStr)\n ) {\n preSlash = '/';\n urlStr = urlStr.substr(1);\n }\n\n try {\n url = new URL(urlStr, BASE_URL);\n } catch (_) {\n // Unable to parse the url\n // If the URL has only the protocol - Eg: \"foo:\"\n res.protocol = protocolPrefix;\n res.href = protocolPrefix;\n return res;\n }\n }\n\n res.slashes = slashes && !preSlash;\n res.host = url.host === HOST ? '' : url.host;\n res.hostname =\n url.hostname === HOST ? '' : url.hostname.replace(/(\\[|\\])/g, '');\n res.protocol = err ? protocolPrefix || null : url.protocol;\n\n res.search = url.search.replace(/\\\\/g, '
|