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

electron-ipc-stream

v0.2.0

Published

Duplex stream over IPC for main/renderer to communicate with each other.

Downloads

30

Readme

electron-ipc-stream

Duplex stream that run over Electron's IPC mechanism.

Why?

This allows you to use any Node.js stream readable/writable and easily communicate between your main/renderer process.

Since your renderer process is also responsible for UI/DOM, etc, you may not want to do any heavy processing on the renderer process. You could leverage this module to have the renderer stream data to the main process for processing and then the main module could stream results back to the renderer process for consumption.

Install

npm i --save electron-ipc-stream

Usage

Example 1: Pipe file from main process to renderer.

main.js:

var app = require('app')
var fs = require('fs')
var path = require('path')
var window = require('electron-window')
var IPCStream = require('electron-ipc-stream')

app.on('ready', function () {
  var win = window.createWindow({ height: 600, with: 1000 })

  var ipcs = new IPCStream('any-arbitrary-channel-name', win)
  win.showUrl(path.resolve(__dirname, './index.html'), function () {
    // window is visible, dom is ready in window
    fs.createReadStream('/tmp/mainfile').pipe(ipcs)
  })
})

rend.js:

var fs = require('fs')
var ipc = require('ipc')
var IPCStream = require('electron-ipc-stream')
var ipcs = new IPCStream('any-arbitrary-channel-name')

document.addEventListener('DOMContentLoaded', function () {
  ipcs.pipe(fs.createWriteStream('/tmp/rendfile')).on('finish', function () {
    console.log('done')
  })
})

Example 2: Pipe file from renderer process to main.

main.js:

var app = require('app')
var fs = require('fs')
var path = require('path')
var window = require('electron-window')
var IPCStream = require('electron-ipc-stream')

var tmpfile = '/tmp/mainfile'
app.on('ready', function () {
  var win = window.createWindow({ height: 600, with: 1000 })
  var ipcs = new IPCStream('any-arbitrary-channel-name', win)
  ipcs.pipe(fs.createWriteStream(tmpfile)).on('finish', function () {
    console.log('done')
  })
  win.showUrl(path.resolve(__dirname, './index.html'), function () { })
})

rend.js:

var crypt = require('crypto') // notice this is 'crypt' and not 'crypto'
var fs = require('fs')
var ipc = require('ipc')
var IPCStream = require('electron-ipc-stream')
var ipcs = new IPCStream('any-arbitrary-channel-name')

fs.writeFileSync('/tmp/rendfile', crypt.randomBytes(10000))
document.addEventListener('DOMContentLoaded', function () {
  fs.createReadStream(tmpfile).pipe(ipcs)
})

API

Main Process

IPCStream(channel, [browserWindow], [streamOptions])

Create a new IPCStream in the main process.

Renderer Process

IPCStream(channel, [streamOptions])

Create a new IPCStream in the renderer process.

Stream Options

You shouldn't have to mess with objectMode. Under the hood, objectMode is true. Buffers are serialized to JSON. This is because of the way that Electron handles buffers in renderer. See: https://github.com/atom/electron/blob/master/docs/api/remote.md for more detail. You also may need to adjust highWaterMark.

JSON Objects

It is completely safe to call write on either end of the stream with objects.

source:

myStream.write({name: 'JP'})

dest:

// streams 1 (flowing):
myStream.on('data', function (data) {
  console.dir(data) // => {name: 'JP'}
})

// streams 2/3 (pull, if you prefer):
myStream.on('readable', function () {
  var data
  while (null !=== (data = myStream.read())) {
    console.dir(data) // => {name: 'JP'}
  }
})

Examples

In the ./test folder, you'll see two examples. You can run these by installing electron-prebuilt:

npm i -g electron-prebuilt
electron ./test/main-to-rend
electron ./test/rend-to-main

License

MIT Copyright JP Richardson