Skip to content

Commit ce6b539

Browse files
Fix(perMessageDeflate): Resolve error handling for large payloads exceeding maxPayload.
This resolves an RFC non-compliance issue where certain large, compressed payloads could return Z_DATA_ERROR(1007) instead of WS_ERR_UNSUPPORTED_MESSAGE_LENGTH(1009) during permessage-deflate decompression. The updated logic ensures compatibility across Node.js versions < 13.10.0 while preserving correct error handling for inflated data. Co-authored-by: Luigi Pinca <[email protected]>
1 parent aa998e3 commit ce6b539

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/permessage-deflate.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ function inflateOnData(chunk) {
494494
this[kError].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
495495
this[kError][kStatusCode] = 1009;
496496
this.removeListener('data', inflateOnData);
497+
498+
//
499+
// The choice to employ `zlib.reset()` over `zlib.close()` is dictated by the
500+
// fact that in Node.js versions prior to 13.10.0, the callback for
501+
// `zlib.flush()` is not called if `zlib.close()` is used. Utilizing
502+
// `zlib.reset()` ensures that either the callback is invoked or an error is
503+
// emitted.
504+
//
497505
this.reset();
498506
}
499507

@@ -509,6 +517,12 @@ function inflateOnError(err) {
509517
// closed when an error is emitted.
510518
//
511519
this[kPerMessageDeflate]._inflate = null;
520+
521+
if (this[kError]) {
522+
this[kCallback](this[kError]);
523+
return;
524+
}
525+
512526
err[kStatusCode] = 1007;
513527
this[kCallback](err);
514528
}

test/permessage-deflate.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ describe('PerMessageDeflate', () => {
592592

593593
it("doesn't call the callback twice when `maxPayload` is exceeded", (done) => {
594594
const perMessageDeflate = new PerMessageDeflate({}, false, 25);
595-
const buf = Buffer.from('A'.repeat(50));
595+
const buf = Buffer.from('A'.repeat(1024 * 1024));
596596

597597
perMessageDeflate.accept([{}]);
598598
perMessageDeflate.compress(buf, true, (err, data) => {

0 commit comments

Comments
 (0)