@pact-foundation/pact
Version:
Pact for all things Javascript
74 lines • 3.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProxy = exports.waitForServerReady = void 0;
const node_http_1 = __importDefault(require("node:http"));
const body_parser_1 = __importDefault(require("body-parser"));
const express_1 = __importDefault(require("express"));
const http_proxy_1 = __importDefault(require("http-proxy"));
const logger_1 = __importDefault(require("../../../common/logger"));
const hooks_1 = require("./hooks");
const messages_1 = require("./messages");
const proxyRequest_1 = require("./proxyRequest");
const stateHandler_1 = require("./stateHandler/stateHandler");
const tracer_1 = require("./tracer");
// Listens for the server start event
const waitForServerReady = (server) => new Promise((resolve, reject) => {
server.on('listening', () => resolve(server));
server.on('error', () => reject(new Error('Unable to start verification proxy server')));
});
exports.waitForServerReady = waitForServerReady;
// Get the Proxy we'll pass to the CLI for verification
const createProxy = (config, stateSetupPath, messageTransportPath, hooksState) => {
const app = (0, express_1.default)();
const proxy = new http_proxy_1.default();
logger_1.default.trace(`Setting up state proxy with path: ${stateSetupPath}`);
// NOTE: if you change any of these global middleware that consumes the body
// review the "proxyReq" event reader below
app.use(body_parser_1.default.json({
type: [
'application/json',
'application/json; charset=utf-8',
'application/json; charset=utf8',
],
}));
app.use(body_parser_1.default.urlencoded({ extended: true }));
app.use('/{*splat}', body_parser_1.default.raw({ type: '*/*' }));
// Hooks. Tracking is only needed when a hook is registered; both hooks share
// the same interaction-boundary tracking, so they are registered together.
if (config.beforeEach || config.afterEach) {
if (config.beforeEach)
logger_1.default.trace("registered 'beforeEach' hook");
if (config.afterEach)
logger_1.default.trace("registered 'afterEach' hook");
app.use(stateSetupPath, (0, hooks_1.registerHooks)({ beforeEach: config.beforeEach, afterEach: config.afterEach }, hooksState));
}
// Trace req/res logging
if (config.logLevel === 'debug' || config.logLevel === 'trace') {
logger_1.default.info('debug request/response logging enabled');
app.use((0, tracer_1.createRequestTracer)());
app.use((0, tracer_1.createResponseTracer)());
}
// Allow for request filtering
if (config.requestFilter !== undefined) {
app.use(config.requestFilter);
}
// Setup provider state handler
app.post(stateSetupPath, (0, stateHandler_1.createProxyStateHandler)(config));
// Register message handler and transport
// TODO: ensure proxy does not interfere with this
app.post(messageTransportPath, (0, messages_1.createProxyMessageHandler)(config));
// Proxy server will respond to Verifier process
app.all('/{*splat}', (req, res) => {
logger_1.default.debug(`Proxying ${req.method}: ${req.path}`);
proxy.web(req, res, (0, proxyRequest_1.toServerOptions)(config, req));
});
// TODO: node is now using ipv6 as a default. This should be customised
return node_http_1.default
.createServer(app)
.listen(undefined, config.proxyHost || '127.0.0.1');
};
exports.createProxy = createProxy;
//# sourceMappingURL=proxy.js.map