Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/client/requests/eventsource.js
305 views
1
import EventEmitter from "events";
2
import HookEvent from "../hook.js";
3
4
/**
5
* @typedef {import('../index').default} UVClient
6
*/
7
8
class EventSourceApi extends EventEmitter {
9
/**
10
*
11
* @param {UVClient} ctx
12
*/
13
constructor(ctx) {
14
super();
15
this.ctx = ctx;
16
this.window = ctx.window;
17
this.EventSource = this.window.EventSource || {};
18
this.esProto = this.EventSource.prototype || {};
19
this.url = ctx.nativeMethods.getOwnPropertyDescriptor(this.esProto, "url");
20
this.CONNECTING = 0;
21
this.OPEN = 1;
22
this.CLOSED = 2;
23
}
24
overrideConstruct() {
25
this.ctx.override(
26
this.window,
27
"EventSource",
28
(target, that, args) => {
29
if (!args.length) return new target(...args);
30
let [url, config = {}] = args;
31
32
const event = new HookEvent({ url, config }, target, that);
33
this.emit("construct", event);
34
35
if (event.intercepted) return event.returnValue;
36
return new event.target(event.data.url, event.data.config);
37
},
38
true
39
);
40
41
if ("EventSource" in this.window) {
42
this.window.EventSource.CONNECTING = this.CONNECTING;
43
this.window.EventSource.OPEN = this.OPEN;
44
this.window.EventSource.CLOSED = this.CLOSED;
45
}
46
}
47
overrideUrl() {
48
this.ctx.overrideDescriptor(this.esProto, "url", {
49
get: (target, that) => {
50
const event = new HookEvent({ value: target.call(that) }, target, that);
51
this.emit("url", event);
52
return event.data.value;
53
},
54
});
55
}
56
}
57
58
export default EventSourceApi;
59
60