@zlink-systems/zlink 0.17.3

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.
Files changed (199) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +198 -0
  3. package/README.typedoc.md +56 -0
  4. package/THIRD_PARTY_NOTICES.md +84 -0
  5. package/binding.gyp +27 -0
  6. package/dist/index.d.mts +3 -0
  7. package/dist/index.d.ts +24 -0
  8. package/dist/index.js +117 -0
  9. package/dist/index.mjs +4 -0
  10. package/dist/zlink/contracts/core/buffer_like.d.ts +2 -0
  11. package/dist/zlink/contracts/core/buffer_like.js +3 -0
  12. package/dist/zlink/contracts/core/context.d.ts +103 -0
  13. package/dist/zlink/contracts/core/context.js +11 -0
  14. package/dist/zlink/contracts/core/index.d.ts +3 -0
  15. package/dist/zlink/contracts/core/index.js +20 -0
  16. package/dist/zlink/contracts/core/routing_id.d.ts +28 -0
  17. package/dist/zlink/contracts/core/routing_id.js +120 -0
  18. package/dist/zlink/contracts/errors/errors.d.ts +44 -0
  19. package/dist/zlink/contracts/errors/errors.js +89 -0
  20. package/dist/zlink/contracts/errors/index.d.ts +2 -0
  21. package/dist/zlink/contracts/errors/index.js +29 -0
  22. package/dist/zlink/contracts/errors/results.d.ts +106 -0
  23. package/dist/zlink/contracts/errors/results.js +102 -0
  24. package/dist/zlink/contracts/eventing/index.d.ts +3 -0
  25. package/dist/zlink/contracts/eventing/index.js +20 -0
  26. package/dist/zlink/contracts/eventing/monitor.d.ts +157 -0
  27. package/dist/zlink/contracts/eventing/monitor.js +82 -0
  28. package/dist/zlink/contracts/eventing/poller.d.ts +76 -0
  29. package/dist/zlink/contracts/eventing/poller.js +3 -0
  30. package/dist/zlink/contracts/eventing/timer.d.ts +38 -0
  31. package/dist/zlink/contracts/eventing/timer.js +3 -0
  32. package/dist/zlink/contracts/index.d.ts +5 -0
  33. package/dist/zlink/contracts/index.js +22 -0
  34. package/dist/zlink/contracts/messaging/handlers.d.ts +5 -0
  35. package/dist/zlink/contracts/messaging/handlers.js +3 -0
  36. package/dist/zlink/contracts/messaging/index.d.ts +8 -0
  37. package/dist/zlink/contracts/messaging/index.js +15 -0
  38. package/dist/zlink/contracts/messaging/message.d.ts +68 -0
  39. package/dist/zlink/contracts/messaging/message.js +280 -0
  40. package/dist/zlink/contracts/messaging/message_parts_envelope.d.ts +14 -0
  41. package/dist/zlink/contracts/messaging/message_parts_envelope.js +56 -0
  42. package/dist/zlink/contracts/messaging/operations.d.ts +54 -0
  43. package/dist/zlink/contracts/messaging/operations.js +3 -0
  44. package/dist/zlink/contracts/messaging/received.d.ts +41 -0
  45. package/dist/zlink/contracts/messaging/received.js +130 -0
  46. package/dist/zlink/contracts/messaging/stream_packet.d.ts +14 -0
  47. package/dist/zlink/contracts/messaging/stream_packet.js +33 -0
  48. package/dist/zlink/contracts/messaging/subscription_event.d.ts +12 -0
  49. package/dist/zlink/contracts/messaging/subscription_event.js +20 -0
  50. package/dist/zlink/contracts/messaging/topic_message.d.ts +14 -0
  51. package/dist/zlink/contracts/messaging/topic_message.js +36 -0
  52. package/dist/zlink/contracts/sockets/dealer_socket.d.ts +29 -0
  53. package/dist/zlink/contracts/sockets/dealer_socket.js +3 -0
  54. package/dist/zlink/contracts/sockets/index.d.ts +10 -0
  55. package/dist/zlink/contracts/sockets/index.js +15 -0
  56. package/dist/zlink/contracts/sockets/pair_socket.d.ts +20 -0
  57. package/dist/zlink/contracts/sockets/pair_socket.js +3 -0
  58. package/dist/zlink/contracts/sockets/pubsub_sockets.d.ts +40 -0
  59. package/dist/zlink/contracts/sockets/pubsub_sockets.js +3 -0
  60. package/dist/zlink/contracts/sockets/router_socket.d.ts +32 -0
  61. package/dist/zlink/contracts/sockets/router_socket.js +3 -0
  62. package/dist/zlink/contracts/sockets/socket.d.ts +58 -0
  63. package/dist/zlink/contracts/sockets/socket.js +3 -0
  64. package/dist/zlink/contracts/sockets/socket_constants.d.ts +88 -0
  65. package/dist/zlink/contracts/sockets/socket_constants.js +67 -0
  66. package/dist/zlink/contracts/sockets/socket_options.d.ts +105 -0
  67. package/dist/zlink/contracts/sockets/socket_options.js +3 -0
  68. package/dist/zlink/contracts/sockets/stream_socket.d.ts +31 -0
  69. package/dist/zlink/contracts/sockets/stream_socket.js +3 -0
  70. package/dist/zlink/runtime/buffers/message_conversion.d.ts +6 -0
  71. package/dist/zlink/runtime/buffers/message_conversion.js +49 -0
  72. package/dist/zlink/runtime/core/context.d.ts +43 -0
  73. package/dist/zlink/runtime/core/context.js +150 -0
  74. package/dist/zlink/runtime/core/context_options.d.ts +19 -0
  75. package/dist/zlink/runtime/core/context_options.js +23 -0
  76. package/dist/zlink/runtime/core/routing_id.d.ts +3 -0
  77. package/dist/zlink/runtime/core/routing_id.js +30 -0
  78. package/dist/zlink/runtime/core/routing_id_conversion.d.ts +2 -0
  79. package/dist/zlink/runtime/core/routing_id_conversion.js +11 -0
  80. package/dist/zlink/runtime/core/runtime_info.d.ts +8 -0
  81. package/dist/zlink/runtime/core/runtime_info.js +39 -0
  82. package/dist/zlink/runtime/errors/error_mapping.d.ts +4 -0
  83. package/dist/zlink/runtime/errors/error_mapping.js +132 -0
  84. package/dist/zlink/runtime/errors/error_state.d.ts +1 -0
  85. package/dist/zlink/runtime/errors/error_state.js +15 -0
  86. package/dist/zlink/runtime/errors/native_errors.d.ts +16 -0
  87. package/dist/zlink/runtime/errors/native_errors.js +86 -0
  88. package/dist/zlink/runtime/eventing/counters.d.ts +16 -0
  89. package/dist/zlink/runtime/eventing/counters.js +33 -0
  90. package/dist/zlink/runtime/eventing/index.d.ts +6 -0
  91. package/dist/zlink/runtime/eventing/index.js +22 -0
  92. package/dist/zlink/runtime/eventing/monitor_event_state.d.ts +3 -0
  93. package/dist/zlink/runtime/eventing/monitor_event_state.js +19 -0
  94. package/dist/zlink/runtime/eventing/monitor_raw.d.ts +50 -0
  95. package/dist/zlink/runtime/eventing/monitor_raw.js +3 -0
  96. package/dist/zlink/runtime/eventing/monitor_socket.d.ts +9 -0
  97. package/dist/zlink/runtime/eventing/monitor_socket.js +39 -0
  98. package/dist/zlink/runtime/eventing/monitor_status.d.ts +3 -0
  99. package/dist/zlink/runtime/eventing/monitor_status.js +52 -0
  100. package/dist/zlink/runtime/eventing/poll_events.d.ts +20 -0
  101. package/dist/zlink/runtime/eventing/poll_events.js +92 -0
  102. package/dist/zlink/runtime/eventing/poller.d.ts +34 -0
  103. package/dist/zlink/runtime/eventing/poller.js +275 -0
  104. package/dist/zlink/runtime/eventing/thread.d.ts +6 -0
  105. package/dist/zlink/runtime/eventing/thread.js +52 -0
  106. package/dist/zlink/runtime/eventing/timer.d.ts +8 -0
  107. package/dist/zlink/runtime/eventing/timer.js +45 -0
  108. package/dist/zlink/runtime/handles/native_handle.d.ts +7 -0
  109. package/dist/zlink/runtime/handles/native_handle.js +24 -0
  110. package/dist/zlink/runtime/messaging/completion_owner.d.ts +86 -0
  111. package/dist/zlink/runtime/messaging/completion_owner.js +570 -0
  112. package/dist/zlink/runtime/messaging/message_materializer.d.ts +36 -0
  113. package/dist/zlink/runtime/messaging/message_materializer.js +199 -0
  114. package/dist/zlink/runtime/messaging/message_parts_state.d.ts +4 -0
  115. package/dist/zlink/runtime/messaging/message_parts_state.js +20 -0
  116. package/dist/zlink/runtime/messaging/message_snapshot.d.ts +13 -0
  117. package/dist/zlink/runtime/messaging/message_snapshot.js +42 -0
  118. package/dist/zlink/runtime/messaging/received_operations.d.ts +5 -0
  119. package/dist/zlink/runtime/messaging/received_operations.js +46 -0
  120. package/dist/zlink/runtime/messaging/received_state.d.ts +13 -0
  121. package/dist/zlink/runtime/messaging/received_state.js +29 -0
  122. package/dist/zlink/runtime/messaging/request_executor.d.ts +2 -0
  123. package/dist/zlink/runtime/messaging/request_executor.js +8 -0
  124. package/dist/zlink/runtime/messaging/send_operation_base.d.ts +13 -0
  125. package/dist/zlink/runtime/messaging/send_operation_base.js +52 -0
  126. package/dist/zlink/runtime/messaging/subscription_event_state.d.ts +4 -0
  127. package/dist/zlink/runtime/messaging/subscription_event_state.js +17 -0
  128. package/dist/zlink/runtime/messaging/topic_message_state.d.ts +11 -0
  129. package/dist/zlink/runtime/messaging/topic_message_state.js +75 -0
  130. package/dist/zlink/runtime/native/binding.d.ts +6 -0
  131. package/dist/zlink/runtime/native/binding.js +3 -0
  132. package/dist/zlink/runtime/native/binding_context.d.ts +14 -0
  133. package/dist/zlink/runtime/native/binding_context.js +3 -0
  134. package/dist/zlink/runtime/native/binding_core.d.ts +21 -0
  135. package/dist/zlink/runtime/native/binding_core.js +3 -0
  136. package/dist/zlink/runtime/native/binding_eventing.d.ts +40 -0
  137. package/dist/zlink/runtime/native/binding_eventing.js +3 -0
  138. package/dist/zlink/runtime/native/binding_socket.d.ts +52 -0
  139. package/dist/zlink/runtime/native/binding_socket.js +3 -0
  140. package/dist/zlink/runtime/native/binding_types.d.ts +8 -0
  141. package/dist/zlink/runtime/native/binding_types.js +3 -0
  142. package/dist/zlink/runtime/native/native.d.ts +2 -0
  143. package/dist/zlink/runtime/native/native.js +40 -0
  144. package/dist/zlink/runtime/native/native_load_paths.d.ts +16 -0
  145. package/dist/zlink/runtime/native/native_load_paths.js +165 -0
  146. package/dist/zlink/runtime/options/byte_values.d.ts +3 -0
  147. package/dist/zlink/runtime/options/byte_values.js +27 -0
  148. package/dist/zlink/runtime/options/option_mapping.d.ts +72 -0
  149. package/dist/zlink/runtime/options/option_mapping.js +41 -0
  150. package/dist/zlink/runtime/options/validation.d.ts +1 -0
  151. package/dist/zlink/runtime/options/validation.js +17 -0
  152. package/dist/zlink/runtime/public_bridge.d.ts +6 -0
  153. package/dist/zlink/runtime/public_bridge.js +23 -0
  154. package/dist/zlink/runtime/sockets/dealer_socket.d.ts +16 -0
  155. package/dist/zlink/runtime/sockets/dealer_socket.js +56 -0
  156. package/dist/zlink/runtime/sockets/index.d.ts +19 -0
  157. package/dist/zlink/runtime/sockets/index.js +27 -0
  158. package/dist/zlink/runtime/sockets/pair_socket.d.ts +7 -0
  159. package/dist/zlink/runtime/sockets/pair_socket.js +12 -0
  160. package/dist/zlink/runtime/sockets/pub_socket.d.ts +7 -0
  161. package/dist/zlink/runtime/sockets/pub_socket.js +12 -0
  162. package/dist/zlink/runtime/sockets/router_socket.d.ts +19 -0
  163. package/dist/zlink/runtime/sockets/router_socket.js +70 -0
  164. package/dist/zlink/runtime/sockets/socket_base.d.ts +21 -0
  165. package/dist/zlink/runtime/sockets/socket_base.js +102 -0
  166. package/dist/zlink/runtime/sockets/socket_operation_builders.d.ts +38 -0
  167. package/dist/zlink/runtime/sockets/socket_operation_builders.js +93 -0
  168. package/dist/zlink/runtime/sockets/socket_operations.d.ts +40 -0
  169. package/dist/zlink/runtime/sockets/socket_operations.js +177 -0
  170. package/dist/zlink/runtime/sockets/socket_options.d.ts +104 -0
  171. package/dist/zlink/runtime/sockets/socket_options.js +259 -0
  172. package/dist/zlink/runtime/sockets/socket_submit_errors.d.ts +3 -0
  173. package/dist/zlink/runtime/sockets/socket_submit_errors.js +13 -0
  174. package/dist/zlink/runtime/sockets/stream_socket.d.ts +15 -0
  175. package/dist/zlink/runtime/sockets/stream_socket.js +93 -0
  176. package/dist/zlink/runtime/sockets/sub_socket.d.ts +7 -0
  177. package/dist/zlink/runtime/sockets/sub_socket.js +12 -0
  178. package/dist/zlink/runtime/sockets/xpub_socket.d.ts +10 -0
  179. package/dist/zlink/runtime/sockets/xpub_socket.js +39 -0
  180. package/dist/zlink/runtime/sockets/xsub_socket.d.ts +7 -0
  181. package/dist/zlink/runtime/sockets/xsub_socket.js +12 -0
  182. package/native/src/addon.cc +11 -0
  183. package/native/src/addon_common_api.h +57 -0
  184. package/native/src/addon_core.cc +3974 -0
  185. package/native/src/addon_core_api.h +110 -0
  186. package/native/src/addon_core_options.cc +119 -0
  187. package/native/src/addon_core_options.h +9 -0
  188. package/native/src/addon_exports.cc +151 -0
  189. package/native/src/addon_exports.h +7 -0
  190. package/native/src/addon_message_parts.h +67 -0
  191. package/native/src/addon_message_values.h +345 -0
  192. package/native/src/addon_monitor_status_values.h +71 -0
  193. package/native/src/addon_submit_results.h +30 -0
  194. package/package.json +74 -0
  195. package/prebuilds/linux-x64/libzlink.so.0.17.3 +0 -0
  196. package/prebuilds/linux-x64/zlink.node +0 -0
  197. package/provenance/core-package-provenance.json +73 -0
  198. package/scripts/install.js +37 -0
  199. package/scripts/resolve_core.js +86 -0
