File tree 5 files changed +54
-4
lines changed
5 files changed +54
-4
lines changed Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
3
3
const response = require ( '../../test-helpers/context' ) . response
4
+ const CustomStream = require ( '../../test-helpers/stream' )
4
5
const assert = require ( 'assert' )
5
6
const fs = require ( 'fs' )
6
7
const Stream = require ( 'stream' )
@@ -109,6 +110,12 @@ describe('res.body=', () => {
109
110
assert . strictEqual ( 'application/octet-stream' , res . header [ 'content-type' ] )
110
111
} )
111
112
113
+ it ( 'should support custom stream' , ( ) => {
114
+ const res = response ( )
115
+ res . body = new CustomStream . Readable ( )
116
+ assert . strictEqual ( 'application/octet-stream' , res . header [ 'content-type' ] )
117
+ } )
118
+
112
119
it ( 'should add error handler to the stream, but only once' , ( ) => {
113
120
const res = response ( )
114
121
const body = new Stream . PassThrough ( )
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ const Emitter = require('events')
16
16
const util = require ( 'util' )
17
17
const Stream = require ( 'stream' )
18
18
const http = require ( 'http' )
19
+ const isStream = require ( './is-stream.js' )
19
20
const only = require ( './only.js' )
20
21
const { HttpError } = require ( 'http-errors' )
21
22
@@ -303,10 +304,10 @@ function respond (ctx) {
303
304
304
305
if ( Buffer . isBuffer ( body ) ) return res . end ( body )
305
306
if ( typeof body === 'string' ) return res . end ( body )
306
- if ( body instanceof Stream ) return body . pipe ( res )
307
307
if ( body instanceof Blob ) return Stream . Readable . from ( body . stream ( ) ) . pipe ( res )
308
308
if ( body instanceof ReadableStream ) return Stream . Readable . from ( body ) . pipe ( res )
309
309
if ( body instanceof Response ) return Stream . Readable . from ( body ?. body ) . pipe ( res )
310
+ if ( isStream ( body ) ) return body . pipe ( res )
310
311
311
312
// body: json
312
313
body = JSON . stringify ( body )
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const Stream = require ( 'stream' )
4
+
5
+ module . exports = ( stream ) => {
6
+ return (
7
+ stream instanceof Stream ||
8
+ ( stream !== null &&
9
+ typeof stream === 'object' &&
10
+ ! ! stream . readable &&
11
+ typeof stream . pipe === 'function' &&
12
+ typeof stream . read === 'function' &&
13
+ typeof stream . readable === 'boolean' &&
14
+ typeof stream . readableObjectMode === 'boolean' &&
15
+ typeof stream . destroy === 'function' &&
16
+ typeof stream . destroyed === 'boolean' )
17
+ )
18
+ }
Original file line number Diff line number Diff line change @@ -14,10 +14,10 @@ const destroy = require('destroy')
14
14
const assert = require ( 'assert' )
15
15
const extname = require ( 'path' ) . extname
16
16
const vary = require ( 'vary' )
17
+ const isStream = require ( './is-stream.js' )
17
18
const only = require ( './only.js' )
18
19
const util = require ( 'util' )
19
20
const encodeUrl = require ( 'encodeurl' )
20
- const Stream = require ( 'stream' )
21
21
22
22
/**
23
23
* Prototype.
@@ -171,7 +171,7 @@ module.exports = {
171
171
}
172
172
173
173
// stream
174
- if ( val instanceof Stream ) {
174
+ if ( isStream ( val ) ) {
175
175
onFinish ( this . res , destroy . bind ( null , val ) )
176
176
if ( original !== val ) {
177
177
val . once ( 'error' , err => this . ctx . onerror ( err ) )
@@ -242,7 +242,7 @@ module.exports = {
242
242
}
243
243
244
244
const { body } = this
245
- if ( ! body || body instanceof Stream ) return undefined
245
+ if ( ! body || isStream ( body ) ) return undefined
246
246
if ( typeof body === 'string' ) return Buffer . byteLength ( body )
247
247
if ( Buffer . isBuffer ( body ) ) return body . length
248
248
return Buffer . byteLength ( JSON . stringify ( body ) )
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const { EventEmitter } = require ( 'events' )
4
+
5
+ class Readable extends EventEmitter {
6
+ pipe ( ) { }
7
+ read ( ) { }
8
+ destroy ( ) { }
9
+ get readable ( ) {
10
+ return true
11
+ }
12
+
13
+ get readableObjectMode ( ) {
14
+ return false
15
+ }
16
+
17
+ get destroyed ( ) {
18
+ return false
19
+ }
20
+ }
21
+
22
+ module . exports = {
23
+ Readable
24
+ }
You can’t perform that action at this time.
0 commit comments