Skip to content

Commit

Permalink
feat(WebSocketStream): rename connection to opened (#20878)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored and bartlomieju committed Oct 12, 2023
1 parent a0b97df commit 932134d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/websocketstream_ping_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const wss = new WebSocketStream("ws://127.0.0.1:4513");
const { readable } = await wss.connection;
const { readable } = await wss.opened;
for await (const _ of readable) {
//
}
30 changes: 15 additions & 15 deletions cli/tests/testdata/run/websocketstream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ Deno.test("duplicate protocols", () => {

Deno.test("connect & close custom valid code", async () => {
const ws = new WebSocketStream("ws://localhost:4242");
await ws.connection;
await ws.opened;
ws.close({ code: 1000 });
await ws.closed;
});

Deno.test("connect & close custom invalid reason", async () => {
const ws = new WebSocketStream("ws://localhost:4242");
await ws.connection;
await ws.opened;
assertThrows(() => ws.close({ code: 1000, reason: "".padEnd(124, "o") }));
ws.close();
await ws.closed;
});

Deno.test("echo string", async () => {
const ws = new WebSocketStream("ws://localhost:4242");
const { readable, writable } = await ws.connection;
const { readable, writable } = await ws.opened;
await writable.getWriter().write("foo");
const res = await readable.getReader().read();
assertEquals(res.value, "foo");
Expand All @@ -49,7 +49,7 @@ Deno.test("echo string", async () => {

Deno.test("echo string tls", async () => {
const ws = new WebSocketStream("wss://localhost:4243");
const { readable, writable } = await ws.connection;
const { readable, writable } = await ws.opened;
await writable.getWriter().write("foo");
const res = await readable.getReader().read();
assertEquals(res.value, "foo");
Expand All @@ -61,7 +61,7 @@ Deno.test("websocket error", async () => {
const ws = new WebSocketStream("wss://localhost:4242");
await Promise.all([
assertRejects(
() => ws.connection,
() => ws.opened,
Deno.errors.UnexpectedEof,
"tls handshake eof",
),
Expand All @@ -75,7 +75,7 @@ Deno.test("websocket error", async () => {

Deno.test("echo uint8array", async () => {
const ws = new WebSocketStream("ws://localhost:4242");
const { readable, writable } = await ws.connection;
const { readable, writable } = await ws.opened;
const uint = new Uint8Array([102, 111, 111]);
await writable.getWriter().write(uint);
const res = await readable.getReader().read();
Expand All @@ -91,7 +91,7 @@ Deno.test("aborting immediately throws an AbortError", async () => {
});
controller.abort();
await assertRejects(
() => wss.connection,
() => wss.opened,
(error: Error) => {
assert(error instanceof DOMException);
assertEquals(error.name, "AbortError");
Expand All @@ -114,7 +114,7 @@ Deno.test("aborting immediately with a reason throws that reason", async () => {
const abortReason = new Error();
controller.abort(abortReason);
await assertRejects(
() => wss.connection,
() => wss.opened,
(error: Error) => assertEquals(error, abortReason),
);
await assertRejects(
Expand All @@ -129,7 +129,7 @@ Deno.test("aborting immediately with a primitive as reason throws that primitive
signal: controller.signal,
});
controller.abort("Some string");
await wss.connection.then(
await wss.opened.then(
() => unreachable(),
(e) => assertEquals(e, "Some string"),
);
Expand Down Expand Up @@ -159,7 +159,7 @@ Deno.test("headers", async () => {
const ws = new WebSocketStream("ws://localhost:4512", {
headers: [["x-some-header", "foo"]],
});
await ws.connection;
await ws.opened;
await promise;
await ws.closed;
listener.close();
Expand Down Expand Up @@ -196,7 +196,7 @@ Deno.test("forbidden headers", async () => {
const ws = new WebSocketStream("ws://localhost:4512", {
headers: forbiddenHeaders.map((header) => [header, "foo"]),
});
await ws.connection;
await ws.opened;
await promise;
await ws.closed;
listener.close();
Expand All @@ -221,7 +221,7 @@ Deno.test("sync close with empty stream", async () => {
})();

const ws = new WebSocketStream("ws://localhost:4512");
const { readable } = await ws.connection;
const { readable } = await ws.opened;
const reader = readable.getReader();
const firstMessage = await reader.read();
assertEquals(firstMessage.value, "first message");
Expand Down Expand Up @@ -254,7 +254,7 @@ Deno.test("sync close with unread messages in stream", async () => {
})();

const ws = new WebSocketStream("ws://localhost:4512");
const { readable } = await ws.connection;
const { readable } = await ws.opened;
const reader = readable.getReader();
const firstMessage = await reader.read();
assertEquals(firstMessage.value, "first message");
Expand Down Expand Up @@ -285,7 +285,7 @@ Deno.test("async close with empty stream", async () => {
})();

const ws = new WebSocketStream("ws://localhost:4512");
const { readable } = await ws.connection;
const { readable } = await ws.opened;
const reader = readable.getReader();
const firstMessage = await reader.read();
assertEquals(firstMessage.value, "first message");
Expand Down Expand Up @@ -320,7 +320,7 @@ Deno.test("async close with unread messages in stream", async () => {
})();

const ws = new WebSocketStream("ws://localhost:4512");
const { readable } = await ws.connection;
const { readable } = await ws.opened;
const reader = readable.getReader();
const firstMessage = await reader.read();
assertEquals(firstMessage.value, "first message");
Expand Down
2 changes: 1 addition & 1 deletion cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ declare interface WebSocketCloseInfo {
*/
declare interface WebSocketStream {
url: string;
connection: Promise<WebSocketConnection>;
opened: Promise<WebSocketConnection>;
closed: Promise<WebSocketCloseInfo>;
close(closeInfo?: WebSocketCloseInfo): void;
}
Expand Down
20 changes: 10 additions & 10 deletions ext/websocket/02_websocketstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const CLOSE_RESPONSE_TIMEOUT = 5000;

const _rid = Symbol("[[rid]]");
const _url = Symbol("[[url]]");
const _connection = Symbol("[[connection]]");
const _opened = Symbol("[[opened]]");
const _closed = Symbol("[[closed]]");
const _earlyClose = Symbol("[[earlyClose]]");
const _closeSent = Symbol("[[closeSent]]");
Expand Down Expand Up @@ -155,7 +155,7 @@ class WebSocketStream {
if (options.signal?.aborted) {
core.close(cancelRid);
const err = options.signal.reason;
this[_connection].reject(err);
this[_opened].reject(err);
this[_closed].reject(err);
} else {
const abort = () => {
Expand Down Expand Up @@ -192,7 +192,7 @@ class WebSocketStream {
"Closed while connecting",
"NetworkError",
);
this[_connection].reject(err);
this[_opened].reject(err);
this[_closed].reject(err);
},
);
Expand All @@ -202,7 +202,7 @@ class WebSocketStream {
"Closed while connecting",
"NetworkError",
);
this[_connection].reject(err);
this[_opened].reject(err);
this[_closed].reject(err);
},
);
Expand Down Expand Up @@ -334,7 +334,7 @@ class WebSocketStream {
},
});

this[_connection].resolve({
this[_opened].resolve({
readable,
writable,
extensions: create.extensions ?? "",
Expand All @@ -349,17 +349,17 @@ class WebSocketStream {
} else {
core.tryClose(cancelRid);
}
this[_connection].reject(err);
this[_opened].reject(err);
this[_closed].reject(err);
},
);
}
}

[_connection] = new Deferred();
get connection() {
[_opened] = new Deferred();
get opened() {
webidl.assertBranded(this, WebSocketStreamPrototype);
return this[_connection].promise;
return this[_opened].promise;
}

[_earlyClose] = false;
Expand Down Expand Up @@ -405,7 +405,7 @@ class WebSocketStream {
code = 1000;
}

if (this[_connection].state === "pending") {
if (this[_opened].state === "pending") {
this[_earlyClose] = true;
} else if (this[_closed].state === "pending") {
PromisePrototypeThen(
Expand Down
48 changes: 18 additions & 30 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -8180,42 +8180,30 @@
"send-many-64K-messages-with-backpressure.any.worker.html?wss": true,
"stream": {
"tentative": {
"abort.any.html?wpt_flags=h2": false,
"abort.any.html?wss": false,
"abort.any.worker.html?wpt_flags=h2": false,
"abort.any.worker.html?wss": false,
"abort.any.html?wpt_flags=h2": [
"abort after connect should do nothing"
],
"abort.any.html?wss": true,
"abort.any.worker.html?wpt_flags=h2": [
"abort after connect should do nothing"
],
"abort.any.worker.html?wss": true,
"backpressure-receive.any.html?wpt_flags=h2": false,
"backpressure-receive.any.html?wss": false,
"backpressure-receive.any.html?wss": true,
"backpressure-receive.any.worker.html?wpt_flags=h2": false,
"backpressure-receive.any.worker.html?wss": false,
"backpressure-receive.any.worker.html?wss": true,
"backpressure-send.any.html?wpt_flags=h2": false,
"backpressure-send.any.html?wss": false,
"backpressure-send.any.html?wss": true,
"backpressure-send.any.worker.html?wpt_flags=h2": false,
"backpressure-send.any.worker.html?wss": false,
"backpressure-send.any.worker.html?wss": true,
"close.any.html?wpt_flags=h2": false,
"close.any.html?wss": false,
"close.any.html?wss": true,
"close.any.worker.html?wpt_flags=h2": false,
"close.any.worker.html?wss": false,
"constructor.any.html?wpt_flags=h2": [
"setting a protocol in the constructor should work",
"connection failure should reject the promises",
"wss.opened should resolve to the right types"
],
"constructor.any.html?wss": [
"setting a protocol in the constructor should work",
"connection failure should reject the promises",
"wss.opened should resolve to the right types"
],
"constructor.any.worker.html?wpt_flags=h2": [
"setting a protocol in the constructor should work",
"connection failure should reject the promises",
"wss.opened should resolve to the right types"
],
"constructor.any.worker.html?wss": [
"setting a protocol in the constructor should work",
"connection failure should reject the promises",
"wss.opened should resolve to the right types"
]
"close.any.worker.html?wss": true,
"constructor.any.html?wpt_flags=h2": false,
"constructor.any.html?wss": true,
"constructor.any.worker.html?wpt_flags=h2": false,
"constructor.any.worker.html?wss": true
}
}
},
Expand Down

0 comments on commit 932134d

Please sign in to comment.