NeahNew/node_modules/imapflow/lib/jp-decoder.js
2025-05-03 14:17:46 +02:00

47 lines
1.1 KiB
JavaScript

'use strict';
const { Transform } = require('stream');
const encodingJapanese = require('encoding-japanese');
class JPDecoder extends Transform {
constructor(charset) {
super();
this.charset = charset;
this.chunks = [];
this.chunklen = 0;
}
_transform(chunk, encoding, done) {
if (typeof chunk === 'string') {
chunk = Buffer.from(chunk, encoding);
}
this.chunks.push(chunk);
this.chunklen += chunk.length;
done();
}
_flush(done) {
let input = Buffer.concat(this.chunks, this.chunklen);
try {
let output = encodingJapanese.convert(input, {
to: 'UNICODE', // to_encoding
from: this.charset, // from_encoding
type: 'string'
});
if (typeof output === 'string') {
output = Buffer.from(output);
}
this.push(output);
} catch (err) {
// keep as is on errors
this.push(input);
}
done();
}
}
module.exports.JPDecoder = JPDecoder;