Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const parse = (filePath: string): string[] => {
try {
const baseDir = path.parse(filePath).dir;
const encoding = chardet.detectFileSync(filePath);
const content = fs.readFileSync(filePath);
if (typeof encoding !== 'string') {
throw (new Error(`could not guess the file encoding (${filePath})`));
}
const decodedContent = iconv.decode(content, encoding);
const files = decodedContent
.split('\n')
.reduce((acc, line) => {
if (line.length === 0) {
return acc;
}
// If absolute path
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);
}
get encoding() {
// When data is huge, we want to optimize performace (in tradeoff of less accuracy):
// So we are using sample of first 100K bytes here:
if (this.size > 1000000) {
return chardet.detectFileSync(this.path, {
sampleSize: 1000000
});
}
return chardet.detectFileSync(this.path);
}
get encoding() {
// When data is huge, we want to optimize performace (in tradeoff of less accuracy):
// So we are using sample of first 100K bytes here:
if (this.size > 1000000) {
return chardet.detectFileSync(this.path, {sampleSize: 1000000})
}
return chardet.detectFileSync(this.path)
}
}
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;
}
}
}