Path: blob/main/src/client/requests/eventsource.js
305 views
import EventEmitter from "events";1import HookEvent from "../hook.js";23/**4* @typedef {import('../index').default} UVClient5*/67class EventSourceApi extends EventEmitter {8/**9*10* @param {UVClient} ctx11*/12constructor(ctx) {13super();14this.ctx = ctx;15this.window = ctx.window;16this.EventSource = this.window.EventSource || {};17this.esProto = this.EventSource.prototype || {};18this.url = ctx.nativeMethods.getOwnPropertyDescriptor(this.esProto, "url");19this.CONNECTING = 0;20this.OPEN = 1;21this.CLOSED = 2;22}23overrideConstruct() {24this.ctx.override(25this.window,26"EventSource",27(target, that, args) => {28if (!args.length) return new target(...args);29let [url, config = {}] = args;3031const event = new HookEvent({ url, config }, target, that);32this.emit("construct", event);3334if (event.intercepted) return event.returnValue;35return new event.target(event.data.url, event.data.config);36},37true38);3940if ("EventSource" in this.window) {41this.window.EventSource.CONNECTING = this.CONNECTING;42this.window.EventSource.OPEN = this.OPEN;43this.window.EventSource.CLOSED = this.CLOSED;44}45}46overrideUrl() {47this.ctx.overrideDescriptor(this.esProto, "url", {48get: (target, that) => {49const event = new HookEvent({ value: target.call(that) }, target, that);50this.emit("url", event);51return event.data.value;52},53});54}55}5657export default EventSourceApi;585960