diff --git a/CHANGELOG.md b/CHANGELOG.md index e03a136..5ac111b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## v3.0.0 2018-02-04 - * New `destroy` method. + * New `setEncoding` and `destroy` methods. * Support for `import PromiseDuplex from 'promise-duplex'` syntax. ## v2.0.4 2018-01-18 diff --git a/README.md b/README.md index df09633..6d3ffd8 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,16 @@ Check [`PromiseReadable.readAll`](https://www.npmjs.com/package/promise-readable#readall) for details. +#### setEncoding + +```js +promiseDuplex = await promiseDuplex.setEncoding(encoding) +``` + +Check +[`PromiseReadable.setEncoding`](https://www.npmjs.com/package/promise-readable#setencoding) +for details. + #### write ```js diff --git a/lib/promise-duplex.d.ts b/lib/promise-duplex.d.ts index 126289c..de20ce1 100644 --- a/lib/promise-duplex.d.ts +++ b/lib/promise-duplex.d.ts @@ -14,6 +14,8 @@ export declare class PromiseDuplex extends PromiseReadab read (size?: number): Promise readAll (): Promise + setEncoding (encoding: string): this + write (chunk: string | Buffer, encoding?: string): Promise writeAll (content: string | Buffer, chunkSize?: number): Promise diff --git a/lib/promise-duplex.js b/lib/promise-duplex.js index 4b5f954..026d720 100644 --- a/lib/promise-duplex.js +++ b/lib/promise-duplex.js @@ -20,6 +20,11 @@ class PromiseDuplex extends PromiseReadable /* and PromiseWritable */ { return this.readable.readAll() } + setEncoding (encoding) { + this.readable.setEncoding(encoding) + return this + } + // PromiseWritable write (chunk, encoding) { return this.writable.write(chunk, encoding) diff --git a/package.json b/package.json index c8437e2..2892499 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ }, "devDependencies": { "@types/node": "^9.4.0", - "@types/ws": "^4.0.0", "chai": "^4.1.2", "chai-as-promised": "^7.1.1", "simple-websocket": "^6.0.0", @@ -39,8 +38,7 @@ "tslint": "^5.9.1", "tslint-config-standard": "^7.0.0", "typescript": "^2.7.1", - "websocket-stream": "^5.1.1", - "ws": "^4.0.0" + "websocket-stream": "^5.1.1" }, "scripts": { "pretest": "standard --verbose | snazzy && tsc --noEmit --pretty && tslint -t stylish -p .", diff --git a/test/promise-duplex.js b/test/promise-duplex.js index 388fc65..f15b01f 100644 --- a/test/promise-duplex.js +++ b/test/promise-duplex.js @@ -49,7 +49,7 @@ class MockStream extends EventEmitter { } const chunk = this._readBuffer.slice(0, size) this._readBuffer = this._readBuffer.slice(size) - return chunk + return this.encoding ? chunk.toString(this.encoding) : chunk } write (chunk) { this._writeBuffer = Buffer.concat([this._writeBuffer, chunk]) @@ -58,6 +58,9 @@ class MockStream extends EventEmitter { end () {} cork () {} uncork () {} + setEncoding (encoding) { + this.encoding = encoding + } _append (chunk) { this._readBuffer = Buffer.concat([this._readBuffer, chunk]) } @@ -97,6 +100,40 @@ Feature('Test promise-duplex module', () => { }) }) + Scenario('Read chunks from stream with encoding', () => { + let promise + let promiseDuplex + let stream + + Given('Duplex object', () => { + stream = new MockStream() + }) + + And('PromiseDuplex object', () => { + promiseDuplex = new PromiseDuplex(stream) + }) + + When('stream contains some data', () => { + stream._append(Buffer.from('chunk1')) + }) + + And('I set encoding', () => { + promise = promiseDuplex.setEncoding('utf8') + }) + + And('I call read method', () => { + promise = promiseDuplex.read() + }) + + Then('promise returns chunk as string', () => { + return promise.should.eventually.deep.equal('chunk1') + }) + + And('stream can be destroyed', () => { + promiseDuplex.destroy() + }) + }) + Scenario('Read all from stream', () => { let promise let promiseDuplex