-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
232 lines (221 loc) · 5.55 KB
/
index.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Imports services
const xgd = require("./services/xgd")
const libaro = require("./services/libaro")
const quecto = require("./services/quecto")
const unshort = require("./services/unshort")
// Running environment
var environment = "node"
if(typeof window != "undefined") environment = "browser"
// List of services caracteristics
var servicesInfos = {
"is.gd": {
name: "is.gd",
website: "https://is.gd",
id: "is-gd",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "xgd"
},
"v.gd": {
name: "v.gd",
website: "https://v.gd",
id: "v-gd",
shortcodes: true,
corsFriendly: true,
instantRedirect: false,
providerFile: "xgd"
},
"liba.ro": {
name: "liba.ro",
website: "https://liba.ro",
id: "liba-ro",
shortcodes: false,
corsFriendly: true,
instantRedirect: true,
providerFile: "libaro"
},
"s.oriondev.fr": {
name: "s.oriondev.fr",
website: "https://s.oriondev.fr",
id: "s-oriondev-fr",
shortcodes: true,
corsFriendly: false,
instantRedirect: true,
providerFile: "quecto"
},
"s.3vm.cl": {
name: "s.3vm.cl",
website: "https://s.3vm.cl",
id: "s-3vm-cl",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.ahpc.fi": {
name: "s.ahpc.fi",
website: "https://s.ahpc.fi",
id: "s-ahpc-fi",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.acme.si": {
name: "s.acme.si",
website: "https://s.acme.si",
id: "s-acme-si",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.585.eu": {
name: "s.585.eu",
website: "https://s.585.eu",
id: "s-585-eu",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.fronturi.ro": {
name: "s.fronturi.ro",
website: "https://s.fronturi.ro",
id: "s-fronturi-ro",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"shor.vercel.app": {
name: "shor.vercel.app",
website: "https://shor.vercel.app",
id: "shor-vercel-app",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.cije.us": {
name: "s.cije.us",
website: "https://s.cije.us",
id: "s-cije-us",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.erc.hr": {
name: "s.erc.hr",
website: "https://s.erc.hr",
id: "s-erc-hr",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.jib.ar": {
name: "s.jib.ar",
website: "https://s.jib.ar",
id: "s-jib-ar",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"s.coute.au": {
name: "s.coute.au",
website: "https://s.coute.au",
id: "s-coute-au",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"mdrr.fr": {
name: "mdrr.fr",
website: "https://mdrr.fr",
id: "mdrr-fr",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
},
"ptdrr.com": {
name: "ptdrr.com",
website: "https://ptdrr.com",
id: "ptdrr-com",
shortcodes: true,
corsFriendly: true,
instantRedirect: true,
providerFile: "unshort"
}
}
// Main function
/**
* Shorten an URL
* @param {string} url - URL to shorten
* @param {string} provider - Provider domain to use
* @param {string} shortcode - Code used for pathname
* @returns {string} Shortened URL
* @throws {string} Error message
*/
async function short(url, provider, shortcode){
// If no url specified, throw an error
if(!url || url.length < 2) return new Error("[lib] No URL specified")
if(!url.startsWith("http://") && !url.startsWith("https://")) url = `https://${url}`
// If no provider specified, use a random one
if(!provider) provider = Object.values(servicesInfos).filter(service => environment == "browser" ? service.corsFriendly : true)[Math.floor(Math.random() * Object.values(servicesInfos).filter(service => environment == "browser" ? service.corsFriendly : true).length)]
else provider = servicesInfos[provider]
// If still no provider found, throw an error
if(!provider) return new Error("[lib] No provider found")
// Shorten the URL
if(provider.providerFile == "xgd") return xgd(provider.name, url, shortcode)
else if(provider.providerFile == "libaro") return libaro(url)
else if(provider.providerFile == "quecto") return quecto(provider.name, url, shortcode)
else if(provider.providerFile == "unshort") return unshort(provider.name, url, shortcode)
else return new Error("[lib] Unknown provider file")
}
// Exports
var exports = {
// Version
version: require("./package.json").version,
// List of services domains
servicesDomains: ["is.gd", "v.gd", "liba.ro", "s.oriondev.fr", "s.3vm.cl", "s.ahpc.fi", "s.acme.si", "s.585.eu", "s.fronturi.ro", "shor.vercel.app", "s.cije.us", "s.erc.hr", "s.jib.ar", "s.coute.au", "mdrr.fr", "ptdrr.com"],
// List of services caracteristics
servicesInfos,
// List of services credits
servicesCredits: [
{
name: "is.gd",
website: "https://is.gd",
corsFriendly: true
},
{
name: "Libaro",
website: "https://liba.ro",
corsFriendly: true
},
{
name: "Quecto",
website: "https://s.oriondev.fr",
corsFriendly: false
},
{
name: "FreeDNS",
website: "https://freedns.afraid.org",
corsFriendly: true
},
{
name: "Unshort",
website: "https://unshort.johanstick.fr",
corsFriendly: true
}
],
// Main function
short
}
if(environment == "browser") window.moreshort = exports, window.short = short
else module.exports = exports