Skip to content

Commit

Permalink
core: Simplify Promises.
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Kohn <[email protected]>
  • Loading branch information
paambaati and ankon committed Dec 5, 2020
1 parent 6e764dd commit 5efecce
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const lz4init = require('lz4-asm');
import lz4init from 'lz4-asm';
const lz4Module = {};
const lz4Ready = lz4init(lz4Module);

Expand Down Expand Up @@ -60,29 +60,19 @@ export default class LZ4Codec {
constructor(private options?: LZ4Options) {}

private async compress(encoder: { buffer: Buffer }): Promise<Buffer> {
return await new Promise<Buffer>((resolve) => {
return lz4Ready.then((lz4) => {
const lz4js = lz4.lz4js;
const compressedBuffer: Buffer = lz4js.compress(encoder.buffer, {
...this.options,
frameInfo: {
...this.options?.frameInfo,
blockMode: 1, // Turning off block mode won't work with KafkaJS.
},
});
return resolve(compressedBuffer);
});
const { lz4js } = await lz4Ready;
return lz4js.compress(encoder.buffer, {
...this.options,
frameInfo: {
...this.options?.frameInfo,
blockMode: 1, // Turning off block mode won't work with KafkaJS.
},
});
}

private async decompress(buffer: Buffer): Promise<Buffer> {
return await new Promise<Buffer>((resolve) => {
return lz4Ready.then((lz4) => {
const lz4js = lz4.lz4js;
const decompressedBuffer: Buffer = lz4js.decompress(buffer);
return resolve(decompressedBuffer);
});
});
const { lz4js } = await lz4Ready;
return lz4js.decompress(buffer);
}

/**
Expand Down

0 comments on commit 5efecce

Please sign in to comment.