Skip to content

Commit

Permalink
chore: remove ws connect method (denoland/deno#7403)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored and caspervonb committed Jan 31, 2021
1 parent c1466d7 commit 07f6297
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 194 deletions.
24 changes: 15 additions & 9 deletions examples/chat/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { assert, assertEquals } from "../../testing/asserts.ts";
import { TextProtoReader } from "../../textproto/mod.ts";
import { BufReader } from "../../io/bufio.ts";
import { connectWebSocket, WebSocket } from "../../ws/mod.ts";
import { delay } from "../../async/delay.ts";
import { resolve, dirname, fromFileUrl } from "../../path/mod.ts";

Expand Down Expand Up @@ -57,18 +56,25 @@ Deno.test({
name: "[examples/chat] GET /ws should upgrade conn to ws",
async fn() {
const server = await startServer();
let ws: WebSocket | undefined;
let ws: WebSocket;
try {
ws = await connectWebSocket("http://127.0.0.1:8080/ws");
const it = ws[Symbol.asyncIterator]();

assertEquals((await it.next()).value, "Connected: [1]");
ws.send("Hello");
assertEquals((await it.next()).value, "[1]: Hello");
ws = new WebSocket("ws://127.0.0.1:8080/ws");
await new Promise((resolve) => {
ws.onmessage = ((message) => {
assertEquals(message.data, "Connected: [1]");
ws.onmessage = ((message) => {
assertEquals(message.data, "[1]: Hello");
ws.close();
resolve();
});
ws.send("Hello");
});
});
} catch (err) {
console.log(err);
} finally {
server.close();
server.stdout.close();
ws!.conn.close();
}
},
});
73 changes: 3 additions & 70 deletions ws/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ws

ws module is made to provide helpers to create WebSocket client/server.
ws module is made to provide helpers to create WebSocket server. For client
WebSockets, use the
[WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API).

## Usage

### Server

```ts
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { serve } from "https://deno.land/std/http/server.ts";
Expand Down Expand Up @@ -67,67 +67,6 @@ if (import.meta.main) {
}
```

### Client

```ts
import {
connectWebSocket,
isWebSocketCloseEvent,
isWebSocketPingEvent,
isWebSocketPongEvent,
} from "https://deno.land/std/ws/mod.ts";
import { encode } from "https://deno.land/std/encoding/utf8.ts";
import { BufReader } from "https://deno.land/std/io/bufio.ts";
import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts";
import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts";

const endpoint = Deno.args[0] || "ws://127.0.0.1:8080";
/** simple websocket cli */
try {
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));

const messages = async (): Promise<void> => {
for await (const msg of sock) {
if (typeof msg === "string") {
console.log(yellow(`< ${msg}`));
} else if (isWebSocketPingEvent(msg)) {
console.log(blue("< ping"));
} else if (isWebSocketPongEvent(msg)) {
console.log(blue("< pong"));
} else if (isWebSocketCloseEvent(msg)) {
console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`));
}
}
};

const cli = async (): Promise<void> => {
const tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
await Deno.stdout.write(encode("> "));
const line = await tpr.readLine();
if (line === null || line === "close") {
break;
} else if (line === "ping") {
await sock.ping();
} else {
await sock.send(line);
}
}
};

await Promise.race([messages(), cli()]).catch(console.error);

if (!sock.isClosed) {
await sock.close(1000).catch(console.error);
}
} catch (err) {
console.error(red(`Could not connect to WebSocket: '${err}'`));
}

Deno.exit(0);
```

## API

### isWebSocketCloseEvent
Expand Down Expand Up @@ -173,9 +112,3 @@ Upgrade inputted TCP connection into WebSocket connection.
### createSecKey

Returns base64 encoded 16 bytes string for Sec-WebSocket-Key header.

### connectWebSocket

Connect to WebSocket endpoint url with inputted endpoint string and headers.

- note: Endpoint must be acceptable for URL.
61 changes: 0 additions & 61 deletions ws/example_client.ts

This file was deleted.

1 change: 0 additions & 1 deletion ws/example_test.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import "./example_client.ts";
import "./example_server.ts";
41 changes: 0 additions & 41 deletions ws/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ export async function readFrame(buf: BufReader): Promise<WebSocketFrame> {
};
}

// Create client-to-server mask, random 32bit number
function createMask(): Uint8Array {
return crypto.getRandomValues(new Uint8Array(4));
}

class WebSocketImpl implements WebSocket {
readonly conn: Deno.Conn;
private readonly mask?: Uint8Array;
Expand Down Expand Up @@ -514,42 +509,6 @@ export async function handshake(
}
}

/**
* Connect to given websocket endpoint url.
* Endpoint must be acceptable for URL.
*/
export async function connectWebSocket(
endpoint: string,
headers: Headers = new Headers(),
): Promise<WebSocket> {
const url = new URL(endpoint);
const { hostname } = url;
let conn: Deno.Conn;
if (url.protocol === "http:" || url.protocol === "ws:") {
const port = parseInt(url.port || "80");
conn = await Deno.connect({ hostname, port });
} else if (url.protocol === "https:" || url.protocol === "wss:") {
const port = parseInt(url.port || "443");
conn = await Deno.connectTls({ hostname, port });
} else {
throw new Error("ws: unsupported protocol: " + url.protocol);
}
const bufWriter = new BufWriter(conn);
const bufReader = new BufReader(conn);
try {
await handshake(url, headers, bufReader, bufWriter);
} catch (err) {
conn.close();
throw err;
}
return new WebSocketImpl({
conn,
bufWriter,
bufReader,
mask: createMask(),
});
}

export function createWebSocket(params: {
conn: Deno.Conn;
bufWriter?: BufWriter;
Expand Down
12 changes: 0 additions & 12 deletions ws/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TextProtoReader } from "../textproto/mod.ts";
import * as bytes from "../bytes/mod.ts";
import {
acceptable,
connectWebSocket,
createSecAccept,
createSecKey,
handshake,
Expand Down Expand Up @@ -194,17 +193,6 @@ Deno.test("[ws] acceptable should return false when headers invalid", () => {
);
});

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

Deno.test("[ws] write and read masked frame", async () => {
const mask = new Uint8Array([0, 1, 2, 3]);
const msg = "hello";
Expand Down

0 comments on commit 07f6297

Please sign in to comment.