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/webpack/universalModuleDefinition","webpack://mcaptchaGlue/webpack/startup","webpack://mcaptchaGlue/./src/index.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"mcaptchaGlue\"] = factory();\n\telse\n\t\troot[\"mcaptchaGlue\"] = factory();\n})(this, () => {\nreturn ","// startup\n// Load entry module and return exports\n// This entry module is referenced by other modules so it can't be inlined\nvar __webpack_exports__ = {};\n__webpack_modules__[607](0, __webpack_exports__);\n","/*\n * mCaptcha is a PoW based DoS protection software.\n * This is the frontend web component of the mCaptcha system\n * Copyright © 2021 Aravinth Manivnanan .\n *\n * Use of this source code is governed by Apache 2.0 or MIT license.\n * You shoud have received a copy of MIT and Apache 2.0 along with\n * this program. If not, see for\n * MIT or for Apache.\n */\n\n/**\n * Site key configuration\n */\nexport type SiteKey = {\n /** site key as given in the mCaptcha admin dashboard */\n key: string;\n /** URL of the mCaptcha instance. Used in building widget link */\n instanceUrl?: URL;\n};\n\n/**\n * Widget configuration\n */\nexport type WidgetConfig = {\n /** site key configuration. Mutually exclusive with widgetLink */\n siteKey?: SiteKey;\n\n /** widget link. Mutually exclusive with siteKey */\n widgetLink?: URL;\n};\n\n/** configuration error thrown by MCaptchaWidget */\nexport class ConfigurationError extends Error {\n /** error message */\n message = \"Provide either widget link or site key to display mCaptcha widget\";\n}\n\n/** Listens for messages from mCaptcha widget and provides hooks to update\n * state and configure mCaptcha widget*/\nexport default class Receiver {\n private updateState: (token: string) => void;\n widgetLink: URL;\n\n /**\n * @param {WidgetConfig} config: used to configure widget link and\n * selectively filter messages\n * @param {(token: string) => void} updateState:\n * callback function used to update input field with the latest received\n * token.\n *\n * @throws {ConfigurationError}: This error is thrown when neither widget\n * link nor site key is provided to this compoenent or when both are provided\n * at the same time.\n */\n constructor(config: WidgetConfig, updateState: (token: string) => void) {\n this.updateState = updateState;\n if (config.widgetLink && config.siteKey) {\n throw new ConfigurationError();\n }\n\n if (config.widgetLink) {\n this.widgetLink = config.widgetLink;\n } else if (config.siteKey) {\n if (config.siteKey.instanceUrl) {\n this.widgetLink = config.siteKey.instanceUrl;\n this.widgetLink.pathname = \"/widget/\";\n this.widgetLink.search = `?sitekey=${config.siteKey.key}`;\n } else {\n this.widgetLink = new URL(\n `https://demo.mcaptcha.org/widget/?sitekey=${config.siteKey.key}`\n );\n }\n } else {\n throw new ConfigurationError();\n }\n }\n\n /**\n * Listen for messages from the mCaptcha iframe widget\n */\n listen() {\n window.addEventListener(\"message\", this.handle);\n }\n\n /**\n * Delete listener\n */\n destroy() {\n window.removeEventListener(\"message\", this.handle);\n }\n\n /**\n * Handle messages sent from mCaptcha widget iframe\n * @param {MessageEvent} e: message containing token from mCaptcha iframe.\n * Message origin should match the hostname of the widget link\n */\n handle = (e: MessageEvent) => {\n console.log(`message received from ${e.origin} with data: ${e.data.token}`);\n if (new URL(e.origin).host != this.widgetLink.host) {\n console.error(\n `expected message from ${this.widgetLink.host} but received message from ${e.origin}. Aborting.`\n );\n return;\n }\n this.updateState(e.data.token);\n };\n}\n"],"names":["root","factory","exports","module","define","amd","this","__webpack_exports__","message","Error","ConfigurationError","config","updateState","handle","e","console","log","origin","data","token","URL","host","widgetLink","error","siteKey","instanceUrl","pathname","search","key","listen","window","addEventListener","destroy","removeEventListener"],"sourceRoot":""}