-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
478 lines (475 loc) · 11.5 KB
/
lib.js
File metadata and controls
478 lines (475 loc) · 11.5 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* @author AtomicGamer9523
* @license MIT
* @version 1.0.0-alpha.5
* @description Titanium Web API Library
*/
// deno-lint-ignore-file
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TITANIUM = {}));
})(this, (function (exports) { exports.__private__ = {}; 'use strict';
class TitaniumConnectError extends Error {
constructor(c) {
super(
"Failed to connect to " +
c.connection.secure ? "wss" : "ws" +
"" +
c.connection.host +
":" +
c.connection.port +
"!"
);
this.name = "TitaniumConnectError";
}
}
class JSLib {
#code;
constructor(code) {
this.#code = code;
}
getCode() {
return this.#code;
}
inject() {
try {
eval(this.#code);
} catch (e) {
console.error("Error injecting JSLIB");
console.error(e);
}
}
}
exports.JSLib = JSLib;
class Option {
#valid;
#value;
/**
* ## `of`
* ### creates a new option with a value
* @param {T} value value to create the option with
* @returns {Option<T>} new option
* @throws {TypeError} if the value is undefined
* @see {@link IOption#ofNullable}
* @see {@link IOption#empty}
*/
static of(value) {
if (value === undefined) throw new TypeError("Value cannot be undefined");
return new Option(value);
}
/**
* ## `ofNullable`
* ### creates a new option with a value or undefined
* @param {T | undefined} value value to create the option with
* @returns {Option<T>} new option
* @see {@link IOption#of}
* @see {@link IOption#empty}
*/
static ofNullable(value) {
return new Option(value);
}
/**
* ## `empty`
* ### creates a new empty option
* @returns {Option<T>} new option
* @see {@link IOption#of}
* @see {@link IOption#ofNullable}
*/
static empty() {
return new Option(undefined);
}
constructor(value) {
if (value === undefined) this.#valid = false;
else this.#valid = true;
this.#value = value;
}
isPresent() {
return this.#valid && this.#value !== undefined;
}
getStrict() {
if (this.#value === undefined) throw new TypeError("Option is empty");
return this.#value;
}
get() {
return this.#value;
}
ifPresent(consumer) {
if (this.#value !== undefined) consumer(this.#value);
}
ifPresentOrElse(consumer, orElse) {
if (this.#value !== undefined) consumer(this.#value);
else orElse();
}
orElseGet(supplier) {
if (this.#value !== undefined) return this.#value;
else return supplier();
}
orElse(other) {
if (this.#value !== undefined) return this.#value;
else return other;
}
orElseThrow(error) {
if (this.#value !== undefined) return this.#value;
else throw error;
}
map(mapper) {
if (this.#value !== undefined) return Option.ofNullable(mapper(this.#value));
else return Option.empty();
}
flatMap(mapper) {
if (this.#value !== undefined) return mapper(this.#value);
else return Option.empty();
}
filter(predicate) {
if (this.#value !== undefined) {
if (predicate(this.#value)) return this;
else return Option.empty();
}
else return Option.empty();
}
equals(other) {
if (this.isPresent() && other.isPresent()) return this.#value === other.#value;
else if (!this.isPresent() && !other.isPresent()) return true;
else return false;
}
set(value) {
this.#value = value;
this.#valid = true;
}
clear() {
this.#value = undefined;
this.#valid = false;
}
setNullable(value) {
this.#value = value;
if (value === undefined) this.#valid = false;
else this.#valid = true;
}
}
exports.Option = Option;
class EventEmitter {
_events_ = new Map();
on(event, listener) {
if (!this._events_.has(event)) this._events_.set(event, new Set());
this._events_.get(event).add(listener);
return this;
}
once(event, listener) {
const l = listener;
l.__once__ = true;
return this.on(event, l);
}
off(event, listener) {
if ((event === undefined || event === null) && listener)
throw new Error("Why is there a listener defined here?");
else if ((event === undefined || event === null) && !listener)
this._events_.clear();
else if (event && !listener)
this._events_.delete(event);
else if (event && listener && this._events_.has(event)) {
const _ = this._events_.get(event);
_.delete(listener);
if (_.size === 0) this._events_.delete(event);
} else;
return this;
}
emitSync(event, ...args) {
if (!this._events_.has(event)) return this;
const _ = this._events_.get(event);
for (const [, listener] of _.entries()) {
const r = listener(...args);
if (r instanceof Promise) r.catch(console.error);
if (listener.__once__) {
delete listener.__once__;
_.delete(listener);
}
}
if (_.size === 0) this._events_.delete(event);
return this;
}
async emit(event, ...args) {
if (!this._events_.has(event)) return this;
const _ = this._events_.get(event);
for (const [, listener] of _.entries()) {
try {
await listener(...args);
if (listener.__once__) {
delete listener.__once__;
_.delete(listener);
}
} catch (error) {
console.error(error);
}
}
if (_.size === 0) this._events_.delete(event);
return this;
}
queue(event, ...args) {
(async () => await this.emit(event, ...args))().catch(console.error);
return this;
}
pull(event, timeout) {
return new Promise(async (resolve, reject) => {
let timeoutId;
const listener = (...args) => {
if (timeoutId !== null)
clearTimeout(timeoutId);
resolve(args);
};
timeoutId = typeof timeout !== "number"
? null
: setTimeout(() => (this.off(event, listener),
reject(new Error("Timed out!"))))
;
this.once(event, listener);
});
}
}
exports.EventEmitter = EventEmitter;
/**
* ## `e2e`
* ### casts a connectable to a connection
* @param {C1 | C2} i
* @returns {C3 | C4}
* @throws {TypeError}
* @template C1 extends IConnection
* @template C2 extends IStrictConnection
* @template C3 extends IConnectable
* @template C4 extends IStrictConnectable
*/
function e2c(i) {
if ("strictConnection" in i) return {
host: i["strictConnection"].host,
port: i["strictConnection"].port,
secure: i["strictConnection"].secure,
};
return {
host: i.connection.host,
port: i.connection.port,
secure: i.connection.secure,
};
}
/**
* ## `c2e`
* ### casts a connection to a connectable
* @param {C1 | C2} i
* @returns {C3 | C4}
* @throws {TypeError}
* @template C1 extends IConnection
* @template C2 extends IStrictConnection
* @template C3 extends IConnectable
* @template C4 extends IStrictConnectable
*/
function c2e(i) {
if ("host" in i && "port" in i && "secure" in i) {
if (typeof i.host === "string" &&
typeof i.port === "number" &&
typeof i.secure === "boolean") {
return {
connection: {
host: i.host,
port: i.port,
secure: i.secure,
},
strictConnection: {
host: i.host,
port: i.port,
secure: i.secure,
}
};
} else return {
connection: {
host: i.host,
port: i.port,
secure: i.secure,
}
};
} else throw new TypeError("Invalid connection");
}
/**
* ## `iConnectionParser`
* ### parses a connection string or object into a connection object
* @param {string | IConnection} connection
* @returns {IStrictConnection}
*/
function iConnectionParser(connection) {
if (connection === undefined) return {
host: "localhost",
port: 8080,
secure: false
};
if (typeof connection !== "string") return {
host: connection.host ?? "localhost",
port: connection.port ?? 8080,
secure: connection.secure ?? false
};
const newHost = {
host: "localhost",
port: 8080,
secure: false
};
if (connection.includes("://")) {
const split = connection.split("://");
newHost.secure = split[0] === "wss";
const sp = split[1];
if (sp.includes(":")) {
const split = sp.split(":");
newHost.host = split[0];
newHost.port = parseInt(split[1]);
} else newHost.host = sp;
} else {
if (connection.includes(":")) {
const split = connection.split(":");
newHost.host = split[0];
newHost.port = parseInt(split[1]);
}
else newHost.host = connection;
}
return newHost;
}
class ConnctedTitaniumServer {
#host;
#port;
#secure;
#ws;
#eventHandler;
/**
* ## `constructor`
* ### creates a new connected titanium server
* @param {IStrictConnectable} c strict connectable object
* @param {WebSocket} ws websocket connection
* @constructor
*/
constructor(c, ws) {
this.#host = c.strictConnection.host;
this.#port = c.strictConnection.port;
this.#secure = c.strictConnection.secure;
this.#ws = ws;
this.#eventHandler = new EventEmitter();
this.#ws.onopen = () => {
this.#eventHandler.emitSync("open");
};
this.#ws.onclose = () => {
this.#eventHandler.emitSync("close");
};
this.#ws.onmessage = (msg) => {
this.#eventHandler.emitSync("message", msg.data);
};
}
get strictConnection() {
return {
host: this.#host,
port: this.#port,
secure: this.#secure
};
}
get connection() {
return this.strictConnection;
}
getHost() {
return this.#host;
}
getPort() {
return this.#port;
}
isSecure() {
return this.#secure;
}
disconnect() {
return new TitaniumServer(this);
}
on(event, listener) {
this.#eventHandler.on(event, listener);
}
once(event, listener) {
this.#eventHandler.once(event, listener);
}
off(event, listener) {
this.#eventHandler.off(event, listener);
}
send(data) {
this.#ws.send(data);
}
}
class TitaniumServer {
#host;
#port;
#secure;
constructor(c) {
this.#host = c.strictConnection.host;
this.#port = c.strictConnection.port;
this.#secure = c.strictConnection.secure;
}
get strictConnection() {
return {
host: this.#host,
port: this.#port,
secure: this.#secure
};
}
get connection() {
return this.strictConnection;
}
getHost() {
return this.#host;
}
getPort() {
return this.#port;
}
isSecure() {
return this.#secure;
}
connect() {
try {
const ws = new WebSocket(`${this.#secure ? "wss" : "ws"}://${this.#host}:${this.#port}`);
return new ConnctedTitaniumServer(this, ws);
} catch (_) {
throw new TitaniumConnectError(this);
}
}
}
let __main__ = Option.empty();
function loadJSLIB(uri, blocking) {
blocking = blocking === undefined ? true : blocking;
let __e__ = "";
try {
const __r__ = new XMLHttpRequest();
__r__.onreadystatechange = function () {
if (__r__.readyState == 4) {
if (__r__.status == 200) {
__e__ = __r__.responseText;
} else {
console.error(
"Error Loading Library '" +
uri + "', Server returned status code " +
__r__.status
);
__e__ = "";
}
}
};
__r__.open("GET", uri, !blocking);
__r__.send();
} catch (__f__) {
console.error("Error loading " + uri);
console.error(__f__);
} finally {
return new JSLib(__e__);
}
}
exports.loadJSLIB = loadJSLIB;
function main(callback) {
__main__ = Option.of(callback);
}
exports.main = main;
function __run_main__() {
__main__.ifPresent((c) => c());
}
exports.__run_main__ = __run_main__;
function connect(host) {
const server = new TitaniumServer(c2e(iConnectionParser(host)));
return server.connect();
}
exports.connect = connect;
}));