mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 04:51:22 +00:00
feat(WebSocketStream): rename connection to opened (#20878)
This commit is contained in:
parent
2215a3ea2e
commit
8ba1242a05
@ -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
cli/tests/testdata/run/websocketstream_test.ts
vendored
30
cli/tests/testdata/run/websocketstream_test.ts
vendored
@ -24,14 +24,14 @@ 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;
|
||||
@ -39,7 +39,7 @@ Deno.test("connect & close custom invalid reason", async () => {
|
||||
|
||||
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");
|
||||
@ -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");
|
||||
@ -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",
|
||||
),
|
||||
@ -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();
|
||||
@ -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");
|
||||
@ -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(
|
||||
@ -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"),
|
||||
);
|
||||
@ -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();
|
||||
@ -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();
|
||||
@ -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");
|
||||
@ -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");
|
||||
@ -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");
|
||||
@ -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");
|
||||
|
2
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
2
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
@ -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]]");
|
||||
@ -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 = () => {
|
||||
@ -192,7 +192,7 @@ class WebSocketStream {
|
||||
"Closed while connecting",
|
||||
"NetworkError",
|
||||
);
|
||||
this[_connection].reject(err);
|
||||
this[_opened].reject(err);
|
||||
this[_closed].reject(err);
|
||||
},
|
||||
);
|
||||
@ -202,7 +202,7 @@ class WebSocketStream {
|
||||
"Closed while connecting",
|
||||
"NetworkError",
|
||||
);
|
||||
this[_connection].reject(err);
|
||||
this[_opened].reject(err);
|
||||
this[_closed].reject(err);
|
||||
},
|
||||
);
|
||||
@ -334,7 +334,7 @@ class WebSocketStream {
|
||||
},
|
||||
});
|
||||
|
||||
this[_connection].resolve({
|
||||
this[_opened].resolve({
|
||||
readable,
|
||||
writable,
|
||||
extensions: create.extensions ?? "",
|
||||
@ -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;
|
||||
@ -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(
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user