-
Notifications
You must be signed in to change notification settings - Fork 1
/
link_manager.ts
163 lines (143 loc) · 5.71 KB
/
link_manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { CustomEmojiId } from "./custom_emoji_id.ts";
import { areTypedArraysEqual, CODEPOINTS, decode, encode, mergeTypedArrays } from "./encode.ts";
import { HttpUrlProtocol, parseURL } from "./http_url.ts";
import { UserId } from "./user_id.ts";
import { checkUtf8 } from "./utf8.ts";
import { beginsWith, CHECK, fullSplit, isAlphaOrDigit, split, toInteger, toLower, tolowerBeginsWith } from "./utilities.ts";
export class LinkManager {
static getLinkUserId(url: Uint8Array): UserId {
const lowerCasedUrl = toLower(url);
url = lowerCasedUrl;
const linkScheme = "tg:";
if (!beginsWith(url, linkScheme)) {
return new UserId();
}
url = url.slice(linkScheme.length);
if (beginsWith(url, "//")) {
url = url.slice(2);
}
const host = "user";
if (!beginsWith(url, host) || (url.length > host.length && !encode("/?#").includes(url[host.length]))) {
return new UserId();
}
url = url.slice(host.length);
if (beginsWith(url, "/")) {
url = url.slice(1);
}
if (!beginsWith(url, "?")) {
return new UserId();
}
url = url.slice(1);
const hashPos = url.indexOf(CODEPOINTS["#"]);
url = url.slice(0, hashPos === -1 ? undefined : hashPos);
for (const parameter of fullSplit(url, CODEPOINTS["&"])) {
const [key, value] = split(parameter, CODEPOINTS["="]);
if (areTypedArraysEqual(key, "id")) {
try {
const rUserId = BigInt(toInteger(value));
return new UserId(rUserId);
} catch (_) {
return new UserId();
}
}
}
return new UserId();
}
static getLinkCustomEmojiId(url: Uint8Array): CustomEmojiId {
const lowerCasedUrl = toLower(url);
url = lowerCasedUrl;
const linkScheme = "tg:";
if (!beginsWith(url, linkScheme)) {
throw new Error("Custom emoji URL must have scheme tg");
}
url = url.slice(linkScheme.length);
if (beginsWith(url, "//")) {
url = url.slice(2);
}
const host = "emoji";
if (!beginsWith(url, host) || (url.length > host.length && !encode("/?#").includes(url[host.length]))) {
throw new Error(`Custom emoji URL must have host "${host}"`);
}
url = url.slice(host.length);
if (beginsWith(url, "/")) {
url = url.slice(1);
}
if (!beginsWith(url, "?")) {
throw new Error("Custom emoji URL must have an emoji identifier");
}
url = url.slice(1);
const hashPos = url.indexOf(CODEPOINTS["#"]);
url = url.slice(0, hashPos === -1 ? undefined : hashPos);
const splitx = fullSplit(url, CODEPOINTS["&"]);
for (const parameter of splitx) {
const [key, value] = split(parameter, CODEPOINTS["="]);
if (areTypedArraysEqual(key, "id")) {
const rDocumentId = BigInt(toInteger(value));
return new CustomEmojiId(rDocumentId);
}
}
throw new Error("Custom emoji URL must have an emoji identifier");
}
static checkLink(link: Uint8Array, httpOnly: boolean, httpsOnly: boolean) {
try {
return this.checkLinkImpl(link, httpOnly, httpsOnly);
} catch (error) {
if (checkUtf8(link)) {
throw new Error(`URL '${decode(link)}' is invalid: ${error.message}`);
} else {
throw new Error(`URL is invalid: ${error.message}`);
}
}
}
static getCheckedLink(link: Uint8Array, httpOnly = false, httpsOnly = false): Uint8Array {
try {
return this.checkLinkImpl(link, httpOnly, httpsOnly);
} catch (_error) {
return new Uint8Array();
}
}
static checkLinkImpl(link: Uint8Array, httpOnly = false, httpsOnly = false): Uint8Array {
let isTg = false;
let isTon = false;
if (tolowerBeginsWith(link, "tg:")) {
link = link.slice(3);
isTg = true;
} else if (tolowerBeginsWith(link, "ton:")) {
link = link.slice(4);
isTon = true;
}
if ((isTg || isTon) && beginsWith(link, "//")) {
link = link.slice(2);
}
const httpUrl = parseURL(link);
if (httpsOnly && (httpUrl.protocol !== HttpUrlProtocol.Https || isTg || isTon)) {
throw new Error("Only HTTP links are allowed");
}
if (isTg || isTon) {
if (httpOnly) {
throw new Error("Only HTTP links are allowed");
}
if (
tolowerBeginsWith(link, "http://") || httpUrl.protocol === HttpUrlProtocol.Https ||
httpUrl.userinfo.length !== 0 || httpUrl.specifiedPort !== 0 || httpUrl.isIpv6
) {
throw new Error(`Wrong ${isTg ? "tg" : "ton"} URL`);
}
let query = httpUrl.query;
CHECK(query[0] === CODEPOINTS["/"]);
if (query.length > 1 && query[1] === CODEPOINTS["?"]) {
query = query.slice(1);
}
for (const c of httpUrl.host) {
if (!isAlphaOrDigit(c) && c !== CODEPOINTS["-"] && c !== CODEPOINTS["_"]) {
throw new Error("Unallowed characters in URL host");
}
}
return mergeTypedArrays(encode(isTg ? "tg" : "ton"), encode("://"), httpUrl.host, query);
}
if (httpUrl.host.indexOf(CODEPOINTS["."]) === -1 && !httpUrl.isIpv6) {
throw new Error("Wrong HTTP URL");
}
return httpUrl.getUrl();
}
}