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 🙏

© 2025 – Pkg Stats / Ryan Hefner

worker-request-message

v0.0.5

Published

使用Promise为Worker,iframe,New tab Window 的 postMessage包装。 使postMessage变得容易;Use Promise as the postMessage wrapper for Worker, iframe, New tab Window. Make postMessage easy

Readme

WorkerMessage

使用Promise为Worker,iframe,New tab Window 的 postMessage包装。 使postMessage变得容易;

demo 请查看 Console 输出

install

npm install worker-request-message

WebWorker

index.html

<script type="module">
    import WorkerMessage from "../index.js";

    const worker = new Worker("./worker.js");//,{type:'module'}

    const message = new WorkerMessage(worker);
    message.onRequestMessage = function (message, resolve, reject) {
        resolve(message + "; main response")
    }

    message.requestMessage("to worker").then((e) => {
        console.log(e)
    }).catch((e) => {
        console.error(e)
    })
</script>

worker.js

//WebWorker
importScripts("../index.inworker.js")
const message = new WorkerMessage(self);

message.onRequestMessage = function (message, resolve, reject) {
    resolve(message + "; worker response")
}

message.requestMessage("to main").then((e) => {
    console.log(e)
}).catch((e) => {
    console.error(e)
})

iframe

index.html

<iframe src="./inner.html" id="innerIframe"></iframe>
<script type="module">
    import WorkerMessage from "../index.js";

    innerIframe.addEventListener("load", (e) => {
        const message = new WorkerMessage(innerIframe.contentWindow);
        message.onRequestMessage = function (message, resolve, reject) {
            resolve(message + "; parent response")
        }

        message.requestMessage("to iframe").then((e) => {
            console.log(e)
        }).catch((e) => {
            console.error(e)
        })
    }, false)
</script>

inner.html

<script type="module">
    import WorkerMessage from "../index.js";

    const message = new WorkerMessage(self);
    message.onRequestMessage = function (message, resolve, reject) {
        resolve(message + "; iframe response")
    }

    message.requestMessage("to parent").then((e) => {
        console.log(e)
    }).catch((e) => {
        console.error(e)
    })
</script>

API

methods

| 方法 | 参数说明 | 返回值 | | :--- | :--- | ----: | |constructor(self,targetOrigin)|target: Window | Worker 用于发送或接收消息的对象 worker、iframe.contentWindow 或 iframe 里的 windowtargetOrigin?: stringtarget 使用 iframe.contentWindow 时, 以防止恶意第三方窃取密码。始终提供具体的信息targetOrigin| | requestMessage(message) | message:any | Promise<any> | | postMessage(message, transfer, targetOrigin)| message:any 消息内容transfer?: TransferabletargetOrigin?:string 默认使用构造函数传入的 targetOrigin 值 |void| |destroy()| 释放资源 |void|

events

| 事件 | 参数说明 | | :--- | :--- | |onRequestMessage(message,resolve,reject)|message:any 消息内容resolve:(message: any) => void 解决请求回调reject:(message: any) => void 拒绝请求回调| |onMessage(message,e)|message:any 消息内容 e:MessageEvent 消息事件对象|