Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit c216cea

Browse files
committed
feat: support browsers
1 parent edf3478 commit c216cea

File tree

16 files changed

+288
-58
lines changed

16 files changed

+288
-58
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,27 @@ jobs:
1919
cache: 'yarn'
2020
- name: Install dependencies with Yarn
2121
run: yarn install --immutable
22-
- name: Build TypeScript
23-
run: yarn run build
24-
- name: Upload compiled TypeScript
25-
uses: actions/upload-artifact@v2
26-
with:
27-
name: dist
28-
path: dist
2922
- name: Build WASM
3023
run: yarn run asbuild
3124
- name: Upload compiled TypeScript
3225
uses: actions/upload-artifact@v2
3326
with:
3427
name: wasm
3528
path: wasm
29+
- name: Bundle
30+
run: yarn run build
31+
- name: Upload bundle
32+
uses: actions/upload-artifact@v2
33+
with:
34+
name: dist
35+
path: dist
36+
- name: Generate typings
37+
run: yarn run types
38+
- name: Upload typings
39+
uses: actions/upload-artifact@v2
40+
with:
41+
name: types
42+
path: tsc_types
3643
docs:
3744
name: Generate docs
3845

@@ -49,7 +56,7 @@ jobs:
4956
cache: 'yarn'
5057
- name: Install dependencies with Yarn
5158
run: yarn install --immutable
52-
- name: Build TypeScript
59+
- name: Generate docs
5360
run: yarn run docs
5461
- name: Upload compiled TypeScript
5562
uses: actions/upload-artifact@v2
@@ -134,8 +141,8 @@ jobs:
134141
cache: 'yarn'
135142
- name: Install dependencies with Yarn
136143
run: yarn install --immutable
137-
- name: Build TypeScript
138-
run: yarn run build
144+
- name: Generate typings
145+
run: yarn run types
139146
- name: Validate API
140147
run: yarn run validate-api
141148
release:
@@ -162,12 +169,14 @@ jobs:
162169
cache: 'yarn'
163170
- name: Install dependencies with Yarn
164171
run: yarn install --immutable
172+
- name: Build WASM
173+
run: yarn run asbuild
165174
- name: Build TypeScript
166175
run: yarn run build
176+
- name: Generate typings
177+
run: yarn run types
167178
- name: Generate .d.ts rollup
168179
run: yarn run validate-api
169-
- name: Build WASM
170-
run: yarn run asbuild
171180
- name: Release
172181
run: yarn run release
173182
env:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,6 @@ $RECYCLE.BIN/
244244

245245
# Generated documentation
246246
/docs_out
247+
248+
# Generated typings
249+
/tsc_types

.prettierignore

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,12 @@ assembly/hash*.ts
232232
# Autogenerated
233233
src/types/wasm-raw.ts
234234

235-
# Generated documentation
236-
/docs_out
237-
238235
# API Extractor
239236
/temp
240237
/docs/api.md
238+
239+
# Generated documentation
240+
/docs_out
241+
242+
# Generated typings
243+
/tsc_types

api-extractor.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*
4646
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
4747
*/
48-
"mainEntryPointFilePath": "<projectFolder>/dist/src/index.d.ts",
48+
"mainEntryPointFilePath": "<projectFolder>/tsc_types/src/index.d.ts",
4949

