|
| 1 | +'use strict'; |
| 2 | +const { Readable, Writable } = require('stream'); |
| 3 | +const { owner_symbol } = require('internal/async_hooks').symbols; |
| 4 | + |
| 5 | +const kFastpipeSupported = Symbol('kFastpipeSupported'); |
| 6 | +const kFastpipeWritableBailoutMethods = {}; |
| 7 | +const kFastpipeWritableBailoutMethodsOriginals = {}; |
| 8 | +const kFastpipeReadableBailoutMethods = {}; |
| 9 | +const kFastpipeReadableBailoutMethodsOriginals = {}; |
| 10 | + |
| 11 | +for (const name of ['cork', 'uncork', 'write', 'destroy']) { |
| 12 | + const original = Writable.prototype[name]; |
| 13 | + kFastpipeWritableBailoutMethodsOriginals[name] = original; |
| 14 | + kFastpipeWritableBailoutMethods[name] = function(...args) { |
| 15 | + bailoutFromWritable(this); |
| 16 | + return original.call(this, ...args); |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +for (const name of [ |
| 21 | + 'pause', 'resume', 'setEncoding', 'unshift', 'push', 'destroy', |
| 22 | + 'pipe', 'unpipe' |
| 23 | +]) { |
| 24 | + const original = Readable.prototype[name]; |
| 25 | + kFastpipeReadableBailoutMethodsOriginals[name] = original; |
| 26 | + kFastpipeReadableBailoutMethods[name] = function(...args) { |
| 27 | + bailoutFromReadable(this); |
| 28 | + return original.call(this, ...args); |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +const OriginalPipe = Readable.prototype.pipe; |
| 33 | + |
| 34 | +function installFastpipe(StreamClass) { |
| 35 | + StreamClass.prototype[kFastpipeSupported] = true; |
| 36 | + StreamClass.prototype.pipe = function(dest, opts) { |
| 37 | + // If one of the streams is already part of a pipe, we first need to unpipe |
| 38 | + // those streams. This will call the original `.pipe()` function, which |
| 39 | + // adds `data` listeners, which prevents a new fastpipe from being formed |
| 40 | + // later here. |
| 41 | + if (dest._handle && dest._handle.pipeSource) |
| 42 | + bailoutFromWritable(dest); |
| 43 | + |
| 44 | + if (!dest[kFastpipeSupported] || |
| 45 | + this.listenerCount('data') > 0 || |
| 46 | + this.listenerCount('readable') > 0 || |
| 47 | + dest.listenerCount('drain') > 0 || |
| 48 | + this._readableState.decoder || |
| 49 | + this._readableState.objectMode || |
| 50 | + (dest._writableState && dest._writableState.objectMode)) { |
| 51 | + return OriginalPipe.call(this, dest, opts); |
| 52 | + } |
| 53 | + |
| 54 | + if (this.connecting) { |
| 55 | + OriginalPipe.call(this, dest, opts); |
| 56 | + this.once('connect', () => { |
| 57 | + this.unpipe(dest); |
| 58 | + this.pipe(dest, opts); |
| 59 | + }); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + if (this._readableState.length > 0) { |
| 64 | + const data = this.read(); |
| 65 | + dest.write(data); |
| 66 | + } |
| 67 | + |
| 68 | + if (dest._writableState && |
| 69 | + (dest._writableState.corked || dest._writableState.writing)) { |
| 70 | + OriginalPipe.call(this, dest, opts); |
| 71 | + dest.once('drain', () => { |
| 72 | + this.unpipe(dest); |
| 73 | + this.pipe(dest, opts); |
| 74 | + }); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + Object.assign(dest, kFastpipeWritableBailoutMethods); |
| 79 | + dest.on('newListener', writableNewListener); |
| 80 | + |
| 81 | + Object.assign(this, kFastpipeReadableBailoutMethods); |
| 82 | + this.on('newListener', readableNewListener); |
| 83 | + |
| 84 | + const { internalBinding } = require('internal/bootstrap/loaders'); |
| 85 | + const { StreamPipe } = internalBinding('stream_pipe'); |
| 86 | + const pipe = new StreamPipe(this._handle._externalStream, |
| 87 | + dest._handle._externalStream); |
| 88 | + pipe.onunpipe = onunpipe; |
| 89 | + pipe.start(); |
| 90 | + return this; |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +function onunpipe() { |
| 95 | + const self = this.source[owner_symbol]; |
| 96 | + bailoutFromReadable(self); |
| 97 | +} |
| 98 | + |
| 99 | +function writableNewListener(name) { |
| 100 | + if (name === 'drain') |
| 101 | + bailoutFromWritable(this); |
| 102 | +} |
| 103 | + |
| 104 | +function readableNewListener(name) { |
| 105 | + if (name === 'data' || name === 'readable') |
| 106 | + bailoutFromReadable(this); |
| 107 | +} |
| 108 | + |
| 109 | +function bailoutFromReadable(self) { |
| 110 | + if (!self._handle) |
| 111 | + return; |
| 112 | + const pipe = self._handle.pipeTarget; |
| 113 | + const dest = pipe.sink[owner_symbol]; |
| 114 | + self.removeListener('newListener', readableNewListener); |
| 115 | + Object.assign(self, kFastpipeReadableBailoutMethodsOriginals); |
| 116 | + dest.removeListener('newListener', writableNewListener); |
| 117 | + Object.assign(dest, kFastpipeWritableBailoutMethodsOriginals); |
| 118 | + pipe.unpipe(); |
| 119 | + pipe.onunpipe = function() {}; |
| 120 | + if (pipe.pendingWrites() > 0) { |
| 121 | + pipe.oncomplete = function() { |
| 122 | + OriginalPipe.call(self, dest); |
| 123 | + }; |
| 124 | + } else { |
| 125 | + OriginalPipe.call(self, dest); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function bailoutFromWritable(dest) { |
| 130 | + bailoutFromReadable(dest._handle.pipeSource.source[owner_symbol]); |
| 131 | +} |
| 132 | + |
| 133 | +module.exports = { |
| 134 | + installFastpipe |
| 135 | +}; |
0 commit comments