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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@yagisumi/e7ipc2-electron

v0.1.0

Published

Electron IPC wrapper using invoke and handle.

Readme

@yagisumi/e7ipc2-electron

Electron IPC wrapper using invoke and handle.

NPM version install size TypeScript
Build Status Coverage percentage

Installation

$ npm i @yagisumi/e7ipc2-electron

Requirements

electron v8 or later

Usage

Simply put, the library executes the command with invoke and receives a response of type Promise<Result<T>>.

type Result

type Result<T> = OK<T> | ERR
type OK<T> = {
  ok: true
  error: undefined
  value: T
}
type ERR = {
  ok: false
  error: Error
  value: undefined
}

Define Commands

Define commands with DefineCommands.
The argument of the command is specified by opts, and the return value is specified by ret. opts can be omitted.
Here, we also define the channel to be used in ipcMain and ipcRenderer.

// commands.ts
import type { DefineCommands } from '@yagisumi/e7ipc2-electron';

export const CHANNEL = 'app'

export type Commands = DefineCommands<{
  hello: {
    opts: {
      name?: string
    }
    ret: string
  }
  foo: {
    ret: {
      bar: number
      baz: number
    }
  }
}>

Define Handlers

Define an handler with defineHandlers.
The return value is returned by OK(). If an error occurs, it returns an error by ERR().

// handlers.ts
import { defineHandlers, CmdHandler, OK, ERR } from '@yagisumi/e7ipc2-electron'
import { Commands } from './commands'

const helloHandler: CmdHandler<Commands, 'hello'> = async (_ev, opts) => {
  const name = opts.name ?? 'World'
  return OK(`Hello ${name}!`)
}

const fooHandler: CmdHandler<Commands, 'foo'> = async (_ev, _opts) => {
  const v = Math.random()
  if (v > 0.5) {
    return OK({
      bar: v,
      baz: 1,
    })
  } else {
    return ERR(new Error('puzzling error'))
  }
}

export const handlers = defineHandlers<Commands>({
  hello: helloHandler,
  foo: fooHandler,
})

Create a server in Electron's main process

Create a server with createServer and add an handler.
The server here is a simple wrapper for ipcMain.

import { CHANNEL, Commands } from './lib/commands'
import { handlers } from './lib/handlers'
import { createServer } from '@yagisumi/e7ipc2-electron'
import { ipcMain } from 'electron'

const server = createServer<Commands>(CHANNEL, ipcMain)
server.handle(handlers)

Create a client in Electron's renderer process

Create the client with createClient and execute the command with invoke.

client.invoke({
  $cmd: `command name`,
  ...other options
})

Define API

// api.ts
import { CHANNEL, Commands } from './commands'
import { createClient, ERR } from '@yagisumi/e7ipc2-electron'
import { ipcRenderer } from 'electron'

const client = createClient<Commands>(CHANNEL, ipcRenderer)

export const api = {
  hello: async (name?: string) => {
    return client.invoke({ $cmd: 'hello', name }).catch(ERR)
  },
  foo: async () => {
    return client.invoke({ $cmd: 'foo' }).catch(ERR)
  },
}

export type ApiType = typeof api

Expose API

// preload.ts
import { contextBridge } from 'electron'
import { api } from './api'

contextBridge.exposeInMainWorld('api', api)

Define API window

// api-window.ts
import type { ApiType } from './api'
declare const window: Window & typeof globalThis & { api: ApiType }
export default window

How to use

import window from './api-window'

async funtion example() {
  await window.api.hello('Dolly') // => {ok: true, error: undefined, value: "Hello Dolly!"}

  await window.api.foo() // => {ok: true, error: undefined, value: {bar: 0.6038104610635642, baz: 1}}
  await window.api.foo() // => {ok: false, error: Error: Uncaught Error: puzzling error, value: undefined}
}

License

MIT License