Code Monkey home page Code Monkey logo

use-node-inspector's Introduction

isDependenciesFree👌 && isSimple && isPure

Did you know that node --inspector can be opened programmatically?

  • However, this often imposes difficulties when the process may not complete correctly and it will run in the background.
  • Unfortunately, this often happens in nodejs applications, when during development you can spawn a huge number of background processes that take up your computer's resources, memory / processor / graphics memory / ...
  • For example if you used nodemon / hot module replacement (create new instance of any child process, use killProcessOnPort, killProcessByPid) / ...

This package tries to solve this problem by adding a function to kill the current process for sure, as well as giving you functions to manually close a process by its pid or port.

import { killProcessOnPort, killProcessByPid } from 'use-node-inspector'

Note: worked only in darwin || linux, maybe later will be added windows (version 3)

This is version 2 and it takes into account the shortcomings of the first version

How it works:

1. Call useNodeInspector anywhere (probably the beginning of the program)

import { useNodeInspector, killProcessOnPort } from 'use-node-inspector'
useNodeInspector({
    port: 9229,
    // current process will not be killed until all async operation are completed
    beforeKillProcess: async () => {
        // async example operation
        await new Promise(resolve => anyCallBackForExample(resolve))
        // for example you need killed your db connection process / web sockets / express / koa / any
        killProcessOnPort(6969)
    },
    // if not clear console your will have log with previous processes
    isClearConsole: true,
})

2. Open debugger

Open debugger

3. And you will have

Opened debugger

Take it easy

Under the hood

1. Start

Alt text

2. process.stdin.resume and listen 'exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException'

Alt text

3. If for some reason a previous unfinished process is found, it will be killed. The process is recorded in prevProcessInfo.json (write/rewrite auto)

type TProcessInfo = {
    pid: number
}

Alt text

Utils kill process on port, kill process by pid

Alt text

use-node-inspector's People

Contributors

100100101 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.