Reading big .txt and checking if string contains on response body - node.js

I have this code:
var fs = require('fs');
var readline = require('readline');
var request = require("request");
var filename = process.argv[2];
readline.createInterface({
input: fs.createReadStream(filename),
terminal: false
}).on('line', function(line) {
sep = line.split("|");
id = sep[0];
name = sep[1];
var options = { method: 'POST',
url: 'https://api.com/mobile_authentications',
proxy: '...',
headers:
{ Host: 'api.com',
'Content-Type': 'application/json',
'Accept-Language': 'pt-BR',
'X-Client-Info': 'Timezone%3D-Infinity%26device_id%3D578e4ac016b69248%26utc_offset%3D-0300%26app_id%3D7092%26resolution%3D720x1280%26connectivity%3DWIFI%26site_id%3DMLB%26density%3D320%26user_id%3Dguest' },
body: '{"authentication_request":{"client_id":"'+id+'|'+name+'"}}' };
request(options, function (error, response, body) {
if (error) throw new Error(error);
if(body.includes('ATTEMPS'))
{
console.log("CAPTCHA");
}
//console.log(body);
});
});
First question: How can I check if body response contains "ATTEMPS" string?
Another problem: When the ids.txt file has more than 100 lines the script returns me this error:
if (error) throw new Error(error);
^
Error: Error: tunneling socket could not be established, statusCode=502
at Request._callback (C:\Users\PC\Desktop\top\convert.js:65:20)
at self.callback (C:\Users\PC\Desktop\top\node_modules\request\request.js:185:22)
at Request.emit (events.js:182:13)
at Request.onRequestError (C:\Users\PC\Desktop\top\node_modules\request\request.js:881:8)
at ClientRequest.emit (events.js:182:13)
at ClientRequest.onConnect (C:\Users\PC\Desktop\top\node_modules\tunnel-agent\index.js:168:23)
at Object.onceWrapper (events.js:273:13)
at ClientRequest.emit (events.js:182:13)
at Socket.socketOnData (_http_client.js:475:11)
at Socket.emit (events.js:182:13)
That is, the script is not able to work with large files?
Thanks.

Related

Random Fetch Error when I restart my program

