cleanup websocket code

There should be less obvious leaks
WebSocket methods aren't hidden
This commit is contained in:
David Reed 2022-11-19 17:35:09 -05:00
parent ca284dad3b
commit 32439bff19
No known key found for this signature in database
GPG key ID: 2211691D8A1EE72F
3 changed files with 10 additions and 103 deletions

View file

@ -5,7 +5,6 @@ import AttrApi from './dom/attr.js';
import FunctionHook from './native/function.js'; import FunctionHook from './native/function.js';
import ObjectHook from './native/object.js'; import ObjectHook from './native/object.js';
import Fetch from './requests/fetch.js'; import Fetch from './requests/fetch.js';
import WebSocketApi from './requests/websocket.js';
import Xhr from './requests/xhr.js'; import Xhr from './requests/xhr.js';
import EventSourceApi from './requests/eventsource.js'; import EventSourceApi from './requests/eventsource.js';
import History from './history.js'; import History from './history.js';
@ -21,6 +20,9 @@ import StyleApi from './dom/style.js';
class UVClient extends EventEmitter { class UVClient extends EventEmitter {
constructor(window = self, worker = !window.window) { constructor(window = self, worker = !window.window) {
super(); super();
/**
* @type {typeof self}
*/
this.window = window; this.window = window;
this.nativeMethods = { this.nativeMethods = {
fnToString: this.window.Function.prototype.toString, fnToString: this.window.Function.prototype.toString,
@ -48,7 +50,6 @@ class UVClient extends EventEmitter {
this.function = new FunctionHook(this); this.function = new FunctionHook(this);
this.object = new ObjectHook(this); this.object = new ObjectHook(this);
this.message = new MessageApi(this); this.message = new MessageApi(this);
this.websocket = new WebSocketApi(this);
this.navigator = new NavigatorApi(this); this.navigator = new NavigatorApi(this);
this.eventSource = new EventSourceApi(this); this.eventSource = new EventSourceApi(this);
this.attribute = new AttrApi(this); this.attribute = new AttrApi(this);

View file

@ -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;

View file

@ -1136,10 +1136,6 @@ function __uvHook(window, config = {}, bare = '/bare/') {
this.#ready.then(() => this.#socket.close(code, reason)); 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'); eventTarget(MockWebSocket.prototype, 'close');
@ -1147,23 +1143,14 @@ function __uvHook(window, config = {}, bare = '/bare/') {
eventTarget(MockWebSocket.prototype, 'message'); eventTarget(MockWebSocket.prototype, 'message');
eventTarget(MockWebSocket.prototype, 'error'); eventTarget(MockWebSocket.prototype, 'error');
client.websocket.on('websocket', (event) => { client.override(
event.respondWith( window,
new MockWebSocket(event.data.url, event.data.protocols) 'WebSocket',
(target, that, args) => new MockWebSocket(...args),
true
); );
});
client.websocket.on('url', (event) => { MockWebSocket.prototype.constructor = window.WebSocket;
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;
}
});
client.function.on('function', (event) => { client.function.on('function', (event) => {
event.data.script = __uv.rewriteJS(event.data.script); event.data.script = __uv.rewriteJS(event.data.script);
@ -1360,9 +1347,6 @@ function __uvHook(window, config = {}, bare = '/bare/') {
client.history.overrideReplaceState(); client.history.overrideReplaceState();
client.eventSource.overrideConstruct(); client.eventSource.overrideConstruct();
client.eventSource.overrideUrl(); client.eventSource.overrideUrl();
client.websocket.overrideWebSocket();
client.websocket.overrideProtocol();
client.websocket.overrideUrl();
client.url.overrideObjectURL(); client.url.overrideObjectURL();
client.document.overrideCookie(); client.document.overrideCookie();
client.message.overridePostMessage(); client.message.overridePostMessage();