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

blocs

v2.0.0

Published

A bloc is a single loosely coupled container that listens for events then processes the input from the invoked event and emits the results.

Downloads

4

Readme

Node Blocs

A bloc is a single loosely coupled module that listens for events then processes the input from the invoked event and emits the results.

By leveraging the event system, a block can be indirectly invoked and can easily be swapped or changed out, without having to refactor the entire application.

A bloc/pkg is a container that can house multiple blocs for a single domain.

Each bloc must implement the following functions on a object:

  • validate - this function takes the payload and returns an object, the object must have an ok node and it should be true or false
  • exec - this function performs the functionality of the bloc and it should return a promise
  • test - this function returns a function that runs the test suite of the bloc

Each bloc must be wrapped in a bloc pkg so that it can be registered for usage.

Install

npm install blocs --save

Usage

Create a Block Package

const pkg = require('blocs/pkg')

module.exports = pkg({
  docs: '',
  blocs: {
  }
})

Invoke a bloc

const exec = require('blocs/promise')(bus)

const results = await exec('MYBLOC', payload)

How to use a bloc

All blocs can be invoked using callbacks or promises:

const exec = require('bloc-promise')(bus)

// exec [type] [payload]
exec('GEOCODE', { address: '1740 Foo Street', city: 'Racoon City', state: 'NJ', zip: '08057'})
  .then(res => {
    console.log('success', res)
  })
  .catch(err => {
    console.error('error', err)
  })

A bus is any type of event emitter that enables the blocs to listen for events and emit results. nanobus is a good first choice, but you can use any node object that supports .on, .once, and .emit methods:

See https://nodejs.org/api/events.html#events_class_eventemitter for more info

Blocs also have documentation built in them that you can pull dynamically:

const { docs } = require('bloc-promise')(bus)

docs('GEOCODE').then(docs => console.log(docs))

You can also get a list of all the blocs registered on a bus.

const { index } = require('bloc-promise')(bus)

index().then(blocs => console.log(blocs))

How to build a bloc and a blocpkg

A blocpkg is a package that manages the shared config of a bloc and registers the bloc on a bus.

To register a blocpkg on a bus you would do the following:

const geocodepkg = require('blocpkg-geocode')
// config would be any keys or basic config needed for your bloc
// bus would be the bus that this bloc would be registered on
const { blocs, validate } = geocodepkg.register(config, bus)

By passing the config and the bus in the register step you will get all of the types returned back, including the docs too if you want them.

The types can be used in your application to not have to pass a string in your exec function.

The validatePayload is a function that you can use to get a true or false if your payload object is valid. For Example,

const exec = require('bloc-promise')(bus)
const payload = { address: '1740 Foo Street', city: 'Racoon City', state: 'NJ', zip: '08057'}
if (validate(blocs.GEOCODE, payload)) {
  result = await exec(blocs.GEOCODE, payload)
}

Ok, back to how to implement a bloc and blocpkg

Take a look at this example.

const blocpkg = require('blocpkg')
module.exports = blocpkg({
  docs: `

Markdown Document

About the pkg and the blocs

  `,
  blocs: {
    GEOCODE: {
      validate (payload) {
        // must return a object that has at least an ok property that is a boolean
        return { ok: true }
      },
      exec (payload) {
        // return a promise
      },
      test (payload) {
        // return a function
      }
    }
  }
})

const blocpkg = require('blocpkg')

module.exports = blocpkg({
  docs: fs.readFileSync('./README.md', 'utf-8'),
  blocs: {
    GEOCODE: {
      validate: require('./validate'),
      exec: require('./exec'),
      test: require('./test')
    }
  }
})