Skip to content

Commit 63d89d6

Browse files
committed
Merge pull request #6 from ok300/copilot/fix-ci-test-failures
Fix JS binding patch scripts for wasm-bindgen 0.2.108 CJS output changes
1 parent 0b48c3e commit 63d89d6

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/bin/patch.mjs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const cargoPackageName = /\[package\]\nname = "(.*?)"/.exec(cargoTomlContent)[1]
1313
const name = cargoPackageName.replace(/-/g, '_')
1414

1515
const content = await readFile(path.join(__dirname, `../../pkg/nodejs/${name}.js`), "utf8");
16+
const wasmBase64 = await readFile(path.join(__dirname, `../../pkg/nodejs/${name}_bg.wasm`), "base64");
1617

1718
const patched = content
1819
// use global TextDecoder TextEncoder
@@ -21,16 +22,28 @@ const patched = content
2122
.replace("= module.exports", "= imports")
2223
// Export classes
2324
.replace(/\nclass (.*?) \{/g, "\n export class $1 {")
24-
// Export functions
25+
// Export enums (Object.freeze declarations)
26+
.replace(/\nconst (\w+) = Object\.freeze/g, "\nexport const $1 = Object.freeze")
27+
// Export standalone functions that are assigned to exports
28+
.replace(/\nfunction (\w+)\(/g, (match, name, offset, str) => {
29+
// Check if there's a corresponding exports.name = name; line
30+
if (str.includes(`exports.${name} = ${name};`)) {
31+
return `\nexport function ${name}(`;
32+
}
33+
return match;
34+
})
35+
// Export functions defined inline on module.exports (for older wasm-bindgen)
2536
.replace(/\nmodule.exports.(.*?) = function/g, "\nimports.$1 = $1;\nexport function $1")
26-
// Add exports to 'imports'
37+
// Replace module.exports.X with imports.X (for older wasm-bindgen)
2738
.replace(/\nmodule\.exports\.(.*?)\s+/g, "\nimports.$1")
39+
// Replace exports.X = X; with imports.X = X; (for newer wasm-bindgen)
40+
.replace(/\nexports\.(\w+) = (\w+);/g, "\nimports.$1 = $2;")
2841
// Remove default export of imports
2942
.replace(/export default imports$/, '')
3043
// Replace inline wasm bytes with __toBinary function and embedded base64 bytes
3144
.replace(
32-
/\nconst path.*\nconst bytes.*\n/,
33-
`
45+
/\nconst (?:wasm)?[Pp]ath.*\nconst ((?:wasm)?[Bb]ytes).*\n/,
46+
(match, bytesVar) => `
3447
var __toBinary = /* @__PURE__ */ (() => {
3548
var table = new Uint8Array(128);
3649
for (var i = 0; i < 64; i++)
@@ -48,19 +61,14 @@ var __toBinary = /* @__PURE__ */ (() => {
4861
};
4962
})();
5063
51-
const bytes = __toBinary(${JSON.stringify(await readFile(path.join(__dirname, `../../pkg/nodejs/${name}_bg.wasm`), "base64"))});
64+
const ${bytesVar} = __toBinary(${JSON.stringify(wasmBase64)});
5265
`
5366
);
5467

5568
// Write the patched JavaScript file with additional exports
5669
// This creates the final index.js that will be used by Node.js/browser consumers
57-
await writeFile(path.join(__dirname, `../../pkg/index.js`), patched
58-
+ "\nglobalThis['pubky'] = imports\n" // Make imports available globally as 'pubky'
59-
+ "// Re-export enums so Next.js can statically import them\n"
60-
+ "export const PubkyAppPostKind = imports.PubkyAppPostKind;\n" // Export enum for named imports
61-
+ "export const PubkyAppFeedLayout = imports.PubkyAppFeedLayout;\n" // Export enum for named imports
62-
+ "export const PubkyAppFeedReach = imports.PubkyAppFeedReach;\n" // Export enum for named imports
63-
+ "export const PubkyAppFeedSort = imports.PubkyAppFeedSort;\n"); // Export enum for named imports
70+
await writeFile(path.join(__dirname, `../../pkg/index.js`), "const imports = {};\n" + patched
71+
+ "\nglobalThis['pubky'] = imports\n"); // Make imports available globally as 'pubky'
6472

6573
// Move outside of nodejs
6674
await Promise.all([".js", ".d.ts", "_bg.wasm"].map(suffix =>

src/bin/patch_async.mjs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,27 @@ const patched = content
2222
.replace("= module.exports", "= imports")
2323
// Export classes
2424
.replace(/\nclass (.*?) \{/g, "\n export class $1 {")
25-
// Export functions
25+
// Export enums (Object.freeze declarations)
26+
.replace(/\nconst (\w+) = Object\.freeze/g, "\nexport const $1 = Object.freeze")
27+
// Export standalone functions that are assigned to exports
28+
.replace(/\nfunction (\w+)\(/g, (match, name, offset, str) => {
29+
// Check if there's a corresponding exports.name = name; line
30+
if (str.includes(`exports.${name} = ${name};`)) {
31+
return `\nexport function ${name}(`;
32+
}
33+
return match;
34+
})
35+
// Export functions defined inline on module.exports (for older wasm-bindgen)
2636
.replace(/\nmodule.exports.(.*?) = function/g, "\nimports.$1 = $1;\nexport function $1")
27-
// Add exports to 'imports'
37+
// Replace module.exports.X with imports.X (for older wasm-bindgen)
2838
.replace(/\nmodule\.exports\.(.*?)\s+/g, "\nimports.$1")
39+
// Replace exports.X = X; with imports.X = X; (for newer wasm-bindgen)
40+
.replace(/\nexports\.(\w+) = (\w+);/g, "\nimports.$1 = $2;")
2941
// Remove default export of imports (will be replaced with init function)
3042
.replace(/export default imports$/, '')
3143
// Remove inline wasm bytes - will use separate file
3244
.replace(
33-
/\nconst path.*\nconst bytes.*\n/,
45+
/\nconst (?:wasm)?[Pp]ath.*\nconst (?:wasm)?[Bb]ytes.*\n/,
3446
'\n// WASM bytes removed - use separate .wasm file\n'
3547
);
3648

@@ -87,13 +99,8 @@ const patchedWithAsync = patched
8799

88100
// Write the patched JavaScript file with additional exports
89101
// This creates the final index.js that will be used by Node.js/browser consumers
90-
await writeFile(path.join(__dirname, `../../pkg/index.js`), patchedWithAsync
91-
+ "\nglobalThis['pubky'] = imports\n" // Make imports available globally as 'pubky'
92-
+ "// Re-export enums so Next.js can statically import them\n"
93-
+ "export const PubkyAppPostKind = imports.PubkyAppPostKind;\n" // Export enum for named imports
94-
+ "export const PubkyAppFeedLayout = imports.PubkyAppFeedLayout;\n" // Export enum for named imports
95-
+ "export const PubkyAppFeedReach = imports.PubkyAppFeedReach;\n" // Export enum for named imports
96-
+ "export const PubkyAppFeedSort = imports.PubkyAppFeedSort;\n"); // Export enum for named imports
102+
await writeFile(path.join(__dirname, `../../pkg/index.js`), "const imports = {};\n" + patchedWithAsync
103+
+ "\nglobalThis['pubky'] = imports\n"); // Make imports available globally as 'pubky'
97104

98105
// Move outside of nodejs
99106
await Promise.all([".js", ".d.ts", "_bg.wasm"].map(suffix =>

0 commit comments

Comments
 (0)