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

stack-graph

v1.10.2

Published

Build graphviz from stack definition file. Outputs .dot file, .json file (structure), .md file (todo list), and .png or .svg graph. Companion to sst-serverless microservice stack

Downloads

17

Readme

Dependency graph

Service to create visual dependency graphs of services setup.

Getting started

Install npm package

npm install stack-graph

Create a stackdef folder and stackdef file

└───stackdef/
    └───stackdef.js

The stackdef file's export default should be a javascript object containing a stack definition

Running locally

Install the graphviz locally, e.g. with Homebrew

brew install graphviz

Local use with a index.js file

const { resolve } = require('path')
const { localGraph, localVerify } = require('stack-graph')

const currentPath = resolve('./')

async function main() {
    const result = await localGraph({
        inputFilename: 'stackdef.js',
        outputFilename: 'service-map',
        graphFormat: 'png', // or 'svg'
        path: currentPath // required
    })
    console.log(result.valid) // true (or errors)

    const validationResult = await localVerify({
        inputFilename: './stackdef.js',
        path: currentPath // required
    })
    console.log(result.valid) // true (or errors)}
}

main()

The function can be called through node function, which takes arguments as well:

node index.js -i stackdef.js -o stackdef -e png -p "/Users/wintvelt/DEV/stack-graph/stackdef"

Parameters are:

  • -i stackdef input filename
  • -o output filename (without extension)
  • -e export format (png is default, could be svg too)
  • -p path where input can be found and output will be stored

Function will evaluate args inindex.js file first, then check parameters passed in through node call.

Functions of this service

Converts a stack definition object (of a single service) to

  • a .dot formatted text string fodc, containing internal entities and event flow (for rendering with graphviz)
  • a .json file with the raw, cleaned up structure of nodes and edges
  • a .md file containing all the todos needed to build the code according to the stackdef structure
  • a .png or .svg file with a picture of the stack stucture

If you update the stackdef source and run the function again

  • the .md file with update: any items that were checked in the old file, will also remain checked in the new file
  • all other files will be overwritten

Stack definition format

Basic structure of stack definition is

// stackdef.js
module.exports = {
    serviceName: "spqr-user",
    nodes: []
}

In this structure, nodes are an array of objects. Each object is a node. A typical node object has following structure

{
    name: "createUser.js",
    type: "function",
    subs: [], // only for functions or queues
    pubs: [], // only for functions
    queries: [], // only for functions
    description: "add non-obvious notes" // optional
    cluster: "internal", // optional, can also be "input" or "output"
}

name or type must be provided, where options are

  • type: function|table|auth|bucket|topic|queue|email|schedule
  • name can be anything, with following remarks
    • function type names should end in .js
    • if the name ends in type, e.g. visit-topic, then type does not need to be provided. Because type can be derived from the name
    • if the name is a clear API - e.g. GET /user - then the API type will be inferred

API nodes - input to many services - can optionally be setup in a different way:

{
    path: '/',
    method: 'GET' // or any other valid http method
    type: 'API' // optional, does not need to be provided
}

Best practice is to define nodes only for functions. All other nodes will then be derived from the dependencies.

Use pubs, subs and queries to make connections between nodes. Each item can be a name string (make sure that the type can be derived from the name, e.g. POST /user (API), createUser.js (another function), table (table) Or an item can be an object

{
    name: 'POST /user',
    serviceName: 'external-service', // optional, add if the dependency is to external item
    async: true, // optional for pub, use to indicate if connection is async
    isQuery: true, // optional for pub, use to indicate if connection is a query (read not write)
    filters: {}, // optional for pubs and subs
    description: "add non-obvious notes" // optional
}

Filters are relevant for pub or sub to topics:

  • for pub they should include the relevant attributes added to topic message, e.g. { sections: "profile, photo, address" }
  • for sub they should include the relevant attributes to filter messages for this sub, e.g. { eventName: "MODIFY", sections: "profile" }

Query dependencies get a different color. Async calls are indicated by an open arrow. NB: Streamed events (from auth and db) are technically not asynchronous. For sake of schema, they are noted as async calls. For both 'real' async calls and streamed events, consideration should be given to capture dead letters and/or failed calls from stream.

If a node or dependency item has the string dlq (for dead letter queue) of failover in its name, the node will get a different layout. You would want to monitor these from somewhere else. Best practice is to include a

  • dead letter queue for any function that is invoked asynchronously (from other Lambda, SQS, SNS, S3 etc)
  • or failover queue needed for any function that is invoked by a stream (from db) - needs setup in eventSourceMapping/ trigger. NB: events only logged if the real event source makes the call NB: Auth/ Cognito does not have any options to queue failed events. Cognito triggers are called synchronously, but these triggers are not configured in Lambda. Any failures should be gracefully fed back to invoking API or call - usually a user/ front-end

Example

The following stackdef.js definition:

stackdef.js

Will generate this image: example stack graph

And this todo list:

stackdef.md