diff --git a/src/client/index.js b/src/client/index.js index 333eec0..1cf71d1 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -5,7 +5,6 @@ import AttrApi from './dom/attr.js'; import FunctionHook from './native/function.js'; import ObjectHook from './native/object.js'; import Fetch from './requests/fetch.js'; -import WebSocketApi from './requests/websocket.js'; import Xhr from './requests/xhr.js'; import EventSourceApi from './requests/eventsource.js'; import History from './history.js'; @@ -21,6 +20,9 @@ import StyleApi from './dom/style.js'; class UVClient extends EventEmitter { constructor(window = self, worker = !window.window) { super(); + /** + * @type {typeof self} + */ this.window = window; this.nativeMethods = { fnToString: this.window.Function.prototype.toString, @@ -48,7 +50,6 @@ class UVClient extends EventEmitter { this.function = new FunctionHook(this); this.object = new ObjectHook(this); this.message = new MessageApi(this); - this.websocket = new WebSocketApi(this); this.navigator = new NavigatorApi(this); this.eventSource = new EventSourceApi(this); this.attribute = new AttrApi(this); diff --git a/src/client/requests/websocket.js b/src/client/requests/websocket.js deleted file mode 100644 index 9b6f63a..0000000 --- a/src/client/requests/websocket.js +++ /dev/null @@ -1,78 +0,0 @@ -import EventEmitter from '../events.js'; -import HookEvent from '../hook.js'; - -class WebSocketApi extends EventEmitter { - constructor(ctx) { - super(); - this.ctx = ctx; - this.window = ctx.window; - this.WebSocket = this.window.WebSocket || {}; - this.wsProto = this.WebSocket.prototype || {}; - this.url = ctx.nativeMethods.getOwnPropertyDescriptor( - this.wsProto, - 'url' - ); - this.protocol = ctx.nativeMethods.getOwnPropertyDescriptor( - this.wsProto, - 'protocol' - ); - this.send = this.wsProto.send; - this.close = this.wsProto.close; - this.CONNECTING = 0; - this.OPEN = 1; - this.CLOSING = 2; - this.CLOSED = 3; - } - overrideWebSocket() { - this.ctx.override( - this.window, - 'WebSocket', - (target, that, args) => { - if (!args.length) return new target(...args); - let [url, protocols = []] = args; - - if (!this.ctx.nativeMethods.isArray(protocols)) - protocols = [protocols]; - const event = new HookEvent({ url, protocols }, target, that); - this.emit('websocket', event); - - if (event.intercepted) return event.returnValue; - return new event.target(event.data.url, event.data.protocols); - }, - true - ); - - this.window.WebSocket.CONNECTING = this.CONNECTING; - this.window.WebSocket.OPEN = this.OPEN; - this.window.WebSocket.CLOSING = this.CLOSING; - this.window.WebSocket.CLOSED = this.CLOSED; - } - overrideUrl() { - this.ctx.overrideDescriptor(this.wsProto, 'url', { - get: (target, that) => { - const event = new HookEvent( - { value: target.call(that) }, - target, - that - ); - this.emit('url', event); - return event.data.value; - }, - }); - } - overrideProtocol() { - this.ctx.overrideDescriptor(this.wsProto, 'protocol', { - get: (target, that) => { - const event = new HookEvent( - { value: target.call(that) }, - target, - that - ); - this.emit('protocol', event); - return event.data.value; - }, - }); - } -} - -export default WebSocketApi; diff --git a/src/uv.handler.js b/src/uv.handler.js index fbbba47..5b2639a 100644 --- a/src/uv.handler.js +++ b/src/uv.handler.js @@ -1136,10 +1136,6 @@ function __uvHook(window, config = {}, bare = '/bare/') { this.#ready.then(() => this.#socket.close(code, reason)); } - static CONNECTING = WebSocket.CONNECTING; - static OPEN = WebSocket.OPEN; - static CLOSING = WebSocket.CLOSING; - static CLOSED = WebSocket.CLOSED; } eventTarget(MockWebSocket.prototype, 'close'); @@ -1147,23 +1143,14 @@ function __uvHook(window, config = {}, bare = '/bare/') { eventTarget(MockWebSocket.prototype, 'message'); eventTarget(MockWebSocket.prototype, 'error'); - client.websocket.on('websocket', (event) => { - event.respondWith( - new MockWebSocket(event.data.url, event.data.protocols) - ); - }); + client.override( + window, + 'WebSocket', + (target, that, args) => new MockWebSocket(...args), + true + ); - client.websocket.on('url', (event) => { - if ('__uv$url' in event.that) { - event.data.value = event.that.__uv$url; - } - }); - - client.websocket.on('protocol', (event) => { - if ('__uv$protocol' in event.that) { - event.data.value = event.that.__uv$protocol; - } - }); + MockWebSocket.prototype.constructor = window.WebSocket; client.function.on('function', (event) => { event.data.script = __uv.rewriteJS(event.data.script); @@ -1360,9 +1347,6 @@ function __uvHook(window, config = {}, bare = '/bare/') { client.history.overrideReplaceState(); client.eventSource.overrideConstruct(); client.eventSource.overrideUrl(); - client.websocket.overrideWebSocket(); - client.websocket.overrideProtocol(); - client.websocket.overrideUrl(); client.url.overrideObjectURL(); client.document.overrideCookie(); client.message.overridePostMessage();