Skip to content

Commit

Permalink
Stop using readFileSync (#130)
Browse files Browse the repository at this point in the history
This ensures we don't block the event loop while reading files, which should result in a overall system performance improvement.
  • Loading branch information
libre-man authored Apr 24, 2024
1 parent b19b4b8 commit da145f6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/node-esm-dict/index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { init, decompressUsingDict, createDCtx, compressUsingDict, createCCtx } from '@bokuweb/zstd-wasm';
import { readFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { resolve, dirname } from 'path';

(async () => {
await init();
const buf = Buffer.from('Hello zstd!!');
const dir = dirname(new URL(import.meta.url).pathname)
const dict = readFileSync(resolve(dir, './json-dict'));
const dict = await readFile(resolve(dir, './json-dict'));
const compressed = compressUsingDict(createCCtx(), buf, dict, 10);
const decompressed = decompressUsingDict(createDCtx(), compressed, dict);
const res = Buffer.from(decompressed).toString();
Expand Down
4 changes: 2 additions & 2 deletions lib/index.node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Module, waitInitialized } from './module';

export const init = async () => {
const { readFileSync } = require('fs');
const { readFile } = require('fs/promises');
const { resolve } = require('path');
const buf = readFileSync(resolve(__dirname, './wasm/zstd.wasm'));
const buf = await readFile(resolve(__dirname, './wasm/zstd.wasm'));
Module['init'](buf);
await waitInitialized();
};
Expand Down
6 changes: 3 additions & 3 deletions test/compress_using_dict.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { resolve } from 'path';

import { decompressUsingDict, createDCtx } from '../lib/simple/decompress_using_dict';
Expand All @@ -7,8 +7,8 @@ import { init } from '../lib/index.node';

test('using dict', async () => {
await init();
const buf = readFileSync(resolve(__dirname, './mock/mock.json'));
const dict = readFileSync(resolve(__dirname, './json-dict'));
const buf = await readFile(resolve(__dirname, './mock/mock.json'));
const dict = await readFile(resolve(__dirname, './json-dict'));
const compressed = compressUsingDict(createCCtx(), buf, dict, 10);
const decompressed = decompressUsingDict(createDCtx(), compressed, dict);
expect(buf.toString()).toBe(Buffer.from(decompressed).toString());
Expand Down
4 changes: 2 additions & 2 deletions test/largefile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { resolve } from 'path';

import { decompress } from '../lib/simple/decompress';
Expand All @@ -7,7 +7,7 @@ import { init } from '../lib/index.node';

test('largefile', async () => {
await init();
const buf = readFileSync(resolve(__dirname, './large-file'));
const buf = await readFile(resolve(__dirname, './large-file'));
const compressed = compress(buf, 10);
const decompressed = decompress(compressed);
expect(buf.equals(Buffer.from(decompressed))).toBeTruthy();
Expand Down

0 comments on commit da145f6

Please sign in to comment.