Skip to content

Commit

Permalink
fix garbled characters
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jul 18, 2022
1 parent 267f6fa commit 1018c96
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/addon/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/browser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trzsz",
"version": "0.4.2",
"version": "0.4.3",
"description": "Making webshell and terminal supports trzsz ( trz / tsz ), which similar to ( rz / sz ), and compatible with tmux.",
"main": "lib/trzsz.js",
"types": "lib/trzsz.d.ts",
Expand Down
10 changes: 7 additions & 3 deletions src/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ export function strToUint8(str: string): Uint8Array {
return Uint8Array.from(str, (v) => v.charCodeAt(0));
}

export async function uint8ToStr(buf: Uint8Array): Promise<string> {
export async function uint8ToStr(buf: Uint8Array, encoding: BufferEncoding = "binary"): Promise<string> {
if (typeof Buffer === "function") {
return Buffer.from(buf).toString("latin1");
return Buffer.from(buf).toString(encoding);
}
return new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result as string);
reader.readAsBinaryString(new Blob([buf]));
if (encoding == "binary") {
reader.readAsBinaryString(new Blob([buf]));
} else {
reader.readAsText(new Blob([buf]), encoding);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class TrzszTransfer {

private async recvString(typ: string, mayHasJunk: boolean = false) {
const buf = await this.recvCheck(typ, mayHasJunk);
return uint8ToStr(decodeBuffer(buf));
return uint8ToStr(decodeBuffer(buf), "utf8");
}

private async checkString(expect: string) {
Expand Down
2 changes: 2 additions & 0 deletions test/comm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ test("string and Uint8Array transform", async () => {
const str2 = "\xAB\xCD\xEF\xFE\xDC\xBA\x80\x81\x82\x83\x84";
expect(await uint8ToStr(strToUint8(str1))).toBe(str1);
expect(await uint8ToStr(strToUint8(str2))).toBe(str2);
expect(await uint8ToStr(strToUint8("\xE4\xB8\xAD\xE6\x96\x87UTF8"), "utf8")).toBe("中文UTF8");
const buffer = global.Buffer;
global.Buffer = undefined;
try {
expect(await uint8ToStr(strToUint8(str1))).toBe(str1);
expect(await uint8ToStr(strToUint8(str2))).toBe(str2);
expect(await uint8ToStr(strToUint8("\xE4\xB8\xAD\xE6\x96\x87UTF8"), "utf8")).toBe("中文UTF8");
} finally {
global.Buffer = buffer;
}
Expand Down
4 changes: 2 additions & 2 deletions test/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ test("cancel upload files", async () => {
expect(sendToServer.mock.calls.length).toBe(1);
const data = sendToServer.mock.calls[0][0];
expect(data).toContain("#ACT:");
const action = JSON.parse(await uint8ToStr(decodeBuffer(data.substring(5, data.length - 1))));
const action = JSON.parse(await uint8ToStr(decodeBuffer(data.substring(5, data.length - 1)), "utf8"));
expect(action.confirm).toBe(false);
});

Expand All @@ -312,7 +312,7 @@ test("cancel download files", async () => {
expect(sendToServer.mock.calls.length).toBe(1);
const data = sendToServer.mock.calls[0][0];
expect(data).toContain("#ACT:");
const action = JSON.parse(await uint8ToStr(decodeBuffer(data.substring(5, data.length - 1))));
const action = JSON.parse(await uint8ToStr(decodeBuffer(data.substring(5, data.length - 1)), "utf8"));
expect(action.confirm).toBe(false);
});

Expand Down
2 changes: 1 addition & 1 deletion test/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ test("send config to remote", async () => {
expect(writer.mock.calls[1][0]).toContain("#CFG:");
expect((trzsz as any).transferConfig.bufsize).toBe(100);
const cfgOut = writer.mock.calls[1][0];
const cfgStr = await uint8ToStr(decodeBuffer(cfgOut.substring(5, cfgOut.length - 1)));
const cfgStr = await uint8ToStr(decodeBuffer(cfgOut.substring(5, cfgOut.length - 1)), "utf8");
expect(cfgStr).toContain('["\\u00ee","\\u00ee\\u00ee"]');
expect(cfgStr).toContain('["~","\\u00ee1"]');
expect(cfgStr).toContain('["\\u0002","\\u00eeA"]');
Expand Down

0 comments on commit 1018c96

Please sign in to comment.