Skip to content

Commit 8542159

Browse files
committed
Add init function and update type definitions for BrotliWasm
1 parent f110150 commit 8542159

File tree

5 files changed

+158
-88
lines changed

5 files changed

+158
-88
lines changed

index.browser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ module.exports.default = module.exports;
99

1010
// Without this, ts-loader gets annoyed by imports for the pure type. Clear ts-loader bug,
1111
// but this is a quick & easy fix on our end:
12-
module.exports.BrotliWasmType = undefined;
12+
module.exports.BrotliWasmType = undefined;
13+
14+
module.exports.init = (_) => {
15+
return globalThis.Promise.resolve();
16+
}

index.d.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,46 @@ export * from './pkg.node/brotli_wasm';
66
declare const promisedValue: Promise<typeof BrotliWasm>;
77
export default promisedValue;
88

9-
export type BrotliWasmType = typeof BrotliWasm;
9+
export type BrotliWasmType = typeof BrotliWasm;
10+
11+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
12+
13+
export interface InitOutput {
14+
readonly memory: WebAssembly.Memory;
15+
readonly __wbg_brotlistreamresult_free: (a: number) => void;
16+
readonly __wbg_get_brotlistreamresult_code: (a: number) => number;
17+
readonly __wbg_set_brotlistreamresult_code: (a: number, b: number) => void;
18+
readonly __wbg_get_brotlistreamresult_buf: (a: number, b: number) => void;
19+
readonly __wbg_set_brotlistreamresult_buf: (a: number, b: number, c: number) => void;
20+
readonly __wbg_get_brotlistreamresult_input_offset: (a: number) => number;
21+
readonly __wbg_set_brotlistreamresult_input_offset: (a: number, b: number) => void;
22+
readonly __wbg_compressstream_free: (a: number) => void;
23+
readonly compressstream_new: (a: number, b: number) => number;
24+
readonly compressstream_compress: (a: number, b: number, c: number, d: number, e: number) => void;
25+
readonly compressstream_total_out: (a: number) => number;
26+
readonly __wbg_decompressstream_free: (a: number) => void;
27+
readonly decompressstream_new: () => number;
28+
readonly decompressstream_decompress: (a: number, b: number, c: number, d: number, e: number) => void;
29+
readonly decompressstream_total_out: (a: number) => number;
30+
readonly compress: (a: number, b: number, c: number, d: number) => void;
31+
readonly decompress: (a: number, b: number, c: number) => void;
32+
readonly __wbindgen_malloc: (a: number) => number;
33+
readonly __wbindgen_realloc: (a: number, b: number, c: number) => number;
34+
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
35+
readonly __wbindgen_free: (a: number, b: number) => void;
36+
}
37+
38+
/**
39+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
40+
* for everything else, calls `WebAssembly.instantiate` directly.
41+
*
42+
* If this project's target is `cloudflare-workers`, you must use this.
43+
* If you use web, this function will return a promise that resolves to the
44+
* `BrotliWasm` module, which you can use directly.
45+
* But if you use other targets, this function will return the void type.
46+
*
47+
* @param {InitInput | Promise<InitInput>} module_or_path
48+
*
49+
* @returns {Promise<InitOutput>}
50+
*/
51+
export function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput | void>;

index.node.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ const nodePkg = require('./pkg.node/brotli_wasm');
33
module.exports = nodePkg;
44

55
// In addition though, we provide a default export, to match the pure ESM web bundle:
6-
module.exports.default = Promise.resolve(nodePkg);
6+
module.exports.default = Promise.resolve(nodePkg);
7+
8+
module.exports.init = (_) => {
9+
return globalThis.Promise.resolve();
10+
}

index.web.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
// For usage with an importmap, it's convenient to add the ".js" extension here, because browsers
66
// don't try to guess the file extension.
77
import init, * as brotliWasm from "./pkg.web/brotli_wasm.js";
8-
export default init().then(() => brotliWasm);
8+
export default init().then(() => brotliWasm);
9+
10+
export { init };

package.json

