diff --git a/Cargo.lock b/Cargo.lock index 158df3c..b648147 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atomic-polyfill" version = "1.0.3" @@ -161,6 +172,12 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" version = "1.2.48" @@ -1009,9 +1026,9 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" -version = "0.3.77" +version = "0.3.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" dependencies = [ "once_cell", "wasm-bindgen", @@ -1029,6 +1046,12 @@ version = "0.2.174" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + [[package]] name = "litemap" version = "0.7.4" @@ -1138,9 +1161,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "minicov" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27fe9f1cc3c22e1687f9446c2083c4c5fc7f0bcf1c7a86bdbded14985895b4b" +checksum = "4869b6a491569605d66d3952bcdf03df789e5b536e5f0cf7758a7f08a55ae24d" dependencies = [ "cc", "walkdir", @@ -1187,12 +1210,28 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", + "libm", +] + [[package]] name = "once_cell" version = "1.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + [[package]] name = "opaque-debug" version = "0.3.1" @@ -2422,37 +2461,25 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.100" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" dependencies = [ "cfg-if", "once_cell", "rustversion", "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.50" +version = "0.4.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61" +checksum = "70a6e77fd0ae8029c9ea0063f87c46fde723e7d887703d74ad2616d792e51e6f" dependencies = [ "cfg-if", + "futures-util", "js-sys", "once_cell", "wasm-bindgen", @@ -2461,9 +2488,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.100" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2471,55 +2498,70 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.100" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" dependencies = [ + "bumpalo", "proc-macro2", "quote", "syn", - "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.100" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" dependencies = [ "unicode-ident", ] [[package]] name = "wasm-bindgen-test" -version = "0.3.50" +version = "0.3.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66c8d5e33ca3b6d9fa3b4676d774c5778031d27a578c2b007f905acf816152c3" +checksum = "45649196a53b0b7a15101d845d44d2dda7374fc1b5b5e2bbf58b7577ff4b346d" dependencies = [ + "async-trait", + "cast", "js-sys", + "libm", "minicov", + "nu-ansi-term", + "num-traits", + "oorandom", + "serde", + "serde_json", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test-macro", + "wasm-bindgen-test-shared", ] [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.50" +version = "0.3.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17d5042cc5fa009658f9a7333ef24291b1291a25b6382dd68862a7f3b969f69b" +checksum = "f579cdd0123ac74b94e1a4a72bd963cf30ebac343f2df347da0b8df24cdebed2" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "wasm-bindgen-test-shared" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8145dd1593bf0fb137dbfa85b8be79ec560a447298955877804640e40c2d6ea" + [[package]] name = "web-sys" -version = "0.3.77" +version = "0.3.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" +checksum = "312e32e551d92129218ea9a2452120f4aabc03529ef03e4d0d82fb2780608598" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 1079907..6f6dd89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,13 +27,13 @@ mime = "0.3" utoipa = { version = "5.4.0", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen = "0.2.100" +wasm-bindgen = "0.2.108" serde-wasm-bindgen = "0.6.5" -js-sys = "0.3.77" -web-sys = "0.3.77" +js-sys = "0.3.85" +web-sys = "0.3.85" [target.'cfg(target_arch = "wasm32")'.dev-dependencies] -wasm-bindgen-test = "0.3.50" +wasm-bindgen-test = "0.3.58" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] pubky = "0.5.4" diff --git a/src/bin/patch.mjs b/src/bin/patch.mjs index 9c62ace..9300368 100644 --- a/src/bin/patch.mjs +++ b/src/bin/patch.mjs @@ -13,6 +13,7 @@ const cargoPackageName = /\[package\]\nname = "(.*?)"/.exec(cargoTomlContent)[1] const name = cargoPackageName.replace(/-/g, '_') const content = await readFile(path.join(__dirname, `../../pkg/nodejs/${name}.js`), "utf8"); +const wasmBase64 = await readFile(path.join(__dirname, `../../pkg/nodejs/${name}_bg.wasm`), "base64"); const patched = content // use global TextDecoder TextEncoder @@ -21,16 +22,28 @@ const patched = content .replace("= module.exports", "= imports") // Export classes .replace(/\nclass (.*?) \{/g, "\n export class $1 {") - // Export functions + // Export enums (Object.freeze declarations) + .replace(/\nconst (\w+) = Object\.freeze/g, "\nexport const $1 = Object.freeze") + // Export standalone functions that are assigned to exports + .replace(/\nfunction (\w+)\(/g, (match, name, offset, str) => { + // Check if there's a corresponding exports.name = name; line + if (str.includes(`exports.${name} = ${name};`)) { + return `\nexport function ${name}(`; + } + return match; + }) + // Export functions defined inline on module.exports (for older wasm-bindgen) .replace(/\nmodule.exports.(.*?) = function/g, "\nimports.$1 = $1;\nexport function $1") - // Add exports to 'imports' + // Replace module.exports.X with imports.X (for older wasm-bindgen) .replace(/\nmodule\.exports\.(.*?)\s+/g, "\nimports.$1") + // Replace exports.X = X; with imports.X = X; (for newer wasm-bindgen) + .replace(/\nexports\.(\w+) = (\w+);/g, "\nimports.$1 = $2;") // Remove default export of imports .replace(/export default imports$/, '') // Replace inline wasm bytes with __toBinary function and embedded base64 bytes .replace( - /\nconst path.*\nconst bytes.*\n/, - ` + /\nconst (?:wasm)?[Pp]ath.*\nconst ((?:wasm)?[Bb]ytes).*\n/, + (match, bytesVar) => ` var __toBinary = /* @__PURE__ */ (() => { var table = new Uint8Array(128); for (var i = 0; i < 64; i++) @@ -48,19 +61,14 @@ var __toBinary = /* @__PURE__ */ (() => { }; })(); -const bytes = __toBinary(${JSON.stringify(await readFile(path.join(__dirname, `../../pkg/nodejs/${name}_bg.wasm`), "base64"))}); +const ${bytesVar} = __toBinary(${JSON.stringify(wasmBase64)}); ` ); // Write the patched JavaScript file with additional exports // This creates the final index.js that will be used by Node.js/browser consumers -await writeFile(path.join(__dirname, `../../pkg/index.js`), patched - + "\nglobalThis['pubky'] = imports\n" // Make imports available globally as 'pubky' - + "// Re-export enums so Next.js can statically import them\n" - + "export const PubkyAppPostKind = imports.PubkyAppPostKind;\n" // Export enum for named imports - + "export const PubkyAppFeedLayout = imports.PubkyAppFeedLayout;\n" // Export enum for named imports - + "export const PubkyAppFeedReach = imports.PubkyAppFeedReach;\n" // Export enum for named imports - + "export const PubkyAppFeedSort = imports.PubkyAppFeedSort;\n"); // Export enum for named imports +await writeFile(path.join(__dirname, `../../pkg/index.js`), "const imports = {};\n" + patched + + "\nglobalThis['pubky'] = imports\n"); // Make imports available globally as 'pubky' // Move outside of nodejs await Promise.all([".js", ".d.ts", "_bg.wasm"].map(suffix => diff --git a/src/bin/patch_async.mjs b/src/bin/patch_async.mjs index 0b8fd02..68fb6e8 100644 --- a/src/bin/patch_async.mjs +++ b/src/bin/patch_async.mjs @@ -22,15 +22,27 @@ const patched = content .replace("= module.exports", "= imports") // Export classes .replace(/\nclass (.*?) \{/g, "\n export class $1 {") - // Export functions + // Export enums (Object.freeze declarations) + .replace(/\nconst (\w+) = Object\.freeze/g, "\nexport const $1 = Object.freeze") + // Export standalone functions that are assigned to exports + .replace(/\nfunction (\w+)\(/g, (match, name, offset, str) => { + // Check if there's a corresponding exports.name = name; line + if (str.includes(`exports.${name} = ${name};`)) { + return `\nexport function ${name}(`; + } + return match; + }) + // Export functions defined inline on module.exports (for older wasm-bindgen) .replace(/\nmodule.exports.(.*?) = function/g, "\nimports.$1 = $1;\nexport function $1") - // Add exports to 'imports' + // Replace module.exports.X with imports.X (for older wasm-bindgen) .replace(/\nmodule\.exports\.(.*?)\s+/g, "\nimports.$1") + // Replace exports.X = X; with imports.X = X; (for newer wasm-bindgen) + .replace(/\nexports\.(\w+) = (\w+);/g, "\nimports.$1 = $2;") // Remove default export of imports (will be replaced with init function) .replace(/export default imports$/, '') // Remove inline wasm bytes - will use separate file .replace( - /\nconst path.*\nconst bytes.*\n/, + /\nconst (?:wasm)?[Pp]ath.*\nconst (?:wasm)?[Bb]ytes.*\n/, '\n// WASM bytes removed - use separate .wasm file\n' ); @@ -87,13 +99,8 @@ const patchedWithAsync = patched // Write the patched JavaScript file with additional exports // This creates the final index.js that will be used by Node.js/browser consumers -await writeFile(path.join(__dirname, `../../pkg/index.js`), patchedWithAsync - + "\nglobalThis['pubky'] = imports\n" // Make imports available globally as 'pubky' - + "// Re-export enums so Next.js can statically import them\n" - + "export const PubkyAppPostKind = imports.PubkyAppPostKind;\n" // Export enum for named imports - + "export const PubkyAppFeedLayout = imports.PubkyAppFeedLayout;\n" // Export enum for named imports - + "export const PubkyAppFeedReach = imports.PubkyAppFeedReach;\n" // Export enum for named imports - + "export const PubkyAppFeedSort = imports.PubkyAppFeedSort;\n"); // Export enum for named imports +await writeFile(path.join(__dirname, `../../pkg/index.js`), "const imports = {};\n" + patchedWithAsync + + "\nglobalThis['pubky'] = imports\n"); // Make imports available globally as 'pubky' // Move outside of nodejs await Promise.all([".js", ".d.ts", "_bg.wasm"].map(suffix =>