diff --git a/docs/guide/bkon.md b/docs/guide/bkon.md index e8d592326..01f49eafe 100644 --- a/docs/guide/bkon.md +++ b/docs/guide/bkon.md @@ -5,7 +5,7 @@ A compact binary representation similar to JSON objects. This documentation uses 1. JSON compressed as BKON, written directly to disk (a direct 50% reduction). 2. BKON further compressed with LZ4 (a clean 90% reduction). 3. The raw JSON file as fetched from a random data source. -4. BKON decompressed once again to JSON. Note that this file is a little larger than the original JSON file as it contains a lot more decimal places due to float conversion precision. +4. BKON decompressed once again to JSON. Note that this file is a little larger than the original JSON file as it contains a lot more decimal places due to float precision. ![an image of file sizes](https://i.imgur.com/9rBnOuS.png) diff --git a/src/core/streams/bkon.ts b/src/core/streams/bkon.ts index 742fd728e..70fe83a3c 100644 --- a/src/core/streams/bkon.ts +++ b/src/core/streams/bkon.ts @@ -1,4 +1,6 @@ +import { writeFile } from 'fs/promises'; import { times } from 'lodash'; +import { decode, encode } from 'lz4'; import { ReadStream, WriteStream } from './buffer'; enum ValueType { @@ -279,3 +281,23 @@ export class BkonWriteStream extends WriteStream { this.ascii('BKON'); } } + +const write = new BkonWriteStream(); +const data = (await fetch( + 'https://raw.githubusercontent.com/tresabhi/blitzkrieg-assets/dev/definitions/tanks.json', +).then((response) => response.json())) as Value; +write.bkon(data); +const buffer = write.buffer; +const read = new BkonReadStream(buffer); +read.bkon(); +const lz4 = encode(buffer); + +writeFile('test.bkon', buffer); +writeFile('test.json', JSON.stringify(data)); +writeFile('test.bkon.lz4', lz4); + +// now we attempt to read it +writeFile( + 'test.read.json', + JSON.stringify(new BkonReadStream(decode(lz4)).bkon()), +);