npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pull-postmsg-stream

v1.2.0

Published

Pull streams over window.postMessage

Downloads

92

Readme

pull-postmsg-stream

Build Status dependencies Status JavaScript Style Guide

Pull streams over window.postMessage

It provides the two parts of a through stream: a source stream and a sink stream that can be used together to stream data over window.postMessage.

Install

npm install pull-postmsg-stream

Usage

To use pull-postmsg-stream you need two window objects. One of the window objects has the data, the other wants the data. Under the hood, pull-postmsg-stream uses postmsg-rpc. If you're not familiar with it, it's a good idea to read up on how it works before continuing!

In the first window (the one that has the data):

const pull = require('pull-stream')
const PMS = require('pull-postmsg-stream')

pull(
  pull.values([/* your data */]),
  PMS.sink('read', {/* options passed to postmsg-rpc expose */})
)

In the second window (the one that wants the data):

const pull = require('pull-stream')
const PMS = require('pull-postmsg-stream')

pull(
  PMS.source('read', {/* options passed to postmsg-rpc caller */}),
  pull.collect(console.log) // Collects and logs out your data
)
  1. Window that has the data calls PMS.sink, which exposes a function called "read" & returns a sink stream
  2. Window that wants the data calls PMS.source, which creates a caller function for "read" & returns a source stream
  3. In the window that wants the data, the pull(...) pipeline starts the flow of data from the PMS.source stream
  4. When data is requested from the PMS.source stream, it calls the exposed "read" function
  5. This causes the PMS.sink stream in the window that has the data to pull out of pull.values and return it all the way back to pull.collect in the window that wants the data

See the example for complete code.

Example

To build and run the example, run the following in your terminal:

git clone https://github.com/tableflip/pull-postmsg-stream.git
cd pull-postmsg-stream
npm install
npm run example

Then open your browser at http://localhost:3000

API

PMS.sink(readFnName, options)

Creates a new sink stream for exposing data to be pulled over postMessage.

  • readFnName - the name of the function that postmsg-rpc will expose for a PMS.source stream to read from
  • options - options passed directly to postmsg-rpc expose, see docs here
    • options.post - function to call after read, see docs here

Note that if you're going to create multiple streams, you'll need to generate a new readFnName for each stream and somehow communicate that to your other window so that it can create a PMS.source that reads from the correct place.

PMS.source(readFnName, options)

Creates a new source stream for pulling data over postMessage.

  • readFnName - the same name that was passed to PMS.sink, allowing the source to read from the sink
  • options - options passed directly to postmsg-rpc caller, see docs here
    • options.post - function to call after read, see docs here

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw