summaryrefslogtreecommitdiff
path: root/cli/js/signals.ts
blob: 3295b9e80650765e4295540180792183fa46c77e (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { bindSignal, pollSignal, unbindSignal } from "./ops/signal.ts";
import { build } from "./build.ts";

// From `kill -l`
enum LinuxSignal {
  SIGHUP = 1,
  SIGINT = 2,
  SIGQUIT = 3,
  SIGILL = 4,
  SIGTRAP = 5,
  SIGABRT = 6,
  SIGBUS = 7,
  SIGFPE = 8,
  SIGKILL = 9,
  SIGUSR1 = 10,
  SIGSEGV = 11,
  SIGUSR2 = 12,
  SIGPIPE = 13,
  SIGALRM = 14,
  SIGTERM = 15,
  SIGSTKFLT = 16,
  SIGCHLD = 17,
  SIGCONT = 18,
  SIGSTOP = 19,
  SIGTSTP = 20,
  SIGTTIN = 21,
  SIGTTOU = 22,
  SIGURG = 23,
  SIGXCPU = 24,
  SIGXFSZ = 25,
  SIGVTALRM = 26,
  SIGPROF = 27,
  SIGWINCH = 28,
  SIGIO = 29,
  SIGPWR = 30,
  SIGSYS = 31
}

// From `kill -l`
enum MacOSSignal {
  SIGHUP = 1,
  SIGINT = 2,
  SIGQUIT = 3,
  SIGILL = 4,
  SIGTRAP = 5,
  SIGABRT = 6,
  SIGEMT = 7,
  SIGFPE = 8,
  SIGKILL = 9,
  SIGBUS = 10,
  SIGSEGV = 11,
  SIGSYS = 12,
  SIGPIPE = 13,
  SIGALRM = 14,
  SIGTERM = 15,
  SIGURG = 16,
  SIGSTOP = 17,
  SIGTSTP = 18,
  SIGCONT = 19,
  SIGCHLD = 20,
  SIGTTIN = 21,
  SIGTTOU = 22,
  SIGIO = 23,
  SIGXCPU = 24,
  SIGXFSZ = 25,
  SIGVTALRM = 26,
  SIGPROF = 27,
  SIGWINCH = 28,
  SIGINFO = 29,
  SIGUSR1 = 30,
  SIGUSR2 = 31
}

/** Signals numbers. This is platform dependent.
 */
export const Signal: { [key: string]: number } = {};

export function setSignals(): void {
  if (build.os === "mac") {
    Object.assign(Signal, MacOSSignal);
  } else {
    Object.assign(Signal, LinuxSignal);
  }
}

/**
 * Returns the stream of the given signal number. You can use it as an async
 * iterator.
 *
 *     for await (const _ of Deno.signal(Deno.Signal.SIGTERM)) {
 *       console.log("got SIGTERM!");
 *     }
 *
 * You can also use it as a promise. In this case you can only receive the
 * first one.
 *
 *     await Deno.signal(Deno.Signal.SIGTERM);
 *     console.log("SIGTERM received!")
 *
 * If you want to stop receiving the signals, you can use .dispose() method
 * of the signal stream object.
 *
 *     const sig = Deno.signal(Deno.Signal.SIGTERM);
 *     setTimeout(() => { sig.dispose(); }, 5000);
 *     for await (const _ of sig) {
 *       console.log("SIGTERM!")
 *     }
 *
 * The above for-await loop exits after 5 seconds when sig.dispose() is called.
 */
export function signal(signo: number): SignalStream {
  if (build.os === "win") {
    throw new Error("not implemented!");
  }
  return new SignalStream(signo);
}

export const signals = {
  /** Returns the stream of SIGALRM signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGALRM). */
  alarm(): SignalStream {
    return signal(Signal.SIGALRM);
  },
  /** Returns the stream of SIGCHLD signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGCHLD). */
  child(): SignalStream {
    return signal(Signal.SIGCHLD);
  },
  /** Returns the stream of SIGHUP signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGHUP). */
  hungup(): SignalStream {
    return signal(Signal.SIGHUP);
  },
  /** Returns the stream of SIGINT signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGINT). */
  interrupt(): SignalStream {
    return signal(Signal.SIGINT);
  },
  /** Returns the stream of SIGIO signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGIO). */
  io(): SignalStream {
    return signal(Signal.SIGIO);
  },
  /** Returns the stream of SIGPIPE signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGPIPE). */
  pipe(): SignalStream {
    return signal(Signal.SIGPIPE);
  },
  /** Returns the stream of SIGQUIT signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGQUIT). */
  quit(): SignalStream {
    return signal(Signal.SIGQUIT);
  },
  /** Returns the stream of SIGTERM signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGTERM). */
  terminate(): SignalStream {
    return signal(Signal.SIGTERM);
  },
  /** Returns the stream of SIGUSR1 signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGUSR1). */
  userDefined1(): SignalStream {
    return signal(Signal.SIGUSR1);
  },
  /** Returns the stream of SIGUSR2 signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGUSR2). */
  userDefined2(): SignalStream {
    return signal(Signal.SIGUSR2);
  },
  /** Returns the stream of SIGWINCH signals.
   * This method is the shorthand for Deno.signal(Deno.Signal.SIGWINCH). */
  windowChange(): SignalStream {
    return signal(Signal.SIGWINCH);
  }
};

/** SignalStream represents the stream of signals, implements both
 * AsyncIterator and PromiseLike */
export class SignalStream
  implements AsyncIterableIterator<void>, PromiseLike<void> {
  private rid: number;
  /** The promise of polling the signal,
   * resolves with false when it receives signal,
   * Resolves with true when the signal stream is disposed. */
  private pollingPromise: Promise<boolean> = Promise.resolve(false);
  /** The flag, which is true when the stream is disposed. */
  private disposed = false;
  constructor(signo: number) {
    this.rid = bindSignal(signo).rid;
    this.loop();
  }

  private async pollSignal(): Promise<boolean> {
    const res = await pollSignal(this.rid);
    return res.done;
  }

  private async loop(): Promise<void> {
    do {
      this.pollingPromise = this.pollSignal();
    } while (!(await this.pollingPromise) && !this.disposed);
  }

  then<T, S>(
    f: (v: void) => T | Promise<T>,
    g?: (v: Error) => S | Promise<S>
  ): Promise<T | S> {
    return this.pollingPromise.then((_): void => {}).then(f, g);
  }

  async next(): Promise<IteratorResult<void>> {
    return { done: await this.pollingPromise, value: undefined };
  }

  [Symbol.asyncIterator](): AsyncIterableIterator<void> {
    return this;
  }

  dispose(): void {
    if (this.disposed) {
      throw new Error("The stream has already been disposed.");
    }
    this.disposed = true;
    unbindSignal(this.rid);
  }
}