Skip to content

Commit

Permalink
Make private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Nov 5, 2018
1 parent ca1dbd4 commit 824c0cc
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions lib/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -26,18 +28,18 @@ 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;

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;
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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]');
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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);
}
}
Expand Down

0 comments on commit 824c0cc

Please sign in to comment.