Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 76 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 20 additions & 12 deletions src/bin/patch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++)
Expand All @@ -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 =>
Expand Down
27 changes: 17 additions & 10 deletions src/bin/patch_async.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

Expand Down Expand Up @@ -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 =>
Expand Down