|
| 1 | +import 'mocha'; |
| 2 | +import { PgPacketStream, Packet } from './' |
| 3 | +import { expect } from 'chai' |
| 4 | +import chunky from 'chunky' |
| 5 | + |
| 6 | +const consume = async (stream: PgPacketStream, count: number): Promise<Packet[]> => { |
| 7 | + const result: Packet[] = []; |
| 8 | + |
| 9 | + return new Promise((resolve) => { |
| 10 | + const read = () => { |
| 11 | + stream.once('readable', () => { |
| 12 | + let packet; |
| 13 | + while (packet = stream.read()) { |
| 14 | + result.push(packet) |
| 15 | + } |
| 16 | + if (result.length === count) { |
| 17 | + resolve(result); |
| 18 | + } else { |
| 19 | + read() |
| 20 | + } |
| 21 | + |
| 22 | + }) |
| 23 | + } |
| 24 | + read() |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +const emptyMessage = Buffer.from([0x0a, 0x00, 0x00, 0x00, 0x04]) |
| 29 | +const oneByteMessage = Buffer.from([0x0b, 0x00, 0x00, 0x00, 0x05, 0x0a]) |
| 30 | +const bigMessage = Buffer.from([0x0f, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e0, 0x0f]) |
| 31 | + |
| 32 | +describe('PgPacketStream', () => { |
| 33 | + it('should chunk a perfect input packet', async () => { |
| 34 | + const stream = new PgPacketStream() |
| 35 | + stream.write(Buffer.from([0x01, 0x00, 0x00, 0x00, 0x04])) |
| 36 | + stream.end() |
| 37 | + const buffers = await consume(stream, 1) |
| 38 | + expect(buffers).to.have.length(1) |
| 39 | + expect(buffers[0].packet).to.deep.equal(Buffer.from([0x1, 0x00, 0x00, 0x00, 0x04])) |
| 40 | + }); |
| 41 | + |
| 42 | + it('should read 2 chunks into perfect input packet', async () => { |
| 43 | + const stream = new PgPacketStream() |
| 44 | + stream.write(Buffer.from([0x01, 0x00, 0x00, 0x00, 0x08])) |
| 45 | + stream.write(Buffer.from([0x1, 0x2, 0x3, 0x4])) |
| 46 | + stream.end() |
| 47 | + const buffers = await consume(stream, 1) |
| 48 | + expect(buffers).to.have.length(1) |
| 49 | + expect(buffers[0].packet).to.deep.equal(Buffer.from([0x1, 0x00, 0x00, 0x00, 0x08, 0x1, 0x2, 0x3, 0x4])) |
| 50 | + }); |
| 51 | + |
| 52 | + it('should read a bunch of big messages', async () => { |
| 53 | + const stream = new PgPacketStream(); |
| 54 | + let totalBuffer = Buffer.allocUnsafe(0); |
| 55 | + const num = 2; |
| 56 | + for (let i = 0; i < 2; i++) { |
| 57 | + totalBuffer = Buffer.concat([totalBuffer, bigMessage, bigMessage]) |
| 58 | + } |
| 59 | + const chunks = chunky(totalBuffer) |
| 60 | + for (const chunk of chunks) { |
| 61 | + stream.write(chunk) |
| 62 | + } |
| 63 | + stream.end() |
| 64 | + const messages = await consume(stream, num * 2) |
| 65 | + expect(messages.map(x => x.code)).to.eql(new Array(num * 2).fill(0x0f)) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should read multiple messages in a single chunk', async () => { |
| 69 | + const stream = new PgPacketStream() |
| 70 | + stream.write(Buffer.from([0x01, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x04])) |
| 71 | + stream.end() |
| 72 | + const buffers = await consume(stream, 2) |
| 73 | + expect(buffers).to.have.length(2) |
| 74 | + expect(buffers[0].packet).to.deep.equal(Buffer.from([0x1, 0x00, 0x00, 0x00, 0x04])) |
| 75 | + expect(buffers[1].packet).to.deep.equal(Buffer.from([0x1, 0x00, 0x00, 0x00, 0x04])) |
| 76 | + }); |
| 77 | + |
| 78 | + it('should read multiple chunks into multiple packets', async () => { |
| 79 | + const stream = new PgPacketStream() |
| 80 | + stream.write(Buffer.from([0x01, 0x00, 0x00, 0x00, 0x05, 0x0a, 0x01, 0x00, 0x00, 0x00, 0x05, 0x0b])) |
| 81 | + stream.write(Buffer.from([0x01, 0x00, 0x00])); |
| 82 | + stream.write(Buffer.from([0x00, 0x06, 0x0c, 0x0d, 0x03, 0x00, 0x00, 0x00, 0x04])) |
| 83 | + stream.end() |
| 84 | + const buffers = await consume(stream, 4) |
| 85 | + expect(buffers).to.have.length(4) |
| 86 | + expect(buffers[0].packet).to.deep.equal(Buffer.from([0x1, 0x00, 0x00, 0x00, 0x05, 0x0a])) |
| 87 | + expect(buffers[1].packet).to.deep.equal(Buffer.from([0x1, 0x00, 0x00, 0x00, 0x05, 0x0b])) |
| 88 | + expect(buffers[2].packet).to.deep.equal(Buffer.from([0x1, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x0d])) |
| 89 | + expect(buffers[3].packet).to.deep.equal(Buffer.from([0x3, 0x00, 0x00, 0x00, 0x04])) |
| 90 | + }); |
| 91 | + |
| 92 | + it('reads packet that spans multiple chunks', async () => { |
| 93 | + const stream = new PgPacketStream() |
| 94 | + stream.write(Buffer.from([0x0d, 0x00, 0x00, 0x00])) |
| 95 | + stream.write(Buffer.from([0x09])) // length |
| 96 | + stream.write(Buffer.from([0x0a, 0x0b, 0x0c, 0x0d])) |
| 97 | + stream.write(Buffer.from([0x0a, 0x0b, 0x0c, 0x0d])) |
| 98 | + stream.write(Buffer.from([0x0a, 0x0b, 0x0c, 0x0d])) |
| 99 | + stream.end() |
| 100 | + const buffers = await consume(stream, 1) |
| 101 | + expect(buffers).to.have.length(1) |
| 102 | + }) |
| 103 | +}); |
0 commit comments