@@ -13,6 +13,10 @@ import * as dns from './dns.js'
1313import * as ipc from './ipc.js'
1414import { rand64 , isArrayBufferView } from './util.js'
1515
16+ const BIND_STATE_UNBOUND = 0
17+ const BIND_STATE_BINDING = 1
18+ const BIND_STATE_BOUND = 2
19+
1620const fixBufferList = list => {
1721 const newlist = new Array ( list . length )
1822
@@ -46,6 +50,7 @@ export class Socket extends EventEmitter {
4650 this . state = {
4751 recvBufferSize : options . recvBufferSize ,
4852 sendBufferSize : options . sendBufferSize ,
53+ _bindState : BIND_STATE_UNBOUND ,
4954 connectState : 2 ,
5055 reuseAddr : options . reuseAddr ,
5156 ipv6Only : options . ipv6Only
@@ -111,7 +116,7 @@ export class Socket extends EventEmitter {
111116 }
112117
113118 this . on ( 'error' , removeListeners )
114- this . on ( 'listening' , onListening )
119+ this . once ( 'listening' , onListening )
115120
116121 if ( ! options . address ) {
117122 if ( this . type === 'udp4' ) {
@@ -141,6 +146,9 @@ export class Socket extends EventEmitter {
141146 return { err : errBind }
142147 }
143148
149+ this . _bindState = BIND_STATE_BOUND
150+ setTimeout ( ( ) => this . emit ( 'listening' ) , 1 )
151+
144152 this . _address = options . address
145153 this . _port = options . port
146154 this . _family = isIPv4 ( options . address ) ? 'IPv4' : 'IPv6'
@@ -157,7 +165,7 @@ export class Socket extends EventEmitter {
157165
158166 if ( data . source === 'dnsLookup' ) {
159167 this . _address = data . params . ip
160- return this . emit ( 'listenting ' )
168+ return this . emit ( 'listening ' )
161169 }
162170
163171 if ( data . source === 'udpReadStart' ) {
@@ -368,18 +376,14 @@ export class Socket extends EventEmitter {
368376 throw new Error ( 'Invalid buffer' )
369377 }
370378
371- // @XXX (jwerle): @heapwolf why is this happening in a `send()` call?
372- //
373- // @jwerle it's from the node.js source code - https://github.com/nodejs/node/blob/main/lib/dgram.js#L645
374- // but it's missing a check to see if the instance is unbound (state.bindState === BIND_STATE_UNBOUND)
375- /*
376- const { err: errBind } = this.bind({ port: 0 }, null)
379+ /* if (this._bindState === BIND_STATE_UNBOUND) {
380+ const { err: errBind } = this.bind({ port: 0 }, null)
377381
378- if (errBind) {
379- if (cb) return cb(errBind)
380- return { err: errBind }
381- }
382- */
382+ if (errBind) {
383+ if (cb) return cb(errBind)
384+ return { err: errBind }
385+ }
386+ } */
383387
384388 if ( list . length === 0 ) {
385389 list . push ( Buffer . alloc ( 0 ) )
0 commit comments