Skip to content

Commit 8407f0d

Browse files
feat: allow origins with IP addresses form
1 parent fd618fb commit 8407f0d

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/events/event.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { EventType, EventData } from './types';
2+
import { isIPAddress } from '../utilities/isIPAddress';
23

34
const ALLOWED_ORIGINS: string[] = [
45
'https://nmrxiv.org',
56
'http://nmrxiv.org',
67
'http://localhost',
78
'http://localhost:3000',
9+
'http://127.0.0.1:',
10+
'http://127.0.0.1:3000',
811
'http://test.nmrxiv.org',
912
'http://193.196.39.168',
1013
'http://193.196.39.168:3000',
@@ -13,13 +16,17 @@ const ALLOWED_ORIGINS: string[] = [
1316
const namespace = 'nmr-wrapper';
1417

1518
function parseOrigin(origin: string) {
19+
let url: string | null = '';
1620
const urlSegments = origin.split('://');
17-
const hostSegments = urlSegments[1].split('.');
18-
const startIndex = hostSegments.length > 2 ? 1 : 0;
19-
20-
const url = `${urlSegments[0]}://${hostSegments
21-
.slice(hostSegments.length > 1 ? startIndex : 0)
22-
.join('.')}`;
21+
if (isIPAddress(origin)) {
22+
url = origin;
23+
} else {
24+
const hostSegments = urlSegments[1].split('.');
25+
const startIndex = hostSegments.length > 2 ? 1 : 0;
26+
url = `${urlSegments[0]}://${hostSegments
27+
.slice(hostSegments.length > 1 ? startIndex : 0)
28+
.join('.')}`;
29+
}
2330

2431
return url;
2532
}

src/utilities/isIPAddress.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isIPAddress(ip: string) {
2+
return /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/.test(ip);
3+
}

0 commit comments

Comments
 (0)