@castleio/castle-js

中文文档
2.8.5发布于 2026/06/26MITTypeScriptCommonJS零依赖近 30 天下载 136.4万

Castle Fingerprinting Script

此软件包已被标记为弃用

Castle.js v2 is deprecated. Please upgrade to v3 — upgrade notes are available in the Castle dashboard.

原始使用文档
文档目录21 节
  1. Introduction
  2. Installation
  3. Configuration
  4. Basic configuration of the app
  5. browser version (legacy setups)
  6. Usage
  7. Getting castle request token
  8. form submit helper example (when cdn version is used)
  9. Sending page event
  10. Sending form event
  11. form submit helper for form events
  12. Sending custom event
  13. Upgrading from 1.x to 2.0.x
  14. 1.x
  15. 2.0.x
  16. Upgrading from 2.0.x to 2.1.x or later
  17. 2.0.x
  18. 2.1.x or later
  19. Changelog
  20. Minimal Requirements:
  21. License

Castle Fingerprinting Script

Packaged version of Castle fingerprinting script.

Introduction

The Castle JavaScript automatically captures every user action in your web application, including clicks, taps, swipes, form submissions, and page views. We use this data to build profiles of good user behavior in order to detect the bad.

Installation

npm install --save @castleio/castle-js

yarn add @castleio/castle-js

Configuration

Basic configuration of the app

// @option options [string] :pk castle publishable key.
// @option options [object] :window (default `window`) eg JSDOM.window
// @option options [string] :apiUrl (default `https://m.castle.io/v1/monitor`) castle api url
// @option options [number] :timeout (default 1000) castle api response timeout
// @option options [boolean] :verbose (default true) verbose mode (console warnings)
// @option options [object] :storage (default name `__cuid`, default expireIn 400 days in seconds: `34560000`) used for storing uuid in the localStorage and the cookies, and for setting cookie expiration time.

import * as Castle from '@castleio/castle-js'

Castle.configure(options);

browser version (legacy setups)

If your environment does not support modules you can use the browser version

import '@castleio/castle-js/dist/castle.browser.js'

Castle.configure(options);

Usage

Getting castle request token

import * as Castle from '@castleio/castle-js'

Castle.createRequestToken().then( (requestToken) => {
  ....
});

// or
const requestToken = await Castle.createRequestToken();

See Castle Docs for more information and how to pass the token further.

form submit helper example (when cdn version is used)

// @param event [Event] submit event
// @param onDone [Function] optional form submit callback replacement
<form action="/" onsubmit="Castle.injectTokenOnSubmit(event)">
  <button type="submit">Submit</button>
</form>

helper is also available directly (when castle.umd.js is used)

import * as Castle from '@castleio/castle-js'

const submitHandler = (evt) => Castle.injectTokenOnSubmit(evt);
<form action="/" onsubmit="submitHandler(event)">
  <button type="submit">Submit</button>
</form>

Sending page event

// @param options [PageParams] castle page command params.
// @option user [UserParams] user object with the required `id`, optional `email`, `phone`, `registered_at`, `name`, `traits`
// @option userJwt [string] optiona jwt encoded UserParams
// @option name [String]
// @option referrer [String]
// @option url [String]

import * as Castle from '@castleio/castle-js'

const page = Castle.page(options);

event response can be verified with promise like call.

import * as Castle from '@castleio/castle-js'

// result - true - success response from the server
// result - false - error response from the server, missing configuration or data
// result - null - timeout or debounced event (300ms)

Castle.page(options).then( (result) => { } )

Sending form event

// @param options [FormParams] castle form command params.
// @option user [UserParams] user object with the required `id`, optional `email`, `phone`, `registered_at`, `name`, `traits`
// @option userJwt [string] JWT encoded UserParams (can be passed instead of the user)
// @option name [String]
// @option values [Record<string, string>]

import * as Castle from '@castleio/castle-js'

Castle.form(options);

event response can be verified with promise like call.

