@@ -4,17 +4,12 @@ import * as pako from 'pako';
44const WASM_MODULE_PATH = 'rust/pkg/wasm-internal_bg.wasm' ;
55const WASM_BINDING_PATH = 'rust/pkg/wasm-internal.js' ;
66const CHUNK_SIZE = 100 ;
7- const REMOVE_LINES = [
8- `
9- if (typeof input === 'undefined') {
10- input = new URL('wasm-internal_bg.wasm', import.meta.url);
11- }
12- ` ,
13- `
14- if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
15- input = fetch(input);
16- }
17- ` ,
7+ const COMMENT_OUT_LINES = [
8+ // This regex matches the block that checks if `module_or_path` is undefined and assigns a URL to it.
9+ / i f \s * \( \s * t y p e o f \s + m o d u l e _ o r _ p a t h \s * = = = \s * [ ' " ` ] u n d e f i n e d [ ' " ` ] \s * \) \s * { \s * m o d u l e _ o r _ p a t h \s * = \s * n e w \s + U R L \s * \( \s * [ ' " ` ] w a s m - i n t e r n a l _ b g \. w a s m [ ' " ` ] , \s * i m p o r t \. m e t a \. u r l \s * \) ; \s * } / g,
10+
11+ // 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`.
12+ / i f \s * \( \s * t y p e o f \s + m o d u l e _ o r _ p a t h \s * = = = \s * [ ' " ` ] s t r i n g [ ' " ` ] \s * \| \| \s * \( t y p e o f \s + R e q u e s t \s * = = = \s * [ ' " ` ] f u n c t i o n [ ' " ` ] \s * & & \s * m o d u l e _ o r _ p a t h \s * i n s t a n c e o f \s + R e q u e s t \) \s * \| \| \s * \( t y p e o f \s + U R L \s * = = = \s * [ ' " ` ] f u n c t i o n [ ' " ` ] \s * & & \s * m o d u l e _ o r _ p a t h \s * i n s t a n c e o f \s + U R L \) \s * \) \s * { \s * m o d u l e _ o r _ p a t h \s * = \s * f e t c h \s * \( m o d u l e _ o r _ p a t h \) ; \s * } / g,
1813] ;
1914
2015function main ( ) {
@@ -31,7 +26,7 @@ function main() {
3126 }
3227
3328 let bindingModuleString = `
34- // @ts-nocheck
29+ // @ts-nocheck - autogenerated from copyWasmBinary.mjs
3530import * as pako from 'pako';
3631 ` ;
3732 bindingModuleString += '\n' ;
@@ -46,13 +41,38 @@ export function getModule() {
4641
4742 bindingModuleString += wasmBindingModule ;
4843
49- for ( const removeItem of REMOVE_LINES ) {
50- const regex = new RegExp (
51- removeItem . replace ( / [ - / \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) ,
52- 'g'
53- ) ; // Escape special characters
44+ let allReplacementsDone = true ; // Track whether all replacements were successful
45+
46+ for ( const commentOutItem of COMMENT_OUT_LINES ) {
47+ const originalString = bindingModuleString ; // Store the original string before replacement
48+
49+ bindingModuleString = bindingModuleString . replace (
50+ commentOutItem ,
51+ ( match ) => {
52+ const headerComment =
53+ '// NOTE: This line is commented out automatically by copyWasmBinary.mjs\n' ;
54+ const commentedBlock = match
55+ . split ( '\n' ) // Split the matched block into lines
56+ . map ( ( line ) => `// ${ line } ` ) // Add "//" to each line
57+ . join ( '\n' ) ; // Re-join the lines into a single string
58+
59+ return `${ headerComment } ${ commentedBlock } ` ; // Prepend the header comment to the commented block
60+ }
61+ ) ;
62+
63+ // If no replacement was done (i.e., original string remains the same), mark as failure
64+ if ( originalString === bindingModuleString ) {
65+ allReplacementsDone = false ;
66+ }
67+ }
5468
55- bindingModuleString = bindingModuleString . replace ( regex , '' ) ;
69+ // throw an error if all replacements were NOT successful
70+ if ( ! allReplacementsDone ) {
71+ throw new Error (
72+ '❗️❗️ Failed to comment out all specified lines in the wasm binding module.'
73+ ) ;
74+ } else {
75+ console . log ( '✅ All replacements were successful!' ) ;
5676 }
5777
5878 console . log ( 'Writing wasm module' ) ;
0 commit comments