Access-Control-Allow-Origin Errors
I'm using both the tus-node-server and the tus-js-client to try to upload files to my server from a web browser. On smaller files (10mb-ish) it seems to be working fine, but on larger files (385mb-ish) it seems to be failing with a Access-Control-Allow-Origin failure.
After it throws that error in the console it retries up until the limit of retries I have set.
I have posted the errors below. Any reason why this would be happening?
[Error] Origin https://mysite.com is not allowed by Access-Control-Allow-Origin.
[Error] XMLHttpRequest cannot load https://upload.mysite.com//saiudfhia1h due to access control checks.
[Error] Failed to load resource: Origin https://mysite.com is not allowed by Access-Control-Allow-Origin.
Also after all the retries have been attempted it fails with this error.
tus: failed to upload chunk at offset 0, caused by [object XMLHttpRequestProgressEvent], originated from request (response code: 0, response text: )
Any reason why this would be happening?
Not out of my head. Would you mind sharing your configuration for tus-node-server and tus-js-client? Also, does tus-node-server produce logs which you can share?
@Acconut I have posted my code below. No logs that I'm aware of.
Front End JS:
var upload = new tus.Upload(file, {
endpoint: "https://upload.example.com/?id=" + res._id,
retryDelays: [0, 1000, 3000, 5000],
metadata: {
filename: res._id,
filetype: file.type
},
onError: function(error) {
console.log("Failed because: " + error)
},
onProgress: function(bytesUploaded, bytesTotal) {
console.log(bytesUploaded, bytesTotal, percentage + "%")
},
onSuccess: function() {
console.log("Download %s from %s", upload.file.name, upload.url)
alert("You have successfully uploaded your file");
}
})
// Start the upload
upload.start()
Back End JS:
server.datastore = new tus.FileStore({
directory: '/files',
path: '/',
namingFunction: fileNameFromUrl
});
server.on(EVENTS.EVENT_UPLOAD_COMPLETE, (event) => {
console.log(`Upload complete for file ${event.file.id}`);
let params = {
Bucket: keys.awsBucketName,
Body: fs.createReadStream(path.join("/files", event.file.id)),
Key: `${event.file.id}/rawfile`
};
s3.upload(params, function(err, data) {
console.log(err, data);
fs.unlink(path.join("/files", event.file.id), (err) => {
if (err) throw err;
console.log('successfully deleted file');
});
});
});
const app = express();
const uploadApp = express();
uploadApp.all('*', server.handle.bind(server));
app.use('/', uploadApp);
app.listen(3000);
Ok, thank you very much, I will see if I can reproduce it. Do you use any proxy in front of tus-node-server? This may screw with response headers if configured wrong.
@Acconut I only use CloudFlare, and I'm just using the following command to map port 3000 to port 80.
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
@Acconut I should also mention, the video gets fully uploaded as onProgress gets completed with 100%, but then it fails and retries. It looks like it has something to do with some type of validation logic to ensure the file was uploaded. The progress hits 100% then retries 5 times before failing for good.
I attempted to reproduce your error but I wasn't able to. The upload always completed without problems even when changing the file sizes as you described. I assume it may be related to your CloudFlare or iptables setup. Maybe some the hostname or port is not properly set by some proxy.
Does this error occur always or just randomly? Would you be able to generate a HAR file and share it with us (see https://www.inflectra.com/support/knowledgebase/kb254.aspx)? HAR files contain the network traffic from a website, so we can inspect the sent and received headers more easily. If you don't want to share this file in the public you can also email me at [email protected].
@Acconut I have just generated a HAR file and emailed it to you. Because of that I was able to figure out a little bit more detail. On a PATCH command I'm getting a 413 status code. 413 Request Entity Too Large from cloudflare. The content type is text/html. I have attached the response below.
<html>
<head><title>413 Request Entity Too Large</title></head>
<body bgcolor="white">
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
I'm assuming the quick solution would be to disable Cloudflare for that subdomain. Although Cloudflare has its benefits, and I would only like to disable it as a last resort.
Any work arounds for this?
Thanks for sending that file over. In that situation, the case is pretty clear. Cloudflare sends the 413 response but does not include the CORS headers, so the browser throws the error you mentioned in the original comment. I see two ways to fix this:
a) Disable the Cloudflare size limit, if possible (not sure whether they support that, I haven't used Cloudflare extensively)
b) Set the chunkSize option (https://github.com/tus/tus-js-client#tusdefaultoptions) in tus-js-client to the maximum size that Cloudflare will accept. This will cause that the file is uploaded through multiple PATCH requests with a smaller size each.
@Acconut It looks like the second option is what I'm looking for. The chunkSize option in that documentation doesn't look like it specifies what type it is looking for. After quickly looking at the code it looks like it accepts it in bytes. Is that correct? If so I can make a PR to add that important detail to the documentation (README).
After quickly looking at the code it looks like it accepts it in bytes. Is that correct?
Yes, that's correct.
If so I can make a PR to add that important detail to the documentation (README).
Thank you for the offer, but I already took care of that :)
Any solutions ? I get the same error in asp.net when I Increase the file upload limit