-
Notifications
You must be signed in to change notification settings - Fork 663
/
Copy pathencode.js
41 lines (36 loc) · 1.06 KB
/
encode.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
const fs = require('node:fs');
const { resolve } = require('node:path');
const { echoService } = require('./utils');
/**
* Serialize a message to a length-delimited byte string.
* @param value
* @returns
*/
function serializeMessage(serialize, value) {
const messageBuffer = serialize(value);
const byteLength = messageBuffer.byteLength;
const output = Buffer.allocUnsafe(byteLength + 5);
/* Note: response compression is currently not supported, so this
* compressed bit is always 0. */
output.writeUInt8(0, 0);
output.writeUInt32BE(byteLength, 1);
messageBuffer.copy(output, 5);
return output;
}
const binaryMessage = serializeMessage(
echoService.service.Echo.requestSerialize,
{
value: 'string-val',
value2: 10,
}
);
if (require.main === module) {
console.log(
'Service %s\nEcho binary bytes: %d, hex: %s',
echoService.service.Echo.path,
binaryMessage.length,
binaryMessage.toString('hex')
);
fs.writeFileSync(resolve(__dirname, '../echo-unary.bin'), binaryMessage);
}
exports.serializeMessage = serializeMessage;