Skip to content

Commit 56579ed

Browse files
Dmitry Sharshakovry
Dmitry Sharshakov
authored andcommitted
Move WebSocket op codes to enum (denoland#171)
1 parent 214fb8d commit 56579ed

File tree

2 files changed

+34
-41
lines changed

2 files changed

+34
-41
lines changed

ws/mod.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { BufReader, BufWriter } from "../io/bufio.ts";
55
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
66
import { Sha1 } from "./sha1.ts";
77

8-
export const OpCodeContinue = 0x0;
9-
export const OpCodeTextFrame = 0x1;
10-
export const OpCodeBinaryFrame = 0x2;
11-
export const OpCodeClose = 0x8;
12-
export const OpcodePing = 0x9;
13-
export const OpcodePong = 0xa;
8+
export enum OpCode {
9+
Continue = 0x0,
10+
TextFrame = 0x1,
11+
BinaryFrame = 0x2,
12+
Close = 0x8,
13+
Ping = 0x9,
14+
Pong = 0xa
15+
}
1416

1517
export type WebSocketEvent =
1618
| string
@@ -77,9 +79,9 @@ class WebSocketImpl implements WebSocket {
7779
for await (const frame of receiveFrame(this.conn)) {
7880
unmask(frame.payload, frame.mask);
7981
switch (frame.opcode) {
80-
case OpCodeTextFrame:
81-
case OpCodeBinaryFrame:
82-
case OpCodeContinue:
82+
case OpCode.TextFrame:
83+
case OpCode.BinaryFrame:
84+
case OpCode.Continue:
8385
frames.push(frame);
8486
payloadsLength += frame.payload.length;
8587
if (frame.isLastFrame) {
@@ -89,7 +91,7 @@ class WebSocketImpl implements WebSocket {
8991
concat.set(frame.payload, offs);
9092
offs += frame.payload.length;
9193
}
92-
if (frames[0].opcode === OpCodeTextFrame) {
94+
if (frames[0].opcode === OpCode.TextFrame) {
9395
// text
9496
yield new Buffer(concat).toString();
9597
} else {
@@ -100,18 +102,18 @@ class WebSocketImpl implements WebSocket {
100102
payloadsLength = 0;
101103
}
102104
break;
103-
case OpCodeClose:
105+
case OpCode.Close:
104106
const code = (frame.payload[0] << 16) | frame.payload[1];
105107
const reason = new Buffer(
106108
frame.payload.subarray(2, frame.payload.length)
107109
).toString();
108110
this._isClosed = true;
109111
yield { code, reason };
110112
return;
111-
case OpcodePing:
113+
case OpCode.Ping:
112114
yield ["ping", frame.payload] as WebSocketPingEvent;
113115
break;
114-
case OpcodePong:
116+
case OpCode.Pong:
115117
yield ["pong", frame.payload] as WebSocketPongEvent;
116118
break;
117119
}
@@ -123,7 +125,7 @@ class WebSocketImpl implements WebSocket {
123125
throw new SocketClosedError("socket has been closed");
124126
}
125127
const opcode =
126-
typeof data === "string" ? OpCodeTextFrame : OpCodeBinaryFrame;
128+
typeof data === "string" ? OpCode.TextFrame : OpCode.BinaryFrame;
127129
const payload = typeof data === "string" ? this.encoder.encode(data) : data;
128130
const isLastFrame = true;
129131
await writeFrame(
@@ -142,7 +144,7 @@ class WebSocketImpl implements WebSocket {
142144
await writeFrame(
143145
{
144146
isLastFrame: true,
145-
opcode: OpCodeClose,
147+
opcode: OpCode.Close,
146148
mask: this.mask,
147149
payload
148150
},
@@ -170,7 +172,7 @@ class WebSocketImpl implements WebSocket {
170172
await writeFrame(
171173
{
172174
isLastFrame: true,
173-
opcode: OpCodeClose,
175+
opcode: OpCode.Close,
174176
mask: this.mask,
175177
payload
176178
},
@@ -205,12 +207,12 @@ export async function* receiveFrame(
205207
const frame = await readFrame(reader);
206208
const { opcode, payload } = frame;
207209
switch (opcode) {
208-
case OpCodeTextFrame:
209-
case OpCodeBinaryFrame:
210-
case OpCodeContinue:
210+
case OpCode.TextFrame:
211+
case OpCode.BinaryFrame:
212+
case OpCode.Continue:
211213
yield frame;
212214
break;
213-
case OpCodeClose:
215+
case OpCode.Close:
214216
await writeFrame(
215217
{
216218
opcode,
@@ -223,18 +225,18 @@ export async function* receiveFrame(
223225
yield frame;
224226
receiving = false;
225227
break;
226-
case OpcodePing:
228+
case OpCode.Ping:
227229
await writeFrame(
228230
{
229231
payload,
230232
isLastFrame,
231-
opcode: OpcodePong
233+
opcode: OpCode.Pong
232234
},
233235
conn
234236
);
235237
yield frame;
236238
break;
237-
case OpcodePong:
239+
case OpCode.Pong:
238240
yield frame;
239241
break;
240242
}

ws/test.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22
import { Buffer } from "deno";
33
import { BufReader } from "../io/bufio.ts";
44
import { test, assert, assertEqual } from "../testing/mod.ts";
5-
import {
6-
createSecAccept,
7-
OpCodeBinaryFrame,
8-
OpCodeContinue,
9-
OpcodePing,
10-
OpcodePong,
11-
OpCodeTextFrame,
12-
readFrame,
13-
unmask
14-
} from "./mod.ts";
5+
import { createSecAccept, OpCode, readFrame, unmask } from "./mod.ts";
156
import { serve } from "../http/http.ts";
167

178
test(async function testReadUnmaskedTextFrame() {
@@ -20,7 +11,7 @@ test(async function testReadUnmaskedTextFrame() {
2011
new Buffer(new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
2112
);
2213
const frame = await readFrame(buf);
23-
assertEqual(frame.opcode, OpCodeTextFrame);
14+
assertEqual(frame.opcode, OpCode.TextFrame);
2415
assertEqual(frame.mask, undefined);
2516
assertEqual(new Buffer(frame.payload).toString(), "Hello");
2617
assertEqual(frame.isLastFrame, true);
@@ -47,7 +38,7 @@ test(async function testReadMakedTextFrame() {
4738
);
4839
const frame = await readFrame(buf);
4940
console.dir(frame);
50-
assertEqual(frame.opcode, OpCodeTextFrame);
41+
assertEqual(frame.opcode, OpCode.TextFrame);
5142
unmask(frame.payload, frame.mask);
5243
assertEqual(new Buffer(frame.payload).toString(), "Hello");
5344
assertEqual(frame.isLastFrame, true);
@@ -63,12 +54,12 @@ test(async function testReadUnmaskedSplittedTextFrames() {
6354
const [f1, f2] = await Promise.all([readFrame(buf1), readFrame(buf2)]);
6455
assertEqual(f1.isLastFrame, false);
6556
assertEqual(f1.mask, undefined);
66-
assertEqual(f1.opcode, OpCodeTextFrame);
57+
assertEqual(f1.opcode, OpCode.TextFrame);
6758
assertEqual(new Buffer(f1.payload).toString(), "Hel");
6859

6960
assertEqual(f2.isLastFrame, true);
7061
assertEqual(f2.mask, undefined);
71-
assertEqual(f2.opcode, OpCodeContinue);
62+
assertEqual(f2.opcode, OpCode.Continue);
7263
assertEqual(new Buffer(f2.payload).toString(), "lo");
7364
});
7465

@@ -78,7 +69,7 @@ test(async function testReadUnmaksedPingPongFrame() {
7869
new Buffer(new Uint8Array([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
7970
);
8071
const ping = await readFrame(buf);
81-
assertEqual(ping.opcode, OpcodePing);
72+
assertEqual(ping.opcode, OpCode.Ping);
8273
assertEqual(new Buffer(ping.payload).toString(), "Hello");
8374

8475
const buf2 = new BufReader(
@@ -99,7 +90,7 @@ test(async function testReadUnmaksedPingPongFrame() {
9990
)
10091
);
10192
const pong = await readFrame(buf2);
102-
assertEqual(pong.opcode, OpcodePong);
93+
assertEqual(pong.opcode, OpCode.Pong);
10394
assert(pong.mask !== undefined);
10495
unmask(pong.payload, pong.mask);
10596
assertEqual(new Buffer(pong.payload).toString(), "Hello");
@@ -112,7 +103,7 @@ test(async function testReadUnmaksedBigBinaryFrame() {
112103
}
113104
const buf = new BufReader(new Buffer(new Uint8Array(a)));
114105
const bin = await readFrame(buf);
115-
assertEqual(bin.opcode, OpCodeBinaryFrame);
106+
assertEqual(bin.opcode, OpCode.BinaryFrame);
116107
assertEqual(bin.isLastFrame, true);
117108
assertEqual(bin.mask, undefined);
118109
assertEqual(bin.payload.length, 256);
@@ -125,7 +116,7 @@ test(async function testReadUnmaskedBigBigBinaryFrame() {
125116
}
126117
const buf = new BufReader(new Buffer(new Uint8Array(a)));
127118
const bin = await readFrame(buf);
128-
assertEqual(bin.opcode, OpCodeBinaryFrame);
119+
assertEqual(bin.opcode, OpCode.BinaryFrame);
129120
assertEqual(bin.isLastFrame, true);
130121
assertEqual(bin.mask, undefined);
131122
assertEqual(bin.payload.length, 0xffff + 1);

0 commit comments

Comments
 (0)