Nukem's Possible Manifestation
Have ideas to improve npm?Join in the discussion! »

data-transportTypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

data-transport

Node CI npm version

A generic and responsible communication transporter

Support Transport

  • iframe
  • Broadcast
  • Web Worker
  • Service Worker
  • Shared Worker
  • Browser Extension
  • WebRTC
  • Electron

Usage

  • Installation
yarn add data-transport
  • Define type
type IFrame = {
  hello: TransportData<{ num: number }, { text: string }>;
};
  • Create transports
const internal: Transport<IFrame> = createTransport('IFrameInternal');
const external: Transport<any, IFrame> = createTransport('IFrameMain');
 
external.listen('hello', ({ request, respond }) => {
  respond({
    text: `hello ${request.num}`,
  });
});
 
expect(await internal.emit('hello', { num: 42 }).toEqual({ text: 'hello 42' });

Another implementation based on inherited classes.

class InternalTransport extends IFrameTransport.IFrame<IFrame> {
  async sayHello() {
    const response = await this.emit('hello', { num: 42 });
    return response;
  }
}
 
class ExternalTransport extends IFrameTransport.Main implements Receiver<IFrame> {
  @listen
  hello({ request, respond }: Listen<IFrame['hello']>) {
    respond({
      text: `hello ${request.num}`,
    });
  }
}
 
const internal = new InternalTransport();
const external = new ExternalTransport();
 
expect(await internal.sayHello()).toEqual({ text: 'hello 42' });

TODO

  • refactor extension transport
  • support retry
  • support Network Request

Examples

Online about Broadcast

Install

npm i data-transport

DownloadsWeekly Downloads

37

Version

0.4.0

License

MIT

Unpacked Size

803 kB

Total Files

85

Last publish

Collaborators

  • avatar