@ -35,7 +35,7 @@ const handleConversion = {
'net.Native' : {
'net.Native' : {
simultaneousAccepts : true ,
simultaneousAccepts : true ,
send : function ( message , handle ) {
send : function ( message , handle , options ) {
return handle ;
return handle ;
} ,
} ,
@ -47,7 +47,7 @@ const handleConversion = {
'net.Server' : {
'net.Server' : {
simultaneousAccepts : true ,
simultaneousAccepts : true ,
send : function ( message , server ) {
send : function ( message , server , options ) {
return server . _ handle ;
return server . _ handle ;
} ,
} ,
@ -60,7 +60,7 @@ const handleConversion = {
} ,
} ,
'net.Socket' : {
'net.Socket' : {
send : function ( message , socket ) {
send : function ( message , socket , options ) {
if ( ! socket . _ handle )
if ( ! socket . _ handle )
return ;
return ;
@ -90,7 +90,7 @@ const handleConversion = {
return handle ;
return handle ;
} ,
} ,
postSend : function ( handle ) {
postSend : function ( handle , options ) {
// Close the Socket handle after sending it
// Close the Socket handle after sending it
if ( handle )
if ( handle )
handle . close ( ) ;
handle . close ( ) ;
@ -117,7 +117,7 @@ const handleConversion = {
'dgram.Native' : {
'dgram.Native' : {
simultaneousAccepts : false ,
simultaneousAccepts : false ,
send : function ( message , handle ) {
send : function ( message , handle , options ) {
return handle ;
return handle ;
} ,
} ,
@ -129,7 +129,7 @@ const handleConversion = {
'dgram.Socket' : {
'dgram.Socket' : {
simultaneousAccepts : false ,
simultaneousAccepts : false ,
send : function ( message , socket ) {
send : function ( message , socket , options ) {
message . dgramType = socket . type ;
message . dgramType = socket . type ;
return socket . _ handle ;
return socket . _ handle ;
@ -466,7 +466,7 @@ function setupChannel(target, channel) {
target . _ handleQueue = null ;
target . _ handleQueue = null ;
queue . forEach ( function ( args ) {
queue . forEach ( function ( args ) {
target . _ send ( args . message , args . handle , false , args . callback ) ;
target . _ send ( args . message , args . handle , args . options , args . callback ) ;
} ) ;
} ) ;
// Process a pending disconnect (if any).
// Process a pending disconnect (if any).
@ -498,13 +498,23 @@ function setupChannel(target, channel) {
} ) ;
} ) ;
} ) ;
} ) ;
target . send = function ( message , handle , callback ) {
target . send = function ( message , handle , options , callback ) {
if ( typeof handle === 'function' ) {
if ( typeof handle === 'function' ) {
callback = handle ;
callback = handle ;
handle = undefined ;
handle = undefined ;
options = undefined ;
} else if ( typeof options === 'function' ) {
callback = options ;
options = undefined ;
} else if ( options !== undefined &&
( options === null || typeof options !== 'object' ) ) {
throw new TypeError ( '"options" argument must be an object' ) ;
}
}
options = Object . assign ( { swallowErrors : false } , options ) ;
if ( this . connected ) {
if ( this . connected ) {
return this . _ send ( message , handle , false , callback ) ;
return this . _ send ( message , handle , options , callback ) ;
}
}
const ex = new Error ( 'channel closed' ) ;
const ex = new Error ( 'channel closed' ) ;
if ( typeof callback === 'function' ) {
if ( typeof callback === 'function' ) {
@ -515,12 +525,17 @@ function setupChannel(target, channel) {
return false ;
return false ;
} ;
} ;
target . _ send = function ( message , handle , swallowError s, callback ) {
target . _ send = function ( message , handle , option s, callback ) {
assert ( this . connected || this . _ channel ) ;
assert ( this . connected || this . _ channel ) ;
if ( message === undefined )
if ( message === undefined )
throw new TypeError ( '"message" argument cannot be undefined' ) ;
throw new TypeError ( '"message" argument cannot be undefined' ) ;
// Support legacy function signature
if ( typeof options === 'boolean' ) {
options = { swallowErrors : options } ;
}
// package messages with a handle object
// package messages with a handle object
if ( handle ) {
if ( handle ) {
// this message will be handled by an internalMessage event handler
// this message will be handled by an internalMessage event handler
@ -549,6 +564,7 @@ function setupChannel(target, channel) {
this . _ handleQueue . push ( {
this . _ handleQueue . push ( {
callback : callback ,
callback : callback ,
handle : handle ,
handle : handle ,
options : options ,
message : message . msg ,
message : message . msg ,
} ) ;
} ) ;
return this . _ handleQueue . length === 1 ;
return this . _ handleQueue . length === 1 ;
@ -557,8 +573,10 @@ function setupChannel(target, channel) {
var obj = handleConversion [ message . type ] ;
var obj = handleConversion [ message . type ] ;
// convert TCP object to native handle object
// convert TCP object to native handle object
handle =
handle = handleConversion [ message . type ] . send . call ( target ,
handleConversion [ message . type ] . send . call ( target , message , handle ) ;
message ,
handle ,
options ) ;
// If handle was sent twice, or it is impossible to get native handle
// If handle was sent twice, or it is impossible to get native handle
// out of it - just send a text without the handle.
// out of it - just send a text without the handle.
@ -575,6 +593,7 @@ function setupChannel(target, channel) {
this . _ handleQueue . push ( {
this . _ handleQueue . push ( {
callback : callback ,
callback : callback ,
handle : null ,
handle : null ,
options : options ,
message : message ,
message : message ,
} ) ;
} ) ;
return this . _ handleQueue . length === 1 ;
return this . _ handleQueue . length === 1 ;
@ -593,7 +612,7 @@ function setupChannel(target, channel) {
if ( this . async === true )
if ( this . async === true )
control . unref ( ) ;
control . unref ( ) ;
if ( obj && obj . postSend )
if ( obj && obj . postSend )
obj . postSend ( handle ) ;
obj . postSend ( handle , options ) ;
if ( typeof callback === 'function' )
if ( typeof callback === 'function' )
callback ( null ) ;
callback ( null ) ;
} ;
} ;
@ -605,9 +624,9 @@ function setupChannel(target, channel) {
} else {
} else {
// Cleanup handle on error
// Cleanup handle on error
if ( obj && obj . postSend )
if ( obj && obj . postSend )
obj . postSend ( handle ) ;
obj . postSend ( handle , options ) ;
if ( ! swallowErrors ) {
if ( ! options . swallowErrors ) {
const ex = errnoException ( err , 'write' ) ;
const ex = errnoException ( err , 'write' ) ;
if ( typeof callback === 'function' ) {
if ( typeof callback === 'function' ) {
process . nextTick ( callback , ex ) ;
process . nextTick ( callback , ex ) ;