1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/std/ws
Bartek Iwańczuk 6e2df8c64f
feat: Deno.test() sanitizes ops and resources (#4399)
This PR brings assertOps and assertResources sanitizers to Deno.test() API.

assertOps checks that test doesn't leak async ops, ie. there are no unresolved
promises originating from Deno APIs. Enabled by default, can be disabled using 
Deno.TestDefinition.disableOpSanitizer.

assertResources checks that test doesn't leak resources, ie. all resources used
in test are closed. For example; if a file is opened during a test case it must be
explicitly closed before test case finishes. It's most useful for asynchronous
generators. Enabled by default, can be disabled using 
Deno.TestDefinition.disableResourceSanitizer.

We've used those sanitizers in internal runtime tests and it proved very useful in
surfacing incorrect tests which resulted in interference between the tests.

All tests have been sanitized.

Closes #4208
2020-03-18 19:25:55 -04:00
..
example_client.ts /std/ws: Fix args index for WS examples (#4046) 2020-02-20 10:19:14 -05:00
example_server.ts /std/ws: Fix args index for WS examples (#4046) 2020-02-20 10:19:14 -05:00
mod.ts fix(std): Use Deno.errors where possible. (#4356) 2020-03-13 21:40:13 -04:00
README.md /std/ws: Fix args index for WS examples (#4046) 2020-02-20 10:19:14 -05:00
sha1.ts Move everything into std subdir 2019-10-09 17:10:09 -04:00
sha1_test.ts [std/ws] provide default close code for ws.close() (#4172) 2020-02-28 11:17:00 -05:00
test.ts feat: Deno.test() sanitizes ops and resources (#4399) 2020-03-18 19:25:55 -04:00

ws

ws module is made to provide helpers to create WebSocket client/server.

Usage

Server

import { serve } from "https://deno.land/std/http/server.ts";
import {
  acceptWebSocket,
  isWebSocketCloseEvent,
  isWebSocketPingEvent,
  WebSocket
} from "https://deno.land/std/ws/mod.ts";

/** websocket echo server */
const port = Deno.args[0] || "8080";
console.log(`websocket server is running on :${port}`);
for await (const req of serve(`:${port}`)) {
  const { headers, conn } = req;
  acceptWebSocket({
    conn,
    headers,
    bufReader: req.r,
    bufWriter: req.w
  })
    .then(
      async (sock: WebSocket): Promise<void> => {
        console.log("socket connected!");
        const it = sock.receive();
        while (true) {
          try {
            const { done, value } = await it.next();
            if (done) {
              break;
            }
            const ev = value;
            if (typeof ev === "string") {
              // text message
              console.log("ws:Text", ev);
              await sock.send(ev);
            } else if (ev instanceof Uint8Array) {
              // binary message
              console.log("ws:Binary", ev);
            } else if (isWebSocketPingEvent(ev)) {
              const [, body] = ev;
              // ping
              console.log("ws:Ping", body);
            } else if (isWebSocketCloseEvent(ev)) {
              // close
              const { code, reason } = ev;
              console.log("ws:Close", code, reason);
            }
          } catch (e) {
            console.error(`failed to receive frame: ${e}`);
            await sock.close(1000).catch(console.error);
          }
        }
      }
    )
    .catch((err: Error): void => {
      console.error(`failed to accept websocket: ${err}`);
    });
}

Client

import {
  connectWebSocket,
  isWebSocketCloseEvent,
  isWebSocketPingEvent,
  isWebSocketPongEvent
} from "https://deno.land/std/ws/mod.ts";
import { encode } from "https://deno.land/std/strings/mod.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 */
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));
(async function(): Promise<void> {
  for await (const msg of sock.receive()) {
    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 tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
  await Deno.stdout.write(encode("> "));
  const line = await tpr.readLine();
  if (line === Deno.EOF) {
    break;
  }
  if (line === "close") {
    break;
  } else if (line === "ping") {
    await sock.ping();
  } else {
    await sock.send(line);
  }
  // FIXME: Without this,
  // sock.receive() won't resolved though it is readable...
  await new Promise((resolve): void => {
    setTimeout(resolve, 0);
  });
}
await sock.close(1000);
// FIXME: conn.close() won't shutdown process...
Deno.exit(0);

API

isWebSocketCloseEvent

Returns true if input value is a WebSocketCloseEvent, false otherwise.

isWebSocketPingEvent

Returns true if input value is a WebSocketPingEvent, false otherwise.

isWebSocketPongEvent

Returns true if input value is a WebSocketPongEvent, false otherwise.

append

This module is used to merge two Uint8Arrays.

  • note: This module might move to common/util.
import { append } from "https://deno.land/std/ws/mod.ts";

// a = [1], b = [2]
append(a, b); // output: [1, 2]

// a = [1], b = null
append(a, b); // output: [1]

// a = [], b = [2]
append(a, b); // output: [2]

unmask

Unmask masked WebSocket payload.

writeFrame

Write WebSocket frame to inputted writer.

readFrame

Read WebSocket frame from inputted BufReader.

createMask

Create mask from the client to the server with random 32bit number.

acceptable

Returns true if input headers are usable for WebSocket, otherwise false

createSecAccept

Create value of Sec-WebSocket-Accept header from inputted nonce.

acceptWebSocket

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.