Skip to content

Commit 092c976

Browse files
[wasm] Fix threaded worker blob syntax error (#6214)
JS's Blob replaces "\n" with a newline character. In the threaded worker bundle, there's a "\n" in `if(ENVIRONMENT_IS_NODE){fs.writeSync(2,text+"\n")`. When it is replaced with a newline, it splits a string across multiple lines, which is not valid JavaScript. This change prevents Blob from doing that replacement by escaping the "\n" character as "\\n". fixes #6203
1 parent a85c34c commit 092c976

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

tfjs-backend-wasm/src/backend_wasm.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ export async function init(): Promise<{wasm: BackendWasmModule}> {
292292
*/
293293
factoryConfig.locateFile = (path, prefix) => {
294294
if (path.endsWith('.worker.js')) {
295-
const response = wasmWorkerContents;
295+
// Escape '\n' because Blob will turn it into a newline.
296+
// There should be a setting for this, but 'endings: "native"' does
297+
// not seem to work.
298+
const response = (wasmWorkerContents as string).replace(/\n/g, '\\n');
296299
const blob = new Blob([response], {type: 'application/javascript'});
297300
return URL.createObjectURL(blob);
298301
}

0 commit comments

Comments
 (0)