@internetarchive/recaptcha-manager 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +29 -0
- package/.github/workflows/ci.yml +26 -0
- package/.husky/pre-commit +4 -0
- package/LICENSE +661 -0
- package/README.md +56 -0
- package/demo/app-root.ts +53 -0
- package/demo/index.html +26 -0
- package/dist/demo/app-root.d.ts +9 -0
- package/dist/demo/app-root.js +49 -0
- package/dist/demo/app-root.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/src/recaptcha-manager.d.ts +68 -0
- package/dist/src/recaptcha-manager.js +126 -0
- package/dist/src/recaptcha-manager.js.map +1 -0
- package/dist/test/recaptcha-manager.test.d.ts +0 -0
- package/dist/test/recaptcha-manager.test.js +4 -0
- package/dist/test/recaptcha-manager.test.js.map +1 -0
- package/dist/test/your-webcomponent.test.d.ts +0 -0
- package/dist/test/your-webcomponent.test.js +4 -0
- package/dist/test/your-webcomponent.test.js.map +1 -0
- package/index.ts +1 -0
- package/package.json +96 -0
- package/src/recaptcha-manager.ts +174 -0
- package/test/recaptcha-manager.test.ts +3 -0
- package/tsconfig.json +20 -0
- package/web-dev-server.config.mjs +28 -0
- package/web-test-runner.config.mjs +41 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LazyLoaderService,
|
|
3
|
+
LazyLoaderServiceInterface,
|
|
4
|
+
} from '@internetarchive/lazy-loader-service';
|
|
5
|
+
|
|
6
|
+
export interface RecaptchaManagerInterface {
|
|
7
|
+
execute(): Promise<string>;
|
|
8
|
+
setup(
|
|
9
|
+
container: HTMLElement,
|
|
10
|
+
tabIndex: number,
|
|
11
|
+
theme: ReCaptchaV2.Theme,
|
|
12
|
+
type: ReCaptchaV2.Type
|
|
13
|
+
): void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class RecaptchaManager implements RecaptchaManagerInterface {
|
|
17
|
+
/**
|
|
18
|
+
* This is a convenience initializer that also lazily loads
|
|
19
|
+
* the recaptcha library from Google.
|
|
20
|
+
*
|
|
21
|
+
* @param options
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
static async getRecaptchaManager(options: {
|
|
25
|
+
siteKey: string;
|
|
26
|
+
}): Promise<RecaptchaManagerInterface> {
|
|
27
|
+
const grecaptchaLibrary = await RecaptchaManager.loadRecaptchaLibrary();
|
|
28
|
+
return new RecaptchaManager({
|
|
29
|
+
grecaptchaLibrary,
|
|
30
|
+
siteKey: options.siteKey,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Load the Recaptch library from Google.
|
|
36
|
+
*
|
|
37
|
+
* @param options
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
static async loadRecaptchaLibrary(options?: {
|
|
41
|
+
lazyLoader?: LazyLoaderServiceInterface;
|
|
42
|
+
}): Promise<ReCaptchaV2.ReCaptcha> {
|
|
43
|
+
const service = options?.lazyLoader ?? new LazyLoaderService();
|
|
44
|
+
|
|
45
|
+
return new Promise(resolve => {
|
|
46
|
+
(window as any).grecaptchaLoadedCallback = (): void => {
|
|
47
|
+
setTimeout(() => {
|
|
48
|
+
// remove the callback when we're done with it
|
|
49
|
+
delete (window as any).grecaptchaLoadedCallback;
|
|
50
|
+
}, 10);
|
|
51
|
+
resolve(window.grecaptcha);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
service.loadScript({
|
|
55
|
+
src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private grecaptchaLibrary: ReCaptchaV2.ReCaptcha;
|
|
61
|
+
|
|
62
|
+
private siteKey: string;
|
|
63
|
+
|
|
64
|
+
constructor(options: {
|
|
65
|
+
grecaptchaLibrary: ReCaptchaV2.ReCaptcha;
|
|
66
|
+
siteKey: string;
|
|
67
|
+
}) {
|
|
68
|
+
this.grecaptchaLibrary = options.grecaptchaLibrary;
|
|
69
|
+
this.siteKey = options.siteKey;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private executionSuccessBlock?: (token: string) => void;
|
|
73
|
+
|
|
74
|
+
private executionExpiredBlock?: () => void;
|
|
75
|
+
|
|
76
|
+
private executionErrorBlock?: () => void;
|
|
77
|
+
|
|
78
|
+
private isExecuting = false;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Execute Recaptcha and return a Promise containing the response token.
|
|
82
|
+
*
|
|
83
|
+
* This is an interesting flow.. we call `execute()` here, but have to wait for the
|
|
84
|
+
* response and expiration handlers that we bind during the inital `setup` call.
|
|
85
|
+
* For consumers, we want to be able to just call `execute()` and wait for a response.
|
|
86
|
+
* To allow this, we assign two callbacks:
|
|
87
|
+
* - `executionSuccessBlock`
|
|
88
|
+
* - `executionExpiredBlock`
|
|
89
|
+
*
|
|
90
|
+
* We then call those callbacks from inside `responseHandler` and `expiredHandler` to
|
|
91
|
+
* either resolve or reject the Promise.
|
|
92
|
+
*
|
|
93
|
+
* ie:
|
|
94
|
+
*
|
|
95
|
+
* try {
|
|
96
|
+
* const recaptchaResult = await recaptchaManager.execute();
|
|
97
|
+
* console.log('recaptcha token:', recaptchaResult);
|
|
98
|
+
* } catch {
|
|
99
|
+
* console.error('something happened')
|
|
100
|
+
* }
|
|
101
|
+
*
|
|
102
|
+
* @returns {Promise<string>}
|
|
103
|
+
* @memberof RecaptchaManager
|
|
104
|
+
*/
|
|
105
|
+
execute(): Promise<string> {
|
|
106
|
+
if (this.isExecuting) {
|
|
107
|
+
this.finishExecution();
|
|
108
|
+
}
|
|
109
|
+
this.isExecuting = true;
|
|
110
|
+
return new Promise((resolve, reject) => {
|
|
111
|
+
this.executionSuccessBlock = (token: string): void => {
|
|
112
|
+
this.finishExecution();
|
|
113
|
+
resolve(token);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
this.executionExpiredBlock = (): void => {
|
|
117
|
+
this.finishExecution();
|
|
118
|
+
reject(new Error('expired'));
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
this.executionErrorBlock = (): void => {
|
|
122
|
+
this.finishExecution();
|
|
123
|
+
reject(new Error('error'));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
this.grecaptchaLibrary.execute();
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private finishExecution(): void {
|
|
131
|
+
this.isExecuting = false;
|
|
132
|
+
this.grecaptchaLibrary.reset();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setup(
|
|
136
|
+
container: HTMLElement,
|
|
137
|
+
tabIndex: number,
|
|
138
|
+
theme: ReCaptchaV2.Theme,
|
|
139
|
+
type: ReCaptchaV2.Type
|
|
140
|
+
): void {
|
|
141
|
+
this.grecaptchaLibrary.render(container, {
|
|
142
|
+
callback: this.responseHandler.bind(this),
|
|
143
|
+
'expired-callback': this.expiredHandler.bind(this),
|
|
144
|
+
'error-callback': this.errorHandler.bind(this),
|
|
145
|
+
sitekey: this.siteKey,
|
|
146
|
+
tabindex: tabIndex,
|
|
147
|
+
theme,
|
|
148
|
+
type,
|
|
149
|
+
size: 'invisible',
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
154
|
+
private responseHandler(response: any): void {
|
|
155
|
+
if (this.executionSuccessBlock) {
|
|
156
|
+
this.executionSuccessBlock(response);
|
|
157
|
+
this.executionSuccessBlock = undefined;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private expiredHandler(): void {
|
|
162
|
+
if (this.executionExpiredBlock) {
|
|
163
|
+
this.executionExpiredBlock();
|
|
164
|
+
this.executionExpiredBlock = undefined;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private errorHandler(): void {
|
|
169
|
+
if (this.executionErrorBlock) {
|
|
170
|
+
this.executionErrorBlock();
|
|
171
|
+
this.executionErrorBlock = undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2018",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"noEmitOnError": true,
|
|
7
|
+
"lib": ["es2017", "dom"],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": false,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"importHelpers": true,
|
|
13
|
+
"outDir": "dist",
|
|
14
|
+
"sourceMap": true,
|
|
15
|
+
"inlineSources": true,
|
|
16
|
+
"rootDir": "./",
|
|
17
|
+
"declaration": true,
|
|
18
|
+
},
|
|
19
|
+
"include": ["**/*.ts"]
|
|
20
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// import { hmrPlugin, presets } from '@open-wc/dev-server-hmr';
|
|
2
|
+
|
|
3
|
+
/** Use Hot Module replacement by adding --hmr to the start command */
|
|
4
|
+
const hmr = process.argv.includes('--hmr');
|
|
5
|
+
|
|
6
|
+
export default /** @type {import('@web/dev-server').DevServerConfig} */ ({
|
|
7
|
+
nodeResolve: true,
|
|
8
|
+
open: '/demo/',
|
|
9
|
+
watch: !hmr,
|
|
10
|
+
|
|
11
|
+
/** Compile JS for older browsers. Requires @web/dev-server-esbuild plugin */
|
|
12
|
+
// esbuildTarget: 'auto'
|
|
13
|
+
|
|
14
|
+
/** Set appIndex to enable SPA routing */
|
|
15
|
+
// appIndex: 'demo/index.html',
|
|
16
|
+
|
|
17
|
+
/** Confgure bare import resolve plugin */
|
|
18
|
+
// nodeResolve: {
|
|
19
|
+
// exportConditions: ['browser', 'development']
|
|
20
|
+
// },
|
|
21
|
+
|
|
22
|
+
plugins: [
|
|
23
|
+
/** Use Hot Module Replacement by uncommenting. Requires @open-wc/dev-server-hmr plugin */
|
|
24
|
+
// hmr && hmrPlugin({ exclude: ['**/*/node_modules/**/*'], presets: [presets.litElement] }),
|
|
25
|
+
],
|
|
26
|
+
|
|
27
|
+
// See documentation for all available options
|
|
28
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// import { playwrightLauncher } from '@web/test-runner-playwright';
|
|
2
|
+
|
|
3
|
+
const filteredLogs = ['Running in dev mode', 'lit-html is in dev mode'];
|
|
4
|
+
|
|
5
|
+
export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
|
|
6
|
+
/** Test files to run */
|
|
7
|
+
files: 'dist/test/**/*.test.js',
|
|
8
|
+
|
|
9
|
+
/** Resolve bare module imports */
|
|
10
|
+
nodeResolve: {
|
|
11
|
+
exportConditions: ['browser', 'development'],
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
/** Filter out lit dev mode logs */
|
|
15
|
+
filterBrowserLogs(log) {
|
|
16
|
+
for (const arg of log.args) {
|
|
17
|
+
if (typeof arg === 'string' && filteredLogs.some(l => arg.includes(l))) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
/** Compile JS for older browsers. Requires @web/dev-server-esbuild plugin */
|
|
25
|
+
// esbuildTarget: 'auto',
|
|
26
|
+
|
|
27
|
+
/** Amount of browsers to run concurrently */
|
|
28
|
+
// concurrentBrowsers: 2,
|
|
29
|
+
|
|
30
|
+
/** Amount of test files per browser to test concurrently */
|
|
31
|
+
// concurrency: 1,
|
|
32
|
+
|
|
33
|
+
/** Browsers to run tests on */
|
|
34
|
+
// browsers: [
|
|
35
|
+
// playwrightLauncher({ product: 'chromium' }),
|
|
36
|
+
// playwrightLauncher({ product: 'firefox' }),
|
|
37
|
+
// playwrightLauncher({ product: 'webkit' }),
|
|
38
|
+
// ],
|
|
39
|
+
|
|
40
|
+
// See documentation for all available options
|
|
41
|
+
});
|