import * as Castle from '@castleio/castle-js'

// result - true - success response from the server
// result - false - error response from the server, missing configuration or data
// result - null - timeout or throttled event (300ms)

Castle.form(options).then( (result) => { } )

form submit helper for form events

import * as Castle from '@castleio/castle-js'

const user = YOUR_USER_DATA;
const submitHandler = (evt) => Castle.formEventOnSubmit(evt, user);
<form action="/" data-castle-name="Change Profile" onsubmit="submitHandler(event)">
  <input type="text" name="user_mail" data-castle-value="email">
  <button type="submit">Submit</button>
</form>

Sending custom event

// @param options [CustomParams] castle custom command params.
// @option user [UserParams] user object with the required `id`, optional `email`, `phone`, `registered_at`, `name`, `traits`
// @option userJwt [string] JWT encoded UserParams (can be passed instead of the user)
// @option name [String]
// @option properties [Record<string,string>]

import * as Castle from '@castleio/castle-js'

const custom = Castle.custom(options);

event response can be verified with promise like call.

import * as Castle from '@castleio/castle-js'

// result - true - success response from the server
// result - false - error response from the server, missing configuration or data
// result - null - timeout or debounced event (default 1000ms)

Castle.custom(options).then( (result) => { } )

Breaking changes

Upgrading from 1.x to 2.0.x

1.x

Legacy package: https://www.npmjs.com/package/castle.js

require "castle.js"
_castle('setAppId', "YOUR_APP_ID")
_castle('getClientId')

2.0.x

New package:

import * as Castle from "@castleio/castle-js"
  1. Renamed the main object from _castle to Castle in the new module, but kept the _castle in the CDN version as well as the browser-specific module @castleio/castle-js/dist/castle.browser.js
  2. Introduced the concept of request tokens that need to be generated for each call to Castle's server-side API.
Castle.createRequestToken().then( (requestToken) => {
});

// or

const token = await Castle.createRequestToken();
  1. Request tokens can no longer be retrieved from the cookie string.
  2. All the following methods were removed:

autoForwardClientId, autoTrack, catchHistoryErrors, identify, setUserId, setAccount, setKey, setAccount, sessionId, reset, page, trackPageView, setTrackerUrl

Request Token is now required to be passed in the request params and it is no longer available in the cookie. Check the docs

Upgrading from 2.0.x to 2.1.x or later

2.0.x

Castle.configure(YOUR_CASTLE_APP_ID);

2.1.x or later

  1. Removed the _castle object for all versions and now only relying on Castle.

  2. Switched to use the Publishable Key that can be found in the same place as the now deprecated App ID.

Castle.configure({pk: YOUR_PUBLISHABLE_KEY});
  1. onFormSubmit has been renamed to injectTokenOnSubmit

  2. _castle global method is no longer supported and has been replaced with Castle (for the CDN and castle.browser.js versions) check docs

  3. CDN version no longer needs appID in the url and requires

<script>Castle.configure({pk: YOUR_CASTLE_PUBLISHABLE_KEY});</script>

to be added. The CDN version can't be used for generating request tokens, but only for tracking client-side events.

  1. Introduced page, form, and custom methods for client-side event tracking

More info can be found in the docs

