diff --git a/package-lock.json b/package-lock.json index c01ef99..d2660d7 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;