Skip to content

Commit

Permalink
Decode non-ANSII strings only on backend
Browse files Browse the repository at this point in the history
Signed-off-by: xychen <xychen@listenai.com>
  • Loading branch information
xychen committed Jun 11, 2024
1 parent 922597a commit d24e227
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/utils/cskburn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from '@tauri-apps/plugin-shell';
import { invoke } from '@tauri-apps/api/core';

import { decode } from './strings';

type ICSKBurnEventHandlers = Partial<{
onOutput: (output: string) => void;
Expand Down Expand Up @@ -76,13 +77,13 @@ export async function cskburn(
}

command.stdout.on('data', async (data: Uint8Array) => {
const line = await invoke('decode', { data });
const line = await decode(data);
outputs.push(line.trim());
handleOutput(line);
});

command.stderr.on('data', async (data: Uint8Array) => {
const line = await invoke('decode', { data });
const line = await decode(data);
outputs.push(line.trim());
handleOutput(line);
});
Expand Down
11 changes: 11 additions & 0 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { invoke } from '@tauri-apps/api/core';

export async function decode(data: ArrayLike<number>): Promise<string> {
// IPC is expensive, so we should avoid it if possible.
const isPureAscii = Array.from(data).every((c) => c < 0x80);
if (isPureAscii) {
return new TextDecoder().decode(new Uint8Array(data));
}

return await invoke('decode', { data });
}

0 comments on commit d24e227

Please sign in to comment.