From 000a10b375662b09d7f998a7ee3ddb40f616ef84 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Sun, 15 Jul 2018 10:10:55 -0500 Subject: [PATCH] host utils --- lib/utils/host.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/utils/host.js diff --git a/lib/utils/host.js b/lib/utils/host.js new file mode 100644 index 00000000..67588161 --- /dev/null +++ b/lib/utils/host.js @@ -0,0 +1,45 @@ +const isDocker = (() => { + let isDocker; + + const hostname = require('os').hostname(); + const pattern = new RegExp( + '[0-9]+\:[a-z_-]+\:\/docker\/' + hostname + '[0-9a-z]+', 'i' + ); + + try { + isDocker = require('child_process') + .execSync( + 'cat /proc/self/cgroup', + {stdio: ['ignore', 'pipe', 'ignore']} + ) + .toString().match(pattern) !== null; + } catch (e) { + isDocker = false; + } + + return isDocker; +})(); + +const defaultHost = isDocker ? '0.0.0.0' : 'localhost'; + +// when we're runing in Docker, we can expect (generally, in a development +// scenario) that the user would like to connect to the service in the +// container via the **host's** loopback address, so this helper can be used to +// swap 0.0.0.0 for localhost in code/messages that pertain to client-side +function canonicalHost(host) { + return isDocker && host === '0.0.0.0' ? 'localhost' : host; +} + +function dockerHostSwap(host) { + return (isDocker && (host === 'localhost' || host === '127.0.0.1')) ? defaultHost : host; +} + +const defaultCorsHost = canonicalHost(defaultHost); + +module.exports = { + canonicalHost, + defaultCorsHost, + defaultHost, + dockerHostSwap, + isDocker +};