Skip to content

Commit 4dffa26

Browse files
committed
feat(feedback): address feedback, add index.web.js, clean up test implementation
1 parent 279d296 commit 4dffa26

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ and you can call it in your code like this:
9797
import init, { decompress } from "brotli-wasm/pkg.web/brotli_wasm";
9898

9999
const initPromise = init("brotli_wasm_bg.wasm");
100-
export const brotliDecompress = zlib.brotliDecompress
101-
? promisify(zlib.brotliDecompress)
102-
: async (buffer: Uint8Array): Promise<Uint8Array | undefined> => {
100+
export const brotliDecompress = async (buffer: Uint8Array): Promise<Uint8Array | undefined> => {
103101
try {
104102
await initPromise;
105103
const output = decompress(buffer);

index.web.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { InitInput, InitOutput } from './pkg.web/brotli_wasm';
2+
import * as BrotliTypes from './pkg.web/brotli_wasm';
3+
export default function init(
4+
module_or_path?: InitInput | Promise<InitInput>
5+
): Promise<typeof BrotliTypes>;

index.web.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// The web version needs to be explicitly initiated, but we want the API to be the same
2+
// as the bundler version, so we write this small wrapper to make it work.
3+
import init, * as brotliWasm from "./pkg.web/brotli_wasm";
4+
5+
export default initOpts => init(initOpts).then(() => brotliWasm);

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"pkg.node",
1111
"pkg.bundler",
1212
"pkg.web",
13-
"index.browser.js"
13+
"index.browser.js",
14+
"index.web.d.ts",
15+
"index.web.js"
1416
],
1517
"scripts": {
1618
"build": "node ./build.js",

test/brotli.spec.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,27 @@ if (typeof global.TextEncoder === 'undefined') {
88

99
import { expect } from 'chai';
1010
import * as brotliPromise from '..';
11-
import init from '../pkg.web/brotli_wasm';
12-
import * as brotliWeb from '../pkg.web/brotli_wasm';
11+
import init from '../index.web.js';
1312
import * as fs from 'fs';
1413
import * as path from 'path';
1514

1615
const webWasmPath = path.join(__dirname, '../pkg.web/brotli_wasm_bg.wasm');
1716

17+
const bundlerName = 'bundler';
18+
const webName = 'web (vite compatible)';
19+
const canReadFile = typeof fs.readFileSync !== 'undefined';
20+
const initOpts = canReadFile ? fs.readFileSync(webWasmPath) : undefined;
1821
describe('Brotli-wasm', () => {
1922
let brotli!: typeof import('..');
20-
for (let i = 0; i < 2; i++) {
21-
let bundlerToUse = i === 0 ? 'bundler' : 'web (vite compatible)';
23+
24+
for (const bundlerToUse of [bundlerName, webName]) {
2225
let bundlerTestName = `Brotli ${bundlerToUse}`;
2326
describe(bundlerTestName, function () {
2427
beforeEach(async () => {
25-
if (i === 0) {
28+
if (bundlerToUse === bundlerName) {
2629
brotli = await brotliPromise;
2730
} else {
28-
brotli = brotliWeb;
29-
}
30-
if (typeof fs.readFileSync === 'undefined') {
31-
await init();
32-
} else {
33-
await init(fs.readFileSync(webWasmPath));
31+
brotli = await init(initOpts);
3432
}
3533
});
3634

0 commit comments

Comments
 (0)