"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockObjectStorage = void 0;
const tslib_1 = require("tslib");
const express_1 = (0, tslib_1.__importDefault)(require("express"));
const got_1 = (0, tslib_1.__importDefault)(require("got"));
const stream_1 = require("stream");
const server_commands_1 = require("../../../../shared/server-commands");
const shared_1 = require("./shared");
class MockObjectStorage {
    initialize() {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            const app = (0, express_1.default)();
            app.get('/:bucketName/:path(*)', (req, res, next) => {
                const url = `http://${req.params.bucketName}.${server_commands_1.ObjectStorageCommand.getEndpointHost()}/${req.params.path}`;
                if (process.env.DEBUG) {
                    console.log('Receiving request on mocked server %s.', req.url);
                    console.log('Proxifying request to %s', url);
                }
                return (0, stream_1.pipeline)(got_1.default.stream(url, { throwHttpErrors: false }), res, (err) => {
                    if (!err)
                        return;
                    console.error('Pipeline failed.', err);
                });
            });
            this.server = yield (0, shared_1.randomListen)(app);
            return (0, shared_1.getPort)(this.server);
        });
    }
    terminate() {
        return (0, shared_1.terminateServer)(this.server);
    }
}
exports.MockObjectStorage = MockObjectStorage;
//# sourceMappingURL=mock-object-storage.js.map