Skip to content

Commit

Permalink
fix: export xxhashBase64Url from wasm (#5203)
Browse files Browse the repository at this point in the history
* fix: export xxhashBase64Url from wasm

* fix: set js_name for xxhash_base64_url function from wasm

---------

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
sapphi-red and lukastaegert committed Oct 15, 2023
1 parent d8b31a2 commit ee0639e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion browser/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line import/no-unresolved
export { parse, xxhash_base64_url as xxhashBase64Url } from '../../wasm/bindings_wasm.js';
export { parse, xxhashBase64Url } from '../../wasm/bindings_wasm.js';
4 changes: 4 additions & 0 deletions native.wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { parse, xxhashBase64Url } = require('./wasm-node/bindings_wasm.js');

exports.parse = parse;
exports.xxhashBase64Url = xxhashBase64Url;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@
"build:docs": "vitepress build docs",
"preview:docs": "vitepress preview docs",
"ci:artifacts": "napi artifacts",
"ci:lint": "concurrently -c red,green,blue 'npm:lint:js:nofix' 'npm:lint:markdown:nofix' 'npm:lint:rust:nofix'",
"ci:lint": "concurrently -c red,yellow,green,blue 'npm:lint:js:nofix' 'npm:lint:native-js' 'npm:lint:markdown:nofix' 'npm:lint:rust:nofix'",
"ci:test:only": "npm run build:cjs && npm run build:copy-native && npm run build:bootstrap && npm run build:copy-native && npm run test:only",
"ci:test:all": "npm run build:cjs && npm run build:copy-native && npm run build:bootstrap && npm run build:copy-native && concurrently --kill-others-on-fail -c green,blue,magenta,cyan 'npm:test:only' 'npm:test:typescript' 'npm:test:leak' 'npm:test:browser'",
"ci:coverage": "npm run build:cjs && npm run build:copy-native && npm run build:bootstrap && npm run build:copy-native && nyc --reporter lcovonly mocha",
"lint": "concurrently -c red,green,blue 'npm:lint:js' 'npm:lint:markdown' 'npm:lint:rust'",
"lint": "concurrently -c red,yellow,green,blue 'npm:lint:js' 'npm:lint:native-js' 'npm:lint:markdown' 'npm:lint:rust'",
"lint:js": "eslint . --fix --cache",
"lint:js:nofix": "eslint . --cache",
"lint:native-js": "node scripts/lint-native-js.js",
"lint:markdown": "prettier --write \"**/*.md\"",
"lint:markdown:nofix": "prettier --check \"**/*.md\"",
"lint:rust": "cd rust && cargo fmt && cargo clippy --fix --allow-dirty",
Expand Down
2 changes: 1 addition & 1 deletion rust/bindings_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn parse(code: String, allow_return_outside_function: bool) -> Vec<u8> {
parse_ast(code, allow_return_outside_function)
}

#[wasm_bindgen]
#[wasm_bindgen(js_name=xxhashBase64Url)]
pub fn xxhash_base64_url(input: Uint8Array) -> String {
xxhash::xxhash_base64_url(&input.to_vec())
}
29 changes: 29 additions & 0 deletions scripts/lint-native-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import url from 'node:url';

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const NATIVE_JS_PATH = path.resolve(__dirname, '../native.js');
const NATIVE_WASM_JS_PATH = path.resolve(__dirname, '../native.wasm.js');

const [nativeJsContent, nativeWasmJsContent] = await Promise.all([
fs.readFile(NATIVE_JS_PATH, 'utf8'),
fs.readFile(NATIVE_WASM_JS_PATH, 'utf8')
]);

const nativeJsExportsMatches = nativeJsContent.matchAll(/exports\.(\w+)\s*=/g);
const notFoundExports = [];
for (const match of nativeJsExportsMatches) {
const exportName = match[1];
if (!nativeWasmJsContent.includes(`exports.${exportName}`)) {
notFoundExports.push(exportName);
}
}

if (notFoundExports.length > 0) {
throw new Error(
`${JSON.stringify(
notFoundExports.join(',')
)} was exported from native.js but not exported from native.wasm.js.`
);
}
9 changes: 3 additions & 6 deletions scripts/publish-wasm-node-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ const WASM_NODE_PACKAGE_INFO = {
};
const COPIED_FILES_OR_DIRS = ['LICENSE.md', 'dist'];
const PACKAGE_DIR = 'wasm-node-package';
const NATIVE_JS_CONTENT = `
const { parse } = require('./wasm-node/bindings_wasm.js');
exports.parse = parse
`;

function getPath(...arguments_) {
return resolve(PACKAGE_DIR, ...arguments_);
Expand Down Expand Up @@ -43,8 +38,10 @@ export default async function publishWasmNodePackage() {
)
]);

const nativeJsContent = await fs.readFile(resolve(__dirname, '../native.wasm.js'), 'utf8');

await Promise.all([
fs.writeFile(getPath('dist', 'native.js'), NATIVE_JS_CONTENT.trimStart()),
fs.writeFile(getPath('dist', 'native.js'), nativeJsContent.trimStart()),
fs.cp('artifacts/bindings-wasm-node/wasm-node', getPath('dist', 'wasm-node'), {
recursive: true
})
Expand Down
4 changes: 2 additions & 2 deletions wasm/bindings_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function parse(code: string, allow_return_outside_function: boolean): Uin
* @param {Uint8Array} input
* @returns {string}
*/
export function xxhash_base64_url(input: Uint8Array): string;
export function xxhashBase64Url(input: Uint8Array): string;
/**
* @param {string} query
* @param {any} opts
Expand All @@ -23,7 +23,7 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly parse: (a: number, b: number, c: number, d: number) => void;
readonly xxhash_base64_url: (a: number, b: number) => void;
readonly xxhashBase64Url: (a: number, b: number) => void;
readonly browserslist: (a: number, b: number, c: number, d: number) => void;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
Expand Down
2 changes: 1 addition & 1 deletion wasm/bindings_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function parse(a: number, b: number, c: number, d: number): void;
export function xxhash_base64_url(a: number, b: number): void;
export function xxhashBase64Url(a: number, b: number): void;
export function browserslist(a: number, b: number, c: number, d: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
Expand Down

0 comments on commit ee0639e

Please sign in to comment.