File size: 3,393 Bytes
15b27a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SigIntWatcher = void 0;
var _class2;
/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class SigIntWatcher {
  constructor() {
    this._hadSignal = false;
    this._sigintPromise = void 0;
    this._sigintHandler = void 0;
    let sigintCallback;
    this._sigintPromise = new Promise(f => sigintCallback = f);
    this._sigintHandler = () => {
      FixedNodeSIGINTHandler.off(this._sigintHandler);
      this._hadSignal = true;
      sigintCallback();
    };
    FixedNodeSIGINTHandler.on(this._sigintHandler);
  }
  promise() {
    return this._sigintPromise;
  }
  hadSignal() {
    return this._hadSignal;
  }
  disarm() {
    FixedNodeSIGINTHandler.off(this._sigintHandler);
  }
}

// NPM/NPX will send us duplicate SIGINT signals, so we need to ignore them.
exports.SigIntWatcher = SigIntWatcher;
class FixedNodeSIGINTHandler {
  static _install() {
    if (!this._handlerInstalled) {
      this._handlerInstalled = true;
      process.on('SIGINT', this._dispatch);
    }
  }
  static _uninstall() {
    if (this._handlerInstalled) {
      this._handlerInstalled = false;
      process.off('SIGINT', this._dispatch);
    }
  }
  static on(handler) {
    this._handlers.push(handler);
    if (this._handlers.length === 1) this._install();
  }
  static off(handler) {
    this._handlers = this._handlers.filter(h => h !== handler);
    if (!this._ignoreNextSIGINTs && !this._handlers.length) this._uninstall();
  }
}
_class2 = FixedNodeSIGINTHandler;
FixedNodeSIGINTHandler._handlers = [];
FixedNodeSIGINTHandler._ignoreNextSIGINTs = false;
FixedNodeSIGINTHandler._handlerInstalled = false;
FixedNodeSIGINTHandler._dispatch = () => {
  if (_class2._ignoreNextSIGINTs) return;
  _class2._ignoreNextSIGINTs = true;
  setTimeout(() => {
    _class2._ignoreNextSIGINTs = false;
    // We remove the handler so that second Ctrl+C immediately kills the process
    // via the default sigint handler. This is handy in the case where our shutdown
    // takes a lot of time or is buggy.
    //
    // When running through NPM we might get multiple SIGINT signals
    // for a single Ctrl+C - this is an NPM bug present since NPM v6+.
    // https://github.com/npm/cli/issues/1591
    // https://github.com/npm/cli/issues/2124
    // https://github.com/npm/cli/issues/5021
    //
    // Therefore, removing the handler too soon will just kill the process
    // with default handler without printing the results.
    // We work around this by giving NPM 1000ms to send us duplicate signals.
    // The side effect is that slow shutdown or bug in our process will force
    // the user to hit Ctrl+C again after at least a second.
    if (!_class2._handlers.length) _class2._uninstall();
  }, 1000);
  for (const handler of _class2._handlers) handler();
};