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

sn-electron-valence

v0.0.4

Published

> In chemistry, a **valence electron** is an outer shell electron that is associated with an atom, and that can participate in the formation of a chemical bond if the outer shell is not closed

Downloads

6

Readme

Electron Valence

In chemistry, a valence electron is an outer shell electron that is associated with an atom, and that can participate in the formation of a chemical bond if the outer shell is not closed

This module allows to create a seamless and transparent bridge across process's and contexts in an Electron app. For instance you can make a bridge between a preload script and your renderer process when contextIsolation is enabled.

Why?

Enabling Context Isolation and Sandbox mode are two things you should definitely do when building an Electron app, without them your app is a security issue waiting to happen. Unfortunately there is a considerable learning curve when adopting this two technologies, all communication has to be done by passing string messages around when most people are used to just injecting methods with window.magicMethod = ... and calling that method from their renderer. This module aims to make the process of sharing information and API's between isolated contexts or processes easy and transparent.

Basic Usage

// Preload.js
import { Transmitter, FrameMessageBus, Validation } from 'electron-valence/transmitter';

const { PropertyType } = Validation;

const transmitter = new Trasmitter(
	new FrameMessageBus(),
	{
		exampleProp:  PropertyType.VALUE,
		deep: {
			type: PropertyType.OBJECT,
			properties: {
				foo: PropertyType.VALUE,
			},
		},
		sayHi: {
			type: PropertyType.METHOD,
			argValidators: [{ type: 'string', minLength: 3 }],
		},
	},
);

transmitter.expose({
	exampleProp: 'exampleValue',
	deeps: {
		foo: 123,
	},
	sayHi: (name) => `Hey There, ${name}`,
});
// Renderer.js
import { Receiver, FrameMessageBus } from 'electron-valence/reciever';

const receiver = new Receiver(new FrameMessageBus());

receiver.ready.then(async () => {
	const firstItem = receiver.items[0];
	console.log(firstItem);
	console.log(await firstItem.exampleProp);
	console.log(await firstItem.deep.foo);
	console.log(await firstItem.sayHi('Sam'));
})

Hold up, why is this kinda verbose...

Although the main goal of this module is to make bridging easier, it needs to do so in a safe, secure and predictable way. This means that any variable exposed from the transmitter to the receiver must be explicitly declared and any argument sent back from the receiver to the transmitter must be validated on arrival.

You should make your exposed interface and your argument validation as strict as possible. The stricter it is, the safer your code is.

Opinionated Restrictions For Your Own Good

  1. You can't set property values from the proxy object on the receiver side

Taking the above example you can't do this: firstItem.exampleProp = 'foo', this is because setters in Javascript can't be await'ed which will inherently introduce race conditions. If you need to set a property value, you should expose a method which will set the value, this is safer as you can use our built in argument validation and less flakey as you can await the method. I.e. You should do this await firstItem.setExampleProp('bar').

  1. You can't pass non-serializable values backwards through the bridge

The bridge is not completely open in both directions, exposing things from the transmitter to the receiver uses a proxy technique that allows methods to exposed safely. Going in the opposite direction (sending things from the receiver to the transmitter) for instance by calling a function and passing arguments in only allows serializable values. Functions will be nullified when crossing the bridge in that direction. The diagrams below illustrate this

Serial Flow

Advanced Usage

You can use this module to span multiple isolation gaps, for example you can go all the way from an isolated renderer process to the main process.

// Main.js
import { Transmitter, IPCMainMessageBus, Validation } from 'electron-valence/transmitter';

const { PropertyType } = Validation;

const transmitter = new Trasmitter(
	new IPCMainMessageBus(mainWindow),
	{
		exampleProp:  PropertyType.VALUE,
	},
);

transmitter.expose({
	exampleProp: 'exampleValue',
});
// Preload.js
import { Booster, IPCRendererMessageBus, FrameMessageBus } from 'electron-valence/booster';

const booster = new Booster(
	new IPCRendererMessageBus(),
	new FrameMessageBus(),
});

booster.start();
// Renderer.js
import { Receiver, FrameMessageBus } from 'electron-valence/receiver';

const receiver = new Receiver(new FrameMessageBus());

receiver.ready.then(async () => {
	const firstItem = receiver.items[0];
	console.log(firstItem);
	console.log(await firstItem.exampleProp);;
});

In this example we are exposing items from the main process, and receiving them across both a process and context isolated boundary in the renderer process. You can use Booster as many times as you want throughout a messages lifecycle so you can do complicated set ups like so.

Complex Flow