sigIntWatcher.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.SigIntWatcher = void 0;
  6. var _class2;
  7. /**
  8. * Copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. class SigIntWatcher {
  23. constructor() {
  24. this._hadSignal = false;
  25. this._sigintPromise = void 0;
  26. this._sigintHandler = void 0;
  27. let sigintCallback;
  28. this._sigintPromise = new Promise(f => sigintCallback = f);
  29. this._sigintHandler = () => {
  30. FixedNodeSIGINTHandler.off(this._sigintHandler);
  31. this._hadSignal = true;
  32. sigintCallback();
  33. };
  34. FixedNodeSIGINTHandler.on(this._sigintHandler);
  35. }
  36. promise() {
  37. return this._sigintPromise;
  38. }
  39. hadSignal() {
  40. return this._hadSignal;
  41. }
  42. disarm() {
  43. FixedNodeSIGINTHandler.off(this._sigintHandler);
  44. }
  45. }
  46. // NPM/NPX will send us duplicate SIGINT signals, so we need to ignore them.
  47. exports.SigIntWatcher = SigIntWatcher;
  48. class FixedNodeSIGINTHandler {
  49. static _install() {
  50. if (!this._handlerInstalled) {
  51. this._handlerInstalled = true;
  52. process.on('SIGINT', this._dispatch);
  53. }
  54. }
  55. static _uninstall() {
  56. if (this._handlerInstalled) {
  57. this._handlerInstalled = false;
  58. process.off('SIGINT', this._dispatch);
  59. }
  60. }
  61. static on(handler) {
  62. this._handlers.push(handler);
  63. if (this._handlers.length === 1) this._install();
  64. }
  65. static off(handler) {
  66. this._handlers = this._handlers.filter(h => h !== handler);
  67. if (!this._ignoreNextSIGINTs && !this._handlers.length) this._uninstall();
  68. }
  69. }
  70. _class2 = FixedNodeSIGINTHandler;
  71. FixedNodeSIGINTHandler._handlers = [];
  72. FixedNodeSIGINTHandler._ignoreNextSIGINTs = false;
  73. FixedNodeSIGINTHandler._handlerInstalled = false;
  74. FixedNodeSIGINTHandler._dispatch = () => {
  75. if (_class2._ignoreNextSIGINTs) return;
  76. _class2._ignoreNextSIGINTs = true;
  77. setTimeout(() => {
  78. _class2._ignoreNextSIGINTs = false;
  79. // We remove the handler so that second Ctrl+C immediately kills the process
  80. // via the default sigint handler. This is handy in the case where our shutdown
  81. // takes a lot of time or is buggy.
  82. //
  83. // When running through NPM we might get multiple SIGINT signals
  84. // for a single Ctrl+C - this is an NPM bug present since NPM v6+.
  85. // https://github.com/npm/cli/issues/1591
  86. // https://github.com/npm/cli/issues/2124
  87. // https://github.com/npm/cli/issues/5021
  88. //
  89. // Therefore, removing the handler too soon will just kill the process
  90. // with default handler without printing the results.
  91. // We work around this by giving NPM 1000ms to send us duplicate signals.
  92. // The side effect is that slow shutdown or bug in our process will force
  93. // the user to hit Ctrl+C again after at least a second.
  94. if (!_class2._handlers.length) _class2._uninstall();
  95. }, 1000);
  96. for (const handler of _class2._handlers) handler();
  97. };