-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
69 lines (59 loc) · 1.83 KB
/
index.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
#!/usr/bin/env node
import { exec } from "child_process";
import { promisify } from "util";
import minimist from "minimist";
import chalk from "chalk";
import which from "which";
var argv = minimist(process.argv.slice(2));
const asyncExec = promisify(exec);
const log = console.log;
let grpcWebTexts: Array<string> = [
"AAAAADEKB2pvZmxveWQSJmh0dHBzOi8vcGljc3VtLnBob3Rvcy9pZC8xMzMvMjc0Mi8xODI4",
"AAAAAAMInRQ=gAAAACBncnBjLXN0YXR1czowDQpncnBjLW1lc3NhZ2U6T0sNCg==",
];
function getBinaryHex(grpcText: Array<string>) {
const buffers: Array<string> = [];
for (const text of grpcText) {
const buffer = Buffer.from(text, "base64");
const bufString = buffer.toString("hex");
buffers.push(bufString);
}
return buffers;
}
function validateProtocExist() {
let resolved = which.sync("protoc", { nothrow: false });
if (!resolved) {
log(
chalk.red(
`\ngrpcwebtext-parser requires ${chalk.bgRed(
chalk.white(`protoc`)
)}\nPlease download it before running this program\n`
)
);
process.exit(1);
}
}
async function main() {
validateProtocExist();
const { _: text } = argv;
if (text.length === 0) {
console.error("No input grpc text, defaulting to demo values");
}
// bytes are in base 16
const buffers = text.length ? getBinaryHex(text) : getBinaryHex(grpcWebTexts);
for (const i in buffers) {
const b = buffers[i];
const webText = grpcWebTexts[i];
if (b.substr(0, 2) !== "00") return log(chalk.bgRed("Cannot Parse"));
const frameLen = b.substr(2, 4 * 2);
// convert to base 10
const len = parseInt(frameLen, 16);
const data = b.substr(10, len * 2);
const { stdout } = await asyncExec(
`echo ${data} | xxd -r -p | protoc --decode_raw`
);
log(`\n${chalk.bgBlue("Web gRPC text:")} ${webText}`);
console.log(stdout);
}
}
main();