-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbytes.ts
More file actions
24 lines (17 loc) · 867 Bytes
/
bytes.ts
File metadata and controls
24 lines (17 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { unsigned } from '../utils/varint.js'
import { DecodeFunction, EncodeFunction, createCodec, EncodingLengthFunction, CODEC_TYPES } from './codec.js'
const encodingLength: EncodingLengthFunction<Uint8Array> = function bytesEncodingLength (val) {
const len = val.byteLength
return unsigned.encodingLength(len) + len
}
const encode: EncodeFunction<Uint8Array> = function bytesEncode (val, buf, offset) {
offset = unsigned.encode(val.byteLength, buf, offset)
buf.write(val, offset)
return offset + val.byteLength
}
const decode: DecodeFunction<Uint8Array> = function bytesDecode (buf, offset) {
const byteLength = unsigned.decode(buf, offset)
offset += unsigned.encodingLength(byteLength)
return buf.slice(offset, offset + byteLength)
}
export const bytes = createCodec('bytes', CODEC_TYPES.LENGTH_DELIMITED, encode, decode, encodingLength)