nodejs: read ECONNRESET - node.js

I am developing a Web crawl program for fun, but when i use superagent to get page, read ECONNRESET occurred.
const https = require('https');
https.globalAgent.options.secureProtocol = 'TLSv1_2_method';
const superagent = require('superagent-use')(require('superagent'))
superagent.use(require('superagent-verbose-errors'))
const cheerio = require('cheerio');
const baidu = "https://www.baidu.com/s?wd=";
const sogou = "https://www.sogou.com/web?query=";
function search(key) {
const fullUrl = baidu + key;
return superagent
.get(fullUrl)
.on('error', error => console.log(error))
.then(response => {
return parsehtml(response.body);
});
}
function parsehtml(htmlBody) {
}
module.exports=search;
The following is error details:
response:undefined
stack:"Error: read ECONNRESET\n at _errnoException (util.js:992:11)\n at
TLSWrap.onread (net.js:618:25)"
status:500
syscall:"read"
and security about https://www.baidu.com
Security in Chrome
How can fix this issue, thanks a lot.

Related

MSW v0.40 Error: thrown: Exceeded timeout of 5000 ms for a test

I've been using MSW since v0.35.0. Recently I updated it to v0.40.1 and now it seems like MSW is not intercepting the request to the servers and I'm getting the following error.
Here is my test code.
import axios from 'axios';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
const path = 'login/';
const accessToken = 'AccessTokenValue';
const correctCredential = { email: 'test#email.com', password: 'password' };
const server = setupServer(
rest.post(path, (req, res, ctx) => {
return res(ctx.status(200), ctx.json({ data: { access: accessToken } }));
}),
);
beforeAll(() => server.listen());
afterAll(() => server.close());
afterEach(() => server.resetHandlers());
describe('Login', () => {
test('Case: Success', async () => {
let token = '';
await axios
.post('https://test.com' + path, correctCredential)
.then((response) => (token = response.data.data.access));
expect(token).toBe(accessToken);
});
});
And this is the error I get.
Error: Request failed with status code 400
at createError (<my_local_path>)
at settle (<my_local_path>)
at IncomingMessage.handleStreamEnd (<my_local_path>)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Here are the versions of the other packages I use.
jest: 27.0.6
axios: 0.25.0
I read Examples from MSW and I don't see any problem with my implementation.
Perhaps this issue is relevant.
https://github.com/mswjs/msw/issues/1125
This issues was fixed 5 days ago, so I believe a corrected version will be released in the near future.
btw, downgrading to 0.36.8 is temporary solution.

Getting getaddrinfo ENOTFOUND when trying to connect to my AWS Neptune with node.js and gremlin

I am trying to connect to my Amazon Neptune instance from a API GW. I am using Node.js and Lambda
My YML looks like this
NeptuneDBCluster:
Type: "AWS::Neptune::DBCluster"
Outputs:
NeptuneEndpointAddress:
Description: Neptune DB endpoint address
Value: !GetAtt NeptuneDBCluster.Endpoint
Export:
Name: !Sub ${env:STAGE}-neptune-endpoint-address
My code looks like this
const gremlin = require('gremlin');
const {
NEPTUNE_ENDPOINT
} = process.env;
const { cardinality: { single } } = gremlin.process;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
async function createUserNode(event, context, callback) {
const url = 'wss://" + NEPTUNE_ENDPOINT + ":8182/gremlin';
const dc = new DriverRemoteConnection(url);
const parsedBody = JSON.parse(event.body);
try {
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
const vertex = await g.addV(parsedBody.type)
.property(single, 'name', parsedBody.name)
.property(single, 'username', parsedBody.username)
.property('age', parsedBody.age)
.property('purpose', parsedBody.purpose)
.next();
if (vertex.value) {
return callback(null, {
statusCode: 200,
body: vertex.value
});
}
} catch (error) {
console.error(error);
}
};
I keep getting the folowing error from Cloudwatch (I also tried creating a local js file and it gives the same error)
ERROR Error: getaddrinfo ENOTFOUND my-url
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'my-url'
}
I also tried to write the endpoint without getting it from process.env and I am still facing the same issue. What am i missing?
Alright for anyone being as confused as I am when trying Neptune for first time. You need to create a database instance aswell, I thought the Serverless Framework would do this to me but now I know.

Making POST call outside of the GCP environment to create workload identity

