@internetarchive/recaptcha-manager 2.0.0 → 2.0.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.
Files changed (58) hide show
  1. package/.claude/settings.local.json +7 -0
  2. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.editorconfig +29 -0
  3. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.github/workflows/ci.yml +27 -0
  4. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.github/workflows/gh-pages-main.yml +39 -0
  5. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.github/workflows/pr-preview.yml +63 -0
  6. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.husky/pre-commit +1 -0
  7. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/.prettierignore +1 -0
  8. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/LICENSE +661 -0
  9. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/README.md +63 -0
  10. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/coverage/.gitignore +3 -0
  11. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/coverage/.placeholder +0 -0
  12. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/demo/app-root.ts +111 -0
  13. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/demo/index.html +21 -0
  14. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/eslint.config.mjs +53 -0
  15. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/index.ts +10 -0
  16. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/package-lock.json +11336 -0
  17. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/package.json +76 -0
  18. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/renovate.json +6 -0
  19. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/src/recaptcha-manager.ts +114 -0
  20. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/src/recaptcha-widget.ts +148 -0
  21. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/ssl/server.crt +22 -0
  22. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/ssl/server.key +28 -0
  23. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/test/mock-grecaptcha.ts +85 -0
  24. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/test/mock-lazy-loader.ts +36 -0
  25. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/test/recaptcha-manager.test.ts +201 -0
  26. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/tsconfig.json +31 -0
  27. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/vite.config.ts +25 -0
  28. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/web-dev-server.config.mjs +32 -0
  29. package/.claude/worktrees/recaptcha-google-url-hardcode-79a528/web-test-runner.config.mjs +41 -0
  30. package/.editorconfig +29 -29
  31. package/.github/workflows/ci.yml +27 -27
  32. package/.github/workflows/gh-pages-main.yml +39 -39
  33. package/.github/workflows/pr-preview.yml +63 -63
  34. package/.husky/pre-commit +1 -1
  35. package/.prettierignore +1 -1
  36. package/LICENSE +661 -661
  37. package/README.md +63 -63
  38. package/coverage/.gitignore +2 -2
  39. package/demo/app-root.ts +111 -111
  40. package/demo/index.html +21 -21
  41. package/dist/demo/app-root.js +31 -31
  42. package/dist/demo/app-root.js.map +1 -1
  43. package/dist/src/recaptcha-manager.d.ts +2 -1
  44. package/dist/src/recaptcha-manager.js +3 -2
  45. package/dist/src/recaptcha-manager.js.map +1 -1
  46. package/dist/test/mock-lazy-loader.js.map +1 -1
  47. package/dist/test/recaptcha-manager.test.js +1 -1
  48. package/dist/test/recaptcha-manager.test.js.map +1 -1
  49. package/eslint.config.mjs +53 -53
  50. package/package.json +76 -76
  51. package/renovate.json +6 -6
  52. package/src/recaptcha-manager.ts +114 -113
  53. package/ssl/server.key +28 -28
  54. package/test/mock-lazy-loader.ts +36 -36
  55. package/test/recaptcha-manager.test.ts +201 -199
  56. package/tsconfig.json +31 -31
  57. package/web-dev-server.config.mjs +32 -32
  58. package/web-test-runner.config.mjs +41 -41
