@farm.js/bot 0.0.0-stage → 0.1.0-beta.2

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 (66) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +240 -2
  3. package/dist/build-mode.d.ts +3 -0
  4. package/dist/build-mode.d.ts.map +1 -0
  5. package/dist/build-mode.js +4 -0
  6. package/dist/captcha/index.d.ts +4 -0
  7. package/dist/captcha/index.d.ts.map +1 -0
  8. package/dist/captcha/index.js +2 -0
  9. package/dist/captcha/providers.d.ts +11 -0
  10. package/dist/captcha/providers.d.ts.map +1 -0
  11. package/dist/captcha/providers.js +90 -0
  12. package/dist/captcha/siteverify.d.ts +37 -0
  13. package/dist/captcha/siteverify.d.ts.map +1 -0
  14. package/dist/captcha/siteverify.js +88 -0
  15. package/dist/captcha/widget.d.ts +19 -0
  16. package/dist/captcha/widget.d.ts.map +1 -0
  17. package/dist/captcha/widget.js +52 -0
  18. package/dist/client.d.ts +69 -0
  19. package/dist/client.d.ts.map +1 -0
  20. package/dist/client.js +235 -0
  21. package/dist/csp.d.ts +15 -0
  22. package/dist/csp.d.ts.map +1 -0
  23. package/dist/csp.js +99 -0
  24. package/dist/errors.d.ts +10 -0
  25. package/dist/errors.d.ts.map +1 -0
  26. package/dist/errors.js +9 -0
  27. package/dist/index.d.ts +28 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +128 -0
  30. package/dist/keys.d.ts +44 -0
  31. package/dist/keys.d.ts.map +1 -0
  32. package/dist/keys.js +67 -0
  33. package/dist/preact.d.ts +10 -0
  34. package/dist/preact.d.ts.map +1 -0
  35. package/dist/preact.js +31 -0
  36. package/dist/provider-widget.d.ts +3 -0
  37. package/dist/provider-widget.d.ts.map +1 -0
  38. package/dist/provider-widget.js +5 -0
  39. package/dist/provider.d.ts +70 -0
  40. package/dist/provider.d.ts.map +1 -0
  41. package/dist/provider.js +83 -0
  42. package/dist/react.d.ts +8 -0
  43. package/dist/react.d.ts.map +1 -0
  44. package/dist/react.js +31 -0
  45. package/dist/runtime.d.ts +5 -0
  46. package/dist/runtime.d.ts.map +1 -0
  47. package/dist/runtime.js +8 -0
  48. package/dist/server.d.ts +9 -0
  49. package/dist/server.d.ts.map +1 -0
  50. package/dist/server.js +14 -0
  51. package/dist/solid.d.ts +9 -0
  52. package/dist/solid.d.ts.map +1 -0
  53. package/dist/solid.js +34 -0
  54. package/dist/svelte.d.ts +12 -0
  55. package/dist/svelte.d.ts.map +1 -0
  56. package/dist/svelte.js +22 -0
  57. package/dist/testing.d.ts +22 -0
  58. package/dist/testing.d.ts.map +1 -0
  59. package/dist/testing.js +102 -0
  60. package/dist/verify.d.ts +39 -0
  61. package/dist/verify.d.ts.map +1 -0
  62. package/dist/verify.js +113 -0
  63. package/dist/vue.d.ts +20 -0
  64. package/dist/vue.d.ts.map +1 -0
  65. package/dist/vue.js +32 -0
  66. package/package.json +122 -3
