-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.ts
240 lines (199 loc) · 6.32 KB
/
buffer.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* buffer reader/writer for postgresql buffer/data-types
* https://www.postgresql.org/docs/13/protocol-message-types.html
*/
/**
* ported from `deno/std/encoding/utf8.ts`
* after deno/std/ v0.88.0, breaking change remove `ecoding/utf8.ts`
* TextEncoder() only support utf-8 encording
*/
const encoder = new TextEncoder()
export function encode(input?: string): Uint8Array {
return encoder.encode(input)
}
const decoder = new TextDecoder()
export function decode(input?: Uint8Array): string {
return decoder.decode(input)
}
/**
* big endian and little endian transformer
* for int8/int16/int32/uint8/uint16/uint32
*/
export function readInt8BE(buffer: Uint8Array, offset: number): number {
// https://blog.vjeux.com/2013/javascript/conversion-from-uint8-to-int8-x-24.html
return buffer[offset] << 24 >> 24
}
export function readInt16BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0 // to uint32
const value = buffer[offset + 1] | (buffer[offset] << 8)
return value & 0x8000 ? value | 0xffff0000 : value
}
export function readInt32BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0
return (
(buffer[offset] << 24) |
(buffer[offset + 1] << 16) |
(buffer[offset + 2] << 8) |
(buffer[offset + 3])
)
}
export function readUint8BE(buffer: Uint8Array, offset: number): number {
return buffer[offset]
}
export function readUint16BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0
return buffer[offset] | (buffer[offset + 1] << 8)
}
export function readUint32BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0
return (
buffer[offset] * 0x1000000 +
(
(buffer[offset + 1] << 16) |
(buffer[offset + 2] << 8) |
(buffer[offset + 3])
)
)
}
/**
* reader for postgresql buffer reading
* big endian buffer
*/
export class BufferReader {
index = 0
get length(): number {
return this.index
}
get ended(): boolean {
return this.index > this.buffer.length
}
constructor(readonly buffer: Uint8Array) {}
read(length: number): Uint8Array {
const buffer = this.buffer.slice(this.index, this.index + length)
this.index += length
return buffer
}
/**
* this.readSlice(0x00) means null terminated string
*/
readSlice(delim: number): string {
let end = this.buffer.indexOf(delim, this.index)
if (end === -1) end = this.buffer.length
const buffer = this.buffer.slice(this.index, end)
this.index += buffer.length + 1
return decode(buffer)
}
readString(length: number): string {
const buffer = this.read(length)
return decode(buffer)
}
readInt8(): number {
const value = readInt8BE(this.buffer, this.index)
this.index += 1
return value
}
readInt16(): number {
const value = readInt16BE(this.buffer, this.index)
this.index += 2
return value
}
readInt32(): number {
const value = readInt32BE(this.buffer, this.index)
this.index += 4
return value
}
readUint8(): number {
const value = readUint8BE(this.buffer, this.index)
this.index += 1
return value
}
readUint16(): number {
const value = readUint16BE(this.buffer, this.index)
this.index += 2
return value
}
readUint32(): number {
const value = readUint32BE(this.buffer, this.index)
this.index += 4
return value
}
}
/**
* writer for postgresql buffer writing
* big endian buffer
*/
export class BufferWriter {
index = 0
constructor(public buffer: Uint8Array) {}
/**
* enlarge current buffer with more length
*/
enlarge(length: number): BufferWriter {
const remainLength = this.buffer.length - this.index
if (remainLength < length) {
const buffer = this.buffer
// enlarge factor ~= 1.5
// https://stackoverflow.com/questions/2269063/buffer-growth-strategy
const newestLength = buffer.length + (buffer.length >> 1) + length
this.buffer = new Uint8Array(newestLength)
// copy current buffer to this.buffer from zero
// enlarge with more length
this.buffer.set(buffer, 0)
}
return this
}
write(buffer: Uint8Array): BufferWriter {
this.enlarge(buffer.length)
// copy buffer to this.buffer start in this.index
this.buffer.set(buffer, this.index)
this.index += buffer.length
return this
}
writeString(string: string): BufferWriter {
const buffer = encode(string)
// buffer.byteLength === buffer.length without offset
this.enlarge(buffer.length)
// copy buffer to this.buffer start in this.index
this.buffer.set(buffer, this.index)
this.index += buffer.length
return this
}
writeInt8(digit: number): BufferWriter {
this.enlarge(1)
this.buffer[this.index++] = (digit >>> 0) & 0xff
return this
}
writeInt16(digit: number): BufferWriter {
this.enlarge(2)
this.buffer[this.index++] = (digit >>> 8) & 0xff
this.buffer[this.index++] = (digit >>> 0) & 0xff
return this
}
writeInt32(digit: number): BufferWriter {
this.enlarge(4)
this.buffer[this.index++] = (digit >>> 24) & 0xff
this.buffer[this.index++] = (digit >>> 16) & 0xff
this.buffer[this.index++] = (digit >>> 8) & 0xff
this.buffer[this.index++] = (digit >>> 0) & 0xff
return this
}
writeUint8(digit: number): BufferWriter {
this.enlarge(1)
this.buffer[this.index++] = (digit >> 0) & 0xff
return this
}
writeUint16(digit: number): BufferWriter {
this.enlarge(2)
this.buffer[this.index++] = (digit >> 0) & 0xff
this.buffer[this.index++] = (digit >> 8) & 0xff
return this
}
writeUint32(digit: number): BufferWriter {
this.enlarge(4)
this.buffer[this.index++] = (digit >> 0) & 0xff
this.buffer[this.index++] = (digit >> 8) & 0xff
this.buffer[this.index++] = (digit >> 16) & 0xff
this.buffer[this.index++] = (digit >> 24) & 0xff
return this
}
}