EPIPE Error when uploading large file using nodejs - node.js

I was following this article to setup a nodejs server on my local machine (which has 16 gb memory and about 170gb free disk-space) and uploaded a 20 gb file, for the first couple of times the file got uploaded successfully, but after a while i started getting EPIPE error:
error FetchError: request to http://localhost:3200/upload failed, reason: write EPIPE
at ClientRequest.<anonymous> (/Volumes/FreeAgent GoFlex Drive/Test/multer-project/node_modules/node-fetch/lib/index.js:1455:11)
at ClientRequest.emit (events.js:327:22)
at Socket.socketErrorListener (_http_client.js:467:9)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:100:8)
at emitErrorCloseNT (internal/streams/destroy.js:68:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
type: 'system',
errno: 'EPIPE',
code: 'EPIPE'
}
When i checked, the file got uploaded partially and was about 28mb in size. I tried uploading the file from both Postman, browser and a nodejs script, but got the same EPIPE error message. I am not sure why is this happening, googling the error message didn't help. I am not sure how to overcome this. Following is my server and client code.
// server.js
const express = require("express"); // Express Web Server
const busboy = require("connect-busboy"); // Middleware to handle the file upload https://github.com/mscdex/connect-busboy
const path = require("path"); // Used for manipulation with path
const fs = require("fs-extra");
const app = express(); // Initialize the express web server
app.use(
busboy({
highWaterMark: 2 * 1024 * 1024 // Set 2MiB buffer
})
); // Insert the busboy middle-ware
const uploadPath = path.join(__dirname, "uploads/"); // Register the upload path
fs.ensureDir(uploadPath); // Make sure that he upload path exits
/**
* Create route /upload which handles the post request
*/
app.route("/upload").post((req, res, next) => {
req.pipe(req.busboy); // Pipe it trough busboy
req.busboy.on("file", (fieldname, file, filename) => {
console.log(`Upload of '${filename}' started`);
// Create a write stream of the new file
const fstream = fs.createWriteStream(path.join(uploadPath, filename));
// Pipe it trough
file.pipe(fstream);
// On finish of the upload
fstream.on("close", () => {
console.log(`Upload of '${filename}' finished`);
res.send("ok");
});
});
});
/**
* Serve the basic index.html with upload form
*/
app.route("/").get((req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.write(
'<form action="upload" method="post" enctype="multipart/form-data">'
);
res.write('<input type="file" name="fileToUpload"><br>');
res.write('<input type="submit">');
res.write("</form>");
return res.end();
});
const server = app.listen(3200, function() {
console.log(`Listening on port ${server.address().port}`);
});
and my client code is:
// client.js
const fs = require("fs");
const FormData = require("form-data");
const fetch = require("node-fetch");
var formdata = new FormData();
formdata.append(
"file",
fs.createReadStream("/Users/phantom007/My Documents/35gb.myfile")
);
var requestOptions = {
method: "POST",
body: formdata,
redirect: "follow"
};
fetch("http://localhost:3200/upload", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log("error", error));

Answering my own question.
After strugging for a long time i figured out that this error was coming because the number of bytes getting written on the same is larger than the number of bytes sent to the server, so in my client code, i changed
this
fs.createReadStream("/Users/phantom007/My Documents/35gb.myfile")
to this
fs.createReadStream("/Users/phantom007/My Documents/35gb.myfile", { highWaterMark: 2 * 1024 * 1024 })

Related

URL inside ffmpeg(fs.createStreamReader(url)) [duplicate]

on nodejs documentation, the streams section says I can do fs.createReadStream(url || path).
But, when I actually do that It tells me Error: ENOENT: no such file or directory.
I just want to pipe the video from a readable to a writable stream, But I'm stuck on creating a readable one.
my code:
const express = require('express')
const fs = require('fs')
const url = 'https://www.example.com/path/to/mp4Video.mp4'
const port = 3000
app.get('/video', (req, res) => {
const readable = fs.createReadStream(url)
})
app.listen(port, () => {
console.log('listening on port ' + port)
})
the ERROR:
listening on port 3000
events.js:291
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open 'https://www.example.com/path/to/mp4Video.mp4'
Emitted 'error' event on ReadStream instance at:
at internal/fs/streams.js:136:12
at FSReqCallback.oncomplete (fs.js:156:23) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'https://www.example.com/path/to/mp4Video.mp4'
}
PS: https://www.example.com/path/to/mp4Video.mp4 IS NOT THE ACTUAL URL
fs.createReadStream() does not work with http URLs only file:// URLs or filename paths. Unfortunately, this is not described in the fs doc, but if you look at the source code for fs.createReadStream() and follow what it calls you can find that it ends up calling fileURULtoPath(url) which will throw if it's not a file: URL.
function fileURLToPath(path) {
if (typeof path === 'string')
path = new URL(path);
else if (!isURLInstance(path))
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
if (path.protocol !== 'file:')
throw new ERR_INVALID_URL_SCHEME('file');
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
}
It would suggest using the got() library to get yourself a readstream from a URL:
const got = require('got');
const mp4Url = 'https://www.example.com/path/to/mp4Video.mp4';
app.get('/video', (req, res) => {
got.stream(mp4Url).pipe(res);
});
More examples described in this article: How to stream file downloads in Nodejs with Got.
You can also use the plain http/https modules to get the readstream, but I find got() to be generally useful at a higher level for lots of http request things so that's what I use. But, here's code with the https module.
const https = require('https');
const mp4Url = 'https://www.example.com/path/to/mp4Video.mp4';
app.get("/", (req, res) => {
https.get(mp4Url, (stream) => {
stream.pipe(res);
});
});
More advanced error handling could be added to both cases.

