Skip to content

Instantly share code, notes, and snippets.

@un4ckn0wl3z
Last active September 5, 2023 07:42
Show Gist options
  • Save un4ckn0wl3z/bf8789cf5dc770c29fbb22e8147f438e to your computer and use it in GitHub Desktop.
Save un4ckn0wl3z/bf8789cf5dc770c29fbb22e8147f438e to your computer and use it in GitHub Desktop.
import { StringDecoder } from 'string_decoder';
import * as brotli from 'brotli';
export class CompressionHelper {
static tryBrotliCompress(rawStringData: string): string {
const buf = Buffer.from(rawStringData, 'utf8');
const bufBytes = new Uint8Array(buf)
const compressChunks = brotli.compress(bufBytes);
if(compressChunks === null) return Buffer.from(bufBytes).toString("base64");
return Buffer.from(compressChunks).toString("base64");
}
static tryBrotliDecompress(base64Encoded: string): string {
const rawByteArray = Buffer.from(base64Encoded, 'base64');
const decoder = new StringDecoder('utf8');
try {
var decompressChunks = brotli.decompress(rawByteArray);
if(decompressChunks?.length < 1) throw new Error('Empty decompress chunk');
} catch (error) {
return decoder.write(rawByteArray);
}
return decoder.write(decompressChunks);
}
}
export class InstanceDataCodec {
static encode<T>(data: T): string {
const json = JSON.stringify(data,null,0);
const base64EncodedData = Buffer.from(json).toString('base64');
return base64EncodedData;
}
static decode<T>(base64Data: string): T {
const byteArray = Buffer.from(base64Data, 'base64');
const decoder = new StringDecoder('utf8');
const json = decoder.write(byteArray);
const data: T = JSON.parse(json);
return data;
}
static decodeNoneObject(base64Data: string): any {
const byteArray = Buffer.from(base64Data, 'base64');
const decoder = new StringDecoder('utf8');
const json = decoder.write(byteArray);
return json
}
static encodeAndCompress<T>(data: T): string {
const jsonString = JSON.stringify(data,null,0);
return CompressionHelper.tryBrotliCompress(jsonString)
}
static decodeAndDecompressed<T>(base64Data: string): T {
const rawStringDecompressed = CompressionHelper.tryBrotliDecompress(base64Data)
const data: T = JSON.parse(rawStringDecompressed);
return data;
}
static decodeAndDecompressedNoneObject(base64Data: string): any {
return CompressionHelper.tryBrotliDecompress(base64Data);
}
}
export class InstanceDataManagerService {
private instanceData: object = {}
private instanceDataPacked: string = ""
init(base64Data: string){
this.instanceDataPacked = base64Data
this.instanceData = InstanceDataCodec.decode(base64Data)
}
getPlainInstanceData(){
return this.instanceData
}
private pack(){
this.instanceDataPacked = InstanceDataCodec.encode(this.instanceData)
}
remove(key: string){
delete this.instanceData[`${key}`]
this.pack()
}
getPackedInstanceData(){
return this.instanceDataPacked
}
set(key: string, value: any){
this.instanceData[`${key}`] = InstanceDataCodec.encode(value)
this.pack()
}
get(key: string){
try {
return InstanceDataCodec.decode(this.instanceData[`${key}`])
} catch (_) {
try {
return InstanceDataCodec.decodeNoneObject(this.instanceData[`${key}`])
} catch (_) {
return ''
}
}
}
getOrThrow(key: string){
try {
return InstanceDataCodec.decode(this.instanceData[`${key}`])
} catch (_) {
try {
return InstanceDataCodec.decodeNoneObject(this.instanceData[`${key}`])
} catch (e) {
throw e
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment