Skip to content

Commit

Permalink
New setEncoding method
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Feb 4, 2018
1 parent d1343f9 commit c001907
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/promise-duplex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export declare class PromiseDuplex<TDuplex extends Duplex> extends PromiseReadab
read (size?: number): Promise<Buffer | undefined>
readAll (): Promise<Buffer | undefined>

setEncoding (encoding: string): this

write (chunk: string | Buffer, encoding?: string): Promise<number>
writeAll (content: string | Buffer, chunkSize?: number): Promise<number>

Expand Down
5 changes: 5 additions & 0 deletions lib/promise-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 .",
Expand Down
39 changes: 38 additions & 1 deletion test/promise-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c001907

Please sign in to comment.