diff --git a/lib/writable.js b/lib/writable.js index b9bdde8..a8b1bc7 100644 --- a/lib/writable.js +++ b/lib/writable.js @@ -4,8 +4,10 @@ const EventEmitter = require('events'); const DEFAULT_HIGH_WATERMARK = 16 * 1024; // 16Kb -const writeChunkFuncName = Symbol('writeChunk'); const errorFuncName = Symbol('error'); +const doWriteSymbol = Symbol('doWrite'); +const validChunkSymbol = Symbol('validChunk'); +const writeChunkSymbol = Symbol('writeChunk'); class WritableBuffer { constructor(highWaterMark) { @@ -16,7 +18,7 @@ class WritableBuffer { this.buffer = Buffer.alloc(highWaterMark); } - [writeChunkFuncName](chunk) { + [writeChunkSymbol](chunk) { chunk.copy(this.buffer, this.offset); this.writableLength += chunk.length; this.offset += chunk.length; @@ -26,10 +28,10 @@ class WritableBuffer { const remainingSize = this.highWaterMark - this.offset; if (chunk.length <= remainingSize) { - this[writeChunkFuncName](chunk, encoding); + this[writeChunkSymbol](chunk, encoding); } else { const currentBufferChunk = chunk.slice(0, this.remainingSize); - this[writeChunkFuncName](currentBufferChunk); + this[writeChunkSymbol](currentBufferChunk); this.queue.push(this.buffer); this.buffer = Buffer.alloc(this.highWaterMark); this.offset = 0; @@ -37,7 +39,7 @@ class WritableBuffer { const nextBufferChunk = chunk.slice(remainingSize); if (nextBufferChunk.length < this.buffer.length) { - this[writeChunkFuncName](nextBufferChunk, encoding); + this[writeChunkSymbol](nextBufferChunk, encoding); } else { this.queue.push(nextBufferChunk); this.writableLength += nextBufferChunk.length; @@ -91,9 +93,9 @@ class Writable extends EventEmitter { uncork() { if (this.corked) this.corked--; - if (!this.corked && this.writableLength) { + if (this.corked === 0 && this.writableLength) { this.writing = true; - this.doWrite(this.buffer.getBuffer()); + this[doWriteSymbol](this.buffer.getBuffer()); } } @@ -113,7 +115,7 @@ class Writable extends EventEmitter { this[errorFuncName](err); } - validChunk(chunk, cb) { + [validChunkSymbol](chunk, cb) { let error; if (chunk === null) { error = new Error('[ERR_STREAM_NULL_VALUES]'); @@ -152,18 +154,18 @@ class Writable extends EventEmitter { if (typeof chunk === 'string') chunk = Buffer.from(chunk, encoding); - if (!this.validChunk(chunk, cb)) return; + if (!this[validChunkSymbol](chunk, cb)) return; if (cb) this.callbacks.push(cb); if (this.corked || this.writing) { this.buffer.write(chunk, encoding); } else { this.writing = true; - this.doWrite(chunk); + this[doWriteSymbol](chunk); } } - doWrite(data) { + [doWriteSymbol](data) { const callbacks = this.callbacks; this.callbacks = []; @@ -177,14 +179,18 @@ class Writable extends EventEmitter { } return; } - this.doWrite(this.buffer.getBuffer()); + this[doWriteSymbol]t (this.buffer.getBuffer()); }; - if (this._writev) { - if (Array.isArray(data) && data.length > 1) this._writev(data, cb); + if (this._writev && this._write) { + if (Array.isArray(data)) this._writev(data, cb); else this._write(data, cb); + } else if (this._writev) { + if (Array.isArray(data)) this._writev(data, cb); + else this._writev([data], cb); + } else if (Array.isArray(data)) { + this._write(Buffer.concat(data), cb); } else { - if (Array.isArray(data)) data = Buffer.concat(data); this._write(data, cb); } }