Skip to content

Commit 3ea0521

Browse files
authored
Adds support for readable-stream@3. (#26)
* Adds support for readable-stream@3. It also adds support for faux streams and relies on duck typing. Removes support for Node 4, 5, 7 and 9. Removes support for safe-buffer as it's not needed anymore. * Updated tests
1 parent 1a5e656 commit 3ea0521

File tree

5 files changed

+20
-31
lines changed

5 files changed

+20
-31
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: node_js
22

33
node_js:
4-
- "9"
54
- "8"
65
- "6"
7-
- "4"
86
- "10"
97

108
notifications:

lib/request.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict'
22

3-
const Buffer = require('safe-buffer').Buffer
4-
const Stream = require('stream')
3+
const { Readable } = require('readable-stream')
54
const Url = require('url')
65
const util = require('util')
76

87
function Request (options) {
9-
Stream.Readable.call(this)
8+
Readable.call(this)
109
// options: method, url, payload, headers, remoteAddress
1110
var url = options.url
1211
if (typeof url === 'object') {
@@ -46,13 +45,13 @@ function Request (options) {
4645
}
4746

4847
var payload = options.payload || null
49-
if (payload && typeof payload !== 'string' && !(payload instanceof Stream) && !Buffer.isBuffer(payload)) {
48+
if (payload && typeof payload !== 'string' && !(typeof payload.resume === 'function') && !Buffer.isBuffer(payload)) {
5049
payload = JSON.stringify(payload)
5150
this.headers['content-type'] = this.headers['content-type'] || 'application/json'
5251
}
5352

5453
// Set the content-length for the corresponding payload if none set
55-
if (payload && !(payload instanceof Stream) && !this.headers.hasOwnProperty('content-length')) {
54+
if (payload && !(typeof payload.resume === 'function') && !this.headers.hasOwnProperty('content-length')) {
5655
this.headers['content-length'] = (Buffer.isBuffer(payload) ? payload.length : Buffer.byteLength(payload)).toString()
5756
}
5857

@@ -66,26 +65,27 @@ function Request (options) {
6665
return this
6766
}
6867

69-
util.inherits(Request, Stream.Readable)
68+
util.inherits(Request, Readable)
7069

7170
Request.prototype.prepare = function (next) {
72-
if (this._lightMyRequest.payload instanceof Stream === false) {
71+
const payload = this._lightMyRequest.payload
72+
if (!payload || typeof payload.resume !== 'function') { // does not quack like a stream
7373
return next()
7474
}
7575

7676
const chunks = []
7777

78-
this._lightMyRequest.payload.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
78+
payload.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
7979

80-
this._lightMyRequest.payload.on('end', () => {
80+
payload.on('end', () => {
8181
const payload = Buffer.concat(chunks)
8282
this.headers['content-length'] = this.headers['content-length'] || ('' + payload.length)
8383
this._lightMyRequest.payload = payload
8484
return next()
8585
})
8686

8787
// Force to resume the stream. Needed for Stream 1
88-
this._lightMyRequest.payload.resume()
88+
payload.resume()
8989
}
9090

9191
Request.prototype._read = function (size) {

lib/response.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict'
22

3-
const Buffer = require('safe-buffer').Buffer
43
const http = require('http')
5-
const stream = require('readable-stream')
4+
const { Writable } = require('readable-stream')
65
const util = require('util')
76

87
function Response (req, onEnd, reject) {
@@ -110,7 +109,7 @@ function generatePayload (response) {
110109

111110
// Throws away all written data to prevent response from buffering payload
112111
function getNullSocket () {
113-
return new stream.Writable({
112+
return new Writable({
114113
write (chunk, encoding, callback) {
115114
setImmediate(callback)
116115
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"main": "index.js",
66
"dependencies": {
77
"ajv": "^6.0.0",
8-
"readable-stream": "^2.3.6",
9-
"safe-buffer": "^5.1.2"
8+
"readable-stream": "^3.0.0"
109
},
1110
"devDependencies": {
1211
"form-data": "^2.3.2",

test/test.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
const t = require('tap')
44
const test = t.test
5-
const util = require('util')
6-
const Stream = require('stream')
5+
const { Readable } = require('readable-stream')
76
const fs = require('fs')
87
const zlib = require('zlib')
98
const inject = require('../index')
@@ -354,7 +353,7 @@ test('pipes response with old stream', (t) => {
354353
res.writeHead(200)
355354
const stream = getTestStream()
356355
stream.pause()
357-
const stream2 = new Stream.Readable().wrap(stream)
356+
const stream2 = new Readable().wrap(stream)
358357
stream.resume()
359358

360359
res.on('finish', () => {
@@ -827,20 +826,14 @@ test('form-data should be handled correctly', (t) => {
827826
})
828827

829828
function getTestStream (encoding) {
830-
const Read = function () {
831-
Stream.Readable.call(this)
832-
}
833-
834-
util.inherits(Read, Stream.Readable)
835-
836829
const word = 'hi'
837830
let i = 0
838831

839-
Read.prototype._read = function (size) {
840-
this.push(word[i] ? word[i++] : null)
841-
}
842-
843-
const stream = new Read()
832+
const stream = new Readable({
833+
read (n) {
834+
this.push(word[i] ? word[i++] : null)
835+
}
836+
})
844837

845838
if (encoding) {
846839
stream.setEncoding(encoding)

0 commit comments

Comments
 (0)