* Parses control frames. * @param {Buffer} body
(body)
| 18076 | } |
| 18077 | } |
| 18078 | } |
| 18079 | } |
| 18080 | } |
| 18081 | /** |
| 18082 | * Take n bytes from the buffered Buffers |
| 18083 | * @param {number} n |
| 18084 | * @returns {Buffer} |
| 18085 | */ |
| 18086 | consume(n) { |
| 18087 | if (n > __privateGet(this, _byteOffset)) { |
| 18088 | throw new Error("Called consume() before buffers satiated."); |
| 18089 | } else if (n === 0) { |
| 18090 | return emptyBuffer; |
| 18091 | } |
| 18092 | if (__privateGet(this, _buffers)[0].length === n) { |
| 18093 | __privateSet(this, _byteOffset, __privateGet(this, _byteOffset) - __privateGet(this, _buffers)[0].length); |
| 18094 | return __privateGet(this, _buffers).shift(); |
| 18095 | } |
| 18096 | const buffer = Buffer.allocUnsafe(n); |
| 18097 | let offset = 0; |
| 18098 | while (offset !== n) { |
| 18099 | const next = __privateGet(this, _buffers)[0]; |
| 18100 | const { length } = next; |
| 18101 | if (length + offset === n) { |
| 18102 | buffer.set(__privateGet(this, _buffers).shift(), offset); |
| 18103 | break; |
| 18104 | } else if (length + offset > n) { |
| 18105 | buffer.set(next.subarray(0, n - offset), offset); |
| 18106 | __privateGet(this, _buffers)[0] = next.subarray(n - offset); |
| 18107 | break; |
| 18108 | } else { |
| 18109 | buffer.set(__privateGet(this, _buffers).shift(), offset); |
| 18110 | offset += next.length; |
| 18111 | } |
| 18112 | } |
| 18113 | __privateSet(this, _byteOffset, __privateGet(this, _byteOffset) - n); |
| 18114 | return buffer; |
| 18115 | } |
| 18116 | writeFragments(fragment) { |
| 18117 | if (__privateGet(this, _maxFragments) > 0 && __privateGet(this, _fragments).length === __privateGet(this, _maxFragments)) { |
| 18118 | failWebsocketConnectionWithCode(this.ws, 1008, "Too many message fragments"); |
| 18119 | return false; |
| 18120 | } |
| 18121 | __privateSet(this, _fragmentsBytes, __privateGet(this, _fragmentsBytes) + fragment.length); |
| 18122 | __privateGet(this, _fragments).push(fragment); |
| 18123 | return true; |
| 18124 | } |
| 18125 | consumeFragments() { |
| 18126 | const fragments = __privateGet(this, _fragments); |
| 18127 | if (fragments.length === 1) { |
| 18128 | __privateSet(this, _fragmentsBytes, 0); |
| 18129 | return fragments.shift(); |
| 18130 | } |
| 18131 | const output = Buffer.concat(fragments, __privateGet(this, _fragmentsBytes)); |
| 18132 | __privateSet(this, _fragments, []); |
no test coverage detected