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ID2Lf,EC3L2B0C,OAAO1C,QAElC0C,OAAO1C,OAASA,EAChB0C,OAAO1C,OAAO2B,c","sources":["webpack://spritz/webpack/bootstrap","webpack://spritz/webpack/runtime/define property getters","webpack://spritz/webpack/runtime/hasOwnProperty shorthand","webpack://spritz/./src/utils/generateKey.js","webpack://spritz/./src/utils/timeout.js","webpack://spritz/./src/utils/scriptObserver.js","webpack://spritz/./src/utils/getObjectSignature.js","webpack://spritz/./src/Spritz.js","webpack://spritz/./index.esm.js"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","const NO_PROTOCOL = /^http(.*)$/;\n\n/**\n * Generates a key based on path and file name\n * @param {string} src\n * @returns {string}\n */\nexport default function generateKey(src) {\n    let _src = src;\n\n    if (!NO_PROTOCOL.test(_src)) {\n        _src = location.protocol + _src;\n    }\n\n    const url = new URL(_src);\n    const filename = url.pathname.split(\"/\").slice(-1)[0];\n\n    return `${ url.host }:${ filename }`;\n}\n","/**\n * Rejects on timeout\n * @param {Number} ms\n * @returns {Promise}\n */\nexport default function timeout(ms) {\n    return new Promise((_, reject) => setTimeout(reject, ms));\n}\n","import generateKey from \"./generateKey\";\n\nconst MATCH_STRING = \"script[src]\";\n\n/**\n * Monitors for new Scripts injected into the document and updates a Set\n * @param {Set} set\n */\nexport default function scriptObserver(set) {\n    const moOptions = { childList: true };\n    const mo = new MutationObserver(onMutation.bind(null, set));\n\n    mo.observe(document.head, moOptions);\n    mo.observe(document.body, moOptions);\n}\n\n/**\n * @param {Set} scriptsSet\n * @param {MutationRecord} mutations\n */\nfunction onMutation(scriptsSet, mutations) {\n    mutations.forEach((mutation) => {\n        Array.from(mutation.addedNodes).forEach((newNode) => {\n            if (!(newNode.nodeType === 1 && newNode.matches(MATCH_STRING))) {\n                return;\n            }\n\n            const key = generateKey(newNode.src);\n\n            scriptsSet.add(key);\n        });\n\n        Array.from(mutation.removedNodes).forEach((oldNode) => {\n            if (!(oldNode.nodeType === 1 && oldNode.matches(MATCH_STRING))) {\n                return;\n            }\n\n            const key = generateKey(oldNode.src);\n\n            scriptsSet.delete(key);\n        });\n    });\n}\n","/**\n * Generates a string signature based on object properties\n * @param {Object} object\n * @returns {string|null}\n */\nexport default function getObjectSignature(object = {}) {\n    if (typeof object !== \"object\" || Array.isArray(object)) {\n        return null;\n    }\n\n    const objectPrototype = Object.getPrototypeOf(object);\n\n    return Object.getOwnPropertyNames(objectPrototype).sort()\n        .join(\":\");\n}\n","import generateKey from \"./utils/generateKey\";\nimport timeout from \"./utils/timeout\";\nimport scriptObserver from \"./utils/scriptObserver\";\nimport getObjectSignature from \"./utils/getObjectSignature\";\n\nconst ERROR = \"error\";\nconst LOADED = \"loaded\";\n\nclass Spritz {\n\n    /**\n     * Map of pending requests\n     * @type {Object}\n     */\n    requestQueue = new Map();\n\n    /**\n     * Set of loaded Scripts\n     * @type {Set<string>}\n     */\n    loadedScripts = new Set();\n\n    /**\n     * @type {string}\n     */\n    version = process.env.VERSION;\n\n    /**\n     * @type {Function[]}\n     */\n    cmd = [];\n\n    /**\n     * @constructor\n     * @param {Spritz|Object} [_stub]\n     */\n    constructor(_stub) {\n        const stubSignature = getObjectSignature(_stub);\n        const selfSignature = getObjectSignature(this);\n\n        // Check if stub is already an instance of this class\n        if (stubSignature === selfSignature) {\n            return _stub;\n        }\n\n        Array.from(document.scripts).forEach((script) => {\n            if (!script.matches(\"script[src]\")) {\n                return;\n            }\n\n            const scriptKey = generateKey(script.src);\n\n            this.loadedScripts.add(scriptKey);\n        });\n\n        scriptObserver(this.loadedScripts);\n\n        this.cmd = _stub && _stub.cmd || [];\n        this.cmd.push = (cb) => {\n            if (typeof cb === \"function\") {\n                cb();\n            }\n        };\n    }\n\n    /**\n     * @param {string} url\n     * @returns {boolean}\n     */\n    isLoaded(url) {\n        const scriptKey = generateKey(url);\n\n        return this.loadedScripts.has(scriptKey) && !this.requestQueue.has(scriptKey);\n    }\n\n    /**\n     * @param url\n     * @return {boolean}\n     */\n    isRequested(url) {\n        const scriptKey = generateKey(url);\n\n        return this.requestQueue.has(scriptKey);\n    }\n\n    /**\n     * Injects a script into a document if its not already loaded. Loaded state is determined by a central\n     * store which keeps a list of all loaded scripts keyed by \"domain:filename\".\n     * If script is 404, it will not be loaded twice. If it times out a secondary attempt would be allowed.\n     * @param {string} url\n     * @param {Object} [options]\n     * @param {number} [options.timeout] Milliseconds\n     * @param {boolean} [options.ordered=false] No Async\n     * @returns {Promise}\n     */\n    inject(url, { timeout: ms, ordered = false } = {}) {\n        const scriptKey = generateKey(url);\n\n        if (this.isRequested(url)) {\n            return this.requestQueue.get(scriptKey);\n        }\n\n        if (this.isLoaded(url)) {\n            return Promise.resolve(url);\n        }\n\n        this.requestQueue.set(scriptKey, Spritz.injectScript(url, document.head, ordered)\n            .then((src) => {\n                this.requestQueue.delete(scriptKey);\n\n                return src;\n            }));\n\n        if (ms) {\n            return Promise.race([timeout(ms), this.requestQueue.get(scriptKey)])\n                .catch(() => {\n                    this.requestQueue.delete(scriptKey);\n                    this.loadedScripts.delete(scriptKey);\n                    throw new Error(`ScriptInjector: Timed out loading \"${ url }\" after ${ ms } ms`);\n                });\n        }\n\n        return this.requestQueue.get(scriptKey);\n    }\n\n    /**\n     * Flushes command queue\n     * @private\n     */\n    _flushQueue() {\n        if (Array.isArray(this.cmd)) {\n            while (this.cmd.length) {\n                const cb = this.cmd.pop();\n\n                if (typeof cb === \"function\") {\n                    cb();\n                }\n            }\n        }\n    }\n\n    /**\n     * @static\n     * @param {string} src Url of the script\n     * @param {Element} dest Element to which script will be appended\n     * @param {boolean} ordered\n     * @returns {Promise}\n     */\n    static injectScript(src, dest, ordered = false) {\n        let tag = document.createElement(\"script\");\n\n        tag.src = src;\n        tag.async = !ordered;\n\n        return new Promise((resolve, reject) => {\n            if (tag.onreadystatechange !== undefined) {\n                tag.onreadystatechange = function onreadystatechange() {\n                    if (LOADED === this.readyState) {\n                        tag.onreadystatechange = null;\n                        tag = null;\n                        resolve(src);\n                    }\n                };\n            } else {\n                // eslint-disable-next-line func-name-matching, no-multi-assign\n                tag.onload = tag.onerror = function onLoadOrError(event) {\n                    if (event.type === ERROR) {\n                        reject(`ScriptInjector: Error loading script \"${ src }\"`);\n                        dest.removeChild(tag);\n                        tag.onload = null;\n                        tag.onerror = null;\n                        tag = null;\n\n                        return;\n                    }\n\n                    tag.onload = null;\n                    tag.onerror = null;\n                    tag = null;\n                    resolve(src);\n                };\n            }\n\n            dest.appendChild(tag);\n        });\n    }\n\n}\n\nexport default Spritz;\n","import _Spritz from \"./src/Spritz\";\n\nconst Spritz = new _Spritz(window.Spritz);\n\nwindow.Spritz = Spritz;\nwindow.Spritz._flushQueue();\n\nexport { Spritz };\n"],"names":["__webpack_require__","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","NO_PROTOCOL","generateKey","src","_src","test","location","protocol","url","URL","filename","pathname","split","slice","concat","host","timeout","ms","Promise","_","reject","setTimeout","MATCH_STRING","scriptObserver","set","moOptions","childList","mo","MutationObserver","onMutation","bind","observe","document","head","body","scriptsSet","mutations","forEach","mutation","Array","from","addedNodes","newNode","nodeType","matches","add","removedNodes","oldNode","delete","getObjectSignature","object","arguments","length","undefined","isArray","objectPrototype","getPrototypeOf","getOwnPropertyNames","sort","join","Spritz","constructor","_stub","_defineProperty","Map","Set","process","this","scripts","script","scriptKey","loadedScripts","cmd","push","cb","isLoaded","has","requestQueue","isRequested","inject","ordered","resolve","injectScript","then","race","catch","Error","_flushQueue","pop","static","dest","tag","createElement","async","onreadystatechange","readyState","onload","onerror","event","type","removeChild","appendChild","window"],"sourceRoot":""}