NODE.JS how to loop stdin process - javascript

When i try loop my input stdin process in file parser.js it run only once, then it exit program.
How to loop my input infinitely?
input.js
function stdinLineByLine() {
return new Promise(resolve => {
console.log("Enter command: " + "\n")
let lines = '';
let buff = '';
process.stdin
.on('data', data => {
buff += data;
lines = buff.split(/\r\n|\n/).join(' ');
input += lines;
buff = "";
if(lines[lines.length - 2] === ';'){
process.stdin.emit('end');
}
})
.on('end', () => {
resolve(input);
process.stdin.destroy();
});
});
}
module.exports = stdinLineByLine;
parser.js
const input = require('./input');
const commands = require('./commands');
let createReg = /^CREATE+\s+\b[a-zA-Z0-9_]+\b$/i;
let insertReg = /^INSERT+\s+[a-zA-Z0-9_]+\s+\B"\b[a-zA-Z0-9_\w\s]+\b"+\B$/i;
let print_indexReg = /^PRINT_INDEX+\s+\b[a-zA-Z0-9_]+\b$/i;
let searchReg = /^SEARCH+\s+\b[a-zA-Z0-9_]+\b$/i;
let searchRegWhere = /^SEARCH+\s+[a-zA-Z0-9_]+\s+WHERE+\s+"\b[a-zA-Z0-9_]+\b"$/i;
let searchWhereDashReg = /^SEARCH+\s+[a-zA-Z0-9_]+\s+WHERE+\s+"\b[a-zA-Z0-9_]+\b"+\s+-+\s+"\b[a-zA-Z0-9_]+\b"$/i;
let searchWhereHooksReg = /^SEARCH+\s+[a-zA-Z0-9_]+\s+WHERE+\s+"\b[a-zA-Z0-9_]+\b"+\s+<+\d+>+\s+"\b[a-zA-Z0-9_]+\b"$/i;
function ifValid(str){
if(createReg.test(str))
return 0;
if(insertReg.test(str))
return 1;
if(print_indexReg.test(str))
return 2;
if(searchReg.test(str))
return 3;
if(searchRegWhere.test(str))
return 4;
if(searchWhereDashReg.test(str))
return 5;
if(searchWhereHooksReg.test(str))
return 6;
throw new Error("INVALID INPUT");
}
input().then(input => {
let tokens = [];
let token = ""
try {
for (let i = 0; i < input.length; i++) {
if (input[i] === " " || input[i] === ";") {
tokens.push(token);
token = "";
} else {
token += input[i];
}
}
tokens = tokens.filter(tokens => tokens !== "");
switch(ifValid(tokens.join(" "))){
case 0:
commands.create(tokens[1]);
break;
case 1:
commands.insert(tokens[1], tokens.slice(2,));
break;
case 2:
commands.print_index(tokens[1]);
break;
case 3:
commands.search(tokens[1]);
break;
case 4:
commands.searchWhere(tokens[1], tokens[3]);
break;
case 5:
commands.searchWhereDash(tokens[1], tokens[3], tokens[5]);
break;
case 6:
commands.searchWhereHooks(tokens[1], tokens[3], tokens[4][1], tokens[5]);
break;
}
console.log(tokens);
}catch(error){
console.log(error);
}
});
I tried loop it by wrapping input().then() in a function that return promise and when that function finished input it call itself recursively. it will loop my input function, but new user input includes previous user input

Related

function getting executed at the start of for loop instead of after it