@@ -0,0 +1,39 @@
1
+ import { BotError } from "./errors.js";
2
+ import { type BotFailureCode, type BotProvider } from "./provider.js";
3
+ export interface BotCheckVerifyOptions {
4
+ /** The request the form arrived with. Defaults to the request being handled. */
5
+ request?: Request;
6
+ /** The visitor's IP, forwarded to the provider. Omit it unless your proxy sets it reliably. */
7
+ remoteIp?: string;
8
+ }
9
+ export interface BotCheckSuccess<TData = unknown> {
10
+ success: true;
11
+ provider: string;
12
+ /** The hostname the provider saw the check on, when it reports one. */
13
+ hostname: string | undefined;
14
+ /** Whether the check ran on the provider's test keys. */
15
+ testKeys: boolean;
16
+ /** Provider details, such as a score or the raw verify response. */
17
+ data: TData | undefined;
18
+ }
19
+ export interface BotCheckFailure {
20
+ success: false;
21
+ provider: string;
22
+ code: BotFailureCode;
23
+ /** A sentence that is safe to show the visitor. */
24
+ message: string;
25
+ /** The HTTP status to answer with: 403, or 503 when the provider can't be reached. */
26
+ status: 403 | 503;
27
+ /** The provider's raw error codes, for logs. */
28
+ providerCodes: readonly string[];
29
+ }
30
+ export type BotCheckResult<TData = unknown> = BotCheckSuccess<TData> | BotCheckFailure;
31
+ export interface BotCheckVerifySettings<TData = unknown> {
32
+ provider: BotProvider<TData>;
33
+ hostnames: readonly string[] | undefined;
34
+ timeoutMs: number;
35
+ }
36
+ export declare function verifyBotCheck<TData>(settings: BotCheckVerifySettings<TData>, input: FormData | string | null | undefined, options?: BotCheckVerifyOptions): Promise<BotCheckResult<TData>>;
37
+ /** Outside development the request's host can't be trusted, so the app must list its own. */
38
+ export declare function missingHostnamesError(): BotError;
39
+ //# sourceMappingURL=verify.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,WAAW,EAEjB,MAAM,eAAe,CAAC;AAKvB,MAAM,WAAW,qBAAqB;IACpC,gFAAgF;IAChF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,+FAA+F;IAC/F,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,eAAe,CAAC,KAAK,GAAG,OAAO;IAC9C,OAAO,EAAE,IAAI,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,yDAAyD;IACzD,QAAQ,EAAE,OAAO,CAAC;IAClB,oEAAoE;IACpE,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;CACzB;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,KAAK,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,cAAc,CAAC;IACrB,mDAAmD;IACnD,OAAO,EAAE,MAAM,CAAC;IAChB,sFAAsF;IACtF,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;IAClB,gDAAgD;IAChD,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;CAClC;AAED,MAAM,MAAM,cAAc,CAAC,KAAK,GAAG,OAAO,IAAI,eAAe,CAAC,KAAK,CAAC,GAAG,eAAe,CAAC;AAEvF,MAAM,WAAW,sBAAsB,CAAC,KAAK,GAAG,OAAO;IACrD,QAAQ,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;IAC7B,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC;IACzC,SAAS,EAAE,MAAM,CAAC;CACnB;AAUD,wBAAsB,cAAc,CAAC,KAAK,EACxC,QAAQ,EAAE,sBAAsB,CAAC,KAAK,CAAC,EACvC,KAAK,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAC3C,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAiEhC;AAkCD,6FAA6F;AAC7F,wBAAgB,qBAAqB,IAAI,QAAQ,CAKhD"}
package/dist/verify.js ADDED
@@ -0,0 +1,113 @@
1
+ import { getCurrentRequest } from "@farm.js/core/request";
2
+ import { BotError } from "./errors.js";
3
+ import { isDevelopment } from "./keys.js";
4
+ import { BOT_FAILURE_CODES, } from "./provider.js";
5
+ // A provider with a documented limit sets its own. This stops junk from being forwarded.
6
+ const DEFAULT_MAX_TOKEN_LENGTH = 32_768;
7
+ const MESSAGES = {
8
+ missing_token: "Complete the bot check, then send the form again.",
9
+ rejected: "The bot check failed. Try again.",
10
+ expired: "The bot check expired. Complete it again, then send the form again.",
11
+ hostname_mismatch: "The bot check failed. Try again.",
12
+ unavailable: "The bot check is unavailable right now. Try again in a moment.",
13
+ };
14
+ export async function verifyBotCheck(settings, input, options = {}) {
15
+ const { provider } = settings;
16
+ const fail = (code, providerCodes = []) => failure(provider.name, code, providerCodes);
17
+ // `farm build` reports this from `configure`; this covers a server that skipped that check.
18
+ if (settings.hostnames === undefined && !isDevelopment())
19
+ throw missingHostnamesError();
20
+ const form = isForm(input) ? input : undefined;
21
+ const token = typeof input === "string" ? input : readToken(form, provider.tokenField);
22
+ if (provider.tokenField !== undefined) {
23
+ if (token === undefined || token.length === 0)
24
+ return fail("missing_token");
25
+ if (token.length > (provider.maxTokenLength ?? DEFAULT_MAX_TOKEN_LENGTH)) {
26
+ return fail("rejected");
27
+ }
28
+ }
29
+ const request = options.request ?? currentRequest();
30
+ const signal = AbortSignal.timeout(settings.timeoutMs);
31
+ let verdict;
32
+ try {
33
+ verdict = await raceAbort(() => provider.verify({ token, form, request, remoteIp: options.remoteIp, signal }), signal);
34
+ }
35
+ catch (error) {
36
+ // A timed-out check refuses the submission. Anything else is the provider reporting a bug.
37
+ if (signal.aborted)
38
+ return fail("unavailable", ["timeout"]);
39
+ throw error;
40
+ }
41
+ if (verdict?.success === false) {
42
+ const code = BOT_FAILURE_CODES.includes(verdict.code) ? verdict.code : "rejected";
43
+ return fail(code, verdict.providerCodes);
44
+ }
45
+ if (verdict?.success !== true) {
46
+ throw new BotError("invalid_provider", `Bot provider "${provider.name}" returned an invalid result. \`verify\` must return \`{ success: true }\` or \`{ success: false, code }\`.`);
47
+ }
48
+ if (verdict.hostname !== undefined && typeof verdict.hostname !== "string") {
49
+ throw new BotError("invalid_provider", `Bot provider "${provider.name}" returned an invalid \`hostname\`. Return a string, or leave it out when the provider doesn't report one.`);
50
+ }
51
+ const hostname = verdict.hostname?.toLowerCase();
52
+ if (hostname !== undefined) {
53
+ const allowed = settings.hostnames ?? [requestHostname(request)];
54
+ if (!allowed.some((candidate) => candidate.toLowerCase() === hostname)) {
55
+ return fail("hostname_mismatch");
56
+ }
57
+ }
58
+ return {
59
+ success: true,
60
+ provider: provider.name,
61
+ hostname,
62
+ testKeys: verdict.testKeys === true,
63
+ data: verdict.data,
64
+ };
65
+ }
66
+ // A JSON API can hand over anything a visitor sent, so don't assume the declared type.
67
+ function isForm(input) {
68
+ return (typeof input === "object" && input !== null && typeof input.get === "function");
69
+ }
70
+ function readToken(form, tokenField) {
71
+ if (!form || tokenField === undefined)
72
+ return undefined;
73
+ const value = form.get(tokenField);
74
+ return typeof value === "string" ? value : undefined;
75
+ }
76
+ /** Enforce the timeout even for a provider that ignores its `signal`. */
77
+ function raceAbort(run, signal) {
78
+ return Promise.race([
79
+ new Promise((resolve) => resolve(run())),
80
+ new Promise((_, reject) => {
81
+ signal.addEventListener("abort", () => reject(signal.reason), { once: true });
82
+ }),
83
+ ]);
84
+ }
85
+ function currentRequest() {
86
+ try {
87
+ return getCurrentRequest();
88
+ }
89
+ catch {
90
+ // Outside a request, the hostname check reports what to pass instead.
91
+ return undefined;
92
+ }
93
+ }
94
+ /** Outside development the request's host can't be trusted, so the app must list its own. */
95
+ export function missingHostnamesError() {
96
+ return new BotError("missing_hostnames", '`bot()` needs `hostnames` outside development, for example `hostnames: ["example.com"]`. A request can claim any host, so a production check only trusts the hostnames you list.');
97
+ }
98
+ function requestHostname(request) {
99
+ if (!request) {
100
+ throw new BotError("missing_request", "Checking a bot token needs the request, to see where the token came from. Pass `{ request }`, or set `hostnames` in `bot()`.");
101
+ }
102
+ return new URL(request.url).hostname;
103
+ }
104
+ function failure(provider, code, providerCodes) {
105
+ return {
106
+ success: false,
107
+ provider,
108
+ code,
109
+ message: MESSAGES[code],
110
+ status: code === "unavailable" ? 503 : 403,
111
+ providerCodes,
112
+ };
113
+ }
package/dist/vue.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { type PropType } from "vue";
2
+ import { type BotTheme, type BotWidgetOptions } from "./client.js";
3
+ export type { BotCheckHandle } from "./client.js";
4
+ /** The bot check widget. Put it inside the form it protects. */
5
+ export declare const BotCheck: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
6
+ theme: PropType<BotTheme>;
7
+ settings: PropType<BotWidgetOptions["settings"]>;
8
+ onVerify: PropType<BotWidgetOptions["onVerify"]>;
9
+ onError: PropType<BotWidgetOptions["onError"]>;
10
+ onExpire: PropType<BotWidgetOptions["onExpire"]>;
11
+ }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
12
+ [key: string]: any;
13
+ }>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
14
+ theme: PropType<BotTheme>;
15
+ settings: PropType<BotWidgetOptions["settings"]>;
16
+ onVerify: PropType<BotWidgetOptions["onVerify"]>;
17
+ onError: PropType<BotWidgetOptions["onError"]>;
18
+ onExpire: PropType<BotWidgetOptions["onExpire"]>;
19
+ }>> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
20
+ //# sourceMappingURL=vue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vue.d.ts","sourceRoot":"","sources":["../src/vue.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8D,KAAK,QAAQ,EAAE,MAAM,KAAK,CAAC;AAChG,OAAO,EAAiB,KAAK,QAAQ,EAAkB,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAElG,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,gEAAgE;AAChE,eAAO,MAAM,QAAQ;WAGA,QAAQ,CAAC,QAAQ,CAAC;cACf,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;cAEpC,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;aACvC,QAAQ,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC;cACpC,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;;;;WAL3C,QAAQ,CAAC,QAAQ,CAAC;cACf,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;cAEpC,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;aACvC,QAAQ,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC;cACpC,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;iGAyB9D,CAAC"}
package/dist/vue.js ADDED
@@ -0,0 +1,32 @@
1
+ import { defineComponent, h, onBeforeUnmount, onMounted, ref, watch } from "vue";
2
+ import { mountBotCheck } from "./client.js";
3
+ /** The bot check widget. Put it inside the form it protects. */
4
+ export const BotCheck = defineComponent({
5
+ name: "BotCheck",
6
+ props: {
7
+ theme: String,
8
+ settings: Object,
9
+ // Declared as props so `@verify`, `@error`, and `@expire` arrive as `onVerify` and friends.
10
+ onVerify: Function,
11
+ onError: Function,
12
+ onExpire: Function,
13
+ },
14
+ setup(props, { expose }) {
15
+ const element = ref();
16
+ let widget;
17
+ const mount = () => {
18
+ if (element.value)
19
+ widget = mountBotCheck(element.value, () => props);
20
+ };
21
+ onMounted(mount);
22
+ // Compare a string, because an inline `settings` object is new on every parent render.
23
+ watch(() => JSON.stringify([props.theme, props.settings ?? null]), () => {
24
+ widget?.remove();
25
+ props.onExpire?.();
26
+ mount();
27
+ });
28
+ onBeforeUnmount(() => widget?.remove());
29
+ expose({ reset: () => widget?.reset() });
30
+ return () => h("div", { ref: element, "data-farm-bot": "" });
31
+ },
32
+ });
package/package.json CHANGED
@@ -1,6 +1,125 @@
1
1
  {
2
2
  "name": "@farm.js/bot",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-beta.2",
4
+ "description": "Stop bots at your forms, with any checker",
5
+ "keywords": [
6
+ "bot-protection",
7
+ "captcha",
8
+ "farm.js",
9
+ "hcaptcha",
10
+ "recaptcha",
11
+ "turnstile"
12
+ ],
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/farming-labs/farm.js",
17
+ "directory": "packages/farm-bot"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "type": "module",
23
+ "sideEffects": false,
24
+ "main": "./dist/index.js",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ },
32
+ "./client": {
33
+ "types": "./dist/client.d.ts",
34
+ "import": "./dist/client.js"
35
+ },
36
+ "./captcha": {
37
+ "types": "./dist/captcha/index.d.ts",
38
+ "import": "./dist/captcha/index.js"
39
+ },
40
+ "./captcha/widget": {
41
+ "types": "./dist/captcha/widget.d.ts",
42
+ "import": "./dist/captcha/widget.js"
43
+ },
44
+ "./provider-widget": {
45
+ "types": "./dist/provider-widget.d.ts",
46
+ "import": "./dist/provider-widget.js"
47
+ },
48
+ "./server": {
49
+ "types": "./dist/server.d.ts",
50
+ "import": "./dist/server.js"
51
+ },
52
+ "./testing": {
53
+ "types": "./dist/testing.d.ts",
54
+ "import": "./dist/testing.js"
55
+ },
56
+ "./react": {
57
+ "types": "./dist/react.d.ts",
58
+ "import": "./dist/react.js"
59
+ },
60
+ "./preact": {
61
+ "types": "./dist/preact.d.ts",
62
+ "import": "./dist/preact.js"
63
+ },
64
+ "./vue": {
65
+ "types": "./dist/vue.d.ts",
66
+ "import": "./dist/vue.js"
67
+ },
68
+ "./solid": {
69
+ "types": "./dist/solid.d.ts",
70
+ "import": "./dist/solid.js"
71
+ },
72
+ "./svelte": {
73
+ "types": "./dist/svelte.d.ts",
74
+ "import": "./dist/svelte.js"
75
+ }
76
+ },
77
+ "publishConfig": {
78
+ "access": "public"
79
+ },
80
+ "dependencies": {
81
+ "@farm.js/core": "0.1.68"
82
+ },
83
+ "devDependencies": {
84
+ "@types/node": "^22.20.5",
85
+ "@types/react": "^19.0.0",
86
+ "jsdom": "^25.0.0",
87
+ "preact": "10.29.8",
88
+ "react": "19.2.8",
89
+ "react-dom": "19.2.8",
90
+ "solid-js": "1.9.18",
91
+ "typescript": "^5.3.3",
92
+ "vite": "8.3.4",
93
+ "vitest": "5.0.5",
94
+ "vue": "3.5.44"
95
+ },
96
+ "peerDependencies": {
97
+ "preact": "^10.0.0",
98
+ "react": "^18.3.0 || ^19.0.0",
99
+ "solid-js": "^1.9.0",
100
+ "vue": "^3.5.0"
101
+ },
102
+ "peerDependenciesMeta": {
103
+ "preact": {
104
+ "optional": true
105
+ },
106
+ "react": {
107
+ "optional": true
108
+ },
109
+ "solid-js": {
110
+ "optional": true
111
+ },
112
+ "vue": {
113
+ "optional": true
114
+ }
115
+ },
116
+ "engines": {
117
+ "node": ">=22.13.0"
118
+ },
119
+ "scripts": {
120
+ "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
121
+ "dev": "tsc --watch",
122
+ "test": "vitest run",
123
+ "type-check": "tsc --noEmit"
124
+ }
6
125
  }