Socket
Book a DemoInstallSign in
Socket

magic-transport

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-transport

Transport for communication between iframe and parent window

3.0.2
latest
Source
npmnpm
Version published
Weekly downloads
55
61.76%
Maintainers
0
Weekly downloads
 
Created
Source

Magic Transport

Transport for communication between iframe and parent window

Install

npm install magic-transport

or

yarn add magic-transport

Usage

Base example

Insert the following code on the page where the iframe is embeded (provider page):

import {Provider} from 'magic-transport'

const id = 'UNIQ_ID'
const childOrigin = '*'
const sharedObject = {
  hello: {
    from: {
      provider() {
        return 'hello from provider'
      }
    }
  }
}

const transport = new Provider({id, childOrigin, ...sharedObject})

transport.once('ready', async () => {
  const consumerResult = await transport.consumer.hello.from.consumer()

  console.log(consumerResult) // 'hello from provider'

  transport.consumer.timeout((result) => {
    console.log(result) // 'hello from consumer'
  }, 1000)

  transport.consumer.on('my_event', (result) => {
    console.log(result) // {foo: 'bar'}
  })
})

Insert the following code on the page loaded in the iframe (consumer page):

import {Consumer} from 'magic-transport'

const id = 'UNIQ_ID'
const parentOrigin = '*'
const sharedObject = {
  hello: {
    from: {
      consumer() {
        return transport.provider.hello.from.provider()
      }
    }
  },
  timeout(callback, timeout) {
    setTimeout(() => {
      callback('hello from consumer')
    }, timeout)
  }
}

const transport = new Consumer({id, parentOrigin, ...sharedObject})

transport.once('ready', () => {
  transport.consumer.emit('my_event', {foo: 'bar'})
})

Both Consumer and Provider interfaces can return any values, which will be resolved as a Promise. Passed callbacks will be called as well.

Connecting to any window or iframe

import {Provider} from 'magic-transport'

const id = 'UNIQ_ID'
const childOrigin = '*'
const sharedObject = {
  hello: {
    from: {
      provider: function () {
        return 'hello from provider'
      }
    }
  }
}

const iframe = document.createElement('iframe')
iframe.src = 'https://site.app/embed'
document.body.appendChild(iframe)

const transport = new Provider({
  id,
  childOrigin,
  connectedWindow: iframe.contentWindow,
  ...sharedObject
});

Documentation

Currently we only have the API which you can check here.

Contributing

Start

After you clone the repo you just need to run yarn's default command to install and build the packages

yarn

Testing

We have a test suite consisting of a bunch of unit tests to verify utils keep working as expected. Test suit is run in CI on every commit.

To run the tests

yarn test

To run the tests in watch mode

yarn test:watch

Code quality

To run linting the codebase

yarn lint

To check typings

yarn typecheck

To check bundle size

yarn sizecheck

Discussion

Please open an issue if you have any questions or concerns.

License

MIT

FAQs

Package last updated on 28 Jun 2024

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.