Skip to content

Commit b8464f1

Browse files
committed
feat: produce error when source is incorrect
incorrect source could be: 🔸 invalid/not-compilable wasm binary 🔸 wasm code over 4KB when using export mode: instance or module ⚠️ Rust v1.29 and nightly produce ~57KB wasm code, smoke-test need to lock on v1.28 👉 https://developers.google.com/web/updates/2018/04/loading-wasm 👈
1 parent f80c74c commit b8464f1

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ rustup_nightly: &rustup_nightly
66
name: Install Cargo and Rust compiler
77
command: |
88
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> $BASH_ENV
9-
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly
9+
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.28.0
1010
source $BASH_ENV
11-
rustup target add wasm32-unknown-unknown --toolchain nightly
11+
rustup target add wasm32-unknown-unknown
1212
1313
unit_tests: &unit_tests
1414
steps:

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function(this: loader.LoaderContext, source: Buffer) {
1919
});
2020

2121
// if (options.export === 'buffer') return source; /*🤔*/
22-
return wasm2js(source, options.export!);
22+
return wasm2js(source, options.export!, errMsg => this.emitError(errMsg));
2323
};
2424

2525
module.exports.raw = true;

src/transform.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
import './__global';
22
import wrap from './wrapper';
33

4+
//#region helpers
5+
const is = (type: string) => ({
6+
oneOf: (types: string[]) => types.some(_ => _ === type)
7+
});
8+
function panic(message: string, cb?: (msg: string) => void) {
9+
if (cb) cb(message);
10+
else throw new Error(message);
11+
}
12+
//#endregion
13+
414
/** Transform WebAssembly binary into JS module
515
* @param source - WebAssembly Buffer
16+
* @param exportType - what type of export you want to generate
17+
* @param {Function} [errorCallback] - callback error in case you want to use your own error reporting
618
* @return string of the code
719
* @example const code = wasm2js(Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0, 0, 0]), 'instance')
820
*/
921
export default function(
1022
source: Buffer,
11-
exportType: Export.Type
23+
exportType: Export.Type,
24+
errorCallback?: (errorMessage: string) => void
1225
): string | never {
26+
if (WebAssembly.validate(source) === false)
27+
panic('Invalid WebAssembly file', errorCallback);
28+
29+
if (is(exportType).oneOf(['instance', 'module']) && source.byteLength > 4000)
30+
panic(
31+
`The buffer size is larger than 4KB. Consider using async-${exportType}`,
32+
errorCallback
33+
);
34+
1335
switch (exportType) {
1436
case 'buffer':
1537
return wrap(source).asBuffer;

0 commit comments

Comments
 (0)