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

pubsub-to-rpc-api

v8.0.2

Published

Converting publish-subscribe/IPC-like interaction model into the request/response model powered by RxJS

Downloads

106

Readme

pubsub-to-rpc-api

Is a Node.js / browser library that converts publish-subscribe / IPC - like interaction model into the request/response model with provider and client parties involved. So it's like flattening pub/sub interactions into the Observables/Promises-based API. It comes with type safety out of the box, thanks to TypeScript.

GitHub Actions CI

Getting started

Your project needs rxjs@6 to be installed, which is a peer dependency of this module.

Example-related source code is located here, can be executed by running yarn example console command.

Let's first describe API methods and create service instance (shared/index.ts):

// no need to put implementation logic here
// but only API definition and service instance creating
// as this file is supposed to be shared between provider and client implementations

import {ActionType, ScanService, createService} from "lib";

const apiDefinition = {
    evaluateMathExpression: ActionType.Promise<string, number>(),
    httpPing: ActionType.Observable<Array<{
        address?: string;
        port?: number;
        attempts?: number;
        timeout?: number;
    }>, { domain: string } & ({ time: number } | { error: string })>(),
};

export const API_SERVICE = createService({
    channel: "some-event-name", // event name used to communicate between the event emitters
    apiDefinition,
});

// optionally exposing inferred API structure
export type ScannedApiService = ScanService<typeof API_SERVICE>;

ActionReturnType.Promise and ActionReturnType.Observable return values used to preserve action result type in runtime so the client-side code is able to distinguish return types not knowing anything about the actual API implementation at the provider-side.

API implementation, ie provider side (provider/index.ts):

import tcpPing from "tcp-ping";
import {evaluate} from "maths.ts";
import {from, merge} from "rxjs";
import {promisify} from "util";

import {API_SERVICE, ScannedApiService} from "../shared";
import {EM_CLIENT, EM_PROVIDER} from "../shared/event-emitters-mock";

export const API_IMPLEMENTATION: ScannedApiService["ApiImpl"] = {
    evaluateMathExpression: async (input) => Number(String(evaluate(input))),
    httpPing(entries) {
        const promises = entries.map(async (entry) => {
            const ping = await promisify(tcpPing.ping)(entry);
            const baseResponse = {domain: ping.address};
            const failed = typeof ping.avg === "undefined" || isNaN(ping.avg);

            return failed
                ? {...baseResponse, error: JSON.stringify(ping)}
                : {...baseResponse, time: ping.avg};
        });

        return merge(
            ...promises.map((promise) => from(promise)),
        );
    },
};

API_SERVICE.register(
    API_IMPLEMENTATION,
    EM_PROVIDER,
    // 3-rd parameter is optional
    // if not defined, then "EM_PROVIDER" would be used for listening and emitting
    // but normally listening and emitting happens on different instances, so specifying separate emitting instance as 3rd parameter
    {
        onEventResolver: (payload) => ({payload, emitter: EM_CLIENT}),
        // in a more real world scenario you would extract emitter from the payload, see Electron.js example:
        // onEventResolver: ({sender}, payload) => ({payload, emitter: {emit: sender.send.bind(sender)}}),
    },
);

Now we can call the defined and implemented methods in a type-safe way (client/index.ts):

// tslint:disable:no-console

import {API_SERVICE} from "../shared";
import {EM_CLIENT, EM_PROVIDER} from "../shared/event-emitters-mock";

const apiClient = API_SERVICE.caller({emitter: EM_PROVIDER, listener: EM_CLIENT});
const evaluateMathExpressionMethod = apiClient("evaluateMathExpression"/*, {timeoutMs: 600}*/);
const httpPingMethod = apiClient("httpPing"/*, {timeoutMs: 600}*/);

evaluateMathExpressionMethod("32 * 2")
    .then(console.log)
    .catch(console.error);

httpPingMethod([{address: "google.com", attempts: 1}, {address: "github.com"}, {address: "1.1.1.1"}])
    .subscribe(console.log, console.error);

And here is how API methods test structure might look (we leverage combination of Api model and TypeScript's Record type to make sure that tests for all the methods got defined, see provider/api.spec.ts):

import test, {ExecutionContext, ImplementationResult} from "ava";
import {bufferCount} from "rxjs/operators";

import {API_IMPLEMENTATION} from ".";

const apiActionTests: Record<keyof typeof API_IMPLEMENTATION, (t: ExecutionContext) => ImplementationResult> = {
    evaluateMathExpression: async (t) => {
        t.is(25, await API_IMPLEMENTATION.evaluateMathExpression("12 * 2 + 1"));
    },
    httpPing: async (t) => {
        const entries = [
            {address: "google.com", attempts: 1},
            {address: "github.com"},
            {address: "1.1.1.1"},
        ];

        const results = await API_IMPLEMENTATION
            .httpPing(...entries)
            .pipe(bufferCount(entries.length))
            .toPromise();

        // type checking like assertions implemented below are not really needed since TypeScript handles the type checking

        t.is(results.length, entries.length);

        for (const result of results) {
            if ("time" in result) {
                t.true(typeof result.time === "number");
                t.false("error" in result);
                continue;
            }

            t.true("error" in result && typeof result.error === "string");
        }
    },
};

for (const [apiMethodName, apiMethodTest] of Object.entries(apiActionTests)) {
    test(`API: ${apiMethodName}`, apiMethodTest);
}