I need to access GCP resources outside of the GCP environment from AWS using a AWS lambda. So, I found this document [accessing GCP resources from AWS][1] which provides a way to access the GCP resources and asks to create a workload identity pool.
I need to create a Workload identity pool in GCP using a REST API call. The REST API call has to run outside of the GCP environment, that is, in this case from the AWS environment. My GCP's IAM user doesn't have privileges to create a workload identity pool (due to org policy reasons). But, I've a service account which has admin privileges to create a workload identity pool and all the required permissions to access the required resources once the pool is created.
I'm a newbie to GCP and figuring out ways of calling a POST REST API call using my service account credentials. Any help is much appreciated.
Edited
Pasting the sample code I've been trying to make the REST call.
const {google} = require('googleapis');
const util = require('util');
const https = require('https');
const aws4 = require('aws4');
const auth = new google.auth.GoogleAuth({
keyFile: 'serviceAccountCreds.json',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
async function createSignedRequestParams(jsonBodyParams) {
const getAccessToken = await auth.getAccessToken();
console.log(`createSignedRequestParams() - this.credentials:${getAccessToken !== null}`);
// Set up the request params object that we'll sign
const requestParams = {
path: '/v1beta/projects/serviceAccountdev/locations/global/workloadIdentityPools?workloadIdentityPoolId=12345',
method: 'POST',
host: 'iam.googleapis.com',
headers: { 'Content-Type': 'application/json' },
body: jsonBodyParams
};
console.log(`createSignedRequestParams() - (signed) requestParams:${util.inspect(requestParams)}`);
return requestParams;
}
const jsonBodyParams = {
"description": "createWorkloadIdentityPool",
"display-name": "devAccount"
};
async function request(requestParams, jsonBodyParams) {
console.log(`request() requestParams:${util.inspect(requestParams)} jsonBodyParams:${jsonBodyParams}`);
// return new pending promise
return new Promise((resolve, reject) => {
const req = https.request(requestParams);
if (['POST', 'PATCH', 'PUT'].includes(requestParams.method)) {
req.write(jsonBodyParams);
}
req.end();
// Stream handlers for the request
req.on('error', (err) => {
console.log(`request() req.on('error') err:${util.inspect(err)}`);
return reject(err);
});
req.on('response', (res) => {
let dataJson = '';
res.on('data', chunk => {
dataJson += chunk;
});
res.on('end', () => {
const statusCode = res.statusCode;
const statusMessage = res.statusMessage;
const data = JSON.parse(dataJson);
console.log(`request() res.on('end')`, { statusCode, statusMessage, data });
resolve({ statusMessage, statusCode, data });
});
});
});
}
async function postTheRequest(reqParams, jsonBodyParams) {
try {
const response = await request(reqParams, jsonBodyParams);
return response;
} catch (error) {
console.log(error);
}
}
reqParams = createSignedRequestParams(jsonBodyParams);
postTheRequest(reqParams, jsonBodyParams);
output of the above code
[Running] node "c:\Users\av250044\.aws\GCP_Code_examples\registerTheWorkloadIdentifier.js"
request() requestParams:Promise { <pending> } jsonBodyParams:[object Object]
request() req.on('error') err:{ Error: connect ECONNREFUSED 127.0.0.1:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 443 }
{ Error: connect ECONNREFUSED 127.0.0.1:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 443 }
Wondering if I'm passing the PATH and host are correct. Please let me know your thoughts on my code sample.
[1]: https://cloud.google.com/iam/docs/access-resources-aws#iam-workload-pools-add-aws-rest

upload failed: { Error: unable to verify the first certificate

I wrote a small code in AWS-Lambda(Node.js) to send the file to some API. I am able to run the code but i am getting the upload error.
Error: Function Logs:
START RequestId: 08ad7fab-3658-11e8-8483-a7fbad976cb7 Version: $LATEST
2018-04-02T09:27:17.787Z 08ad7fab-3658-11e8-8483-a7fbad976cb7 upload failed: { Error: unable to verify the first certificate
at Error (native)
at TLSSocket.<anonymous> (_tls_wrap.js:1092:38)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket._finishInit (_tls_wrap.js:610:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }
END RequestId: 08ad7fab-3658-11e8-8483-a7fbad976cb7.
The code which i have written is :
console.log('Loading Function');
const fs = require('fs');
const request = require('request');
const url = require('url');
const https = require('https');
https.globalAgent.options.ca = rootCas;
var rootCas = require('ssl-root-cas').create();
// default for all https requests
// (whether using https directly, request, or another module)
require('https').globalAgent.options.ca = rootCas;
exports.handler = (event, context, callback) => {
var formData = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/myfile.csv'),
// Pass multiple values /w an Array
// attachments: [
// fs.createReadStream(__dirname + '/myfile.txt'),
// fs.createReadStream(__dirname + '/myfile.txt')
// ],
};
var req = request.post({url:'https://abc.xyz.com:443/file/', formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
};
When i am trying to run the same code locally, i am not getting any error even the module ssl is able to download all the certificates. The same when i am trying in AWS-Lambda , the code wont be able to download the certificates. I even tried in the Configure test with adding the JSON as rejectUnauthorized": false ,still not able to overcome with the error.

twilio-node unable to reach host "api.twilio.com" TLSWrap.onread

I am getting the following error when using the twilio node module
{ Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
{ status: 'ECONNRESET',
message: 'Unable to reach host: "api.twilio.com"' }
I am able to send SMS messages through Postman and their console interface but when trying it through NodeJS I get the error.
The code that I have is this:
var express = require('express');
var app = express();
var twilio = require('twilio');
var accountSid = 'ACXXXXXXXXXXXXXXX';
var apiKey = 'XXXXXXXXXXXXXXXXXXX';
var apiSecret = 'XXXXXXXXXXXXXX';
var twilioClient = new twilio.RestClient(accountSid, apiSecret);
//var account = twilioClient.accounts(accountSid);
//Send an text message
twilioClient.messages.create({
body: 'Hello from Node',
to: '+my_num', // Text this number
from: '+twilio_num' // From a valid Twilio number
}, function(err, message) {
if (err) {
console.log(err);
}
else {
console.log(message);
}
});
I found a similar problem here and followed the posted solution but that didn't work. I am guessing this is an entirely different error.

Resources