forked from VictorTaelin/uwuchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
180 lines (155 loc) · 3.39 KB
/
lib.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { BSON } from "bson";
const WATCH = 0;
const UNWATCH = 1;
const POST = 2;
const SHOW = 3;
const TIME = 4;
// type RoomID = U64
// type PostID = U64
// type Time = U64
// type Address = U64
// type PostData = Vec<U64>
function hex_to_bytes(hex) {
var arr = [];
for (var i = 0; i < hex.length/2; ++i) {
arr.push((parseInt(hex[i*2+0],16)<<4)|parseInt(hex[i*2+1],16));
};
return new Uint8Array(arr);
};
const hex_char = "0123456789abcdef".split("");
function bytes_to_hex(buf) {
var hex = "";
for (var i = 0; i < buf.length; ++i) {
hex += hex_char[buf[i]>>>4] + hex_char[buf[i]&0xF];
};
return hex;
};
function hex_join(arr) {
let res = "";
for (var i = 0; i < arr.length; ++i) {
res += arr[i];
}
return res;
};
function hexs_to_bytes(arr) {
return hex_to_bytes(hex_join(arr));
};
function u8_to_hex(num) {
return ("00" + num.toString(16)).slice(-2);
};
function hex_to_u8(hex) {
return parseInt(hex, 16);
};
function hex_to_u32(hex) {
return parseInt(hex.slice(-32), 16);
};
function hex_to_u64(hex) {
return parseInt(hex.slice(-64), 16);
};
function uN_to_hex(N, num) {
var hex = "";
for (var i = 0; i < N/4; ++i) {
hex += hex_char[(num / (2**((N/4-i-1)*4))) & 0xF];
};
return hex;
};
function u32_to_hex(num) {
return uN_to_hex(32, num);
};
function u64_to_hex(num) {
return uN_to_hex(64, num);
};
function check_hex(bits, hex) {
if (typeof hex !== "string") {
return null;
}
if (!/^[a-fA-F0-9]*$/.test(hex)) {
return null;
}
if (bits) {
while (hex.length * 4 < bits) {
hex = "0" + hex;
}
if (hex.length * 4 > bits) {
hex = hex.slice(0, Math.floor(bits / 4));
}
return hex.toLowerCase();
} else {
hex = hex.length % 2 === 1 ? "0" + hex : hex;
return hex.toLowerCase();
}
};
var utf8_encoder = new TextEncoder("utf-8");
function string_to_bytes(str) {
return utf8_encoder.encode(str);
};
var utf8_decoder = new TextDecoder("utf-8");
function bytes_to_string(buf) {
return utf8_decoder.decode(buf);
};
function string_to_hex(str) {
return bytes_to_hex(string_to_bytes(str));
};
function hex_to_string(hex) {
return bytes_to_string(hex_to_bytes(hex));
};
function states_new() {
return null;
}
function json_to_hex(json) {
return bytes_to_hex(BSON.serialize(json));
}
function hex_to_json(hex) {
return BSON.deserialize(hex_to_bytes(hex));
}
// Adds a state to the list of states
// It only keeps log(N) states, where N is the amount of ticks recorded
function states_push(states, new_state) {
if (states === null) {
return {bit: 0, current: new_state, older: null};
} else {
var {bit, current, older} = states;
if (bit === 0) {
return {bit: 1, current, older};
} else {
return {bit: 0, current: new_state, older: states_push(older, current)};
}
}
}
// Finds the latest state that happened before a tick
function states_before(states, tick) {
if (states === null) {
return null;
} else {
if (states.current.tick < tick) {
return states.current;
} else {
return latest(states.older, tick);
}
}
}
export default {
WATCH,
UNWATCH,
POST,
SHOW,
TIME,
hex_to_bytes,
bytes_to_hex,
hexs_to_bytes,
hex_join,
u8_to_hex,
hex_to_u8,
u32_to_hex,
hex_to_u32,
u64_to_hex,
hex_to_u64,
string_to_hex,
hex_to_string,
check_hex,
json_to_hex,
hex_to_json,
states_new,
states_push,
states_before,
};