-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bfj_compression.ts
More file actions
38 lines (30 loc) · 1009 Bytes
/
test_bfj_compression.ts
File metadata and controls
38 lines (30 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import bfj from "bfj";
import fs from "fs";
import zlib from "zlib";
const testFile = "test_compression.json.gz";
const data = { hello: "world" };
async function test() {
try {
// Test Write
const outStream = fs.createWriteStream(testFile);
const gzip = zlib.createGzip();
outStream.on("error", console.error);
gzip.pipe(outStream);
// bfj.streamify returns a stream
const bfjStream = bfj.streamify(data);
bfjStream.pipe(gzip);
await new Promise(fulfill => outStream.on("finish", fulfill));
console.log("Write done");
// Test Read
const inStream = fs.createReadStream(testFile);
const gunzip = zlib.createGunzip();
inStream.pipe(gunzip);
const result = await bfj.parse(gunzip);
console.log("Read result:", result);
} catch (e) {
console.error(e);
} finally {
if (fs.existsSync(testFile)) fs.unlinkSync(testFile);
}
}
test();