|
| 1 | +const { Transform } = require('stream'); |
| 2 | +const { |
| 3 | + FrameEncoder, |
| 4 | + createLibp2pStreamFactory, |
| 5 | + encodeFrame |
| 6 | +} = require('./FrameCodecShared'); |
| 7 | + |
| 8 | +const DEBUG_FRAME_DECODER = process.env.FRAME_DECODER_DEBUG === '1'; |
| 9 | + |
| 10 | +class FrameDecoderCirc extends Transform { |
| 11 | + constructor(bufferSize = 16384) { |
| 12 | + super({ readableObjectMode: true }); |
| 13 | + this._buf = Buffer.allocUnsafe(bufferSize); |
| 14 | + this._head = 0; |
| 15 | + this._tail = 0; |
| 16 | + this._size = bufferSize; |
| 17 | + this._e = null; |
| 18 | + this._vlen = 0; |
| 19 | + this._id = FrameDecoderCirc._nextId++; |
| 20 | + } |
| 21 | + |
| 22 | + _log(event, details) { |
| 23 | + if (!DEBUG_FRAME_DECODER) return; |
| 24 | + const prefix = `FrameDecoderCirc#${this._id}`; |
| 25 | + if (details) { |
| 26 | + console.log(`${prefix} ${event}`, details); |
| 27 | + } else { |
| 28 | + console.log(`${prefix} ${event}`); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + _available() { |
| 33 | + return (this._tail - this._head + this._size) % this._size; |
| 34 | + } |
| 35 | + |
| 36 | + _transform(chunk, encoding, cb) { |
| 37 | + try { |
| 38 | + if (!Buffer.isBuffer(chunk)) chunk = Buffer.from(chunk); |
| 39 | + |
| 40 | + const avail = this._available(); |
| 41 | + const needed = chunk.length; |
| 42 | + |
| 43 | + if (needed > this._size - avail - 1) { |
| 44 | + const newSize = Math.max(this._size * 2, this._size + needed); |
| 45 | + const newBuf = Buffer.allocUnsafe(newSize); |
| 46 | + const used = avail; |
| 47 | + |
| 48 | + if (this._head <= this._tail) { |
| 49 | + this._buf.copy(newBuf, 0, this._head, this._tail); |
| 50 | + } else { |
| 51 | + const firstPart = this._size - this._head; |
| 52 | + this._buf.copy(newBuf, 0, this._head, this._size); |
| 53 | + this._buf.copy(newBuf, firstPart, 0, this._tail); |
| 54 | + } |
| 55 | + |
| 56 | + this._buf = newBuf; |
| 57 | + this._head = 0; |
| 58 | + this._tail = used; |
| 59 | + this._size = newSize; |
| 60 | + } |
| 61 | + |
| 62 | + let written = 0; |
| 63 | + while (written < chunk.length) { |
| 64 | + const contiguous = this._tail < this._head |
| 65 | + ? this._head - this._tail - 1 |
| 66 | + : this._size - this._tail - (this._head === 0 ? 1 : 0); |
| 67 | + const toWrite = Math.min(chunk.length - written, contiguous); |
| 68 | + chunk.copy(this._buf, this._tail, written, written + toWrite); |
| 69 | + this._tail = (this._tail + toWrite) % this._size; |
| 70 | + written += toWrite; |
| 71 | + } |
| 72 | + |
| 73 | + this._log('chunk', { chunkLength: chunk.length, buffered: this._available() }); |
| 74 | + |
| 75 | + while (this._available() > 0) { |
| 76 | + this._log('loop', { buffered: this._available(), expectedPayload: this._e, varintBytes: this._vlen }); |
| 77 | + |
| 78 | + if (this._e === null) { |
| 79 | + const decoded = this._dv(); |
| 80 | + if (!decoded) { |
| 81 | + this._log('await_varint', { buffered: this._available() }); |
| 82 | + break; |
| 83 | + } |
| 84 | + this._e = decoded.value; |
| 85 | + this._vlen = decoded.bytes; |
| 86 | + this._log('varint_ready', { payloadLength: this._e, headerBytes: this._vlen, buffered: this._available() }); |
| 87 | + } |
| 88 | + |
| 89 | + if (this._e !== null) { |
| 90 | + const need = this._vlen + this._e; |
| 91 | + if (this._available() < need) { |
| 92 | + this._log('await_payload', { need, buffered: this._available() }); |
| 93 | + break; |
| 94 | + } |
| 95 | + this._consume(this._vlen); |
| 96 | + const payload = this._take(this._e); |
| 97 | + this._log('frame_emitted', { payloadLength: this._e, buffered: this._available() }); |
| 98 | + this._e = null; |
| 99 | + this._vlen = 0; |
| 100 | + this.push(payload); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + cb(); |
| 105 | + } catch (err) { |
| 106 | + cb(err); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + _dv() { |
| 111 | + let r = 0, s = 0, p = 0; |
| 112 | + const avail = this._available(); |
| 113 | + let pos = this._head; |
| 114 | + |
| 115 | + while (p < avail) { |
| 116 | + const v = this._buf[pos]; |
| 117 | + pos = (pos + 1) % this._size; |
| 118 | + r |= (v & 0x7f) << s; |
| 119 | + p++; |
| 120 | + this._log('varint_byte', { byte: v, shift: s, partialValue: r, bytesRead: p }); |
| 121 | + if ((v & 0x80) === 0) { |
| 122 | + this._log('varint_complete', { value: r, bytes: p }); |
| 123 | + return { value: r, bytes: p }; |
| 124 | + } |
| 125 | + s += 7; |
| 126 | + if (s > 53) break; |
| 127 | + } |
| 128 | + this._log('varint_incomplete', { bytesScanned: p, buffered: avail }); |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + _consume(n) { |
| 133 | + this._head = (this._head + n) % this._size; |
| 134 | + } |
| 135 | + |
| 136 | + _take(n) { |
| 137 | + const f = Buffer.allocUnsafe(n); |
| 138 | + let w = 0; |
| 139 | + this._log('take_start', { bytes: n, buffered: this._available() }); |
| 140 | + |
| 141 | + while (w < n) { |
| 142 | + const contiguous = this._head < this._tail |
| 143 | + ? this._tail - this._head |
| 144 | + : this._size - this._head; |
| 145 | + const toCopy = Math.min(n - w, contiguous); |
| 146 | + this._buf.copy(f, w, this._head, this._head + toCopy); |
| 147 | + this._head = (this._head + toCopy) % this._size; |
| 148 | + w += toCopy; |
| 149 | + this._log('take_progress', { copied: toCopy, written: w, buffered: this._available() }); |
| 150 | + } |
| 151 | + |
| 152 | + this._log('take_complete', { bytes: n, buffered: this._available() }); |
| 153 | + return f; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +FrameDecoderCirc._nextId = 1; |
| 158 | + |
| 159 | +module.exports = { |
| 160 | + FrameEncoder, |
| 161 | + FrameDecoderCirc, |
| 162 | + createLibp2pStream: createLibp2pStreamFactory(() => new FrameDecoderCirc()), |
| 163 | + encodeFrame |
| 164 | +}; |
0 commit comments