tunel

tunel is a RESTful message tunnel over IPC.

Usage no npm install needed!

<script type="module">
  import tunel from 'https://cdn.skypack.dev/tunel';
</script>

README

  ___                 /
    _-_-  _/\____    / __\\__
 _-_-__  / ,-. -|   /  -  ,-.`-.
    _-_- `( o )--  /  --( o )-'
           `-'    /      `-'
tunel (n): RESTful message tunneling over IPC.

Quick Example

Here’s the code for Electron’s main thread:

//
// Main Thread
//

const electron = require('electron');
const channel = electron.ipcMain;

const { app, registerChannel } = require('tunel/server');

registerChannel(channel);

// `app` API is similar to `express.js`
app.get('/api/v1/profile', async req => {
  // You can access req.body, req.params…
  void req;

  // Unlike express, there is no `res` object.
  // As soon as you return from the function, you’ll relay
  // a response back.
  // So, returning a JSON `json`, is similar to `res.send(json)`.
  return {
    userName: 'robert',
    fullName: 'Robert Denir Ona'
  };
});

Here’s the cod for Electron’s renderer thread (i.e., the browser):

//
// Renderer Thread
//

import request from 'tunel';

const doFetch = async () => {
  // `request` API is similar to `axios`.
  const response = await request({
    method: 'GET',
    url: '/api/v1/profile',
    data: { some: 'payload' },
    headers: {
      'Content-Type': 'application/json'
    }
  });

  // Will log
  // `{ status: 200, data: { userName: 'robert',
  // fullName: 'Robert Denir Ona' }}`
  console.log(response);
};

doFetch();