Lines changed: 102 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,105 @@
11
{
2-
"name": "brotli-wasm",
3-
"version": "3.0.1",
4-
"description": "A reliable compressor and decompressor for Brotli, supporting node & browsers via wasm",
5-
"types": "./index.d.ts",
6-
"main": "./index.node.js",
7-
"browser": "./index.browser.js",
8-
"exports": {
9-
".": {
10-
"types": "./index.d.ts",
11-
"import": "./index.web.js",
12-
"browser": "./index.browser.js",
13-
"require": "./index.node.js",
14-
"default": "./index.web.js"
2+
"name": "brotli-wasm",
3+
"version": "3.0.1",
4+
"description": "A reliable compressor and decompressor for Brotli, supporting node & browsers via wasm",
5+
"types": "./index.d.ts",
6+
"main": "./index.node.js",
7+
"browser": "./index.browser.js",
8+
"exports": {
9+
".": {
10+
"types": "./index.d.ts",
11+
"import": "./index.web.js",
12+
"browser": "./index.browser.js",
13+
"require": "./index.node.js",
14+
"default": "./index.web.js"
15+
},
16+
"./wasm": {
17+
"import": {
18+
"types": "./pkg.web/brotli_wasm_bg.wasm.d.ts",
19+
"default": "./pkg.web/brotli_wasm_bg.wasm"
20+
},
21+
"browser": {
22+
"types": "./pkg.bundler/brotli_wasm_bg.wasm.d.ts",
23+
"default": "./pkg.bundler/brotli_wasm_bg.wasm"
24+
},
25+
"require": {
26+
"types": "./pkg.node/brotli_wasm_bg.wasm.d.ts",
27+
"default": "./pkg.node/brotli_wasm_bg.wasm"
28+
},
29+
"default": {
30+
"types": "./pkg.web/brotli_wasm_bg.wasm.d.ts",
31+
"default": "./pkg.web/brotli_wasm_bg.wasm"
32+
}
33+
}
34+
},
35+
"sideEffects": false,
36+
"files": [
37+
"pkg.node",
38+
"pkg.bundler",
39+
"pkg.web",
40+
"index.node.js",
41+
"index.browser.js",
42+
"index.web.js",
43+
"index.d.ts"
44+
],
45+
"scripts": {
46+
"build": "node ./build.js",
47+
"pretest": "npm run build",
48+
"test": "npm run test:node && npm run test:esm && npm run test:webpack",
49+
"test:node": "ts-mocha -p test/tsconfig.json 'test/**/*.spec.ts'",
50+
"test:webpack": "karma start ./karma-webpack.conf.js",
51+
"test:esm": "karma start ./karma-esm.conf.js",
52+
"test:webpack:debug": "npm run test:webpack -- --single-run=false --browsers Chrome",
53+
"test:esm:debug": "npm run test:esm -- --single-run=false --browsers Chrome"
54+
},
55+
"repository": {
56+
"type": "git",
57+
"url": "git+ssh://[email protected]/httptoolkit/brotli-wasm.git"
58+
},
59+
"keywords": [
60+
"brotli",
61+
"wasm",
62+
"compression",
63+
"decompression"
64+
],
65+
"author": "Tim Perry <[email protected]>",
66+
"license": "Apache-2.0",
67+
"bugs": {
68+
"url": "https://github.com/httptoolkit/brotli-wasm/issues"
69+
},
70+
"homepage": "https://github.com/httptoolkit/brotli-wasm#readme",
71+
"engines": {
72+
"node": ">=v18.0.0"
73+
},
74+
"devDependencies": {
75+
"@peculiar/webcrypto": "^1.4.0",
76+
"@types/atob": "^2.1.2",
77+
"@types/btoa": "^1.2.3",
78+
"@types/chai": "^4.2.18",
79+
"@types/mocha": "^8.2.2",
80+
"@types/node": "^20.12.7",
81+
"@types/text-encoding": "0.0.36",
82+
"atob": "^2.1.2",
83+
"btoa": "^1.2.1",
84+
"buffer": "^6.0.3",
85+
"chai": "^4.3.4",
86+
"karma": "^6.3.2",
87+
"karma-chai": "^0.1.0",
88+
"karma-chrome-launcher": "^3.1.0",
89+
"karma-mocha": "^2.0.1",
90+
"karma-sourcemap-loader": "^0.3.8",
91+
"karma-spec-reporter": "0.0.32",
92+
"karma-typescript": "^5.5.1",
93+
"karma-vite": "^1.0.1",
94+
"karma-webpack": "^5.0.0",
95+
"mocha": "^8.4.0",
96+
"shelljs": "^0.8.4",
97+
"text-encoding": "^0.7.0",
98+
"ts-loader": "^9.2.1",
99+
"ts-mocha": "^10.0.0",
100+
"ts-node": "^9.1.1",
101+
"typescript": "^4.2.4",
102+
"wasm-pack": "^0.10.3",
103+
"webpack": "^5.37.1"
15104
}
16-
},
17-
"sideEffects": false,
18-
"files": [
19-
"pkg.node",
20-
"pkg.bundler",
21-
"pkg.web",
22-
"index.node.js",
23-
"index.browser.js",
24-
"index.web.js",
25-
"index.d.ts"
26-
],
27-
"scripts": {
28-
"build": "node ./build.js",
29-
"pretest": "npm run build",
30-
"test": "npm run test:node && npm run test:esm && npm run test:webpack",
31-
"test:node": "ts-mocha -p test/tsconfig.json 'test/**/*.spec.ts'",
32-
"test:webpack": "karma start ./karma-webpack.conf.js",
33-
"test:esm": "karma start ./karma-esm.conf.js",
34-
"test:webpack:debug": "npm run test:webpack -- --single-run=false --browsers Chrome",
35-
"test:esm:debug": "npm run test:esm -- --single-run=false --browsers Chrome"
36-
},
37-
"repository": {
38-
"type": "git",
39-
"url": "git+ssh://[email protected]/httptoolkit/brotli-wasm.git"
40-
},
41-
"keywords": [
42-
"brotli",
43-
"wasm",
44-
"compression",
45-
"decompression"
46-
],
47-
"author": "Tim Perry <[email protected]>",
48-
"license": "Apache-2.0",
49-
"bugs": {
50-
"url": "https://github.com/httptoolkit/brotli-wasm/issues"
51-
},
52-
"homepage": "https://github.com/httptoolkit/brotli-wasm#readme",
53-
"engines": {
54-
"node": ">=v18.0.0"
55-
},
56-
"devDependencies": {
57-
"@peculiar/webcrypto": "^1.4.0",
58-
"@types/atob": "^2.1.2",
59-
"@types/btoa": "^1.2.3",
60-
"@types/chai": "^4.2.18",
61-
"@types/mocha": "^8.2.2",
62-
"@types/node": "^20.12.7",
63-
"@types/text-encoding": "0.0.36",
64-
"atob": "^2.1.2",
65-
"btoa": "^1.2.1",
66-
"buffer": "^6.0.3",
67-
"chai": "^4.3.4",
68-
"karma": "^6.3.2",
69-
"karma-chai": "^0.1.0",
70-
"karma-chrome-launcher": "^3.1.0",
71-
"karma-mocha": "^2.0.1",
72-
"karma-sourcemap-loader": "^0.3.8",
73-
"karma-spec-reporter": "0.0.32",
74-
"karma-typescript": "^5.5.1",
75-
"karma-vite": "^1.0.1",
76-
"karma-webpack": "^5.0.0",
77-
"mocha": "^8.4.0",
78-
"shelljs": "^0.8.4",
79-
"text-encoding": "^0.7.0",
80-
"ts-loader": "^9.2.1",
81-
"ts-mocha": "^10.0.0",
82-
"ts-node": "^9.1.1",
83-
"typescript": "^4.2.4",
84-
"wasm-pack": "^0.10.3",
85-
"webpack": "^5.37.1"
86-
}
87105
}

0 commit comments

Comments
 (0)