npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kazura/web-postmsg

v2.1.1

Published

web-postmsg

Downloads

89

Readme

npm size license

@kazura/web-postmsg

Web PostMsg 是一个用于 Web 环境下的 postMessage 通信工具包,提供了简单易用的 API 和类型安全的消息处理。

特性

  • 简单易用的 postMessage 通信 API
  • 支持消息通道(channel)管理
  • 支持消息超时处理(默认 5 秒)
  • 支持 Promise 风格的消息响应
  • 支持消息 UUID 追踪
  • 支持跨窗口通信

安装

npm install @kazura/web-postmsg

使用方法

基本使用

import WebPostMsg from '@kazura/web-postmsg'

// 创建发送方
const sender = new WebPostMsg({
  receiver: window.parent,
  channel: 'main',
})

// 创建接收方
const receiver = new WebPostMsg({
  receiver: window,
  channel: 'main',
})

// 发送消息
sender.emit('greeting', { message: 'Hello' }).then((response) => {
  console.log('Response:', response)
})

// 接收消息
receiver.on('greeting', (data) => {
  console.log(data.message) // 'Hello'
  return { status: 'received' } // 返回响应数据
})

跨窗口通信

// 父窗口
const parent = new WebPostMsg({
  receiver: window.frames[0],
  channel: 'parent-child',
})

// 子窗口
const child = new WebPostMsg({
  receiver: window.parent,
  channel: 'parent-child',
})

// 父窗口发送消息
parent.emit('parentMessage', { data: 'from parent' }).then((response) => {
  console.log('Child response:', response)
})

// 子窗口接收消息
child.on('parentMessage', (data) => {
  console.log('Received from parent:', data)
  return { status: 'received' }
})

接收所有通道的消息

const receiver = new WebPostMsg({
  receiver: window,
  channel: 'main',
  receiveAllChannel: true, // 接收所有通道的消息
})

receiver.on('anyChannelMessage', (data) => {
  console.log('Received message from any channel:', data)
})

API 参考

WebPostMsg 构造函数

constructor(options: {
  receiver: Window
  channel: string
  listeners?: Map<string, Listener>
  receiveAllChannel?: boolean
  self?: Window
})

创建一个 WebPostMsg 实例。

参数:

  • options.receiver: 目标窗口
  • options.channel: 消息通道名称
  • options.listeners: 可选的初始监听器映射
  • options.receiveAllChannel: 是否接收所有通道的消息
  • options.self: 可选的自身窗口引用(默认为 window)

实例方法

emit

emit(type: string, resources: any): Promise<any>

发送消息并等待响应。

参数:

  • type: 消息类型
  • resources: 消息数据

返回值:

  • Promise 包装的响应数据

on

on(type: string, listener: (resources: any, event: MessageEvent) => any): void

注册消息监听器。

参数:

  • type: 消息类型
  • listener: 消息处理函数,可以返回响应数据

off

off(type: string): void

移除消息监听器。

参数:

  • type: 消息类型

destroy

destroy(): void

销毁实例,移除所有事件监听器。

静态属性

  • MESSAGE_TAG: 消息标签,用于标识消息类型
  • PREFIX_DEFAULT: 默认消息类型前缀
  • PREFIX_INTERNAL: 内部消息类型前缀
  • REPLY_TYPE: 回复消息类型

注意事项

  1. 消息处理函数可以返回普通值或 Promise,返回值将作为响应发送给发送方
  2. 消息超时时间默认为 5 秒
  3. 使用 destroy() 方法清理实例,避免内存泄漏
  4. 确保正确设置 channel 以区分不同的消息通道
  5. 使用 receiveAllChannel 选项可以接收所有通道的消息

示例

异步消息处理

const receiver = new WebPostMsg({
  receiver: window,
  channel: 'main',
})

receiver.on('asyncTask', async (data) => {
  // 异步处理消息
  const result = await someAsyncOperation(data)
  return result // 返回处理结果
})

// 发送方
sender.emit('asyncTask', { task: 'process' }).then((result) => {
  console.log('Task result:', result)
})

多通道通信

// 创建多个通道的实例
const channelA = new WebPostMsg({
  receiver: window.parent,
  channel: 'channelA',
})

const channelB = new WebPostMsg({
  receiver: window.parent,
  channel: 'channelB',
})

// 在不同通道上发送消息
channelA.emit('message', { channel: 'A' })
channelB.emit('message', { channel: 'B' })

文档

更多详细信息请查看 文档

许可证

MIT

Author

👤 kazura233

  • Website: https://github.com/kazura233
  • Github: @kazura233

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!