Select Git revision
api-web.ts 5.07 KiB
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import * as fs from 'fs';
import * as path from 'path';
import * as express from 'express';
import Config from '../config';
import { ConsoleLogger } from 'mol-util/console-logger';
import { resolveJob } from './query';
import { JobManager } from './jobs';
import { UUID } from 'mol-util';
import { LandingPage } from './landing';
function makePath(p: string) {
return Config.appPrefix + '/' + p;
}
function wrapResponse(fn: string, res: express.Response) {
const w = {
doError(this: any, code = 404, message = 'Not Found.') {
if (!this.headerWritten) {
res.status(code).send(message);
this.headerWritten = true;
}
this.end();
},
writeHeader(this: any, binary: boolean) {
if (this.headerWritten) return;
res.writeHead(200, {
'Content-Type': binary ? 'application/octet-stream' : 'text/plain; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'X-Requested-With',
'Content-Disposition': `inline; filename="${fn}"`
});
this.headerWritten = true;
},
writeBinary(this: any, data: Uint8Array) {
if (!this.headerWritten) this.writeHeader(true);
return res.write(new Buffer(data.buffer));
},
writeString(this: any, data: string) {
if (!this.headerWritten) this.writeHeader(false);
return res.write(data);
},
end(this: any) {
if (this.ended) return;
res.end();
this.ended = true;
},
ended: false,
headerWritten: false
};
return w;
}
const responseMap = new Map<UUID, express.Response>();
async function processNextJob() {
if (!JobManager.hasNext()) return;
const job = JobManager.getNext();
const response = responseMap.get(job.id)!;
responseMap.delete(job.id);
const filenameBase = `${job.entryId}_${job.queryDefinition.name.replace(/\s/g, '_')}`
const writer = wrapResponse(job.responseFormat.isBinary ? `${filenameBase}.bcif` : `${filenameBase}.cif`, response);