Path: blob/master/src/packages/sync-fs/lib/compressed-json.ts
1496 views
/*1Compress and deocmpression JSON to a Buffer. This buffer *is* suitable2to write to an lz4 file and lz4 -d will work with it.34NOTE: I was worried because lz4-napi's compressSync and uncompressSync5seem to have a MASSIVE memory leak. I tested these functions via the6following, and did NOT observe a memory leak. So it's maybe just a problem7with their sync functions, fortunately.89a = require('@cocalc/sync-fs/lib/compressed-json')10t=Date.now(); for(i=0;i<10000;i++) { await a.fromCompressedJSON(await a.toCompressedJSON({a:'x'.repeat(1000000)}))}; Date.now()-t11*/1213import { compressFrame, decompressFrame } from "lz4-napi";1415export async function toCompressedJSON(obj: any): Promise<Buffer> {16return await compressFrame(Buffer.from(JSON.stringify(obj)));17}1819export async function fromCompressedJSON(compressedJSON: Buffer): Promise<any> {20return JSON.parse((await decompressFrame(compressedJSON)).toString());21}222324