1
0
mirror of https://git.tuxpa.in/a/code-server.git synced 2024-12-26 12:15:26 +00:00

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.
This commit is contained in:
Asher 2019-02-27 14:40:57 -06:00
parent 20c0fc4c52
commit 3bacbca325
No known key found for this signature in database
GPG Key ID: 7BB4BA9C783D2BBC
4 changed files with 51 additions and 36 deletions

View File

@ -407,13 +407,12 @@ class FS {
return util.promisify(fs.read)(fd, buffer, 0, length, position).then((resp) => { return util.promisify(fs.read)(fd, buffer, 0, length, position).then((resp) => {
return { return {
bytesRead: resp.bytesRead, bytesRead: resp.bytesRead,
content: (resp.bytesRead < buffer.length ? buffer.slice(0, resp.bytesRead) : buffer).toString("utf8"), content: resp.bytesRead < buffer.length ? buffer.slice(0, resp.bytesRead) : buffer,
}; };
}); });
}, fd, length, position).then((resp) => { }, fd, length, position).then((resp) => {
const newBuf = Buffer.from(resp.content, "utf8"); buffer.set(resp.content, offset);
buffer.set(newBuf, offset); callback(undefined!, resp.bytesRead, resp.content as TBuffer);
callback(undefined!, resp.bytesRead, newBuf as TBuffer);
}).catch((ex) => { }).catch((ex) => {
callback(ex, undefined!, undefined!); callback(ex, undefined!, undefined!);
}); });

View File

@ -57,33 +57,28 @@ export const stringify = (arg: any, isError?: boolean): string => { // tslint:di
* Parse an event argument. * Parse an event argument.
*/ */
export const parse = (arg: string): any => { // tslint:disable-line no-any export const parse = (arg: string): any => { // tslint:disable-line no-any
if (!arg) { const convert = (value: any): any => { // tslint:disable-line no-any
return arg; if (value && value.data && value.type) {
} switch (value.type) {
const result = JSON.parse(arg);
if (result && result.data && result.type) {
switch (result.type) {
// JSON.stringify turns a Buffer into an object but JSON.parse doesn't // JSON.stringify turns a Buffer into an object but JSON.parse doesn't
// turn it back, it just remains an object. // turn it back, it just remains an object.
case "Buffer": case "Buffer":
if (Array.isArray(result.data)) { if (Array.isArray(value.data)) {
return Buffer.from(result); return Buffer.from(value);
} }
break; break;
// Errors apparently can't be stringified, so we do something similar to // Errors apparently can't be stringified, so we do something similar to
// what happens to buffers and stringify them as regular objects. // what happens to buffers and stringify them as regular objects.
case "Error": case "Error":
if (result.data.message) { if (value.data.message) {
const error = new Error(result.data.message); const error = new Error(value.data.message);
// TODO: Can we set the stack? Doing so seems to make it into an // TODO: Can we set the stack? Doing so seems to make it into an
// "invalid object". // "invalid object".
if (typeof result.data.code !== "undefined") { if (typeof value.data.code !== "undefined") {
(error as NodeJS.ErrnoException).code = result.data.code; (error as NodeJS.ErrnoException).code = value.data.code;
} }
// tslint:disable-next-line no-any // tslint:disable-next-line no-any
(error as any).originalStack = result.data.stack; (error as any).originalStack = value.data.stack;
return error; return error;
} }
@ -91,5 +86,14 @@ export const parse = (arg: string): any => { // tslint:disable-line no-any
} }
} }
return result; if (value && typeof value === "object") {
Object.keys(value).forEach((key) => {
value[key] = convert(value[key]);
});
}
return value;
};
return arg ? convert(JSON.parse(arg)) : arg;
}; };

View File

@ -19,6 +19,12 @@ export const evaluate = (connection: SendableConnection, message: NewEvalMessage
*/ */
// tslint:disable-next-line no-any // tslint:disable-next-line no-any
const sendResp = (resp: any): void => { const sendResp = (resp: any): void => {
logger.trace(() => [
"resolve",
field("id", message.getId()),
field("response", stringify(resp)),
]);
const evalDone = new EvalDoneMessage(); const evalDone = new EvalDoneMessage();
evalDone.setId(message.getId()); evalDone.setId(message.getId());
evalDone.setResponse(stringify(resp)); evalDone.setResponse(stringify(resp));
@ -34,6 +40,12 @@ export const evaluate = (connection: SendableConnection, message: NewEvalMessage
* Send an exception and call onDispose. * Send an exception and call onDispose.
*/ */
const sendException = (error: Error): void => { const sendException = (error: Error): void => {
logger.trace(() => [
"reject",
field("id", message.getId()),
field("response", stringify(error, true)),
]);
const evalFailed = new EvalFailedMessage(); const evalFailed = new EvalFailedMessage();
evalFailed.setId(message.getId()); evalFailed.setId(message.getId());
evalFailed.setResponse(stringify(error, true)); evalFailed.setResponse(stringify(error, true));

View File

@ -10,7 +10,7 @@ class IconvLiteDecoderStream extends Transform {
super(options); super(options);
// tslint:disable-next-line no-any // tslint:disable-next-line no-any
this.conv = (iconv as any).getDecoder(options.encoding, undefined); this.conv = (iconv as any).getDecoder(options.encoding, undefined);
options.encoding = this.encoding = "utf8"; this.encoding = options.encoding;
} }
// tslint:disable-next-line no-any // tslint:disable-next-line no-any