@@ -21,6 +21,8 @@ const OPENFILE = Symbol('openfile')
21
21
const ONOPENFILE = Symbol ( 'onopenfile' )
22
22
const CLOSE = Symbol ( 'close' )
23
23
const MODE = Symbol ( 'mode' )
24
+ const AWAITDRAIN = Symbol ( 'awaitDrain' )
25
+ const ONDRAIN = Symbol ( 'ondrain' )
24
26
const warner = require ( './warn-mixin.js' )
25
27
const winchars = require ( './winchars.js' )
26
28
const stripAbsolutePath = require ( './strip-absolute-path.js' )
@@ -232,7 +234,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
232
234
this . pos = 0
233
235
this . remain = this . stat . size
234
236
this . length = this . buf . length
235
- this [ READ ] ( this . stat . size )
237
+ this [ READ ] ( )
236
238
}
237
239
238
240
[ READ ] ( ) {
@@ -284,13 +286,23 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
284
286
285
287
const writeBuf = this . offset === 0 && bytesRead === this . buf . length ?
286
288
this . buf : this . buf . slice ( this . offset , this . offset + bytesRead )
287
- this . remain -= bytesRead
288
- this . blockRemain -= bytesRead
289
- this . pos += bytesRead
290
- this . offset += bytesRead
289
+ this . remain -= writeBuf . length
290
+ this . blockRemain -= writeBuf . length
291
+ this . pos += writeBuf . length
292
+ this . offset += writeBuf . length
293
+
294
+ const flushed = this . write ( writeBuf )
295
+ if ( ! flushed )
296
+ this [ AWAITDRAIN ] ( ( ) => this [ ONDRAIN ] ( ) )
297
+ else
298
+ this [ ONDRAIN ] ( )
299
+ }
291
300
292
- this . write ( writeBuf )
301
+ [ AWAITDRAIN ] ( cb ) {
302
+ this . once ( 'drain' , cb )
303
+ }
293
304
305
+ [ ONDRAIN ] ( ) {
294
306
if ( ! this . remain ) {
295
307
if ( this . blockRemain )
296
308
this . write ( Buffer . alloc ( this . blockRemain ) )
@@ -339,6 +351,10 @@ class WriteEntrySync extends WriteEntry {
339
351
}
340
352
}
341
353
354
+ [ AWAITDRAIN ] ( cb ) {
355
+ cb ( )
356
+ }
357
+
342
358
[ CLOSE ] ( cb ) {
343
359
fs . closeSync ( this . fd )
344
360
cb ( )
0 commit comments