So I'm trying to execute some code after a for loop in nodejs file
let DirectoriesDHRD = getDirectories('.\\captures\\ref');
for (let i = 0; i < DirectoriesDHRD.length; i++) {
//some things I coded
if (i==DirectoriesDHRD.length-1)
{
//execute this
}
I've already seen a similar issue and one of the solution was to do that conditional statement, but it didn't work for me. I've tried to debug the issue and I noticed that the loop iterator I get to its limit value (DirectoriesDHRD.length-1), then it executes the code inside the conditional statement before executing the other code outside of it.
It s like this is what happening:
for (let i = 0; i < DirectoriesDHRD.length; i++)
{
// conditional statement code
}
for (let i = 0; i < DirectoriesDHRD.length; i++)
{
// other code
}
How can I fix this ?
EDIT: here's the code I've been working on
for (let i = 0; i < DirectoriesDHRD.length; i++) {
let DirectoriesSCENARIO= getDirectories('.\\captures\\ref\\'+DirectoriesDHRD[i]);
for (let j=0;j<DirectoriesSCENARIO.length;j++){
let dirtestresult = path.join('VisualTests',DirectoriesDHRD[i],DirectoriesSCENARIO[j]);
let dirref = path.join('captures','ref',DirectoriesDHRD[i],DirectoriesSCENARIO[j]);
let dirtest= path.join('captures','test',DirectoriesDHRD[i],DirectoriesSCENARIO[j]);
if (!fs.existsSync(dirtestresult)) {
fs.mkdirSync(dirtestresult,{ recursive: true });
}
fs.readdir(dirref, function (err, files) {
let TestResult="";
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
else if (files.length == 0) {
console.log("test skipped or pending");
TestResult="skipped/pending";
}
else {
TestResult="passed";
files.forEach(function (file,Status) {
var filepathref=path.join(dirref, file);
var filepathtest=path.join(dirtest,file);
if (fs.existsSync(filepathtest))
{
Status=compare(file,filepathref,filepathtest,dirtestresult);
if (Status=="failed"){
TestResult="failed";
}
}
else {
console.log("test missing screenshots");
TestResult="failed";
}
});
}
switch (TestResult) {
case 'passed':
pass++;
break;
case 'skipped/pending':
skip++;
break;
case 'failed':
fail++;
break;
default:
console.log(`error`);
}
let Test={
name : DirectoriesSCENARIO[j],
tag : DirectoriesDHRD[i],
status : TestResult
};
let TestJson=JSON.stringify(Test, null, "\t");
alltests.push(Test);
console.log(alltests[i]);
fs.writeFile(dirtestresult+"\\test.json", TestJson, (err) => {
if (err)
console.log(err);
else {
console.log("File written successfully\n");
}
});
});
}
if (i==DirectoriesDHRD.length-1)
{
let result=JSON.stringify({pass: pass,fail: fail, skip :skip},null,"\t");
fs.writeFile(".\\VisualTests\\result.json", result, (err) => {
if (err)
console.log(err);
else {
console.log("Result File written successfully\n");
}
});
let jsonresult=JSON.stringify(alltests,null,"\t");
console.log("///////////////////////");
console.log(jsonresult);
fs.writeFile(".\\VisualTests\\Tests_results.json",jsonresult,(err)=>{
if (err)
console.log(err);
else {
console.log("Result File written successfully\n");
}
});
}
}
getdirectories:
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path + '/' + file).isDirectory();
});
}
compare:
function compare(file,filepathref,filepathtest,dirtestresult){
var Status="passed";
var imageref = PNG.sync.read(fs.readFileSync(filepathref));
var imagetest=PNG.sync.read(fs.readFileSync(filepathtest));
var i=file.replace(/\.[^/.]+$/, "");
var {width, height} = imageref;
var diff = new PNG({ width, height });
var result=pixelmatch(imageref.data, imagetest.data, diff.data, width, height, { threshold: 0.4 ,alpha:1});
fs.writeFileSync(dirtestresult+"\\"+i+".png", PNG.sync.write(diff));
var percent=(result*100)/(width*height);
console.log(percent);
if (percent>15)
{
Status="failed";
}
return Status ;
}
You had a mix of some synchronous calls from the fs module and some asynchronous and the asynchronous ones were not waiting for your loops so things were going out of sequence.
Since this is a utility stand-alone script (not a server), you can just use the synchronous version of things everywhere and simplify your coding flow. Change fs.readdir() to fs.readdirSync() and fs.writeFile() to fs.writeFileSync().
Also, be sure to declare any variable before using it.
for (let i = 0; i < DirectoriesDHRD.length; i++) {
let DirectoriesSCENARIO = getDirectories('.\\captures\\ref\\' + DirectoriesDHRD[i]);
for (let j = 0; j < DirectoriesSCENARIO.length; j++) {
let dirtestresult = path.join('VisualTests', DirectoriesDHRD[i], DirectoriesSCENARIO[j]);
let dirref = path.join('captures', 'ref', DirectoriesDHRD[i], DirectoriesSCENARIO[j]);
let dirtest = path.join('captures', 'test', DirectoriesDHRD[i], DirectoriesSCENARIO[j]);
if (!fs.existsSync(dirtestresult)) {
fs.mkdirSync(dirtestresult, { recursive: true });
}
let files = fs.readdirSync(dirref);
let TestResult = "";
let Status;
if (files.length == 0) {
console.log("test skipped or pending");
TestResult = "skipped/pending";
} else {
TestResult = "passed";
files.forEach(function(file, Status) {
var filepathref = path.join(dirref, file);
var filepathtest = path.join(dirtest, file);
if (fs.existsSync(filepathtest)) {
Status = compare(file, filepathref, filepathtest, dirtestresult);
if (Status == "failed") {
TestResult = "failed";
}
} else {
console.log("test missing screenshots");
TestResult = "failed";
}
});
}
switch (TestResult) {
case 'passed':
pass++;
break;
case 'skipped/pending':
skip++;
break;
case 'failed':
fail++;
break;
default:
console.log(`error`);
}
let Test = {
name: DirectoriesSCENARIO[j],
tag: DirectoriesDHRD[i],
status: TestResult
};
let TestJson = JSON.stringify(Test, null, "\t");
alltests.push(Test);
console.log(alltests[i]);
fs.writeFileSync(dirtestresult + "\\test.json", TestJson);
console.log("File written successfully\n");
}
if (i == DirectoriesDHRD.length - 1) {
let result = JSON.stringify({ pass: pass, fail: fail, skip: skip }, null, "\t");
fs.writeFileSync(".\\VisualTests\\result.json", result);
console.log("Result File written successfully\n");
let jsonresult = JSON.stringify(alltests, null, "\t");
console.log("///////////////////////");
console.log(jsonresult);
fs.writeFileSync(".\\VisualTests\\Tests_results.json", jsonresult);
console.log("Result File written successfully\n");
}
}
Note that error handling with the Sync versions is via throwing an exception so if you want to catch those exceptions in order to continue rather than abort, you will need to put appropriate try/catch blocks in whatever scope you're trying to catch the errors. Right now the code assumes you put a try/catch block at a higher level and catch the error there.

