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

@nlfmt/electron-bridge

v0.3.0

Published

Typesafe IPC bridge for electron

Downloads

6

Readme

Electron Bridge

Easily define and use IPC functions and events in your Electron app. Fully typesafe.
Inspired by tRPC's architecture.

Installation

npm install @nlfmt/electron-bridge
pnpm add @nlfmt/electron-bridge
yarn add @nlfmt/electron-bridge

Usage

This library is designed to be used with electron apps that enable contextIsolation, sandbox and disable nodeIntegration.

Create bridge

// bridge.ts
import { createBridge, createBridgeRouter } from '@nlfmt/electron-bridge'
  
// You can place routers in separate files and import them here
const exampleRouter = createBridgeRouter({
  log: (e, message: string) => {
    console.log("Message from IPC:", message)
  },
})

export const bridge = createBridge({
  example: exampleRouter,
})

Register bridge

// main.ts
import { bridge } from './bridge'

// This will call ipcMain.handle() under-the-hood and set up all listeners
// Don't forget to call this before you send any IPC messages
bridge.register()

Register preload script

If you have sandbox enabled, you wont be able to import registerBridgePreload, unless you are using a bundler, such as vite with the vite-plugin-electron plugin. For a workaround, see the Issues section.

// preload.ts
import { registerBridgePreload } from '@nlfmt/electron-bridge/preload'

registerBridgePreload()

Use bridge

// App.tsx
import { createRendererBridge } from '@nlfmt/electron-bridge/renderer'

// Usually would be defined in a separate file like `bridge.ts`
export const bridge = createRendererBridge()

function App() {
  return (
    <button onClick={() => bridge.example.log("Hello from App.tsx")}>
      Send message
    </button>
  )
}

export default App

Set up Events

Register Events using the withEvents method on the bridge.

// bridge.ts

type RendererEvents = {
  userClickedButton: { message: string }
}
type MainEvents = {
  ping: { data: number }
}

export const bridge = createBridge({
  example: exampleRouter,
}).withEvents<RendererEvents, MainEvents>()

// send events every second
setInterval(() => {
  bridge.emit("ping", { data: Math.random() })
}, 1000)

bridge.on()

Then, use the events api in the renderer:

// App.tsx
import { createRendererBridge } from '@nlfmt/electron-bridge/renderer'
import { useEffect } from 'react'

export const bridge = createRendererBridge()

function App() {

  useEffect(() => {
    // methods like `on` and `once` return a function that can be used to unsubscribe
    return bridge.events.on("ping", (e, data) => {
      console.log("Ping from main:", data)
    })
  }, [])
  
  return (
    <button onClick={() => bridge.example.log("Hello from App.tsx")}>
      Send message
    </button>
  )
}

Issues

Preload scripts without a bundler

If you are not using a bundler, you can use the following workaround to register everything needed by the bridge:

import { contextBridge, ipcRenderer } from "electron"

contextBridge.exposeInMainWorld("__bridge", {
  invoke: ipcRenderer.invoke,
  emit: ipcRenderer.send,
  on: (...args: Parameters<typeof ipcRenderer.on>) => {
    ipcRenderer.on(...args)
    return () => ipcRenderer.off(...args)
  },
  once: (...args: Parameters<typeof ipcRenderer.once>) => {
    ipcRenderer.once(...args)
    return () => ipcRenderer.off(...args)
  },
  off: ipcRenderer.off,
})

License

See LICENSE for more information.