Changelog

  • 2.8.5 - fix types field in package.json
  • 2.8.4 - performance updates
  • 2.8.3 - performance updates
  • 2.8.2 – security enhancements
  • 2.8.1 – bug fixes
  • 2.8.0 - security updates, removed official support for older browsers (target updated from ES3 to ES5); updated minimal requirements
  • 2.7.1 - no changes
  • 2.7.0 - createRequestToken, page, form, and custom return Promise if available (Thenable otherwise), internal enhancements
  • 2.6.2 - reduced bundle size, added internal features, added support for chaining .then, as well as using .catch and .finally for the createRequestToken, page, form, and custom
  • 2.6.1 - removed undocumented getVersion function and dropped usage of temporary cookie, internal enhancements
  • 2.6.0 - bug fixes and internal features
  • 2.5.3 - security updates
  • 2.5.2 - internal release
  • 2.5.1 - extended browser support range and bug fixes
  • 2.5.0 - enhanced bot detection capabilities, performance improvements, bug fixes
  • 2.4.3 - internal enhancements
  • 2.4.2 - force https for the monitor requests, bug fixes, utf enhancements
  • 2.4.1 – internal enhancement
  • 2.4.0 – larger set of enhancements and internal features
  • 2.3.6 – internal enhancements
  • 2.3.5 – fixed issue with exporting types in the d.ts file
  • 2.3.4 – internal enhancements
  • 2.3.3 – internal enhancements
  • 2.3.2 – bug fixes
  • 2.3.1 – internal stability fixes
  • 2.3.0 – new configuration option throttling, enabled by default, and set to 500ms, is used when multiple custom events are being sent within short time intervals.
  • 2.2.0 – new configuration option storage, deprecated storageNamespace, and stability improvements.
  • 2.1.15 – new configuration option storageNamespace, for specifing localStorage and cookie storage namespace, stability improvements
  • 2.1.14 – stability improvements
  • 2.1.13 – throw error when pk is not configured
  • 2.1.12 – made Castle.configure to return Castle interface object
  • 2.1.11 – added address support to user for page, form, custom events
  • 2.1.10 – bug fixes and stability improvements
  • 2.1.9 – bug fixes and stability improvements
  • 2.1.8 – pk validation and internal enhancements
  • 2.1.7 – page command bug fixes and improvements
  • 2.1.6 – fixed JWT validation issue
  • 2.1.5 – readme update
  • 2.1.4 – stability improvements
  • 2.1.3 – view events bug fixes and stability improvements
  • 2.1.2 – view events bug fixes
  • 2.1.1 – internal enhancements and stability improvements
  • 2.1.0 – added page, form, custom events support, changed configuration DSL, added formEventOnSubmit helper, renamed onFormSubmit to injectTokenOnSubmit (breaking changes - check docs )
  • 2.0.4 – bug fixes
  • 2.0.3 – bug fixes and stability improvements
  • 2.0.2 – bug fixes
  • 2.0.1 – internal enhancements
  • 2.0.0 – migration from 1.x, modularization, updated DSL, (breaking changes - check docs )

Minimal Requirements:

  • Chrome 38+
  • Firefox 44+
  • Opera 32+
  • Edge 12+
  • Safari 8+
  • All modern browsers (desktop and mobile)

License

MIT

版本与发布记录

@castleio/castle-js 共发布 53 个版本,首次发布于 2021/06/15;过去 12 个月发布了 4 个版本。

版本发布日期距今
2.8.5latest2026/06/264 个月前
2.8.42026/01/209 个月前
2.8.32026/01/0710 个月前
2.8.22025/11/1711 个月前
2.8.12025/10/071.1 年前
2.8.02025/09/181.1 年前

其他发布通道:rc → 2.1.0-rc.1。

关于 @castleio/castle-js 的常见问题

@castleio/castle-js 是什么?

Castle Fingerprinting Script注意:该包已被作者标记为弃用——Castle.js v2 is deprecated. Please upgrade to v3 — upgrade notes are available in the Castle dashboard.

如何安装 @castleio/castle-js?

在项目目录运行 npm install @castleio/castle-js,或使用 pnpm add / yarn add / bun add。

@castleio/castle-js 支持 TypeScript 吗?

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

@castleio/castle-js 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@castleio/castle-js 可以商用吗?

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

@castleio/castle-js 的下载量有多少?

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

@castleio/castle-js 还在维护吗?

最新版本 2.8.5 发布于 2026/06/26(4 个月前);至今共发布 53 个版本,其中过去 12 个月发布了 4 个;首次发布于 2021/06/15。作者已将其标记为弃用,新项目建议寻找替代方案。