Archived
1
0
This repository has been archived on 2024-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
code-server/packages/vscode/src/fill/iconv-lite.ts

66 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-01-24 00:44:57 +01:00
import * as iconv from "../../node_modules/iconv-lite";
import { Transform, TransformCallback } from "stream";
class IconvLiteDecoderStream extends Transform {
// tslint:disable-next-line no-any
private conv: any;
private encoding: string;
public constructor(options: { encoding: string }) {
super(options);
// tslint:disable-next-line no-any
this.conv = (iconv as any).getDecoder(options.encoding, undefined);
options.encoding = this.encoding = "utf8";
}
// tslint:disable-next-line no-any
public _transform(chunk: any, _encoding: string, done: TransformCallback): void {
if (!Buffer.isBuffer(chunk)) {
return done(new Error("Iconv decoding stream needs buffers as its input."));
}
try {
const res = this.conv.write(chunk);
if (res && res.length) {
this.push(res, this.encoding);
}
done();
} catch (error) {
done(error);
}
}
public _flush(done: TransformCallback): void {
try {
const res = this.conv.end();
if (res && res.length) {
this.push(res, this.encoding);
}
done();
} catch (error) {
done(error);
}
}
// tslint:disable-next-line no-any
public collect(cb: (error: Error | null, response?: any) => void): this {
let res = "";
this.on("error", cb);
this.on("data", (chunk) => res += chunk);
this.on("end", () => {
cb(null, res);
});
return this;
}
}
const decodeStream = (encoding: string): NodeJS.ReadWriteStream => {
return new IconvLiteDecoderStream({ encoding });
};
// @ts-ignore
iconv.decodeStream = decodeStream;
export = iconv;