I'm looping through this array of names and using each name to change the end point in the URI. The data from the end points is then stored in a Promise.all() array. I then run the array through a for loop and outputs the data into an EJS file. This works sometimes. However sometimes the console logs the error at the bottom of this post. When I increase the amount of endpoints i'm requesting this error happens everytime. The error appears to be very random and the error stops at random endpoints. I'm assuming i'm maybe sending to many requests? I eventually want to loop through around 5000 endpoints in this code but i'm struggling just loop through 20. Any help is much appreciated.
body: new URLSearchParams({ grant_type: 'client_credentials' }),
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(appId + ':' + appSecret).toString('base64')
}
})
.then(response => response.json())
.then(token2 => secondtoken(token2))
function secondtoken(token2){
let accesstoken = token2.access_token;
const promises = [];
for(var i = 0; i < 20;i++){
promises.push(fetch('https://us.api.blizzard.com/profile/wow/character/'+ slug[i]+'/'+ lowercasenames[i] +'?namespace=profile-us&locale=en_US', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accesstoken}`
}
})
.then(response => response.json()))
}
Promise.all(promises)
.then(data => { profiledata(data)})
};
function profiledata(data){
const name = []
for(var i = 0; i < data.length;i++){
name.push(data[i].name)
};
console.log(name)
res.render('index', {
leaderboard: diomerda,
rank: rank,
rating: rating,
slug: slug,
faction: faction,
statsplayed: statsplayed,
statswon: statswon,
statslost: statslost,
race: racename,
name: name,
})
}}});```
The error is below:
D:\Coding\Form 1\node_modules\node-fetch\lib\index.js:1461
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
^
FetchError: request to https://us.api.blizzard.com/profile/wow/character/area-52/toonah?namespace=profile-us&locale=en_US failed, reason: read ECONNRESET
at ClientRequest.<anonymous> (D:\Coding\Form 1\node_modules\node-fetch\lib\index.js:1461:11)
at ClientRequest.emit (node:events:520:28)
at TLSSocket.socketErrorListener (node:_http_client:442:9)
at TLSSocket.emit (node:events:520:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
type: 'system',
errno: 'ECONNRESET',
code: 'ECONNRESET'
}

Howo to get IPP endpoint

I'm using the ipp npm module to send a print job from a google cloud function. I believe I have set up the printer correctly but I don't know how I'm supposed to know the exact uri for sending the print job.
The printer model is Brother MFC-L3770CDW
Here is how my settings look in the web view for the printer configuration.
And here is the function code.:
var ipp = require('ipp');
var PDFDocument = require('pdfkit');
var doc = new PDFDocument;
doc.text("Hello World");
var buffers = [];
doc.on('data', buffers.push.bind(buffers));
doc.on('end', function () {
var printer = ipp.Printer("https://10.0.0.55:443");
var file = {
"operation-attributes-tag":{
"requesting-user-name": "User",
"job-name": "Print Job",
"document-format": "application/pdf"
},
data: Buffer.concat(buffers)
};
printer.execute("Print-Job", file, function (err, res) {
if(err) {
console.log(err);
}
else{
console.log("Printed: "+res.statusCode);
}
});
console.log('executing');
});
doc.end();
console.log('finished executing');
I have tried various uris such as
https://10.0.0.55:631
https://10.0.0.55:443
https://10.0.0.55:631/ipp
https://10.0.0.55:631/ipp/printer
Sometimes I get an error like:
"Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1148:19)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
code: 'ECONNRESET',
path: null,
host: '10.0.0.55',
port: '631',
localAddress: undefined }"

Error: socket hang up in Http Request made in Node JS using request-promise causing for loop to restart

I am trying to make an Http Request using request-promise inside a for loop. But it seems if a Http Request takes long, request-promise closes the connection.
This behavior is ok but what I am not able to grasp is the for loop starts from 0 again after the error is printed.
Below is the code
const rp = require('request-promise');
async function stepIterator(processingSteps, documentId) {
var finalResult = null;
for (var step = 0, len = processingSteps.length; step < len; step++) {
if (step === 0 || step === 1 || step == 2 || step == 3) {
try {
console.log('Calling step ', step);
let url = 'http://internal-server:8080/process';
let collection = getCollection(documentId);
let splitText = getSPlit(documentId);
let outputFormat = 'xmi';
let documentObject = await callServer(url, collection, splitText, outputFormat);
finalResult = documentObject;
} catch (error) {
console.log("Error");
}
}
}
return finalResult;
}
async function callServer(url, collection, splitText, outputFormat) {
var options = {
method: 'POST',
uri: url,
headers: {
'Content-Type': 'multipart/form-data',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive '
},
formData: {
collection: collection,
text: splitText,
output: outputFormat
}
};
return rp(options)
}
The complete error trace is as follows
{ RequestError: Error: socket hang up
at new RequestError (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\errors.js:14:15)
at Request.plumbing.callback (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\plumbing.js:87:29)
at Request.RP$callback [as _callback] (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\plumbing.js:46:31)
at self.callback (D:\New_Projects\new-data-access-layer\node_modules\request\request.js:185:22)
at Request.emit (events.js:182:13)
at Request.onRequestError (D:\New_Projects\new-data-access-layer\node_modules\request\request.js:881:8)
at ClientRequest.emit (events.js:182:13)
at Socket.socketOnEnd (_http_client.js:425:9)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19) name: 'RequestError', message: 'Error: socket hang up', cause:
{ Error: socket hang up
at createHangUpError (_http_client.js:322:15)
at Socket.socketOnEnd (_http_client.js:425:23)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }, error: { Error: socket hang up
at createHangUpError (_http_client.js:322:15)
at Socket.socketOnEnd (_http_client.js:425:23)
at Socket.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }, options: { method: 'POST',
uri: 'http://internal-server:8080/process',
json: true,
headers: { Connection: 'keep-alive ' },
body:
{ docSplitId: [Array],
_id: 5c579d84812acb17ec74ac39,
contentType: 'application/pdf',
location:
'C:\\Users\\newuser\\AppData\\Local\\Temp\\2\\report.pdf',
docModelVersion: '1',
visualMetaDataId: null,
categoryId: '5c52a72f6df294140c0535bc',
deductedInfo: null,
status: 'New',
isDeleted: false,
metadata: [Object],
detailedStatus: [Array] },
callback: [Function: RP$callback],
transform: undefined,
simple: true,
resolveWithFullResponse: false,
transform2xxOnly: false }, response: undefined }
Obviously the socket is hanging! Don't bother with http, it is a little complex. Use node unirest and it closes the data stream.
var unirest = require('unirest');
var req = unirest('POST', 'localhost:3200/store/artifact/metamodel')
.attach('file', '/home/arsene/DB.ecore')
.field('description', 'We are trying to save the metamodel')
.field('project', '6256d72a81c4b80ccfc1768b')
.end(function (res) {
if (res.error) throw new Error(res.error);
console.log(res.raw_body);
});
Hope this helps!

Nodejs, soap, callback is not a function

first i got pfx certficate which a exported it to pem and key with openssl (if this could be an issue). I trid to use client.setSecurity(new soap.ClientSSLSecurit ()) but then i get client undefined.
my code goes like this:
var soap = require('soap'),
fs = require('fs');
var url = 'tes.com';
var auth = "Basic " + new Buffer("user" + ":" + "password").toString("base64")
soap.createClient( url, {wsdl_options: {
cert: fs.readFileSync('cert/certificate.pem'),
key: fs.readFileSync('cert/certificate.key')
}
}, { wsdl_headers: {Authorization: auth} }, function(err, client) {
client.describe();
});
I am getting this error and do not know what is wrong:
callback(err, wsdl && new Client(wsdl, endpoint, options));
^
TypeError: callback is not a function
at D:\web\mobile\node_modules\soap\lib\soap.js:50:5
at D:\web\mobile\node_modules\soap\lib\soap.js:33:16
at D:\web\mobile\node_modules\soap\lib\wsdl.js:2167:9
at Request._callback (D:\web\mobile\node_modules\soap\lib\http.js:114:14)
at self.callback (D:\web\mobile\node_modules\request\request.js:187:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (D:\web\mobile\node_modules\request\request.js:813:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
tnx
miha
Your arguments is not correct. Please correct like this :
soap.createClient( url, {wsdl_options: {
cert: fs.readFileSync('cert/certificate.pem'),
key: fs.readFileSync('cert/certificate.key')
},
wsdl_headers: {Authorization: auth} }, function(err, client) {
client.describe();
});

API request in Node returning error

I am making a simple http request to the Sphere Engine API with some request parameters . However, I cannot interpret the error .
API specification : http://sphere-engine.com/services/docs/compilers#status
Code:
http = require('http') ;
var info = {
sourceCode: 'print 3+4',
language: 4,
input: ''
} ;
var infoString = JSON.stringify(info);
var options = {
host: 'api.compilers.sphere-engine.com',
port: 80,
path: '/api/v3/submissions?access_token=b11bf50b8a391d4e8560e97fd9d63460',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': infoString.length
}
} ;
var req = http.request(options,function(res) {
res.setEncoding('utf-8');
var responseString = '' ;
res.on('data', function(data) {
responseString += data ;
});
res.on('end', function() {
var resultObject = JSON.parse(responseString);
});
} );
req.write(infoString);
req.end();
Error:
undefined:0
^
SyntaxError: Unexpected end of input
at Object.parse (native)
at IncomingMessage.<anonymous> (/Users/sarthakmunshi/nodetry.js:29:27)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:943:16
at process._tickCallback (node.js:419:13)
This error caused by JSON.parse(responseString);. You get response as not-json string (XML, HTML?), but try to parse it as a json.
You can use xml-stream library to parse XML.

Resources