0.3.1 • Published 3 years ago

cross-origin-courier v0.3.1

Weekly downloads
113
License
MIT
Repository
github
Last release
3 years ago

npm npm bundle size NPM GitHub top language

This project is under active development.

Until major version released, the API is a subject to change.

cross-origin-courier

A wrapper over Window.postMessage() and MessageChannel to facilitate messaging between cross origin contexts.

The library exposes a connection function that returns a Promise. On a successful connection, the Promise is resolved with the courier object to be used to send and listen to messages. Note: the child frame should call the connection function first.

Setup

NPM

npm i cross-origin-courier
import connect from 'cross-origin-courier';
// ...
connect().then(courier => {
    courier.listen(handler);
    courier.send({ answer: 42 });
});

function handler(data) {
    // do your stuff
}

CDN

<script src="https://unpkg.com/cross-origin-courier"></script>

Loaded this way, the connection function is exposed under the following long-ish name

window.createCrossOrigConnection().then(courier => {
    // ...
});

Options

const options = {
    passphrase: 'foo-bar-baz-qux',
    isParent: true,
    targetOrigin: '*',
};
createConnection(options).then(courier => {
    // ...
});
OptionTypeDefaultDescription
passphrasestring'DEFAULT_PASSPHRASE'todo
targetOriginstring'*'todo
isParentbooleanfalsetodo

Courier API

MethodArgumentsDescription
senddata<any>Sends data to the counterparty
listendataHandler<(data: any) => void>Sets a handler for the counterparty messages

Documentation

Please find the full docs here

License

Copyright © 2021, Sergey Chernykh. Released under the MIT License.

0.3.0

3 years ago

0.3.1

3 years ago

0.3.0-0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago