Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
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...)
This is a very simple package. it cannot transfer complex data such as
function
orclass
. if you want to useAPI
containing complex data use workercom
NPM / Yarn / Pnpm
pnpm add message-port-api
worker.ts
import { useReceive } from "message-port-api"
const receive = useReceive(self)
const controllers = receive({
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"
import { useSend } from "message-port-api"
const worker = new Worker()
const send = useSend(worker)
console.log(await send<Controller>("sum", [1, 2])) // 3
iframe
import { useReceive } from "message-port-api"
const receive = useReceive(window, parent)
receive(
{
changeBg(color: string) {
document.body.style.backgroundColor = color
}
},
{
targetOrigin: "*"
}
)
main.ts
import { useSend } from "message-port-api"
const send = useSend(window, () => iframe.contentWindow)
await send("changeBg", ["red"])
This function will be located on the host
side to handle the requested tasks
function useReceive(
sender: Sender, // contains `postMessage` function to send return results
receiver: Receiver // contains 2 functions `addEventListener` and `removeEventListener` to listen to and cancel the `message` event
): receive
function receive(
controllers: Record<string, Function>, // processing functions
targetOptions?: string | WindowPostMessageOptions // option 2 propagates to `postMessage`
): Controller
If you want to cancel the call
receive.cancel()'. This is useful when you use
receive` in components
import { useReceive } from "message-port-api"
const receive = useReceive(self)
receive({ sum })
receive.cancel() // cancel receive call
This function will be on the client
side to send processing requests and return a Promise containing processed results from receive
function
function useSend(
sender: Sender, // contains `postMessage` function to send processing request
receiver: Receiver // contains 2 functions `addEventLister` and `removeEventListener` to listen to and cancel the event `message` containing the results processed through the `receive` function,
timeout: boolean | number = 30000 // the interval that waits for data to return if the timeout throws a `TIMEOUT` error. Default is 30_0000
): send
function send<Controllers>(
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...)
The npm package message-port-api receives a total of 60 weekly downloads. As such, message-port-api popularity was classified as not popular.
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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.