@datadog/framepost

中文文档
0.4.1发布于 2024/05/08MITTypeScriptCommonJS零依赖近 30 天下载 4.6万

Secure parent-child iframe communication

原始使用文档
文档目录11 节
  1. Parent Client
  2. Child Client
  3. Shared API
  4. client.getContext() :
  5. client.on()
  6. client.send()
  7. client.request() :
  8. client.onRequest() :
  9. Advanced
  10. Debug mode
  11. Profiling

FramePost

FramePost is a library for parent-child iframe communication. It is designed to handle the details of secure communication while also providing a user-friendly API that makes iframe communcation feel more like using an API. This works by establishing a channel between the parent and child through a mutual handshake process before any other logic may proceed.

Parent Client

The parent client is responsible for initializing a channel with the child. When establishing a channel, the parent may include arbitrary data as context sent to the child during the handshake process. This must be done after the iframe is fully loaded. It is highly recommended that the channel be initialized in an onload event:

import { ParentClient } from '@datadog/framepost';

const client = new ParentClient();

const frame = document.createElement('iframe');

frame.src = 'https://domain.dom/path/to/frame.html';

const context = {
    infoAboutParent: 'it is a demo',
}

frame.onload = () => {
    client.requestChannel(frame, frame.src, context);
}

Child Client

The child client must be initialized in the iframe during initial script execution. The child may provide context to be sent to the parent during the handshake process in the context config option.

import { ChildClient } from '@datadog/framepost';

const context = {
    infoAboutChild: 'It is a demo'
}

const client = new ChildClient({ context });

Shared API

After the initial handshake succeeds, each client will handle communication with its counterpart using a shared API.

client.getContext():

Retrieves the context that the opposite client sent during the handshake process:

// in parent
const contextFromChild = await client.getContext();

// in child
const contextFromParent = await client.getContext();

client.on()

The counterpart to client.send(). Subscribes an event handler that will execute on events of the specified type from the opposite client. Event keys must be unique. Returns an unsubscribe hook.

const unsubscribe = client.on('route_change', newRouteData => {
    ...
})

// later
unsubscribe();

client.send()

The counterpart to client.on(). Sends an event to the opposite client with the specified event key and data. Event keys must be unique:

client.send('event_key', dataAboutEvent);

client.request():

The counterpart to client.onRequest(). A convenience method to send and receive data asynchronously from the opposite client. Designed to function like a promise-based api call. In order for a round-trip request to function correctly, an onRequest handler with a matching request key must be listening in the opposite client before the requeset is initiated. See below.

const data = await client.request('getMyData', requestParams);

client.onRequest():

The counterpart to client.request(). Receives request data send from a client.request call in the opposite client. Executes the provided handler, sending the returned response back to the opposite client.

client.onRequest('getMyData', async requestParams => {
    const responseData = await doSomeStuff());

    return responseData;
})

Advanced

Debug mode

Either client can be initialized in debug mode:

const parentClient = new ParentClient({ debug: true });
const childClient = new ChildClient({ debug: true });

This will print debug statements on various events and error cases.

Profiling

Cool feature alert! FramePost will collect and report timing profiles of message transactions handled by each channel. To enable, pass profile: true to each client on init. Each client will silently collect data that can be requested in the parent with parentClient.getProfile():

// in child
const childClient = new ChildClient({ proile: true });

// in parent
child parentClient = new ParentClient({ profile: true });

// after channel init and a few transactions:

parentClient.getMessageProfile().then(profile => {
    // do whatever you want with profile data
    console.log(profile);
});

版本与发布记录

@datadog/framepost 共发布 68 个版本,首次发布于 2020/10/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.1latest2024/05/082 年前
0.4.1-canary.5d878a0.0canary2024/05/062 年前
0.4.1-canary.38d3514.02024/05/062 年前
0.4.02022/12/103 年前
0.3.2-canary.134.15e3345.02022/04/134 年前
0.3.2-canary.134.797fbaf.02022/04/134 年前

关于 @datadog/framepost 的常见问题

@datadog/framepost 是什么?

Secure parent-child iframe communication

如何安装 @datadog/framepost?

在项目目录运行 npm install @datadog/framepost,或使用 pnpm add / yarn add / bun add。

@datadog/framepost 支持 TypeScript 吗?

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

@datadog/framepost 支持 ESM 还是 CommonJS?

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

@datadog/framepost 可以商用吗?

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

@datadog/framepost 的下载量有多少?

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

@datadog/framepost 还在维护吗?

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