@@ -63,7 +63,9 @@ const {
6363 writeGeneric,
6464 onStreamRead,
6565 kAfterAsyncWrite,
66- kUpdateTimer
66+ kUpdateTimer,
67+ kBuffer,
68+ kBufferCb
6769} = require ( 'internal/stream_base_commons' ) ;
6870const {
6971 codes : {
@@ -101,18 +103,20 @@ function getFlags(ipv6Only) {
101103 return ipv6Only === true ? TCPConstants . UV_TCP_IPV6ONLY : 0 ;
102104}
103105
104- function createHandle ( fd , is_server ) {
106+ function createHandle ( fd , is_server , buf ) {
105107 validateInt32 ( fd , 'fd' , 0 ) ;
106108 const type = TTYWrap . guessHandleType ( fd ) ;
107109 if ( type === 'PIPE' ) {
108110 return new Pipe (
109- is_server ? PipeConstants . SERVER : PipeConstants . SOCKET
111+ is_server ? PipeConstants . SERVER : PipeConstants . SOCKET ,
112+ buf
110113 ) ;
111114 }
112115
113116 if ( type === 'TCP' ) {
114117 return new TCP (
115- is_server ? TCPConstants . SERVER : TCPConstants . SOCKET
118+ is_server ? TCPConstants . SERVER : TCPConstants . SOCKET ,
119+ buf
116120 ) ;
117121 }
118122
@@ -241,6 +245,8 @@ function Socket(options) {
241245 this . _host = null ;
242246 this [ kLastWriteQueueSize ] = 0 ;
243247 this [ kTimeout ] = null ;
248+ this [ kBuffer ] = null ;
249+ this [ kBufferCb ] = null ;
244250
245251 if ( typeof options === 'number' )
246252 options = { fd : options } ; // Legacy interface.
@@ -265,40 +271,50 @@ function Socket(options) {
265271 if ( options . handle ) {
266272 this . _handle = options . handle ; // private
267273 this [ async_id_symbol ] = getNewAsyncId ( this . _handle ) ;
268- } else if ( options . fd !== undefined ) {
269- const { fd } = options ;
270- let err ;
271-
272- // createHandle will throw ERR_INVALID_FD_TYPE if `fd` is not
273- // a valid `PIPE` or `TCP` descriptor
274- this . _handle = createHandle ( fd , false ) ;
275-
276- err = this . _handle . open ( fd ) ;
274+ } else {
275+ const onread = options . onread ;
276+ if ( onread !== null && typeof onread === 'object' &&
277+ Buffer . isBuffer ( onread . buffer ) &&
278+ typeof onread . callback === 'function' ) {
279+ this [ kBuffer ] = onread . buffer ;
280+ this [ kBufferCb ] = onread . callback ;
281+ }
282+ if ( options . fd !== undefined ) {
283+ const { fd } = options ;
284+ let err ;
277285
278- // While difficult to fabricate, in some architectures
279- // `open` may return an error code for valid file descriptors
280- // which cannot be opened. This is difficult to test as most
281- // un-openable fds will throw on `createHandle`
282- if ( err )
283- throw errnoException ( err , 'open' ) ;
286+ // createHandle will throw ERR_INVALID_FD_TYPE if `fd` is not
287+ // a valid `PIPE` or `TCP` descriptor
288+ this . _handle = createHandle ( fd , false ) ;
284289
285- this [ async_id_symbol ] = this . _handle . getAsyncId ( ) ;
290+ err = this . _handle . open ( fd ) ;
286291
287- if ( ( fd === 1 || fd === 2 ) &&
288- ( this . _handle instanceof Pipe ) &&
289- process . platform === 'win32' ) {
290- // Make stdout and stderr blocking on Windows
291- err = this . _handle . setBlocking ( true ) ;
292+ // While difficult to fabricate, in some architectures
293+ // `open` may return an error code for valid file descriptors
294+ // which cannot be opened. This is difficult to test as most
295+ // un-openable fds will throw on `createHandle`
292296 if ( err )
293- throw errnoException ( err , 'setBlocking' ) ;
294-
295- this . _writev = null ;
296- this . _write = makeSyncWrite ( fd ) ;
297- // makeSyncWrite adjusts this value like the original handle would, so
298- // we need to let it do that by turning it into a writable, own property.
299- Object . defineProperty ( this . _handle , 'bytesWritten' , {
300- value : 0 , writable : true
301- } ) ;
297+ throw errnoException ( err , 'open' ) ;
298+
299+ this [ async_id_symbol ] = this . _handle . getAsyncId ( ) ;
300+
301+ if ( ( fd === 1 || fd === 2 ) &&
302+ ( this . _handle instanceof Pipe ) &&
303+ process . platform === 'win32' ) {
304+ // Make stdout and stderr blocking on Windows
305+ err = this . _handle . setBlocking ( true ) ;
306+ if ( err )
307+ throw errnoException ( err , 'setBlocking' ) ;
308+
309+ this . _writev = null ;
310+ this . _write = makeSyncWrite ( fd ) ;
311+ // makeSyncWrite adjusts this value like the original handle would, so
312+ // we need to let it do that by turning it into a writable, own
313+ // property.
314+ Object . defineProperty ( this . _handle , 'bytesWritten' , {
315+ value : 0 , writable : true
316+ } ) ;
317+ }
302318 }
303319 }
304320
@@ -888,8 +904,8 @@ Socket.prototype.connect = function(...args) {
888904
889905 if ( ! this . _handle ) {
890906 this . _handle = pipe ?
891- new Pipe ( PipeConstants . SOCKET ) :
892- new TCP ( TCPConstants . SOCKET ) ;
907+ new Pipe ( PipeConstants . SOCKET , this [ kBuffer ] ) :
908+ new TCP ( TCPConstants . SOCKET , this [ kBuffer ] ) ;
893909 initSocketHandle ( this ) ;
894910 }
895911
0 commit comments