Skip to content

Commit a1d6719

Browse files
authored
Added tests and improved bundle size (#7)
Added tests and improved bundle size
2 parents 0452c24 + 182302b commit a1d6719

File tree

6 files changed

+60
-13
lines changed

6 files changed

+60
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A small library to detect which features of WebAssembly are supported.
66
- ✅ Runs in Node
77
- ✅ Provided as an ES6 module, CommonJS and UMD module.
88
- ✅ CSP compatible
9-
- ✅ Only ~520B gzipped
9+
- ✅ Only ~504B gzipped
1010

1111
## Installation
1212

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"build": "npm run build:library && npm run build:readme && npm run fmt",
1111
"build:website": "npm run build && node genwebsite.js",
1212
"build:website:netlify": "./build.sh",
13-
"fmt": "prettier --write ./{,{src,rollup-plugins}/**/}*.{mjs,js,md}"
13+
"fmt": "prettier --write ./{,{src,rollup-plugins}/**/}*.{mjs,js,md}",
14+
"fmt_test": "test $(prettier -l ./{,{src,rollup-plugins}/**/}*.{mjs,js,md} | wc -l) -eq 0",
15+
"test": "npm run fmt_test && npm run build && node --no-warnings test/index.js"
1416
},
1517
"repository": "GoogleChromeLabs/wasm-feature-detect",
1618
"keywords": [],

rollup-plugins/index-generator.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,16 @@ export default function({ indexPath, pluginFolder }) {
4646
flags
4747
))
4848
]);
49+
const pluginName = camelCaseify(plugin);
4950
if (await fileExists(`./src/${pluginFolder}/${plugin}/index.js`)) {
51+
const importName = `${pluginName}_internal`;
5052
return `
51-
import { default as ${camelCaseify(
52-
plugin
53-
)}_internal } from "./${pluginFolder}/${plugin}/index.js";
54-
export async function ${camelCaseify(plugin)}() {
55-
return (await Promise.all([validate(${module}), ${camelCaseify(
56-
plugin
57-
)}_internal()])).every(x => x);
58-
}
53+
import { default as ${importName} } from "./${pluginFolder}/${plugin}/index.js";
54+
export const ${pluginName} = ${importName}.bind(null, new Uint8Array(${module}), validate);
5955
`;
6056
}
6157
return `
62-
export const ${camelCaseify(plugin)} = validate.bind(null, ${module});
58+
export const ${pluginName} = validate.bind(null, new Uint8Array(${module}));
6359
`;
6460
})
6561
);

src/detectors/threads/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
* limitations under the License.
1212
*/
1313

14-
export default async function() {
14+
export default async function(module, validate) {
15+
if (!(await validate(module))) return false;
1516
try {
1617
// Test for transferability of SABs (needed for Firefox)
1718
// https://groups.google.com/forum/#!msg/mozilla.dev.platform/IHkBZlHETpA/dwsMNchWEQAJ

src/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
// be called quite often and by having our own function terser can give it
1616
// a one-letter name.
1717
export async function validate(module) {
18-
return WebAssembly.validate(new Uint8Array(module));
18+
return WebAssembly.validate(module);
1919
}

test/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2019 Google Inc. All Rights Reserved.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
const assert = require('assert');
15+
16+
process.on("unhandledRejection", err => {
17+
throw err;
18+
});
19+
20+
// We try to import the generated module
21+
const features = require(__dirname+'/../dist/cjs');
22+
23+
const isBoolean = function (value) {
24+
return typeof value === 'boolean';
25+
}
26+
27+
const checkFeature = async function (featureName) {
28+
const feature = features[featureName];
29+
assert(feature, `The feature ${featureName} doesn't exist`);
30+
const result = await feature();
31+
console.log(`The feature ${featureName} returned: ${result}`);
32+
assert(isBoolean(result), `The feature ${featureName} returned: ${result}`);
33+
}
34+
35+
async function run() {
36+
await checkFeature("bulkMemory");
37+
await checkFeature("exceptions");
38+
await checkFeature("multiValue");
39+
await checkFeature("mutableGlobals");
40+
await checkFeature("referenceTypes");
41+
await checkFeature("saturatedFloatToInt");
42+
await checkFeature("signExtensions");
43+
await checkFeature("simd");
44+
await checkFeature("tailCall");
45+
await checkFeature("threads");
46+
}
47+
48+
run();

0 commit comments

Comments
 (0)