| 19 | private buf = new Uint8Array(0); |
| 20 | |
| 21 | push(chunk: Uint8Array): NBMessage[] { |
| 22 | // append |
| 23 | const merged = new Uint8Array(this.buf.length + chunk.length); |
| 24 | merged.set(this.buf); |
| 25 | merged.set(chunk, this.buf.length); |
| 26 | this.buf = merged; |
| 27 | |
| 28 | const out: NBMessage[] = []; |
| 29 | while (this.buf.length >= 4) { |
| 30 | const type = this.buf[0]; |
| 31 | // length is 17-bit: high bit of byte 1, then bytes 2-3 big-endian |
| 32 | const len = ((this.buf[1] & 0x01) << 16) | (this.buf[2] << 8) | this.buf[3]; |
| 33 | const total = 4 + len; |
| 34 | if (this.buf.length < total) break; |
| 35 | |
| 36 | const frame = this.buf.subarray(0, total); |
| 37 | this.buf = this.buf.slice(total); |
| 38 | |
| 39 | if (type === NB_SESSION_REQUEST) { |
| 40 | out.push({ type: NB_SESSION_REQUEST }); |
| 41 | } else if (type === NB_SESSION_MESSAGE) { |
| 42 | out.push({ type: NB_SESSION_MESSAGE, payload: frame.slice(4) }); |
| 43 | } else if (type === NB_SESSION_KEEPALIVE) { |
| 44 | out.push({ type: NB_SESSION_KEEPALIVE }); |
| 45 | } |
| 46 | // anything else: drop |
| 47 | } |
| 48 | return out; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | export function nbPositiveResponse(): Uint8Array { |