Skip to content

Commit e182968

Browse files
committed
feat: zstandard support
1 parent 5d691ff commit e182968

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

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
*
@@ -224,12 +230,16 @@ function createDecompressionStream (encoding, debug) {
224230
case 'br':
225231
debug('brotli decompress body')
226232
return zlib.createBrotliDecompress()
227-
default:
228-
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
229-
encoding: encoding,
230-
type: 'encoding.unsupported'
231-
})
233+
case 'zstd':
234+
if (hasZstandardSupport) {
235+
debug('zstd decompress body')
236+
return zlib.createZstdDecompress()
237+
}
232238
}
239+
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
240+
encoding: encoding,
241+
type: 'encoding.unsupported'
242+
})
233243
}
234244

235245
/**

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())
@@ -686,6 +691,24 @@ describe('bodyParser.json()', function () {
686691
test.expect(200, '{"name":"论"}', done)
687692
})
688693

694+
zstandardit('should support zstandard encoding', function (done) {
695+
const server = createServer({ experimentalZstd: true, limit: '1kb' })
696+
var test = request(server).post('/')
697+
test.set('Content-Encoding', 'zstd')
698+
test.set('Content-Type', 'application/json')
699+
test.write(Buffer.from('28b52ffd200e7100007b226e616d65223a22e8aeba227d', 'hex'))
700+
test.expect(200, '{"name":"论"}', done)
701+
})
702+
703+
nozstandardit('should throw 415 if there\'s no zstandard support', function (done) {
704+
const server = createServer({ experimentalZstd: true, limit: '1kb' })
705+
var test = request(server).post('/')
706+
test.set('Content-Encoding', 'zstd')
707+
test.set('Content-Type', 'application/json')
708+
test.write(Buffer.from('28b52ffd200e7100007b226e616d65223a22e8aeba227d', 'hex'))
709+
test.expect(415, '[encoding.unsupported] unsupported content encoding "zstd"', done)
710+
})
711+
689712
it('should be case-insensitive', function (done) {
690713
var test = request(this.server).post('/')
691714
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()
@@ -906,6 +911,24 @@ describe('bodyParser.urlencoded()', function () {
906911
test.expect(200, '{"name":"论"}', done)
907912
})
908913

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

0 commit comments

Comments
 (0)