decaffeinate: Convert FilestoreApp.coffee and 1 other file to JS

This commit is contained in:
decaffeinate 2019-12-16 10:42:36 +00:00 committed by Simon Detheridge
parent 1c7d1af4ca
commit e6e75d4e19
2 changed files with 332 additions and 224 deletions

View file

@ -1,45 +1,73 @@
app = require('../../../app') /*
require("logger-sharelatex").logger.level("info") * decaffeinate suggestions:
logger = require("logger-sharelatex") * DS101: Remove unnecessary use of Array.from
Settings = require("settings-sharelatex") * DS102: Remove unnecessary code created because of implicit returns
request = require('request') * DS103: Rewrite code to no longer use __guard__
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const app = require('../../../app');
require("logger-sharelatex").logger.level("info");
const logger = require("logger-sharelatex");
const Settings = require("settings-sharelatex");
const request = require('request');
S3_TRIES = 30 const S3_TRIES = 30;
module.exports = module.exports = {
running: false running: false,
initing: false initing: false,
callbacks: [] callbacks: [],
ensureRunning: (callback = (error) ->) -> ensureRunning(callback) {
if @running if (callback == null) { callback = function(error) {}; }
return callback() if (this.running) {
else if @initing return callback();
@callbacks.push callback } else if (this.initing) {
else return this.callbacks.push(callback);
@initing = true } else {
@callbacks.push callback this.initing = true;
app.listen Settings.internal?.filestore?.port, "localhost", (error) => this.callbacks.push(callback);
throw error if error? return app.listen(__guard__(Settings.internal != null ? Settings.internal.filestore : undefined, x => x.port), "localhost", error => {
@running = true if (error != null) { throw error; }
logger.log("filestore running in dev mode") this.running = true;
logger.log("filestore running in dev mode");
for callback in @callbacks return (() => {
callback() const result = [];
for (callback of Array.from(this.callbacks)) {
result.push(callback());
}
return result;
})();
});
}
},
waitForS3: (callback, tries) -> waitForS3(callback, tries) {
return callback() unless Settings.filestore.s3?.endpoint if (!(Settings.filestore.s3 != null ? Settings.filestore.s3.endpoint : undefined)) { return callback(); }
tries = 1 unless tries if (!tries) { tries = 1; }
request.get "#{Settings.filestore.s3.endpoint}/", (err, response) => return request.get(`${Settings.filestore.s3.endpoint}/`, (err, response) => {
console.log(err, response?.statusCode, tries) console.log(err, response != null ? response.statusCode : undefined, tries);
if !err && [200, 404].includes(response?.statusCode) if (!err && [200, 404].includes(response != null ? response.statusCode : undefined)) {
return callback() return callback();
}
if tries == S3_TRIES if (tries === S3_TRIES) {
return callback('timed out waiting for S3') return callback('timed out waiting for S3');
}
setTimeout( return setTimeout(
() => () => {
@waitForS3 callback, tries + 1 return this.waitForS3(callback, tries + 1);
},
1000 1000
) );
});
}
};
function __guard__(value, transform) {
return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
}

View file

@ -1,226 +1,306 @@
assert = require("chai").assert /*
sinon = require('sinon') * decaffeinate suggestions:
chai = require('chai') * DS102: Remove unnecessary code created because of implicit returns
should = chai.should() * DS103: Rewrite code to no longer use __guard__
expect = chai.expect * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
modulePath = "../../../app/js/LocalFileWriter.js" */
SandboxedModule = require('sandboxed-module') const {
fs = require("fs") assert
request = require("request") } = require("chai");
settings = require("settings-sharelatex") const sinon = require('sinon');
FilestoreApp = require "./FilestoreApp" const chai = require('chai');
async = require('async') const should = chai.should();
const {
expect
} = chai;
const modulePath = "../../../app/js/LocalFileWriter.js";
const SandboxedModule = require('sandboxed-module');
const fs = require("fs");
const request = require("request");
const settings = require("settings-sharelatex");
const FilestoreApp = require("./FilestoreApp");
const async = require('async');
getMetric = (filestoreUrl, metric, cb) -> const getMetric = (filestoreUrl, metric, cb) => request.get(`${filestoreUrl}/metrics`, function(err, res) {
request.get "#{filestoreUrl}/metrics", (err, res) -> expect(res.statusCode).to.equal(200);
expect(res.statusCode).to.equal 200 const metricRegex = new RegExp(`^${metric}{[^}]+} ([0-9]+)$`, "m");
metricRegex = new RegExp("^#{metric}{[^}]+} ([0-9]+)$", "m") return cb(parseInt(__guard__(metricRegex.exec(res.body), x => x[1]) || '0'));
cb(parseInt(metricRegex.exec(res.body)?[1] || '0')) });
describe "Filestore", -> describe("Filestore", function() {
before (done)-> before(function(done){
@localFileReadPath = "/tmp/filestore_acceptence_tests_file_read.txt" this.localFileReadPath = "/tmp/filestore_acceptence_tests_file_read.txt";
@localFileWritePath = "/tmp/filestore_acceptence_tests_file_write.txt" this.localFileWritePath = "/tmp/filestore_acceptence_tests_file_write.txt";
@constantFileContent = [ this.constantFileContent = [
"hello world" "hello world",
"line 2 goes here #{Math.random()}" `line 2 goes here ${Math.random()}`,
"there are 3 lines in all" "there are 3 lines in all"
].join("\n") ].join("\n");
@filestoreUrl = "http://localhost:#{settings.internal.filestore.port}" this.filestoreUrl = `http://localhost:${settings.internal.filestore.port}`;
fs.writeFile @localFileReadPath, @constantFileContent, (err) -> return fs.writeFile(this.localFileReadPath, this.constantFileContent, function(err) {
return done(err) if err if (err) { return done(err); }
FilestoreApp.waitForS3(done) return FilestoreApp.waitForS3(done);
});
});
beforeEach (done)-> beforeEach(function(done){
FilestoreApp.ensureRunning => return FilestoreApp.ensureRunning(() => {
async.parallel [ return async.parallel([
(cb) => cb => {
fs.unlink @localFileWritePath, () -> return fs.unlink(this.localFileWritePath, () => cb());
cb() },
(cb) => cb => {
getMetric @filestoreUrl, 's3_egress', (metric) => return getMetric(this.filestoreUrl, 's3_egress', metric => {
@previousEgress = metric this.previousEgress = metric;
cb() return cb();
(cb) => });
getMetric @filestoreUrl, 's3_ingress', (metric) => },
@previousIngress = metric cb => {
cb() return getMetric(this.filestoreUrl, 's3_ingress', metric => {
], done this.previousIngress = metric;
return cb();
});
}
], done);
});
});
it "should send a 200 for status endpoint", (done)-> it("should send a 200 for status endpoint", function(done){
request "#{@filestoreUrl}/status", (err, response, body)-> return request(`${this.filestoreUrl}/status`, function(err, response, body){
response.statusCode.should.equal 200 response.statusCode.should.equal(200);
body.indexOf("filestore").should.not.equal -1 body.indexOf("filestore").should.not.equal(-1);
body.indexOf("up").should.not.equal -1 body.indexOf("up").should.not.equal(-1);
done() return done();
});
});
describe "with a file on the server", -> describe("with a file on the server", function() {
beforeEach (done)-> beforeEach(function(done){
@timeout(1000 * 10) this.timeout(1000 * 10);
@file_id = Math.random() this.file_id = Math.random();
@fileUrl = "#{@filestoreUrl}/project/acceptence_tests/file/#{@file_id}" this.fileUrl = `${this.filestoreUrl}/project/acceptence_tests/file/${this.file_id}`;
writeStream = request.post(@fileUrl) const writeStream = request.post(this.fileUrl);
writeStream.on "end", done writeStream.on("end", done);
fs.createReadStream(@localFileReadPath).pipe writeStream return fs.createReadStream(this.localFileReadPath).pipe(writeStream);
});
it "should return 404 for a non-existant id", (done) -> it("should return 404 for a non-existant id", function(done) {
@timeout(1000 * 20) this.timeout(1000 * 20);
options = const options =
uri: @fileUrl + '___this_is_clearly_wrong___' {uri: this.fileUrl + '___this_is_clearly_wrong___'};
request.get options, (err, response, body) => return request.get(options, (err, response, body) => {
response.statusCode.should.equal 404 response.statusCode.should.equal(404);
done() return done();
});
});
it 'should record an egress metric for the upload', (done) -> it('should record an egress metric for the upload', function(done) {
getMetric @filestoreUrl, 's3_egress', (metric) => return getMetric(this.filestoreUrl, 's3_egress', metric => {
expect(metric - @previousEgress).to.equal @constantFileContent.length expect(metric - this.previousEgress).to.equal(this.constantFileContent.length);
done() return done();
});
});
it "should return the file size on a HEAD request", (done) -> it("should return the file size on a HEAD request", function(done) {
expectedLength = Buffer.byteLength(@constantFileContent) const expectedLength = Buffer.byteLength(this.constantFileContent);
request.head @fileUrl, (err, res) => return request.head(this.fileUrl, (err, res) => {
expect(res.statusCode).to.equal(200) expect(res.statusCode).to.equal(200);
expect(res.headers['content-length']).to.equal(expectedLength.toString()) expect(res.headers['content-length']).to.equal(expectedLength.toString());
done() return done();
});
});
it "should be able get the file back", (done)-> it("should be able get the file back", function(done){
@timeout(1000 * 10) this.timeout(1000 * 10);
request.get @fileUrl, (err, response, body)=> return request.get(this.fileUrl, (err, response, body)=> {
body.should.equal @constantFileContent body.should.equal(this.constantFileContent);
done() return done();
});
});
it "should record an ingress metric when downloading the file", (done)-> it("should record an ingress metric when downloading the file", function(done){
@timeout(1000 * 10) this.timeout(1000 * 10);
request.get @fileUrl, () => return request.get(this.fileUrl, () => {
getMetric @filestoreUrl, 's3_ingress', (metric) => return getMetric(this.filestoreUrl, 's3_ingress', metric => {
expect(metric - @previousIngress).to.equal @constantFileContent.length expect(metric - this.previousIngress).to.equal(this.constantFileContent.length);
done() return done();
});
});
});
it "should be able to get back the first 9 bytes of the file", (done) -> it("should be able to get back the first 9 bytes of the file", function(done) {
@timeout(1000 * 10) this.timeout(1000 * 10);
options = const options = {
uri: @fileUrl uri: this.fileUrl,
headers: headers: {
'Range': 'bytes=0-8' 'Range': 'bytes=0-8'
request.get options, (err, response, body)=> }
body.should.equal 'hello wor' };
done() return request.get(options, (err, response, body)=> {
body.should.equal('hello wor');
return done();
});
});
it "should record an ingress metric for a partial download", (done)-> it("should record an ingress metric for a partial download", function(done){
@timeout(1000 * 10) this.timeout(1000 * 10);
options = const options = {
uri: @fileUrl uri: this.fileUrl,
headers: headers: {
'Range': 'bytes=0-8' 'Range': 'bytes=0-8'
request.get options, ()=> }
getMetric @filestoreUrl, 's3_ingress', (metric) => };
expect(metric - @previousIngress).to.equal 9 return request.get(options, ()=> {
done() return getMetric(this.filestoreUrl, 's3_ingress', metric => {
expect(metric - this.previousIngress).to.equal(9);
return done();
});
});
});
it "should be able to get back bytes 4 through 10 of the file", (done) -> it("should be able to get back bytes 4 through 10 of the file", function(done) {
@timeout(1000 * 10) this.timeout(1000 * 10);
options = const options = {
uri: @fileUrl uri: this.fileUrl,
headers: headers: {
'Range': 'bytes=4-10' 'Range': 'bytes=4-10'
request.get options, (err, response, body)=> }
body.should.equal 'o world' };
done() return request.get(options, (err, response, body)=> {
body.should.equal('o world');
return done();
});
});
it "should be able to delete the file", (done)-> it("should be able to delete the file", function(done){
@timeout(1000 * 20) this.timeout(1000 * 20);
request.del @fileUrl, (err, response, body)=> return request.del(this.fileUrl, (err, response, body)=> {
response.statusCode.should.equal 204 response.statusCode.should.equal(204);
request.get @fileUrl, (err, response, body)=> return request.get(this.fileUrl, (err, response, body)=> {
response.statusCode.should.equal 404 response.statusCode.should.equal(404);
done() return done();
});
});
});
it "should be able to copy files", (done)-> return it("should be able to copy files", function(done){
@timeout(1000 * 20) this.timeout(1000 * 20);
newProjectID = "acceptence_tests_copyied_project" const newProjectID = "acceptence_tests_copyied_project";
newFileId = Math.random() const newFileId = Math.random();
newFileUrl = "#{@filestoreUrl}/project/#{newProjectID}/file/#{newFileId}" const newFileUrl = `${this.filestoreUrl}/project/${newProjectID}/file/${newFileId}`;
opts = const opts = {
method: 'put' method: 'put',
uri: newFileUrl uri: newFileUrl,
json: json: {
source: source: {
project_id:"acceptence_tests" project_id:"acceptence_tests",
file_id: @file_id file_id: this.file_id
request opts, (err, response, body)=> }
response.statusCode.should.equal 200 }
request.del @fileUrl, (err, response, body)=> };
response.statusCode.should.equal 204 return request(opts, (err, response, body)=> {
request.get newFileUrl, (err, response, body)=> response.statusCode.should.equal(200);
body.should.equal @constantFileContent return request.del(this.fileUrl, (err, response, body)=> {
done() response.statusCode.should.equal(204);
return request.get(newFileUrl, (err, response, body)=> {
body.should.equal(this.constantFileContent);
return done();
});
});
});
});
});
describe "with a pdf file", -> return describe("with a pdf file", function() {
beforeEach (done)-> beforeEach(function(done){
@timeout(1000 * 10) this.timeout(1000 * 10);
@file_id = Math.random() this.file_id = Math.random();
@fileUrl = "#{@filestoreUrl}/project/acceptence_tests/file/#{@file_id}" this.fileUrl = `${this.filestoreUrl}/project/acceptence_tests/file/${this.file_id}`;
@localFileReadPath = __dirname + '/../../fixtures/test.pdf' this.localFileReadPath = __dirname + '/../../fixtures/test.pdf';
fs.stat @localFileReadPath, (err, stat) => return fs.stat(this.localFileReadPath, (err, stat) => {
@localFileSize = stat.size this.localFileSize = stat.size;
writeStream = request.post(@fileUrl) const writeStream = request.post(this.fileUrl);
writeStream.on "end", done writeStream.on("end", done);
fs.createReadStream(@localFileReadPath).pipe writeStream return fs.createReadStream(this.localFileReadPath).pipe(writeStream);
});
});
it 'should record an egress metric for the upload', (done) -> it('should record an egress metric for the upload', function(done) {
getMetric @filestoreUrl, 's3_egress', (metric) => return getMetric(this.filestoreUrl, 's3_egress', metric => {
expect(metric - @previousEgress).to.equal @localFileSize expect(metric - this.previousEgress).to.equal(this.localFileSize);
done() return done();
});
});
it "should be able get the file back", (done)-> it("should be able get the file back", function(done){
@timeout(1000 * 10) this.timeout(1000 * 10);
request.get @fileUrl, (err, response, body)=> return request.get(this.fileUrl, (err, response, body)=> {
expect(body.substring(0, 8)).to.equal '%PDF-1.5' expect(body.substring(0, 8)).to.equal('%PDF-1.5');
done() return done();
});
});
describe "getting the preview image", -> describe("getting the preview image", function() {
beforeEach -> beforeEach(function() {
@previewFileUrl = "#{@fileUrl}?style=preview" return this.previewFileUrl = `${this.fileUrl}?style=preview`;
});
it "should not time out", (done) -> it("should not time out", function(done) {
@timeout(1000 * 20) this.timeout(1000 * 20);
request.get @previewFileUrl, (err, response, body) => return request.get(this.previewFileUrl, (err, response, body) => {
expect(response).to.not.equal null expect(response).to.not.equal(null);
done() return done();
});
});
it "should respond with image data", (done) -> return it("should respond with image data", function(done) {
# note: this test relies of the imagemagick conversion working // note: this test relies of the imagemagick conversion working
@timeout(1000 * 20) this.timeout(1000 * 20);
request.get @previewFileUrl, (err, response, body) => return request.get(this.previewFileUrl, (err, response, body) => {
expect(response.statusCode).to.equal 200 expect(response.statusCode).to.equal(200);
expect(body.length).to.be.greaterThan 400 expect(body.length).to.be.greaterThan(400);
done() return done();
});
});
});
describe "warming the cache", -> return describe("warming the cache", function() {
beforeEach -> beforeEach(function() {
@fileUrl = @fileUrl + '?style=preview&cacheWarm=true' return this.fileUrl = this.fileUrl + '?style=preview&cacheWarm=true';
});
it "should not time out", (done) -> it("should not time out", function(done) {
@timeout(1000 * 20) this.timeout(1000 * 20);
request.get @fileUrl, (err, response, body) => return request.get(this.fileUrl, (err, response, body) => {
expect(response).to.not.equal null expect(response).to.not.equal(null);
done() return done();
});
});
it "should respond with only an 'OK'", (done) -> return it("should respond with only an 'OK'", function(done) {
# note: this test relies of the imagemagick conversion working // note: this test relies of the imagemagick conversion working
@timeout(1000 * 20) this.timeout(1000 * 20);
request.get @fileUrl, (err, response, body) => return request.get(this.fileUrl, (err, response, body) => {
expect(response.statusCode).to.equal 200 expect(response.statusCode).to.equal(200);
body.should.equal 'OK' body.should.equal('OK');
done() return done();
});
});
});
});
});
function __guard__(value, transform) {
return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
}