how to create a readable stream from a remote url in nodejs?

on nodejs documentation, the streams section says I can do fs.createReadStream(url || path).
But, when I actually do that It tells me Error: ENOENT: no such file or directory.
I just want to pipe the video from a readable to a writable stream, But I'm stuck on creating a readable one.
my code:
const express = require('express')
const fs = require('fs')
const url = 'https://www.example.com/path/to/mp4Video.mp4'
const port = 3000
app.get('/video', (req, res) => {
const readable = fs.createReadStream(url)
})
app.listen(port, () => {
console.log('listening on port ' + port)
})
the ERROR:
listening on port 3000
events.js:291
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open 'https://www.example.com/path/to/mp4Video.mp4'
Emitted 'error' event on ReadStream instance at:
at internal/fs/streams.js:136:12
at FSReqCallback.oncomplete (fs.js:156:23) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'https://www.example.com/path/to/mp4Video.mp4'
}
PS: https://www.example.com/path/to/mp4Video.mp4 IS NOT THE ACTUAL URL
fs.createReadStream() does not work with http URLs only file:// URLs or filename paths. Unfortunately, this is not described in the fs doc, but if you look at the source code for fs.createReadStream() and follow what it calls you can find that it ends up calling fileURULtoPath(url) which will throw if it's not a file: URL.
function fileURLToPath(path) {
if (typeof path === 'string')
path = new URL(path);
else if (!isURLInstance(path))
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
if (path.protocol !== 'file:')
throw new ERR_INVALID_URL_SCHEME('file');
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
}
It would suggest using the got() library to get yourself a readstream from a URL:
const got = require('got');
const mp4Url = 'https://www.example.com/path/to/mp4Video.mp4';
app.get('/video', (req, res) => {
got.stream(mp4Url).pipe(res);
});
More examples described in this article: How to stream file downloads in Nodejs with Got.
You can also use the plain http/https modules to get the readstream, but I find got() to be generally useful at a higher level for lots of http request things so that's what I use. But, here's code with the https module.
const https = require('https');
const mp4Url = 'https://www.example.com/path/to/mp4Video.mp4';
app.get("/", (req, res) => {
https.get(mp4Url, (stream) => {
stream.pipe(res);
});
});
More advanced error handling could be added to both cases.

Piping Firebase stream to Express response result in HPE_INVALID_CONSTANT

I am trying to stream a video (mp4) from firebase storage to <video> on client. What I'm doing is using createReadStream and piping it to Express response object. However on the client this error is thrown:
Proxy error: Could not proxy request /movie/600d31f192e0941f9c4b4773/stream from localhost:3000 to http://localhost:5000/.
[1] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (HPE_INVALID_CONSTANT).
const streamMovie = async (req, res) => {
const range = req.headers.range
const movie = await Movie.findById(req.params.id)
const bucket = firebase.storage().bucket()
// Get video size
const videoFile = bucket.file(movie.videoFileUrl)
const [metadata] = await videoFile.getMetadata()
const videoSize = metadata.size
// Parse range
const parts = range.replace('bytes=', '').split('-')
const start = parseInt(parts[0], 10)
const end = parts[1] ? parseInt(parts[1], 10) : videoSize - 1
res.writeHead(206, {
'Content-Type': 'video/mp4',
'Content-Range': `bytes ${start}-${end}/${videoSize}`,
'Content-Length': `${end - start + 1}`,
'Accept-Ranges': 'bytes',
})
videoFile.createReadStream({ start, end }).pipe(res)
}
I could not pinpoint the source of the error since I'm pretty inexperienced with streams. Any help is appreciated. Thanks!
If you want to stream a video file to the express response object, you can use the code example from the client library official docs. I have tested the following sample successfully:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
const {Storage} = require('#google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('bucket123');
const remoteFile = bucket.file('videotest123.mp4');
res.writeHead(200, {
'Content-Type': 'video/mp4',
'Content-Range': 'bytes=0-',
'Accept-Ranges': 'bytes',
})
remoteFile.createReadStream()
.on('error', function(err) {
res.send('there was an error');
})
.on('response', function(response) {})
.on('end', function() {})
.pipe(res);
})
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})
If you want to request only certain parts of the video (aka Google Cloud Storage object), you can specify within the createReadStream method the start and end options.
const remoteFile = bucket.file('videotest123');
logFile.createReadStream({
start: 10000,
end: 20000
})
.on('error', function(err) {})
.pipe(res);
In regards to the HPE_INVALID_CONSTANT message, I have found here that the message indicates an web server response malformed. In this case the resobject from the code.
I hope you find this useful

