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

enime-nestjs-electron-ipc-transport

v1.0.2

Published

## Description

Downloads

7

Readme

nestjs-electron-ipc-transport

Description

nestjs-electron-ipc-transport is a custom micro-service transport for NestJS, and let you integrate NestJS into your electron main process in a more reasonable way.

Keep in mind that NestJS is a framework designed for back-end, so this integration solution is design for code in main process. In traditional B/S architecture, user's browsers running front-end application, servers running back-end application, and communicate using HTTP protocol with each other. In electron's scenario, renderer process is the front-end, main process is the back-end, and IPC is the communication protocol.

How to use this package properly?

Basically, this package act as a custom micro-service transport for NestJS, and add event listeners automatically, when messages send from renderer process, the respective method in controller will be called. To send messages safely, you should use contextBridge offered by electron and enable contextIsolation.

Install

yarn add nestjs-electron-ipc-transport

or

npm install nestjs-electron-ipc-transport

Usage

see full example here

Bootstrap your nestjs app

import { NestFactory } from '@nestjs/core';
import { app as electronApp, BrowserWindow } from 'electron';
import { MicroserviceOptions } from '@nestjs/microservices';
import { ElectronIPCTransport } from 'nestjs-electron-ipc-transport';
import { resolve } from 'path';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      strategy: new ElectronIPCTransport(),
    },
  );

  app.listen(() => console.log('app started'));
  electronApp.whenReady()
    .then(async () => {
      const win = new BrowserWindow({
        webPreferences: {
          contextIsolation: true,
          preload: resolve(__dirname, './bridge/index.js'),
        },
      });
      await win.loadFile('../../dist/renderer/index.html');
    });
}

bootstrap();

Add bridge API for sending messages

import { contextBridge, ipcRenderer } from 'electron';

export const Bridge = {
  app: appBridge,
};

contextBridge.exposeInMainWorld(
  'AppBridge',
   {
       app: {
           maximize() => ipcRenderer.send('app.maximize'),
       	   getUser() => ipcRenderer.invoke('app.get-user'),
    	   prompt(message) => ipcRenderer.send('app.prompt', message),
       }
   },
);

Link messages to controller

import { Controller } from '@nestjs/common';
import { BrowserWindow, dialog } from 'electron';
import { AppService } from './app.service';
import { HandleIPCMessageWithResult, HandleIPCMessage, IPCContext } from 'electron-ipc-transport';
import { Ctx, Payload } from '@nestjs/microservices';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {
  }

  @HandleIPCMessageWithResult('app.get-user')
  getUser() {
    return {
        name: 'NimitzDEV'
    };
  }
    
  @HandleIPCMessage('app.prompt')
  promptFromMain(
    @Payload() data: string,
  ) {
    dialog.showMessageBox({
        title: 'Messages from renderer',
        message: data,
    });
  }

  @HandleIPCMessage('app.maximize')
  max(
    @Ctx() ctx: IPCContext,
  ) {
    BrowserWindow.fromWebContents(ctx.evt.sender).maximize();
  }
}

APIs

@HandleIPCMessageWithResult(channel)

As name suggest, methods decorated with this decorator will return it's results to renderer process. Corresponds to ipcRenderer.invoke and ipcRenderer.handle.

@HandleIPCMessage(channel)

Mthods decorated with this decorator will be called without passing it's results to renderer process.

Limitations

You could only pass all your parameters inside first parameter slot.

ipcRenderer.send('app.message', 'hello', 'word'); // @Payload will only received 'hello'
ipcRenderer.send('app.message', {title: 'hello', message: 'word'}); // This is the proper way to pass more than one parameters.

Interface

IPCContext contains one property call evt, it's the same as IpcMainEvent object.

License

This package is MIT Licensed.