@admiral-vrm/sdk-node

中文文档
1.1.3发布于 2026/03/16Apache-2.0 WITH Commons-ClauseTypeScriptES Module零依赖近 30 天下载 7804

The Admiral SDK for NodeJS simplifies the process of using Admiral's APIs for applications written in server-side JavaScript.

原始使用文档
文档目录17 节
  1. Requirements
  2. Installation
  3. Configuration
  4. Documentation
  5. Admiral
  6. getPossibleHandlers(): string[]
  7. getPossibleProxyPathPrefixes(): string[]
  8. getCurrentProxyPathPrefix(): string
  9. isProxyRequest(urlOrReq: string | MiddlewareRequest): boole…
  10. getProxyRoutingInfo(): { handlers: string[]; prefixes: stri…
  11. fetchSSRBootstraps(): Promise
  12. fetchBootstraps(): Promise<string[]>
  13. renderSSRBootstraps(payload: AdmiralSSRBootstrapWrapper, fo…
  14. handleRequestAsMiddleware(req: MiddlewareRequest): Promise
  15. Install API and proxy behavior
  16. Usage with Next.js and React
  17. Usage with Express.js / Node.js

Admiral SDK for NodeJS

Provides a simple interface for using Admiral's APIs for applications written in server-side JavaScript. Visit us at Admiral for more information.

Requirements

Node.js 18 or later (see engines in package.json).

Installation

Install the package to your project:

npm install @admiral-vrm/sdk-node
# or
yarn add @admiral-vrm/sdk-node

This package has peer dependencies (@edge-runtime/cookies, @noble/hashes v1.x, plus React/Next when using those features). Next.js apps typically satisfy these. If you use the SDK in a non-Next environment (e.g. Express or plain Node), install the peers manually if needed. Use @noble/hashes v1.x; v2.x uses different export paths and is not yet supported.

npm install @edge-runtime/cookies @noble/hashes@^1.7.0

Configuration

  • propertyID (required): Your Admiral property ID (e.g. "A-1234-1").
  • tokenSecretKey (recommended when using Protect): Secret used to sign visitor tokens. Set a strong, unique value in production; otherwise a default is used (not suitable for production). Can also be set via ADMIRAL_TOKEN_SECRET_KEY.
  • proxy: Optional. When using proxyAdmiral: true, set proxy.host (and optionally scheme, prefix).
  • protectOptions: Optional. For Protect/Partner API: partnerApiEndpoint, cacheTimeout. API credentials are configured inside the Protect integration (see examples).

Documentation

Admiral

The Admiral class is the main class for the Admiral SDK. It contains configuration for the Admiral object, such as the property ID, environment, enabling Protect and Proxy, and additional parameters.

export const admiral = new Admiral({
  propertyID: "A-1234-1",
  environment: "production",
  protect: true,
  proxyAdmiral: true,
  tokenSecretKey: process.env.ADMIRAL_TOKEN_SECRET_KEY, // recommended for production
  proxy: {
    host: "host.com",
    scheme: "https",
  },
});

The class provides the following methods:

getPossibleHandlers(): string[]

Returns an array of possible Admiral prefix handlers that can be used as prefixes when using the proxy functionality.

getPossibleProxyPathPrefixes(): string[]

Returns path prefixes that match proxy routes (e.g. ["/abcde.../", "/fghij.../"]). No I/O. Use for gate checks or diagnostics. Prefixes rotate over time; treat as runtime routing hints, not static invariants.

getCurrentProxyPathPrefix(): string

Returns the current (today's) proxy path prefix (e.g. "/abcde.../"), or "" if none. Same as getPossibleProxyPathPrefixes()[0]. No I/O.

isProxyRequest(urlOrReq: string | MiddlewareRequest): boolean

Returns true if the request or URL matches a proxy path. Accepts a full URL, pathname, or MiddlewareRequest. No I/O. Use before calling the SDK to skip non-Admiral requests (gate-first pattern).

getProxyRoutingInfo(): { handlers: string[]; prefixes: string[]; currentPrefix: string; generatedAt: number }

Returns current proxy handlers, path prefixes, the current (today's) prefix, and generation time. For admin/debug use only; restrict access and do not expose publicly.

fetchSSRBootstraps(): Promise

Fetches Admiral bootstrap scripts for server-side rendering. Returns a promise containing:

  • admiral: Array of bootstrap script strings, each of these should be script tags

fetchBootstraps(): Promise<string[]>

Fetches Admiral bootstrap scripts. Returns a promise containing an array of script strings.

renderSSRBootstraps(payload: AdmiralSSRBootstrapWrapper, format: "html" | "react" = "react"): React.ReactNode[]

Renders the Admiral bootstrap scripts for server-side rendering as React components. Takes a bootstrap wrapper payload from fetchSSRBootstraps and returns an array of React script elements.

Parameters:

  • payload: Object containing array of bootstrap scripts under admiral key

handleRequestAsMiddleware(req: MiddlewareRequest): Promise

Handles Admiral middleware functionality including proxy requests and Protect functionality. Returns a promise containing the middleware response with:

  • response: Response object (optional)
  • headers: Headers object (optional)
  • cookies: ResponseCookies object (optional)

Parameters:

  • req: MiddlewareRequest object containing the incoming request details

You can instead use the split handlers and a gate for more control:

  • handleProxyRequestAsMiddleware(req) – Proxy only. Use when the request is a proxy path (e.g. after isProxyRequest(url)).
  • handleProtectRequestAsMiddleware(req) – Protect only (token, cookie reset, admiral-protect-block header). No proxy.

Install API and proxy behavior

  • Bootstrap (Install API): You can keep using a cron or build step that fetches bootstrap scripts (e.g. fetchSSRBootstraps() or the Install API) and caches them on your CDN. You do not need to call the Install API at runtime for every user.
  • Proxy: A network round-trip in middleware happens only when a request matches one of the proxy path prefixes (Admiral runtime script/beacon traffic). Normal page loads, logins, and app API calls do not hit the proxy if you use a gate: call admiral.isProxyRequest(url) first and only invoke the SDK when it returns true (or when the URL is a Protect token/reset URL). See the nextjs and expressjs examples for a gate-first middleware pattern.
  • Why path-based proxy? A CNAME to a third-party host would be blockable by adblockers. The path-based proxy keeps requests on your host (yourdomain.com/<prefix>/...), so the host stays first-party and is not filtered. Expose any routing diagnostics (e.g. getProxyRoutingInfo()) only to admin/debug audiences, not publicly.

Usage with Next.js and React

See the nextjs example for more information with a full example and documentation on how to use the Admiral SDK with Next.js.

The ProtectEmbed component is a React component that can be used to embed the Protect functionality into a React application.

Usage with Express.js / Node.js

See the expressjs example for more information with a full example and documentation on how to use the Admiral SDK with Express.

The ProtectHTMLEmbed library allows embedding HTML with Protect functionality into your application without using React.

版本与发布记录

@admiral-vrm/sdk-node 共发布 6 个版本,首次发布于 2026/02/04;过去 12 个月发布了 6 个版本。

版本发布日期距今
1.1.3latest2026/03/167 个月前
1.1.22026/03/127 个月前
1.1.12026/03/127 个月前
1.1.02026/03/038 个月前
1.0.22026/02/049 个月前
1.0.12026/02/049 个月前

关于 @admiral-vrm/sdk-node 的常见问题

@admiral-vrm/sdk-node 是什么?

The Admiral SDK for NodeJS simplifies the process of using Admiral's APIs for applications written in server-side JavaScript.

如何安装 @admiral-vrm/sdk-node?

在项目目录运行 npm install @admiral-vrm/sdk-node,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@edge-runtime/cookies(>=6)、@noble/hashes(^1.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@admiral-vrm/sdk-node 支持 TypeScript 吗?

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

@admiral-vrm/sdk-node 支持 ESM 还是 CommonJS?

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

@admiral-vrm/sdk-node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@admiral-vrm/sdk-node 可以商用吗?

@admiral-vrm/sdk-node 使用 Apache-2.0 WITH Commons-Clause 许可。声明的许可为 Apache-2.0 WITH Commons-Clause,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@admiral-vrm/sdk-node 的下载量有多少?

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

@admiral-vrm/sdk-node 还在维护吗?

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