Skip to content

Commit d7d53e9

Browse files
committed
refactor(tauri): enhance script handling in execute function
- Updated the `execute` function to improve handling of string and function scripts. - Strings are now passed as-is for embedded paths, while tauri-driver paths utilize `JSON.stringify` for proper escaping. - Adjusted comments for clarity on the handling of different script types.
1 parent 0130321 commit d7d53e9

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

packages/tauri-service/src/service.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,12 @@ export default class TauriWorkerService {
318318
script: string | ((...args: InnerArguments) => ReturnValue),
319319
...args: InnerArguments
320320
): Promise<ReturnValue> {
321-
// For strings, use JSON.stringify to safely escape special characters
322-
// For functions, toString() gives the function source which is already valid JS
323-
const scriptString = typeof script === 'function' ? script.toString() : JSON.stringify(script);
321+
// For functions, always use .toString() - produces valid JS function source
322+
// For strings:
323+
// - embedded path: pass as-is (WebDriver handles execution)
324+
// - tauri-driver path: use JSON.stringify (wrapped in template literal)
325+
const scriptString =
326+
typeof script === 'function' ? script.toString() : isEmbedded ? script : JSON.stringify(script);
324327

325328
if (isEmbedded) {
326329
// For embedded WebDriver: skip console wrapper as console forwarding
@@ -329,8 +332,7 @@ export default class TauriWorkerService {
329332
}
330333

331334
// For tauri-driver: use sync execute with console wrapper
332-
// Note: scriptString is already properly escaped from above - functions use .toString()
333-
// which produces valid JS, strings use JSON.stringify() which also produces valid JS
335+
// Note: scriptString is already properly escaped via JSON.stringify above
334336
const wrappedScript = `
335337
${CONSOLE_WRAPPER_SCRIPT}
336338
return (${scriptString}).apply(null, arguments);

0 commit comments

Comments
 (0)