Skip to content

Commit f7375bc

Browse files
committed
feat: zstandard support
1 parent 266e69e commit f7375bc

5 files changed

Lines changed: 107 additions & 5 deletions

File tree

lib/read.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ var { getCharset } = require('./utils')
2525

2626
module.exports = read
2727

28+
/**
29+
* @const
30+
* whether current node version has zstandard support
31+
*/
32+
const hasZstandardSupport = 'createZstdDecompress' in zlib
33+
2834
/**
2935
* Read a request into a buffer and parse.
3036
*
@@ -222,12 +228,16 @@ function createDecompressionStream (encoding, debug) {
222228
case 'br':
223229
debug('brotli decompress body')
224230
return zlib.createBrotliDecompress()
225-
default:
226-
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
227-
encoding: encoding,
228-
type: 'encoding.unsupported'
229-
})
231+
case 'zstd':
232+
if (hasZstandardSupport) {
233+
debug('zstd decompress body')
234+
return zlib.createZstdDecompress()
235+
}
230236
}
237+
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
238+
encoding: encoding,
239+
type: 'encoding.unsupported'
240+
})
231241
}
232242

233243
/**

test/json.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
var assert = require('node:assert')
44
var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage
55
var http = require('node:http')
6+
const zlib = require('node:zlib')
67
var request = require('supertest')
78

89
var bodyParser = require('..')
910

11+
const hasZstandardSupport = 'createZstdDecompress' in zlib
12+
const zstandardit = hasZstandardSupport ? it : it.skip
13+
const nozstandardit = !hasZstandardSupport ? it : it.skip
14+
1015
describe('bodyParser.json()', function () {
1116
it('should parse JSON', function (done) {
1217
request(createServer())
@@ -702,6 +707,24 @@ describe('bodyParser.json()', function () {
702707
test.expect(200, '{"name":"论"}', done)
703708
})
704709

710+
zstandardit('should support zstandard encoding', function (done) {
711+
const server = createServer({ experimentalZstd: true, limit: '1kb' })
712+
var test = request(server).post('/')
713+
test.set('Content-Encoding', 'zstd')
714+
test.set('Content-Type', 'application/json')
715+
test.write(Buffer.from('28b52ffd200e7100007b226e616d65223a22e8aeba227d', 'hex'))
716+
test.expect(200, '{"name":"论"}', done)
717+
})
718+
719+
nozstandardit('should throw 415 if there\'s no zstandard support', function (done) {
720+
const server = createServer({ experimentalZstd: true, limit: '1kb' })
721+
var test = request(server).post('/')
722+
test.set('Content-Encoding', 'zstd')
723+
test.set('Content-Type', 'application/json')
724+
test.write(Buffer.from('28b52ffd200e7100007b226e616d65223a22e8aeba227d', 'hex'))
725+
test.expect(415, '[encoding.unsupported] unsupported content encoding "zstd"', done)
726+
})
727+
705728
it('should be case-insensitive', function (done) {
706729
var test = request(this.server).post('/')
707730
test.set('Content-Encoding', 'GZIP')

test/raw.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
var assert = require('node:assert')
44
var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage
55
var http = require('node:http')
6+
const zlib = require('node:zlib')
67
var request = require('supertest')
78

89
var bodyParser = require('..')
910

11+
const hasZstandardSupport = 'createZstdDecompress' in zlib
12+
const zstandardit = hasZstandardSupport ? it : it.skip
13+
const nozstandardit = !hasZstandardSupport ? it : it.skip
14+
1015
describe('bodyParser.raw()', function () {
1116
before(function () {
1217
this.server = createServer()
@@ -458,6 +463,24 @@ describe('bodyParser.raw()', function () {
458463
test.expect(200, 'buf:6e616d653de8aeba', done)
459464
})
460465

466+
zstandardit('should support zstandard encoding', function (done) {
467+
const server = createServer({ experimentalZstd: true, limit: '10kb' })
468+
var test = request(server).post('/')
469+
test.set('Content-Encoding', 'zstd')
470+
test.set('Content-Type', 'application/octet-stream')
471+
test.write(Buffer.from('28b52ffd20084100006e616d653de8aeba', 'hex'))
472+
test.expect(200, 'buf:6e616d653de8aeba', done)
473+
})
474+
475+
nozstandardit('should throw 415 if there\'s no zstandard support', function (done) {
476+
const server = createServer({ experimentalZstd: true, limit: '10kb' })
477+
var test = request(server).post('/')
478+
test.set('Content-Encoding', 'zstd')
479+
test.set('Content-Type', 'application/octet-stream')
480+
test.write(Buffer.from('28b52ffd20084100006e616d653de8aeba', 'hex'))
481+
test.expect(415, '[encoding.unsupported] unsupported content encoding "zstd"', done)
482+
})
483+
461484
it('should be case-insensitive', function (done) {
462485
var test = request(this.server).post('/')
463486
test.set('Content-Encoding', 'GZIP')

test/text.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
var assert = require('node:assert')
44
var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage
55
var http = require('node:http')
6+
const zlib = require('node:zlib')
67
var request = require('supertest')
78

89
var bodyParser = require('..')
910

11+
const hasZstandardSupport = 'createZstdDecompress' in zlib
12+
const zstandardit = hasZstandardSupport ? it : it.skip
13+
const nozstandardit = !hasZstandardSupport ? it : it.skip
14+
1015
describe('bodyParser.text()', function () {
1116
before(function () {
1217
this.server = createServer()
@@ -528,6 +533,24 @@ describe('bodyParser.text()', function () {
528533
test.expect(200, '"name is 论"', done)
529534
})
530535

536+
zstandardit('should support zstandard encoding', function (done) {
537+
const server = createServer({ experimentalZstd: true, limit: '10kb' })
538+
var test = request(server).post('/')
539+
test.set('Content-Encoding', 'zstd')
540+
test.set('Content-Type', 'text/plain')
541+
test.write(Buffer.from('28b52ffd200b5900006e616d6520697320e8aeba', 'hex'))
542+
test.expect(200, '"name is 论"', done)
543+
})
544+
545+
nozstandardit('should throw 415 if there\'s no zstandard support', function (done) {
546+
const server = createServer({ experimentalZstd: true, limit: '10kb' })
547+
var test = request(server).post('/')
548+
test.set('Content-Encoding', 'zstd')
549+
test.set('Content-Type', 'text/plain')
550+
test.write(Buffer.from('28b52ffd200b5900006e616d6520697320e8aeba', 'hex'))
551+
test.expect(415, '[encoding.unsupported] unsupported content encoding "zstd"', done)
552+
})
553+
531554
it('should be case-insensitive', function (done) {
532555
var test = request(this.server).post('/')
533556
test.set('Content-Encoding', 'GZIP')

test/urlencoded.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
var assert = require('node:assert')
44
var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage
55
var http = require('node:http')
6+
const zlib = require('node:zlib')
67
var request = require('supertest')
78

89
var bodyParser = require('..')
910

11+
const hasZstandardSupport = 'createZstdDecompress' in zlib
12+
const zstandardit = hasZstandardSupport ? it : it.skip
13+
const nozstandardit = !hasZstandardSupport ? it : it.skip
14+
1015
describe('bodyParser.urlencoded()', function () {
1116
before(function () {
1217
this.server = createServer()
@@ -910,6 +915,24 @@ describe('bodyParser.urlencoded()', function () {
910915
test.expect(200, '{"name":"论"}', done)
911916
})
912917

918+
zstandardit('should support zstandard encoding', function (done) {
919+
const server = createServer({ experimentalZstd: true })
920+
var test = request(server).post('/')
921+
test.set('Content-Encoding', 'zstd')
922+
test.set('Content-Type', 'application/x-www-form-urlencoded')
923+
test.write(Buffer.from('28b52ffd20084100006e616d653de8aeba', 'hex'))
924+
test.expect(200, '{"name":"论"}', done)
925+
})
926+
927+
nozstandardit('should throw 415 if there\'s no zstandard support', function (done) {
928+
const server = createServer({ experimentalZstd: true })
929+
var test = request(server).post('/')
930+
test.set('Content-Encoding', 'zstd')
931+
test.set('Content-Type', 'application/x-www-form-urlencoded')
932+
test.write(Buffer.from('28b52ffd20084100006e616d653de8aeba', 'hex'))
933+
test.expect(415, '[encoding.unsupported] unsupported content encoding "zstd"', done)
934+
})
935+
913936
it('should be case-insensitive', function (done) {
914937
var test = request(this.server).post('/')
915938
test.set('Content-Encoding', 'GZIP')

0 commit comments

Comments
 (0)