@finamx/bridge-protocol

中文文档
3.7.1发布于 2026/11/11TypeScriptESM + CJS近 30 天下载 3977
# @finamx/bridge-protocol
原始使用文档
文档目录9 节
  1. Installation
  2. Usage
  3. Exported API
  4. Constants
  5. Types / Interfaces
  6. Validators / Utilities
  7. Identity grant (3.0.0)
  8. Build
  9. Test

@finamx/bridge-protocol

Shared TypeScript types, constants, and validators for the postMessage bridge protocol between extension iframes and the FinamX host application.

Both the host-side (@finamx/host-bridge) and the extension-side (@finamx/extension-sdk) depend on this package to guarantee a single source of truth for message shapes, version strings, and scope definitions.

Installation

Within the monorepo, reference it as a file: dependency:

"dependencies": {
  "@finamx/bridge-protocol": "file:../bridge-protocol"
}

For a published extension SDK that ships to npm, use the semver range:

"dependencies": {
  "@finamx/bridge-protocol": "^1.0.0"
}

Usage

import {
  parseEnvelope,
  serializeEnvelope,
  isMethodAllowed,
  DEFAULT_GRANTED_SCOPES,
  type BridgeEnvelope,
  type BridgeHelloPayload,
} from '@finamx/bridge-protocol';

// Parse an incoming postMessage data value
const envelope = parseEnvelope(event.data); // throws BridgeProtocolError on invalid input

// Check whether a platform invoke is permitted
if (!isMethodAllowed('market.quote', session.grantedScopes)) {
  throw new Error('Scope not granted');
}

Exported API

Constants

ExportValueDescription
BRIDGE_VERSION'1.0'Current wire-protocol version; validated on every message.
EMBED_TO_HOST_TYPESarrayAll message types an extension iframe may send.
HOST_TO_EMBED_TYPESarrayAll message types the host may send back.
METHOD_SCOPE_MAPobjectMaps platform method names to required capability scopes.
DEFAULT_GRANTED_SCOPESarrayScopes granted by default (market.read, ui.toast, user.profile.read).

Types / Interfaces

TypeDescription
BridgeEnvelope<T>Wire envelope (v, type, id?, sessionId?, payload?).
BridgeMessageTypeUnion of all valid message type strings.
BridgeHelloPayloadPayload for bridge:hello — extension identifies itself to the host.
BridgeReadyPayloadPayload for bridge:ready — host confirms session and granted scopes.
BridgeErrorPayloadPayload for bridge:error — structured error with code and message.
HostContextTheme, locale, platform (web/ios/android), and app version.
UiResizePayloadPayload for ui:resize — desired iframe height in pixels.
UiSizePreferencePayloadPayload for ui:sizePreference — aspect ratio XOR named breakpoint; not ui:resize. Size is a UI surface preference (D-80-04), not a new extension kind.
UiWallPayloadPayload for ui:wall — visible plus reason auth | payment; payment is type-only, no billing fields.
UiToastPayloadPayload for ui:toast — message and optional variant.
PlatformInvokePayloadPayload for platform:invoke — method and optional params.
PlatformResultPayloadPayload for platform:result — ok, optional data or error.
PlatformEventPayloadPayload for platform:event — event name and optional data.
PlatformRevokePayloadPayload for platform:revoke — revocation reason.
BridgeSessionConfigInternal session record held by the host for a live iframe session.
LaunchResponseShape returned by POST /embed/launch before an iframe is opened.
InvokeRequestInternal type for host-side method dispatch.
AuditEntryAudit log record written on every platform invoke.

Validators / Utilities

ExportDescription
parseEnvelope(data)Parses and validates a raw postMessage data value; throws BridgeProtocolError.
serializeEnvelope(envelope)JSON-serializes an envelope for postMessage.
parseInvokePayload(payload)Validates a platform:invoke payload.
parseResizePayload(payload, maxHeight)Validates a ui:resize payload and clamps height.
parseSizePreferencePayload(payload)Validates a ui:sizePreference payload (exactly one of ratio or breakpoint).
resolveSizePreference(pref, bounds)Shared host layout math: preference → clamped pixel height.
parseWallPayload(payload)Validates a ui:wall payload (visible + closed reason auth | payment).
assertSessionMatch(envelope, session)Throws if the envelope sessionId does not match the active session.
assertExternalEmbedPolicy(props)Throws if an external extension tries to supply a raw embedUrl.
requiredScopeForMethod(method)Returns the scope string required for a given platform method.
isMethodAllowed(method, grantedScopes)Returns true when the method's required scope is in grantedScopes.
BridgeProtocolErrorError class with a code string for structured error handling.

Identity grant (3.0.0)

widget.identity.grant keeps its wire name, but 3.0.0 removed the HMAC identity helpers and the ./identity-context subpath. The grant now parses a profile: identity carries identityToken, a host-signed ES256 JWT (typ: platform.identity.v1); exchange carries exchangeCode + redeemUrl. A grant that mixes the two is rejected. This package only defines and validates shapes; it never signs. sessionJwt and Core/Gateway tokens must not be placed in the iframe or in the identity grant.

Build

npm run build

Compiles TypeScript to dist/ (ESM). The prepare script runs automatically on npm install within the monorepo.

Test

npm test

Builds first, then runs dist/validators.test.js using Node's built-in test runner (node --test).

版本与发布记录

@finamx/bridge-protocol 共发布 21 个版本,首次发布于 2026/06/10;过去 12 个月发布了 21 个版本。

版本发布日期距今
3.7.1latest2026/11/114 天前
3.7.02026/11/105 天前
3.6.12026/11/095 天前
3.6.02026/11/077 天前
3.5.12026/11/0410 天前
3.5.02026/10/2916 天前

关于 @finamx/bridge-protocol 的常见问题

@finamx/bridge-protocol 是什么?

# @finamx/bridge-protocol

如何安装 @finamx/bridge-protocol?

在项目目录运行 npm install @finamx/bridge-protocol,或使用 pnpm add / yarn add / bun add。

@finamx/bridge-protocol 支持 TypeScript 吗?

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

@finamx/bridge-protocol 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@finamx/bridge-protocol 可以商用吗?

@finamx/bridge-protocol 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@finamx/bridge-protocol 的下载量有多少?

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

@finamx/bridge-protocol 还在维护吗?

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