import url from 'url'; import getGitConfig from 'parse-git-config'; const getPathname = (stringUrl: string) => url.parse(stringUrl).pathname; const suricateURL = 'https://bo.wix.com/suricate'; const WIX_EMAIL_PATTERN = '@wix.com'; const getTunnelId = (namespace: string) => { const gitConfig = getGitConfig.sync({ include: true, type: 'global' }); const gitEmail = gitConfig.user ? gitConfig.user.email : ''; const processUser = process.env.USER; let uniqueTunnelId; if (process.env.SURICATE_TUNNEL_ID) { uniqueTunnelId = process.env.SURICATE_TUNNEL_ID; } else if (gitEmail.endsWith(WIX_EMAIL_PATTERN)) { uniqueTunnelId = gitEmail.replace(WIX_EMAIL_PATTERN, ''); } else if (processUser) { uniqueTunnelId = processUser; } else { return undefined; } const normalizedNamespace = namespace.replace('/', '-').replace('@', '_'); return `${uniqueTunnelId}.${normalizedNamespace}`; }; export const createSocket = ( namespace: string, targetPort: number, options = {}, ) => { // The consumer project needs to install @wix/suricate-client > 0.0.3 // by itself because this is a private dependency // eslint-disable-next-line import/no-unresolved const { socket } = require('@wix/suricate-client'); // eslint-disable-line import/no-extraneous-dependencies return socket({ target: { port: targetPort }, url: suricateURL, tunnelID: getTunnelId(namespace), ...options, }); }; export const getUrl = (namespace: string) => `${suricateURL}/tunnel/${getTunnelId(namespace)}/`; export const getDevServerUrl = (appName: string) => getUrl(`${appName}-dev-server`); export const createDevServerSocket = ( appName: string, port: number, options = {}, ) => createSocket(`${appName}-dev-server`, port, options); export const getDevServerSocketPath = (appName: string) => { const devServerUrl = getDevServerUrl(appName); return `${devServerUrl}?&sockPath=${getPathname(devServerUrl)}sockjs-node`; };