Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function maybeTranslate(res, charset) {
const iconv = require('iconv-lite')
// use iconv if its not utf8 already.
if (charset && !/utf-*8/i.test(charset)) {
try {
res.pipe(
iconv.decodeStream(charset)
).pipe(
iconv.encodeStream('utf-8')
)
} catch (err) {
res.emit('error', err);
}
}
return res;
}
function getWriterOutputStream(resType, ext) {
var stream;
if (zipExt) { ext += zipExt; }
job.contentType = resType;
job.fileExtension = ext;
var zipStream = getZipStream();
if (!job.resIsDeferred) {
stream = iconv.encodeStream(job.outputCharset || 'utf-8');
if (zipStream) { stream = stream.pipe(zipStream); }
stream.pipe(res);
return stream;
}
job.logger.info('Deferred response requested: ECs will be writen in a temp file');
var date = moment().format('YYYY-MM-DD_HH[h]mm');
// create a file writer stream to store ECs into
job.ecsPath = path.join(job.jobPath, date + '.job-ecs.' + ext);
job.ecsStreamEnd = false;
var jobPath = job.jobPath;
var reg = /.*\.job-ecs(\.[a-z]+){1,2}$/;
var files = fs.readdirSync(jobPath);
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
cb();
return;
}
if (file.isStream()) {
try {
file.contents = file.contents
.pipe(iconv.decodeStream(options.from, options.iconv.decode))
.pipe(iconv.encodeStream(options.to, options.iconv.encode));
this.push(file);
} catch (err) {
this.emit('error', new pluginError('gulp-convert-encoding', err));
}
}
if (file.isBuffer()) {
try {
var content = iconv.decode(file.contents, options.from, options.iconv.decode);
file.contents = iconv.encode(content, options.to, options.iconv.encode);
this.push(file);
} catch (err) {
this.emit('error', new pluginError('gulp-convert-encoding', err));
}
}
pipeStream.addTail(function(src, next) {
next(src.pipe(iconv.encodeStream(charset)));
});
}
var unzip, zip, stream, result;
switch (headers['content-encoding']) {
case 'gzip':
unzip = zlib.createGunzip();
zip = zlib.createGzip();
break;
case 'deflate':
unzip = zlib.createInflate();
zip = zlib.createDeflate();
break;
}
if (unzip) {
res = res.pipe(unzip).pipe(iconv.decodeStream(charset));
stream = iconv.encodeStream(charset);
result = stream.pipe(zip);
} else {
res = res.pipe(iconv.decodeStream(charset));
result = stream = iconv.encodeStream(charset);
}
var rest = '';
res.on('data', function(data) {
var len = data.length - 7;
if (len > 0) {
data = (rest + data).replace(WHISTLE_SSL_RE, 'http://' + HTTPS_FLAG);
rest = data.substring(len);
data = data.substring(0, len);
stream.write(data, charset);
} else {
rest = data;
processFiles(fileStreams, callback) {
const results = [];
fileStreams[0]
.pipe(iconv.decodeStream("macintosh"))
.pipe(iconv.encodeStream("utf8"))
.pipe(csv({
objectMode: true,
delimiter: "\t",
newline: "\n",
columns: true,
}))
.on("data", (data) => {
if (data.bibRecordNumberLong) {
const result = searchByProps(data, propMap);
if (result.id) {
result.lang = "en";
results.push(result);
}
}
})
.on("error", callback)
function convertFrom (encoding) {
return splicer([
iconv.decodeStream(encoding),
iconv.encodeStream('utf8')
])
}
const pkg = {
other: {
contentType,
contentLength
}
}
const multibyteEncodings = [ 'CP932', 'CP936', 'CP949', 'CP950', 'GB2312', 'GBK', 'GB18030', 'Big5', 'Shift_JIS', 'EUC-JP' ]
if (multibyteEncodings.includes(charset)) {
log('converting multibyte encoding from', charset, 'to utf-8')
res.body = res.body
.pipe(iconv.decodeStream(charset))
.pipe(iconv.encodeStream('utf-8'))
}
return [pkg, res]
})
}
export function encodeStream(encoding: string, options?: { addBOM?: boolean }): NodeJS.ReadWriteStream {
return iconv.encodeStream(toNodeEncoding(encoding), options);
}