@@ -0,0 +1,63 @@
1
+ ![Build Status](https://github.com/internetarchive/iaux-recaptcha-manager/actions/workflows/ci.yml/badge.svg)
2
+
3
+ # Internet Archive reCaptcha Manager
4
+
5
+ A library to lazy load and interact with reCaptcha
6
+
7
+ ## Installation
8
+
9
+ ```shell
10
+ > npm install @internetarchive/recaptcha-manager
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ const recaptchaManager = new RecaptchaManager({
17
+ defaultSiteKey: 'your-site-key',
18
+ });
19
+
20
+ // will load recaptcha library if it's not loaded
21
+ const recaptchaWidget = await recaptchaManager.getRecaptchaWidget({
22
+ recaptchaParams: {
23
+ tabindex: 0,
24
+ theme: 'light',
25
+ type: 'image',
26
+ },
27
+ });
28
+
29
+ const token = await recaptchaWidget.execute();
30
+
31
+ // submit token with your post and validate on the backend
32
+ ```
33
+
34
+ For more usage examples, see `demo/app-root.ts` and `test/recaptcha-manager.test.ts`.
35
+
36
+ ## Local Demo with `web-dev-server`
37
+ ```bash
38
+ npm run start
39
+ ```
40
+ To run a local development server that serves the basic demo located in `demo/index.html`
41
+
42
+ ## Testing with Web Test Runner
43
+ To run the suite of Web Test Runner tests, run
44
+ ```bash
45
+ npm run test
46
+ ```
47
+
48
+ To run the tests in watch mode (for <abbr title="test driven development">TDD</abbr>, for example), run
49
+
50
+ ```bash
51
+ npm run test:watch
52
+ ```
53
+
54
+ ## Linting with ESLint, Prettier, and Types
55
+ To scan the project for linting errors, run
56
+ ```bash
57
+ npm run lint
58
+ ```
59
+
60
+ To automatically fix many linting errors, run
61
+ ```bash
62
+ npm run format
63
+ ```
@@ -0,0 +1,3 @@
1
+ *
2
+ !.placeholder
3
+ !.gitignore
@@ -0,0 +1,111 @@
1
+ import { html, css, LitElement } from 'lit';
2
+ import { customElement, state, query } from 'lit/decorators.js';
3
+ import { RecaptchaWidgetInterface } from '../src/recaptcha-widget';
4
+ import {
5
+ RecaptchaManager,
6
+ RecaptchaManagerInterface,
7
+ } from '../src/recaptcha-manager';
8
+
9
+ @customElement('app-root')
10
+ export class AppRoot extends LitElement {
11
+ @state() result?: string;
12
+
13
+ @state() result2?: string;
14
+
15
+ @query('#timeout') timeoutInput!: HTMLInputElement;
16
+
17
+ private recaptchaManager: RecaptchaManagerInterface = new RecaptchaManager({
18
+ defaultSiteKey: '6Ld64a8UAAAAAGbDwi1927ztGNw7YABQ-dqzvTN2',
19
+ });
20
+
21
+ private recaptcha1?: RecaptchaWidgetInterface;
22
+
23
+ private recaptcha2?: RecaptchaWidgetInterface;
24
+
25
+ render() {
26
+ return html`
27
+ <label for="timeout">
28
+ Timeout (ms):
29
+ <input
30
+ type="number"
31
+ id="timeout"
32
+ value="10000"
33
+ min="0"
34
+ max="99999"
35
+ step="100"
36
+ @change=${this.updateTimeout}
37
+ />
38
+ </label>
39
+ <p>
40
+ <button @click="${this.loadRecaptcha}">Load Recaptcha</button
41
+ ><button @click="${this.executeRecaptcha}">Execute Recaptcha</button>
42
+ </p>
43
+ ${this.result
44
+ ? html`<p><strong>Token:</strong></p>
45
+ <p>${this.result}</p>`
46
+ : ''}
47
+ <p>
48
+ <button @click="${this.loadRecaptcha2}">Load Another Recaptcha</button
49
+ ><button @click="${this.executeRecaptcha2}">
50
+ Execute Another Recaptcha
51
+ </button>
52
+ </p>
53
+ ${this.result2
54
+ ? html`<p><strong>Token 2:</strong></p>
55
+ <p>${this.result2}</p>`
56
+ : ''}
57
+ `;
58
+ }
59
+
60
+ private updateTimeout() {
61
+ this.recaptchaManager.setTimeoutDelay(+this.timeoutInput.value);
62
+ }
63
+
64
+ private async loadRecaptcha() {
65
+ this.recaptcha1 = await this.recaptchaManager?.getRecaptchaWidget({
66
+ recaptchaParams: {
67
+ tabindex: 0,
68
+ theme: 'light',
69
+ type: 'image',
70
+ },
71
+ });
72
+ }
73
+
74
+ private async executeRecaptcha() {
75
+ if (!this.recaptcha1) {
76
+ await this.loadRecaptcha();
77
+ }
78
+
79
+ if (!this.recaptcha1) {
80
+ console.error('error loading recaptcha');
81
+ return;
82
+ }
83
+
84
+ this.result = await this.recaptcha1.execute();
85
+ }
86
+
87
+ private async loadRecaptcha2() {
88
+ this.recaptcha2 = await this.recaptchaManager?.getRecaptchaWidget({
89
+ siteKey: '<your-site-key>',
90
+ });
91
+ }
92
+
93
+ private async executeRecaptcha2() {
94
+ if (!this.recaptcha2) {
95
+ await this.loadRecaptcha();
96
+ }
97
+
98
+ if (!this.recaptcha2) {
99
+ console.error('error loading recaptcha');
100
+ return;
101
+ }
102
+
103
+ this.result2 = await this.recaptcha2.execute();
104
+ }
105
+
106
+ static styles = css`
107
+ :host {
108
+ display: block;
109
+ }
110
+ `;
111
+ }
@@ -0,0 +1,21 @@
1
+ <!doctype html>
2
+ <html lang="en-GB">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <style>
6
+ html {
7
+ font-size: 10px; /* This is to match petabox's base font size */
8
+ }
9
+
10
+ body {
11
+ background: #fafafa;
12
+ }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <app-root></app-root>
17
+
18
+ <script type="module" src="../dist/demo/app-root.js">
19
+ </script>
20
+ </body>
21
+ </html>
@@ -0,0 +1,53 @@
1
+ import typescriptEslint from '@typescript-eslint/eslint-plugin';
2
+ import html from 'eslint-plugin-html';
3
+ import tsParser from '@typescript-eslint/parser';
4
+ import path from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ import js from '@eslint/js';
7
+ import { FlatCompat } from '@eslint/eslintrc';
8
+
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = path.dirname(__filename);
11
+ const compat = new FlatCompat({
12
+ baseDirectory: __dirname,
13
+ recommendedConfig: js.configs.recommended,
14
+ allConfig: js.configs.all,
15
+ });
16
+
17
+ export default [
18
+ ...compat.extends('plugin:@typescript-eslint/recommended'),
19
+ {
20
+ plugins: {
21
+ '@typescript-eslint': typescriptEslint,
22
+ html,
23
+ },
24
+
25
+ languageOptions: {
26
+ parser: tsParser,
27
+ },
28
+
29
+ settings: {
30
+ 'import/resolver': {
31
+ node: {
32
+ extensions: ['.ts', '.tsx'],
33
+ moduleDirectory: ['node_modules', 'src', 'demo'],
34
+ },
35
+ },
36
+ },
37
+
38
+ rules: {
39
+ '@typescript-eslint/no-unsafe-function-type': 'warn',
40
+ '@typescript-eslint/no-unused-vars': 'warn',
41
+ '@typescript-eslint/no-explicit-any': 'warn',
42
+ },
43
+ },
44
+ {
45
+ ignores: ['**/*.js', '**/*.mjs'],
46
+ },
47
+ {
48
+ files: ['**/*.test.ts'],
49
+ rules: {
50
+ '@typescript-eslint/no-unused-expressions': 'off',
51
+ },
52
+ },
53
+ ];
@@ -0,0 +1,10 @@
1
+ export {
2
+ RecaptchaManager,
3
+ RecaptchaManagerInterface,
4
+ } from './src/recaptcha-manager';
5
+
6
+ export {
7
+ RecaptchaWidget,
8
+ RecaptchaWidgetInterface,
9
+ RecaptchaWidgetConfig,
10
+ } from './src/recaptcha-widget';