AxiosError: maxContentLength size of -1 exceeded - node.js

I am trying to download some images from a website using axios, but I get this error every time I try. I tried setting the maxContentLength, but it didn't fix anything.
This is a part of my code
const resp = await axios({
method: 'GET',
url: `${host}/data/${chapterHash}/${page}`,
maxContentLength: 20000,
responseType: 'arraybuffer'
this is the error
const err = new AxiosError(
^
AxiosError: maxContentLength size of -1 exceeded
at IncomingMessage.handlerStreamAborted (C:\Users\alexou\Documents\node-reader\node_modules\axios\dist\node\axios.cjs:2912:23)
at IncomingMessage.emit (node:events:513:28)
at IncomingMessage._destroy (node:_http_incoming:224:10)
at _destroy (node:internal/streams/destroy:102:25)
at IncomingMessage.destroy (node:internal/streams/destroy:64:5)
at TLSSocket.socketCloseListener (node:_http_client:441:11)
at TLSSocket.emit (node:events:525:35)
at node:net:757:14
at TCP.done (node:_tls_wrap:584:7) {
code: 'ERR_BAD_RESPONSE'

what I ended up doing was just adding a try{ }catch. The code isn't crashing anymore, but I still get the errors. If you have a better way of handling this, please tell me.

Related

GOT package: RequestError: The body option must be a stream.Readable, string or Buffer

I am getting this error while trying to hit the POST and PUT APIs using GOT package in Node.Js (for external API calling).
I am using Got#11.8.6 with Node.js version 16.18.1 in Windows 10 OS.
Code Snippet
const gotObject = { method: 'POST', json: true, body: reqBodyObj, headers: reqHeader, };
const apiResponse = await got(microserviceUrl, gotObject);
Error Stack
2022-12-19T11:38:44.708Z - error: [D:\Projects\NodeJs\gateway\src\services\microservice.ts] callService at Request._destroy (D:\Projects\NodeJs\node-api-gateway\node_modules\got\dist\source\core\index.js:1386:21) at _destroy (node:internal/streams/destroy:102:25) at Request.destroy (node:internal/streams/destroy:64:5) at D:\Projects\NodeJs\node-api-gateway\node_modules\got\dist\source\core\index.js:354:26 at processTicksAndRejections (node:internal/process/task_queues:96:5) at Request._finalizeBody (D:\Projects\NodeJs\node-api-gateway\node_modules\got\dist\source\core\index.js:726:23) at D:\Projects\NodeJs\node-api-gateway\node_modules\got\dist\source\core\index.js:333:28 at new Request (D:\Projects\NodeJs\node-api-gateway\node_modules\got\dist\source\core\index.js:357:11) at makeRequest (D:\Projects\NodeJs\node-api-gateway\node_modules\got\dist\source\as-promise\index.js:35:29)

How can I get rid of 504 timeout error on web3 when calling a smart contract method?

I am not sure what to do, I have increased the timeout by doing this
var options = {
timeout: 60000 * 2, // ms
// Enable auto reconnection
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
}
};
const web3 = new Web3( new Web3.providers.HttpProvider(web3Settings.provider, options));
but I am still getting 504 timeout error sometimes on calling function of a contract
2022-02-16T08:19:00.456908+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: Error: Invalid JSON RPC response: "<html>\r\n<head><title>504 Gateway Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway Time-out</h1></center>\r\n</body>\r\n</html>\r\n"
2022-02-16T08:19:00.456917+00:00 app[web.1]: at Object.InvalidResponse (/app/node_modules/web3-core-helpers/lib/errors.js:43:16)
2022-02-16T08:19:00.456918+00:00 app[web.1]: at XMLHttpRequest.request.onreadystatechange (/app/node_modules/web3-providers-http/lib/index.js:95:32)
2022-02-16T08:19:00.456918+00:00 app[web.1]: at XMLHttpRequestEventTarget.dispatchEvent (/app/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
2022-02-16T08:19:00.456919+00:00 app[web.1]: at XMLHttpRequest._setReadyState (/app/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
2022-02-16T08:19:00.456919+00:00 app[web.1]: at XMLHttpRequest._onHttpResponseEnd (/app/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
2022-02-16T08:19:00.456919+00:00 app[web.1]: at IncomingMessage.<anonymous> (/app/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
I am using bsc testnet network.
If you're in front-end, than we should init web3 using
new Web3(window.ethereum);
await window.ethereum.enable();
If you're in backend, than use they HttpProvider is a good choice.
There's something you can do:
Make sure that the rpc you're using is valid. May be it point to a wrong address.
Try to interact with another SC to see whether the code work or not.

How to upload file from the back-end to another back-end?

I am trying to upload file from another back-end to currently existing API.
For doing so, I am trying to replicate what's being done in the front-end.
To create FormData and then send it to the API.
But the request seems to hang...
And if I would try to replicate the request express would console log error
Error: Request aborted
at IncomingMessage.<anonymous> (C:\Users\TCP_BCP\node_modules\formidable\lib\incoming_form.js:122:19)
at IncomingMessage.emit (events.js:314:20)
at IncomingMessage.EventEmitter.emit (domain.js:506:15)
at abortIncoming (_http_server.js:533:9)
at socketOnEnd (_http_server.js:549:5)
at Socket.emit (events.js:326:22)
at Socket.EventEmitter.emit (domain.js:483:12)
at endReadableNT (_stream_readable.js:1241:12)
I understand that if I am trying to perform new request the old one gets aborted probably.
But why the request could hang? The target file is small in size (500kb +-).
In the front-end there would be used $(".form").ajaxSubmit
In the back-end I am trying to create form with FormData.
What could be wrong?
So the issues was with headers.
Formidable at other API would hang without passing FormData headers for axios.
Code bellow allowed me to successfully upload files to a other API.
const data = await fs.readFile(zipPath);
const form = new FormData();
form.append("file", data, zipName);
const headers = form.getHeaders();
Axios.defaults.headers.cookie = cookies;
await Axios({
method: "POST",
url: endpoint,
headers: {
...headers,
cookie: cookies,
},
data: form,
});

Duplicate Content-Length with Axios running in NodeJS/JEST

I face really weird problem with Axios / nanoexpress. I started to write JEST test and copied working piece of code from Vuex that calls my backend. This code works reliably in Vue but here it fails with a network error. The Axios version is the same on both SPA and JEST test.
JEST:
let response = await axios.get(`${API}/users/1234`, getAuthHeader());
function getAuthHeader() {
const config = { headers: { } };
config.headers.Authorization = "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxZTQwdjBiMWo1Iiwibmlja25hbWUiOiJsaXRlcmFrbCIsInB3ZFRpbWVzdGFtcCI6IjIwMjAtMDMtMjJUMTE6MTA6NDkuNDg2WiIsInJvbGVzIjpbImFkbWluOnBvbGwiXSwiaWF0IjoxNTg2NjkwNzc2LCJleHAiOjE1ODkzNjkxNzZ9.N5MfpZ9i9Sjv-izdYItR4gXCmzVkqkuVcVSEL_6Q89c";
return config;
}
When I remove the config with Authorization header, the code works well:
let response = await axios.get(`${API}/users/1234`, getAuthHeader());
This is the error:
Network Error
at createError (node_modules/axios/lib/core/createError.js:16:15)
at XMLHttpRequest.handleError (node_modules/axios/lib/adapters/xhr.js:83:14)
at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:33:32)
at innerInvokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:316:27)
at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
at XMLHttpRequestEventTargetImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
at fireAnEvent (node_modules/jsdom/lib/jsdom/living/helpers/events.js:17:36)
at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:121:3)
at Object.dispatchError (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:51:3)
at EventEmitter.<anonymous> (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:675:20)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:384:47)
at Request.onRequestError (node_modules/request/request.js:877:8)
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Error: Parse Error: Duplicate Content-Length
at Object.dispatchError (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:54:19)
at EventEmitter.<anonymous> (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:675:20)
at EventEmitter.emit (events.js:327:22)
at Request.<anonymous> (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:384:47)
at Request.emit (events.js:315:20)
at Request.onRequestError (C:\Users\leos\WebstormProjects\nano-options\node_modules\request\request.js:877:8)
at ClientRequest.emit (events.js:315:20)
at Socket.socketOnData (_http_client.js:486:9)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:297:12) undefined
The service is nothing fancy and it is never called when Authorization header is present and it is called when the Authorization header is missing.
app.get('/v1/users/:userId', async (req, res) => {
console.log("getUser handler starts");
const response = {};
res.send(response);
return res;
});
I have spent several hours trying to see the raw network communication but failed. It must be something with Axios because I can run successfuly exactly the same request with the Authorization header from Postman.
Update:
it seems to be related to NodeJS version. I submitted an issue: https://github.com/axios/axios/issues/2889
Update 2:
I switched from Axios to Got and the test is working properly. https://github.com/literakl/mezinamiridici/commit/e667c5661429fc8cba273716dfced2244e211abf

Cloud Functions 400 Bad Request with invalid JSON in hello world app

I have hello world cloud functions app, it's working fine either locally by firebase serve or online after firebase deploy
const functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Locally I can open http://localhost:5000/testing-98b0b/us-central1/helloWorld without any issues, but if I use postman to send post call with an invalid body like "#" it crashes and keep loading with below error:
error: { SyntaxError: Unexpected token # in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:158:10)
at parse (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:83:15)
at /usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/read.js:121:18
at invokeCallback (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:224:16)
at done (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
expose: true,
statusCode: 400,
status: 400,
body: '#',
type: 'entity.parse.failed' }
Also after firebase deploy I can open
https://us-central1-staff-testing-98b0b.cloudfunctions.net/helloWorld
without any issues but if I use postman to send post call with an invalid body like "#" it crashes and returns 400 bad request with below error:
SyntaxError: Unexpected token # in JSON at position 0
at Object.parse (native)
at createStrictSyntaxError (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:157:10)
at parse (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:83:15)
at /var/tmp/worker/node_modules/body-parser/lib/read.js:121:18
at invokeCallback (/var/tmp/worker/node_modules/raw-body/index.js:224:16)
at done (/var/tmp/worker/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/var/tmp/worker/node_modules/raw-body/index.js:273:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
Is it bug in the firebase-functions library? Is there any way to handle this type of invalid requests?

Resources