I have created calculator without eval() in javascript. Now i want to store history(previous calculations) down there

** As we can see below i want to store all calculations in the history id tag using js. I tried many functions but not getting to the results. I want to know which paramter should i keep inside the function addToHistory so that it can easily print the input string when i click equal sign? Please help me with that.**
(function () {
const display = document.getElementById('display');
const withoutEval = (str) => {
let arr = str.split(' ');
switch (arr[1]) {
case '+':
return Math.round((+arr[0] + +arr[2]));
break;
case '-':
return Math.round((+arr[0] - +arr[2]));
break;
case '/':
return Math.round((+arr[0] / +arr[2]));
break;
case '*':
return Math.round((+arr[0] * +arr[2]));
break;
};
};
let booleanSign = true;
let count = 0;
let str = '';
const keyboard = document.getElementById('keyboard');
keyboard.addEventListener('click', (e) => {
let target = e.target;
if (target.value.match(/[0123456789\.]/)) {
if (booleanSign === true) {
display.textContent = '';
booleanSign = false;
};
display.textContent += target.value;
str += target.value;
};
if (target.value.match(/[^0123456789\.=c]/)) {
count += 1;
if (count === 2) {
display.textContent = withoutEval(str);
str = withoutEval(str);
count = 1;
};
str += ' ' + target.value + ' ';
booleanSign = true;
};
if (target.value.match('c')) {
count = 0;
booleanSign = true;
str = ''
display.textContent = '0';
};
if (target.value.match('=')) {
count = 0;
display.textContent = withoutEval(str);
str = withoutEval(str);
booleanSign = false;
addToHistory();
};
let history = '';
function addToHistory(value) {
history += value;
document.getElementById('history').innerText = history;
}
});
})();

