Socket
Socket
Sign inDemoInstall

generate-call-graph

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    generate-call-graph

Generate Call Graph in Node/Electron project


Version published
Weekly downloads
4
Maintainers
1
Install size
27.8 kB
Created
Weekly downloads
 

Readme

Source

This tiny tool is to help generate a complete module relationship graph for Node.js/Electron project.

How to use

const generate = require('generate-call-graph');
const path = require('path');

const dot = generate(require.main, {
  sep: path.sep,
  title: 'Title of Graph',
  ignore: ['path', 'generate-call-graph'],
});

require('fs').writeFileSync('output.dot', dot, 'utf8');

Please notice that API only provides string as result. You will need graphviz to generate the final SVG/PNG graph out of this dot file. For example, run the following script:

# generate PNG version
dot -Tpng -O output.dot
# generate SVG version
dot -Tsvg -O output.dot

Time Cost of Loading Module

This package also contains a tiny utility that helps you to determine the loading time cost of each module. To use this, first load the module as early as possible (for example, load it in preload script in Electron):

require('generate-call-graph/enhancer');

Then, you could generate a graph with time cost labeled:

const generate = require('generate-call-graph');
const path = require('path');

const dot = generate(require.main, {
  sep: path.sep,
  title: 'Title of Graph',
  ignore: ['path', 'generate-call-graph'].map(require.resolve),
  labelTime: true,
  threshold: {
    warn: 50,
    error: 100,
  },
});

require('fs').writeFileSync('output.dot', dot, 'utf8');

In the example above, it will also mark the line as orange if it costs more than 50ms to load, and mark it as red if it costs more than 100ms. Easy to illustrate the potential issue when initializing the app.

Options

Following are possible options to modify the behavior of API:

  • sep: required / string, simply pass require('path').sep will be enough.

    This API is design to be runnable in web environment, thus need to pass environment related info (such as path.sep from outside);

  • title: required / string, title of the generate graph.

  • ignore: optional / string[], list of IDs that should be ignored from generating the graph. In Node.js, each module as a unique ID, which is equal to the path of that file.

  • labelTime: optional / boolean, whether or not the graph should label cost time on each line.

    Please notice that: 1) one module might be loaded multiple times, only the first time will be labeled by time cost, for the rest of usages, cache will be used instead of loading it again; 2) you will need to load generate-call-graph/enhancer utility file as early as possible to get those info beforehand.

  • threshold: optional / { warn?: number; error?: number }, the threshold to warn the loading of modules based on time cost.

    Default threshold are 50ms for warning (orange color) and 100ms for error (red color). You can overwrite these two thresholds using this option.

Keywords

FAQs

Last updated on 06 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc