From 1138ea8ad309584b32ee4d6db1167219cd696e88 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Mon, 8 Jul 2024 14:58:35 +0200 Subject: [PATCH] Use standard JavaScript modules (#67) Closes #49 Signed-off-by: Jon Koops Co-authored-by: Mathias Buus --- README.md | 12 ++++++------ cli.js | 8 ++++---- example.js | 6 +++--- include.js | 5 +++-- index.js | 23 ++++++++++++++--------- package.json | 8 ++++++-- 6 files changed, 36 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d5e4c54..891887d 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ npm i why-is-node-running@v1.x -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() @@ -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) ``` @@ -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 diff --git a/cli.js b/cli.js index 15cf2b9..ccd4c6d 100755 --- a/cli.js +++ b/cli.js @@ -1,7 +1,7 @@ #!/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) @@ -9,8 +9,8 @@ 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) diff --git a/example.js b/example.js index 4584d4b..eef3b7a 100644 --- a/example.js +++ b/example.js @@ -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() @@ -11,5 +11,5 @@ createServer() createServer() setTimeout(function () { - log() + why() }, 100) diff --git a/include.js b/include.js index c7ffe28..8a7f568 100644 --- a/include.js +++ b/include.js @@ -1,3 +1,4 @@ -var why = require('./') +import why from './index.js' +import siginfo from 'siginfo' -require('siginfo')(why, true) +siginfo(why, true) diff --git a/index.js b/index.js index 70842c5..1c8e700 100644 --- a/index.js +++ b/index.js @@ -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() @@ -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() @@ -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)) @@ -65,3 +66,7 @@ function whyIsNodeRunning (logger) { } } } + +function normalizeFileName(fileName) { + return fileName.startsWith('file://') ? fileURLToPath(fileName) : fileName +} diff --git a/package.json b/package.json index 9bf6a6d..1ff7f11 100644 --- a/package.json +++ b/package.json @@ -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" @@ -11,7 +15,7 @@ "why-is-node-running": "cli.js" }, "engines": { - "node": ">=8" + "node": ">=20.11" }, "repository": { "type": "git",