How do I run code after all threads have finished running?

I have a multithreaded web crawler that downloads a website and stores it in a database (which takes around 4 minutes). To make the crawling faster, I used node.js cluster module, but I have a problem, I want to iterate over to the next segment of the while loop, after all the threads have done their processes, not as soon as they start. How do I make sure all my threads are concluded and then move on?
Here is the relevant code in the main while loop:
while (indexSize !== indexSizeLimit) {
const queueLength = queue.length;
const numberOfThreads = Math.min(numberOfCPUs, queueLength);
const threadAllocations = Array(numberOfThreads).fill(0);
let queuesAllocated = 0;
const queueChunks = [];
function fillQueueChunks() {
loop: while (true) {
for (let i = 0; i < numberOfThreads; i++) {
threadAllocations[i] += 1;
queuesAllocated += 1;
if (queuesAllocated === queueLength) {
break loop;
};
};
};
let start = 0;
for (let threadAllocation of threadAllocations) {
const end = start + threadAllocation;
queueChunks.push(queue.slice(start, end));
start = end;
};
};
fillQueueChunks();
// Find out how to make multithreading finish, and then move on with the loop.
if (cluster.isMaster) {
for (let i = 0; i < numberOfThreads; i++) {
cluster.fork();
};
} else {
const chunk = queueChunks[cluster.worker.id - 1];
await Promise.all(chunk.map(function (url) {
return new Promise(async function (resolve) {
const webcode = await request(url);
if (webcode !== "Failure") {
indexSize += 1;
const document = new Document(url, webcode);
const hrefs = document.hrefs();
const hrefsQuery = Query(hrefs);
// Also make sure it is not included in indexed webpages.
const hrefIndividualized = hrefsQuery.individualize();
hrefIndividualized;
// Do something with hrefIndividualized in regards to maintaining a queue in the database.
// And in adding a nextQueue which to replace the queue in code with.
await document.save();
};
resolve("Written");
});
}));
process.exit(0);
};
};
Wrap the threading in a promise. You can check in the parent thread if there is a disconnect event, and if the amount of disconnects is equal to the number of threads, then you can resolve the promise.
Here is what I have
while (indexSize !== indexSizeLimit) {
let nextQueue = [];
const queueLength = queue.length;
const numberOfThreads = Math.min(numberOfCPUs, queueLength);
const threadAllocations = Array(numberOfThreads).fill(0);
let queuesAllocated = 0;
// queueChunks: [[{_id: ..., ...}], [...], ...]
const queueChunks = [];
function fillQueueChunks() {
loop: while (true) {
for (let i = 0; i < numberOfThreads; i++) {
threadAllocations[i] += 1;
queuesAllocated += 1;
if (queuesAllocated === queueLength) {
break loop;
};
};
};
let start = 0;
for (let threadAllocation of threadAllocations) {
const end = start + threadAllocation;
queueChunks.push(queue.slice(start, end));
start = end;
};
};
fillQueueChunks();
await new Promise(async function (resolve) {
if (cluster.isMaster) {
let threadsDone = 0;
for (let i = 0; i < numberOfThreads; i++) {
cluster.fork();
};
cluster.on("disconnect", function (_) {
threadsDone += 1;
if (threadsDone === numberOfThreads) {
resolve("Queue Processed");
};
});
} else {
const queueJob = queueChunks[cluster.id - 1];
await Promise.all(queueJob.map(function (queueItem) {
return new Promise(async function (resolve) {
const url = queueItem._id;
const webcode = await request(url);
if (webcode !== "Failure") {
const document = Document(url, webcode);
let hrefs = document.hrefs();
const hrefsQuery = Query(hrefs);
await document.save();
indexSize += 1;
hrefs = hrefsQuery.individualize();
const hrefIncidences = Promise.all(hrefs.map(function (href) {
return new Promise(async function (resolve) {
const incidences = await Site.countDocuments({
url: href
});
resolve(incidences);
});
}));
hrefs = hrefs.filter(function (_, i) {
return hrefIncidences[i] === 0;
}).map(function (href) {
return {
_id: href
};
});
await Queue.insertMany(hrefs);
nextQueue = nextQueue.concat(hrefs);
};
await Queue.deleteOne({
_id: url
});
resolve("Success");
});
}));
process.exit(0);
};
});
queue = nextQueue;
};

