Skip to content

Commit

Permalink
fix: allow importing as native browser module, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Feb 12, 2020
1 parent 7b37f12 commit 8c48e49
Show file tree
Hide file tree
Showing 19 changed files with 270 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TARGETS=nodejs browser
TARGETS=nodejs browser web
MODE=dev

RUST_WASM_SRC = $(wildcard rs/wasm/src/*.rs)
Expand Down
7 changes: 7 additions & 0 deletions browser-async.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as blake3 from './esm/browser';

/**
* Manually loads the WebAssembly module, returning a promise that resolves
* to the BLAKE3 implementation once available.
*/
export default function load(module: string | URL | Request | object): Promise<typeof blake3>;
20 changes: 20 additions & 0 deletions browser-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { provideWasm } from './esm/browser/wasm.js';
import * as wasm from './dist/wasm/web/blake3_js.js';
import * as blake3 from './esm/browser/index.js';

let cached;

/**
* Manually loads the WebAssembly module, returning a promise that resolves
* to the BLAKE3 implementation once available.
*/
export default function load(module) {
if (!cached) {
cached = wasm.default(module).then(() => {
provideWasm(wasm);
return blake3;
});
}

return cached;
}
17 changes: 17 additions & 0 deletions browser-async.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>blake3 import async test</title>
</head>

<body>
<script type="module">
import init from './browser-async.js';
init().then(blake3 => window.blake3 = blake3).catch(console.error);
</script>
</body>

</html>
5 changes: 5 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import { provideWasm } from './esm/browser/wasm';
import * as wasm from './dist/wasm/browser';

provideWasm(wasm);

export * from './esm/browser';
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.1 - 2019-02-11

- allow importing the module without a bundler or with tools like Parcel

## 2.1.0 - 2019-01-23

- add keyed hash and key derivation support (fixes [#2](https://github.com/connor4312/blake3/issues/2), [#9](https://github.com/connor4312/blake3/issues/9))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prepack": "make clean && make MODE=release && npm test && rimraf dist/native.node",
"test": "mocha --require source-map-support/register --recursive \"dist/**/*.test.js\" --timeout 5000",
"fmt": "make fmt",
"compile": "tsc && tsc -p tsconfig.esm.json",
"compile": "tsc && tsc -p tsconfig.esm.json && node dist/build/add-js-extensions",
"watch": "tsc --watch"
},
"repository": {
Expand Down
12 changes: 11 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,17 @@ using(hash.reader(), async reader => {

### Browser

The browser API can be imported via `import('blake3/browser')`.
The browser API can be imported via `import('blake3/browser')`, which works well with Webpack.

If you aren't using a bundler or using a more "pure" bundler like Parcel, you can import `blake3/browser-async` which exports a function to asynchronously load the WebAssembly code and resolves to the package contents.

```js
import load from 'blake3/brwoser-async';

load().then(blake3 => {
console.log(blake3.hash('hello world'));
});
```

#### `hash(data: BinaryLike, options?: { length: number }): Hash`

Expand Down
Loading

0 comments on commit 8c48e49

Please sign in to comment.