@@ -0,0 +1,34 @@
1
+ import { type PollEventFlagValue } from '../../contracts/sockets/socket_constants';
2
+ import { PollEvents } from './poll_events';
3
+ import { Timer } from './timer';
4
+ import { MonitorSocket } from './monitor_socket';
5
+ import type { RuntimeBaseSocket as BaseSocket } from '../sockets';
6
+ type BasePollable = BaseSocket | MonitorSocket;
7
+ export declare class Poller {
8
+ private _native;
9
+ private readonly _socketRegistrations;
10
+ private readonly _timerRegistrations;
11
+ private readonly _fdRegistrations;
12
+ private readonly _registrationsByToken;
13
+ private _nextRegistrationToken;
14
+ constructor();
15
+ add(socket: BasePollable, events: readonly PollEventFlagValue[], slot: number): void;
16
+ add(timer: Timer, slot: number): void;
17
+ modify(socket: BasePollable, events: readonly PollEventFlagValue[]): void;
18
+ remove(socket: BasePollable): boolean;
19
+ remove(timer: Timer): boolean;
20
+ addFd(fd: number, events: readonly PollEventFlagValue[], slot: number): void;
21
+ modifyFd(fd: number, events: readonly PollEventFlagValue[]): void;
22
+ removeFd(fd: number): boolean;
23
+ get size(): number;
24
+ wait(events: PollEvents, timeoutMs: number): number;
25
+ destroy(): void;
26
+ close(): void;
27
+ private addSocketInternal;
28
+ private modifySocketInternal;
29
+ private removeSocketInternal;
30
+ private allocateRegistrationToken;
31
+ private addTimerInternal;
32
+ private removeTimerInternal;
33
+ }
34
+ export { Poller as RuntimePoller };
@@ -0,0 +1,275 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.RuntimePoller = exports.Poller = void 0;
5
+ const socket_constants_1 = require("../../contracts/sockets/socket_constants");
6
+ const error_mapping_1 = require("../errors/error_mapping");
7
+ const native_errors_1 = require("../errors/native_errors");
8
+ const native_handle_1 = require("../handles/native_handle");
9
+ const native_1 = require("../native/native");
10
+ const socket_options_1 = require("../sockets/socket_options");
11
+ const timer_1 = require("./timer");
12
+ const monitor_socket_1 = require("./monitor_socket");
13
+ const completion_owner_1 = require("../messaging/completion_owner");
14
+ function validateSlot(slot) {
15
+ if (!Number.isSafeInteger(slot) || slot < 0) {
16
+ throw new RangeError('slot must be a non-negative safe integer');
17
+ }
18
+ return slot;
19
+ }
20
+ function acquireCompletionOwner(owner, poller) {
21
+ try {
22
+ return owner.transferToPublic(poller);
23
+ }
24
+ catch (error) {
25
+ throw (0, error_mapping_1.createError)('config', 16, (0, native_errors_1.nativeErrorMessage)(error, 'poller completion ownership transfer failed'));
26
+ }
27
+ }
28
+ function validateMonitorEvents(events) {
29
+ if (events !== socket_constants_1.PollEventFlag.PollIn) {
30
+ throw (0, error_mapping_1.createError)('config', 22, 'socket monitor poll events must contain PollIn only');
31
+ }
32
+ return events;
33
+ }
34
+ class Poller {
35
+ _native;
36
+ _socketRegistrations = new Map();
37
+ _timerRegistrations = new Map();
38
+ _fdRegistrations = new Map();
39
+ _registrationsByToken = new Map();
40
+ _nextRegistrationToken = 1;
41
+ constructor() { this._native = (0, native_1.requireNative)().pollerNew(); }
42
+ add(item, eventsOrSlot, slot) {
43
+ if (item instanceof timer_1.Timer) {
44
+ this.addTimerInternal(item, eventsOrSlot);
45
+ return;
46
+ }
47
+ const events = (0, socket_options_1.flagsToMask)(eventsOrSlot);
48
+ this.addSocketInternal(item, item instanceof monitor_socket_1.MonitorSocket ? validateMonitorEvents(events) : events, slot);
49
+ }
50
+ modify(socket, events) {
51
+ const mask = (0, socket_options_1.flagsToMask)(events);
52
+ this.modifySocketInternal(socket, socket instanceof monitor_socket_1.MonitorSocket ? validateMonitorEvents(mask) : mask);
53
+ }
54
+ remove(item) {
55
+ if (item instanceof timer_1.Timer) {
56
+ return this.removeTimerInternal(item);
57
+ }
58
+ return this.removeSocketInternal(item);
59
+ }
60
+ addFd(fd, events, slot) {
61
+ const mask = (0, socket_options_1.flagsToMask)(events);
62
+ const normalizedFd = fd | 0;
63
+ const normalizedSlot = validateSlot(slot);
64
+ const nativeToken = this.allocateRegistrationToken();
65
+ try {
66
+ (0, native_1.requireNative)().pollerAddFd(this._native, normalizedFd, BigInt(nativeToken), mask);
67
+ const registration = {
68
+ source: normalizedFd,
69
+ slot: normalizedSlot,
70
+ nativeToken,
71
+ };
72
+ this._fdRegistrations.set(normalizedFd, registration);
73
+ this._registrationsByToken.set(nativeToken, registration);
74
+ }
75
+ catch (error) {
76
+ throw (0, error_mapping_1.createError)('config', (0, native_errors_1.readErrno)(), (0, native_errors_1.nativeErrorMessage)(error, 'poller fd add failed'));
77
+ }
78
+ }
79
+ modifyFd(fd, events) {
80
+ const mask = (0, socket_options_1.flagsToMask)(events);
81
+ const normalizedFd = fd | 0;
82
+ (0, native_errors_1.configCall)('poller fd modify failed', () => {
83
+ (0, native_1.requireNative)().pollerModifyFd(this._native, normalizedFd, mask);
84
+ });
85
+ }
86
+ removeFd(fd) {
87
+ const normalizedFd = fd | 0;
88
+ (0, native_errors_1.configCall)('poller fd remove failed', () => {
89
+ (0, native_1.requireNative)().pollerRemoveFd(this._native, normalizedFd);
90
+ });
91
+ const registration = this._fdRegistrations.get(normalizedFd);
92
+ this._fdRegistrations.delete(normalizedFd);
93
+ if (registration)
94
+ this._registrationsByToken.delete(registration.nativeToken);
95
+ return true;
96
+ }
97
+ get size() {
98
+ return (0, native_errors_1.configCall)('poller size failed', () => (0, native_1.requireNative)().pollerSize(this._native));
99
+ }
100
+ wait(events, timeoutMs) {
101
+ let nativeCount;
102
+ try {
103
+ nativeCount = (0, native_1.requireNative)().pollerWaitInto(this._native, (0, native_handle_1.getNativeHandle)(events), events.capacity | 0, timeoutMs | 0);
104
+ }
105
+ catch (error) {
106
+ if ((0, native_errors_1.isWouldBlock)()) {
107
+ events.markCombined(0, []);
108
+ return 0;
109
+ }
110
+ throw (0, native_errors_1.recvNativeError)(error, socket_constants_1.RecvFlags.None, 'poller wait failed');
111
+ }
112
+ events.markReadyCount(nativeCount | 0);
113
+ const processedEvents = [];
114
+ for (let index = 0; index < nativeCount; index += 1) {
115
+ let revents = events.revents(index);
116
+ const sourceKind = events.sourceKind(index);
117
+ const nativeSlot = events.slot(index);
118
+ const registration = this._registrationsByToken.get(nativeSlot);
119
+ if (!registration)
120
+ continue;
121
+ const socketRegistration = typeof registration.source === 'number'
122
+ ? undefined
123
+ : this._socketRegistrations.get(registration.source);
124
+ if (socketRegistration?.owner
125
+ && (revents & (socket_constants_1.PollEventFlag.PollOut | socket_constants_1.PollEventFlag.PollCompletion)) !== 0) {
126
+ const completionReady = socketRegistration.transferred
127
+ && (revents & socket_constants_1.PollEventFlag.PollCompletion) !== 0;
128
+ const managedWritableReady = (revents & socket_constants_1.PollEventFlag.PollOut) !== 0
129
+ && socketRegistration.owner.hasManagedWritableWait();
130
+ const processed = completionReady || managedWritableReady
131
+ ? socketRegistration.owner.drain(this)
132
+ : 0;
133
+ if (processed === 0) {
134
+ revents &= ~socket_constants_1.PollEventFlag.PollCompletion;
135
+ }
136
+ }
137
+ if (revents === 0)
138
+ continue;
139
+ processedEvents.push({
140
+ source: registration.source,
141
+ sourceKind,
142
+ slot: registration.slot,
143
+ revents,
144
+ fd: events.fd(index),
145
+ });
146
+ }
147
+ events.markCombined(0, processedEvents);
148
+ return processedEvents.length;
149
+ }
150
+ destroy() {
151
+ if (this._native) {
152
+ (0, native_errors_1.closeCall)('poller close failed', () => {
153
+ (0, native_1.requireNative)().pollerDestroy(this._native);
154
+ });
155
+ this._native = null;
156
+ for (const registration of this._socketRegistrations.values()) {
157
+ if (registration.transferred)
158
+ registration.owner.transferToRuntime(this);
159
+ }
160
+ }
161
+ this._socketRegistrations.clear();
162
+ this._timerRegistrations.clear();
163
+ this._fdRegistrations.clear();
164
+ this._registrationsByToken.clear();
165
+ }
166
+ close() { this.destroy(); }
167
+ addSocketInternal(socket, events, slot) {
168
+ const normalizedSlot = validateSlot(slot);
169
+ const owner = socket instanceof monitor_socket_1.MonitorSocket ? null : (0, completion_owner_1.completionOwnerOf)(socket);
170
+ const transferred = owner !== null
171
+ && (events & socket_constants_1.PollEventFlag.PollCompletion) !== 0;
172
+ const nativeToken = this.allocateRegistrationToken();
173
+ const handle = (0, native_handle_1.getNativeHandle)(socket);
174
+ const acquired = transferred ? acquireCompletionOwner(owner, this) : false;
175
+ try {
176
+ (0, native_1.requireNative)().pollerAdd(this._native, handle, BigInt(nativeToken), events | 0);
177
+ const registration = {
178
+ source: socket,
179
+ events: events | 0,
180
+ handle,
181
+ slot: normalizedSlot,
182
+ nativeToken,
183
+ owner,
184
+ transferred,
185
+ };
186
+ this._socketRegistrations.set(socket, registration);
187
+ this._registrationsByToken.set(nativeToken, registration);
188
+ }
189
+ catch (error) {
190
+ if (acquired)
191
+ owner.transferToRuntime(this);
192
+ throw (0, error_mapping_1.createError)('config', (0, native_errors_1.readErrno)(), (0, native_errors_1.nativeErrorMessage)(error, 'poller socket add failed'));
193
+ }
194
+ }
195
+ modifySocketInternal(socket, events) {
196
+ const handle = (0, native_handle_1.getNativeHandle)(socket);
197
+ const registration = this._socketRegistrations.get(socket);
198
+ const shouldTransfer = registration?.owner != null
199
+ && (events & socket_constants_1.PollEventFlag.PollCompletion) !== 0;
200
+ const acquired = registration?.owner && shouldTransfer && !registration.transferred
201
+ ? acquireCompletionOwner(registration.owner, this)
202
+ : false;
203
+ try {
204
+ (0, native_errors_1.configCall)('poller socket modify failed', () => {
205
+ (0, native_1.requireNative)().pollerModify(this._native, handle, events | 0);
206
+ });
207
+ }
208
+ catch (error) {
209
+ if (acquired)
210
+ registration.owner.transferToRuntime(this);
211
+ throw error;
212
+ }
213
+ if (registration) {
214
+ if (!shouldTransfer && registration.transferred) {
215
+ registration.owner.transferToRuntime(this);
216
+ }
217
+ registration.events = events | 0;
218
+ registration.transferred = shouldTransfer;
219
+ }
220
+ }
221
+ removeSocketInternal(socket) {
222
+ (0, native_errors_1.configCall)('poller socket remove failed', () => {
223
+ (0, native_1.requireNative)().pollerRemove(this._native, (0, native_handle_1.getNativeHandle)(socket));
224
+ });
225
+ const registration = this._socketRegistrations.get(socket);
226
+ if (registration?.transferred)
227
+ registration.owner.transferToRuntime(this);
228
+ this._socketRegistrations.delete(socket);
229
+ if (registration)
230
+ this._registrationsByToken.delete(registration.nativeToken);
231
+ return true;
232
+ }
233
+ allocateRegistrationToken() {
234
+ if (this._registrationsByToken.size >= Number.MAX_SAFE_INTEGER) {
235
+ throw new RangeError('poller registration token space exhausted');
236
+ }
237
+ for (;;) {
238
+ const token = this._nextRegistrationToken;
239
+ this._nextRegistrationToken = token === Number.MAX_SAFE_INTEGER
240
+ ? 1
241
+ : token + 1;
242
+ if (!this._registrationsByToken.has(token))
243
+ return token;
244
+ }
245
+ }
246
+ addTimerInternal(timer, slot) {
247
+ const normalizedSlot = validateSlot(slot);
248
+ const nativeToken = this.allocateRegistrationToken();
249
+ try {
250
+ (0, native_1.requireNative)().pollerAddTimer(this._native, (0, native_handle_1.getNativeHandle)(timer), BigInt(nativeToken));
251
+ const registration = {
252
+ source: timer,
253
+ slot: normalizedSlot,
254
+ nativeToken,
255
+ };
256
+ this._timerRegistrations.set(timer, registration);
257
+ this._registrationsByToken.set(nativeToken, registration);
258
+ }
259
+ catch (error) {
260
+ throw (0, error_mapping_1.createError)('config', (0, native_errors_1.readErrno)(), (0, native_errors_1.nativeErrorMessage)(error, 'poller timer add failed'));
261
+ }
262
+ }
263
+ removeTimerInternal(timer) {
264
+ (0, native_errors_1.configCall)('poller timer remove failed', () => {
265
+ (0, native_1.requireNative)().pollerRemoveTimer(this._native, (0, native_handle_1.getNativeHandle)(timer));
266
+ });
267
+ const registration = this._timerRegistrations.get(timer);
268
+ this._timerRegistrations.delete(timer);
269
+ if (registration)
270
+ this._registrationsByToken.delete(registration.nativeToken);
271
+ return true;
272
+ }
273
+ }
274
+ exports.Poller = Poller;
275
+ exports.RuntimePoller = Poller;
@@ -0,0 +1,6 @@
1
+ export declare class Thread {
2
+ private readonly _state;
3
+ private readonly _worker;
4
+ constructor(handler: () => void);
5
+ join(): void;
6
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Thread = void 0;
5
+ const node_worker_threads_1 = require("node:worker_threads");
6
+ class Thread {
7
+ _state = new Int32Array(new SharedArrayBuffer(4));
8
+ _worker;
9
+ constructor(handler) {
10
+ if (typeof handler !== 'function') {
11
+ throw new TypeError('handler must be a function');
12
+ }
13
+ const source = `
14
+ const { workerData } = require('node:worker_threads');
15
+ const state = new Int32Array(workerData.state);
16
+ (async () => {
17
+ try {
18
+ const handler = (${handler.toString()});
19
+ await handler();
20
+ Atomics.store(state, 0, 1);
21
+ } catch {
22
+ Atomics.store(state, 0, 2);
23
+ } finally {
24
+ Atomics.notify(state, 0);
25
+ }
26
+ })();
27
+ `;
28
+ this._worker = new node_worker_threads_1.Worker(source, {
29
+ eval: true,
30
+ workerData: { state: this._state.buffer }
31
+ });
32
+ this._worker.on('error', () => {
33
+ Atomics.store(this._state, 0, 2);
34
+ Atomics.notify(this._state, 0);
35
+ });
36
+ this._worker.on('exit', (code) => {
37
+ if (code !== 0 && Atomics.load(this._state, 0) === 0) {
38
+ Atomics.store(this._state, 0, 2);
39
+ Atomics.notify(this._state, 0);
40
+ }
41
+ });
42
+ }
43
+ join() {
44
+ while (Atomics.load(this._state, 0) === 0) {
45
+ Atomics.wait(this._state, 0, 0);
46
+ }
47
+ if (Atomics.load(this._state, 0) === 2) {
48
+ throw new Error('thread handler failed');
49
+ }
50
+ }
51
+ }
52
+ exports.Thread = Thread;
@@ -0,0 +1,8 @@
1
+ import { NativeHandle } from '../handles/native_handle';
2
+ export declare class Timer extends NativeHandle {
3
+ constructor();
4
+ start(intervalNs: bigint, repeatCount: bigint): void;
5
+ stop(): void;
6
+ recv(): bigint | null;
7
+ close(): void;
8
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Timer = void 0;
5
+ const errors_1 = require("../../contracts/errors/errors");
6
+ const socket_constants_1 = require("../../contracts/sockets/socket_constants");
7
+ const native_errors_1 = require("../errors/native_errors");
8
+ const native_1 = require("../native/native");
9
+ const native_handle_1 = require("../handles/native_handle");
10
+ class Timer extends native_handle_1.NativeHandle {
11
+ constructor() {
12
+ super((0, native_1.requireNative)().timerNew());
13
+ }
14
+ start(intervalNs, repeatCount) {
15
+ (0, native_errors_1.configCall)('timer start failed', () => {
16
+ (0, native_1.requireNative)().timerStart(this._native, intervalNs, repeatCount);
17
+ });
18
+ }
19
+ stop() {
20
+ (0, native_errors_1.configCall)('timer stop failed', () => {
21
+ (0, native_1.requireNative)().timerStop(this._native);
22
+ });
23
+ }
24
+ recv() {
25
+ const flags = socket_constants_1.RecvFlags.None;
26
+ try {
27
+ return (0, native_1.requireNative)().timerRecv(this._native, flags | 0);
28
+ }
29
+ catch (error) {
30
+ const recvError = (0, native_errors_1.recvNativeError)(error, flags, 'timer recv failed');
31
+ if (recvError.result === errors_1.RecvResult.NoData)
32
+ return null;
33
+ throw recvError;
34
+ }
35
+ }
36
+ close() {
37
+ if (this._native) {
38
+ (0, native_errors_1.closeCall)('timer close failed', () => {
39
+ (0, native_1.requireNative)().timerDestroy(this._native);
40
+ });
41
+ this._native = null;
42
+ }
43
+ }
44
+ }
45
+ exports.Timer = Timer;
@@ -0,0 +1,7 @@
1
+ export declare function getNativeHandle(handle: NativeHandle): unknown;
2
+ export declare class NativeHandle {
3
+ protected constructor(native: unknown);
4
+ protected get _native(): unknown | null;
5
+ protected set _native(native: unknown | null);
6
+ close(): void;
7
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.NativeHandle = void 0;
5
+ exports.getNativeHandle = getNativeHandle;
6
+ const nativeHandles = new WeakMap();
7
+ function getNativeHandle(handle) {
8
+ return nativeHandles.get(handle) ?? null;
9
+ }
10
+ class NativeHandle {
11
+ constructor(native) {
12
+ nativeHandles.set(this, native);
13
+ }
14
+ get _native() {
15
+ return nativeHandles.get(this) ?? null;
16
+ }
17
+ set _native(native) {
18
+ nativeHandles.set(this, native);
19
+ }
20
+ close() {
21
+ this._native = null;
22
+ }
23
+ }
24
+ exports.NativeHandle = NativeHandle;
@@ -0,0 +1,86 @@
1
+ import { Message, type MessageLike } from '../../contracts';
2
+ import type { NativeHandle } from '../native/binding_types';
3
+ import type { OperationPayloadValue } from './send_operation_base';
4
+ export interface NativeCompletion {
5
+ readonly kind: number;
6
+ readonly completionId: bigint;
7
+ readonly userContext: bigint;
8
+ readonly peerRoutingId: Buffer | null;
9
+ readonly sendResult: number;
10
+ readonly terminalErrno: number;
11
+ readonly requestResult: number;
12
+ readonly parts?: Buffer[];
13
+ }
14
+ export interface NativeSubmitResult {
15
+ readonly result: number;
16
+ readonly nativeErrno: number;
17
+ readonly completionId: bigint;
18
+ }
19
+ export interface NativeSyncRequestResult extends NativeSubmitResult {
20
+ readonly completions: NativeCompletion[];
21
+ }
22
+ type CompletionKind = 'send' | 'request';
23
+ export declare function consumeSubmittedMessages(payload: OperationPayloadValue<MessageLike>): void;
24
+ /** Two-phase correlation entry: publish and capture may arrive in either order. */
25
+ export declare class CompletionEntry<T> {
26
+ readonly token: bigint;
27
+ readonly kind: CompletionKind;
28
+ readonly expectsUserContext: boolean;
29
+ readonly promise: Promise<T>;
30
+ completionId: bigint;
31
+ published: boolean;
32
+ captured: boolean;
33
+ settled: boolean;
34
+ value: T | undefined;
35
+ error: unknown;
36
+ private capturedCompletionId;
37
+ private resolvePromise;
38
+ private rejectPromise;
39
+ constructor(token: bigint, kind: CompletionKind, expectsUserContext?: boolean);
40
+ publish(completionId: bigint): void;
41
+ awaitWritable(completionId: bigint): void;
42
+ succeed(value: T): void;
43
+ capture(completion: NativeCompletion): void;
44
+ fail(error: unknown): void;
45
+ private settleIfJoined;
46
+ private validateRequestCorrelation;
47
+ }
48
+ export declare class CompletionOwner {
49
+ private readonly handle;
50
+ private readonly native;
51
+ private readonly byToken;
52
+ private readonly byId;
53
+ private readonly retries;
54
+ private readonly writableRetries;
55
+ private nextToken;
56
+ private publicOwner;
57
+ private runtimeWatch;
58
+ private managedWritableWaitCount;
59
+ private closed;
60
+ constructor(handle: NativeHandle);
61
+ submitSend(payload: OperationPayloadValue<MessageLike>, routingId: Buffer | null): Promise<void>;
62
+ submitRequest(payload: OperationPayloadValue<MessageLike>, target: Buffer | null, timeoutMs: number): Promise<Message[]>;
63
+ requestSync(payload: OperationPayloadValue<MessageLike>, target: Buffer | null, timeoutMs: number): Message[];
64
+ sendSync(payload: OperationPayloadValue<MessageLike>, routingId: Buffer | null): void;
65
+ transferToPublic(owner: object): boolean;
66
+ transferToRuntime(owner: object): void;
67
+ drain(caller?: object): number;
68
+ hasManagedWritableWait(): boolean;
69
+ close(): void;
70
+ private register;
71
+ private createEntry;
72
+ private publish;
73
+ private capture;
74
+ private captureWritable;
75
+ private attemptRetry;
76
+ private failEntry;
77
+ private removeIfSettled;
78
+ private removeRetry;
79
+ private ensureRuntimeWatch;
80
+ private runtimeWake;
81
+ private closeRuntimeWatch;
82
+ }
83
+ export declare function installCompletionOwner(socket: object, handle: NativeHandle): CompletionOwner;
84
+ export declare function completionOwnerOf(socket: object): CompletionOwner;
85
+ export declare function releaseCompletionOwner(socket: object): void;
86
+ export {};