|
1 | | -import parseRawProto from 'rawprotoparse'; |
| 1 | +/// <reference path="../../custom-typings/rawproto.d.ts" /> |
| 2 | +import RawProto from 'rawproto'; |
2 | 3 | import { gunzipSync, inflateSync } from 'zlib'; |
3 | 4 |
|
4 | 5 | import { Headers } from '../types'; |
5 | 6 | import { getHeaderValue } from '../model/http/headers'; |
6 | 7 |
|
| 8 | +/** |
| 9 | + * Recursively converts a RawProto tree to a simple JS object. |
| 10 | + * This mirrors the output format of the deprecated rawprotoparse package. |
| 11 | + * |
| 12 | + * The old rawprotoparse returned: |
| 13 | + * - Single values directly: { "1": "Hello World" } |
| 14 | + * - Nested messages as objects: { "1": { "2": "value" } } |
| 15 | + * - Buffers for binary data |
| 16 | + * |
| 17 | + * @param protoNode - A RawProto node to convert |
| 18 | + * @returns A plain JavaScript object with field numbers as keys |
| 19 | + */ |
| 20 | +function convertRawProtoToObject(protoNode: RawProto): Record<string, any> { |
| 21 | + const result: Record<string, any> = {}; |
| 22 | + |
| 23 | + if (!protoNode.sub) { |
| 24 | + return result; |
| 25 | + } |
| 26 | + |
| 27 | + for (const fieldNum of Object.keys(protoNode.sub)) { |
| 28 | + const fields = protoNode.sub[fieldNum]; |
| 29 | + |
| 30 | + if (fields.length === 1) { |
| 31 | + const field = fields[0]; |
| 32 | + |
| 33 | + let handled = false; |
| 34 | + |
| 35 | + // Try string first |
| 36 | + if (field.likelyString !== undefined ? field.likelyString : false) { |
| 37 | + try { |
| 38 | + const str = field.string; |
| 39 | + if (str !== undefined) { |
| 40 | + result[fieldNum] = str; |
| 41 | + handled = true; |
| 42 | + } |
| 43 | + } catch (e) {} |
| 44 | + } |
| 45 | + |
| 46 | + if (handled) continue; |
| 47 | + |
| 48 | + // Check if it's a sub-message (has its own sub fields) |
| 49 | + if (field.sub && Object.keys(field.sub).length > 0) { |
| 50 | + result[fieldNum] = convertRawProtoToObject(field); |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + // Try int for varint types |
| 55 | + try { |
| 56 | + const num = field.int; |
| 57 | + if (num !== undefined && typeof num === 'number' && Number.isSafeInteger(num)) { |
| 58 | + result[fieldNum] = num; |
| 59 | + continue; |
| 60 | + } |
| 61 | + } catch (e) {} |
| 62 | + |
| 63 | + // Fall back to bytes |
| 64 | + try { |
| 65 | + const bytes = field.bytes; |
| 66 | + if (bytes !== undefined) { |
| 67 | + result[fieldNum] = Buffer.from(bytes); |
| 68 | + continue; |
| 69 | + } |
| 70 | + } catch (e) {} |
| 71 | + |
| 72 | + result[fieldNum] = null; |
| 73 | + } else { |
| 74 | + // Multiple values - create an array |
| 75 | + result[fieldNum] = fields.map((field: any) => { |
| 76 | + if (field.likelyString !== undefined ? field.likelyString : false) { |
| 77 | + try { |
| 78 | + const str = field.string; |
| 79 | + if (str !== undefined) return str; |
| 80 | + } catch (e) {} |
| 81 | + } |
| 82 | + |
| 83 | + if (field.sub && Object.keys(field.sub).length > 0) { |
| 84 | + return convertRawProtoToObject(field); |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + const num = field.int; |
| 89 | + if (num !== undefined && typeof num === 'number' && Number.isSafeInteger(num)) return num; |
| 90 | + } catch (e) {} |
| 91 | + |
| 92 | + try { |
| 93 | + const bytes = field.bytes; |
| 94 | + if (bytes !== undefined) return Buffer.from(bytes); |
| 95 | + } catch (e) {} |
| 96 | + |
| 97 | + return null; |
| 98 | + }); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return result; |
| 103 | +} |
| 104 | + |
7 | 105 | export function isProbablyProtobuf(input: Uint8Array) { |
8 | 106 | // Protobuf data starts with a varint, consisting of a |
9 | 107 | // field number in [1, 2^29[ and a field type in [0, 5]*. |
@@ -36,7 +134,22 @@ export function isProbablyProtobuf(input: Uint8Array) { |
36 | 134 | [0, 1, 2, 5].includes(fieldType); |
37 | 135 | } |
38 | 136 |
|
39 | | -export const parseRawProtobuf = parseRawProto; |
| 137 | +/** |
| 138 | + * Parse raw protobuf binary data into a JavaScript object. |
| 139 | + * This is a wrapper around rawproto that maintains backward compatibility |
| 140 | + * with the old rawprotoparse API. |
| 141 | + * |
| 142 | + * @param input - The protobuf binary data to parse |
| 143 | + * @param _options - Options object (for backward compatibility, currently unused) |
| 144 | + * @returns Parsed protobuf data as a JavaScript object |
| 145 | + */ |
| 146 | +export function parseRawProtobuf( |
| 147 | + input: Uint8Array | Buffer, |
| 148 | + _options?: { prefix?: string } |
| 149 | +): any { |
| 150 | + const rawProto = new RawProto(input); |
| 151 | + return convertRawProtoToObject(rawProto); |
| 152 | +} |
40 | 153 |
|
41 | 154 | // GRPC message structure: |
42 | 155 | // Ref: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md |
|
0 commit comments