Wrong values with using gpu.js

I use my code in modes 'dev', 'gpu' and 'cpu'. When I run up my code in 'dev' mode I had right number of results word 'more' in almost 100mb file. There is 259200 words 'more'. But when I run it up in 'gpu' or 'cpu' mode, I have only 3 results from entire file. And second thing, even if I used 'gpu' and 'cpu' mode there is a time differences. When I used gpu it finish in 4000-4100ms and when I use cpu it finish in 2600-2700ms. I think using gpu should be faster than cpu in this searching. Hope for help!
const { GPU } = require('gpu.js');
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const gpu = new GPU({ mode: 'cpu' });
rl.question('Wprowadż szukaną frazę: ', (value) => {
let phrase = value;
let start = new Date();
fs.readFile('plik2.txt', 'utf8', function(err, data) {
if (err) throw err;
let tempPhraseLen = phrase.length;
let tempDataLen = data.length;
let tempPhrase = [];
for (let i = 0; i < tempPhraseLen; i++) {
tempPhrase.push(phrase[i].charCodeAt(0));
}
let tempData = [];
for (let i = 0; i < tempDataLen; i++) {
tempData.push(data[i].charCodeAt(0));
}
function kernelFunction(pFunc, arrFunc, pLen, arrLen) {
let done = false;
let counter = 0;
for (let i = 0; i < arrLen; i++) {
if (arrFunc[i] === pFunc[0]) {
let isChosen = true;
let k = 0;
for (let j = i; j < i + pLen; j++) {
isChosen = true;
if (arrFunc[j] !== pFunc[k]) {
isChosen = false;
break;
}
k++;
}
if (isChosen) {
done = true;
counter++;
}
}
}
if (!done) {
return 0;
} else {
return counter;
}
}
const kernel = gpu.createKernel(kernelFunction, {
output: [ 1 ],
tactic: 'precision'
});
const result = kernel(tempPhrase, tempData, tempPhraseLen, tempDataLen);
if (result[0] === 0) {
console.log('Nie udało się znaleźć podanego wzorca');
} else {
console.log('Znalazłeś ' + result[0] + ' powtórzeń/nia frazy "' + phrase + '"');
}
let end = new Date() - start;
console.info('Time: %dms', end);
});
rl.close();
});

iframe element returns undefined after onload event?

