diff options
| author | Dmitry Sharshakov <sh7dm@outlook.com> | 2019-03-05 02:23:04 +0300 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-03-04 18:23:04 -0500 |
| commit | 9f33cd28963a72d8fea0b1e99bb61ca9bec21a94 (patch) | |
| tree | 29b8b1bb8debadf3a92f900ea51d7979550697ac /ws/mod.ts | |
| parent | ce7a987009aed294d336dc420b72743ecd51db89 (diff) | |
Refactor WebSockets (denoland/deno_std#173)
* Use assert.equal instead of deprecated assertEqual
* Replace let with const where possible
* Add WebSocketMessage type
* Use OpCode in WebSocketFrame
* Use const where possible in WS
* Separate sha1 tests, use const instead of let
Original: https://github.com/denoland/deno_std/commit/385f866a545176261806d75184e2ef97fa0d5269
Diffstat (limited to 'ws/mod.ts')
| -rw-r--r-- | ws/mod.ts | 31 |
1 files changed, 21 insertions, 10 deletions
@@ -43,10 +43,16 @@ export function isWebSocketPongEvent(a): a is WebSocketPongEvent { return Array.isArray(a) && a[0] === "pong" && a[1] instanceof Uint8Array; } +export type WebSocketMessage = string | Uint8Array; + // TODO move this to common/util module export function append(a: Uint8Array, b: Uint8Array) { - if (a == null || !a.length) return b; - if (b == null || !b.length) return a; + if (a == null || !a.length) { + return b; + } + if (b == null || !b.length) { + return a; + } const output = new Uint8Array(a.length + b.length); output.set(a, 0); output.set(b, a.length); @@ -57,7 +63,7 @@ export class SocketClosedError extends Error {} export type WebSocketFrame = { isLastFrame: boolean; - opcode: number; + opcode: OpCode; mask?: Uint8Array; payload: Uint8Array; }; @@ -65,8 +71,8 @@ export type WebSocketFrame = { export type WebSocket = { readonly isClosed: boolean; receive(): AsyncIterableIterator<WebSocketEvent>; - send(data: string | Uint8Array): Promise<void>; - ping(data?: string | Uint8Array): Promise<void>; + send(data: WebSocketMessage): Promise<void>; + ping(data?: WebSocketMessage): Promise<void>; close(code: number, reason?: string): Promise<void>; }; @@ -118,11 +124,12 @@ class WebSocketImpl implements WebSocket { case OpCode.Pong: yield ["pong", frame.payload] as WebSocketPongEvent; break; + default: } } } - async send(data: string | Uint8Array): Promise<void> { + async send(data: WebSocketMessage): Promise<void> { if (this.isClosed) { throw new SocketClosedError("socket has been closed"); } @@ -141,7 +148,7 @@ class WebSocketImpl implements WebSocket { ); } - async ping(data: string | Uint8Array): Promise<void> { + async ping(data: WebSocketMessage): Promise<void> { const payload = typeof data === "string" ? this.encoder.encode(data) : data; await writeFrame( { @@ -188,7 +195,10 @@ class WebSocketImpl implements WebSocket { } private ensureSocketClosed(): Error { - if (this.isClosed) return; + if (this.isClosed) { + return; + } + try { this.conn.close(); } catch (e) { @@ -203,7 +213,7 @@ export async function* receiveFrame( conn: Conn ): AsyncIterableIterator<WebSocketFrame> { let receiving = true; - let isLastFrame = true; + const isLastFrame = true; const reader = new BufReader(conn); while (receiving) { const frame = await readFrame(reader); @@ -241,12 +251,13 @@ export async function* receiveFrame( case OpCode.Pong: yield frame; break; + default: } } } export async function writeFrame(frame: WebSocketFrame, writer: Writer) { - let payloadLength = frame.payload.byteLength; + const payloadLength = frame.payload.byteLength; let header: Uint8Array; const hasMask = frame.mask ? 0x80 : 0; if (payloadLength < 126) { |
