Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active July 16, 2024 10:34
Show Gist options
  • Save x-yuri/560ba38817a8abadc48b6882aa151251 to your computer and use it in GitHub Desktop.
Save x-yuri/560ba38817a8abadc48b6882aa151251 to your computer and use it in GitHub Desktop.
Parsing nodejs command line

Parsing nodejs command line

a.mjs:

export default function getInspectInfo(nodeOptions, execArgv) {
    const args = (
        nodeOptions?.match(/(?<=^| )--inspect(-brk)?(=\S+)?(?= |$)/g) || []
    ).concat(execArgv.filter(
        a => a.startsWith('--inspect')
    ));
    if (!args.length) return;
    const r = args.reduce((a, i) => {
        const [k, v] = i.split('=', 2);
        if (k == '--inspect-brk') a[0] = k;
        if (!v) return a;
        if (v.includes(':')) {
            const [ip, port] = v.split(':', 2);
            a[1] = ip;
            a[2] = +port;
        } else if (v >= 1024)
            a[2] = +v;
        else
            a[1] = v;
        return a;
    }, ['--inspect', '127.0.0.1', 9229]);
    return r;
}

a.test.mjs:

import t from 'tap'
import getInspectInfo from './a.mjs'

t.equal(getInspectInfo(undefined, []), undefined);

for (const el of [
    // keys
    ['--inspect',     [], ['--inspect',     '127.0.0.1', 9229]],
    ['--inspect-brk', [], ['--inspect-brk', '127.0.0.1', 9229]],
    [undefined, ['--inspect'],     ['--inspect',     '127.0.0.1', 9229]],
    [undefined, ['--inspect-brk'], ['--inspect-brk', '127.0.0.1', 9229]],

    // values: nodeOptions
    ['--inspect=0.0.0.0:2222', [], ['--inspect', '0.0.0.0',   2222]],
    ['--inspect=0.0.0.0',      [], ['--inspect', '0.0.0.0',   9229]],
    ['--inspect=2222',         [], ['--inspect', '127.0.0.1', 2222]],

    // values: execArgv
    [undefined, ['--inspect=0.0.0.0:2222'], ['--inspect', '0.0.0.0',   2222]],
    [undefined, ['--inspect=0.0.0.0'],      ['--inspect', '0.0.0.0',   9229]],
    [undefined, ['--inspect=2222'],         ['--inspect', '127.0.0.1', 2222]],

    // overriding brk
    ['--inspect-brk --inspect', [],                             ['--inspect-brk', '127.0.0.1', 9229]],
    ['--inspect-brk',           ['--inspect'],                  ['--inspect-brk', '127.0.0.1', 9229]],
    [undefined,                 ['--inspect-brk', '--inspect'], ['--inspect-brk', '127.0.0.1', 9229]],
    ['--inspect --inspect-brk', [],                             ['--inspect-brk', '127.0.0.1', 9229]],
    ['--inspect',               ['--inspect-brk'],              ['--inspect-brk', '127.0.0.1', 9229]],
    [undefined,                 ['--inspect', '--inspect-brk'], ['--inspect-brk', '127.0.0.1', 9229]],

    // overriding port
    ['--inspect=1111 --inspect=2222', [],                 ['--inspect', '127.0.0.1', 2222]],
    ['--inspect=1111',                ['--inspect=2222'], ['--inspect', '127.0.0.1', 2222]],
    [undefined,                       ['--inspect=1111',
                                       '--inspect=2222'], ['--inspect', '127.0.0.1', 2222]],

    // overriding ip
    ['--inspect=1.1.1.1 --inspect=2.2.2.2', [],                    ['--inspect', '2.2.2.2', 9229]],
    ['--inspect=1.1.1.1',                   ['--inspect=2.2.2.2'], ['--inspect', '2.2.2.2', 9229]],
    [undefined,                             ['--inspect=1.1.1.1',
                                             '--inspect=2.2.2.2'], ['--inspect', '2.2.2.2', 9229]],
]) {
    t.matchOnlyStrict(getInspectInfo(el[0], el[1]), el[2]);
}
$ docker run --rm -itv "$PWD:/app" -w /app alpine:3.20
/app # apk add nodejs npm
/app # npm i tap
/app # npx tap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment