UNPKG

is-same-origin

Version:

Check if two URLs are same origin, for Node and the browser

5 lines 8.1 kB
/** @license is-same-origin v0.0.7 * Copyright (c) 2018-present, Qingrong Ke <keqingrong1992@gmail.com> (https://keqingrong.github.io/) * Released under the MIT license */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.isSameOrigin=t()}(this,function(){"use strict";function e(e){var t=null;try{t=new URL(e)}catch(e){console.error(e)}return t}return function(e){function t(t){return!!t&&("Symbol"in e&&"iterator"in e.Symbol&&"function"==typeof t[Symbol.iterator]||!!Array.isArray(t))}function n(e){return"from"in Array?Array.from(e):Array.prototype.slice.call(e)}!function(){var r,i=e.URL;try{if(i){if("searchParams"in(r=new e.URL("http://example.com")))return;"href"in r||(r=void 0)}}catch(e){}function o(e){var t="",n=!0;return e.forEach(function(e){var r=encodeURIComponent(e.name),i=encodeURIComponent(e.value);n||(t+="&"),t+=r+"="+i,n=!1}),t.replace(/%20/g,"+")}function a(e,t){var n=e.split("&");t&&-1===n[0].indexOf("=")&&(n[0]="="+n[0]);var r=[];n.forEach(function(e){if(0!==e.length){var t=e.indexOf("=");if(-1!==t)var n=e.substring(0,t),i=e.substring(t+1);else n=e,i="";n=n.replace(/\+/g," "),i=i.replace(/\+/g," "),r.push({name:n,value:i})}});var i=[];return r.forEach(function(e){i.push({name:decodeURIComponent(e.name),value:decodeURIComponent(e.value)})}),i}function u(e){var r=this;this._list=[],void 0===e||null===e||(e instanceof u?this._list=a(String(e)):"object"==typeof e&&t(e)?n(e).forEach(function(e){if(!t(e))throw TypeError();var i=n(e);if(2!==i.length)throw TypeError();r._list.push({name:String(i[0]),value:String(i[1])})}):"object"==typeof e&&e?Object.keys(e).forEach(function(t){r._list.push({name:String(t),value:String(e[t])})}):("?"===(e=String(e)).substring(0,1)&&(e=e.substring(1)),this._list=a(e))),this._url_object=null,this._setList=function(e){i||(r._list=e)};var i=!1;this._update_steps=function(){i||(i=!0,r._url_object&&("about:"===r._url_object.protocol&&-1!==r._url_object.pathname.indexOf("?")&&(r._url_object.pathname=r._url_object.pathname.split("?")[0]),r._url_object.search=o(r._list),i=!1))}}function l(e,t){var n=0;this.next=function(){if(n>=e.length)return{done:!0,value:void 0};var r=e[n++];return{done:!1,value:"key"===t?r.name:"value"===t?r.value:[r.name,r.value]}}}function c(t,n){if(!(this instanceof e.URL))throw new TypeError("Failed to construct 'URL': Please use the 'new' operator.");n&&(t=function(){if(r)return new i(t,n).href;var e;try{var o;if("[object OperaMini]"===Object.prototype.toString.call(window.operamini)?((e=document.createElement("iframe")).style.display="none",document.documentElement.appendChild(e),o=e.contentWindow.document):document.implementation&&document.implementation.createHTMLDocument?o=document.implementation.createHTMLDocument(""):document.implementation&&document.implementation.createDocument?((o=document.implementation.createDocument("http://www.w3.org/1999/xhtml","html",null)).documentElement.appendChild(o.createElement("head")),o.documentElement.appendChild(o.createElement("body"))):window.ActiveXObject&&((o=new window.ActiveXObject("htmlfile")).write("<head></head><body></body>"),o.close()),!o)throw Error("base not supported");var a=o.createElement("base");a.href=n,o.getElementsByTagName("head")[0].appendChild(a);var u=o.createElement("a");return u.href=t,u.href}finally{e&&e.parentNode.removeChild(e)}}());var o=function(e){if(r)return new i(e);var t=document.createElement("a");return t.href=e,t}(t||""),l=function(){if(!("defineProperties"in Object))return!1;try{var e={};return Object.defineProperties(e,{prop:{get:function(){return!0}}}),e.prop}catch(e){return!1}}()?this:document.createElement("a"),c=new u(o.search?o.search.substring(1):null);function s(){var e=o.href.replace(/#$|\?$|\?(?=#)/g,"");o.href!==e&&(o.href=e)}function f(){c._setList(o.search?a(o.search.substring(1)):[]),c._update_steps()}return c._url_object=l,Object.defineProperties(l,{href:{get:function(){return o.href},set:function(e){o.href=e,s(),f()},enumerable:!0,configurable:!0},origin:{get:function(){return"origin"in o?o.origin:this.protocol+"//"+this.host},enumerable:!0,configurable:!0},protocol:{get:function(){return o.protocol},set:function(e){o.protocol=e},enumerable:!0,configurable:!0},username:{get:function(){return o.username},set:function(e){o.username=e},enumerable:!0,configurable:!0},password:{get:function(){return o.password},set:function(e){o.password=e},enumerable:!0,configurable:!0},host:{get:function(){var e={"http:":/:80$/,"https:":/:443$/,"ftp:":/:21$/}[o.protocol];return e?o.host.replace(e,""):o.host},set:function(e){o.host=e},enumerable:!0,configurable:!0},hostname:{get:function(){return o.hostname},set:function(e){o.hostname=e},enumerable:!0,configurable:!0},port:{get:function(){return o.port},set:function(e){o.port=e},enumerable:!0,configurable:!0},pathname:{get:function(){return"/"!==o.pathname.charAt(0)?"/"+o.pathname:o.pathname},set:function(e){o.pathname=e},enumerable:!0,configurable:!0},search:{get:function(){return o.search},set:function(e){o.search!==e&&(o.search=e,s(),f())},enumerable:!0,configurable:!0},searchParams:{get:function(){return c},enumerable:!0,configurable:!0},hash:{get:function(){return o.hash},set:function(e){o.hash=e,s()},enumerable:!0,configurable:!0},toString:{value:function(){return o.toString()},enumerable:!1,configurable:!0},valueOf:{value:function(){return o.valueOf()},enumerable:!1,configurable:!0}}),l}if(Object.defineProperties(u.prototype,{append:{value:function(e,t){this._list.push({name:e,value:t}),this._update_steps()},writable:!0,enumerable:!0,configurable:!0},delete:{value:function(e){for(var t=0;t<this._list.length;)this._list[t].name===e?this._list.splice(t,1):++t;this._update_steps()},writable:!0,enumerable:!0,configurable:!0},get:{value:function(e){for(var t=0;t<this._list.length;++t)if(this._list[t].name===e)return this._list[t].value;return null},writable:!0,enumerable:!0,configurable:!0},getAll:{value:function(e){for(var t=[],n=0;n<this._list.length;++n)this._list[n].name===e&&t.push(this._list[n].value);return t},writable:!0,enumerable:!0,configurable:!0},has:{value:function(e){for(var t=0;t<this._list.length;++t)if(this._list[t].name===e)return!0;return!1},writable:!0,enumerable:!0,configurable:!0},set:{value:function(e,t){for(var n=!1,r=0;r<this._list.length;)this._list[r].name===e?n?this._list.splice(r,1):(this._list[r].value=t,n=!0,++r):++r;n||this._list.push({name:e,value:t}),this._update_steps()},writable:!0,enumerable:!0,configurable:!0},entries:{value:function(){return new l(this._list,"key+value")},writable:!0,enumerable:!0,configurable:!0},keys:{value:function(){return new l(this._list,"key")},writable:!0,enumerable:!0,configurable:!0},values:{value:function(){return new l(this._list,"value")},writable:!0,enumerable:!0,configurable:!0},forEach:{value:function(e){var t=arguments.length>1?arguments[1]:void 0;this._list.forEach(function(n,r){e.call(t,n.value,n.name)})},writable:!0,enumerable:!0,configurable:!0},toString:{value:function(){return o(this._list)},writable:!0,enumerable:!1,configurable:!0}}),"Symbol"in e&&"iterator"in e.Symbol&&(Object.defineProperty(u.prototype,e.Symbol.iterator,{value:u.prototype.entries,writable:!0,enumerable:!0,configurable:!0}),Object.defineProperty(l.prototype,e.Symbol.iterator,{value:function(){return this},writable:!0,enumerable:!0,configurable:!0})),i)for(var s in i)i.hasOwnProperty(s)&&"function"==typeof i[s]&&(c[s]=i[s]);e.URL=c,e.URLSearchParams=u}(),function(){if("1"!==new e.URLSearchParams([["a",1]]).get("a")||"1"!==new e.URLSearchParams({a:1}).get("a")){var r=e.URLSearchParams;e.URLSearchParams=function(e){if(e&&"object"==typeof e&&t(e)){var i=new r;return n(e).forEach(function(e){if(!t(e))throw TypeError();var r=n(e);if(2!==r.length)throw TypeError();i.append(r[0],r[1])}),i}return e&&"object"==typeof e?(i=new r,Object.keys(e).forEach(function(t){i.set(t,e[t])}),i):new r(e)}}}()}(self),function(t,n){var r=e(t),i=e(n);return!(!r||!i)&&(r.origin===i.origin||r.protocol===i.protocol&&r.hostname===i.hostname&&r.port===i.port)}});