I'm using NodeJS on a daemon that then contacts the webhost via Laravel. No biggy here, I use Ajax to initiate a download for the client's vps. For some reason I get the error 'Can't remove headers after they are sent'. It only occurs when I enable the 'downloadModpack' method. Here is the code for the function that downloads the modpack.
downloadMinecraftModPack(url, snapshot, next) {
var fs = require('fs');
var files = new Array();
var newFile = '';
var isEmpty = false;
var server = this.server;
this.server.log.error("CALLED");
function makeDir(callback) {
const Exec = Process.spawn('mkdir', ['-p', server.path('backups')], {
uid: server.json.build.user,
gid: server.json.build.user,
});
Exec.on('exit', (code, signal) => {
if (code !== 0) {
callback(new Error(`Unable to make backups directory properly, exited with code ${code} signal ${signal}.`));
return;
} else {
callback();
return;
}
});
}
function checkForSnapshot(callback) {
fs.readdir(server.path('backups'), function(err, items) {
if(err || !items.length) {
callback();
return;
} else {
if(items.indexOf(snapshot + ".zip") > -1) {
callback(new Error('Snapshot with that name already exists!'));
return;
} else {
callback();
return;
}
}
});
}
function checkEmpty(callback) {
fs.readdir(server.path(), function(err, items) {
if(err || !items.length) {
isEmpty = true;
callback();
return;
} else {
if(items.indexOf("backups") > -1 && items.length <= 1) {
isEmpty = true;
}
callback();
return;
}
});
}
function createSnapshot(callback) {
if(isEmpty) {
callback();
return;
}
fs.readdir(server.path(), function(err, items) {
if(err || !items.length) {
callback();
return;
}
});
const Exec = Process.spawn('zip', ['-r', 'backups/' + snapshot + '.zip', '.', '-x', 'backups/*'], {
cwd: server.path(),
uid: server.json.build.user,
gid: server.json.build.user,
});
Exec.on('error', execErr => {
server.log.error(execErr);
callback(new Error('There was an error while attempting to compress this folder.'));
return;
});
Exec.on('exit', (code, signal) => {
if (code !== 0) {
callback(new Error(`Compression of files exited with code ${code} signal ${signal}.`));
return;
}
callback();
return;
});
}
function clearDirectory(callback) {
if(isEmpty) {
callback();
return;
}
const Exec = Process.spawn('find', ['-maxdepth', '1', '!', '-name', 'backups', '!', '-name', '.', '-exec', 'rm', '-rv', '{}', '\;'], {
cwd: server.path(),
uid: server.json.build.user,
gid: server.json.build.user,
});
Exec.on('error', execErr => {
server.log.error(execErr);
callback(new Error('There was an error while attempting to delete this folder.'));
return;
});
Exec.on('exit', (code, signal) => {
if (code !== 0) {
callback(new Error(`Deletion of files exited with code ${code} signal ${signal}.`));
return;
}
callback();
return;
});
}
function downloadModpack(callback) {
const Exec = Process.spawn('wget', ['--content-disposition', '-P', server.path(), "https://feed-the-beast.com" + url + "/download"], {
uid: server.json.build.user,
gid: server.json.build.user,
});
Exec.stdout.on('data', function(data) {
});
Exec.stderr.on('data', function(data) {
});
Exec.on('exit', (code, signal) => {
callback();
});
}
function unzipModpack(callback) {
const Exec = Process.spawn('unzip', ['-l', '*.zip'], {
cwd: server.path(),
uid: server.json.build.user,
gid: server.json.build.user,
});
Exec.on('exit', (code, signal) => {
callback();
return;
});
}
Async.series([
makeDir,
checkForSnapshot,
checkEmpty,
createSnapshot,
clearDirectory,
downloadModpack
], function (err, results) {
// Here, results is an array of the value from each function
console.log('done'); // outputs: ['two', 'five']
next(err);
});
}
Here is the error log.
22:41:17.162Z ERROR wings: CALLED (server=ffc14002-5e58-4f57-9b86-e3671e30b132) done 22:41:24.243Z FATAL wings: (path=/server/file/mcmodpack, method=POST) Error: Can't remove headers after they are sent at ServerResponse.OutgoingMessage.removeHeader (_http_outgoing.js:392:11) at ServerResponse.restifyWriteHead [as writeHead] (/srv/daemon/node_modules/restify/lib/response.js:426:14) at _cb (/srv/daemon/node_modules/restify/lib/response.js:326:14) at ServerResponse.send (/srv/daemon/node_modules/restify/lib/response.js:342:9) at Responses.generic204 (/srv/daemon/src/helpers/responses.js:35:25) at Auth.server.fs.downloadMinecraftModPack.err (/srv/daemon/src/controllers/routes.js:313:23) at /srv/daemon/src/controllers/fs.js:819:13 at /srv/daemon/node_modules/async/dist/async.js:3853:9 at /srv/daemon/node_modules/async/dist/async.js:484:16 at iteratorCallback (/srv/daemon/node_modules/async/dist/async.js:1084:13)
via Colby McHenry
No comments:
Post a Comment