From 7e378446655de4754b412974503cc619a25774af Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 3 Aug 2022 11:39:56 +0800 Subject: [PATCH] fix(Server): correct Content-Range implement --- packages/server/js/response.js | 2 +- test/cases/http-pipe-file-stream-range.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/server/js/response.js b/packages/server/js/response.js index d790922..fca14ce 100644 --- a/packages/server/js/response.js +++ b/packages/server/js/response.js @@ -269,9 +269,9 @@ class Response extends Writable { if (Array.isArray(ranges) && ranges.length === 1 && ranges.type === 'bytes') { const [{ start, end }] = ranges if (this._totalSize && end <= this._totalSize) { - this._totalSize = end - start this.status(206) .setHeader('Content-Range', `bytes ${start}-${end}/${this._totalSize}`) + this._totalSize = end - start return stream.pipe(rangeStream(ranges)).pipe(this) } } diff --git a/test/cases/http-pipe-file-stream-range.js b/test/cases/http-pipe-file-stream-range.js index da6cf4a..5eef3ab 100644 --- a/test/cases/http-pipe-file-stream-range.js +++ b/test/cases/http-pipe-file-stream-range.js @@ -13,7 +13,10 @@ module.exports = async function ({ HTTP_PORT }) { throw new Error('Unknown Content-Type') } if (Number(res.headers['content-length']) !== 4) { - throw new Error(`Invalid Content-Length ${res.headers['content-length']}`) + throw new Error(`Invalid Content-Length: ${res.headers['content-length']}`) + } + if (!['bytes 0-4/10', 'bytes 0-4/11'].includes(res.headers['content-range'])) { + throw new Error(`Invalid Content-Range: ${res.headers['content-range']}`) } if (res.data !== 'TEST') { throw new Error('Response data mismatch')