Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [ws] wss support with dialTLS #615

Merged
merged 2 commits into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions ws/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,17 @@ export async function connectWebSocket(
headers: Headers = new Headers()
): Promise<WebSocket> {
const url = new URL(endpoint);
let { hostname, port } = url;
if (!port) {
if (url.protocol === "http" || url.protocol === "ws") {
port = "80";
} else if (url.protocol === "https" || url.protocol === "wss") {
throw new Error("currently https/wss is not supported");
}
let { hostname } = url;
let conn: Conn;
if (url.protocol === "http:" || url.protocol === "ws:") {
const port = parseInt(url.port || "80");
conn = await Deno.dial({ hostname, port });
} else if (url.protocol === "https:" || url.protocol === "wss:") {
const port = parseInt(url.port || "443");
conn = await Deno.dialTLS({ hostname, port });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we should make it so dialTLS also accepts port as a string...

} else {
throw new Error("ws: unsupported protocol: " + url.protocol);
}
const conn = await Deno.dial({ hostname, port: Number(port) });
const bufWriter = new BufWriter(conn);
const bufReader = new BufReader(conn);
try {
Expand Down
14 changes: 12 additions & 2 deletions ws/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { BufReader } from "../io/bufio.ts";
import { assert, assertEquals } from "../testing/asserts.ts";
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
import { runIfMain, test } from "../testing/mod.ts";
import {
acceptable,
connectWebSocket,
createSecAccept,
OpCode,
readFrame,
Expand Down Expand Up @@ -46,7 +47,6 @@ test(async function wsReadMaskedTextFrame(): Promise<void> {
)
);
const frame = await readFrame(buf);
console.dir(frame);
assertEquals(frame.opcode, OpCode.TextFrame);
unmask(frame.payload, frame.mask);
assertEquals(new Buffer(frame.payload).toString(), "Hello");
Expand Down Expand Up @@ -192,6 +192,16 @@ test(function wsAcceptableInvalid(): void {
);
});

test("connectWebSocket should throw invalid scheme of url", async (): Promise<
void
> => {
await assertThrowsAsync(
async (): Promise<void> => {
await connectWebSocket("file://hoge/hoge");
}
);
});

test(async function wsWriteReadMaskedFrame(): Promise<void> {
const mask = new Uint8Array([0, 1, 2, 3]);
const msg = "hello";
Expand Down