UNPKG

@quo0/stiletto

Version:

With stiletto library you will be able to mock requests and choose between preconfigured responses right on the fly via UI

162 lines 7.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserServerServiceFacade = void 0; const tslib_1 = require("tslib"); const constants_1 = require("../../constants"); const environment_1 = require("../../../environments/environment"); const inversify_1 = require("inversify"); const playwright_1 = require("playwright"); const rxjs_1 = require("rxjs"); const ioc_1 = require("../../../ioc"); const mock_taiga_ui_icons_requests_fix_1 = require("./mock-taiga-ui-icons-requests.fix"); const path_1 = require("path"); let BrowserServerServiceFacade = class BrowserServerServiceFacade { constructor(fsService, processService, webSocketServer, structureService) { this.fsService = fsService; this.processService = processService; this.webSocketServer = webSocketServer; this.structureService = structureService; this.mocksAreActive = true; } launch() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { console.log('start browser server'); this.routesConfig = yield this.structureService.current.ROUTES_CONFIG.read(); yield this.launchWebsocketServer(); yield this.launchBrowser(); // and set mocks }); } ; launchWebsocketServer() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { this.webSocketServer.launch(); this.webSocketServer.isReady$.subscribe((isReady) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { if (isReady) { this.webSocketServer.sendMessage({ source: 'server', type: '[WS CONNECTION] ESTABLISHED', }); this.webSocketServer.sendMessage({ source: 'server', type: '[MOCKS]', data: this.routesConfig, }); } })); // testing toggleStatus this.webSocketServer.messages$ .pipe((0, rxjs_1.filter)(m => m.type === '[MOCKS] toggle')) .subscribe(() => { if (this.mocksAreActive) { this.unsetMocks(); } else { this.setMocks(); } this.mocksAreActive = !this.mocksAreActive; }); }); } launchBrowser() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { // TODO: connect to existing or launch const getBrowserOptions = { connect: { endpointURL: 'http://localhost:9222/', }, }; const browser = yield this.getBrowser(getBrowserOptions); const context = yield this.getContext(browser); this.context = context; yield this.bypassCSP(); this.provideSettingsToLocalStorage(); if (!environment_1.environment.serve) { this.registerWebComponentInitScript(); yield (0, mock_taiga_ui_icons_requests_fix_1.mockTigaUiIconsRequests)(context, this.processService, this.fsService); yield this.setMocks(); } }); } getBrowser({ connect }) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { if (connect) { console.log('connecteeed'); return playwright_1.chromium.connectOverCDP(connect.endpointURL, connect.options); } return playwright_1.chromium.launch({ headless: false }); }); } getContext(browser) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { return browser.contexts()[0]; }); } bypassCSP() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { yield this.context.on('page', (page) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { const client = yield this.context.newCDPSession(page); yield client.send('Page.setBypassCSP', { enabled: true }); })); }); } provideSettingsToLocalStorage() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { const content = ` localStorage.setItem('${"STILETTO_TOGGLE_UI_SHORTCUT" /* TOGGLE_UI_SHORTCUT */}', JSON.stringify([ ${constants_1.STILETTO_TOGGLE_UI_SHORTCUT.map(s => `"${s}"`)} ])); localStorage.setItem('${"STILETTO_WS_PORT" /* WS_PORT */}', JSON.stringify(${constants_1.STILETTO_WS_PORT})); `; yield this.context.addInitScript({ content }); }); } registerWebComponentInitScript() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { const isPublishedVersion = __dirname.split(path_1.sep).some(segment => ['publish', 'node_modules'].includes(segment)); const pathToAssets = isPublishedVersion ? (0, path_1.resolve)(__dirname, '../../../../') : 'libs/stiletto/src/assets/web-component'; console.log('__dirname', __dirname); console.log('pathToAssets', pathToAssets); yield this.context.addInitScript({ path: `${pathToAssets}/stiletto-ui.js` }); yield this.context.addInitScript({ path: `${pathToAssets}/stiletto-ui.init.js` }); }); } setMocks() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { for (const mock of this.routesConfig || []) { console.log('mocking:', mock.urlPattern); yield this.context.route(mock.urlPattern, (route) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { if (!mock.enabled) { return route.continue(); } const { body, contentType, headers, status } = mock.config.onSuccess.mockValue; route.fulfill({ body, status, headers: headers || (yield route.request().allHeaders()), contentType: contentType || 'application/json', }); })); } }); } unsetMocks() { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { for (const mock of this.routesConfig || []) { console.log('UNmocking:', mock.urlPattern); yield this.context.unroute(mock.urlPattern); } }); } }; BrowserServerServiceFacade = (0, tslib_1.__decorate)([ (0, inversify_1.injectable)(), (0, tslib_1.__param)(0, (0, inversify_1.inject)(ioc_1.TOKENS.fsService)), (0, tslib_1.__param)(1, (0, inversify_1.inject)(ioc_1.TOKENS.processService)), (0, tslib_1.__param)(2, (0, inversify_1.inject)(ioc_1.TOKENS.webSocketServerService)), (0, tslib_1.__param)(3, (0, inversify_1.inject)(ioc_1.TOKENS.structureService)), (0, tslib_1.__metadata)("design:paramtypes", [Object, Object, Object, Object]) ], BrowserServerServiceFacade); exports.BrowserServerServiceFacade = BrowserServerServiceFacade; //# sourceMappingURL=browser-server-faced.service.js.map