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

hyperbridge

v0.2.1

Published

A library for smooth communication between different javascript contexts

Downloads

65

Readme

HyperBridge

A library for smooth communication between different javascript contexts

API References

  • Fast. It has many performance optimization to track changes only from needed instances.
  • Typed. The library provide full coverage typings via TypeScript.
  • Small. We try to minimize distributed size and use tiny dependencies.

Version Size on bundlephobia Openned issues License MIT

🚀 Quick Start

1. Install the library from NPM

Execute this command in your project to install the library as new dependency:

npm install --save hyperbridge

Or if you using yarn:

yarn add hyperbridge

2. Define your API.

import { declareApi } from "hyperbridge";

export const api = declareApi({
  actions: {
    test1: () => console.log("test1"),
    test2: () => Promise.resolve().then(() => console.log("test2")),
  },
  queries: {
    load1: () => ({ value: "sync" }),
    load2: () => Promise.resolve().then(() => ({ value: "async" })),
  },
  stores: {
    timer: declareStore((set) => {
      let value = 0;
      const callUpdater = () => set(value);
      setInterval(callUpdater, 1000);
    }),
  },
});

3. Create client info provider

Not required for Worker.

import { ClientInfoProvider } from "hyperbridge";

export type ClientInfo =
  | {
      readonly type: "my-client-one";
      readonly foo: "additional-value";
    }
  | { readonly type: "my-client-two" };

export const clientInfoProvider = new ClientInfoProvider<ClientInfo>((info) => {
  if (info.type === "my-client-one") {
    return `one_${info.value}`;
  }
  if (info.type === "my-client-two") {
    return `two`;
  }

  throw new Error(`Unknown client info: ${JSON.stringify(info)}`);
});

4. Create server

Use here WorkerHyperBridgeServer or ChromeHyperBridgeServer instead of abstract HyperBridgeServer

import { HyperBridgeServer } from "hyperbridge";
import { api } from "./api";
import { clientInfoProvider } from "./clientInfoProvider";

const server = new HyperBridgeServer({ api, clientInfoProvider, ... });

5. Use it on client-side

Use here WorkerHyperBridgeClient or ChromeHyperBridgeClient instead of abstract HyperBridgeClient

import { HyperBridgeClient } from "hyperbridge";
import { clientInfoProvider } from "./clientInfoProvider";

const client = new HyperBridgeClient({
  clientInfo: { type: "my-client-two" },
  clientInfoProvider,
  ...
});

// Actions are return nothing
client.actions.test1();
client.actions.test2();

// Queries always returns promise with response from server
client.queries.load1().then(console.log);
client.queries.load2().then(console.log);

// Stores have `null` state by default after creating.
// After receiving every update from server it will notify all subscribers
client.stores.timer.subscribe((fooValue) => console.log(fooValue));

🤖 Worker

TODO

Server

import { WorkerHyperBridgeServer } from "hyperbridge";
import { api } from "./api";

const server = new WorkerHyperBridgeServer({ api });

export type Server = typeof server;

Client

import { useWorkerHyperBridge } from "hyperbridge";
import type { Server } from "./server";

const worker = new Worker("./server.js");
const client = useWorkerHyperBridge<Server>({ worker });

💽 Chrome Extension

TODO

Server

import { ChromeHyperBridgeServer, ChromeHealthCheckServer } from "hyperbridge";
import { api } from "./api";

// required only once for auto-reloading all connected clients
const healthCheck = new ChromeHealthCheckServer(chrome);

const ntpServer = new ChromeHyperBridgeServer({
  id: "ntp",
  clientInfoProvider,
  api,

  // Optional callbacks:
  onConnect: (port) =>
    console.log(`Client with name "${port.name}" has been connected`),
  onDisconnect: (port) =>
    console.log(`Client with name "${port.name}" has been disconnected`),
});

export type NtpServer = typeof ntpServer;

Client

import { useChromeHyperBridge, connectToBackgroundPort } from "hyperbridge";
import { clientInfoProvider } from "./clientInfoProvider";
import type { NtpServer } from "./server";

const client = useChromeHyperBridge<NtpServer>({
  port: connectToBackgroundPort(`ntp_${crypto.randomUUID()}`),
  clientInfo: { type: "my-client-two" },
  clientInfoProvider,
});

✨ API References

TODO