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

introspec

v0.0.1

Published

Dependencies and configuration described through data

Downloads

3

Readme

Introspec

Dependencies and configuration described through data.

npm install introspec

Build Status Coverage Status

Why even?

Introspec is a variation of Integrant for JavaScript. A great talk by the author of the framework goes into the details of why it simplifies system structure.

Re-iterating one main point here:

Code obscures structure

Component systems often leave dependency injection to the code. For example, when component A needs component B to do its job.

class B {}
class A {}

const b = new B({ username: 'bender', password: 'antiquing' })
const a = new A({ bot: b, debug: false, bug: true })
a.job()

The problem is that this dependency carries a lot of overhead configuring A. We know need to know the invocation of B, the instantiation order, what options both A and B need. Some of this is avoidable, but consider if we put this into a data structure instead.

import {ref} from 'introspec'
export default {
  a: {
    bot: ref('b'), // ref points to a top key in the same config
    debug: false,
    bug: true
  },
  b: {
    username: 'bender',
    password: 'antiquing'
  }
}

The data structure isolates the dependencies and configuration. The invocation happens, unavoidably, in the code. We do stop caring how some B gets to some A. The other benefit is we can query our configuration in one place to get values like b.username.

Documentation

Introspec has a three function API surface.

ref

  • ref(key: string) returns an internal symbol for pointing to a top-level configuration value. This is the function that applies to building configurations.

start

  • start(key: string, startFn: (options: map) -> Promise<service: any>) assigns a startup hook to the top-level config property with the given key which calls with its instantiated options when that property resolves for usage.

  • start(config: object, entryPoints: Array<string>) completion: Promise<system> resolves the dependency graph for top-level entryPoints, applying the startup hooks for the respective dependencies, and returns a promise which resolves with a started system reference.

stop

  • stop(key: string, stopFn: (service: any) -> Promise<_: any>) assigns a shutdown hook to the top-level config property with the given key which calls with the startup result when that property resolves for destruction.

  • stop(system) completion: Promise shuts down the system starting with the top-level entryPoints, applying the shutdown hooks for the respective dependencies, and returns a promise which resolves on completion.

Non-global Introspecs

Introspec exports a default instance for convenience. Make Introspec instances with their own lifecycle hook registries and nest them at will.

import {Introspec} from 'introspec'
const myIntrospec = new Introspec()

myIntrospec.ref('b')
myIntrospec.start({ b: 'cake' }, ['b']).then(system => {
  return myIntrospec.stop(system)
})

Introspec IRL

For larger projects, this data structure based dependency graph shines.

Imagine the following application:

  • Five services: a HTTP(S) webserver, Redis connection, Postgres connection, email client, and payments API.
  • The webserver uses the four other services handling requests.
  • The email client needs Redis for storing email link tokens.
  • The payments API needs both Redis and Postgres for persistence.
import {ref, start, stop} from 'introspec'

/*
  Environment variables and intermediate transformations are fine.
  Configs are plain data structures after all.
*/
const config = {
  webserver: {
    port: 8080,
    cache: ref('redis'),
    database: ref('postgres'),
    email: ref('email'),
    payments: ref('payments')
  },
  redis: 'redis://...',
  postgres: 'postgres://...',
  email: {
    from: '[email protected]',
    redis: ref('redis')
  },
  payments: {
    apiKey: 'xkcd12b4ucab',
    cache: ref('redis'),
    database: ref('postgres')
  }
}

/*
  Start/stop hooks can fit anywhere, preferably with
  the relevant code (which I was too lazy to mock here).
*/
start('webserver', ({port, cache, database, email, payments}) => {})
stop('webserver', webserver => {})

start('redis', uri => {})
stop('redis', connection => {})

start('postgres', uri => {})
stop('postgres', connection => {})

start('email', ({from, redis}) => {})
stop('email', client => {})

start('payments', ({apiKey, cache, database}) => {})
stop('payments', service => {})

start(config, ['webserver']).then(app => {
  return stop(app) // sometime later
})

Contributing

Contributions are incredibly welcome as long as they are standardly applicable and pass the tests (or break bad ones). Tests are in AVA.

# running tests
npm run test

Follow me on Twitter for updates or for the lolz and please check out my other repositories if I have earned it. I thank you for reading.