Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync-fs/lib/compressed-json.ts
1496 views
1
/*
2
Compress and deocmpression JSON to a Buffer. This buffer *is* suitable
3
to write to an lz4 file and lz4 -d will work with it.
4
5
NOTE: I was worried because lz4-napi's compressSync and uncompressSync
6
seem to have a MASSIVE memory leak. I tested these functions via the
7
following, and did NOT observe a memory leak. So it's maybe just a problem
8
with their sync functions, fortunately.
9
10
a = require('@cocalc/sync-fs/lib/compressed-json')
11
t=Date.now(); for(i=0;i<10000;i++) { await a.fromCompressedJSON(await a.toCompressedJSON({a:'x'.repeat(1000000)}))}; Date.now()-t
12
*/
13
14
import { compressFrame, decompressFrame } from "lz4-napi";
15
16
export async function toCompressedJSON(obj: any): Promise<Buffer> {
17
return await compressFrame(Buffer.from(JSON.stringify(obj)));
18
}
19
20
export async function fromCompressedJSON(compressedJSON: Buffer): Promise<any> {
21
return JSON.parse((await decompressFrame(compressedJSON)).toString());
22
}
23
24