Skip to content

Commit

Permalink
Use standard JavaScript modules (#67)
Browse files Browse the repository at this point in the history
Closes #49

Signed-off-by: Jon Koops <[email protected]>
Co-authored-by: Mathias Buus <[email protected]>
  • Loading branch information
jonkoops and mafintosh authored Jul 8, 2024
1 parent 93efc6a commit 1138ea8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ npm i [email protected] -g
## Usage

```js
const log = require('why-is-node-running') // should be your first require
const net = require('net')
import why from 'why-is-node-running' // should be your first import
import net from 'node:net'

function createServer () {
const server = net.createServer()
Expand All @@ -32,7 +32,7 @@ createServer()
createServer()

setTimeout(function () {
log() // logs out active handles that are keeping node running
why() // logs out active handles that are keeping node running
}, 100)
```

Expand Down Expand Up @@ -89,12 +89,12 @@ To trigger the log:
kill -SIGUSR1 31115
```

## Require CLI Option
## Import CLI Option

You can also use the node `-r` option to include `why-is-node-running`:
You can also use Node's [`--import`](https://nodejs.org/api/cli.html#--importmodule) option to preload `why-is-node-running`:

```bash
node -r why-is-node-running/include /path/to/some/file.js
node --import why-is-node-running/include /path/to/some/file.js
```

The steps are otherwise the same as the above CLI section
Expand Down
8 changes: 4 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env node

var spawn = require('child_process').spawn
var path = require('path')
import { spawn } from 'node:child_process'
import path from 'node:path'

var prog = path.resolve(process.argv[2])
var progArgs = process.argv.slice(3)

console.log('probing program', prog)

var nodeArgs = [
'-r',
path.join(__dirname, 'include.js')
'--import',
path.join(import.meta.dirname, 'include.js')
]
var nodeOpts = { stdio: 'inherit' }
var child = spawn('node', nodeArgs.concat(prog).concat(progArgs), nodeOpts)
Expand Down
6 changes: 3 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var log = require('./')
var net = require('net')
import why from './index.js'
import net from 'node:net'

function createServer () {
var server = net.createServer()
Expand All @@ -11,5 +11,5 @@ createServer()
createServer()

setTimeout(function () {
log()
why()
}, 100)
5 changes: 3 additions & 2 deletions include.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var why = require('./')
import why from './index.js'
import siginfo from 'siginfo'

require('siginfo')(why, true)
siginfo(why, true)
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var asyncHooks = require('async_hooks')
var stackback = require('stackback')
var path = require('path')
var fs = require('fs')
import asyncHooks from 'node:async_hooks'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import stackback from 'stackback'

var sep = path.sep

var active = new Map()
Expand All @@ -19,9 +21,8 @@ var hook = asyncHooks.createHook({
})

hook.enable()
module.exports = whyIsNodeRunning

function whyIsNodeRunning (logger) {
export default function whyIsNodeRunning (logger) {
if (!logger) logger = console

hook.disable()
Expand Down Expand Up @@ -50,13 +51,13 @@ function whyIsNodeRunning (logger) {
} else {
var padding = ''
stacks.forEach(function (s) {
var pad = (s.getFileName() + ':' + s.getLineNumber()).replace(/./g, ' ')
var pad = (normalizeFileName(s.getFileName()) + ':' + s.getLineNumber()).replace(/./g, ' ')
if (pad.length > padding.length) padding = pad
})
stacks.forEach(function (s) {
var prefix = s.getFileName() + ':' + s.getLineNumber()
var prefix = normalizeFileName(s.getFileName()) + ':' + s.getLineNumber()
try {
var src = fs.readFileSync(s.getFileName(), 'utf-8').split(/\n|\r\n/)
var src = fs.readFileSync(normalizeFileName(s.getFileName()), 'utf-8').split(/\n|\r\n/)
logger.error(prefix + padding.slice(prefix.length) + ' - ' + src[s.getLineNumber() - 1].trim())
} catch (e) {
logger.error(prefix + padding.slice(prefix.length))
Expand All @@ -65,3 +66,7 @@ function whyIsNodeRunning (logger) {
}
}
}

function normalizeFileName(fileName) {
return fileName.startsWith('file://') ? fileURLToPath(fileName) : fileName
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "why-is-node-running",
"type": "module",
"version": "2.3.0",
"description": "Node is running but you don't know why? why-is-node-running is here to help you.",
"main": "index.js",
"exports": {
".": "./index.js",
"./includes": "./include.js"
},
"dependencies": {
"siginfo": "^2.0.0",
"stackback": "0.0.2"
Expand All @@ -11,7 +15,7 @@
"why-is-node-running": "cli.js"
},
"engines": {
"node": ">=8"
"node": ">=20.11"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 1138ea8

Please sign in to comment.