aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/node_modules/undici/lib/web/websocket/permessage-deflate.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/undici/lib/web/websocket/permessage-deflate.js')
-rw-r--r--node_modules/undici/lib/web/websocket/permessage-deflate.js44
1 files changed, 13 insertions, 31 deletions
diff --git a/node_modules/undici/lib/web/websocket/permessage-deflate.js b/node_modules/undici/lib/web/websocket/permessage-deflate.js
index 1f1a130..6a6e438 100644
--- a/node_modules/undici/lib/web/websocket/permessage-deflate.js
+++ b/node_modules/undici/lib/web/websocket/permessage-deflate.js
@@ -8,40 +8,35 @@ const tail = Buffer.from([0x00, 0x00, 0xff, 0xff])
const kBuffer = Symbol('kBuffer')
const kLength = Symbol('kLength')
-// Default maximum decompressed message size: 4 MB
-const kDefaultMaxDecompressedSize = 4 * 1024 * 1024
-
class PerMessageDeflate {
/** @type {import('node:zlib').InflateRaw} */
#inflate
#options = {}
- /** @type {boolean} */
- #aborted = false
-
- /** @type {Function|null} */
- #currentCallback = null
+ #maxPayloadSize = 0
/**
* @param {Map<string, string>} extensions
*/
- constructor (extensions) {
+ constructor (extensions, options) {
this.#options.serverNoContextTakeover = extensions.has('server_no_context_takeover')
this.#options.serverMaxWindowBits = extensions.get('server_max_window_bits')
+
+ this.#maxPayloadSize = options.maxPayloadSize
}
+ /**
+ * Decompress a compressed payload.
+ * @param {Buffer} chunk Compressed data
+ * @param {boolean} fin Final fragment flag
+ * @param {Function} callback Callback function
+ */
decompress (chunk, fin, callback) {
// An endpoint uses the following algorithm to decompress a message.
// 1. Append 4 octets of 0x00 0x00 0xff 0xff to the tail end of the
// payload of the message.
// 2. Decompress the resulting data using DEFLATE.
-
- if (this.#aborted) {
- callback(new MessageSizeExceededError())
- return
- }
-
if (!this.#inflate) {
let windowBits = Z_DEFAULT_WINDOWBITS
@@ -64,23 +59,12 @@ class PerMessageDeflate {
this.#inflate[kLength] = 0
this.#inflate.on('data', (data) => {
- if (this.#aborted) {
- return
- }
-
this.#inflate[kLength] += data.length
- if (this.#inflate[kLength] > kDefaultMaxDecompressedSize) {
- this.#aborted = true
+ if (this.#maxPayloadSize > 0 && this.#inflate[kLength] > this.#maxPayloadSize) {
+ callback(new MessageSizeExceededError())
this.#inflate.removeAllListeners()
- this.#inflate.destroy()
this.#inflate = null
-
- if (this.#currentCallback) {
- const cb = this.#currentCallback
- this.#currentCallback = null
- cb(new MessageSizeExceededError())
- }
return
}
@@ -93,14 +77,13 @@ class PerMessageDeflate {
})
}
- this.#currentCallback = callback
this.#inflate.write(chunk)
if (fin) {
this.#inflate.write(tail)
}
this.#inflate.flush(() => {
- if (this.#aborted || !this.#inflate) {
+ if (!this.#inflate) {
return
}
@@ -108,7 +91,6 @@ class PerMessageDeflate {
this.#inflate[kBuffer].length = 0
this.#inflate[kLength] = 0
- this.#currentCallback = null
callback(null, full)
})