@crvouga/mockingbird-service-turnstile

中文文档
1.10.0发布于 2026/11/11MITTypeScriptES Module构建来源已验证近 30 天下载 2331

Deterministic Cloudflare Turnstile Siteverify token validation.

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. Contract
  4. Controls
  5. Deliberately not modelled
  6. API

@crvouga/mockingbird-service-turnstile

Local emulators. Real API contracts. Part of Mockingbird.

WIP Cloudflare Turnstile server-side Siteverify emulator. It never solves or issues real challenges.

Install

bun add @crvouga/mockingbird-service-turnstile

Usage

import { createServer } from "@crvouga/mockingbird-service-turnstile/server"

// Test setup: a local Siteverify, and a token like the one the widget hands the browser.
const server = await createServer()
const { token } = await server.runtime.instance().issue({
  hostname: "app.example.test", action: "signup",
}).json()

// Application code: the Siteverify call it makes to Cloudflare, sent to the emulator's URL.
const siteverifyUrl = `${server.url}/turnstile/v0/siteverify`
const result = await fetch(siteverifyUrl, {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ secret: "mock_secret", response: token }),
})
console.log(await result.json()) // success, challenge_ts, hostname, action, cdata

await server.close()

Run mockingbird-turnstile serve --port 12126, then override the consumer's Siteverify URL with http://localhost:12126/turnstile/v0/siteverify. Keep the real Cloudflare origin in production. There is no universal consumer environment variable for this override. Send synthetic secret: mock_secret; configure additional sites with sites.

Contract

POST /turnstile/v0/siteverify accepts JSON or URL-encoded form fields secret, response, optional remoteip and UUID idempotency_key. Valid tokens return HTTP 200 and success: true with metadata. Rejection is HTTP 200 with success: false and error-codes, not a transport exception. Missing/invalid secrets, missing/invalid tokens, malformed bodies and expired/used tokens retain their documented error codes.

Tokens expire at five minutes on the emulator clock and are consumed once. Matching idempotency key retries replay the original response; other retries return timeout-or-duplicate. Wrong secrets do not consume a valid token. Applications must independently check the returned hostname/action. Remote IP is accepted, not used for actual bot detection.

Controls

  • POST /__admin/issue: issue a synthetic token; accepts siteKey (default mock_site), hostname, action, cdata. Defaults are for local testing only.
  • Constructor tokens and shared /__admin/state/tokens: seed token expiry/use state.
  • GET /__admin/attempts: metadata-only verification attempts, without secret/token bodies.
  • Shared clock, Timeline, reset, state, faults and request journal are available. Reset restores constructor sites/tokens; namespaces are isolated by header or /__admin/ns/{namespace}. Body secrets do not select namespaces. adminPrefix relocates the control tree.

Presets: internal_error (HTTP 200 verification rejection), rate_limited (scripted 429), server_error (503), connection_drop. Generic fault rules add deterministic latency. Transport presets are test controls, not claims about normal Siteverify status codes. No webhooks.

Tests use raw fetch (the requested client), JSON/form success, expiry boundary/replay, idempotency, malformed input, namespace/reset isolation, redacted journals and served HTTP timeouts/fail-closed behavior. Property tests validate Siteverify and detect divergence. bun scripts/parity.ts needs TURNSTILE_TEST_SECRET set to one of Cloudflare's documented public dummy keys; production keys are refused. It compares success/error envelopes in JSON and form modes, not production token issuance or private metadata.

Deliberately not modelled

Browser widget/script, bot detection, real challenges/tokens, multipart encoding, production token signing, IP reputation, hostname policy administration and enterprise metadata. Idempotency cache retention and conflicting/in-flight key policies are deterministic local stand-ins; only identical retry behavior is covered by the public contract evidence.

API

  • TurnstileAPI: FetchAPI with fetch, reset, issue, and site/token/attempt collections.
  • createRuntime: shared emulator contract plus token issuance/attempt inspection.
  • TURNSTILE_NAMESPACE: service name.
  • TURNSTILE_PRESETS: named failure controls.
  • document, operationIds, supportedOperationIds: generated OpenAPI metadata.
  • createServer, serveTarget, DEFAULT_PORT from ./server: Node HTTP/CLI serving, default port 12126.

Public types include Site, Token, TurnstileAPIOptions, OperationId and SupportedOperationId.

版本与发布记录

@crvouga/mockingbird-service-turnstile 共发布 19 个版本,首次发布于 2026/10/05;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.10.0latest2026/11/11今天
1.9.62026/11/038 天前
1.9.52026/11/038 天前
1.9.42026/11/038 天前
1.9.32026/11/038 天前
1.9.22026/11/038 天前

关于 @crvouga/mockingbird-service-turnstile 的常见问题

@crvouga/mockingbird-service-turnstile 是什么?

Deterministic Cloudflare Turnstile Siteverify token validation.

如何安装 @crvouga/mockingbird-service-turnstile?

在项目目录运行 npm install @crvouga/mockingbird-service-turnstile,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 mockingbird-turnstile,可以用 npx mockingbird-turnstile 直接运行。

@crvouga/mockingbird-service-turnstile 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@crvouga/mockingbird-service-turnstile 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@crvouga/mockingbird-service-turnstile 可以商用吗?

@crvouga/mockingbird-service-turnstile 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@crvouga/mockingbird-service-turnstile 的下载量有多少?

根据 npm 官方下载统计,@crvouga/mockingbird-service-turnstile 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,331 次,日均约 78 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@crvouga/mockingbird-service-turnstile 还在维护吗?

最新版本 1.10.0 发布于 2026/11/11(今天);至今共发布 19 个版本,其中过去 12 个月发布了 19 个;首次发布于 2026/10/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。