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

@kabacorp/pauls-electron-rpc

v7.0.3

Published

An RPC solution for exporting APIs from the Electron background process to renderers and webviews.

Downloads

142

Readme

pauls-electron-rpc

Features:

  • Supports RPC calls to/from the renderer or webview or a node child-process to the background process
  • Supports methods which return:
    • Sync values
    • Async CBs
    • Promises
    • Readable streams
    • Writable streams
    • Duplex streams
  • Permissions by examining the sender of the call
  • Monitors renderer/webview lifetime to automatically release streams
  • Optional timeout for async methods

Example usage

In a shared example-api-manifest.js:

module.exports = {
  // simple method-types
  readFile: 'async',
  readFileSync: 'sync',
  sayHello: 'promise',
  createReadStream: 'readable',
  createWriteStream: 'writable',
  createDuplexStream: 'duplex'
}

In the main electron process:

var rpc = require('pauls-electron-rpc')
var manifest = require('./example-api-manifest')
var fs = require('fs')

// export over the 'example-api' channel
var api = rpc.exportAPI('example-api', manifest, {
  // the exported API behaves like normal calls:
  readFile: fs.readFile,
  readFileSync: fs.readFileSync,
  sayHello: () => return Promise.resolve('hello!'),
  createReadStream: fs.createReadStream,
  createWriteStream: /* ... */,
  createDuplexStream: /* ... */
})

// log any errors
api.on('error', console.log)

In the renderer or webview process:

var rpc = require('pauls-electron-rpc')
var manifest = require('./example-api-manifest')

// import over the 'example-api' channel
var api = rpc.importAPI('example-api', manifest, { timeout: 30e3 })

// now use, as usual:
api.readFileSync('/etc/hosts') // => '...'

API

rpc.exportAPI(channelName, manifest, methods, [globalPermissionCheck])

Methods will be called with a this set to the event object from electron ipc. Don't touch returnValue.

You can optionally specify a method for globalPermissionCheck with the following signature:

function globalPermissionCheck (event, methodName, args) {
  if (event.sender.getURL() != 'url-I-trust') return false
  return true
}

If globalPermissionCheck is specified, and does not return true, the method call will respond with a 'Denied' error.

rpc.importAPI(channelName, manifest [,options])

  • options.timeout Number. Specify how long in ms that async methods wait before erroring. Set to false to disable timeout.
  • options.errors Object. Provides custom error constructors.
  • options.wc WebContents. The web-contents that is exporting the API. Use this when importing an API from a webContents into the main thread.
  • options.proc ChildProcess. The child-process that is exporting the API. Use this when importing an API from a node child process into the main thread.

Readable Streams

Readable streams in the clientside are given a .close() method. All serverside streams MUST implement .close() or .destroy(), either of which will be called.

Stream methods can return a promise that resolves to a stream.

Buffers and ArrayBuffers

Arguments and return values are massaged so that they are Buffers on the exporter's side, and ArrayBuffers on the importer side.

Custom Errors

// shared code
// =

var manifest = {
  testThrow: 'promise'
}

class MyCustomError extends Error {
  constructor() {
    super()
    this.name = 'MyCustomError'
    this.message = 'Custom error!'
  }
}

// server
// =

rpc.exportAPI('error-api', manifest, {
  testThrow() {
    return Promise.reject(new MyCustomError())
  }
})

// client
// =

var rpcClient = rpc.importAPI('error-api', manifest, {
  errors: {MyCustomError} // pass in custom error constructors
})
rpcClient.testThrow().catch(console.log) // => MyCustomError