66import { EventEmitter } from "events"
77import * as zmq from "."
88import { FullError } from "./errors"
9+ import * as longOptions from "./compat/long-options"
10+ import * as pollStates from "./compat/poll-states"
11+ import * as sendOptions from "./compat/send-options"
912
1013type AnySocket =
1114 | zmq . Pair
@@ -22,99 +25,6 @@ type AnySocket =
2225 | zmq . Stream
2326
2427let count = 1
25- const types = {
26- ZMQ_PAIR : 0 ,
27- ZMQ_PUB : 1 ,
28- ZMQ_SUB : 2 ,
29- ZMQ_REQ : 3 ,
30- ZMQ_REP : 4 ,
31- ZMQ_DEALER : 5 ,
32- ZMQ_XREQ : 5 ,
33- ZMQ_ROUTER : 6 ,
34- ZMQ_XREP : 6 ,
35- ZMQ_PULL : 7 ,
36- ZMQ_PUSH : 8 ,
37- ZMQ_XPUB : 9 ,
38- ZMQ_XSUB : 10 ,
39- ZMQ_STREAM : 11 ,
40- }
41-
42- const longOptions = {
43- ZMQ_AFFINITY : 4 ,
44- ZMQ_IDENTITY : 5 ,
45- ZMQ_SUBSCRIBE : 6 ,
46- ZMQ_UNSUBSCRIBE : 7 ,
47- ZMQ_RATE : 8 ,
48- ZMQ_RECOVERY_IVL : 9 ,
49- ZMQ_RECOVERY_IVL_MSEC : 9 ,
50- ZMQ_SNDBUF : 11 ,
51- ZMQ_RCVBUF : 12 ,
52- ZMQ_RCVMORE : 13 ,
53- ZMQ_FD : 14 ,
54- ZMQ_EVENTS : 15 ,
55- ZMQ_TYPE : 16 ,
56- ZMQ_LINGER : 17 ,
57- ZMQ_RECONNECT_IVL : 18 ,
58- ZMQ_BACKLOG : 19 ,
59- ZMQ_RECONNECT_IVL_MAX : 21 ,
60- ZMQ_MAXMSGSIZE : 22 ,
61- ZMQ_SNDHWM : 23 ,
62- ZMQ_RCVHWM : 24 ,
63- ZMQ_MULTICAST_HOPS : 25 ,
64- ZMQ_RCVTIMEO : 27 ,
65- ZMQ_SNDTIMEO : 28 ,
66- ZMQ_IPV4ONLY : 31 ,
67- ZMQ_LAST_ENDPOINT : 32 ,
68- ZMQ_ROUTER_MANDATORY : 33 ,
69- ZMQ_TCP_KEEPALIVE : 34 ,
70- ZMQ_TCP_KEEPALIVE_CNT : 35 ,
71- ZMQ_TCP_KEEPALIVE_IDLE : 36 ,
72- ZMQ_TCP_KEEPALIVE_INTVL : 37 ,
73- ZMQ_TCP_ACCEPT_FILTER : 38 ,
74- ZMQ_DELAY_ATTACH_ON_CONNECT : 39 ,
75- ZMQ_XPUB_VERBOSE : 40 ,
76- ZMQ_ROUTER_RAW : 41 ,
77- ZMQ_IPV6 : 42 ,
78- ZMQ_MECHANISM : 43 ,
79- ZMQ_PLAIN_SERVER : 44 ,
80- ZMQ_PLAIN_USERNAME : 45 ,
81- ZMQ_PLAIN_PASSWORD : 46 ,
82- ZMQ_CURVE_SERVER : 47 ,
83- ZMQ_CURVE_PUBLICKEY : 48 ,
84- ZMQ_CURVE_SECRETKEY : 49 ,
85- ZMQ_CURVE_SERVERKEY : 50 ,
86- ZMQ_ZAP_DOMAIN : 55 ,
87- ZMQ_HEARTBEAT_IVL : 75 ,
88- ZMQ_HEARTBEAT_TTL : 76 ,
89- ZMQ_HEARTBEAT_TIMEOUT : 77 ,
90- ZMQ_CONNECT_TIMEOUT : 79 ,
91- ZMQ_IO_THREADS : 1 ,
92- ZMQ_MAX_SOCKETS : 2 ,
93- ZMQ_ROUTER_HANDOVER : 56 ,
94- }
95-
96- const pollStates = {
97- ZMQ_POLLIN : 1 ,
98- ZMQ_POLLOUT : 2 ,
99- ZMQ_POLLERR : 4 ,
100- }
101-
102- const sendOptions = {
103- ZMQ_SNDMORE : 2 ,
104- }
105-
106- const capabilities = {
107- ZMQ_CAN_MONITOR : 1 ,
108- ZMQ_CAN_DISCONNECT : 1 ,
109- ZMQ_CAN_UNBIND : 1 ,
110- ZMQ_CAN_SET_CTX : 1 ,
111- }
112-
113- const socketStates = {
114- STATE_READY : 0 ,
115- STATE_BUSY : 1 ,
116- STATE_CLOSED : 2 ,
117- }
11828
11929const shortOptions = {
12030 _fd : longOptions . ZMQ_FD ,
@@ -241,9 +151,11 @@ class Socket extends EventEmitter {
241151 case "stream" :
242152 this . _socket = new zmq . Stream ( )
243153 break
154+ default :
155+ throw new Error ( `Invalid socket type: ${ type } ` )
244156 }
245157
246- const recv = async ( ) => {
158+ const recv = ( ) => {
247159 this . once ( "_flushRecv" , async ( ) => {
248160 while ( ! this . _socket . closed && ! this . _paused ) {
249161 await this . _recv ( )
@@ -347,7 +259,7 @@ class Socket extends EventEmitter {
347259 . catch ( err => {
348260 process . nextTick ( ( ) => {
349261 if ( cb ) {
350- cb ( err )
262+ cb ( err as Error )
351263 } else {
352264 this . emit ( "error" , err )
353265 }
@@ -371,7 +283,7 @@ class Socket extends EventEmitter {
371283 . catch ( err => {
372284 process . nextTick ( ( ) => {
373285 if ( cb ) {
374- cb ( err )
286+ cb ( err as Error )
375287 } else {
376288 this . emit ( "error" , err )
377289 }
@@ -391,8 +303,12 @@ class Socket extends EventEmitter {
391303 return this
392304 }
393305
394- send ( message : zmq . MessageLike [ ] , flags = 0 , cb ?: Callback ) {
395- flags = flags | 0
306+ send (
307+ message : zmq . MessageLike [ ] | zmq . MessageLike ,
308+ givenFlags : number | undefined | null = 0 ,
309+ cb : Callback | undefined = undefined ,
310+ ) {
311+ const flags = ( givenFlags ?? 0 ) | 0
396312 this . _msg = this . _msg . concat ( message )
397313 if ( ( flags & sendOptions . ZMQ_SNDMORE ) === 0 ) {
398314 this . _sendQueue . push ( [ this . _msg , cb ] )
@@ -460,7 +376,7 @@ class Socket extends EventEmitter {
460376 return this . _socket . closed
461377 }
462378
463- monitor ( interval : number , num : number ) {
379+ monitor ( interval ? : number , num ? : number ) {
464380 this . _count = count ++
465381
466382 /* eslint-disable-next-line no-unused-expressions */
@@ -560,8 +476,9 @@ class Socket extends EventEmitter {
560476 }
561477 }
562478
563- setsockopt ( option : number | keyof typeof shortOptions , value : any ) {
564- option = typeof option !== "number" ? shortOptions [ option ] : option
479+ setsockopt ( givenOption : number | keyof typeof shortOptions , value : any ) {
480+ const option =
481+ typeof givenOption === "number" ? givenOption : shortOptions [ givenOption ]
565482
566483 switch ( option ) {
567484 case longOptions . ZMQ_AFFINITY :
@@ -699,8 +616,9 @@ class Socket extends EventEmitter {
699616 return this
700617 }
701618
702- getsockopt ( option : number | keyof typeof shortOptions ) {
703- option = typeof option !== "number" ? shortOptions [ option ] : option
619+ getsockopt ( givenOption : number | keyof typeof shortOptions ) {
620+ const option =
621+ typeof givenOption !== "number" ? shortOptions [ givenOption ] : givenOption
704622
705623 switch ( option ) {
706624 case longOptions . ZMQ_AFFINITY :
@@ -822,10 +740,9 @@ for (const key in shortOptions) {
822740 get ( this : Socket ) {
823741 return this . getsockopt ( shortOptions [ key as keyof typeof shortOptions ] )
824742 } ,
825- set ( this : Socket , val : string | Buffer ) {
826- if ( "string" === typeof val ) {
827- val = Buffer . from ( val , "utf8" )
828- }
743+ set ( this : Socket , givenVal : string | Buffer ) {
744+ const val =
745+ typeof givenVal === "string" ? Buffer . from ( givenVal , "utf8" ) : givenVal
829746 return this . setsockopt (
830747 shortOptions [ key as keyof typeof shortOptions ] ,
831748 val ,
@@ -909,11 +826,9 @@ export {
909826 shortOptions as options ,
910827}
911828
912- /* Unfortunately there is no easy way to include these in the resulting
913- TS definitions. */
914- Object . assign ( module . exports , longOptions )
915- Object . assign ( module . exports , types )
916- Object . assign ( module . exports , pollStates )
917- Object . assign ( module . exports , sendOptions )
918- Object . assign ( module . exports , socketStates )
919- Object . assign ( module . exports , capabilities )
829+ export * from "./compat/long-options"
830+ export * from "./compat/types"
831+ export * from "./compat/poll-states"
832+ export * from "./compat/send-options"
833+ export * from "./compat/capabilities"
834+ export * from "./compat/socket-states"
0 commit comments