koa 3.0.0-alpha.2 → 3.0.0-alpha.4

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.
@@ -55,7 +55,7 @@ module.exports = class Application extends Emitter {
55
55
  * @param {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For
56
56
  * @param {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity)
57
57
  * @param {function} [options.compose] Function to handle middleware composition
58
- * @param {boolean} [options.asyncLocalStorage] Max IPs read from proxy IP header, default to 0 (means infinity)
58
+ * @param {boolean} [options.asyncLocalStorage] Enable AsyncLocalStorage, default to false
59
59
  *
60
60
  */
61
61
 
@@ -258,9 +258,10 @@ function respond (ctx) {
258
258
  // allow bypassing koa
259
259
  if (ctx.respond === false) return
260
260
 
261
- if (!ctx.writable) return
262
-
263
261
  const res = ctx.res
262
+
263
+ if (!ctx.writable) return res.end()
264
+
264
265
  let body = ctx.body
265
266
  const code = ctx.status
266
267
 
package/lib/context.js CHANGED
@@ -150,9 +150,6 @@ const proto = module.exports = {
150
150
 
151
151
  let statusCode = err.status || err.statusCode
152
152
 
153
- // ENOENT support
154
- if (err.code === 'ENOENT') statusCode = 404
155
-
156
153
  // default to 500
157
154
  if (typeof statusCode !== 'number' || !statuses.message[statusCode]) statusCode = 500
158
155
 
package/lib/request.js CHANGED
@@ -89,14 +89,14 @@ module.exports = {
89
89
  },
90
90
 
91
91
  /**
92
- * Get origin of URL.
92
+ * Get the origin header.
93
93
  *
94
94
  * @return {String}
95
95
  * @api public
96
96
  */
97
97
 
98
98
  get origin () {
99
- return `${this.protocol}://${this.host}`
99
+ return this.req.headers.origin || null
100
100
  },
101
101
 
102
102
  /**
@@ -109,7 +109,7 @@ module.exports = {
109
109
  get href () {
110
110
  // support: `GET http://example.com/foo`
111
111
  if (/^https?:\/\//i.test(this.originalUrl)) return this.originalUrl
112
- return this.origin + this.originalUrl
112
+ return this.protocol + '://' + this.host + this.originalUrl
113
113
  },
114
114
 
115
115
  /**
@@ -256,7 +256,7 @@ module.exports = {
256
256
  if (!host) host = this.get('Host')
257
257
  }
258
258
  if (!host) return ''
259
- return host.split(/\s*,\s*/, 1)[0]
259
+ return splitCommaSeparatedValues(host, 1)[0]
260
260
  },
261
261
 
262
262
  /**
@@ -288,7 +288,7 @@ module.exports = {
288
288
  if (!this.memoizedURL) {
289
289
  const originalUrl = this.originalUrl || '' // avoid undefined in template string
290
290
  try {
291
- this.memoizedURL = new URL(`${this.origin}${originalUrl}`)
291
+ this.memoizedURL = new URL(`${this.protocol}://${this.host}${originalUrl}`)
292
292
  } catch (err) {
293
293
  this.memoizedURL = Object.create(null)
294
294
  }
@@ -401,7 +401,7 @@ module.exports = {
401
401
  if (this.socket.encrypted) return 'https'
402
402
  if (!this.app.proxy) return 'http'
403
403
  const proto = this.get('X-Forwarded-Proto')
404
- return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http'
404
+ return proto ? splitCommaSeparatedValues(proto, 1)[0] : 'http'
405
405
  },
406
406
 
407
407
  /**
@@ -433,7 +433,7 @@ module.exports = {
433
433
  const proxy = this.app.proxy
434
434
  const val = this.get(this.app.proxyIpHeader)
435
435
  let ips = proxy && val
436
- ? val.split(/\s*,\s*/)
436
+ ? splitCommaSeparatedValues(val)
437
437
  : []
438
438
  if (this.app.maxIpsCount > 0) {
439
439
  ips = ips.slice(-this.app.maxIpsCount)
@@ -723,3 +723,15 @@ module.exports = {
723
723
  if (util.inspect.custom) {
724
724
  module.exports[util.inspect.custom] = module.exports.inspect
725
725
  }
726
+
727
+ /**
728
+ * Split a comma-separated value string into an array of values, with an optional limit.
729
+ * All the values are trimmed of whitespace.
730
+ *
731
+ * @param {string} value - The comma-separated value string to split.
732
+ * @param {number} [limit] - The maximum number of values to return.
733
+ * @returns {string[]} An array of values from the comma-separated string.
734
+ */
735
+ function splitCommaSeparatedValues (value, limit) {
736
+ return value.split(',', limit).map(v => v.trim())
737
+ }
package/lib/response.js CHANGED
@@ -211,7 +211,7 @@ module.exports = {
211
211
 
212
212
  // json
213
213
  this.remove('Content-Length')
214
- this.type = 'json'
214
+ if (!this.type || !/\bjson\b/i.test(this.type)) this.type = 'json'
215
215
  },
216
216
 
217
217
  /**
@@ -513,22 +513,18 @@ module.exports = {
513
513
  * this.set('Accept', 'application/json');
514
514
  * this.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
515
515
  *
516
- * @param {String|Object|Array} field
517
- * @param {*} val
516
+ * @param {String|{ [k: string]: any }} field
517
+ * @param {any} [val]
518
518
  * @api public
519
519
  */
520
520
 
521
521
  set (field, val) {
522
- if (this.headerSent) return
522
+ if (this.headerSent || !field) return
523
523
 
524
- if (arguments.length === 2) {
525
- if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v))
526
- else if (typeof val !== 'string') val = String(val)
524
+ if (typeof field === 'string') {
527
525
  this.res.setHeader(field, val)
528
526
  } else {
529
- for (const key in field) {
530
- this.set(key, field[key])
531
- }
527
+ Object.keys(field).forEach(header => this.res.setHeader(header, field[header]))
532
528
  }
533
529
  },
534
530
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.4",
4
4
  "publishConfig": {
5
5
  "tag": "experimental"
6
6
  },
@@ -70,5 +70,6 @@
70
70
  "files": [
71
71
  "dist",
72
72
  "lib"
73
- ]
73
+ ],
74
+ "homepage": "https://koajs.com"
74
75
  }