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