1

I'm using Node.JS and I'm trying to convert this string that looks like this

{"blocks":[{"id":"block","block":"event","args":{"items":[]},"action":"Join"},{"id":"block","block":"player_action","args":{"items":[{"item":{"id":"txt","data":{"name":"hi"}},"slot":0},{"item":{"id":"bl_tag","data":{"option":"Add spaces","tag":"Text Value Merging","action":"SendMessage","block":"player_action"}},"slot":25},{"item":{"id":"bl_tag","data":{"option":"Regular","tag":"Alignment Mode","action":"SendMessage","block":"player_action"}},"slot":26}]},"action":"SendMessage"}]}

into Gzip using zlib I have tried following some answers on Stack Overflow like this

var deflated = zlib.deflateSync('{"blocks":[{"id":"block","block":"event","args":{"items":[]},"action":"Join"},{"id":"block","block":"player_action","args":{"items":[{"item":{"id":"txt","data":{"name":"hi"}},"slot":0},{"item":{"id":"bl_tag","data":{"option":"Add spaces","tag":"Text Value Merging","action":"SendMessage","block":"player_action"}},"slot":25},{"item":{"id":"bl_tag","data":{"option":"Regular","tag":"Alignment Mode","action":"SendMessage","block":"player_action"}},"slot":26}]},"action":"SendMessage"}]}').toString('base64');
var inflated = zlib.inflateSync(new Buffer(deflated, 'base64')).toString();

console.log(inflated);

But then I get the error:

{"blocks":[{"id":"block","block":"event","args":{"items":[]},"action":"Join"},{"id":"block","block":"player_action","args":{"items":[{"item":{"id":"txt","data":{"name":"hi"}},"slot":0},{"item":{"id":"bl_tag","data":{"option":"Add spaces","tag":"Text Value Merging","action":"SendMessage","block":"player_action"}},"slot":25},{"item":{"id":"bl_tag","data":{"option":"Regular","tag":"Alignment Mode","action":"SendMessage","block":"player_action"}},"slot":26}]},"action":"SendMessage"}]}
(node:25548) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

This is what I'm trying to achieve (this is after its been base64'd) H4sIAAAAAAAA/6VRsQrCMBD9lXKzgwg6ZHMVuqi4iJRrc8RgmpQmFaX03720RSviIE7JvXvv3rukhdy44uJBHFvQEsRQw2w8BdCVbOAaa8UsJgUqI/3UMVYE7SyTNk5b6GZfRlQG71RnI/tz1HDrkSgPt+gnMWCELJbE4FlDx47euABi3ltNNbnJAqqJzFVjtLWUia+wIM/dyBGwp1tIDmgaSlKqlbZR+NxlR1am5D0q+rrDK8pi+UOWLanGYP0MsjZa2ZLfN0mdpD9CrLq375iqufMAPTQAnOUBAAA= (from a website)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I don't see an error, I only see a deprecation **warning**. – Mark Rotteveel May 02 '21 at 09:04
  • It dosent log the gzipped string though. – Blobster System May 02 '21 at 09:08
  • What is the line before the warning then? And note that you only write `inflated` to the console, not `deflated`. – Mark Rotteveel May 02 '21 at 09:10
  • 1
    This is not an error but a warning. And you can get around it by using `Buffer.from()`. And it doesn't log the gzipped result, because you are doing a `deflateSync` (ie compress) followed by an `inflateSync` (ie decompress) and only log the result of the decompression. You have to do `console.log(deflated)` to log out the zipped string – derpirscher May 02 '21 at 09:11
  • BTW your expected result does not seem to be Buffer originating from zlib compression because it throws an error on `inflateSync` – derpirscher May 02 '21 at 09:18

1 Answers1

0

Aside from the warning against using Buffer ctor which is documented as deprecated and as derpischer commented you can fix by using Buffer.from:

TLDR: gzip != deflate -- but abs(gzip-deflate)<epsilon

The zlib library, and the nodejs module accessing it, supports several formats that are similar and related but different and not the same, all using the deflate/inflate compression algorithm originally developed for (PK)ZIP, plus the newer Brotli algorithm (not relevant here).

zlib supports: (1) 'raw' inflate/deflate; (2) 'zlib' format, which adds a trivial header and an integrity check; (3) 'gzip' format, which adds a different header and a different integrity check. See How are zlib, gzip and zip related? What do they have in common and how are they different? for an explanation of this situation by Mark Adler, one of the zlib authors.

nodejs confuses this slightly by using the names {In,De}Flate and {in,de}flate[Sync] for zlib format; {In,De}FlateRaw and {in,de}flateRaw[Sync] for raw format; and G[un]zip and g[un]zip[Sync] for gzip format. You are using the methods for zlib format, so you get zlib format and not gzip format; to get gzip format, use the methods for gzip format.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70