Skip to content
Merged
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
3 changes: 1 addition & 2 deletions packages/wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lit-protocol/wasm",
"version": "6.4.0",
"version": "6.7.0",
"type": "commonjs",
"homepage": "https://github.com/Lit-Protocol/js-sdk",
"repository": {
Expand All @@ -25,7 +25,6 @@
"universal"
],
"scripts": {
"preinstall": "yarn rust:build",
"rust:build": "wasm-pack build ./rust --target web --release --out-name wasm-internal && yarn rust:postbuild",
"rust:postbuild": "node scripts/copyWasmBinary.mjs && rm -rf src/pkg && mkdir src/pkg && mv rust/pkg/wasm-internal.js src/pkg && mv rust/pkg/wasm-internal.d.ts src/pkg",
"rust:build:debug": "wasm-pack build ./rust --target web --dev --out-name wasm-internal && yarn rust:postbuild"
Expand Down
56 changes: 38 additions & 18 deletions packages/wasm/scripts/copyWasmBinary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import * as pako from 'pako';
const WASM_MODULE_PATH = 'rust/pkg/wasm-internal_bg.wasm';
const WASM_BINDING_PATH = 'rust/pkg/wasm-internal.js';
const CHUNK_SIZE = 100;
const REMOVE_LINES = [
`
if (typeof input === 'undefined') {
input = new URL('wasm-internal_bg.wasm', import.meta.url);
}
`,
`
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetch(input);
}
`,
const COMMENT_OUT_LINES = [
// This regex matches the block that checks if `module_or_path` is undefined and assigns a URL to it.
/if\s*\(\s*typeof\s+module_or_path\s*===\s*['"`]undefined['"`]\s*\)\s*{\s*module_or_path\s*=\s*new\s+URL\s*\(\s*['"`]wasm-internal_bg\.wasm['"`],\s*import\.meta\.url\s*\);\s*}/g,

// This regex matches the block that checks if `module_or_path` is a string, `Request`, or `URL` and assigns it to the result of a `fetch`.
/if\s*\(\s*typeof\s+module_or_path\s*===\s*['"`]string['"`]\s*\|\|\s*\(typeof\s+Request\s*===\s*['"`]function['"`]\s*&&\s*module_or_path\s*instanceof\s+Request\)\s*\|\|\s*\(typeof\s+URL\s*===\s*['"`]function['"`]\s*&&\s*module_or_path\s*instanceof\s+URL\)\s*\)\s*{\s*module_or_path\s*=\s*fetch\s*\(module_or_path\);\s*}/g,
];

function main() {
Expand All @@ -31,7 +26,7 @@ function main() {
}

let bindingModuleString = `
// @ts-nocheck
// @ts-nocheck - autogenerated from copyWasmBinary.mjs
import * as pako from 'pako';
`;
bindingModuleString += '\n';
Expand All @@ -46,13 +41,38 @@ export function getModule() {

bindingModuleString += wasmBindingModule;

for (const removeItem of REMOVE_LINES) {
const regex = new RegExp(
removeItem.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'),
'g'
); // Escape special characters
let allReplacementsDone = true; // Track whether all replacements were successful

for (const commentOutItem of COMMENT_OUT_LINES) {
const originalString = bindingModuleString; // Store the original string before replacement

bindingModuleString = bindingModuleString.replace(
commentOutItem,
(match) => {
const headerComment =
'// NOTE: This line is commented out automatically by copyWasmBinary.mjs\n';
const commentedBlock = match
.split('\n') // Split the matched block into lines
.map((line) => `// ${line}`) // Add "//" to each line
.join('\n'); // Re-join the lines into a single string

return `${headerComment}${commentedBlock}`; // Prepend the header comment to the commented block
}
);

// If no replacement was done (i.e., original string remains the same), mark as failure
if (originalString === bindingModuleString) {
allReplacementsDone = false;
}
}

bindingModuleString = bindingModuleString.replace(regex, '');
// throw an error if all replacements were NOT successful
if (!allReplacementsDone) {
throw new Error(
'❗️❗️ Failed to comment out all specified lines in the wasm binding module.'
);
} else {
console.log('✅ All replacements were successful!');
}

console.log('Writing wasm module');
Expand Down
Loading