EDIT: Is there no one who can shed some light on this issue? Anything would be appreciated. :)
I have a script that is supposed to check to see if an elements html contains a given string..
When these elements do exist, my code throws this error: Uncaught TypeError: Cannot read property 'outerHTML' of null
This is the line: let check = document.querySelector("#iframe_${globalI}").contentWindow.document.querySelector(".Row"+inc).outerHTML
I then check to see if the string includes a check string.. IE: check.includes("Pre Trip")
If I run this line directly in the console it works and returns true... So what is going on here..?? How can I get this check to pass..?
I have this check executing after a setTimeout of 20 seconds, then wrapped again in another setTimeout for 500ms as I was trying to figure this out..
Also, I need to note that there are no XSS / CORS issues.
Here is my code..
function checkRowCount(x){
console.log("Row count called on "+x);
let rowCount = 0;
for(let i = 0; i < 30; i++){
if(typeof(document.querySelector(`#iframe_${x}`).contentWindow.document.querySelector('.Row'+i)) != 'undefined' && document.querySelector(`#iframe_${x}`).contentWindow.document.querySelector('.Row'+i) != null){
rowCount++;
}
}
console.log(rowCount);
return rowCount;
}
let globalCompiler = []; //globalCompiler[globalI] = {unit: unitNumber[globalI], data: ["X", " ", "NO POST TRIP]}
let unitNumber = [1031,1743,1744,1986,3239,3256,3257,4024,4062,4063,4064,4065,4247,4309,4315,4326,4327,4334,4335,4337,4350,4382,4385,7166,7380,7381,8765,8823,8945,8950,8988,10720,17045,17163,40014,40069,40122,40380,80129,80188,80700,80701,80702,80728,80831,80852,80875,"80876","81027","81038","401288","401306","402409","60099T","CH889","CH890","SR31077","T19","U5509","U6660","U6667","U6675","U8854","US1025T"];
let url = "http://winweb.cleanharbors.com/Vehicle/VehicleTDSearch.aspx?SearchType=DVIR";
function iframeLoaded(selector, unit, setDate, callback){
document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#txtStartDate").value = setDate;
document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#txtEndDate").value = setDate;
document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#txtVhcleNo").value = unit;
document.querySelector(`#iframe_${selector}`).contentWindow.document.querySelector("#btnRetrieve").click();
}
let loadFinished = {};
for(let dec = 0; dec < unitNumber.length; dec++){
loadFinished[unitNumber[dec]] = false;
}
console.log(loadFinished);
for(let globalI = 0; globalI < 3; globalI++){
globalCompiler[globalI] = {unit: unitNumber[globalI], data: []};
let iframeObj = document.createElement('iframe');
iframeObj.id = `iframe_${globalI}`;
iframeObj.hidden = false;
iframeObj.src = url;
iframeObj.onload = () => {
if (loadFinished[unitNumber[globalI]] == false) {
loadFinished[unitNumber[globalI]] = true;
let setDate = "11/01/2019";
iframeLoaded(globalI, unitNumber[globalI], setDate);
console.log("iframeloaded called on " + globalI);
setTimeout(() => {
setTimeout(() => {
let dateCheckObject = {}, rowCount = checkRowCount(globalI), trackingArr = [];
if (rowCount == 0) {
globalCompiler[globalI].data.push(" ");
} else {
for (let inc = 1; inc <= rowCount; inc++) {
//let check = $('#iframe_'+globalI).contents().find(`.Row` + inc).html().includes("Pre Trip");
let check = document.querySelector(`#iframe_${globalI}`).contentWindow.document.querySelector(".Row"+inc).outerHTML
if (check.includes("Pre Trip")) {
dateCheckObject.pre = true;
} else {
dateCheckObject.post = true;
}
}
if(dateCheckObject.pre && dateCheckObject.post) {
console.log("X");
globalCompiler[globalI].data.push("X");
dateCheckObject = {};
} else if (dateCheckObject.pre == 'undefined') {
console.log("NO PRE");
globalCompiler[globalI].data.push("NO PRE TRIP");
dateCheckObject = {};
} else {
console.log("NO POST");
globalCompiler[globalI].data.push("NO POST TRIP");
dateCheckObject = {};
}
}
},500);
}, 20000);
}
};
document.body.appendChild(iframeObj);
console.log("Global Loop called");
}
```
A for loop ran one count too far...
e.g.: for (let inc = 1; inc <= rowCount; inc++)
Should have been for (let inc = 1; inc < rowCount; inc++)

Categories

Resources