-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
126 lines (98 loc) · 3.12 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { varnum } from "./deps.ts";
const BYOND_RESPONSE_HEADER_SIZE = 5;
const BYOND_FLOAT_TYPE = 0x2a;
const BYOND_TEXT_TYPE = 0x06;
export class ByondClient {
#connection?: Deno.TcpConn;
constructor() {}
async connect(connectionOptions: Deno.ConnectTlsOptions) {
this.#connection = await Deno.connect(connectionOptions);
}
#urlEncode(topic: string, parameters?: { [key: string]: string }) {
if (topic[0] !== "?") topic = "?" + topic;
if (parameters === undefined) return topic;
Object.keys(parameters).forEach((parameter) => {
const value = parameters[parameter];
if (value === undefined) {
topic = topic += `&${parameter}`;
} else {
topic = topic += `&${parameter}=${encodeURIComponent(
parameters[parameter]
)}`;
}
});
return topic;
}
#createByondPacket(topic: string) {
const size = 2 + 2 + 5 + topic.length + 1;
const dataBuffer = new Uint8Array(2 + 2 + 5 + topic.length + 1);
dataBuffer[0] = 0x00;
dataBuffer[1] = 0x83;
dataBuffer[2] = 0x00;
dataBuffer[3] = 5 + topic.length + 1;
dataBuffer[4] = 0x00;
dataBuffer[5] = 0x00;
dataBuffer[6] = 0x00;
dataBuffer[7] = 0x00;
dataBuffer[8] = 0x00;
const utfEncoded = new TextEncoder().encode(topic);
dataBuffer.set(utfEncoded, 9);
dataBuffer[size] = 0x00;
return dataBuffer;
}
#decodeByondRespose(
headerBuffer: Uint8Array,
dataBuffer: Uint8Array
): number | string {
const dataType = headerBuffer[4];
switch (dataType) {
case BYOND_FLOAT_TYPE: {
const number = varnum(dataBuffer, {
dataType: "float32",
endian: "little",
});
if (number === null) {
throw new Error(
`Unexpected data from server, was expecting 4 bytes but received ${dataBuffer}`
);
}
return number;
}
case BYOND_TEXT_TYPE:
return new TextDecoder().decode(dataBuffer);
default:
throw new Error(
`Unexpected type header returned by server: ${dataType}`
);
}
}
#decodeByondResponseSize(headerBuffer: Uint8Array) {
const responseSize = varnum(headerBuffer.slice(2, 4), {
dataType: "uint16",
});
if (responseSize === null) {
throw new Error(`Unexpected header returned by server ${headerBuffer}`);
}
return responseSize;
}
async send(
topic: string,
parameters?: { [key: string]: string }
): Promise<string | number> {
if (this.#connection === undefined)
throw new Error("Tried using send() without open()ing first.");
const byondPacket = this.#createByondPacket(
this.#urlEncode(topic, parameters)
);
await this.#connection.write(byondPacket);
const headerBuffer = new Uint8Array(BYOND_RESPONSE_HEADER_SIZE);
await this.#connection.read(headerBuffer);
const responseSize = this.#decodeByondResponseSize(headerBuffer);
const dataBuffer = new Uint8Array(responseSize);
await this.#connection.read(dataBuffer);
return this.#decodeByondRespose(headerBuffer, dataBuffer);
}
close() {
this.#connection?.close();
}
}