Socket
Socket
Sign inDemoInstall

structured-channel

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    structured-channel

A wrapper around MessageChannel API for bi-directional communication between two browsing contexts.


Version published
Weekly downloads
251
decreased by-34.46%
Maintainers
1
Install size
18.0 kB
Created
Weekly downloads
 

Readme

Source

A wrapper around MessageChannel API for bi-directional communication between two browsing contexts.

Usage

The bundles in the dist/ directory are in UMD format which allows them to be included as CommonJS and AMD modules or directly in a <script> tag.

Simple example

Here's an example of Window - Worker communication using a StructuredChannel.

// In worker.js
self.importScripts("structured-channel.js");
StructuredChannel.waitForConnection(this).then(function(channel) {
  channel.on("sum", function(data) {
    return data.a + data.b;
  });
});

// In main.js with structured-channel.js included via script tag.
var worker = new Worker("worker.js");
StructuredChannel.connectTo(worker).then(function(channel) {
  channel.send("sum", { a: 1, b: 2 }).then(function(reply) {
    // reply == 3
  });
});

API

Initialization

A new StructuredChannel is initialized by calling the static methods connectTo() and waitForConnection() on the different sides of the channel.

StructuredChannel.connectTo(target, [targetOrigin], [global]) ⇒ Promise

Opens a StructuredChannel to the given target. The target must load this script and call StructuredChannel.waitForConnection().

Returns: Promise - A Promise that is fulfilled with a StructuredChannel instance once the connection has been established. The promise is rejected on error.

ParamTypeDefaultDescription
targetWindow | WorkerThe target window or a worker to connect to.
[targetOrigin]String*If the target is a Window, this is the targetOrigin for Window.postMessage(). Failing to provide this parameter might have security implications as the channel could be opened to a malicious target. If the target is a Worker, this parameter is ignored.
[global]ObjectAn optional global object that can be used to get a reference to the MessageChannel constructor.

StructuredChannel.waitForConnection([target], [origin]) ⇒ Promise

Waits for a connection request from StructuredChannel.connectTo() to arrive as a message event to the given target.

Returns: Promise - that is resolved with a StructuredChannel instance once the connection request is received.

ParamTypeDescription
[target]Window | WorkerThe target that should receive the connection attempt (default self).
[origin]StringThe origin from which the connection attempt should come from. If undefined or '*', connection attempts and messages from all origins are allowed. Failing to provide a specific origin might have security implications as malicious parties could establish a connection to this target.

Instance methods

You can attach handlers to messages by calling StructuredChannel.on() and send messages with StructuredChannel.send().

StructuredChannel.send(type, payload) ⇒ Promise

Sends a message to the other side of the channel.

Returns: Promise - A Promise that is resolved once the receiver has handled the message. The resolution value will be the object the handler method returned. If the other party fails to handle the message, the promise is rejected.

ParamTypeDescription
typeStringThe type of the message.
payloadObjectThe payload for the message. The value must support structured cloning.

StructuredChannel.on(type, handler)

Adds a handler for given message type.

ParamTypeDescription
typeStringThe type of the message to handle.
handlerfunctionThe handler function. The return value will be transferred back to the sender and the Promise returned by send() is settled according to it. If the function throws, returns a Promise that is eventually rejected or returns a value that cannot be transmitted to the sender, the send() Promise rejects. If the function returns a value, the send() Promise is fulfilled with that value. If the function returns a Promise that is eventually fulfilled, the send() Promise is fulfilled with the fulfillment value.

StructuredChannel.off(type)

Removes the handler for the message of given type.

ParamTypeDescription
typeStringThe type of the message for which the handler is to be removed.

Keywords

FAQs

Last updated on 02 Nov 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc