Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

extension-port-stream

MetaMask1.5mISC5.0.0TypeScript support: included

A module for creating a node style stream over a WebExtension port object.

WebExtension, Stream

readme

Extension Port Stream

A module for creating a Node-style stream over a WebExtension Runtime.Port object.

Breaking changes from v4 to v5

  1. Chunking mode is enabled by default (see the Usage section below).
  2. Node.js-style Buffer messages are no longer supported.

Additionally, the timing of logging, errors, and callbacks may be handled differently.

Usage

By default, ExtensionPortStream will send messages in 64MB chunks on Chromium-based browsers. When this mode is used the receiving end must also use ExtensionPortStream in its default mode.

import { ExtensionPortStream } from "extension-port-stream";

extension.runtime.onConnect.addListener(connectRemote);
const portStream = new ExtensionPortStream(remotePort, {
  chunkSize: 0, // disable chunking
});

// Enjoy!

To disable chunking set the chunkSize option to 0. This will make the transport mostly backwards compatible with v4.

import { ExtensionPortStream } from "extension-port-stream";

extension.runtime.onConnect.addListener(connectRemote);
const portStream = new ExtensionPortStream(remotePort, {
  chunkSize: 0, // disable chunking
});

// Enjoy!

Events

ExtensionPortStream extends Node.js Duplex stream, so it inherits all EventEmitter capabilities. Additionally, it emits the following custom events:

message-too-large

Emitted when a message is too large to send in a single postMessage call and needs to be chunked. This event is only emitted when chunking is enabled (default).

import {
  ExtensionPortStream,
  MessageTooLargeEventData,
} from "extension-port-stream";

const portStream = new ExtensionPortStream(remotePort);

portStream.on("message-too-large", (data: MessageTooLargeEventData) => {
  console.log(
    `Message too large (${
      JSON.stringify(data.message).length
    } bytes), chunking into ${data.chunkSize}-byte pieces`
  );
  console.log("Original error:", data.originalError.message);
});

Running tests

yarn test

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

5.0.0

Changed

  • BREAKING: Chunk large port stream messages into smaller frames by default (#68)

    • By default, ExtensionPortStream will send messages in 64MB chunks on Chromium-based browsers. When this mode is used the receiving end must also use ExtensionPortStream in its default mode:

      import { ExtensionPortStream } from "extension-port-stream";
      
      extension.runtime.onConnect.addListener(connectRemote);
      const portStream = new ExtensionPortStream(remotePort, {
        chunkSize: 0, // disable chunking
      });
      
      // Enjoy!
    • To disable chunking set the chunkSize option to 0. This will make the transport mostly backwards compatible with v4:
      import { ExtensionPortStream } from "extension-port-stream";
      extension.runtime.onConnect.addListener(connectRemote);
      const portStream = new ExtensionPortStream(remotePort, {
        chunkSize: 0, // disable chunking
      });
      // Enjoy!
    • message-too-large is emitted when a message is too large to send in a single postMessage call and needs to be chunked. This event is only emitted when chunking is enabled (default).
  • BREAKING: Node.js-style Buffer messages are no longer supported (#68)

4.2.0

Changed

  • Allow overriding Duplex stream constructor options (#59)

4.1.0

Added

  • Add named export of PortDuplexStream in addition to existing default export (#57)

4.0.0

Fixed

  • BREAKING: webextension-polyfill is now a peer-dependency rather than a dependency. Users are expected to provide the runtime. (#54)
  • webextension-polyfill import changed to type-only import (#54)

3.0.0

Changed

  • BREAKING: Use portable readable-stream@^3.6.2 instead of native streams (#51)

2.1.1

Changed

  • deps: replace webextension-polyfill-ts with webextension-polyfill (#43)

2.1.0 - 2023-06-15

Added

  • _setLogger method can be used to inject custom logger for incoming/outgoing messages (#46)

Changed

Fixed

2.0.1 - 2021-04-29

Changed

  • Move webextension-polyfill-ts from devDependencies to dependencies (#11)

2.0.0 - 2020-11-23

Added

  • TypeScript typings (#4)

Removed

  • (BREAKING) Remove readable-stream dependency (#4)
    • Consumers using this package in browser environments will have to bring their own Node.js stream polyfill.