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

proxy-com

v0.0.12

Published

Consume apis that run on different context

Downloads

117

Readme

dist test GitHub license

proxy-com

Consume apis that run on different context.

Suppose you have an api running on Process A and you want to use it on Process B. Usually you will use some IPC mechanism forcing you to pass messages between processes. As the project grows, message passing becomes verbose with a lot of repeated code.

Proxy-com aims to help by implementing a Proxy pattern to the API you want to consume.

Install

From NodeJs:

npm i proxy-com

Or include on your HTML:

<script src="https://unpkg.com/proxy-com@<version>/dist/proxycom-umd.js" />

Replacing <version> with the desired version.

Require or import

const { proxycom } = require("proxy-com");

// or

import { proxycom } from "proxy-com";

Expose an api:

const myApi = {
    foo: () => {
        // do some stuff
    }
}

proxycom.exposeApi({
    apiConfig: <someConfig>, // we will explain this ahead
    transport: <someTransport>, // we will explain this ahead
    api: myApi
});

And then create a proxy to that api:

const proxy = proxycom.createProxy({
    apiConfig: <someConfig>, // we will explain this ahead
    transport: <someTransport>, // we will explain this ahead
});

proxy.foo();

One lib, different contexts

Proxy-com itself is unaware of the context it is used in. It can be used between NodeJs processes, Browser windows, Web Workers, Chrome extensions, Electron processes, etc. To achieve this, it uses specific Transports that do the actual work of sending messages between contexts.

One common scenario is when we want to consume apis that run on a different NodeJs process. This is one possible (simplified) way to create a new process in NodeJs. Consider two sibling files: parentProcess.js and childProcess.js:

// parentProcess.js
const path = require("path");
const { fork } = require("./child_process");
const child = fork(path.resolve(__dirname, "childProcess.js"));

child.on("message", (message) => {
  console.log("parentProcess received:", message);
});

child.send("Message from parentProcess");
// childProcess.js
process.on("message", (message) => {
  console.log("childProcess received:", message);
});

process.send("Message from childProcess");

If you run: node parentProcess.js

You will get the following output:

parentProcess received: Message from childProcess
childProcess received: Message from parentProcess

Now, suppose parentProcess is running some api we want to consume on childProcess:

// parentProcess.js
const path = require("path");
const { fork } = require("child_process");
const child = fork(path.resolve(__dirname, "childProcess.js"));

// we want childProcess to access this api:
const myApi = {
  foo: (arg) => {
    console.log("Foo called with", arg);
  },
};

child.on("message", (message) => {
  console.log("parentProcess received:", message);
});

child.send("Message from parentProcess");

By using proxy-com we don't need to handle the message passing between processes. Instead, we can do the following:

// parentProcess.js

const path = require("path");
const { fork } = require("child_process");
const child = fork(path.resolve(__dirname, "childProcess.js"));

const { proxycom } = require("proxy-com");
const { processTransport } = require("proxy-com/transports/nodejs/process");

const myApi = {
  foo: (arg) => {
    console.log("Foo called with", arg);
    return `Hi ${arg}, nice to meet you!`;
  },
};

proxycom.exposeApi({
  apiConfig: { name: "myApi", props: ["foo"] }, // declaring what methods of myApi we want to expose
  transport: processTransport.getForParentProcess(child), // using a parent process transport specific for multi NodeJs processes
  api: myApi, // exposing the api
});

and:

// childProcess.js

const { proxycom } = require("proxy-com");
const { processTransport } = require("proxy-com/transports/nodejs/process");

const proxy = proxycom.createProxy({
  apiConfig: { name: "myApi", props: ["foo"] }, // declaring what method we want to proxy (usually the same that are exposed)
  transport: processTransport.getForChildProcess(process), // using a child process transport specific for multi NodeJs processes
});

proxy.foo("childProcess").then((response) => {
  console.log(response); // "Hi childProcess, nice to meet you!"
});

Further documentation at:

proxy-com

NPM package:

proxy-com