Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec881f8

Browse files
committedFeb 27, 2019
Fix images not appearing (iconv encoding issue)
Fixed by returning the original buffer from `fs.read` and then just using whatever encoding was passed in to iconv, so this should all work exactly the same now as it does on native Node.
1 parent c66e333 commit ec881f8

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed
 

‎packages/ide/src/fill/fs.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,12 @@ class FS {
407407
return util.promisify(fs.read)(fd, buffer, 0, length, position).then((resp) => {
408408
return {
409409
bytesRead: resp.bytesRead,
410-
content: (resp.bytesRead < buffer.length ? buffer.slice(0, resp.bytesRead) : buffer).toString("utf8"),
410+
content: resp.bytesRead < buffer.length ? buffer.slice(0, resp.bytesRead) : buffer,
411411
};
412412
});
413413
}, fd, length, position).then((resp) => {
414-
const newBuf = Buffer.from(resp.content, "utf8");
415-
buffer.set(newBuf, offset);
416-
callback(undefined!, resp.bytesRead, newBuf as TBuffer);
414+
buffer.set(resp.content, offset);
415+
callback(undefined!, resp.bytesRead, resp.content as TBuffer);
417416
}).catch((ex) => {
418417
callback(ex, undefined!, undefined!);
419418
});

‎packages/protocol/src/common/util.ts

+34-30
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,43 @@ export const stringify = (arg: any, isError?: boolean): string => { // tslint:di
5757
* Parse an event argument.
5858
*/
5959
export const parse = (arg: string): any => { // tslint:disable-line no-any
60-
if (!arg) {
61-
return arg;
62-
}
63-
64-
const result = JSON.parse(arg);
60+
const convert = (value: any): any => { // tslint:disable-line no-any
61+
if (value && value.data && value.type) {
62+
switch (value.type) {
63+
// JSON.stringify turns a Buffer into an object but JSON.parse doesn't
64+
// turn it back, it just remains an object.
65+
case "Buffer":
66+
if (Array.isArray(value.data)) {
67+
return Buffer.from(value);
68+
}
69+
break;
70+
// Errors apparently can't be stringified, so we do something similar to
71+
// what happens to buffers and stringify them as regular objects.
72+
case "Error":
73+
if (value.data.message) {
74+
const error = new Error(value.data.message);
75+
// TODO: Can we set the stack? Doing so seems to make it into an
76+
// "invalid object".
77+
if (typeof value.data.code !== "undefined") {
78+
(error as NodeJS.ErrnoException).code = value.data.code;
79+
}
80+
// tslint:disable-next-line no-any
81+
(error as any).originalStack = value.data.stack;
6582

66-
if (result && result.data && result.type) {
67-
switch (result.type) {
68-
// JSON.stringify turns a Buffer into an object but JSON.parse doesn't
69-
// turn it back, it just remains an object.
70-
case "Buffer":
71-
if (Array.isArray(result.data)) {
72-
return Buffer.from(result);
73-
}
74-
break;
75-
// Errors apparently can't be stringified, so we do something similar to
76-
// what happens to buffers and stringify them as regular objects.
77-
case "Error":
78-
if (result.data.message) {
79-
const error = new Error(result.data.message);
80-
// TODO: Can we set the stack? Doing so seems to make it into an
81-
// "invalid object".
82-
if (typeof result.data.code !== "undefined") {
83-
(error as NodeJS.ErrnoException).code = result.data.code;
83+
return error;
8484
}
85-
// tslint:disable-next-line no-any
86-
(error as any).originalStack = result.data.stack;
85+
break;
86+
}
87+
}
8788

88-
return error;
89-
}
90-
break;
89+
if (value && typeof value === "object") {
90+
Object.keys(value).forEach((key) => {
91+
value[key] = convert(value[key]);
92+
});
9193
}
92-
}
9394

94-
return result;
95+
return value;
96+
};
97+
98+
return arg ? convert(JSON.parse(arg)) : arg;
9599
};

‎packages/protocol/src/node/evaluate.ts

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export const evaluate = (connection: SendableConnection, message: NewEvalMessage
1919
*/
2020
// tslint:disable-next-line no-any
2121
const sendResp = (resp: any): void => {
22+
logger.trace(() => [
23+
"resolve",
24+
field("id", message.getId()),
25+
field("response", stringify(resp)),
26+
]);
27+
2228
const evalDone = new EvalDoneMessage();
2329
evalDone.setId(message.getId());
2430
evalDone.setResponse(stringify(resp));
@@ -34,6 +40,12 @@ export const evaluate = (connection: SendableConnection, message: NewEvalMessage
3440
* Send an exception and call onDispose.
3541
*/
3642
const sendException = (error: Error): void => {
43+
logger.trace(() => [
44+
"reject",
45+
field("id", message.getId()),
46+
field("response", stringify(error, true)),
47+
]);
48+
3749
const evalFailed = new EvalFailedMessage();
3850
evalFailed.setId(message.getId());
3951
evalFailed.setResponse(stringify(error, true));

‎packages/vscode/src/fill/iconv-lite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class IconvLiteDecoderStream extends Transform {
1010
super(options);
1111
// tslint:disable-next-line no-any
1212
this.conv = (iconv as any).getDecoder(options.encoding, undefined);
13-
options.encoding = this.encoding = "utf8";
13+
this.encoding = options.encoding;
1414
}
1515

1616
// tslint:disable-next-line no-any

0 commit comments

Comments
 (0)
Please sign in to comment.