s3-autoindex
Version:
Serve the contents of a S3 bucket (private or public) over HTTP
145 lines • 5.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const server_1 = require("../src/server");
const AWSMock = require("mock-aws-s3");
const uuid = require("uuid");
const request = require("request-promise-native");
const htmlparser = require("htmlparser2");
const rxme = require("rxme");
const memwatch = require("memwatch-next");
const timers_1 = require("timers");
const DomHandler = require('domhandler');
const DomUtils = require('domutils');
const testId = uuid.v4();
const testPort = '' + ~~((Math.random() * (65535 - 1024)) + 1024);
AWSMock.config.basePath = `dist/test`;
const s3 = new AWSMock.S3({});
function htmlToDom(htmlfrag) {
return rxme.Observable.create(obs => {
const dhandler = new DomHandler((_, dom) => {
// console.log(_, dom);
obs.next(new rxme.RxMe(dom));
obs.complete();
});
const parser = new htmlparser.Parser(dhandler);
parser.write(htmlfrag);
parser.end();
});
}
// a FSM is simple but who can understand this -;)
const actions = [
(idx, html, fname, cb) => {
// console.log(html);
html.match(rx => {
if (!Array.isArray(rx.data)) {
return;
}
// console.log(`Jojo:`, rx.data);
const pre = DomUtils.getElementsByTagName('pre', rx.data, true);
chai_1.assert.equal(1, pre.length);
chai_1.assert.equal('', pre[0].next.data.trim());
}).match(rxme.Matcher.Complete(() => {
s3.putObject({ Body: '<hw>hello world</hw>', Bucket: testId, Key: 'hello-world' }, (err, data) => {
// console.log(err, data);
requestServer(idx + 1, fname, cb);
});
})).passTo();
},
(idx, html, fname, cb) => {
html.match(rx => {
if (!Array.isArray(rx.data)) {
return;
}
// console.log(rx.data);
const pre = DomUtils.getElementsByTagName('a', rx.data, true);
chai_1.assert.equal(2, pre.length);
// console.log(pre[1].attribs);
// console.log(pre[1].children);
chai_1.assert.equal('hello-world', pre[1].attribs.href);
chai_1.assert.equal('hello-world', pre[1].children[0].data);
}).match(rxme.Matcher.Complete(() => {
requestServer(idx + 1, `${fname}/hello-world`, cb);
})).passTo();
},
(idx, html, fname, cb) => {
html.match(rx => {
if (!Array.isArray(rx.data)) {
return;
}
// console.log(rx.data);
const pre = DomUtils.getElementsByTagName('hw', rx.data, true);
chai_1.assert.equal(1, pre.length);
chai_1.assert.equal('hw', pre[0].name);
// console.log(pre[1].children);
chai_1.assert.equal('hello world', pre[0].children[0].data);
}).match(rxme.Matcher.Complete(() => {
let next = idx + 1;
if (fname.startsWith('/basepath')) {
next = -1;
}
requestServer(next, '/basepath//', cb);
})).passTo();
}
];
function requestServer(idx, fname, cb) {
if (idx < 0) {
// srv.close();
cb();
return;
}
const url = `http://localhost:${testPort}/${fname}`;
// console.log(`requestServer:${url}:${idx}`);
request(url).then(html => {
// console.log(`requestServer:${html}`);
actions[idx % actions.length](idx, htmlToDom(html), fname, cb);
});
}
function forceGC() {
if (global.gc) {
global.gc();
}
else {
console.warn('No GC hook! Start your program as `node --expose-gc file.js`.');
}
}
s3.createBucket({ Bucket: testId }, (err, data) => {
// console.log(`createBucket`, err);
let looping = 1000;
let obs;
server_1.server([
'--basepath', '/basepath',
'--s3-Bucket', testId,
'--port', testPort,
'--aws-endpoint', 'https://mock.land',
'--aws-module', 'mock'
]).match(rxme.Matcher.Log(log => {
// if (log.level != rxme.LogLevel.DEBUG) {
// console.log(log);
// }
})).match(rxme.Matcher.Observer(rx => {
// console.log(rx);
obs = rx;
})).match(server_1.RxHttpMatcher(srv => {
const hd = new memwatch.HeapDiff();
function loop() {
// console.log(looping);
if (--looping > 0) {
requestServer(0, '', loop);
}
else {
obs.complete();
forceGC();
timers_1.setTimeout(() => {
const diff = hd.end();
console.log(`Sending Complete:`, JSON.stringify({
before: diff.before,
after: diff.after
}, null, 2));
}, 50);
}
}
loop();
})).passTo();
});
//# sourceMappingURL=test.js.map