5050
/**
5151
* A list of NPM package names whose exports should be treated as part of this package.

docs/api.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
/// <reference types="node" />
88

9-
import { Buffer as Buffer_2 } from 'node:buffer';
10-
119
// @public
12-
function hash32(key: ArrayBufferLike, seed: number): Buffer_2;
10+
function hash32(key: ArrayBufferLike, seed: number): Buffer;
1311

1412
declare namespace MurmurHash3 {
1513
export {

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ module.exports = {
8080
// ],
8181

8282
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
83-
// moduleNameMapper: {},
83+
moduleNameMapper: {
84+
'mod.wasm': '<rootDir>/test/wasm.js',
85+
},
8486

8587
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
8688
// modulePathIgnorePatterns: [],

package.json

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"name": "murmurhash-wasm",
33
"version": "0.0.0-development",
4-
"description": "MurmurHash implementations in WASM",
4+
"description": "MurmurHash in WASM for Node.js and the browser",
55
"keywords": [
66
"murmurhash",
77
"murmurhash3",
88
"crypto",
9-
"wasm"
9+
"wasm",
10+
"hashing",
11+
"hash"
1012
],
1113
"homepage": "https://murmurhash-wasm.jonah.pw",
1214
"bugs": {
@@ -24,28 +26,36 @@
2426
},
2527
"sideEffects": false,
2628
"type": "commonjs",
27-
"exports": "./dist/src/index.js",
28-
"main": "./dist/src/index.js",
29-
"types": "./dist/src/index.d.ts",
29+
"exports": {
30+
"node": "./dist/node.cjs",
31+
"default": "./dist/index.js",
32+
"import": "./dist/index.mjs",
33+
"require": "./dist/index.js"
34+
},
35+
"main": "./dist/index.js",
36+
"types": "./dist/index.d.ts",
3037
"files": [
3138
"dist",
32-
"!dist/test",
33-
"!dist/**/*.d.ts",
34-
"dist/index.d.ts",
35-
"wasm"
39+
"wasm/mod.wat"
3640
],
3741
"scripts": {
3842
"asbuild": "yarn asbuild:optimized",
3943
"asbuild:optimized": "asc assembly/index.ts --target release",
4044
"asbuild:untouched": "asc assembly/index.ts --target debug",
4145
"prebuild": "rm -rf dist",
42-
"build": "yarn run prebuild && tsc",
46+
"build": "yarn build:esm && yarn build:cjs && yarn build:node",
47+
"build:base": "esbuild --bundle --loader:.wasm=binary --external:@assemblyscript/loader src/index.ts --sourcemap=inline",
48+
"build:cjs": "yarn run build:base --outfile=dist/index.js --format=cjs",
49+
"build:esm": "yarn run build:base --outfile=dist/index.mjs --format=esm",
50+
"build:node": "yarn run build:base --outfile=dist/node.cjs --format=cjs --target=node16.9.0 ",
4351
"docs": "typedoc",
4452
"lint": "xo",
4553
"release": "semantic-release",
4654
"style": "prettier --check .",
4755
"test": "jest",
4856
"test:coverage": "jest --coverage",
57+
"pretypes": "rm -rf tsc_types",
58+
"types": "tsc",
4959
"validate-api": "api-extractor run",
5060
"validate-api:local": "api-extractor run --local --verbose"
5161
},
@@ -66,6 +76,7 @@
6676
"assemblyscript": "0.19.22",
6777
"babel-jest": "27.4.6",
6878
"enhanced-resolve": "5.8.3",
79+
"esbuild": "0.14.11",
6980
"jest": "27.4.7",
7081
"prettier": "2.5.1",
7182
"semantic-release": "18.0.1",

readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
55
[![codecov](https://codecov.io/gh/jonahsnider/murmurhash-wasm/branch/main/graph/badge.svg)](https://codecov.io/jonahsnider/murmurhash-wasm)
66

7-
MurmurHash implementations in WASM.
7+
MurmurHash in WASM for Node.js and the browser.
88

99
Generated documentation is here: <https://murmurhash-wasm.jonah.pw>.
1010

11+
The WASM is inlined into the JS file to prevent issues from loading the module.
12+
1113
## Usage
1214

1315
### MurmurHash3
@@ -35,3 +37,7 @@ npm install murmurhash-wasm
3537
# or
3638
yarn add murmurhash-wasm
3739
```
40+
41+
### Browsers
42+
43+
When running in the browser you will to polyfill `Buffer`.

src/murmur-hash3/hash-32.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint-disable no-bitwise */
22

3-
import assert from 'assert/strict';
4-
import {Buffer} from 'buffer';
53
import {UINT32_MAX_VALUE} from '../constants';
64
import {wasm} from '../wasm';
75

@@ -31,11 +29,9 @@ import {wasm} from '../wasm';
3129
* @public
3230
*/
3331
export function hash32(key: ArrayBufferLike, seed: number): Buffer {
34-
assert.equal(
35-
seed >= 0 && seed <= UINT32_MAX_VALUE,
36-
true,
37-
new RangeError(`The value of "value" is out of range. It must be >= 0 and <= ${UINT32_MAX_VALUE}.`),
38-
);
32+
if (seed < 0 || seed > UINT32_MAX_VALUE) {
33+
throw new RangeError(`The value of "value" is out of range. It must be >= 0 and <= ${UINT32_MAX_VALUE}.`);
34+
}
3935

4036
const keyPointer = wasm.__pin(wasm.__newArrayBuffer(key));
4137

src/wasm.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
import fs from 'fs';
2-
import path from 'path';
31
import {instantiateSync} from '@assemblyscript/loader';
2+
import wasmBuffer from '../wasm/mod.wasm';
43
import type * as Wasm from './types/wasm.js';
54

6-
let PROJECT_ROOT = path.join(__dirname, '..');
7-
8-
if (path.basename(PROJECT_ROOT) === 'dist') {
9-
PROJECT_ROOT = path.join(PROJECT_ROOT, '..');
10-
}
11-
12-
const WASM_PATH = path.join(PROJECT_ROOT, 'wasm', 'mod.wasm');
13-
14-
const wasmModule = instantiateSync<Wasm.Exports>(fs.readFileSync(WASM_PATH));
5+
const wasmModule = instantiateSync<Wasm.Exports>(wasmBuffer);
156

167
export const wasm = wasmModule.exports;

0 commit comments

Comments
 (0)