Debian Codegraph @

node/@bufbuild/protobuf/dist/esm/wire/text-format.js

raw
1 // Copyright 2021-2026 Buf Technologies, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 import { ScalarType } from "../descriptors.js";
15 import { protoInt64 } from "../proto-int64.js";
16 /**
17 * Parse an enum value from the Protobuf text format.
18 *
19 * @private
20 */
21 export function parseTextFormatEnumValue(descEnum, value) {
22 const enumValue = descEnum.values.find((v) => v.name === value);
23 if (!enumValue) {
24 throw new Error(`cannot parse ${descEnum} default value: ${value}`);
25 }
26 return enumValue.number;
27 }
28 /**
29 * Parse a scalar value from the Protobuf text format.
30 *
31 * @private
32 */
33 export function parseTextFormatScalarValue(type, value) {
34 switch (type) {
35 case ScalarType.STRING:
36 return value;
37 case ScalarType.BYTES: {
38 const u = unescapeBytesDefaultValue(value);
39 if (u === false) {
40 throw new Error(`cannot parse ${ScalarType[type]} default value: ${value}`);
41 }
42 return u;
43 }
44 case ScalarType.INT64:
45 case ScalarType.SFIXED64:
46 case ScalarType.SINT64:
47 return protoInt64.parse(value);
48 case ScalarType.UINT64:
49 case ScalarType.FIXED64:
50 return protoInt64.uParse(value);
51 case ScalarType.DOUBLE:
52 case ScalarType.FLOAT:
53 switch (value) {
54 case "inf":
55 return Number.POSITIVE_INFINITY;
56 case "-inf":
57 return Number.NEGATIVE_INFINITY;
58 case "nan":
59 return Number.NaN;
60 default:
61 return parseFloat(value);
62 }
63 case ScalarType.BOOL:
64 return value === "true";
65 case ScalarType.INT32:
66 case ScalarType.UINT32:
67 case ScalarType.SINT32:
68 case ScalarType.FIXED32:
69 case ScalarType.SFIXED32:
70 return parseInt(value, 10);
71 }
72 }
73 /**
74 * Parses a text-encoded default value (proto2) of a BYTES field.
75 */
76 function unescapeBytesDefaultValue(str) {
77 const b = [];
78 const input = {
79 tail: str,
80 c: "",
81 next() {
82 if (this.tail.length == 0) {
83 return false;
84 }
85 this.c = this.tail[0];
86 this.tail = this.tail.substring(1);
87 return true;
88 },
89 take(n) {
90 if (this.tail.length >= n) {
91 const r = this.tail.substring(0, n);
92 this.tail = this.tail.substring(n);
93 return r;
94 }
95 return false;
96 },
97 };
98 while (input.next()) {
99 switch (input.c) {
100 case "\\":
101 if (input.next()) {
102 switch (input.c) {
103 case "\\":
104 b.push(input.c.charCodeAt(0));
105 break;
106 case "b":
107 b.push(0x08);
108 break;
109 case "f":
110 b.push(0x0c);
111 break;
112 case "n":
113 b.push(0x0a);
114 break;
115 case "r":
116 b.push(0x0d);
117 break;
118 case "t":
119 b.push(0x09);
120 break;
121 case "v":
122 b.push(0x0b);
123 break;
124 case "0":
125 case "1":
126 case "2":
127 case "3":
128 case "4":
129 case "5":
130 case "6":
131 case "7": {
132 const s = input.c;
133 const t = input.take(2);
134 if (t === false) {
135 return false;
136 }
137 const n = parseInt(s + t, 8);
138 if (Number.isNaN(n)) {
139 return false;
140 }
141 b.push(n);
142 break;
143 }
144 case "x": {
145 const s = input.c;
146 const t = input.take(2);
147 if (t === false) {
148 return false;
149 }
150 const n = parseInt(s + t, 16);
151 if (Number.isNaN(n)) {
152 return false;
153 }
154 b.push(n);
155 break;
156 }
157 case "u": {
158 const s = input.c;
159 const t = input.take(4);
160 if (t === false) {
161 return false;
162 }
163 const n = parseInt(s + t, 16);
164 if (Number.isNaN(n)) {
165 return false;
166 }
167 const chunk = new Uint8Array(4);
168 const view = new DataView(chunk.buffer);
169 view.setInt32(0, n, true);
170 b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
171 break;
172 }
173 case "U": {
174 const s = input.c;
175 const t = input.take(8);
176 if (t === false) {
177 return false;
178 }
179 const tc = protoInt64.uEnc(s + t);
180 const chunk = new Uint8Array(8);
181 const view = new DataView(chunk.buffer);
182 view.setInt32(0, tc.lo, true);
183 view.setInt32(4, tc.hi, true);
184 b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
185 break;
186 }
187 }
188 }
189 break;
190 default:
191 b.push(input.c.charCodeAt(0));
192 }
193 }
194 return new Uint8Array(b);
195 }