UNPKG

@puppedo/atoms-mock-server

Version:
231 lines (210 loc) 7.3 kB
/******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ({ /***/ 56: /***/ ((module, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* module decorator */ module = __webpack_require__.hmd(module); module.exports = async function atomRun() { const http = require('http'); const port = this.options.port || process.env.PPD_MOCK_SERVER_PORT || '9009'; const serverData = { routes: [], response: [] }; const { log } = this; const updateRules = body => { const { data = {} } = JSON.parse(body); const { response = [], append = false } = data; let { routes = [] } = data; if (!Array.isArray(routes)) { const newRoutes = []; for (const route of Object.keys(routes)) { newRoutes.push([...routes[route], route]); } routes = newRoutes; } if (!append) { var _response$responses; serverData.routes = routes; serverData.response = (_response$responses = response.responses) !== null && _response$responses !== void 0 ? _response$responses : []; } else { var _response$responses2, _response$responses3; serverData.routes = [...serverData.routes, ...routes]; const newResponseKeys = ((_response$responses2 = response.responses) !== null && _response$responses2 !== void 0 ? _response$responses2 : []).map(v => v.responseKey); serverData.response = serverData.response.filter(v => !newResponseKeys.includes(v.responseKey)); serverData.response = [...serverData.response, ...((_response$responses3 = response.responses) !== null && _response$responses3 !== void 0 ? _response$responses3 : [])]; } }; const resolverMock = (req, res) => { let requestBody = ''; req.on('data', chunk => { requestBody += chunk; }); req.on('end', () => { try { for (const routeData of serverData.routes) { const { type, responseKey, data, route, dataRegExp = false } = routeData; if (req.method === type && req.url === route) { let dataMatch = true; if (data && (dataRegExp ? !new RegExp(data, 'gm').test(requestBody) : data !== requestBody)) { dataMatch = false; } if (dataMatch) { const response = serverData.response.find(v => v.responseKey === responseKey); const { body, code } = response !== null && response !== void 0 ? response : {}; log({ text: `Request Url: ${req.url}, Method: ${req.method}, Code: ${code}, Body: ${JSON.stringify(body)}`, level: 'info' }); res.writeHead(parseInt(code !== null && code !== void 0 ? code : '200', 10), { 'Content-Type': 'application/json' }); res.write(JSON.stringify(body !== null && body !== void 0 ? body : {})); return res.end(); } } } } catch (error) { console.log(error); res.write(error); } finally { res.statusCode = 500; res.end(); } }); return null; // return null, because we will handle 'end' events in this function }; async function requestHandler(request, response) { return new Promise((resolve, reject) => { if (request.method === 'DELETE' && request.url === '/ppd-mock-server-stop') { // eslint-disable-next-line no-use-before-define server.close(); log({ text: 'Server Stoped.', level: 'info' }); resolve(); } else if (request.method === 'POST' && request.url === '/ppd-mock-server-routes') { let body = ''; request.on('data', data => { body += data; log({ text: `Routes Data: ${data}`, level: 'info' }); }); request.on('end', () => { try { updateRules(body); } catch (error) { reject(error); } resolve(); }); } else { resolverMock(request, response); resolve(); // Resolve the promise because response.end() will be called inside resolverMock } }); } const server = http.createServer(requestHandler); server.listen(port, error => { if (error) { this.log({ text: `Something bad happened with server on port: ${port}. ${error}`, level: 'error' }); } }); await this.log({ text: `Starting server port: ${port}`, level: 'info' }); }; /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ id: moduleId, /******/ loaded: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/harmony module decorator */ /******/ (() => { /******/ __webpack_require__.hmd = (module) => { /******/ module = Object.create(module); /******/ if (!module.children) module.children = []; /******/ Object.defineProperty(module, 'exports', { /******/ enumerable: true, /******/ set: () => { /******/ throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id); /******/ } /******/ }); /******/ return module; /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ /******/ /******/ // startup /******/ // Load entry module and return exports /******/ // This entry module is referenced by other modules so it can't be inlined /******/ var __webpack_exports__ = __webpack_require__(56); /******/ module.exports = __webpack_exports__; /******/ /******/ })() ; //# sourceMappingURL=mockServerStart.js.map