koa 3.0.0-alpha.4 → 3.0.0
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 +5 -19
- package/package.json +4 -6
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,11 +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
|
-
const deprecate = require('depd')('koa')
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
23
|
* Prototype.
|
|
@@ -172,7 +171,7 @@ module.exports = {
|
|
|
172
171
|
}
|
|
173
172
|
|
|
174
173
|
// stream
|
|
175
|
-
if (val
|
|
174
|
+
if (isStream(val)) {
|
|
176
175
|
onFinish(this.res, destroy.bind(null, val))
|
|
177
176
|
if (original !== val) {
|
|
178
177
|
val.once('error', err => this.ctx.onerror(err))
|
|
@@ -240,7 +239,7 @@ module.exports = {
|
|
|
240
239
|
}
|
|
241
240
|
|
|
242
241
|
const { body } = this
|
|
243
|
-
if (!body || body
|
|
242
|
+
if (!body || isStream(body)) return undefined
|
|
244
243
|
if (typeof body === 'string') return Buffer.byteLength(body)
|
|
245
244
|
if (Buffer.isBuffer(body)) return body.length
|
|
246
245
|
return Buffer.byteLength(JSON.stringify(body))
|
|
@@ -273,29 +272,16 @@ module.exports = {
|
|
|
273
272
|
/**
|
|
274
273
|
* Perform a 302 redirect to `url`.
|
|
275
274
|
*
|
|
276
|
-
* The string "back" is special-cased
|
|
277
|
-
* to provide Referrer support, when Referrer
|
|
278
|
-
* is not present `alt` or "/" is used.
|
|
279
|
-
*
|
|
280
275
|
* Examples:
|
|
281
276
|
*
|
|
282
|
-
* this.redirect('back');
|
|
283
|
-
* this.redirect('back', '/index.html');
|
|
284
277
|
* this.redirect('/login');
|
|
285
278
|
* this.redirect('http://google.com');
|
|
286
279
|
*
|
|
287
280
|
* @param {String} url
|
|
288
|
-
* @param {String} [alt]
|
|
289
281
|
* @api public
|
|
290
282
|
*/
|
|
291
283
|
|
|
292
|
-
redirect (url
|
|
293
|
-
// location
|
|
294
|
-
if (url === 'back') {
|
|
295
|
-
deprecate('Special-cased string "back" through redirect will be removed in v3, ' +
|
|
296
|
-
'consider migrating usage to ctx.back() instead.')
|
|
297
|
-
url = this.ctx.get('Referrer') || alt || '/'
|
|
298
|
-
}
|
|
284
|
+
redirect (url) {
|
|
299
285
|
if (/^https?:\/\//i.test(url)) {
|
|
300
286
|
// formatting url again avoid security escapes
|
|
301
287
|
url = new URL(url).toString()
|
|
@@ -309,7 +295,7 @@ module.exports = {
|
|
|
309
295
|
if (this.ctx.accepts('html')) {
|
|
310
296
|
url = escape(url)
|
|
311
297
|
this.type = 'text/html; charset=utf-8'
|
|
312
|
-
this.body = `Redirecting to
|
|
298
|
+
this.body = `Redirecting to ${url}.`
|
|
313
299
|
return
|
|
314
300
|
}
|
|
315
301
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa",
|
|
3
|
-
"version": "3.0.0
|
|
4
|
-
"publishConfig": {
|
|
5
|
-
"tag": "experimental"
|
|
6
|
-
},
|
|
3
|
+
"version": "3.0.0",
|
|
7
4
|
"description": "Koa web app framework",
|
|
8
5
|
"main": "lib/application.js",
|
|
9
6
|
"exports": {
|
|
@@ -19,7 +16,7 @@
|
|
|
19
16
|
},
|
|
20
17
|
"scripts": {
|
|
21
18
|
"test": "node --test",
|
|
22
|
-
"test:coverage": "
|
|
19
|
+
"test:coverage": "c8 --reporter=lcov --reporter=text-summary node --test",
|
|
23
20
|
"lint": "standard",
|
|
24
21
|
"lint:pretty": "standard | snazzy",
|
|
25
22
|
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
|
|
@@ -55,10 +52,11 @@
|
|
|
55
52
|
"on-finished": "^2.3.0",
|
|
56
53
|
"parseurl": "^1.3.2",
|
|
57
54
|
"statuses": "^2.0.1",
|
|
58
|
-
"type-is": "^
|
|
55
|
+
"type-is": "^2.0.1",
|
|
59
56
|
"vary": "^1.1.2"
|
|
60
57
|
},
|
|
61
58
|
"devDependencies": {
|
|
59
|
+
"c8": "^10.1.3",
|
|
62
60
|
"gen-esm-wrapper": "^1.0.6",
|
|
63
61
|
"snazzy": "^9.0.0",
|
|
64
62
|
"standard": "^17.1.0",
|