1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00

Move WebSocket op codes to enum (#171)

This commit is contained in:
Dmitry Sharshakov 2019-02-02 22:57:27 +03:00 committed by Ryan Dahl
parent 214fb8d2c6
commit 56579ed6ad
2 changed files with 34 additions and 41 deletions

View file

@ -5,12 +5,14 @@ import { BufReader, BufWriter } from "../io/bufio.ts";
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
import { Sha1 } from "./sha1.ts";
export const OpCodeContinue = 0x0;
export const OpCodeTextFrame = 0x1;
export const OpCodeBinaryFrame = 0x2;
export const OpCodeClose = 0x8;
export const OpcodePing = 0x9;
export const OpcodePong = 0xa;
export enum OpCode {
Continue = 0x0,
TextFrame = 0x1,
BinaryFrame = 0x2,
Close = 0x8,
Ping = 0x9,
Pong = 0xa
}
export type WebSocketEvent =
| string
@ -77,9 +79,9 @@ class WebSocketImpl implements WebSocket {
for await (const frame of receiveFrame(this.conn)) {
unmask(frame.payload, frame.mask);
switch (frame.opcode) {
case OpCodeTextFrame:
case OpCodeBinaryFrame:
case OpCodeContinue:
case OpCode.TextFrame:
case OpCode.BinaryFrame:
case OpCode.Continue:
frames.push(frame);
payloadsLength += frame.payload.length;
if (frame.isLastFrame) {
@ -89,7 +91,7 @@ class WebSocketImpl implements WebSocket {
concat.set(frame.payload, offs);
offs += frame.payload.length;
}
if (frames[0].opcode === OpCodeTextFrame) {
if (frames[0].opcode === OpCode.TextFrame) {
// text
yield new Buffer(concat).toString();
} else {
@ -100,7 +102,7 @@ class WebSocketImpl implements WebSocket {
payloadsLength = 0;
}
break;
case OpCodeClose:
case OpCode.Close:
const code = (frame.payload[0] << 16) | frame.payload[1];
const reason = new Buffer(
frame.payload.subarray(2, frame.payload.length)
@ -108,10 +110,10 @@ class WebSocketImpl implements WebSocket {
this._isClosed = true;
yield { code, reason };
return;
case OpcodePing:
case OpCode.Ping:
yield ["ping", frame.payload] as WebSocketPingEvent;
break;
case OpcodePong:
case OpCode.Pong:
yield ["pong", frame.payload] as WebSocketPongEvent;
break;
}
@ -123,7 +125,7 @@ class WebSocketImpl implements WebSocket {
throw new SocketClosedError("socket has been closed");
}
const opcode =
typeof data === "string" ? OpCodeTextFrame : OpCodeBinaryFrame;
typeof data === "string" ? OpCode.TextFrame : OpCode.BinaryFrame;
const payload = typeof data === "string" ? this.encoder.encode(data) : data;
const isLastFrame = true;
await writeFrame(
@ -142,7 +144,7 @@ class WebSocketImpl implements WebSocket {
await writeFrame(
{
isLastFrame: true,
opcode: OpCodeClose,
opcode: OpCode.Close,
mask: this.mask,
payload
},
@ -170,7 +172,7 @@ class WebSocketImpl implements WebSocket {
await writeFrame(
{
isLastFrame: true,
opcode: OpCodeClose,
opcode: OpCode.Close,
mask: this.mask,
payload
},
@ -205,12 +207,12 @@ export async function* receiveFrame(
const frame = await readFrame(reader);
const { opcode, payload } = frame;
switch (opcode) {
case OpCodeTextFrame:
case OpCodeBinaryFrame:
case OpCodeContinue:
case OpCode.TextFrame:
case OpCode.BinaryFrame:
case OpCode.Continue:
yield frame;
break;
case OpCodeClose:
case OpCode.Close:
await writeFrame(
{
opcode,
@ -223,18 +225,18 @@ export async function* receiveFrame(
yield frame;
receiving = false;
break;
case OpcodePing:
case OpCode.Ping:
await writeFrame(
{
payload,
isLastFrame,
opcode: OpcodePong
opcode: OpCode.Pong
},
conn
);
yield frame;
break;
case OpcodePong:
case OpCode.Pong:
yield frame;
break;
}

View file

@ -2,16 +2,7 @@
import { Buffer } from "deno";
import { BufReader } from "../io/bufio.ts";
import { test, assert, assertEqual } from "../testing/mod.ts";
import {
createSecAccept,
OpCodeBinaryFrame,
OpCodeContinue,
OpcodePing,
OpcodePong,
OpCodeTextFrame,
readFrame,
unmask
} from "./mod.ts";
import { createSecAccept, OpCode, readFrame, unmask } from "./mod.ts";
import { serve } from "../http/http.ts";
test(async function testReadUnmaskedTextFrame() {
@ -20,7 +11,7 @@ test(async function testReadUnmaskedTextFrame() {
new Buffer(new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
);
const frame = await readFrame(buf);
assertEqual(frame.opcode, OpCodeTextFrame);
assertEqual(frame.opcode, OpCode.TextFrame);
assertEqual(frame.mask, undefined);
assertEqual(new Buffer(frame.payload).toString(), "Hello");
assertEqual(frame.isLastFrame, true);
@ -47,7 +38,7 @@ test(async function testReadMakedTextFrame() {
);
const frame = await readFrame(buf);
console.dir(frame);
assertEqual(frame.opcode, OpCodeTextFrame);
assertEqual(frame.opcode, OpCode.TextFrame);
unmask(frame.payload, frame.mask);
assertEqual(new Buffer(frame.payload).toString(), "Hello");
assertEqual(frame.isLastFrame, true);
@ -63,12 +54,12 @@ test(async function testReadUnmaskedSplittedTextFrames() {
const [f1, f2] = await Promise.all([readFrame(buf1), readFrame(buf2)]);
assertEqual(f1.isLastFrame, false);
assertEqual(f1.mask, undefined);
assertEqual(f1.opcode, OpCodeTextFrame);
assertEqual(f1.opcode, OpCode.TextFrame);
assertEqual(new Buffer(f1.payload).toString(), "Hel");
assertEqual(f2.isLastFrame, true);
assertEqual(f2.mask, undefined);
assertEqual(f2.opcode, OpCodeContinue);
assertEqual(f2.opcode, OpCode.Continue);
assertEqual(new Buffer(f2.payload).toString(), "lo");
});
@ -78,7 +69,7 @@ test(async function testReadUnmaksedPingPongFrame() {
new Buffer(new Uint8Array([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
);
const ping = await readFrame(buf);
assertEqual(ping.opcode, OpcodePing);
assertEqual(ping.opcode, OpCode.Ping);
assertEqual(new Buffer(ping.payload).toString(), "Hello");
const buf2 = new BufReader(
@ -99,7 +90,7 @@ test(async function testReadUnmaksedPingPongFrame() {
)
);
const pong = await readFrame(buf2);
assertEqual(pong.opcode, OpcodePong);
assertEqual(pong.opcode, OpCode.Pong);
assert(pong.mask !== undefined);
unmask(pong.payload, pong.mask);
assertEqual(new Buffer(pong.payload).toString(), "Hello");
@ -112,7 +103,7 @@ test(async function testReadUnmaksedBigBinaryFrame() {
}
const buf = new BufReader(new Buffer(new Uint8Array(a)));
const bin = await readFrame(buf);
assertEqual(bin.opcode, OpCodeBinaryFrame);
assertEqual(bin.opcode, OpCode.BinaryFrame);
assertEqual(bin.isLastFrame, true);
assertEqual(bin.mask, undefined);
assertEqual(bin.payload.length, 256);
@ -125,7 +116,7 @@ test(async function testReadUnmaskedBigBigBinaryFrame() {
}
const buf = new BufReader(new Buffer(new Uint8Array(a)));
const bin = await readFrame(buf);
assertEqual(bin.opcode, OpCodeBinaryFrame);
assertEqual(bin.opcode, OpCode.BinaryFrame);
assertEqual(bin.isLastFrame, true);
assertEqual(bin.mask, undefined);
assertEqual(bin.payload.length, 0xffff + 1);