UNPKG

react-grecaptcha-v3

Version:

Google Recaptcha V3 for React.js based application. Keep website performance high while prioritizing security.

1 lines 14.2 kB
{"version":3,"sources":["/home/runner/work/react-grecaptcha-v3/react-grecaptcha-v3/dist/ReCaptchaProvider.cjs","../src/ReCaptchaProvider.tsx"],"names":["__create","Object","create","__defProp","defineProperty","__getOwnPropDesc","getOwnPropertyDescriptor","__getOwnPropNames","getOwnPropertyNames","__getProtoOf","getPrototypeOf","__hasOwnProp","prototype","hasOwnProperty","__export","target","all","name","get","enumerable","__copyProps","to","from","except","desc","key","call","__toESM","mod","isNodeMode","__esModule","value","__toCommonJS","ReCaptchaProvider_exports","Context","default","ReCaptchaProvider_default","defaultScriptId","module","exports","import_jsx_runtime","require","import_react","import_subscribeEvent","import_unsubscribeEvent","import_useHandleNextInQueue","import_useQueueRef","import_utils","createContext","ReCaptchaProvider","siteKey","children","scriptProps","useRecaptchaNet","enterprise","injectionDelay","injectCallbackRef","useRef","queueRef","useQueueRef","handleNextInQueue","useHandleNextInQueue","useEffect","subscribeEvent","unsubscribeEvent","reCaptchaScriptId","id","maybeRemoveScript","inject","maybeInjectScript","src","getScriptSrc","appendTo","async","defer","nonce","current","timeout","setTimeout","clearTimeout","executeRecaptcha","useCallback","action","Promise","resolve","reject","push","onComplete","onError","contextValue","useMemo","injectScript","jsx","Provider"],"mappings":"AAAA;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,IAAIA,WAAWC,OAAOC,MAAM;AAC5B,IAAIC,YAAYF,OAAOG,cAAc;AACrC,IAAIC,mBAAmBJ,OAAOK,wBAAwB;AACtD,IAAIC,oBAAoBN,OAAOO,mBAAmB;AAClD,IAAIC,eAAeR,OAAOS,cAAc;AACxC,IAAIC,eAAeV,OAAOW,SAAS,CAACC,cAAc;AA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;AACA,IAAOf,4BAAQa;ADOf,6DAA6D;AAC7D,KAAMX,CAAAA,OAAOC,OAAO,GAAG;IACrBL,SAAAA;IACAG,iBAAAA;AACF,CAAA","sourcesContent":["\"use strict\";\n\"use client\";\nvar __create = Object.create;\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __getProtoOf = Object.getPrototypeOf;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(\n // If the importer is in node compatibility mode or this is not an ESM\n // file that has been converted to a CommonJS file using a Babel-\n // compatible transform (i.e. \"__esModule\" has not been set), then set\n // \"default\" to the CommonJS \"module.exports\" for node compatibility.\n isNodeMode || !mod || !mod.__esModule ? __defProp(target, \"default\", { value: mod, enumerable: true }) : target,\n mod\n));\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\nvar ReCaptchaProvider_exports = {};\n__export(ReCaptchaProvider_exports, {\n Context: () => Context,\n default: () => ReCaptchaProvider_default,\n defaultScriptId: () => defaultScriptId\n});\nmodule.exports = __toCommonJS(ReCaptchaProvider_exports);\nvar import_jsx_runtime = require(\"react/jsx-runtime\");\nvar import_react = require(\"react\");\nvar import_subscribeEvent = __toESM(require(\"./subscribeEvent\"), 1);\nvar import_unsubscribeEvent = __toESM(require(\"./unsubscribeEvent\"), 1);\nvar import_useHandleNextInQueue = __toESM(require(\"./useHandleNextInQueue\"), 1);\nvar import_useQueueRef = __toESM(require(\"./useQueueRef\"), 1);\nvar import_utils = require(\"./utils\");\nconst Context = (0, import_react.createContext)(null);\nconst defaultScriptId = \"rusted_labs_react_recaptcha_v3\";\nconst ReCaptchaProvider = ({\n siteKey,\n children,\n scriptProps = {},\n useRecaptchaNet = false,\n enterprise = false,\n injectionDelay = null\n}) => {\n const injectCallbackRef = (0, import_react.useRef)(null);\n const queueRef = (0, import_useQueueRef.default)();\n const handleNextInQueue = (0, import_useHandleNextInQueue.default)(siteKey, queueRef);\n (0, import_react.useEffect)(() => {\n (0, import_subscribeEvent.default)(handleNextInQueue);\n return () => {\n (0, import_unsubscribeEvent.default)(handleNextInQueue);\n };\n }, [handleNextInQueue]);\n (0, import_react.useEffect)(() => {\n const reCaptchaScriptId = scriptProps.id ?? defaultScriptId;\n if (siteKey === null) {\n (0, import_utils.maybeRemoveScript)(reCaptchaScriptId);\n } else {\n const inject = () => {\n (0, import_utils.maybeInjectScript)({\n src: (0, import_utils.getScriptSrc)({\n enterprise,\n useRecaptchaNet,\n siteKey\n }),\n appendTo: scriptProps.appendTo ?? \"head\",\n id: reCaptchaScriptId,\n async: scriptProps.async ?? true,\n defer: scriptProps.defer ?? true,\n nonce: scriptProps.nonce\n });\n };\n injectCallbackRef.current = inject;\n if (injectionDelay === null) {\n inject();\n } else {\n const timeout = setTimeout(inject, injectionDelay);\n return () => {\n injectCallbackRef.current = null;\n (0, import_utils.maybeRemoveScript)(reCaptchaScriptId);\n clearTimeout(timeout);\n };\n }\n }\n return () => {\n (0, import_utils.maybeRemoveScript)(reCaptchaScriptId);\n };\n }, [\n enterprise,\n handleNextInQueue,\n injectionDelay,\n scriptProps.appendTo,\n scriptProps.async,\n scriptProps.defer,\n scriptProps.id,\n scriptProps.nonce,\n siteKey,\n useRecaptchaNet\n ]);\n const executeRecaptcha = (0, import_react.useCallback)(\n async (action) => (\n // eslint-disable-next-line promise/avoid-new -- no time to fix\n await new Promise((resolve, reject) => {\n queueRef.current.push({\n action,\n onComplete: resolve,\n onError: reject\n });\n handleNextInQueue();\n })\n ),\n [handleNextInQueue, queueRef]\n );\n const contextValue = (0, import_react.useMemo)(\n () => ({\n executeRecaptcha,\n injectScript: injectCallbackRef\n }),\n [executeRecaptcha]\n );\n return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value: contextValue, children });\n};\nvar ReCaptchaProvider_default = ReCaptchaProvider;\n// Annotate the CommonJS export names for ESM import in node:\n0 && (module.exports = {\n Context,\n defaultScriptId\n});\n","'use client'\nimport {\n createContext,\n type FunctionComponent,\n type ReactNode,\n type RefObject,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n} from 'react'\nimport subscribeEvent from './subscribeEvent'\nimport unsubscribeEvent from './unsubscribeEvent'\nimport type { ExecuteRecaptcha } from './useExecuteReCaptcha'\nimport useHandleNextInQueue from './useHandleNextInQueue'\nimport useQueueRef from './useQueueRef'\nimport { getScriptSrc, maybeInjectScript, maybeRemoveScript } from './utils'\n\ninterface ContextType {\n executeRecaptcha: ExecuteRecaptcha\n injectScript: RefObject<null | (() => void)>\n}\nexport const Context = createContext<ContextType | null>(null)\nexport interface ScriptProps {\n nonce?: string\n defer?: boolean\n async?: boolean\n appendTo?: 'head' | 'body'\n id?: string\n}\nexport const defaultScriptId = 'rusted_labs_react_recaptcha_v3'\nexport type Props = Readonly<{\n siteKey: string | null\n children: ReactNode\n useRecaptchaNet?: boolean\n enterprise?: boolean\n scriptProps?: ScriptProps\n injectionDelay?: number\n}>\nconst ReCaptchaProvider: FunctionComponent<Props> = ({\n siteKey,\n children,\n scriptProps = {},\n useRecaptchaNet = false,\n enterprise = false,\n injectionDelay = null,\n}) => {\n const injectCallbackRef = useRef<null | (() => void)>(null)\n const queueRef = useQueueRef()\n const handleNextInQueue = useHandleNextInQueue(siteKey, queueRef)\n useEffect(() => {\n subscribeEvent(handleNextInQueue)\n return () => {\n unsubscribeEvent(handleNextInQueue)\n }\n }, [handleNextInQueue])\n useEffect(() => {\n const reCaptchaScriptId = scriptProps.id ?? defaultScriptId\n if (siteKey === null) {\n maybeRemoveScript(reCaptchaScriptId)\n } else {\n const inject = (): void => {\n maybeInjectScript({\n src: getScriptSrc({\n enterprise,\n useRecaptchaNet,\n siteKey,\n }),\n appendTo: scriptProps.appendTo ?? 'head',\n id: reCaptchaScriptId,\n async: scriptProps.async ?? true,\n defer: scriptProps.defer ?? true,\n nonce: scriptProps.nonce,\n })\n }\n injectCallbackRef.current = inject\n if (injectionDelay === null) {\n inject()\n } else {\n const timeout = setTimeout(inject, injectionDelay)\n return () => {\n injectCallbackRef.current = null\n maybeRemoveScript(reCaptchaScriptId)\n clearTimeout(timeout)\n }\n }\n }\n return () => {\n maybeRemoveScript(reCaptchaScriptId)\n }\n }, [\n enterprise,\n handleNextInQueue,\n injectionDelay,\n scriptProps.appendTo,\n scriptProps.async,\n scriptProps.defer,\n scriptProps.id,\n scriptProps.nonce,\n siteKey,\n useRecaptchaNet,\n ])\n\n const executeRecaptcha: ExecuteRecaptcha = useCallback(\n async (action: string): Promise<string> =>\n // eslint-disable-next-line promise/avoid-new -- no time to fix\n await new Promise((resolve, reject) => {\n queueRef.current.push({\n action,\n onComplete: resolve,\n onError: reject,\n })\n handleNextInQueue()\n }),\n [handleNextInQueue, queueRef]\n )\n const contextValue = useMemo(\n () => ({\n executeRecaptcha,\n injectScript: injectCallbackRef,\n }),\n [executeRecaptcha]\n )\n return <Context.Provider value={contextValue}>{children}</Context.Provider>\n}\nexport default ReCaptchaProvider\n"]}