multiparty error in parsing callback request

I am using multiparty. It was working fine but suddenly it is throwing error.
Error
err: { Error: stream ended unexpectedly
at Form.<anonymous> (/user_code/node_modules/multiparty/index.js:754:24)
at emitNone (events.js:91:20)
at Form.emit (events.js:185:7)
at finishMaybe (_stream_writable.js:514:14)
at endWritable (_stream_writable.js:524:3)
at Form.Writable.end (_stream_writable.js:489:5)
at onend (_stream_readable.js:511:10)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9) status: 400, statusCode: 400 }
Code
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var multiparty = require('multiparty');
var http = require('http');
var util = require('util');
exports.helloWorld = functions.https.onRequest((request, response) => {
var body = "";
var POST = {};
var form = new multiparty.Form();
form.on('error', function(err) {
console.log('Error parsing form: ' + err.stack);
});
form.parse(request, function(err, fields, files) {
response.status(500).send({
message: err
});
})
});
});
The "stream ended unexpectedly" error implies the underlying TCP socket was closed before a complete multipart form was received.
As you say this was previously working you should check the server to which you are making the request for any errors which may be closing the response early. One common cause is the size of the response data being larger than accepted by the server or request/response headers.

Node.js Proxy with custom Http(s) Agent and Connect.js Middleware

I've put together a proxy server in Node that needs the ability to tunnel https requests over tls and that all works. Using the the following two packages this was extremely easy to setup: proxy, https-proxy-agent. My issue is that I'm trying to capture HAR files using connect as a middleware layer and I'm getting the following error:
_http_outgoing.js:357
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.writeHead (_http_server.js:180:21)
at ClientRequest.<anonymous> (/.../node_modules/proxy/proxy.js:233:11)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:473:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at Socket.socketOnData (_http_client.js:362:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
The is as simple as the following and it only seems to happen when I'm using connect and proxying through my local browser(this proxy is actually being used with BrowserStackLocal). When I pass through the proxy from anything other than my local machines browser, it's like it doesn't even know the middleware exists.
So basically, I just need to get connect working in this scenario and I'm not sure if I need to pause something and resume, or what... any ideas would be greatly appreciated. The base code is below:
const path = require('path');
const http = require('http');
const proxy = require('proxy');
const Agent = require('https-proxy-agent');
const connect = require('connect');
const har = require('./har');
const middleware = connect();
middleware.use(har({
harOutputDir: path.resolve(process.cwd(), 'har/')
}));
const server = proxy(http.createServer(middleware));
server.agent = new Agent('http://localhost:8081');
server.listen(8081)
Thanks!
EDIT: Just a note: the har middleware is not modifying headers at all.
proxy hasn't been maintained in a while. Builds are not passing, last commit don't pass tests. The source of the stack trace you've put up is coming from here in Line 233 - buggy library code.
Writing a similar proxy is trivial. Following code illustrates how to create one.
const http = require('http');
const urlP = require('url');
const proxiedServer = 'http://localhost:8888';
// Proxy server
http.createServer((req, res) => {
console.log(`Proxy: Got ${req.url}`);
const _r = http.request(
Object.assign(
{},
urlP.parse(proxiedServer),
{
method: req.method,
path: req.url
}
),
_res => {
res.writeHead(_res.statusCode, _res.headers);
_res.pipe(res);
}
);
req.pipe(_r);
}).listen(3124, () => {
console.log("Listening on 3124");
});
// Regular server. Could be Express
http.createServer((req, res) => {
console.log('Proxied server: ', req.url);
let b = '';
req.on('data', c => {
b += c;
});
req.on('end', () => {
console.log('Proxied server: ', b);
});
res.writeHead(200);
res.end('ok');
}).listen(8888, () => {
console.log('proxied server listening on 8888');
});
Your code using your own custom proxy would look like the following:
const urlP = require('url');
const path = require('path');
const http = require('http');
const connect = require('connect');
const har = require('./har');
const proxiedServer = 'http://localhost:8888';
// Proxy server
http.createServer((req, res) => {
console.log(`Proxy: Got ${req.url}`);
const _r = http.request(
Object.assign(
{},
urlP.parse(proxiedServer),
{
method: req.method,
path: req.url
}
),
_res => {
res.writeHead(_res.statusCode, _res.headers);
_res.pipe(res);
}
);
req.pipe(_r);
}).listen(3124, () => {
console.log("Listening on 3124");
});
const middleware = connect();
middleware.use(har({
harOutputDir: path.resolve(process.cwd(), 'har/')
}));
middleware.use((req, res) => {
console.log('Proxied server: ', req.url);
let b = '';
req.on('data', c => {
b += c;
});
req.on('end', () => {
console.log('Proxied server: ', b);
});
res.writeHead(200);
res.end('ok');
});
http.createServer(middleware).listen(8888, () => {
console.log('proxied server listening on 8888');
});

Resources