From 3cba0a4f4ac601a7039b0a1e40dd42385f4a3bfb Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Sun, 29 Sep 2019 01:46:21 +0900 Subject: [PATCH] feat: wss support with dialTLS (denoland/deno_std#615) Original: https://github.com/denoland/deno_std/commit/cac2d5ee68332956e59f548ff08f73b0fadf83d1 --- ws/mod.ts | 18 ++++++++++-------- ws/test.ts | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ws/mod.ts b/ws/mod.ts index 316e19abdf..2f85e58f8a 100644 --- a/ws/mod.ts +++ b/ws/mod.ts @@ -480,15 +480,17 @@ export async function connectWebSocket( headers: Headers = new Headers() ): Promise { 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 { diff --git a/ws/test.ts b/ws/test.ts index b8eb42803e..4351a391b0 100644 --- a/ws/test.ts +++ b/ws/test.ts @@ -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, @@ -46,7 +47,6 @@ test(async function wsReadMaskedTextFrame(): Promise { ) ); 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"); @@ -192,6 +192,16 @@ test(function wsAcceptableInvalid(): void { ); }); +test("connectWebSocket should throw invalid scheme of url", async (): Promise< + void +> => { + await assertThrowsAsync( + async (): Promise => { + await connectWebSocket("file://hoge/hoge"); + } + ); +}); + test(async function wsWriteReadMaskedFrame(): Promise { const mask = new Uint8Array([0, 1, 2, 3]); const msg = "hello";