-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.js
More file actions
131 lines (112 loc) · 3.85 KB
/
encoder.js
File metadata and controls
131 lines (112 loc) · 3.85 KB
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
const { xxhash32, xxhash64, xxhash128 } = require('hash-wasm')
// no checksums
class Encoder {
constructor() {
this.lockLen = 8
this.metaLen = 24
this.bodyLen = 0
}
async encodeLock(log, buf, o, seq) {
buf.writeBigInt64BE(seq, o)
}
async decodeLock(log, buf, o) {
return buf.readBigInt64BE(o)
}
async encodeMeta(log, buf, o, seq, off, len) {
buf.writeBigInt64BE(seq, o)
buf.writeBigInt64BE(off, o + 8)
buf.writeBigInt64BE(len, o + 16)
}
async decodeMeta(log, buf, o) {
const seq = buf.readBigInt64BE(o)
const off = buf.readBigInt64BE(o + 8)
const len = buf.readBigInt64BE(o + 16)
return { seq, off, len }
}
async encodeBody(log, buf) {
return buf
}
async decodeBody(log, buf) {
return buf
}
}
const writeHash32 = (hash, buf, off) => {
hash = Buffer.from(hash, 'hex')
buf.writeUInt32BE(hash.readUInt32BE(0), off)
}
const writeHash64 = (hash, buf, off) => {
hash = Buffer.from(hash, 'hex')
buf.writeBigInt64BE(hash.readBigInt64BE(0), off)
}
const writeHash128 = (hash, buf, off) => {
hash = Buffer.from(hash, 'hex')
buf.writeBigInt64BE(hash.readBigInt64BE(0), off)
buf.writeBigInt64BE(hash.readBigInt64BE(8), off + 8)
}
const basic = new Encoder()
// lock = checksum
// meta = checksum
// body = optional
class XxHashEncoder {
constructor(body = true) {
this.lockLen = 12
this.metaLen = 32
this.bodyLen = body ? 16 : basic.bodyLen
this.encodeBody = body ? this._encodeBody : basic.encodeBody
this.decodeBody = body ? this._decodeBody : basic.decodeBody
}
async encodeLock(log, buf, o, seq) {
const { path: name } = log
buf.writeBigInt64BE(seq, o + 4)
const data = buf.subarray(o + 4, o + 12)
const hash = await xxhash32(data).catch((err) => {throw new Error(`${name} lock encode make hash error`)})
writeHash32(hash, buf, o)
}
async decodeLock(log, buf, o) {
const { path: name } = log
const hash = buf.subarray(o, o + 4)
const data = buf.subarray(o + 4, o + 12)
let hash2 = await xxhash32(data).catch((err) => {throw new Error(`${name} lock decode make hash error`)})
hash2 = Buffer.from(hash2, 'hex')
if (!hash.equals(hash2)) { throw new Error(`${name} lock corrupt`) }
return buf.readBigInt64BE(o + 4)
}
async encodeMeta(log, buf, o, seq, off, len) {
const { path: name } = log
buf.writeBigInt64BE(seq, o + 8)
buf.writeBigInt64BE(off, o + 16)
buf.writeBigInt64BE(len, o + 24)
const data = buf.subarray(o + 8, o + 32)
const hash = await xxhash64(data).catch((err) => {throw new Error(`${name} meta encode make hash error`)})
writeHash64(hash, buf, o)
}
async decodeMeta(log, buf, o) {
const { path: name } = log
const hash = buf.subarray(o, o + 8)
const data = buf.subarray(o + 8, o + 32)
let hash2 = await xxhash64(data).catch((err) => {throw new Error(`${name} meta decode make hash error`)})
hash2 = Buffer.from(hash2, 'hex')
if (!hash.equals(hash2)) { throw new Error(`${name} meta corrupt`) }
const seq = buf.readBigInt64BE(o + 8)
const off = buf.readBigInt64BE(o + 16)
const len = buf.readBigInt64BE(o + 24)
return { seq, off, len }
}
async _encodeBody(log, buf) {
const { path: name } = log
const hash = await xxhash128(buf).catch((err) => {throw new Error(`${name} body encode make hash error`)})
const hashbuf = Buffer.allocUnsafe(16)
writeHash128(hash, hashbuf, 0)
return Buffer.concat([hashbuf, buf])
}
async _decodeBody(log, buf) {
const { path: name } = log
const hash = buf.subarray(0, 16)
const data = buf.subarray(16)
let hash2 = await xxhash128(data).catch((err) => {throw new Error(`${name} body decode make hash error`)})
hash2 = Buffer.from(hash2, 'hex')
if (!hash.equals(hash2)) { throw new Error(`${name} body corrupt`) }
return buf.subarray(16)
}
}
module.exports = { Encoder, XxHashEncoder }