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

muzzle

v0.0.14

Published

Wrapper around [GunDB](https://github.com/amark/gun) to provide alternative syntax, promise support, utility modules, and easy Gun adapter events.

Downloads

3

Readme

muzzle

Wrapper around GunDB to provide alternative syntax, promise support, utility modules, and easy Gun adapter events.

Rewriting Gun docs:

Original:

var gun = Gun();

gun.get('mark').put({
  name: "Mark",
  email: "[email protected]",
});

gun.get('mark').on(function(data, key){
  console.log("update:", data);
});

With Muzzle:

var muzzle = new Muzzle()

muzzle.mark = {
  name: "Mark",
  email: "[email protected]",
}

muzzle.mark.on(data => {
  console.log("update:", data);
})

Second Example:

var cat = {name: "Fluffy", species: "kitty"};
var mark = {boss: cat};
cat.slave = mark;

// partial updates merge with existing data!
gun.get('mark').put(mark);

// access the data as if it is a document.
gun.get('mark').get('boss').get('name').once(function(data, key){
  // `val` grabs the data once, no subscriptions.
  console.log("Mark's boss is", data);
});

// traverse a graph of circular references!
gun.get('mark').get('boss').get('slave').once(function(data, key){
  console.log("Mark is the slave!", data);
});

// add both of them to a table!
gun.get('list').set(gun.get('mark').get('boss'));
gun.get('list').set(gun.get('mark'));

// grab each item once from the table, continuously:
gun.get('list').map().once(function(data, key){
  console.log("Item:", data);
});

// live update the table!
gun.get('list').set({type: "cucumber", goal: "scare cat"});

With Muzzle:

var cat = { name: 'Fluffy', species: 'kitty' }
var mark = { boss: cat }
cat.slave = mark

// partial updates merge with existing data!
muzzle.mark = mark

// access the data as if it is a document.
let marksBoss = await muzzle.mark.boss.name.value
console.log("Mark's boss is", marksBoss)

// add both of them to a table!
muzzle.list.set(muzzle.mark.boss)
muzzle.list.set(muzzle.mark)

// grab each item once from the table, continuously:
muzzle.list.map().once(data => {
  console.log("Item:", data)
})

// live update the table!
muzzle.list.set({ type: "cucumber", goal: "scare cat" })

How is this possible?

It's simple really. Muzzle wraps the Gun instance with a Proxy. Each property lookup (using dot notation or bracket) calls a gun.get() and returns the Proxy to be used for chaining. Any call to muzzle.value will return a promise getter of gun.once().

Utilities:

  • .value - Promise getter for .once(); let cats = await muzzle.cats.value
  • .remove() - muzzle.cats.remove()

Writing your own Gun adapter, made easy:

One of the best features of Muzzle is to write Gun adapters without having to know too much about the Gun constructor, proper placement to initialize your adapter, and bugs caused by not forwarding the events. Muzzle takes care of all of this for you! Simply define a Function or Class, return an object that contains the events you want to hook into, and the rest is up to you.

Example class:

class gunAdapter {
  constructor(muzzle, opts, context) {
    return {
      events : {
        // Storage
        get: function(...),
        put: function(...),
        
        // Wire
        in: function(...),
        out: function(...),
      }
    }
  }
}

Example function:

function gunAdapter(muzzle, opts, context) {
  return {
    events: { 
      // Storage
      get: function(...),
      put: function(...),
        
      // Wire
      in: function(...),
      out: function(...),
    }
  }
}

To use your adapter, include it in your project then: muzzle.extend(gunAdapter)

More coming soon!

Muzzle expects to be a wrapper for other utility functions, offering an easy way to extend either muzzle or gun via Proxies or directly. This will allow you to create custom muzzle methods for wrapping verbose syntaxes.

Some utility proposals have already emerged, such as handling arrays in a more suitable fashion.

muzzle.cats.sparky = { color: 'orange' }
muzzle.cats.howie = { color: 'white' }

muzzle.mark.cats = [muzzle.cats.sparky, muzzle.cats.howie]

Another utility proposal was RPC

// local
muzzle.rpc.host('peerName')
muzzle.rpc.register('procName', function(data) {})

// Remote
muzzle.rpc.exec('procName', { some: 'data' })
// or
muzzle.rpc.select('peerName').exec('procName')

License:

MIT