UNPKG

@n1k1t/mock-server

Version:

The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations

64 lines 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WsRequestContext = void 0; const utils_1 = require("../../../utils"); const logger_1 = require("../../../logger"); const models_1 = require("../../models"); const logger = logger_1.Logger.build('Transports.Ws.Context'); class WsRequestContext extends models_1.RequestContext { constructor(provider, incoming, request, socket, head) { super(provider, { transport: 'ws' }); this.provider = provider; this.incoming = incoming; this.request = request; this.socket = socket; this.head = head; this.snapshot = this.compileSnapshot(); this.history = this.compileHistory(); this.additional = { ws: (0, utils_1.cast)(null), }; } compileSnapshot() { const snapshot = super.compileSnapshot(); const state = this.incoming.headers['x-use-mock-state'] ? (0, utils_1.parseJsonSafe)(Buffer.from(String(this.incoming.headers['x-use-mock-state']), 'base64').toString()) : null; if (state?.status === 'ERROR') { logger.error('Got error while parsing [x-use-mock-state] header', state.error?.stack ?? state.error); } snapshot.incoming.method = 'WS'; return snapshot.assign({ ...(state?.status === 'OK' && { state: state.result, }), }); } handle() { logger.info(`Incoming WS connection [${this.incoming.path}]`); this.streams.incoming.subscribe({ error: () => null, next: (message) => logger.info(`Incoming WS connection [${this.incoming.path}] got message of [${message.size()}] bytes`), }); this.streams.outgoing.subscribe({ error: () => null, next: (message) => logger.info(`Incoming WS connection [${this.incoming.path}] sent message with [${message.size()}] bytes`), }); return super.handle(); } skip() { this.streams.incoming.complete(); this.streams.outgoing.complete(); return super.skip(); } complete() { logger.info(`Incoming WS connection [${this.incoming.path}] has finished`, `with status [${this.outgoing?.status ?? 0}] in [${Date.now() - this.timestamp}ms]`); return super.complete(); } static async build(provider, request, socket, head) { const incoming = await (0, models_1.extractHttpIncommingContext)(request); return new WsRequestContext(provider, incoming, request, socket, head); } } exports.WsRequestContext = WsRequestContext; //# sourceMappingURL=context.js.map