s3-autoindex
Version:
Serve the contents of a S3 bucket (private or public) over HTTP
91 lines • 3.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// import * as express from 'express';
// var argv = require('minimist')(process.argv.slice(2));
// import * as path from 'path';
// import * as leftPad from 'left-pad';
const https = require("https");
const http = require("http");
const net = require("net");
const rxme = require("rxme");
// import { NextFunction, Response, Request } from 'express-serve-static-core';
// import { RxMe, MatcherCallback } from 'rxme';
// import { Endpoint } from 'aws-sdk/lib/endpoint';
// import { removeAllListeners } from 'cluster';
// import { RxExpress } from './rx-express';
// import * as leftPad from 'left-pad';
// import { Request } from 'aws-sdk/lib/request';
const simqle = require("simqle");
const q_worker_1 = require("./q-worker");
const rx_http_1 = require("./rx-http");
const file_matcher_1 = require("./file-matcher");
const directory_matcher_1 = require("./directory-matcher");
const parse_config_1 = require("./parse-config");
const AWSReal = require("aws-sdk");
const AWSMock = require("mock-aws-s3");
function RxHttpMatcher(cb) {
return (rx, sub) => {
if (rx.data instanceof net.Server ||
rx.data instanceof http.Server ||
rx.data instanceof https.Server) {
return cb(rx.data, sub);
}
};
}
exports.RxHttpMatcher = RxHttpMatcher;
function RxMsgHttp(rapp) {
return (rq, rs) => {
rapp.next(rx_http_1.RxHttp(rq, rs));
};
}
function server(argv) {
let runningServer;
let runningQueue;
return rxme.Observable.create(rxo => {
const config = parse_config_1.default(argv);
let s3;
if (config.aws_module == 'aws') {
rxo.next(rxme.Msg.LogInfo(`booting AWSReal:`, JSON.stringify(config)));
s3 = new AWSReal.S3(config.aws);
}
else {
rxo.next(rxme.Msg.LogInfo(`booting AWSMock:`, JSON.stringify(config)));
s3 = new AWSMock.S3(config.aws);
}
rxo.next(rxme.Msg.Observer(rxo));
const rapp = new rxme.Subject();
simqle.start({ taskTimer: 60000 }).match(simqle.MatchQ(rq => {
runningQueue = rq;
Array(config.s3.Concurrent).fill(0).forEach(a => {
rq.addWorker(q_worker_1.default);
});
rxo.next(rxme.Msg.LogInfo(`Started Q with ${config.s3.Concurrent} workers.`));
rapp
.match(directory_matcher_1.default(rq, rapp, s3, config))
.match(file_matcher_1.default(rq, rapp, s3, config))
.passTo(rxo);
// app.use('/', (req, res, next) => { rapp.next(RxExpress(req, res, next)); });
if (config.https) {
const httpServer = https.createServer(config.https, RxMsgHttp(rapp));
rxo.next(rxme.Msg.LogInfo(`Listen on: https ${config.port}`));
httpServer.listen(config.port);
rxo.next(rxme.Msg.Type(httpServer));
runningServer = httpServer;
}
else {
const httpServer = http.createServer(RxMsgHttp(rapp));
rxo.next(rxme.Msg.LogInfo(`Listen on: http ${config.port}`));
httpServer.listen(config.port);
rxo.next(rxme.Msg.Type(httpServer));
runningServer = httpServer;
}
})).passTo(rapp);
}).match(rxme.Matcher.Complete(() => {
console.log(`got complete`);
runningQueue.stop().passTo();
runningServer.close();
}));
}
exports.server = server;
exports.default = server;
//# sourceMappingURL=server.js.map