Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default (stream, pattern) => new Promise((resolve, reject) => {
const search = new StreamSearch(pattern);
let matched = false;
search.on('info', isMatch => {
if (isMatch) {
resolve();
matched = true;
}
});
search.on('error', err => reject(err));
search.on('finish', () => matched ? null : reject(new Error(`Match ${pattern} not found`)));
stream.on('data', data => search.push(data));
});
builtinSniff: async function (opts, needles) {
const {archivePath} = opts
let result = null
let searches = []
let onInfo = (needle, format, isMatch, data, start, end) => {
if (!isMatch) return
log(opts, `builtinSniff: found needle ${needle}`)
result = format
}
for (const needle of Object.keys(needles)) {
const format = needles[needle]
const search = new StreamSearch(needle)
search.on('info', onInfo::partial(needle, format))
searches.push(search)
}
const reader = sf.createReadStream(archivePath, {encoding: 'binary'})
reader.on('data', (buf) => {
for (let search of searches) {
search.push(buf)
}
})
await sf.promised(reader)
return result
},