diff --git a/browser/src/wasm.ts b/browser/src/wasm.ts index d57a0c8236d..03bf6168f5b 100644 --- a/browser/src/wasm.ts +++ b/browser/src/wasm.ts @@ -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'; diff --git a/native.wasm.js b/native.wasm.js new file mode 100644 index 00000000000..7af1fe75607 --- /dev/null +++ b/native.wasm.js @@ -0,0 +1,4 @@ +const { parse, xxhashBase64Url } = require('./wasm-node/bindings_wasm.js'); + +exports.parse = parse; +exports.xxhashBase64Url = xxhashBase64Url; diff --git a/package.json b/package.json index ff8fb84ddf7..eb9ac0478ee 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rust/bindings_wasm/src/lib.rs b/rust/bindings_wasm/src/lib.rs index 500176068d8..d84eb52ff5e 100644 --- a/rust/bindings_wasm/src/lib.rs +++ b/rust/bindings_wasm/src/lib.rs @@ -7,7 +7,7 @@ pub fn parse(code: String, allow_return_outside_function: bool) -> Vec { 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()) } diff --git a/scripts/lint-native-js.js b/scripts/lint-native-js.js new file mode 100644 index 00000000000..87936a90108 --- /dev/null +++ b/scripts/lint-native-js.js @@ -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.` + ); +} diff --git a/scripts/publish-wasm-node-package.js b/scripts/publish-wasm-node-package.js index 45426fa88b5..11f342baae8 100644 --- a/scripts/publish-wasm-node-package.js +++ b/scripts/publish-wasm-node-package.js @@ -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_); @@ -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 }) diff --git a/wasm/bindings_wasm.d.ts b/wasm/bindings_wasm.d.ts index 8823988c4fa..0f969452abe 100644 --- a/wasm/bindings_wasm.d.ts +++ b/wasm/bindings_wasm.d.ts @@ -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 @@ -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; diff --git a/wasm/bindings_wasm_bg.wasm.d.ts b/wasm/bindings_wasm_bg.wasm.d.ts index 08c257c7958..14398a0b64c 100644 --- a/wasm/bindings_wasm_bg.wasm.d.ts +++ b/wasm/bindings_wasm_bg.wasm.d.ts @@ -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;