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

magic-gateway-js

v0.0.2

Published

A javascript gateway adapter for the MAGIC protocol

Downloads

5

Readme

MAGIC gateway

This is the https magical gateway adapter for javascript server frameworks like express.

Usage

import express from 'express'; // for example
import gateway from 'magic-gateway-js';
import fount from 'fount-js'; // this and sessionless aren't strictly needed, but they are part of the same ecosystem
import sessionless from 'sessionless-node';

const saveKeys = (keys) => { /* save your keys here */ };
const getKeys = () => { /* return your keys here */ };

const fountUser = await fount.createUser(saveKeys, getKeys); // again, Fount isn't necessary, but you will need a user 
                                                             // registered with a MAGIC resolver
await sessionless.generateKeys(() => {}, getKeys); // you have to prime sessionless with how to getKeys.

const spellbook = getSpellbook(); // spellbooks are part of the MAGIC protocol, and you'll need one for your gateway
const myStopName = 'cool-server'; // this string needs to map to a stopName in a spell in the spellbook for 
                                  // you to get a spell cast your way.

const extraForGateway = (spellName) => {
  // return optional extra stuff for the gateway here
};

const onSuccess = (req, res, result) => {
  // do something cool here. req and res are the standard request and response objects in the framework you're using
  result.myStopName = {};
  result.myStopName.says = 'yo'; // you can add to the result object you send back
  res.send(result); // the result needs to make it back to the caster for the spell to complete
};

const app = express(); // standard stuff here, make the app
app.use(express.json());

gateway.expressApp(app, fountUser, spellbook, myStopName, sessionless, extraForGateway, onSuccess);

app.listen(3000);

Woof! I know that's a lot. I've tried to make it as simple and straight forward as possible, but MAGIC always comes with a cost, and the cost in this instance is a lack of a one-line solution (that would of course be possible if someone were to take all this and wrap it up in a single type of MAGIC, and if you do that, please let me know about it!).

So here's what's going on. Spells have two plus n participants: the caster who initiates the spell, the resolver who resolves the spell, and n gateways who act in between the caster and resolver. This lib sets you up to be a gateway (to learn more about these roles please see the MAGIC repo).[^1]

Each participant needs to be registered with the resolver, so that's the first step here with creating the fountUser.

Each spell follows a path through transports and gateways, and those paths are defined in the spellbook. Destinations on the path are represented by a (name, route) tuple, and so your server needs a stopName that maps to where it fits in the spell.

In addition to forwarding the spell, and returning the response, gateways can do three things. They can attach extra information to the spell object that gets forwarded, and modify the result object that gets sent back, and they can perform some action based on success or failure. That last thing is what makes this magical since this server could be some boring cloud whatever, or it could be a raspberry pi wired up to a discoball and strobe light and jukebox that plays September by Earth, Wind, and Fire when you snap your fingers.

But the good news is that once you've assembled all of the above, then all you need to do is call one method, and you're good to go :).

[^1]: The transport matters too. This lib is for https. In the future it may include other server transports like websockets and/or udp.