@internetarchive/recaptcha-manager 0.1.0 → 0.1.2-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 -29
- package/.github/workflows/ci.yml +26 -26
- package/.husky/pre-commit +4 -4
- package/LICENSE +661 -661
- package/README.md +85 -85
- package/demo/app-root.ts +93 -93
- package/demo/index.html +21 -21
- package/dist/demo/app-root.d.ts +14 -14
- package/dist/demo/app-root.js +85 -85
- package/dist/demo/app-root.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/src/recaptcha-manager.d.ts +35 -35
- package/dist/src/recaptcha-manager.js +52 -52
- package/dist/src/recaptcha-manager.js.map +1 -1
- package/dist/src/recaptcha-widget.d.ts +39 -39
- package/dist/src/recaptcha-widget.js +95 -95
- package/dist/src/recaptcha-widget.js.map +1 -1
- package/dist/test/mock-grecaptcha.d.ts +24 -24
- package/dist/test/mock-grecaptcha.js +61 -61
- package/dist/test/mock-grecaptcha.js.map +1 -1
- package/dist/test/mock-lazy-loader.d.ts +15 -15
- package/dist/test/mock-lazy-loader.js +16 -16
- package/dist/test/mock-lazy-loader.js.map +1 -1
- package/dist/test/recaptcha-manager.test.d.ts +1 -1
- package/dist/test/recaptcha-manager.test.js +137 -137
- package/dist/test/recaptcha-manager.test.js.map +1 -1
- package/index.ts +10 -10
- package/package.json +98 -97
- package/src/recaptcha-manager.ts +87 -87
- package/src/recaptcha-widget.ts +148 -148
- package/test/mock-grecaptcha.ts +87 -87
- package/test/mock-lazy-loader.ts +38 -38
- package/test/recaptcha-manager.test.ts +151 -151
- package/tsconfig.json +20 -20
- package/web-dev-server.config.mjs +28 -28
- package/web-test-runner.config.mjs +41 -41
- package/dist/src/recaptcha-manager copy.d.ts +0 -82
- package/dist/src/recaptcha-manager copy.js +0 -140
- package/dist/src/recaptcha-manager copy.js.map +0 -1
- package/dist/src/recaptcha-manager-bak.d.ts +0 -82
- package/dist/src/recaptcha-manager-bak.js +0 -140
- package/dist/src/recaptcha-manager-bak.js.map +0 -1
- package/dist/src/recaptcha.d.ts +0 -28
- package/dist/src/recaptcha.js +0 -87
- package/dist/src/recaptcha.js.map +0 -1
- package/dist/test/your-webcomponent.test.d.ts +0 -0
- package/dist/test/your-webcomponent.test.js +0 -4
- package/dist/test/your-webcomponent.test.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,97 +1,98 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@internetarchive/recaptcha-manager",
|
|
3
|
-
"description": "A library that lets you lazy load and interact with reCaptcha.",
|
|
4
|
-
"repository": {
|
|
5
|
-
"type": "git",
|
|
6
|
-
"url": "git+https://github.com/internetarchive/iaux-recaptcha-manager.git"
|
|
7
|
-
},
|
|
8
|
-
"license": "AGPL-3.0-only",
|
|
9
|
-
"author": "Internet Archive",
|
|
10
|
-
"version": "0.1.
|
|
11
|
-
"main": "dist/index.js",
|
|
12
|
-
"module": "dist/index.js",
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"test
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
"@
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"@open-wc/
|
|
31
|
-
"@
|
|
32
|
-
"@typescript-eslint/
|
|
33
|
-
"@
|
|
34
|
-
"@web/
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"eslint
|
|
38
|
-
"eslint-
|
|
39
|
-
"eslint-plugin-
|
|
40
|
-
"eslint-plugin-
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"
|
|
76
|
-
"import/
|
|
77
|
-
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@internetarchive/recaptcha-manager",
|
|
3
|
+
"description": "A library that lets you lazy load and interact with reCaptcha.",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/internetarchive/iaux-recaptcha-manager.git"
|
|
7
|
+
},
|
|
8
|
+
"license": "AGPL-3.0-only",
|
|
9
|
+
"author": "Internet Archive",
|
|
10
|
+
"version": "0.1.2-alpha.1",
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"module": "dist/index.js",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"start": "tsc && concurrently -k -r \"tsc --watch --preserveWatchOutput\" \"wds\"",
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"prepare": "tsc && husky install",
|
|
18
|
+
"lint": "eslint --ext .ts,.html . --ignore-path .gitignore && prettier \"**/*.ts\" --check --ignore-path .gitignore",
|
|
19
|
+
"format": "eslint --ext .ts,.html . --fix --ignore-path .gitignore && prettier \"**/*.ts\" --write --ignore-path .gitignore",
|
|
20
|
+
"circular": "madge --circular --extensions ts .",
|
|
21
|
+
"test": "tsc && yarn run lint && yarn run circular && wtr --coverage",
|
|
22
|
+
"test:watch": "tsc && concurrently -k -r \"tsc --watch --preserveWatchOutput\" \"wtr --watch\""
|
|
23
|
+
},
|
|
24
|
+
"types": "dist/index.d.ts",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@internetarchive/lazy-loader-service": "^0.2.1-alpha.1",
|
|
27
|
+
"@types/grecaptcha": "^2"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@open-wc/eslint-config": "^7.0.0",
|
|
31
|
+
"@open-wc/testing": "^3.0.3",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "^5.3.1",
|
|
33
|
+
"@typescript-eslint/parser": "^5.3.1",
|
|
34
|
+
"@web/dev-server": "^0.1.28",
|
|
35
|
+
"@web/test-runner": "^0.13.22",
|
|
36
|
+
"concurrently": "^6.3.0",
|
|
37
|
+
"eslint": "^8.2.0",
|
|
38
|
+
"eslint-config-prettier": "^8.3.0",
|
|
39
|
+
"eslint-plugin-import": "^2.25.3",
|
|
40
|
+
"eslint-plugin-lit-a11y": "^2.2.0",
|
|
41
|
+
"eslint-plugin-wc": "^1.3.2",
|
|
42
|
+
"husky": "^7.0.0",
|
|
43
|
+
"lit": "^2.2.0",
|
|
44
|
+
"madge": "^5.0.1",
|
|
45
|
+
"nanoevents": "^6.0.2",
|
|
46
|
+
"prettier": "^2.4.1",
|
|
47
|
+
"sinon": "^12.0.1",
|
|
48
|
+
"tslib": "^2.3.1",
|
|
49
|
+
"typescript": "^4.4.4"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"eslintConfig": {
|
|
55
|
+
"parser": "@typescript-eslint/parser",
|
|
56
|
+
"extends": [
|
|
57
|
+
"@open-wc",
|
|
58
|
+
"prettier"
|
|
59
|
+
],
|
|
60
|
+
"plugins": [
|
|
61
|
+
"@typescript-eslint"
|
|
62
|
+
],
|
|
63
|
+
"globals": {
|
|
64
|
+
"ReCaptchaV2": "readonly"
|
|
65
|
+
},
|
|
66
|
+
"rules": {
|
|
67
|
+
"no-unused-vars": "off",
|
|
68
|
+
"@typescript-eslint/no-unused-vars": [
|
|
69
|
+
"error"
|
|
70
|
+
],
|
|
71
|
+
"no-shadow": "off",
|
|
72
|
+
"@typescript-eslint/no-shadow": [
|
|
73
|
+
"error"
|
|
74
|
+
],
|
|
75
|
+
"class-methods-use-this": "off",
|
|
76
|
+
"import/no-unresolved": "off",
|
|
77
|
+
"import/extensions": [
|
|
78
|
+
"off",
|
|
79
|
+
"ignorePackages",
|
|
80
|
+
{
|
|
81
|
+
"js": "never",
|
|
82
|
+
"ts": "never"
|
|
83
|
+
}
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"prettier": {
|
|
88
|
+
"singleQuote": true,
|
|
89
|
+
"arrowParens": "avoid"
|
|
90
|
+
},
|
|
91
|
+
"lint-staged": {
|
|
92
|
+
"*.ts": [
|
|
93
|
+
"eslint --fix",
|
|
94
|
+
"prettier --write",
|
|
95
|
+
"git add"
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
}
|
package/src/recaptcha-manager.ts
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LazyLoaderService,
|
|
3
|
-
LazyLoaderServiceInterface,
|
|
4
|
-
} from '@internetarchive/lazy-loader-service';
|
|
5
|
-
import { RecaptchaWidget } from './recaptcha-widget';
|
|
6
|
-
|
|
7
|
-
export interface RecaptchaManagerInterface {
|
|
8
|
-
/**
|
|
9
|
-
* Load a recaptcha widget for a given site key or the default site key.
|
|
10
|
-
*/
|
|
11
|
-
getRecaptchaWidget(options?: {
|
|
12
|
-
siteKey?: string;
|
|
13
|
-
recaptchaParams?: ReCaptchaV2.Parameters;
|
|
14
|
-
}): Promise<
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class RecaptchaManager implements RecaptchaManagerInterface {
|
|
18
|
-
private lazyLoader: LazyLoaderServiceInterface;
|
|
19
|
-
|
|
20
|
-
private defaultSiteKey?: string;
|
|
21
|
-
|
|
22
|
-
private recaptchaCache: Record<string, RecaptchaWidget> = {};
|
|
23
|
-
|
|
24
|
-
constructor(options?: {
|
|
25
|
-
defaultSiteKey?: string;
|
|
26
|
-
lazyLoader?: LazyLoaderServiceInterface;
|
|
27
|
-
grecaptchaLibrary?: ReCaptchaV2.ReCaptcha; // allows dependency injection or will be lazy loaded
|
|
28
|
-
}) {
|
|
29
|
-
this.defaultSiteKey = options?.defaultSiteKey;
|
|
30
|
-
this.lazyLoader = options?.lazyLoader ?? new LazyLoaderService();
|
|
31
|
-
this.grecaptchaLibraryCache = options?.grecaptchaLibrary;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** @inheritdoc */
|
|
35
|
-
async getRecaptchaWidget(options?: {
|
|
36
|
-
siteKey?: string;
|
|
37
|
-
recaptchaParams?: ReCaptchaV2.Parameters;
|
|
38
|
-
}): Promise<
|
|
39
|
-
const key = options?.siteKey ?? this.defaultSiteKey;
|
|
40
|
-
if (!key) {
|
|
41
|
-
throw new Error('The reCaptcha widget requires a site key');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const cached = this.recaptchaCache[key];
|
|
45
|
-
if (cached) return cached;
|
|
46
|
-
|
|
47
|
-
const grecaptchaLibrary = await this.getRecaptchaLibrary();
|
|
48
|
-
const recaptcha = new RecaptchaWidget(
|
|
49
|
-
{
|
|
50
|
-
siteKey: key,
|
|
51
|
-
grecaptchaLibrary,
|
|
52
|
-
},
|
|
53
|
-
options?.recaptchaParams
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
this.recaptchaCache[key] = recaptcha;
|
|
57
|
-
return recaptcha;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Load the Recaptch library from Google.
|
|
62
|
-
*
|
|
63
|
-
* @returns Promise<ReCaptchaV2.ReCaptcha>
|
|
64
|
-
*/
|
|
65
|
-
private async getRecaptchaLibrary(): Promise<ReCaptchaV2.ReCaptcha> {
|
|
66
|
-
if (this.grecaptchaLibraryCache) {
|
|
67
|
-
return this.grecaptchaLibraryCache;
|
|
68
|
-
}
|
|
69
|
-
return new Promise(resolve => {
|
|
70
|
-
(window as any).grecaptchaLoadedCallback = (): void => {
|
|
71
|
-
setTimeout(() => {
|
|
72
|
-
// remove the callback when we're done with it
|
|
73
|
-
delete (window as any).grecaptchaLoadedCallback;
|
|
74
|
-
}, 10);
|
|
75
|
-
this.grecaptchaLibraryCache = window.grecaptcha;
|
|
76
|
-
resolve(window.grecaptcha);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
this.lazyLoader.loadScript({
|
|
80
|
-
src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** don't use directly, use `getRecaptchaLibrary()` */
|
|
86
|
-
private grecaptchaLibraryCache?: ReCaptchaV2.ReCaptcha;
|
|
87
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
LazyLoaderService,
|
|
3
|
+
LazyLoaderServiceInterface,
|
|
4
|
+
} from '@internetarchive/lazy-loader-service';
|
|
5
|
+
import { RecaptchaWidget, RecaptchaWidgetInterface } from './recaptcha-widget.js';
|
|
6
|
+
|
|
7
|
+
export interface RecaptchaManagerInterface {
|
|
8
|
+
/**
|
|
9
|
+
* Load a recaptcha widget for a given site key or the default site key.
|
|
10
|
+
*/
|
|
11
|
+
getRecaptchaWidget(options?: {
|
|
12
|
+
siteKey?: string;
|
|
13
|
+
recaptchaParams?: ReCaptchaV2.Parameters;
|
|
14
|
+
}): Promise<RecaptchaWidgetInterface>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class RecaptchaManager implements RecaptchaManagerInterface {
|
|
18
|
+
private lazyLoader: LazyLoaderServiceInterface;
|
|
19
|
+
|
|
20
|
+
private defaultSiteKey?: string;
|
|
21
|
+
|
|
22
|
+
private recaptchaCache: Record<string, RecaptchaWidget> = {};
|
|
23
|
+
|
|
24
|
+
constructor(options?: {
|
|
25
|
+
defaultSiteKey?: string;
|
|
26
|
+
lazyLoader?: LazyLoaderServiceInterface;
|
|
27
|
+
grecaptchaLibrary?: ReCaptchaV2.ReCaptcha; // allows dependency injection or will be lazy loaded
|
|
28
|
+
}) {
|
|
29
|
+
this.defaultSiteKey = options?.defaultSiteKey;
|
|
30
|
+
this.lazyLoader = options?.lazyLoader ?? new LazyLoaderService();
|
|
31
|
+
this.grecaptchaLibraryCache = options?.grecaptchaLibrary;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @inheritdoc */
|
|
35
|
+
async getRecaptchaWidget(options?: {
|
|
36
|
+
siteKey?: string;
|
|
37
|
+
recaptchaParams?: ReCaptchaV2.Parameters;
|
|
38
|
+
}): Promise<RecaptchaWidgetInterface> {
|
|
39
|
+
const key = options?.siteKey ?? this.defaultSiteKey;
|
|
40
|
+
if (!key) {
|
|
41
|
+
throw new Error('The reCaptcha widget requires a site key');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const cached = this.recaptchaCache[key];
|
|
45
|
+
if (cached) return cached;
|
|
46
|
+
|
|
47
|
+
const grecaptchaLibrary = await this.getRecaptchaLibrary();
|
|
48
|
+
const recaptcha = new RecaptchaWidget(
|
|
49
|
+
{
|
|
50
|
+
siteKey: key,
|
|
51
|
+
grecaptchaLibrary,
|
|
52
|
+
},
|
|
53
|
+
options?.recaptchaParams
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
this.recaptchaCache[key] = recaptcha;
|
|
57
|
+
return recaptcha;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Load the Recaptch library from Google.
|
|
62
|
+
*
|
|
63
|
+
* @returns Promise<ReCaptchaV2.ReCaptcha>
|
|
64
|
+
*/
|
|
65
|
+
private async getRecaptchaLibrary(): Promise<ReCaptchaV2.ReCaptcha> {
|
|
66
|
+
if (this.grecaptchaLibraryCache) {
|
|
67
|
+
return this.grecaptchaLibraryCache;
|
|
68
|
+
}
|
|
69
|
+
return new Promise(resolve => {
|
|
70
|
+
(window as any).grecaptchaLoadedCallback = (): void => {
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
// remove the callback when we're done with it
|
|
73
|
+
delete (window as any).grecaptchaLoadedCallback;
|
|
74
|
+
}, 10);
|
|
75
|
+
this.grecaptchaLibraryCache = window.grecaptcha;
|
|
76
|
+
resolve(window.grecaptcha);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
this.lazyLoader.loadScript({
|
|
80
|
+
src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** don't use directly, use `getRecaptchaLibrary()` */
|
|
86
|
+
private grecaptchaLibraryCache?: ReCaptchaV2.ReCaptcha;
|
|
87
|
+
}
|