koa 3.0.0-alpha.4 → 3.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/application.js +2 -1
- package/lib/is-stream.js +20 -0
- package/lib/response.js +4 -4
- package/package.json +2 -2
package/lib/application.js
CHANGED
|
@@ -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
|
|
|
@@ -304,10 +305,10 @@ function respond (ctx) {
|
|
|
304
305
|
|
|
305
306
|
if (Buffer.isBuffer(body)) return res.end(body)
|
|
306
307
|
if (typeof body === 'string') return res.end(body)
|
|
307
|
-
if (body instanceof Stream) return body.pipe(res)
|
|
308
308
|
if (body instanceof Blob) return Stream.Readable.from(body.stream()).pipe(res)
|
|
309
309
|
if (body instanceof ReadableStream) return Stream.Readable.from(body).pipe(res)
|
|
310
310
|
if (body instanceof Response) return Stream.Readable.from(body?.body || '').pipe(res)
|
|
311
|
+
if (isStream(body)) return body.pipe(res)
|
|
311
312
|
|
|
312
313
|
// body: json
|
|
313
314
|
body = JSON.stringify(body)
|
package/lib/is-stream.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Stream = require('stream')
|
|
4
|
+
|
|
5
|
+
// TODO: use a third party library for this
|
|
6
|
+
|
|
7
|
+
module.exports = (stream) => {
|
|
8
|
+
return (
|
|
9
|
+
stream instanceof Stream ||
|
|
10
|
+
(stream !== null &&
|
|
11
|
+
typeof stream === 'object' &&
|
|
12
|
+
!!stream.readable &&
|
|
13
|
+
typeof stream.pipe === 'function' &&
|
|
14
|
+
typeof stream.read === 'function' &&
|
|
15
|
+
typeof stream.readable === 'boolean' &&
|
|
16
|
+
typeof stream.readableObjectMode === 'boolean' &&
|
|
17
|
+
typeof stream.destroy === 'function' &&
|
|
18
|
+
typeof stream.destroyed === 'boolean')
|
|
19
|
+
)
|
|
20
|
+
}
|
package/lib/response.js
CHANGED
|
@@ -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
|
const deprecate = require('depd')('koa')
|
|
22
22
|
|
|
23
23
|
/**
|
|
@@ -172,7 +172,7 @@ module.exports = {
|
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
// stream
|
|
175
|
-
if (val
|
|
175
|
+
if (isStream(val)) {
|
|
176
176
|
onFinish(this.res, destroy.bind(null, val))
|
|
177
177
|
if (original !== val) {
|
|
178
178
|
val.once('error', err => this.ctx.onerror(err))
|
|
@@ -240,7 +240,7 @@ module.exports = {
|
|
|
240
240
|
}
|
|
241
241
|
|
|
242
242
|
const { body } = this
|
|
243
|
-
if (!body || body
|
|
243
|
+
if (!body || isStream(body)) return undefined
|
|
244
244
|
if (typeof body === 'string') return Buffer.byteLength(body)
|
|
245
245
|
if (Buffer.isBuffer(body)) return body.length
|
|
246
246
|
return Buffer.byteLength(JSON.stringify(body))
|
|
@@ -309,7 +309,7 @@ module.exports = {
|
|
|
309
309
|
if (this.ctx.accepts('html')) {
|
|
310
310
|
url = escape(url)
|
|
311
311
|
this.type = 'text/html; charset=utf-8'
|
|
312
|
-
this.body = `Redirecting to
|
|
312
|
+
this.body = `Redirecting to ${url}.`
|
|
313
313
|
return
|
|
314
314
|
}
|
|
315
315
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.5",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"tag": "experimental"
|
|
6
6
|
},
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"on-finished": "^2.3.0",
|
|
56
56
|
"parseurl": "^1.3.2",
|
|
57
57
|
"statuses": "^2.0.1",
|
|
58
|
-
"type-is": "^
|
|
58
|
+
"type-is": "^2.0.1",
|
|
59
59
|
"vary": "^1.1.2"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|