-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.js
More file actions
47 lines (41 loc) · 1.04 KB
/
encode.js
File metadata and controls
47 lines (41 loc) · 1.04 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
const fs = require('fs');
const data= {name:'test', myprop:'demo'};
const encodeString = (str) => {
const strl = str.length;
let ret = [];
if(strl < 32){
ret[0] = 0xa0 | strl;
}
for(let i = 0; i < strl; i++){
ret[i+1] = str[i].charCodeAt(0);
}
return ret;
}
const encodeMapLength = len => {
if(len < 16){
return [0x80 | len];
}
}
const encodeMap = (map) => {
const output = [];
const mapKeys = Object.keys(data);
output.push(...encodeMapLength(mapKeys.length));
mapKeys.forEach(key => {
const value = map[key];
const encodedKeyValue = [...encodeString(key),...encodeString(value)];
output.push(...encodedKeyValue);
});
return output;
};
const encodeData = data => {
if(typeof data === 'object'){
const encoded = encodeMap(data);
const intArr = new Uint8Array(2**22);
encoded.forEach((byte,offset) => {
intArr[offset] = byte;
});
return Buffer.from(intArr).slice(0, encoded.length);
}
}
encodedBuffer = encodeData(data);
fs.writeFileSync('bsondata.bin', encodedBuffer, 'hex');