我正在阅读一个 csv 文件,每一行都包含一个网站 url。我有这个功能来读取文件:
function readCSV(csv){
var lines=csv.split("\n");
var result = [];
var headers=lines[0].split(",");
// for every line of the file I call check_page function to check the policies (csp and xfo)
Promise.all(
lines.map(line => {
var obj = {};
var currentline=line.split(",");
console.log("currentline: "+currentline[1])
return check_page("https://www."+currentline[1])
})
).then(() => console.log('it worked')).catch(err => console.log(err));
}
该函数调用内部的另一个函数以通过 http-request 获取 csp 和 xfo 标头。
async function check_page(web_page){
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(web_page)
console.log("MAIN: "+page.mainFrame().url())
/* I send for every iframe an http request for retrieve the policies from http header */
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var req = new XMLHttpRequest();
console.log("FACCIO LA GET: "+page.mainFrame().url())
req.open('GET', page.mainFrame().url(), false);
req.send(null)
var headers = req.getAllResponseHeaders().toLowerCase();
var arr = headers.trim().split(/[\r\n]+/);
// Create a map of header names to values
var headerMap = {};
arr.forEach(function (line) {
var parts = line.split(': ');
var header = parts.shift();
var value = parts.join(': ');
headerMap[header] = value;
});
await browser.close();
}
如果我的行数很少,我的代码就可以工作:但是如果我有一个包含 100 行的文件,我会遇到这个错误:
(node:1076) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:1076) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:1076) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGTERM listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:1076) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGHUP listeners added to [process]. Use emitter.setMaxListeners() to increase limit
我想我必须以某种方式分配工作,但我不知道如何。