decaffeinate: Convert CommandRunner.coffee and 25 other files to JS
This commit is contained in:
@@ -1,70 +1,88 @@
|
||||
request = require("request").defaults(jar: false)
|
||||
fs = require("fs")
|
||||
logger = require "logger-sharelatex"
|
||||
settings = require("settings-sharelatex")
|
||||
URL = require('url');
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* DS207: Consider shorter variations of null checks
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
let UrlFetcher;
|
||||
const request = require("request").defaults({jar: false});
|
||||
const fs = require("fs");
|
||||
const logger = require("logger-sharelatex");
|
||||
const settings = require("settings-sharelatex");
|
||||
const URL = require('url');
|
||||
|
||||
oneMinute = 60 * 1000
|
||||
const oneMinute = 60 * 1000;
|
||||
|
||||
module.exports = UrlFetcher =
|
||||
pipeUrlToFile: (url, filePath, _callback = (error) ->) ->
|
||||
callbackOnce = (error) ->
|
||||
clearTimeout timeoutHandler if timeoutHandler?
|
||||
_callback(error)
|
||||
_callback = () ->
|
||||
module.exports = (UrlFetcher = {
|
||||
pipeUrlToFile(url, filePath, _callback) {
|
||||
if (_callback == null) { _callback = function(error) {}; }
|
||||
const callbackOnce = function(error) {
|
||||
if (timeoutHandler != null) { clearTimeout(timeoutHandler); }
|
||||
_callback(error);
|
||||
return _callback = function() {};
|
||||
};
|
||||
|
||||
if settings.filestoreDomainOveride?
|
||||
p = URL.parse(url).path
|
||||
url = "#{settings.filestoreDomainOveride}#{p}"
|
||||
timeoutHandler = setTimeout () ->
|
||||
timeoutHandler = null
|
||||
logger.error url:url, filePath: filePath, "Timed out downloading file to cache"
|
||||
callbackOnce(new Error("Timed out downloading file to cache #{url}"))
|
||||
# FIXME: maybe need to close fileStream here
|
||||
, 3 * oneMinute
|
||||
if (settings.filestoreDomainOveride != null) {
|
||||
const p = URL.parse(url).path;
|
||||
url = `${settings.filestoreDomainOveride}${p}`;
|
||||
}
|
||||
var timeoutHandler = setTimeout(function() {
|
||||
timeoutHandler = null;
|
||||
logger.error({url, filePath}, "Timed out downloading file to cache");
|
||||
return callbackOnce(new Error(`Timed out downloading file to cache ${url}`));
|
||||
}
|
||||
// FIXME: maybe need to close fileStream here
|
||||
, 3 * oneMinute);
|
||||
|
||||
logger.log url:url, filePath: filePath, "started downloading url to cache"
|
||||
urlStream = request.get({url: url, timeout: oneMinute})
|
||||
urlStream.pause() # stop data flowing until we are ready
|
||||
logger.log({url, filePath}, "started downloading url to cache");
|
||||
const urlStream = request.get({url, timeout: oneMinute});
|
||||
urlStream.pause(); // stop data flowing until we are ready
|
||||
|
||||
# attach handlers before setting up pipes
|
||||
urlStream.on "error", (error) ->
|
||||
logger.error err: error, url:url, filePath: filePath, "error downloading url"
|
||||
callbackOnce(error or new Error("Something went wrong downloading the URL #{url}"))
|
||||
// attach handlers before setting up pipes
|
||||
urlStream.on("error", function(error) {
|
||||
logger.error({err: error, url, filePath}, "error downloading url");
|
||||
return callbackOnce(error || new Error(`Something went wrong downloading the URL ${url}`));
|
||||
});
|
||||
|
||||
urlStream.on "end", () ->
|
||||
logger.log url:url, filePath: filePath, "finished downloading file into cache"
|
||||
urlStream.on("end", () => logger.log({url, filePath}, "finished downloading file into cache"));
|
||||
|
||||
urlStream.on "response", (res) ->
|
||||
if res.statusCode >= 200 and res.statusCode < 300
|
||||
fileStream = fs.createWriteStream(filePath)
|
||||
return urlStream.on("response", function(res) {
|
||||
if ((res.statusCode >= 200) && (res.statusCode < 300)) {
|
||||
const fileStream = fs.createWriteStream(filePath);
|
||||
|
||||
# attach handlers before setting up pipes
|
||||
fileStream.on 'error', (error) ->
|
||||
logger.error err: error, url:url, filePath: filePath, "error writing file into cache"
|
||||
fs.unlink filePath, (err) ->
|
||||
if err?
|
||||
logger.err err: err, filePath: filePath, "error deleting file from cache"
|
||||
callbackOnce(error)
|
||||
// attach handlers before setting up pipes
|
||||
fileStream.on('error', function(error) {
|
||||
logger.error({err: error, url, filePath}, "error writing file into cache");
|
||||
return fs.unlink(filePath, function(err) {
|
||||
if (err != null) {
|
||||
logger.err({err, filePath}, "error deleting file from cache");
|
||||
}
|
||||
return callbackOnce(error);
|
||||
});
|
||||
});
|
||||
|
||||
fileStream.on 'finish', () ->
|
||||
logger.log url:url, filePath: filePath, "finished writing file into cache"
|
||||
callbackOnce()
|
||||
fileStream.on('finish', function() {
|
||||
logger.log({url, filePath}, "finished writing file into cache");
|
||||
return callbackOnce();
|
||||
});
|
||||
|
||||
fileStream.on 'pipe', () ->
|
||||
logger.log url:url, filePath: filePath, "piping into filestream"
|
||||
fileStream.on('pipe', () => logger.log({url, filePath}, "piping into filestream"));
|
||||
|
||||
urlStream.pipe(fileStream)
|
||||
urlStream.resume() # now we are ready to handle the data
|
||||
else
|
||||
logger.error statusCode: res.statusCode, url:url, filePath: filePath, "unexpected status code downloading url to cache"
|
||||
# https://nodejs.org/api/http.html#http_class_http_clientrequest
|
||||
# If you add a 'response' event handler, then you must consume
|
||||
# the data from the response object, either by calling
|
||||
# response.read() whenever there is a 'readable' event, or by
|
||||
# adding a 'data' handler, or by calling the .resume()
|
||||
# method. Until the data is consumed, the 'end' event will not
|
||||
# fire. Also, until the data is read it will consume memory
|
||||
# that can eventually lead to a 'process out of memory' error.
|
||||
urlStream.resume() # discard the data
|
||||
callbackOnce(new Error("URL returned non-success status code: #{res.statusCode} #{url}"))
|
||||
urlStream.pipe(fileStream);
|
||||
return urlStream.resume(); // now we are ready to handle the data
|
||||
} else {
|
||||
logger.error({statusCode: res.statusCode, url, filePath}, "unexpected status code downloading url to cache");
|
||||
// https://nodejs.org/api/http.html#http_class_http_clientrequest
|
||||
// If you add a 'response' event handler, then you must consume
|
||||
// the data from the response object, either by calling
|
||||
// response.read() whenever there is a 'readable' event, or by
|
||||
// adding a 'data' handler, or by calling the .resume()
|
||||
// method. Until the data is consumed, the 'end' event will not
|
||||
// fire. Also, until the data is read it will consume memory
|
||||
// that can eventually lead to a 'process out of memory' error.
|
||||
urlStream.resume(); // discard the data
|
||||
return callbackOnce(new Error(`URL returned non-success status code: ${res.statusCode} ${url}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user