Skip to content

Commit

Permalink
feat: wss support with dialTLS (denoland/std#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored and ry committed Sep 28, 2019
1 parent a472b67 commit 3cba0a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
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 });
} 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

0 comments on commit 3cba0a4

Please sign in to comment.