kryptheon 0.1.66 → 0.1.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/kryptheon.js CHANGED
@@ -3499,7 +3499,11 @@ function dashboardAlreadyRunning(port) {
3499
3499
  const same = (a) => (process.platform === 'win32' ? path.resolve(a).toLowerCase() : path.resolve(a));
3500
3500
  if (res.statusCode !== 200 || typeof root !== 'string' || same(root) !== same(USER_DIR)) return resolve(false);
3501
3501
  // Before 0.1.41 a dashboard did not say which it was: an old one.
3502
- resolve({ version: typeof state.kryptheon === 'string' ? state.kryptheon : null });
3502
+ // From 0.1.67 it also says which process it is.
3503
+ resolve({
3504
+ version: typeof state.kryptheon === 'string' ? state.kryptheon : null,
3505
+ pid: Number.isInteger(state.pid) && state.pid > 0 ? state.pid : null,
3506
+ });
3503
3507
  } catch (err) {
3504
3508
  resolve(false);
3505
3509
  }
@@ -3510,16 +3514,36 @@ function dashboardAlreadyRunning(port) {
3510
3514
  });
3511
3515
  }
3512
3516
 
3513
- /** The process listening on 127.0.0.1:port, or null. */
3517
+ /** The process listening on 127.0.0.1:port in netstat's lines, or null. */
3518
+ function listenerInNetstat(out, port) {
3519
+ for (const line of String(out || '').split(/\r?\n/)) {
3520
+ const m = line.trim().split(/\s+/);
3521
+ if (m.length >= 5 && m[0] === 'TCP' && m[1] === '127.0.0.1:' + port && m[3] === 'LISTENING' && /^\d+$/.test(m[4]) && Number(m[4]) > 0) return Number(m[4]);
3522
+ }
3523
+ return null;
3524
+ }
3525
+
3526
+ /**
3527
+ * The process listening on 127.0.0.1:port, or null. On Windows netstat is
3528
+ * asked twice, with and without -p TCP, then PowerShell: on one Windows 11
3529
+ * build `netstat -p TCP` left the port out, and plain netstat did not always
3530
+ * list it either.
3531
+ */
3514
3532
  function listenerOn(port) {
3533
+ // For the check that a dashboard's stated process is enough on its own.
3534
+ if (process.env.KRYPTHEON_NO_LISTENER_LOOKUP === '1') return null;
3515
3535
  try {
3516
3536
  if (process.platform === 'win32') {
3517
- const out = spawnSync('netstat', ['-ano', '-p', 'TCP'], { encoding: 'utf8', windowsHide: true, timeout: 10000 }).stdout || '';
3518
- for (const line of out.split(/\r?\n/)) {
3519
- const m = line.trim().split(/\s+/);
3520
- if (m.length >= 5 && m[1] === '127.0.0.1:' + port && m[3] === 'LISTENING' && /^\d+$/.test(m[4])) return Number(m[4]);
3537
+ for (const args of [['-ano'], ['-ano', '-p', 'TCP']]) {
3538
+ const out = spawnSync('netstat', args, { encoding: 'utf8', windowsHide: true, timeout: 10000 }).stdout;
3539
+ const pid = listenerInNetstat(out, port);
3540
+ if (pid) return pid;
3521
3541
  }
3522
- return null;
3542
+ const ps = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command',
3543
+ '(Get-NetTCPConnection -LocalAddress 127.0.0.1 -LocalPort ' + Number(port) + ' -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1).OwningProcess'],
3544
+ { encoding: 'utf8', windowsHide: true, timeout: 15000 }).stdout || '';
3545
+ const pid = Number(ps.trim());
3546
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
3523
3547
  }
3524
3548
  const out = spawnSync('lsof', ['-nP', '-iTCP@127.0.0.1:' + port, '-sTCP:LISTEN', '-t'], { encoding: 'utf8', timeout: 10000 }).stdout || '';
3525
3549
  const pid = Number(out.trim().split(/\s+/)[0]);
@@ -3537,8 +3561,12 @@ function listenerOn(port) {
3537
3561
  * Only the process that answered as this folder's Kryptheon dashboard is
3538
3562
  * stopped. Whether the port came free is the answer.
3539
3563
  */
3540
- async function replaceOldDashboard(port) {
3541
- const pid = listenerOn(port);
3564
+ async function replaceOldDashboard(port, said) {
3565
+ // The process the system says holds the port comes first. A dashboard from
3566
+ // 0.1.67 on also says which process it is: used when the system does not
3567
+ // say, and only if that process is Node.
3568
+ const stated = said && said.pid ? said.pid : null;
3569
+ const pid = listenerOn(port) || (stated && isNodeProcess(stated) ? stated : null) || onlyOtherDashboard();
3542
3570
  if (!pid || pid === process.pid) return false;
3543
3571
  try {
3544
3572
  if (process.platform === 'win32') spawnSync('taskkill', ['/PID', String(pid), '/F'], { windowsHide: true, timeout: 10000 });
@@ -3546,13 +3574,72 @@ async function replaceOldDashboard(port) {
3546
3574
  } catch (err) {
3547
3575
  return false;
3548
3576
  }
3577
+ // Whether the port came free is asked of the port itself, not of netstat.
3549
3578
  for (let i = 0; i < 50; i++) {
3550
- if (listenerOn(port) === null) return true;
3579
+ if (!(await portAnswers(port))) return true;
3551
3580
  await new Promise((r) => setTimeout(r, 100));
3552
3581
  }
3553
3582
  return false;
3554
3583
  }
3555
3584
 
3585
+ /** Whether something accepts connections on 127.0.0.1:port. */
3586
+ function portAnswers(port) {
3587
+ return new Promise((resolve) => {
3588
+ const socket = require('net').connect({ host: '127.0.0.1', port: port });
3589
+ const done = (yes) => { socket.destroy(); resolve(yes); };
3590
+ socket.setTimeout(1000, () => done(false));
3591
+ socket.once('connect', () => done(true));
3592
+ socket.once('error', () => done(false));
3593
+ });
3594
+ }
3595
+
3596
+ /**
3597
+ * A dashboard from before 0.1.67 does not say which process it is. When the
3598
+ * system will not say who holds the port either, the one other process
3599
+ * running `kryptheon.js dashboard` is it - only when there is exactly one,
3600
+ * and never this process or the one that started it (from the home folder a
3601
+ * dashboard runs itself again inside kryptheon-tests).
3602
+ */
3603
+ function onlyOtherDashboard() {
3604
+ if (process.env.KRYPTHEON_NO_LISTENER_LOOKUP === '1') return null;
3605
+ const isDashboard = (line) => /kryptheon[\\/]bin[\\/]kryptheon\.js"?\s+dashboard(\s|$)/i.test(line);
3606
+ let rows = [];
3607
+ try {
3608
+ if (process.platform === 'win32') {
3609
+ const out = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command',
3610
+ "Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" | ForEach-Object { [string]$_.ProcessId + ' ' + $_.CommandLine }"],
3611
+ { encoding: 'utf8', windowsHide: true, timeout: 15000 }).stdout || '';
3612
+ rows = out.split(/\r?\n/);
3613
+ } else {
3614
+ rows = (spawnSync('ps', ['-eo', 'pid=,args='], { encoding: 'utf8', timeout: 10000 }).stdout || '').split(/\n/);
3615
+ }
3616
+ } catch (err) {
3617
+ return null;
3618
+ }
3619
+ const found = [];
3620
+ for (const row of rows) {
3621
+ const m = /^\s*(\d+)\s+(.*)$/.exec(row);
3622
+ if (!m || !isDashboard(m[2])) continue;
3623
+ const pid = Number(m[1]);
3624
+ if (pid !== process.pid && pid !== process.ppid) found.push(pid);
3625
+ }
3626
+ return found.length === 1 ? found[0] : null;
3627
+ }
3628
+
3629
+ /** Whether the process with this id is Node - the only kind a dashboard is. */
3630
+ function isNodeProcess(pid) {
3631
+ try {
3632
+ if (process.platform === 'win32') {
3633
+ const out = spawnSync('tasklist', ['/FI', 'PID eq ' + Number(pid), '/FO', 'CSV', '/NH'], { encoding: 'utf8', windowsHide: true, timeout: 10000 }).stdout || '';
3634
+ return /^"node\.exe","\d+"/im.test(out.trim()) && out.includes('"' + Number(pid) + '"');
3635
+ }
3636
+ const out = spawnSync('ps', ['-p', String(Number(pid)), '-o', 'comm='], { encoding: 'utf8', timeout: 10000 }).stdout || '';
3637
+ return /node/i.test(out);
3638
+ } catch (err) {
3639
+ return false;
3640
+ }
3641
+ }
3642
+
3556
3643
  /**
3557
3644
  * The dashboard, started from the home folder: kept in a kryptheon-tests
3558
3645
  * folder there, the one record already uses - set up the first time, asked
@@ -3645,7 +3732,7 @@ async function dashboardCommand() {
3645
3732
  if (running && running.version !== mine) {
3646
3733
  const was = running.version || 'from before 0.1.41';
3647
3734
  console.log('');
3648
- console.log(await replaceOldDashboard(DASHBOARD_PORT)
3735
+ console.log(await replaceOldDashboard(DASHBOARD_PORT, running)
3649
3736
  ? ' An older Kryptheon dashboard (' + was + ') was running for this folder.\n It was closed, and ' + mine + ' starts in its place.'
3650
3737
  : ' An older Kryptheon dashboard (' + was + ') is still running and could not be\n closed. This one starts at another address; close the old window when you can.');
3651
3738
  }
@@ -1004,6 +1004,10 @@ function buildState(root, env) {
1004
1004
  // Which kryptheon this dashboard is: a newer `dashboard` asks, and takes
1005
1005
  // the place of an older one still running, instead of opening it.
1006
1006
  kryptheon: VERSION,
1007
+ // Its process, so that newer one can close exactly it. Found when
1008
+ // netstat on Windows left the dashboard's port out of its list, and an
1009
+ // old dashboard kept the port.
1010
+ pid: process.pid,
1007
1011
  // a newer kryptheon on npm (from the day's answer, asked when the dashboard started), or null
1008
1012
  update: (() => {
1009
1013
  const latest = newer.cachedLatest('kryptheon');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.66",
3
+ "version": "0.1.67",
4
4
  "description": "Record what you do in your app and get plain-language reports when it breaks.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "commonjs",