kryptheon 0.1.48 → 0.1.49

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
@@ -49,6 +49,11 @@ const MIN_NODE = [20, 6, 0];
49
49
  const WINDOW_POLL_MS = 5000;
50
50
  const WINDOW_TIMEOUT_MS = 30000;
51
51
  const WINDOW_HARD_LIMIT_MS = 90000;
52
+ // A browser left running with no window confirmed is watched on, quietly, and
53
+ // if this long goes by from the start with still none seen, the person is told
54
+ // once what to look at. Found on Windows 10: no window for an hour, and
55
+ // nothing said after the first minute and a half.
56
+ const WINDOW_LATE_MS = 3 * 60 * 1000;
52
57
  // This was 4 seconds, kept under the poll interval so a probe never held the
53
58
  // loop past the next one. Measured since, on a Windows 11 machine: the probe
54
59
  // takes 2.5-3 seconds with nothing else running, and while the browser was
@@ -685,21 +690,66 @@ function whenWindowUnconfirmed(verdict, atKeyboard) {
685
690
  return atKeyboard ? 'leave' : 'stop';
686
691
  }
687
692
 
693
+ /**
694
+ * Whether this run was started from the dashboard (kryptheonChild says so).
695
+ * There is no terminal then: nobody can press Ctrl+C, and the way to end a
696
+ * run is the Stop button on the page.
697
+ */
698
+ function fromDashboardJob() {
699
+ return process.env.KRYPTHEON_DASHBOARD_JOB === '1';
700
+ }
701
+
688
702
  // Said once, while the person may still be recording, instead of taking the
