@ohos-ports/bare-signals 5.0.0-beta.0

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/CMakeLists.txt ADDED
@@ -0,0 +1,13 @@
1
+ cmake_minimum_required(VERSION 4.0)
2
+
3
+ find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
4
+
5
+ project(bare_signals C)
6
+
7
+ add_bare_module(bare_signals)
8
+
9
+ target_sources(
10
+ ${bare_signals}
11
+ PRIVATE
12
+ binding.c
13
+ )
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # bare-signals
2
+
3
+ Native signal handling for JavaScript.
4
+
5
+ ```
6
+ npm i bare-signals
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```js
12
+ const Signal = require('bare-signals')
13
+
14
+ const sigint = new Signal('SIGINT')
15
+
16
+ sigint.on('signal', () => console.log('SIGINT caught')).start()
17
+ ```
18
+
19
+ ## License
20
+
21
+ Apache-2.0
package/binding.c ADDED
@@ -0,0 +1,431 @@
1
+ #include <assert.h>
2
+ #include <signal.h>
3
+ #include <uv.h>
4
+
5
+ #include <node_api.h>
6
+
7
+ typedef struct {
8
+ uv_signal_t handle;
9
+
10
+ napi_env env;
11
+ napi_ref ctx;
12
+ napi_ref on_signal;
13
+ napi_ref on_close;
14
+
15
+ bool closing;
16
+ bool exiting;
17
+ bool hook_added;
18
+ } bare_signal_t;
19
+
20
+ static void
21
+ bare_signals__on_signal(uv_signal_t *handle, int signum) {
22
+ int err;
23
+
24
+ bare_signal_t *signal = (bare_signal_t *) handle;
25
+
26
+ if (signal->exiting) return;
27
+
28
+ napi_env env = signal->env;
29
+
30
+ napi_handle_scope scope;
31
+ err = napi_open_handle_scope(env, &scope);
32
+ assert(err == 0);
33
+
34
+ napi_value on_signal;
35
+ err = napi_get_reference_value(env, signal->on_signal, &on_signal);
36
+ assert(err == 0);
37
+
38
+ napi_value ctx;
39
+ err = napi_get_reference_value(env, signal->ctx, &ctx);
40
+ assert(err == 0);
41
+
42
+ err = napi_make_callback(env, NULL, ctx, on_signal, 0, NULL, NULL);
43
+ assert(err == 0);
44
+
45
+ err = napi_close_handle_scope(env, scope);
46
+ assert(err == 0);
47
+ }
48
+
49
+ static void
50
+ bare_signals__on_close(uv_handle_t *handle) {
51
+ int err;
52
+
53
+ bare_signal_t *signal = (bare_signal_t *) handle;
54
+
55
+ napi_env env = signal->env;
56
+
57
+ napi_handle_scope scope;
58
+ err = napi_open_handle_scope(env, &scope);
59
+ assert(err == 0);
60
+
61
+ napi_value on_close;
62
+ err = napi_get_reference_value(env, signal->on_close, &on_close);
63
+ assert(err == 0);
64
+
65
+ napi_value ctx;
66
+ err = napi_get_reference_value(env, signal->ctx, &ctx);
67
+ assert(err == 0);
68
+
69
+ err = napi_delete_reference(env, signal->on_signal);
70
+ assert(err == 0);
71
+
72
+ err = napi_delete_reference(env, signal->on_close);
73
+ assert(err == 0);
74
+
75
+ err = napi_delete_reference(env, signal->ctx);
76
+ assert(err == 0);
77
+
78
+ if (!signal->exiting) {
79
+ err = napi_make_callback(env, NULL, ctx, on_close, 0, NULL, NULL);
80
+ assert(err == 0);
81
+ }
82
+
83
+ err = napi_close_handle_scope(env, scope);
84
+ assert(err == 0);
85
+ }
86
+
87
+ // Node N-API equivalent of the Bare deferred teardown callback: close the
88
+ // uv_signal handle when the environment is being torn down.
89
+ static void
90
+ bare_signals__on_teardown(void *data) {
91
+ bare_signal_t *signal = (bare_signal_t *) data;
92
+
93
+ signal->exiting = true;
94
+
95
+ if (signal->closing) return;
96
+
97
+ uv_close((uv_handle_t *) &signal->handle, bare_signals__on_close);
98
+ }
99
+
100
+ static napi_value
101
+ bare_signals_init(napi_env env, napi_callback_info info) {
102
+ int err;
103
+
104
+ size_t argc = 3;
105
+ napi_value argv[3];
106
+
107
+ err = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
108
+ assert(err == 0);
109
+
110
+ assert(argc == 3);
111
+
112
+ napi_value handle;
113
+
114
+ bare_signal_t *signal;
115
+ err = napi_create_arraybuffer(env, sizeof(bare_signal_t), (void **) &signal, &handle);
116
+ if (err < 0) return NULL;
117
+
118
+ uv_loop_t *loop;
119
+ err = napi_get_uv_event_loop(env, &loop);
120
+ assert(err == 0);
121
+
122
+ err = uv_signal_init(loop, &signal->handle);
123
+
124
+ if (err < 0) {
125
+ napi_throw_error(env, uv_err_name(err), uv_strerror(err));
126
+ return NULL;
127
+ }
128
+
129
+ signal->env = env;
130
+ signal->closing = false;
131
+ signal->exiting = false;
132
+ signal->hook_added = false;
133
+
134
+ err = napi_create_reference(env, argv[0], 1, &signal->ctx);
135
+ assert(err == 0);
136
+
137
+ err = napi_create_reference(env, argv[1], 1, &signal->on_signal);
138
+ assert(err == 0);
139
+
140
+ err = napi_create_reference(env, argv[2], 1, &signal->on_close);
141
+ assert(err == 0);
142
+
143
+ err = napi_add_env_cleanup_hook(env, bare_signals__on_teardown, (void *) signal);
144
+ assert(err == 0);
145
+
146
+ signal->hook_added = true;
147
+
148
+ return handle;
149
+ }
150
+
151
+ static napi_value
152
+ bare_signals_close(napi_env env, napi_callback_info info) {
153
+ int err;
154
+
155
+ size_t argc = 1;
156
+ napi_value argv[1];
157
+
158
+ err = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
159
+ assert(err == 0);
160
+
161
+ assert(argc == 1);
162
+
163
+ bare_signal_t *signal;
164
+ err = napi_get_arraybuffer_info(env, argv[0], (void **) &signal, NULL);
165
+ assert(err == 0);
166
+
167
+ signal->closing = true;
168
+
169
+ if (signal->hook_added) {
170
+ err = napi_remove_env_cleanup_hook(env, bare_signals__on_teardown, (void *) signal);
171
+ assert(err == 0);
172
+ signal->hook_added = false;
173
+ }
174
+
175
+ uv_close((uv_handle_t *) &signal->handle, bare_signals__on_close);
176
+
177
+ return NULL;
178
+ }
179
+
180
+ static napi_value
181
+ bare_signals_start(napi_env env, napi_callback_info info) {
182
+ int err;
183
+
184
+ size_t argc = 2;
185
+ napi_value argv[2];
186
+
187
+ err = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
188
+ assert(err == 0);
189
+
190
+ assert(argc == 2);
191
+
192
+ bare_signal_t *signal;
193
+ err = napi_get_arraybuffer_info(env, argv[0], (void **) &signal, NULL);
194
+ assert(err == 0);
195
+
196
+ int32_t signum;
197
+ err = napi_get_value_int32(env, argv[1], &signum);
198
+ assert(err == 0);
199
+
200
+ err = uv_signal_start(&signal->handle, bare_signals__on_signal, signum);
201
+
202
+ if (err < 0) {
203
+ napi_throw_error(env, uv_err_name(err), uv_strerror(err));
204
+ return NULL;
205
+ }
206
+
207
+ return NULL;
208
+ }
209
+
210
+ static napi_value
211
+ bare_signals_stop(napi_env env, napi_callback_info info) {
212
+ int err;
213
+
214
+ size_t argc = 1;
215
+ napi_value argv[1];
216
+
217
+ err = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
218
+ assert(err == 0);
219
+
220
+ assert(argc == 1);
221
+
222
+ bare_signal_t *signal;
223
+ err = napi_get_arraybuffer_info(env, argv[0], (void **) &signal, NULL);
224
+ assert(err == 0);
225
+
226
+ err = uv_signal_stop(&signal->handle);
227
+
228
+ if (err < 0) {
229
+ napi_throw_error(env, uv_err_name(err), uv_strerror(err));
230
+ return NULL;
231
+ }
232
+
233
+ return NULL;
234
+ }
235
+
236
+ static napi_value
237
+ bare_signals_ref(napi_env env, napi_callback_info info) {
238
+ int err;
239
+
240
+ size_t argc = 1;
241
+ napi_value argv[1];
242
+
243
+ err = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
244
+ assert(err == 0);
245
+
246
+ assert(argc == 1);
247
+
248
+ bare_signal_t *signal;
249
+ err = napi_get_arraybuffer_info(env, argv[0], (void **) &signal, NULL);
250
+ assert(err == 0);
251
+
252
+ uv_ref((uv_handle_t *) &signal->handle);
253
+
254
+ return NULL;
255
+ }
256
+
257
+ static napi_value
258
+ bare_signals_unref(napi_env env, napi_callback_info info) {
259
+ int err;
260
+
261
+ size_t argc = 1;
262
+ napi_value argv[1];
263
+
264
+ err = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
265
+ assert(err == 0);
266
+
267
+ assert(argc == 1);
268
+
269
+ bare_signal_t *signal;
270
+ err = napi_get_arraybuffer_info(env, argv[0], (void **) &signal, NULL);
271
+ assert(err == 0);
272
+
273
+ uv_unref((uv_handle_t *) &signal->handle);
274
+
275
+ return NULL;
276
+ }
277
+
278
+ static napi_value
279
+ bare_signals_exports(napi_env env, napi_value exports) {
280
+ int err;
281
+
282
+ #define V(name, fn) \
283
+ { \
284
+ napi_value val; \
285
+ err = napi_create_function(env, name, NAPI_AUTO_LENGTH, fn, NULL, &val); \
286
+ assert(err == 0); \
287
+ err = napi_set_named_property(env, exports, name, val); \
288
+ assert(err == 0); \
289
+ }
290
+
291
+ V("init", bare_signals_init)
292
+ V("close", bare_signals_close)
293
+ V("start", bare_signals_start)
294
+ V("stop", bare_signals_stop)
295
+ V("ref", bare_signals_ref)
296
+ V("unref", bare_signals_unref)
297
+ #undef V
298
+
299
+ napi_value signals;
300
+ err = napi_create_object(env, &signals);
301
+ assert(err == 0);
302
+
303
+ err = napi_set_named_property(env, exports, "signals", signals);
304
+ assert(err == 0);
305
+
306
+ #define V(name) \
307
+ { \
308
+ napi_value val; \
309
+ err = napi_create_uint32(env, name, &val); \
310
+ assert(err == 0); \
311
+ err = napi_set_named_property(env, signals, #name, val); \
312
+ assert(err == 0); \
313
+ }
314
+
315
+ #ifdef SIGHUP
316
+ V(SIGHUP);
317
+ #endif
318
+ #ifdef SIGINT
319
+ V(SIGINT);
320
+ #endif
321
+ #ifdef SIGQUIT
322
+ V(SIGQUIT);
323
+ #endif
324
+ #ifdef SIGILL
325
+ V(SIGILL);
326
+ #endif
327
+ #ifdef SIGTRAP
328
+ V(SIGTRAP);
329
+ #endif
330
+ #ifdef SIGABRT
331
+ V(SIGABRT);
332
+ #endif
333
+ #ifdef SIGIOT
334
+ V(SIGIOT);
335
+ #endif
336
+ #ifdef SIGBUS
337
+ V(SIGBUS);
338
+ #endif
339
+ #ifdef SIGFPE
340
+ V(SIGFPE);
341
+ #endif
342
+ #ifdef SIGKILL
343
+ V(SIGKILL);
344
+ #endif
345
+ #ifdef SIGUSR1
346
+ V(SIGUSR1);
347
+ #endif
348
+ #ifdef SIGSEGV
349
+ V(SIGSEGV);
350
+ #endif
351
+ #ifdef SIGUSR2
352
+ V(SIGUSR2);
353
+ #endif
354
+ #ifdef SIGPIPE
355
+ V(SIGPIPE);
356
+ #endif
357
+ #ifdef SIGALRM
358
+ V(SIGALRM);
359
+ #endif
360
+ #ifdef SIGTERM
361
+ V(SIGTERM);
362
+ #endif
363
+ #ifdef SIGCHLD
364
+ V(SIGCHLD);
365
+ #endif
366
+ #ifdef SIGSTKFLT
367
+ V(SIGSTKFLT);
368
+ #endif
369
+ #ifdef SIGCONT
370
+ V(SIGCONT);
371
+ #endif
372
+ #ifdef SIGSTOP
373
+ V(SIGSTOP);
374
+ #endif
375
+ #ifdef SIGTSTP
376
+ V(SIGTSTP);
377
+ #endif
378
+ #ifdef SIGBREAK
379
+ V(SIGBREAK);
380
+ #endif
381
+ #ifdef SIGTTIN
382
+ V(SIGTTIN);
383
+ #endif
384
+ #ifdef SIGTTOU
385
+ V(SIGTTOU);
386
+ #endif
387
+ #ifdef SIGURG
388
+ V(SIGURG);
389
+ #endif
390
+ #ifdef SIGXCPU
391
+ V(SIGXCPU);
392
+ #endif
393
+ #ifdef SIGXFSZ
394
+ V(SIGXFSZ);
395
+ #endif
396
+ #ifdef SIGVTALRM
397
+ V(SIGVTALRM);
398
+ #endif
399
+ #ifdef SIGPROF
400
+ V(SIGPROF);
401
+ #endif
402
+ #ifdef SIGWINCH
403
+ V(SIGWINCH);
404
+ #endif
405
+ #ifdef SIGIO
406
+ V(SIGIO);
407
+ #endif
408
+ #ifdef SIGPOLL
409
+ V(SIGPOLL);
410
+ #endif
411
+ #ifdef SIGLOST
412
+ V(SIGLOST);
413
+ #endif
414
+ #ifdef SIGPWR
415
+ V(SIGPWR);
416
+ #endif
417
+ #ifdef SIGINFO
418
+ V(SIGINFO);
419
+ #endif
420
+ #ifdef SIGSYS
421
+ V(SIGSYS);
422
+ #endif
423
+ #ifdef SIGUNUSED
424
+ V(SIGUNUSED);
425
+ #endif
426
+ #undef V
427
+
428
+ return exports;
429
+ }
430
+
431
+ NAPI_MODULE(bare_signals, bare_signals_exports)
package/binding.js ADDED
@@ -0,0 +1,7 @@
1
+ // Load the native addon. Under the Bare runtime `require.addon()` resolves
2
+ // the cmake-bare addon; under Node.js fall back to the node-gyp built addon.
3
+ if (typeof require.addon === 'function') {
4
+ module.exports = require.addon()
5
+ } else {
6
+ module.exports = require('./build/Release/bare_signals.node')
7
+ }
Binary file
package/index.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import EventEmitter from 'bare-events'
2
+
3
+ interface SignalEmitter extends EventEmitter<{ [signal: string]: [string, number] }> {
4
+ ref(): this
5
+ unref(): this
6
+ }
7
+
8
+ declare class SignalEmitter {}
9
+
10
+ declare class SignalError extends Error {
11
+ readonly code: string
12
+ }
13
+
14
+ interface Signal extends EventEmitter<{ close: []; signal: [signum: number] }> {
15
+ start(): this
16
+ stop(): this
17
+ ref(): this
18
+ unref(): this
19
+ close(): Promise<void>
20
+ }
21
+
22
+ declare class Signal {
23
+ constructor(signum: number | string)
24
+ }
25
+
26
+ declare namespace Signal {
27
+ export { SignalEmitter as Emitter, SignalError as errors }
28
+
29
+ export const constants: Record<string, number>
30
+ }
31
+
32
+ export = Signal
package/index.js ADDED
@@ -0,0 +1,71 @@
1
+ const EventEmitter = require('bare-events')
2
+ const binding = require('./binding')
3
+ const errors = require('./lib/errors')
4
+
5
+ const signals = binding.signals
6
+
7
+ module.exports = exports = class Signal extends EventEmitter {
8
+ constructor(signum) {
9
+ super()
10
+
11
+ if (typeof signum === 'string') {
12
+ if (signum in signals === false) {
13
+ throw errors.UNKNOWN_SIGNAL(`Unknown signal '${signum}'`)
14
+ }
15
+
16
+ signum = signals[signum]
17
+ }
18
+
19
+ this._signum = signum
20
+ this._closing = null
21
+ this._handle = binding.init(this, this._onsignal, this._onclose)
22
+ }
23
+
24
+ start() {
25
+ if (this._closing) throw errors.SIGNAL_CLOSED('Signal is closed')
26
+ binding.start(this._handle, this._signum)
27
+ return this
28
+ }
29
+
30
+ stop() {
31
+ if (this._closing) return this
32
+ binding.stop(this._handle)
33
+ return this
34
+ }
35
+
36
+ ref() {
37
+ if (this._closing) return this
38
+ binding.ref(this._handle)
39
+ return this
40
+ }
41
+
42
+ unref() {
43
+ if (this._closing) return this
44
+ binding.unref(this._handle)
45
+ return this
46
+ }
47
+
48
+ close() {
49
+ if (this._closing) return this._closing
50
+ this._closing = EventEmitter.once(this, 'close')
51
+
52
+ binding.close(this._handle)
53
+
54
+ return this._closing
55
+ }
56
+
57
+ _onsignal() {
58
+ this.emit('signal', this._signum)
59
+ }
60
+
61
+ _onclose() {
62
+ this._handle = null
63
+
64
+ this.emit('close')
65
+ }
66
+ }
67
+
68
+ exports.Emitter = require('./lib/emitter')
69
+
70
+ exports.constants = signals
71
+ exports.errors = errors
package/lib/emitter.js ADDED
@@ -0,0 +1,57 @@
1
+ const EventEmitter = require('bare-events')
2
+ const Signal = require('..')
3
+
4
+ module.exports = class SignalEmitter extends EventEmitter {
5
+ constructor() {
6
+ super()
7
+
8
+ this._signals = new Map()
9
+ this._unrefed = false
10
+
11
+ this.on('newListener', this._onnewlistener).on('removeListener', this._onremovelistener)
12
+ }
13
+
14
+ ref() {
15
+ this._unrefed = false
16
+ for (const signal of this._signals.values()) signal.ref()
17
+ return this
18
+ }
19
+
20
+ unref() {
21
+ this._unrefed = true
22
+ for (const signal of this._signals.values()) signal.unref()
23
+ return this
24
+ }
25
+
26
+ _onnewlistener(name) {
27
+ if (name === 'newListener' || name === 'removeListener') return
28
+
29
+ if (this.listenerCount(name) === 0) {
30
+ const signal = new Signal(name)
31
+
32
+ signal.on('signal', this._onsignal.bind(this, name)).start()
33
+
34
+ if (this._unrefed) signal.unref()
35
+
36
+ this._signals.set(name, signal)
37
+ }
38
+ }
39
+
40
+ _onremovelistener(name) {
41
+ if (name === 'newListener' || name === 'removeListener') return
42
+
43
+ if (this.listenerCount(name) === 0) {
44
+ const signal = this._signals.get(name)
45
+
46
+ if (this._unrefed) signal.ref()
47
+
48
+ signal.close()
49
+
50
+ this._signals.delete(name)
51
+ }
52
+ }
53
+
54
+ _onsignal(name) {
55
+ this.emit(name, name, Signal.constants[name])
56
+ }
57
+ }
package/lib/errors.js ADDED
@@ -0,0 +1,22 @@
1
+ module.exports = class SignalError extends Error {
2
+ constructor(msg, fn = SignalError, code = fn.name) {
3
+ super(`${code}: ${msg}`)
4
+ this.code = code
5
+
6
+ if (Error.captureStackTrace) {
7
+ Error.captureStackTrace(this, fn)
8
+ }
9
+ }
10
+
11
+ get name() {
12
+ return 'SignalError'
13
+ }
14
+
15
+ static UNKNOWN_SIGNAL(msg) {
16
+ return new SignalError(msg, SignalError.UNKNOWN_SIGNAL)
17
+ }
18
+
19
+ static SIGNAL_CLOSED(msg) {
20
+ return new SignalError(msg, SignalError.SIGNAL_CLOSED)
21
+ }
22
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@ohos-ports/bare-signals",
3
+ "version": "5.0.0-beta.0",
4
+ "description": "Native signal handling for JavaScript",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./index.d.ts",
8
+ "default": "./index.js"
9
+ },
10
+ "./package": "./package.json",
11
+ "./errors": "./lib/errors.js",
12
+ "./emitter": "./lib/emitter.js"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "index.d.ts",
17
+ "binding.c",
18
+ "binding.js",
19
+ "CMakeLists.txt",
20
+ "lib",
21
+ "build/Release/*.node"
22
+ ],
23
+ "addon": true,
24
+ "scripts": {
25
+ "format": "prettier --write . && lunte --fix",
26
+ "lint": "prettier --check . && lunte",
27
+ "test": "brittle-bare --coverage test.js"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
32
+ "directory": "ports/bare-signals/5.0.0"
33
+ },
34
+ "author": "Holepunch",
35
+ "license": "Apache-2.0",
36
+ "bugs": {
37
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
38
+ },
39
+ "homepage": "https://github.com/holepunchto/bare-signals#readme",
40
+ "engines": {
41
+ "bare": ">=1.7.0"
42
+ },
43
+ "dependencies": {
44
+ "bare-events": "^2.5.3"
45
+ },
46
+ "devDependencies": {
47
+ "@ohos-ports/bare-os": "^3.9.3-beta.0",
48
+ "brittle": "^3.2.1",
49
+ "cmake-bare": "^1.1.6",
50
+ "lunte": "^1.8.3",
51
+ "prettier": "^3.4.1",
52
+ "prettier-config-holepunch": "^2.0.0"
53
+ }
54
+ }