Skip to content

Commit 6407b0b

Browse files
refactor: use URL interface to parse origin
close #59 close #60
1 parent 2c863c9 commit 6407b0b

2 files changed

Lines changed: 3 additions & 21 deletions

File tree

src/events/event.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { EventType, EventData } from './types';
2-
import { isIPAddress } from '../utilities/isIPAddress';
32

43
const ALLOWED_ORIGINS: string[] = [
54
'https://nmrxiv.org',
@@ -16,22 +15,6 @@ const ALLOWED_ORIGINS: string[] = [
1615

1716
const namespace = 'nmr-wrapper';
1817

19-
function parseOrigin(origin: string) {
20-
let url: string | null = '';
21-
const urlSegments = origin.split('://');
22-
if (isIPAddress(origin)) {
23-
url = origin;
24-
} else {
25-
const hostSegments = urlSegments[1].split('.');
26-
const startIndex = hostSegments.length > 2 ? 1 : 0;
27-
url = `${urlSegments[0]}://${hostSegments
28-
.slice(hostSegments.length > 1 ? startIndex : 0)
29-
.join('.')}`;
30-
}
31-
32-
return url;
33-
}
34-
3518
function trigger<T extends EventType>(type: T, data: EventData<T>) {
3619
window.parent.postMessage({ type: `${namespace}:${type}`, data }, '*');
3720
}
@@ -47,7 +30,9 @@ function on<T extends EventType>(
4730
data: { type: targetType, data },
4831
} = event;
4932

50-
if (!ALLOWED_ORIGINS.includes(parseOrigin(origin))) {
33+
const url = new URL(origin);
34+
35+
if (!ALLOWED_ORIGINS.includes(url.origin)) {
5136
throw new Error(`Invalid Origin ${origin}`);
5237
}
5338

src/utilities/isIPAddress.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)