
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
message-port-api
Advanced tools
A simple package that allows you to simplify the use of `MessagePort API` more easily (Worker, IFrame...)
A simple package that allows you to simplify the use of MessagePort API more easily (Worker, IFrame...)
NPM / Yarn / Pnpm
pnpm add message-port-api
worker.ts
import { receive } from "message-port-api"
const controllers = receive(
{
receiver: self,
sender: self
},
{
sum(a: number, b: number) {
return a + b
}
}
)
export type Controller = typeof controller
index.ts
import Worker from "./worker?worker"
import type { Controller } from "./worker"
const worker = new Worker()
console.log(
await sender<Controller>(
{
receiver: worker,
sender: worker
},
"sum",
[1, 2]
)
) // 3
iframe
import { receive } from "message-port-api"
receive(
{
receiver: parent,
sender: window
},
{
changeBg(color: string) {
document.body.style.backgroundColor = color
}
},
{
targetOrigin: "*"
}
)
main.ts
import { send } from "message-port-api"
await send(
{
get receiver() {
return iframe.contentWindow
},
sender: window
},
"changeBg",
["red"]
)
This function will be located on the host
side to handle the requested tasks
function receive(
config: {
receiver: Receiver // contains `postMessage` function to send return results
sender: Sender // contains 2 functions `addEventListener` and `removeEventListener` to listen to and cancel the `message` event
},
controllers: Record<string, Function>, // processing functions
targetOptions?: string | WindowPostMessageOptions // option 2 propagates to `postMessage`
): Controller
This function will be on the client
side to send processing requests and return a Promise containing processed results from receive
function
function sender<Controllers>(
config: {
receiver: Receiver // contains `postMessage` function to send processing request
sender: Sender // contains 2 functions `addEventLister` and `removeEventListener` to listen to and cancel the event `message` containing the results processed through the `receive` function
},
name: keyof Controllers, // function name for handling
arguments: Arguments<Controllers[name]>, // processing function call parameter
targetOptions?: string | WindowPostMessageOptions // option 2 propagates to `postMessage`
): Promise<ReturnType<Controllers[name]>> // prompt containing processed results
FAQs
A simple package that allows you to simplify the use of `MessagePort API` more easily (Worker, IFrame...)
We found that message-port-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.