From e63facc547a8cf76c01d0fd645b4fd4a71117885 Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 30 Dec 2021 19:55:43 +0000 Subject: [PATCH] decode snappy compressed base64 json. --- package-lock.json | 5 ++++ package.json | 3 +- .../JSONFormatter/JSONFormatter.jsx | 30 ++++++++++++++----- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index b8dc45e..9d3c2d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12117,6 +12117,11 @@ "kind-of": "^3.2.0" } }, + "snappyjs": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/snappyjs/-/snappyjs-0.6.1.tgz", + "integrity": "sha512-YIK6I2lsH072UE0aOFxxY1dPDCS43I5ktqHpeAsuLNYWkE5pGxRGWfDM4/vSUfNzXjC1Ivzt3qx31PCLmc9yqg==" + }, "sockjs": { "version": "0.3.19", "resolved": "https://registry.npmjs.org/sockjs/-/sockjs-0.3.19.tgz", diff --git a/package.json b/package.json index c77a3c3..6ea82a7 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "react-codemirror2": "^7.1.0", "react-copy-to-clipboard": "^5.0.2", "react-dom": "^16.13.1", - "react-scripts": "3.4.1" + "react-scripts": "3.4.1", + "snappyjs": "^0.6.1" }, "scripts": { "start": "react-scripts start", diff --git a/src/components/JSONFormatter/JSONFormatter.jsx b/src/components/JSONFormatter/JSONFormatter.jsx index 7676eac..fdb4e3e 100644 --- a/src/components/JSONFormatter/JSONFormatter.jsx +++ b/src/components/JSONFormatter/JSONFormatter.jsx @@ -1,6 +1,8 @@ import React, { Component } from "react"; import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles'; import { UnControlled as CodeMirror } from 'react-codemirror2' +import SnappyJS from 'snappyjs' +import { Buffer } from "buffer"; const zlib = require('zlib'); require('codemirror/mode/javascript/javascript'); @@ -61,16 +63,30 @@ export class JSONFormatter extends Component { formatJSON(input) { if (input) { + var jsonString = ""; if (!input.startsWith("0x")) { - throw new Error("input needs to be a Hex String starts with '0x'."); + input = input.replace(/\r?\n|\r/g, ""); + // check whether the input is a base64 string and then try use snappy to decompress it + var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/; + if (base64regex.test(input)) { + var uncompressed = SnappyJS.uncompress(Buffer.from(input, 'base64')); + let utf8decoder = new TextDecoder() + jsonString = utf8decoder.decode(uncompressed); + } + else { + throw new Error("input needs to be a Hex String starts with '0x' or valid base64 string."); + } } - input = input.substring(2); - var hex = Buffer.from(input, 'hex'); - if (input.toLowerCase().startsWith("1f8b")) { - // gzip string, need to unzip first - hex = zlib.gunzipSync(hex); + else { + input = input.substring(2); + var hex = Buffer.from(input, 'hex'); + if (input.toLowerCase().startsWith("1f8b")) { + // gzip string, need to unzip first + hex = zlib.gunzipSync(hex); + } + jsonString = hex.toString('utf-8'); } - var jsonString = hex.toString('utf-8'); + var parsedData = JSON.parse(jsonString); var outputText = JSON.stringify(parsedData, null, 4); return outputText;