From da1fa153a9d52049f57dbee9649ae333ef3c548a Mon Sep 17 00:00:00 2001 From: Carlo Beltrame Date: Fri, 30 Oct 2020 11:03:44 +0100 Subject: [PATCH] Treat the environment during webpack build as browser rather than node.js Fixes mock-server/mockserver-client-node#47 --- mockServerClient.js | 8 ++++++-- package-lock.json | 5 +++++ package.json | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mockServerClient.js b/mockServerClient.js index f40af99..ae83fd0 100644 --- a/mockServerClient.js +++ b/mockServerClient.js @@ -10,6 +10,10 @@ var mockServerClient; (function () { "use strict"; + runningInNode = function () { + return (typeof require !== 'undefined') && require('browser-or-node').isNode + } + /** * Start the client communicating at the specified host and port * for example: @@ -24,7 +28,7 @@ var mockServerClient; */ mockServerClient = function (host, port, contextPath, tls, caCertPemFilePath) { - var makeRequest = (typeof require !== 'undefined' ? require('./sendRequest').sendRequest(tls, caCertPemFilePath) : function (host, port, path, jsonBody, resolveCallback) { + var makeRequest = (runningInNode() ? require('./sendRequest').sendRequest(tls, caCertPemFilePath) : function (host, port, path, jsonBody, resolveCallback) { var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); var url = (tls ? 'https' : 'http') + '://' + host + ':' + port + path; @@ -171,7 +175,7 @@ var mockServerClient; }; }; - var WebSocketClient = (typeof require !== 'undefined' ? require('./webSocketClient').webSocketClient(tls, caCertPemFilePath) : function (host, port, contextPath) { + var WebSocketClient = (runningInNode() ? require('./webSocketClient').webSocketClient(tls, caCertPemFilePath) : function (host, port, contextPath) { var clientId; var clientIdHandler; var requestHandler; diff --git a/package-lock.json b/package-lock.json index e762a34..f83dfba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -528,6 +528,11 @@ } } }, + "browser-or-node": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/browser-or-node/-/browser-or-node-1.3.0.tgz", + "integrity": "sha512-0F2z/VSnLbmEeBcUrSuDH5l0HxTXdQQzLjkmBR4cYfvg1zJrKSlmIZFqyFR8oX0NrwPhy3c3HQ6i3OxMbew4Tg==" + }, "browser-process-hrtime": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz", diff --git a/package.json b/package.json index 76faef3..1c0205c 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "gruntplugin" ], "dependencies": { + "browser-or-node": "^1.3.0", "grunt-ts": "^6.0.0-beta.22", "q": "~2.0", "websocket": "^1.0.30"