Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
const contentType = text.match(contentTypeReg)[1].trim()
const charset = (text.match(secondChartsetReg) || text.match(charsetReg))[1].trim().replace(/"/g, '')
const transferEncoding = text.match(contentTransferEncodingReg)[1].trim()
const subTextArr = text.split(blankLineReg)
subTextArr.shift()
const contentText = subTextArr.filter(item => (item.trim())).join('')
if (contentType.match(htmlTypeReg)) {
if (transferEncoding === 'base64') {
const buff = Buffer.from(contentText, transferEncoding)
result.bodyHtml = iconv.decode(buff, charset || 'utf-8')
} else if (transferEncoding === 'quoted-printable') {
// 用try包裹是因为发现有邮件写着quoted-printable编码,正文却没有编码。quotedPrintable模块decode没有编码的内容就报错,进入catch
try {
// 编码为quoted-printable且charset为utf8。暂时遇到为utf8的,未看到其他字符集
const quotedHtml = utf8.decode(quotedPrintable.decode(contentText))
result.bodyHtml = quotedHtml
} catch (e) {
result.bodyHtml = contentText
}
} else {
result.bodyHtml = contentText
}
} else if (contentType.match(plainTypeReg)) {
if (transferEncoding === 'base64') {
const buff = Buffer.from(contentText, transferEncoding)
result.bodyText += iconv.decode(buff, charset || 'utf-8')
} else {
try {
// 编码为quoted-printable且charset为utf8。其他字符集用quotedPrintable会报错,进入catch
const quotedText = utf8.decode(quotedPrintable.decode(contentText))
result.bodyText += quotedText
hex = hex.replace(/^(?:00)*/, '');
hex = hex
.split('')
.reverse()
.join('');
const l = hex.length;
for (let i = 0; i < l; i += 2) {
code = parseInt(hex.substr(i, 2), 16);
// if (code !== 0) {
string += String.fromCharCode(code);
// }
}
return utf8.decode(string);
};
}
}
if (auth) {
if (apiState.token) {
options.headers.authorization = `Bearer ${apiState.token}`;
}
}
adjustedEndpoint = adjustedEndpoint.endsWith('/')
? adjustedEndpoint
: `${adjustedEndpoint}/`;
let apiURL = `${config.get('apiHost')}${adjustedEndpoint}${queryString}`;
if (_config.get('server')) {
_log.debug('Encoding `apiURL` in UTF8 before fetch().');
// Workaround for https://github.com/bitinn/node-fetch/issues/245
apiURL = utf8.encode(apiURL);
}
return fetch(apiURL, options)
.then((response) => {
// There isn't always a 'Content-Type' in headers, e.g., with a DELETE
// method or 5xx responses.
let contentType = response.headers.get('Content-Type');
contentType = contentType && contentType.toLowerCase();
// This is a bit paranoid, but we ensure the API returns a JSON response
// (see https://github.com/mozilla/addons-frontend/issues/1701).
// If not we'll store the text response in JSON and log an error.
// If the JSON parsing fails; we log the error and return an "unknown
// error".
if (contentType === 'application/json') {
return response
exports.decodePacket = function (data, binaryType, utf8decode) {
// String data
if (typeof data == 'string' || data === undefined) {
if (data.charAt(0) == 'b') {
return exports.decodeBase64Packet(data.substr(1), binaryType);
}
var type = data.charAt(0);
if (utf8decode) {
try {
data = utf8.decode(data);
} catch (e) {
return err;
}
}
if (Number(type) != type || !packetslist[type]) {
return err;
}
if (data.length > 1) {
return { type: packetslist[type], data: data.substring(1) };
} else {
return { type: packetslist[type] };
}
}
exports.decodePacket = function (data, binaryType, utf8decode) {
// String data
if (typeof data == 'string' || data === undefined) {
if (data.charAt(0) == 'b') {
return exports.decodeBase64Packet(data.substr(1), binaryType);
}
if (utf8decode) {
try {
data = utf8.decode(data);
} catch (e) {
return err;
}
}
var type = data.charAt(0);
if (Number(type) != type || !packetslist[type]) {
return err;
}
if (data.length > 1) {
return { type: packetslist[type], data: data.substring(1) };
} else {
return { type: packetslist[type] };
}
}
public readString(): string {
// Read string length
const firstByte: number = this.readByte();
let strLength: number = firstByte;
if (firstByte >= 128) {
const secondByte: number = this.readByte();
strLength = (firstByte - 128) + secondByte << 7;
}
// Read string content using length
const rawText = bufferToText(this._data.slice(this.head, this.head + strLength));
const strContent: string = utf8.decode(rawText);
this.head += strLength;
return strContent;
}
const quotedHtml = utf8.decode(quotedPrintable.decode(contentText))
result.bodyHtml = quotedHtml
} catch (e) {
result.bodyHtml = contentText
}
} else {
result.bodyHtml = contentText
}
} else if (contentType.match(plainTypeReg)) {
if (transferEncoding === 'base64') {
const buff = Buffer.from(contentText, transferEncoding)
result.bodyText += iconv.decode(buff, charset || 'utf-8')
} else {
try {
// 编码为quoted-printable且charset为utf8。其他字符集用quotedPrintable会报错,进入catch
const quotedText = utf8.decode(quotedPrintable.decode(contentText))
result.bodyText += quotedText
} catch (e) {
// 不是quoted-printable,那html本身应该是utf-8编码而不是gb18030
result.bodyText += iconv.decode(iconv.encode(contentText, 'gb18030'), 'utf-8')
}
}
}
})
} else if (contentType.match(mixedMultipart) || (contentType.match(relatedMultipart) && textArr.length > 2)) {
exports.decodePacket = function (data, binaryType, utf8decode) {
// String data
if (typeof data == 'string' || data === undefined) {
if (data.charAt(0) == 'b') {
return exports.decodeBase64Packet(data.substr(1), binaryType);
}
var type = data.charAt(0);
if (utf8decode) {
try {
data = utf8.decode(data);
} catch (e) {
return err;
}
}
if (Number(type) != type || !packetslist[type]) {
return err;
}
if (data.length > 1) {
return { type: packetslist[type], data: data.substring(1) };
} else {
return { type: packetslist[type] };
}
}
function test_version(): void {
if (typeof utf8.version === "string") {
console.log(utf8.version);
}
}
function test_version(): void {
if (typeof utf8.version === "string") {
console.log(utf8.version);
}
}