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

eipc-api

v1.0.1

Published

Expose namespaced API's that connect the render processes with the main process using electron's IPC feature.

Downloads

10

Readme

eipc-api

This module is designed to allow for easily designing modular/namespaced API's and exposing them to the browser process through electron's IPC functionality.

Installation

npm install eipc-api

Usage

lib/api.js - api declaration:

const IpcApi = require('eipc-api');

const api = {
    ns1: {
        method1: function() { /* do something */ },
        method2: function() { /* do something */ },
    }
};

module.exports = new IpcApi(api);

electron.js - main electron file, runs in main process

const { app, BrowserWindow, ipcMain } = require('electron');
const api = require('.lib/api.js');

...

app.whenReady().then(() => {
    api.registerHandlers(ipcMain);
});

...

preload.js - electron preload file, exposes to renderer process


const { contextBridge, ipcRenderer } = require('electron');
const api = require('./lib/api.js');

...
const invoker = api.getInvoker(ipcRenderer)
contextBridge.exposeInMainWorld('api', invoker);

client.js - javascript running in the browser renderer process

...

const returnValue = window.api.ns1.method1(/* params */);

...

IpcApi

Constructor

IpcApi(api [, config])

  • api - an object containing, methods, properties, and namespaced properties to parse and register.
  • config - object containing config options for the api
    • includeBaseMethods - boolean - if true any "base methods" (methods defined on the root api object, and not in a namespace) will be included in the generated api object. Base methods will be registered and have an invoker handler created for them just like namespaced methods.
    • includeBaseProperties - array - a list containing the names of base properties (any non-function and non-namespace properties defined on the root api object) to include in the generated api. Base properties will be included in the generated invoker, but wil not have any IPC functionality. You can use this to pass static data from the main to the render process.

Instance Methods

registerHandlers(ipcMain) - register handlers in the main process using the provided ipcMain objects handle function. This sets up the main process to handle IPC requests from the renderer process

  • ipcMain - the ipcMain object provided by electron

getInvoker(ipcRenderer) - generates and returns an api invoker object that includes namespaced methods and optionally base properties/methods, if configured to. The generated invoker will have the same structure as the originally passed api object, but any methods will automatically handle the calling of ipcRenderer.invoke(), passing params, and returning any return value that might exist.

Test

npm test

Real World Example

img-keeper

I designed this package for use in my project github.com/slimnate/img-keeper, check it out. Relevant files:

  • src/lib/api.js
  • public/electron.js
  • public/preload.js