I am using node-rest-client to make a request.
I am mocking the response with a stream but facing an issue where I am getting this error:
TypeError: The "stream" argument must be an instance of ReadableStream, WritableStream, or Stream. Received an instance of Socket
Below is the simple snipet that can be run to see the error.
const NodeRestClient = require('node-rest-client').Client;
const nock = require('nock');
const { Readable } = require('stream');
// Create a Readable stream from a string
const streamResponse = new Readable();
streamResponse._read = () => {};
streamResponse.push('Hello, this is a streamable buffer response');
streamResponse.push(null);
// Define the API endpoint you want to mock
const apiUrl = 'https://api.example.com';
// Use nock to intercept the HTTP request and mock the response with a stream
nock(apiUrl)
.get('/endpoint')
.reply(200, streamResponse, { 'Content-Type': 'application/octet-stream' });
// Make a request using node-rest-client
const client = new NodeRestClient();
client.get(`${apiUrl}/endpoint`, { responseType: 'arraybuffer' }, (data, response) => {
if (response.statusCode === 200) {
// Response data will be a Buffer
console.log('Mocked Streamable Buffer Response:', data.toString('utf-8'));
} else {
console.error('Error:', response.statusCode, data);
}
});
below is the stack trace:
Mocked Streamable Buffer Response: Hello, this is a streamable buffer response
node:events:495
throw er; // Unhandled 'error' event
^
<ref *2> TypeError [ERR_INVALID_ARG_TYPE]: The "stream" argument must be an instance of ReadableStream, WritableStream, or Stream. Received an instance of Socket
at new NodeError (node:internal/errors:405:5)
at eos (node:internal/streams/end-of-stream:74:11)
at IncomingMessage._destroy (node:_http_incoming:234:21)
at _destroy (node:internal/streams/destroy:109:10)
at IncomingMessage.destroy (node:internal/streams/destroy:71:5)
at endReadableNT (node:internal/streams/readable:1384:16)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
I want to understand what is causing this issue. Is there a solution/workaround for this issue?
What is your version of nock
?
There is an issue with older versions where nock
overrides https.request
on its own, preventing it from passing the logic to check for streams on node
.
If it's not the latest version, please update to the latest version and try again. https://github.com/nock/nock/issues/2466