689
- // browser away from them.
690
- function windowUnconfirmedLines() {
703
+ // browser away from them. From the dashboard it names Stop: found on Windows
704
+ // 10, where the page told somebody to press Ctrl+C - which the page does not
705
+ // have - while a recording with no window stayed RUNNING for an hour.
706
+ function windowUnconfirmedLines(fromDashboard) {
691
707
  return [
692
708
  '',
693
709
  ' (I could not confirm the browser window from here - that check is slow',
694
710
  ' on some machines - so I am leaving it alone. If it is on your screen,',
695
711
  ' carry on and close it when you are done. If there is no window',
696
- ' anywhere, press Ctrl+C to stop.)',
712
+ fromDashboard ? ' anywhere, press Stop on the dashboard.)' : ' anywhere, press Ctrl+C to stop.)',
697
713
  '',
698
714
  ];
699
715
  }
700
716
 
701
717
  // The probe kept failing to answer. Nothing was learned about the browser
702
718
  // either way, and saying "no window appeared" would be inventing a fact.
719
+ /**
720
+ * Said once, three minutes in, when no window has been seen - the probe did
721
+ * not answer, or answered that there is none. The browser is left running
722
+ * all the same: an unanswered probe is not proof, and someone may be using
723
+ * the window. Antivirus is named because that is what held Chrome for
724
+ * Testing on the machine where this was found.
725
+ */
726
+ function windowLateLines(platform, fromDashboard) {
727
+ const where = platform === 'win32'
728
+ ? [
729
+ ' - Open Task Manager and look for "Chrome for Testing". If it is there',
730
+ ' but nothing is on your screen, it is stuck.',
731
+ ' - Antivirus (for example Quick Heal or Avast) may be holding it. Allow',
732
+ ' the folder %LOCALAPPDATA%\\ms-playwright in your antivirus, then',
733
+ ' record again.',
734
+ ]
735
+ : [
736
+ ' - Look for "Chrome for Testing" among the programs running. If it is',
737
+ ' there but nothing is on your screen, it is stuck.',
738
+ ' - Security software may be holding it. Allow the ms-playwright folder,',
739
+ ' where the browser is kept, then record again.',
740
+ ];
741
+ return [
742
+ '',
743
+ ' No browser window could be seen, 3 minutes after it was started.',
744
+ '',
745
+ ].concat(where, [
746
+ fromDashboard ? ' - Or press Stop on the dashboard.' : ' - Or press Ctrl+C here to stop.',
747
+ '',
748
+ ' If the window is on your screen after all, carry on - nothing has been stopped.',
749
+ '',
750
+ ]);
751
+ }
752
+
703
753
  function probeUnclearLines(answered, unanswered, platform) {
704
754
  return [
705
755
  '',
@@ -738,6 +788,7 @@ function createWindowWatch(options) {
738
788
  const clock = opts.now || Date.now;
739
789
  const budgetMs = opts.budgetMs == null ? WINDOW_TIMEOUT_MS : opts.budgetMs;
740
790
  const hardLimitMs = opts.hardLimitMs == null ? WINDOW_HARD_LIMIT_MS : opts.hardLimitMs;
791
+ const lateMs = opts.lateMs == null ? WINDOW_LATE_MS : opts.lateMs;
741
792
 
742
793
  const started = clock();
743
794
  let lastTick = started;
@@ -748,6 +799,7 @@ function createWindowWatch(options) {
748
799
  let answered = 0;
749
800
  let unanswered = 0;
750
801
  let sawProcess = false;
802
+ let lateSaid = false;
751
803
 
752
804
  return {
753
805
  tick: function () {
@@ -778,6 +830,20 @@ function createWindowWatch(options) {
778
830
  seconds: Math.round((now - started) / 1000),
779
831
  };
780
832
  },
833
+ /**
834
+ * After a verdict that left the browser running with no window seen: keep
835
+ * looking, quietly. 'window' once one is seen; 'late' once, when lateMs
836
+ * have gone by since the start with none (null after that); otherwise
837
+ * null. Never a reason to stop the browser.
838
+ */
839
+ later: function () {
840
+ if (lateSaid) return null;
841
+ const seen = probe();
842
+ if (seen && seen.known && seen.windows > 0) return 'window';
843
+ if (clock() - started < lateMs) return null;
844
+ lateSaid = true;
845
+ return 'late';
846
+ },
781
847
  stats: function () {
782
848
  return { spent: spent, answered: answered, unanswered: unanswered, sawProcess: sawProcess };
783
849
  },
@@ -1654,6 +1720,8 @@ async function record(url, options) {
1654
1720
  // Someone whose window is real but undetectable can turn this off.
1655
1721
  const watchdogOff = !!process.env.KRYPTHEON_FORCE_RECORD;
1656
1722
  const watch = createWindowWatch();
1723
+ // Left running with no window seen: looking on for the three-minute note.
1724
+ let lookingLater = false;
1657
1725
  const watchdog = setInterval(() => {
1658
1726
  if (watchdogOff) {
1659
1727
  clearInterval(watchdog);
@@ -1665,15 +1733,25 @@ async function record(url, options) {
1665
1733
  clearInterval(watchdog);
1666
1734
  return;
1667
1735
  }
1736
+ if (lookingLater) {
1737
+ const later = watch.later();
1738
+ if (!later) return;
1739
+ clearInterval(watchdog);
1740
+ if (later === 'late') for (const line of windowLateLines(process.platform, fromDashboardJob())) console.log(line);
1741
+ return;
1742
+ }
1668
1743
  const outcome = watch.tick();
1669
1744
  if (!outcome) return;
1670
- clearInterval(watchdog);
1671
1745
  if (whenWindowUnconfirmed(outcome.verdict, !!process.stdin.isTTY) === 'leave') {
1672
- if (outcome.verdict !== 'window') {
1673
- for (const line of windowUnconfirmedLines()) console.log(line);
1746
+ if (outcome.verdict === 'window') {
1747
+ clearInterval(watchdog);
1748
+ return;
1674
1749
  }
1750
+ for (const line of windowUnconfirmedLines(fromDashboardJob())) console.log(line);
1751
+ lookingLater = true;
1675
1752
  return;
1676
1753
  }
1754
+ clearInterval(watchdog);
1677
1755
  stopChild();
1678
1756
  finalise(outcome.verdict, outcome).then(resolve);
1679
1757
  }, WINDOW_POLL_MS);
@@ -1686,6 +1764,15 @@ async function record(url, options) {
1686
1764
  process.once('SIGINT', onSignal);
1687
1765
  process.once('SIGTERM', onSignal);
1688
1766
  process.once('SIGHUP', onSignal);
1767
+ // Started from the dashboard, Stop on the page is this process's Ctrl+C.
1768
+ // The channel must not keep the process alive once the recording is done.
1769
+ const onMessage = (message) => {
1770
+ if (message === STOP_MESSAGE) onSignal();
1771
+ };
1772
+ if (process.channel) {
1773
+ process.on('message', onMessage);
1774
+ process.channel.unref();
1775
+ }
1689
1776
 
1690
1777
  child.on('error', () => {
1691
1778
  clearInterval(watchdog);
@@ -1697,6 +1784,7 @@ async function record(url, options) {
1697
1784
  process.removeListener('SIGINT', onSignal);
1698
1785
  process.removeListener('SIGTERM', onSignal);
1699
1786
  process.removeListener('SIGHUP', onSignal);
1787
+ process.removeListener('message', onMessage);
1700
1788
  finalise('exit').then(resolve);
1701
1789
  });
1702
1790
  });
@@ -2574,7 +2662,7 @@ const DASHBOARD_PORT = Number(process.env.KRYPTHEON_DASHBOARD_PORT) || 4789;
2574
2662
  * so the page can show progress instead of a spinner. Nothing is read from
2575
2663
  * the keyboard: a command started from the page has nobody at one.
2576
2664
  */
2577
- function spawnLines(file, args, options, say) {
2665
+ function spawnLines(file, args, options, say, onChild) {
2578
2666
  return new Promise((resolve) => {
2579
2667
  let child;
2580
2668
  try {
@@ -2584,6 +2672,7 @@ function spawnLines(file, args, options, say) {
2584
2672
  resolve({ code: null, why: err.message });
2585
2673
  return;
2586
2674
  }
2675
+ if (onChild) onChild(child);
2587
2676
  const pending = { out: '', err: '' };
2588
2677
  const feed = (which) => (chunk) => {
2589
2678
  pending[which] += chunk.toString();
@@ -2605,9 +2694,37 @@ function spawnLines(file, args, options, say) {
2605
2694
  });
2606
2695
  }
2607
2696
 
2608
- /** This same command, as a child, with these arguments. */
2609
- function kryptheonChild(args, say) {
2610
- return spawnLines(process.execPath, [path.join(PACKAGE_DIR, 'bin', 'kryptheon.js')].concat(args), {}, say);
2697
+ /**
2698
+ * This same command, as a child, with these arguments. KRYPTHEON_DASHBOARD_JOB
2699
+ * tells it that it was started from the page: nobody is at a terminal to press
2700
+ * Ctrl+C, and what it says should name the page's Stop button instead.
2701
+ *
2702
+ * With `onChild`, the child also gets a message channel, and onChild is handed
2703
+ * the child: stopChildJob() asks it over that channel to stop the way Ctrl+C
2704
+ * does. Killing it would not - on Windows a kill gives it no chance to stop
2705
+ * the browser it started, or to keep what was recorded.
2706
+ */
2707
+ function kryptheonChild(args, say, onChild) {
2708
+ const options = { env: Object.assign({}, process.env, { KRYPTHEON_DASHBOARD_JOB: '1' }) };
2709
+ if (onChild) options.stdio = ['ignore', 'pipe', 'pipe', 'ipc'];
2710
+ return spawnLines(process.execPath, [path.join(PACKAGE_DIR, 'bin', 'kryptheon.js')].concat(args), options, say, onChild);
2711
+ }
2712
+
2713
+ // What stopChildJob sends, and what record listens for.
2714
+ const STOP_MESSAGE = 'kryptheon:stop';
2715
+
2716
+ /** Asks a child started by kryptheonChild to stop as Ctrl+C would; a kill only if it cannot be asked. */
2717
+ function stopChildJob(child) {
2718
+ try {
2719
+ if (child.connected) {
2720
+ child.send(STOP_MESSAGE);
2721
+ return 'asked';
2722
+ }
2723
+ child.kill();
2724
+ return 'killed';
2725
+ } catch (err) {
2726
+ return 'gone';
2727
+ }
2611
2728
  }
2612
2729
 
2613
2730
  /**
@@ -2773,12 +2890,19 @@ function recheckFromDashboard(id, opened, say) {
2773
2890
  });
2774
2891
  }
2775
2892
 
2776
- /** Records one flow the way `npx kryptheon record <address>` does, and names what it saved. */
2893
+ /**
2894
+ * Records one flow the way `npx kryptheon record <address>` does, and names
2895
+ * what it saved. The page's Stop ends it as Ctrl+C would in a terminal: found
2896
+ * on Windows 10, where a browser that never showed its window left a
2897
+ * recording RUNNING for an hour with no way to end it from the page.
2898
+ */
2777
2899
  function recordFromDashboard(address, say, options) {
2778
2900
  const phone = Boolean(options && options.phone);
2901
+ const control = options && options.control;
2779
2902
  const before = listSpecFiles();
2780
2903
  say('A ' + (phone ? 'phone-sized ' : '') + 'browser window opens on ' + address + '. Use your app, then close that window to save.');
2781
- return kryptheonChild(['record', address].concat(phone ? ['--phone'] : []), say).then(({ code }) => {
2904
+ const onChild = control ? (child) => control.onStop(() => stopChildJob(child)) : null;
2905
+ return kryptheonChild(['record', address].concat(phone ? ['--phone'] : []), say, onChild).then(({ code }) => {
2782
2906
  const added = listSpecFiles().filter((name) => !before.includes(name));
2783
2907
  if (!added.length) return { ok: false, exit: code, why: 'no recording was saved' };
2784
2908
  return { ok: true, exit: code, recordings: added, summary: 'saved ' + added.join(', ') };
@@ -3512,6 +3636,11 @@ module.exports = {
3512
3636
  askKey: askKey,
3513
3637
  whenWindowUnconfirmed: whenWindowUnconfirmed,
3514
3638
  windowUnconfirmedLines: windowUnconfirmedLines,
3639
+ windowLateLines: windowLateLines,
3640
+ WINDOW_LATE_MS: WINDOW_LATE_MS,
3641
+ fromDashboardJob: fromDashboardJob,
3642
+ stopChildJob: stopChildJob,
3643
+ STOP_MESSAGE: STOP_MESSAGE,
3515
3644
  WINDOW_TIMEOUT_MS: WINDOW_TIMEOUT_MS,
3516
3645
  WINDOW_HARD_LIMIT_MS: WINDOW_HARD_LIMIT_MS,
3517
3646
  WINDOW_POLL_MS: WINDOW_POLL_MS,
@@ -1055,7 +1055,7 @@ function createServer(root, options) {
1055
1055
  if (!address) return json(res, 400, { ok: false, why: notAnAddress(input.url, 'http://localhost:3000') });
1056
1056
  const phone = input.phone === true;
1057
1057
  return begin('record', 'Recording ' + address + (phone ? ' at phone size' : ''),
1058
- 'npx kryptheon record ' + address + (phone ? ' --phone' : ''), (say) => opts.record(address, say, { phone: phone }));
1058
+ 'npx kryptheon record ' + address + (phone ? ' --phone' : ''), (say, control) => opts.record(address, say, { phone: phone, control: control }));
1059
1059
  }
1060
1060
  if (url.pathname === '/api/db' && typeof opts.db === 'function') {
1061
1061
  const action = String(input.action || '');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kryptheon",
3
- "version": "0.1.48",
3
+ "version": "0.1.49",
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",