mirror of
https://github.com/denoland/deno.git
synced 2024-11-30 16:40:57 -05:00
Move WebSocket op codes to enum (#171)
This commit is contained in:
parent
214fb8d2c6
commit
56579ed6ad
2 changed files with 34 additions and 41 deletions
48
ws/mod.ts
48
ws/mod.ts
|
@ -5,12 +5,14 @@ import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||||
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
|
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
|
||||||
import { Sha1 } from "./sha1.ts";
|
import { Sha1 } from "./sha1.ts";
|
||||||
|
|
||||||
export const OpCodeContinue = 0x0;
|
export enum OpCode {
|
||||||
export const OpCodeTextFrame = 0x1;
|
Continue = 0x0,
|
||||||
export const OpCodeBinaryFrame = 0x2;
|
TextFrame = 0x1,
|
||||||
export const OpCodeClose = 0x8;
|
BinaryFrame = 0x2,
|
||||||
export const OpcodePing = 0x9;
|
Close = 0x8,
|
||||||
export const OpcodePong = 0xa;
|
Ping = 0x9,
|
||||||
|
Pong = 0xa
|
||||||
|
}
|
||||||
|
|
||||||
export type WebSocketEvent =
|
export type WebSocketEvent =
|
||||||
| string
|
| string
|
||||||
|
@ -77,9 +79,9 @@ class WebSocketImpl implements WebSocket {
|
||||||
for await (const frame of receiveFrame(this.conn)) {
|
for await (const frame of receiveFrame(this.conn)) {
|
||||||
unmask(frame.payload, frame.mask);
|
unmask(frame.payload, frame.mask);
|
||||||
switch (frame.opcode) {
|
switch (frame.opcode) {
|
||||||
case OpCodeTextFrame:
|
case OpCode.TextFrame:
|
||||||
case OpCodeBinaryFrame:
|
case OpCode.BinaryFrame:
|
||||||
case OpCodeContinue:
|
case OpCode.Continue:
|
||||||
frames.push(frame);
|
frames.push(frame);
|
||||||
payloadsLength += frame.payload.length;
|
payloadsLength += frame.payload.length;
|
||||||
if (frame.isLastFrame) {
|
if (frame.isLastFrame) {
|
||||||
|
@ -89,7 +91,7 @@ class WebSocketImpl implements WebSocket {
|
||||||
concat.set(frame.payload, offs);
|
concat.set(frame.payload, offs);
|
||||||
offs += frame.payload.length;
|
offs += frame.payload.length;
|
||||||
}
|
}
|
||||||
if (frames[0].opcode === OpCodeTextFrame) {
|
if (frames[0].opcode === OpCode.TextFrame) {
|
||||||
// text
|
// text
|
||||||
yield new Buffer(concat).toString();
|
yield new Buffer(concat).toString();
|
||||||
} else {
|
} else {
|
||||||
|
@ -100,7 +102,7 @@ class WebSocketImpl implements WebSocket {
|
||||||
payloadsLength = 0;
|
payloadsLength = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case OpCodeClose:
|
case OpCode.Close:
|
||||||
const code = (frame.payload[0] << 16) | frame.payload[1];
|
const code = (frame.payload[0] << 16) | frame.payload[1];
|
||||||
const reason = new Buffer(
|
const reason = new Buffer(
|
||||||
frame.payload.subarray(2, frame.payload.length)
|
frame.payload.subarray(2, frame.payload.length)
|
||||||
|
@ -108,10 +110,10 @@ class WebSocketImpl implements WebSocket {
|
||||||
this._isClosed = true;
|
this._isClosed = true;
|
||||||
yield { code, reason };
|
yield { code, reason };
|
||||||
return;
|
return;
|
||||||
case OpcodePing:
|
case OpCode.Ping:
|
||||||
yield ["ping", frame.payload] as WebSocketPingEvent;
|
yield ["ping", frame.payload] as WebSocketPingEvent;
|
||||||
break;
|
break;
|
||||||
case OpcodePong:
|
case OpCode.Pong:
|
||||||
yield ["pong", frame.payload] as WebSocketPongEvent;
|
yield ["pong", frame.payload] as WebSocketPongEvent;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -123,7 +125,7 @@ class WebSocketImpl implements WebSocket {
|
||||||
throw new SocketClosedError("socket has been closed");
|
throw new SocketClosedError("socket has been closed");
|
||||||
}
|
}
|
||||||
const opcode =
|
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 payload = typeof data === "string" ? this.encoder.encode(data) : data;
|
||||||
const isLastFrame = true;
|
const isLastFrame = true;
|
||||||
await writeFrame(
|
await writeFrame(
|
||||||
|
@ -142,7 +144,7 @@ class WebSocketImpl implements WebSocket {
|
||||||
await writeFrame(
|
await writeFrame(
|
||||||
{
|
{
|
||||||
isLastFrame: true,
|
isLastFrame: true,
|
||||||
opcode: OpCodeClose,
|
opcode: OpCode.Close,
|
||||||
mask: this.mask,
|
mask: this.mask,
|
||||||
payload
|
payload
|
||||||
},
|
},
|
||||||
|
@ -170,7 +172,7 @@ class WebSocketImpl implements WebSocket {
|
||||||
await writeFrame(
|
await writeFrame(
|
||||||
{
|
{
|
||||||
isLastFrame: true,
|
isLastFrame: true,
|
||||||
opcode: OpCodeClose,
|
opcode: OpCode.Close,
|
||||||
mask: this.mask,
|
mask: this.mask,
|
||||||
payload
|
payload
|
||||||
},
|
},
|
||||||
|
@ -205,12 +207,12 @@ export async function* receiveFrame(
|
||||||
const frame = await readFrame(reader);
|
const frame = await readFrame(reader);
|
||||||
const { opcode, payload } = frame;
|
const { opcode, payload } = frame;
|
||||||
switch (opcode) {
|
switch (opcode) {
|
||||||
case OpCodeTextFrame:
|
case OpCode.TextFrame:
|
||||||
case OpCodeBinaryFrame:
|
case OpCode.BinaryFrame:
|
||||||
case OpCodeContinue:
|
case OpCode.Continue:
|
||||||
yield frame;
|
yield frame;
|
||||||
break;
|
break;
|
||||||
case OpCodeClose:
|
case OpCode.Close:
|
||||||
await writeFrame(
|
await writeFrame(
|
||||||
{
|
{
|
||||||
opcode,
|
opcode,
|
||||||
|
@ -223,18 +225,18 @@ export async function* receiveFrame(
|
||||||
yield frame;
|
yield frame;
|
||||||
receiving = false;
|
receiving = false;
|
||||||
break;
|
break;
|
||||||
case OpcodePing:
|
case OpCode.Ping:
|
||||||
await writeFrame(
|
await writeFrame(
|
||||||
{
|
{
|
||||||
payload,
|
payload,
|
||||||
isLastFrame,
|
isLastFrame,
|
||||||
opcode: OpcodePong
|
opcode: OpCode.Pong
|
||||||
},
|
},
|
||||||
conn
|
conn
|
||||||
);
|
);
|
||||||
yield frame;
|
yield frame;
|
||||||
break;
|
break;
|
||||||
case OpcodePong:
|
case OpCode.Pong:
|
||||||
yield frame;
|
yield frame;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
27
ws/test.ts
27
ws/test.ts
|
@ -2,16 +2,7 @@
|
||||||
import { Buffer } from "deno";
|
import { Buffer } from "deno";
|
||||||
import { BufReader } from "../io/bufio.ts";
|
import { BufReader } from "../io/bufio.ts";
|
||||||
import { test, assert, assertEqual } from "../testing/mod.ts";
|
import { test, assert, assertEqual } from "../testing/mod.ts";
|
||||||
import {
|
import { createSecAccept, OpCode, readFrame, unmask } from "./mod.ts";
|
||||||
createSecAccept,
|
|
||||||
OpCodeBinaryFrame,
|
|
||||||
OpCodeContinue,
|
|
||||||
OpcodePing,
|
|
||||||
OpcodePong,
|
|
||||||
OpCodeTextFrame,
|
|
||||||
readFrame,
|
|
||||||
unmask
|
|
||||||
} from "./mod.ts";
|
|
||||||
import { serve } from "../http/http.ts";
|
import { serve } from "../http/http.ts";
|
||||||
|
|
||||||
test(async function testReadUnmaskedTextFrame() {
|
test(async function testReadUnmaskedTextFrame() {
|
||||||
|
@ -20,7 +11,7 @@ test(async function testReadUnmaskedTextFrame() {
|
||||||
new Buffer(new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
new Buffer(new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
||||||
);
|
);
|
||||||
const frame = await readFrame(buf);
|
const frame = await readFrame(buf);
|
||||||
assertEqual(frame.opcode, OpCodeTextFrame);
|
assertEqual(frame.opcode, OpCode.TextFrame);
|
||||||
assertEqual(frame.mask, undefined);
|
assertEqual(frame.mask, undefined);
|
||||||
assertEqual(new Buffer(frame.payload).toString(), "Hello");
|
assertEqual(new Buffer(frame.payload).toString(), "Hello");
|
||||||
assertEqual(frame.isLastFrame, true);
|
assertEqual(frame.isLastFrame, true);
|
||||||
|
@ -47,7 +38,7 @@ test(async function testReadMakedTextFrame() {
|
||||||
);
|
);
|
||||||
const frame = await readFrame(buf);
|
const frame = await readFrame(buf);
|
||||||
console.dir(frame);
|
console.dir(frame);
|
||||||
assertEqual(frame.opcode, OpCodeTextFrame);
|
assertEqual(frame.opcode, OpCode.TextFrame);
|
||||||
unmask(frame.payload, frame.mask);
|
unmask(frame.payload, frame.mask);
|
||||||
assertEqual(new Buffer(frame.payload).toString(), "Hello");
|
assertEqual(new Buffer(frame.payload).toString(), "Hello");
|
||||||
assertEqual(frame.isLastFrame, true);
|
assertEqual(frame.isLastFrame, true);
|
||||||
|
@ -63,12 +54,12 @@ test(async function testReadUnmaskedSplittedTextFrames() {
|
||||||
const [f1, f2] = await Promise.all([readFrame(buf1), readFrame(buf2)]);
|
const [f1, f2] = await Promise.all([readFrame(buf1), readFrame(buf2)]);
|
||||||
assertEqual(f1.isLastFrame, false);
|
assertEqual(f1.isLastFrame, false);
|
||||||
assertEqual(f1.mask, undefined);
|
assertEqual(f1.mask, undefined);
|
||||||
assertEqual(f1.opcode, OpCodeTextFrame);
|
assertEqual(f1.opcode, OpCode.TextFrame);
|
||||||
assertEqual(new Buffer(f1.payload).toString(), "Hel");
|
assertEqual(new Buffer(f1.payload).toString(), "Hel");
|
||||||
|
|
||||||
assertEqual(f2.isLastFrame, true);
|
assertEqual(f2.isLastFrame, true);
|
||||||
assertEqual(f2.mask, undefined);
|
assertEqual(f2.mask, undefined);
|
||||||
assertEqual(f2.opcode, OpCodeContinue);
|
assertEqual(f2.opcode, OpCode.Continue);
|
||||||
assertEqual(new Buffer(f2.payload).toString(), "lo");
|
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]))
|
new Buffer(new Uint8Array([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
||||||
);
|
);
|
||||||
const ping = await readFrame(buf);
|
const ping = await readFrame(buf);
|
||||||
assertEqual(ping.opcode, OpcodePing);
|
assertEqual(ping.opcode, OpCode.Ping);
|
||||||
assertEqual(new Buffer(ping.payload).toString(), "Hello");
|
assertEqual(new Buffer(ping.payload).toString(), "Hello");
|
||||||
|
|
||||||
const buf2 = new BufReader(
|
const buf2 = new BufReader(
|
||||||
|
@ -99,7 +90,7 @@ test(async function testReadUnmaksedPingPongFrame() {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
const pong = await readFrame(buf2);
|
const pong = await readFrame(buf2);
|
||||||
assertEqual(pong.opcode, OpcodePong);
|
assertEqual(pong.opcode, OpCode.Pong);
|
||||||
assert(pong.mask !== undefined);
|
assert(pong.mask !== undefined);
|
||||||
unmask(pong.payload, pong.mask);
|
unmask(pong.payload, pong.mask);
|
||||||
assertEqual(new Buffer(pong.payload).toString(), "Hello");
|
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 buf = new BufReader(new Buffer(new Uint8Array(a)));
|
||||||
const bin = await readFrame(buf);
|
const bin = await readFrame(buf);
|
||||||
assertEqual(bin.opcode, OpCodeBinaryFrame);
|
assertEqual(bin.opcode, OpCode.BinaryFrame);
|
||||||
assertEqual(bin.isLastFrame, true);
|
assertEqual(bin.isLastFrame, true);
|
||||||
assertEqual(bin.mask, undefined);
|
assertEqual(bin.mask, undefined);
|
||||||
assertEqual(bin.payload.length, 256);
|
assertEqual(bin.payload.length, 256);
|
||||||
|
@ -125,7 +116,7 @@ test(async function testReadUnmaskedBigBigBinaryFrame() {
|
||||||
}
|
}
|
||||||
const buf = new BufReader(new Buffer(new Uint8Array(a)));
|
const buf = new BufReader(new Buffer(new Uint8Array(a)));
|
||||||
const bin = await readFrame(buf);
|
const bin = await readFrame(buf);
|
||||||
assertEqual(bin.opcode, OpCodeBinaryFrame);
|
assertEqual(bin.opcode, OpCode.BinaryFrame);
|
||||||
assertEqual(bin.isLastFrame, true);
|
assertEqual(bin.isLastFrame, true);
|
||||||
assertEqual(bin.mask, undefined);
|
assertEqual(bin.mask, undefined);
|
||||||
assertEqual(bin.payload.length, 0xffff + 1);
|
assertEqual(bin.payload.length, 0xffff + 1);
|
||||||
|
|
Loading…
Reference in a new issue