I am getting the data which is compressed(Hex data) by Gzip and the text encoding is GBK from the server like this:
<Buffer 2d 00 00 00 1f 8b 08 00 00 00 00 00 00 ff 32 34 34 37 32 31 33 32 36 32 30 a8 31 ac 31 e2 02 00 00 00 ff ff 01 00 00 ff ff 96 39 e9 8c 10 00 00 00>
<Buffer d5 19 0f 00 1f 8b 08 00 00 00 00 00 00 ff a4 bd ff 73 1b d7 b5 27 f8 fb fc 15 5d 35 55 5b 49 6d 3d 4d df db df f3 d3 82 a0 28 29 24 25 0d 21 89 13 67 ... 12986 more bytes>
<Buffer ca ee e7 13 20 8a c5 f7 1b 66 68 1f d7 64 13 b3 f7 64 d8 0e 7d a7 fd bb 10 1f 4a 27 53 b9 c9 0b 2c 93 36 3e 94 ef db 9e 88 bc c6 6b 2e 1c ed ea b5 e8 ... 15878 more bytes>
The data format is [{4bytes}{Content data}] 4 bytes: Packet Length not included by these 4 bytes self. (The first byte is the lower byte of the Length)
I tried the zlib.inflate
, however TypeError: Cannot read properties of undefined (reading 'toString')
is occurring.
client.on("data", (data) => {
zlib.inflate(data, (err, data) => {
console.log(data.toString('hex'));
});
How can I decode and decompress the above hex response, and also handle the TCP packet fragmentation? Thank you.
first of all you probably have error here
client.on("data", (data) => {
zlib.inflate(data, (err, data) => {
//HERE YOU PROBABLY HAVE ERROR. YOU SHOULD CHECK IT
console.log(data.toString('hex'));
});
you also need to skip the first 4 bytes as you expiation of the data structure
but anyway you should use gunzip
here working example including data structure handling
let compressBuffer = Buffer.alloc(0);
let remainSize = 0;
client.on("data", (data) => {
while (data.length) {
if (remainSize == 0) {
remainSize = data.readInt32LE();
data = data.slice(4);
compressBuffer = Buffer.alloc(0);
}
//read as much as you can
const readingPart = data.slice(0, remainSize);
compressBuffer = Buffer.concat([compressBuffer, readingPart]);
data = data.slice(readingPart.length);
remainSize -= readingPart.length;
if (remainSize == 0) {
//we have the full gzip buffer lets decompress it
zlib.gunzip(compressBuffer, (err, decompressedData) => {
if (err) console.log(err);
else {
console.log(decompressedData.toString());
}
});
}
}
});