-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f51dcf
commit 2dced39
Showing
5 changed files
with
86 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters