Skip to content

Commit

Permalink
feat: now supports 'content-encoding:utf-8' (closes stream-utils#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingxinxin committed Oct 14, 2020
1 parent f35cb31 commit d7bbe76
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var zlib = require('zlib')

module.exports = inflate
Expand All @@ -11,19 +10,22 @@ function inflate(stream, options) {
options = options || {}

var encoding = options.encoding
|| (stream.headers && stream.headers['content-encoding'])
|| (stream.headers && stream.headers[ 'content-encoding' ])
|| 'identity'

encoding = encoding.toLowerCase()

switch (encoding) {
case 'gzip':
case 'deflate':
break
case 'identity':
return stream
default:
var err = new Error('Unsupported Content-Encoding: ' + encoding)
err.status = 415
throw err
case 'gzip':
case 'deflate':
break
case 'identity':
case 'utf-8':
return stream
default:
var err = new Error('Unsupported Content-Encoding: ' + encoding)
err.status = 415
throw err
}

// no not pass-through encoding
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "inflation",
"description": "Easily unzip an HTTP stream",
"version": "2.0.0",
"version": "2.1.0",
"author": "Jonathan Ong <[email protected]> (http://jongleberry.com)",
"license": "MIT",
"repository": "stream-utils/inflation",
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe('inflate(stream, options)', function () {
assertBuffer(inflation(stream), string, done)
})

it('should pass-through utf-8 streams', function (done) {
var stream = createStream(new Buffer('identity!', 'utf-8'))
var string = 'identity!'
stream.headers = {'content-encoding': 'utf-8'}
assertBuffer(inflation(stream), string, done)
})

it('should inflate gzip streams', function (done) {
var stream = createStream(new Buffer('1f8b080000000000000b4bcecf2d284a2d2e4e4d510400fb94f3640b000000', 'hex'))
var string = 'compressed!'
Expand Down

0 comments on commit d7bbe76

Please sign in to comment.