1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 01:59:06 -05:00

feat: wss support with dialTLS (denoland/deno_std#615)

Original: cac2d5ee68
This commit is contained in:
Yusuke Sakurai 2019-09-29 01:46:21 +09:00 committed by Ryan Dahl
parent a472b6732d
commit 3cba0a4f4a
2 changed files with 22 additions and 10 deletions

View file

@ -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 {

View file

@ -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<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");
@ -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";