fix(ext/websocket): WebSocket dispatch single close event (#13443)

This commit is contained in:
Leo Kettmeir 2022-05-23 13:21:11 +02:00 committed by GitHub
parent d55444b41c
commit 3c97bbe165
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 39 deletions

View file

@ -29,6 +29,8 @@
StringPrototypeToLowerCase,
Symbol,
SymbolIterator,
PromisePrototypeCatch,
SymbolFor,
} = window.__bootstrap.primordials;
webidl.converters["sequence<DOMString> or DOMString"] = (V, opts) => {
@ -367,16 +369,19 @@
} else if (this[_readyState] === OPEN) {
this[_readyState] = CLOSING;
PromisePrototypeThen(
PromisePrototypeCatch(
core.opAsync("op_ws_close", this[_rid], code, reason),
() => {
(err) => {
this[_readyState] = CLOSED;
const event = new CloseEvent("close", {
wasClean: true,
code: code ?? 1005,
reason,
const errorEv = new ErrorEvent("error", {
error: err,
message: ErrorPrototypeToString(err),
});
this.dispatchEvent(event);
this.dispatchEvent(errorEv);
const closeEv = new CloseEvent("close");
this.dispatchEvent(closeEv);
core.tryClose(this[_rid]);
},
);
@ -384,7 +389,7 @@
}
async [_eventLoop]() {
while (this[_readyState] === OPEN) {
while (this[_readyState] !== CLOSED) {
const { kind, value } = await core.opAsync(
"op_ws_next_event",
this[_rid],
@ -429,9 +434,23 @@
}
case "closed":
case "close": {
const prevState = this[_readyState];
this[_readyState] = CLOSED;
clearTimeout(this[_idleTimeoutTimeout]);
if (prevState === OPEN) {
try {
await core.opAsync(
"op_ws_close",
this[_rid],
value.code,
value.reason,
);
} catch {
// ignore failures
}
}
const event = new CloseEvent("close", {
wasClean: true,
code: value.code,
@ -487,6 +506,19 @@
}, (this[_idleTimeoutDuration] / 2) * 1000);
}
}
[SymbolFor("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${
inspect({
url: this.url,
readyState: this.readyState,
extensions: this.extensions,
protocol: this.protocol,
binaryType: this.binaryType,
bufferedAmount: this.bufferedAmount,
})
}`;
}
}
ObjectDefineProperties(WebSocket, {