Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
res.on('end', function() {
var result = '';
// Buffer
if (opts.isBuffer) {
result = Buffer.concat(body, size);
} else {
var buffer = new Buffer(size);
for (var i = 0, pos = 0, l = body.length; i < l; i++) {
var chunk = body[i];
chunk.copy(buffer, pos);
pos += chunk.length;
}
result = Iconv.decode(buffer, Chardet.detect(buffer)).toString(); //buffer.toString(opts.encoding);
if (opts.json) {
result = JSON.parse(result);
}
}
callback(null, res, result);
});
});
ExternalEditor.prototype.readTemporaryFile = function () {
try {
var tempFileBuffer = fs_1.readFileSync(this.tempFile);
if (tempFileBuffer.length === 0) {
this.text = "";
}
else {
var encoding = chardet_1.detect(tempFileBuffer).toString();
if (!iconv_lite_1.encodingExists(encoding)) {
// Probably a bad idea, but will at least prevent crashing
encoding = "utf8";
}
this.text = iconv_lite_1.decode(tempFileBuffer, encoding);
}
}
catch (readFileError) {
throw new ReadFileError_1.ReadFileError(readFileError);
}
};
ExternalEditor.prototype.removeTemporaryFile = function () {
_transform (chunk, encoding, done) {
if (!config.feeds.decode || !config.feeds.decode[this.url]) this.stream.write(chunk)
else {
const encoding = config.feeds.decode[this.url]
this.stream.write(iconv.decode(chunk, encoding === 'auto' ? require('chardet').detect(chunk) : encoding)) // Assumes that the encoding specified is valid, and will not check via iconv.encodingExists()
}
done()
}
}
function converToUTF8(buffer) {
// Detect the encoding
var detectedEncoding = chardet.detect(buffer);
// Already UTF8
if (!detectedEncoding || detectedEncoding.toLowerCase() == 'utf-8' || detectedEncoding.toLowerCase() == 'ascii') {
return buffer.toString();
}
return iconvLite.decode(buffer, detectedEncoding);
}
export function parse(filename: string): ICueSheet {
const cuesheet = new CueSheet();
if (!filename) {
console.log('no file name specified for parse');
return;
}
if (!fs.existsSync(filename)) {
throw new Error('file ' + filename + ' does not exist');
}
cuesheet.encoding = chardet.detect(fs.readFileSync(filename));
let encoding = cuesheet.encoding;
switch (cuesheet.encoding) {
case 'ISO-8859-1':
encoding = 'binary';
break;
}
const lines = (fs.readFileSync(filename, {encoding, flag: 'r'}) as any)
.replace(/\r\n/, '\n').split('\n');
lines.forEach(line => {
if (!line.match(/^\s*$/)) {
const lineParser = parseCommand(line);
commandMap[lineParser.command](lineParser.params, cuesheet);
}
function autoDecodeCharset(data){
if(data){
var buffer = new Buffer(data),
charset = chardet.detect(buffer);
console.log(('Data charset is '+charset ).magenta.bold);
try {
data = buffer.toString(charset);
} catch (e) {
if(Iconv.encodingExists(charset)){
data = Iconv.decode(buffer,charset);
}
}
return data;
}
}//end autoDecodeCharset
/**
async provideTextDocumentContent(uri: vscode.Uri): Promise {
try {
const buffer = await readFile(uri.path);
const charset = chardet.detect(buffer, {
returnAllMatches: false
}) as string | undefined;
return iconv.decode(buffer, charset || 'utf8');
} catch (err) {
console.log(err);
throw err;
}
}
}
request(options, function (error, response, body) {
var formatBody = body;
if (error) {
return callback(error, null, response);
} else if (response && response.statusCode && (response.statusCode.toString().substring(0, 1) === '4' || response.statusCode.toString().substring(0, 1) === '5')) {
return callback('Server Has Ran Into A Error', null, response);
}
if (options.encoding === null) {
var char = charset(response.headers, formatBody, peekSize) || chardet.detect(formatBody);
if (char) {
try {
formatBody = iconv.decode(formatBody, char);
} catch (ex) {
return callback(ex, null, response);
}
} else {
formatBody = formatBody.toString();
}
}
var ogObject = extractMetaTags(formatBody, options);
if (options.withCharset) {
ogObject.charset = charset(response.headers, formatBody, peekSize);
}
return callback(null, ogObject, response);