Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
add support for local environments
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Feb 21, 2022
1 parent 2afe0f5 commit 6053664
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
27 changes: 27 additions & 0 deletions src/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@ import { notificationsChannelURLFromHookURL } from '../utils';

describe('notificationsChannelURLFromHookURL tests', () => {
test('returns notificationsChannelURL, projectName & workspaceName', () => {
// localhost
const localhosts = ["127.0.0.1", "127.0.0.1:9001", "localhost", "localhost:9001"];

for (const hostname of localhosts) {
expect(
notificationsChannelURLFromHookURL(
`http://${hostname}/widget-hook/massive-dynamic/z10`
)
).toEqual({
notificationsChannelURL:
`ws://${hostname}/widget-notifications/massive-dynamic/z10`,
projectName: 'z10',
workspaceName: 'massive-dynamic',
});

expect(
notificationsChannelURLFromHookURL(
`https://${hostname}/widget-hook/massive-dynamic/z10`
)
).toEqual({
notificationsChannelURL:
`wss://${hostname}/widget-notifications/massive-dynamic/z10`,
projectName: 'z10',
workspaceName: 'massive-dynamic',
});
}

// rawhide
expect(
notificationsChannelURLFromHookURL(
Expand Down
22 changes: 15 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,28 @@ export const getDefaultPayload = () => ({
*/
export const notificationsChannelURLFromHookURL = hookURL => {
const hookURLTokens = hookURL.split('/');
const hookURLParsed = new URL(hookURL);
let environment;
let notificationsChannelURL;

const environmentSearch = /api\.(.*)\.canvas/.exec(hookURL);
if (['localhost', '127.0.0.1'].includes(hookURLParsed.hostname)) {
notificationsChannelURL = `${hookURLParsed.protocol === 'http:' ? 'ws' : 'wss'}://${hookURLParsed.hostname}`;
if (hookURLParsed.port) {
notificationsChannelURL += `:${hookURLParsed.port}`
}
} else {
const environmentSearch = /api\.(.*)\.canvas/.exec(hookURL);

if (!environmentSearch) environment = 'production';
else [, environment] = environmentSearch;
if (!environmentSearch) environment = 'production';
else [, environment] = environmentSearch;

notificationsChannelURL = `wss://notification.${
!environmentSearch ? '' : `${environment}.`
}canvas.twyla.ai/widget-notifications/${hookURLTokens[4]}/${hookURLTokens[5]}`;
notificationsChannelURL = `wss://notification.${
!environmentSearch ? '' : `${environment}.`
}canvas.twyla.ai`;
}

return {
notificationsChannelURL,
notificationsChannelURL: `${notificationsChannelURL}/widget-notifications/${hookURLTokens[4]}/${hookURLTokens[5]}`,
workspaceName: hookURLTokens[4],
projectName: hookURLTokens[5],
};
Expand Down

0 comments on commit 6053664

Please sign in to comment.