| 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.reflect = reflect; |
| 17 |
exports.reflectList = reflectList; |
| 18 |
exports.reflectMap = reflectMap; |
| 19 |
const descriptors_js_1 = require("../descriptors.js"); |
| 20 |
const reflect_check_js_1 = require("./reflect-check.js"); |
| 21 |
const error_js_1 = require("./error.js"); |
| 22 |
const unsafe_js_1 = require("./unsafe.js"); |
| 23 |
const create_js_1 = require("../create.js"); |
| 24 |
const wrappers_js_1 = require("../wkt/wrappers.js"); |
| 25 |
const scalar_js_1 = require("./scalar.js"); |
| 26 |
const proto_int64_js_1 = require("../proto-int64.js"); |
| 27 |
const guard_js_1 = require("./guard.js"); |
| 28 |
const message_js_1 = require("./message.js"); |
| 29 |
/** |
| 30 |
* Create a ReflectMessage. |
| 31 |
*/ |
| 32 |
function reflect(messageDesc, message, |
| 33 |
/** |
| 34 |
* By default, field values are validated when setting them. For example, |
| 35 |
* a value for an uint32 field must be a ECMAScript Number >= 0. |
| 36 |
* |
| 37 |
* When field values are trusted, performance can be improved by disabling |
| 38 |
* checks. |
| 39 |
*/ |
| 40 |
check = true) { |
| 41 |
return new ReflectMessageImpl(messageDesc, message, check); |
| 42 |
} |
| 43 |
const messageSortedFields = new WeakMap(); |
| 44 |
class ReflectMessageImpl { |
| 45 |
get sortedFields() { |
| 46 |
const cached = messageSortedFields.get(this.desc); |
| 47 |
if (cached) { |
| 48 |
return cached; |
| 49 |
} |
| 50 |
const sortedFields = this.desc.fields |
| 51 |
.concat() |
| 52 |
.sort((a, b) => a.number - b.number); |
| 53 |
messageSortedFields.set(this.desc, sortedFields); |
| 54 |
return sortedFields; |
| 55 |
} |
| 56 |
constructor(messageDesc, message, check = true) { |
| 57 |
this.lists = new Map(); |
| 58 |
this.maps = new Map(); |
| 59 |
this.check = check; |
| 60 |
this.desc = messageDesc; |
| 61 |
this.message = this[unsafe_js_1.unsafeLocal] = message !== null && message !== void 0 ? message : (0, create_js_1.create)(messageDesc); |
| 62 |
this.fields = messageDesc.fields; |
| 63 |
this.oneofs = messageDesc.oneofs; |
| 64 |
this.members = messageDesc.members; |
| 65 |
} |
| 66 |
findNumber(number) { |
| 67 |
if (!this._fieldsByNumber) { |
| 68 |
this._fieldsByNumber = new Map(this.desc.fields.map((f) => [f.number, f])); |
| 69 |
} |
| 70 |
return this._fieldsByNumber.get(number); |
| 71 |
} |
| 72 |
oneofCase(oneof) { |
| 73 |
assertOwn(this.message, oneof); |
| 74 |
return (0, unsafe_js_1.unsafeOneofCase)(this.message, oneof); |
| 75 |
} |
| 76 |
isSet(field) { |
| 77 |
assertOwn(this.message, field); |
| 78 |
return (0, unsafe_js_1.unsafeIsSet)(this.message, field); |
| 79 |
} |
| 80 |
clear(field) { |
| 81 |
assertOwn(this.message, field); |
| 82 |
(0, unsafe_js_1.unsafeClear)(this.message, field); |
| 83 |
} |
| 84 |
get(field) { |
| 85 |
assertOwn(this.message, field); |
| 86 |
const value = (0, unsafe_js_1.unsafeGet)(this.message, field); |
| 87 |
switch (field.fieldKind) { |
| 88 |
case "list": |
| 89 |
// eslint-disable-next-line no-case-declarations |
| 90 |
let list = this.lists.get(field); |
| 91 |
if (!list || list[unsafe_js_1.unsafeLocal] !== value) { |
| 92 |
this.lists.set(field, |
| 93 |
// biome-ignore lint/suspicious/noAssignInExpressions: no |
| 94 |
(list = new ReflectListImpl(field, value, this.check))); |
| 95 |
} |
| 96 |
return list; |
| 97 |
case "map": |
| 98 |
let map = this.maps.get(field); |
| 99 |
if (!map || map[unsafe_js_1.unsafeLocal] !== value) { |
| 100 |
this.maps.set(field, |
| 101 |
// biome-ignore lint/suspicious/noAssignInExpressions: no |
| 102 |
(map = new ReflectMapImpl(field, value, this.check))); |
| 103 |
} |
| 104 |
return map; |
| 105 |
case "message": |
| 106 |
return messageToReflect(field, value, this.check); |
| 107 |
case "scalar": |
| 108 |
return (value === undefined |
| 109 |
? (0, scalar_js_1.scalarZeroValue)(field.scalar, false) |
| 110 |
: longToReflect(field, value)); |
| 111 |
case "enum": |
| 112 |
return (value !== null && value !== void 0 ? value : field.enum.values[0].number); |
| 113 |
} |
| 114 |
} |
| 115 |
set(field, value) { |
| 116 |
assertOwn(this.message, field); |
| 117 |
if (this.check) { |
| 118 |
const err = (0, reflect_check_js_1.checkField)(field, value); |
| 119 |
if (err) { |
| 120 |
throw err; |
| 121 |
} |
| 122 |
} |
| 123 |
let local; |
| 124 |
if (field.fieldKind == "message") { |
| 125 |
local = messageToLocal(field, value); |
| 126 |
} |
| 127 |
else if ((0, guard_js_1.isReflectMap)(value) || (0, guard_js_1.isReflectList)(value)) { |
| 128 |
local = value[unsafe_js_1.unsafeLocal]; |
| 129 |
} |
| 130 |
else { |
| 131 |
local = longToLocal(field, value); |
| 132 |
} |
| 133 |
(0, unsafe_js_1.unsafeSet)(this.message, field, local); |
| 134 |
} |
| 135 |
getUnknown() { |
| 136 |
return this.message.$unknown; |
| 137 |
} |
| 138 |
setUnknown(value) { |
| 139 |
this.message.$unknown = value; |
| 140 |
} |
| 141 |
} |
| 142 |
function assertOwn(owner, member) { |
| 143 |
if (member.parent.typeName !== owner.$typeName) { |
| 144 |
throw new error_js_1.FieldError(member, `cannot use ${member.toString()} with message ${owner.$typeName}`, "ForeignFieldError"); |
| 145 |
} |
| 146 |
} |
| 147 |
/** |
| 148 |
* Create a ReflectList. |
| 149 |
*/ |
| 150 |
function reflectList(field, unsafeInput, |
| 151 |
/** |
| 152 |
* By default, field values are validated when setting them. For example, |
| 153 |
* a value for an uint32 field must be a ECMAScript Number >= 0. |
| 154 |
* |
| 155 |
* When field values are trusted, performance can be improved by disabling |
| 156 |
* checks. |
| 157 |
*/ |
| 158 |
check = true) { |
| 159 |
return new ReflectListImpl(field, unsafeInput !== null && unsafeInput !== void 0 ? unsafeInput : [], check); |
| 160 |
} |
| 161 |
class ReflectListImpl { |
| 162 |
field() { |
| 163 |
return this._field; |
| 164 |
} |
| 165 |
get size() { |
| 166 |
return this._arr.length; |
| 167 |
} |
| 168 |
constructor(field, unsafeInput, check) { |
| 169 |
this._field = field; |
| 170 |
this._arr = this[unsafe_js_1.unsafeLocal] = unsafeInput; |
| 171 |
this.check = check; |
| 172 |
} |
| 173 |
get(index) { |
| 174 |
const item = this._arr[index]; |
| 175 |
return item === undefined |
| 176 |
? undefined |
| 177 |
: listItemToReflect(this._field, item, this.check); |
| 178 |
} |
| 179 |
set(index, item) { |
| 180 |
if (index < 0 || index >= this._arr.length) { |
| 181 |
throw new error_js_1.FieldError(this._field, `list item #${index + 1}: out of range`); |
| 182 |
} |
| 183 |
if (this.check) { |
| 184 |
const err = (0, reflect_check_js_1.checkListItem)(this._field, index, item); |
| 185 |
if (err) { |
| 186 |
throw err; |
| 187 |
} |
| 188 |
} |
| 189 |
this._arr[index] = listItemToLocal(this._field, item); |
| 190 |
} |
| 191 |
add(item) { |
| 192 |
if (this.check) { |
| 193 |
const err = (0, reflect_check_js_1.checkListItem)(this._field, this._arr.length, item); |
| 194 |
if (err) { |
| 195 |
throw err; |
| 196 |
} |
| 197 |
} |
| 198 |
this._arr.push(listItemToLocal(this._field, item)); |
| 199 |
return undefined; |
| 200 |
} |
| 201 |
clear() { |
| 202 |
this._arr.splice(0, this._arr.length); |
| 203 |
} |
| 204 |
[Symbol.iterator]() { |
| 205 |
return this.values(); |
| 206 |
} |
| 207 |
keys() { |
| 208 |
return this._arr.keys(); |
| 209 |
} |
| 210 |
*values() { |
| 211 |
for (const item of this._arr) { |
| 212 |
yield listItemToReflect(this._field, item, this.check); |
| 213 |
} |
| 214 |
} |
| 215 |
*entries() { |
| 216 |
for (let i = 0; i < this._arr.length; i++) { |
| 217 |
yield [i, listItemToReflect(this._field, this._arr[i], this.check)]; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
/** |
| 222 |
* Create a ReflectMap. |
| 223 |
*/ |
| 224 |
function reflectMap(field, unsafeInput, |
| 225 |
/** |
| 226 |
* By default, field values are validated when setting them. For example, |
| 227 |
* a value for an uint32 field must be a ECMAScript Number >= 0. |
| 228 |
* |
| 229 |
* When field values are trusted, performance can be improved by disabling |
| 230 |
* checks. |
| 231 |
*/ |
| 232 |
check = true) { |
| 233 |
return new ReflectMapImpl(field, unsafeInput, check); |
| 234 |
} |
| 235 |
class ReflectMapImpl { |
| 236 |
constructor(field, unsafeInput, check = true) { |
| 237 |
this.obj = this[unsafe_js_1.unsafeLocal] = unsafeInput !== null && unsafeInput !== void 0 ? unsafeInput : {}; |
| 238 |
this.check = check; |
| 239 |
this._field = field; |
| 240 |
} |
| 241 |
field() { |
| 242 |
return this._field; |
| 243 |
} |
| 244 |
set(key, value) { |
| 245 |
if (this.check) { |
| 246 |
const err = (0, reflect_check_js_1.checkMapEntry)(this._field, key, value); |
| 247 |
if (err) { |
| 248 |
throw err; |
| 249 |
} |
| 250 |
} |
| 251 |
this.obj[mapKeyToLocal(key)] = mapValueToLocal(this._field, value); |
| 252 |
return this; |
| 253 |
} |
| 254 |
delete(key) { |
| 255 |
const k = mapKeyToLocal(key); |
| 256 |
const has = Object.prototype.hasOwnProperty.call(this.obj, k); |
| 257 |
if (has) { |
| 258 |
delete this.obj[k]; |
| 259 |
} |
| 260 |
return has; |
| 261 |
} |
| 262 |
clear() { |
| 263 |
for (const key of Object.keys(this.obj)) { |
| 264 |
delete this.obj[key]; |
| 265 |
} |
| 266 |
} |
| 267 |
get(key) { |
| 268 |
let val = this.obj[mapKeyToLocal(key)]; |
| 269 |
if (val !== undefined) { |
| 270 |
val = mapValueToReflect(this._field, val, this.check); |
| 271 |
} |
| 272 |
return val; |
| 273 |
} |
| 274 |
has(key) { |
| 275 |
return Object.prototype.hasOwnProperty.call(this.obj, mapKeyToLocal(key)); |
| 276 |
} |
| 277 |
*keys() { |
| 278 |
for (const objKey of Object.keys(this.obj)) { |
| 279 |
yield mapKeyToReflect(objKey, this._field.mapKey); |
| 280 |
} |
| 281 |
} |
| 282 |
*entries() { |
| 283 |
for (const objEntry of Object.entries(this.obj)) { |
| 284 |
yield [ |
| 285 |
mapKeyToReflect(objEntry[0], this._field.mapKey), |
| 286 |
mapValueToReflect(this._field, objEntry[1], this.check), |
| 287 |
]; |
| 288 |
} |
| 289 |
} |
| 290 |
[Symbol.iterator]() { |
| 291 |
return this.entries(); |
| 292 |
} |
| 293 |
get size() { |
| 294 |
return Object.keys(this.obj).length; |
| 295 |
} |
| 296 |
*values() { |
| 297 |
for (const val of Object.values(this.obj)) { |
| 298 |
yield mapValueToReflect(this._field, val, this.check); |
| 299 |
} |
| 300 |
} |
| 301 |
forEach(callbackfn, thisArg) { |
| 302 |
for (const mapEntry of this.entries()) { |
| 303 |
callbackfn.call(thisArg, mapEntry[1], mapEntry[0], this); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
function messageToLocal(field, value) { |
| 308 |
if (!(0, guard_js_1.isReflectMessage)(value)) { |
| 309 |
return value; |
| 310 |
} |
| 311 |
if ((0, wrappers_js_1.isWrapper)(value.message) && |
| 312 |
!field.oneof && |
| 313 |
field.fieldKind == "message") { |
| 314 |
// Types from google/protobuf/wrappers.proto are unwrapped when used in |
| 315 |
// a singular field that is not part of a oneof group. |
| 316 |
return value.message.value; |
| 317 |
} |
| 318 |
if (value.desc.typeName == "google.protobuf.Struct" && |
| 319 |
field.parent.typeName != "google.protobuf.Value") { |
| 320 |
// google.protobuf.Struct is represented with JsonObject when used in a |
| 321 |
// field, except when used in google.protobuf.Value. |
| 322 |
return (0, message_js_1.wktStructToLocal)(value.message); |
| 323 |
} |
| 324 |
return value.message; |
| 325 |
} |
| 326 |
function messageToReflect(field, value, check) { |
| 327 |
if (value !== undefined) { |
| 328 |
if ((0, wrappers_js_1.isWrapperDesc)(field.message) && |
| 329 |
!field.oneof && |
| 330 |
field.fieldKind == "message") { |
| 331 |
// Types from google/protobuf/wrappers.proto are unwrapped when used in |
| 332 |
// a singular field that is not part of a oneof group. |
| 333 |
value = { |
| 334 |
$typeName: field.message.typeName, |
| 335 |
value: longToReflect(field.message.fields[0], value), |
| 336 |
}; |
| 337 |
} |
| 338 |
else if (field.message.typeName == "google.protobuf.Struct" && |
| 339 |
field.parent.typeName != "google.protobuf.Value" && |
| 340 |
(0, guard_js_1.isObject)(value)) { |
| 341 |
// google.protobuf.Struct is represented with JsonObject when used in a |
| 342 |
// field, except when used in google.protobuf.Value. |
| 343 |
value = (0, message_js_1.wktStructToReflect)(value); |
| 344 |
} |
| 345 |
} |
| 346 |
return new ReflectMessageImpl(field.message, value, check); |
| 347 |
} |
| 348 |
function listItemToLocal(field, value) { |
| 349 |
if (field.listKind == "message") { |
| 350 |
return messageToLocal(field, value); |
| 351 |
} |
| 352 |
return longToLocal(field, value); |
| 353 |
} |
| 354 |
function listItemToReflect(field, value, check) { |
| 355 |
if (field.listKind == "message") { |
| 356 |
return messageToReflect(field, value, check); |
| 357 |
} |
| 358 |
return longToReflect(field, value); |
| 359 |
} |
| 360 |
function mapValueToLocal(field, value) { |
| 361 |
if (field.mapKind == "message") { |
| 362 |
return messageToLocal(field, value); |
| 363 |
} |
| 364 |
return longToLocal(field, value); |
| 365 |
} |
| 366 |
function mapValueToReflect(field, value, check) { |
| 367 |
if (field.mapKind == "message") { |
| 368 |
return messageToReflect(field, value, check); |
| 369 |
} |
| 370 |
return value; |
| 371 |
} |
| 372 |
function mapKeyToLocal(key) { |
| 373 |
return typeof key == "string" || typeof key == "number" ? key : String(key); |
| 374 |
} |
| 375 |
/** |
| 376 |
* Converts a map key (any scalar value except float, double, or bytes) from its |
| 377 |
* representation in a message (string or number, the only possible object key |
| 378 |
* types) to the closest possible type in ECMAScript. |
| 379 |
*/ |
| 380 |
function mapKeyToReflect(key, type) { |
| 381 |
switch (type) { |
| 382 |
case descriptors_js_1.ScalarType.STRING: |
| 383 |
return key; |
| 384 |
case descriptors_js_1.ScalarType.INT32: |
| 385 |
case descriptors_js_1.ScalarType.FIXED32: |
| 386 |
case descriptors_js_1.ScalarType.UINT32: |
| 387 |
case descriptors_js_1.ScalarType.SFIXED32: |
| 388 |
case descriptors_js_1.ScalarType.SINT32: { |
| 389 |
const n = Number.parseInt(key); |
| 390 |
if (Number.isFinite(n)) { |
| 391 |
return n; |
| 392 |
} |
| 393 |
break; |
| 394 |
} |
| 395 |
case descriptors_js_1.ScalarType.BOOL: |
| 396 |
switch (key) { |
| 397 |
case "true": |
| 398 |
return true; |
| 399 |
case "false": |
| 400 |
return false; |
| 401 |
} |
| 402 |
break; |
| 403 |
case descriptors_js_1.ScalarType.UINT64: |
| 404 |
case descriptors_js_1.ScalarType.FIXED64: |
| 405 |
try { |
| 406 |
return proto_int64_js_1.protoInt64.uParse(key); |
| 407 |
} |
| 408 |
catch (_a) { |
| 409 |
// |
| 410 |
} |
| 411 |
break; |
| 412 |
default: |
| 413 |
// INT64, SFIXED64, SINT64 |
| 414 |
try { |
| 415 |
return proto_int64_js_1.protoInt64.parse(key); |
| 416 |
} |
| 417 |
catch (_b) { |
| 418 |
// |
| 419 |
} |
| 420 |
break; |
| 421 |
} |
| 422 |
return key; |
| 423 |
} |
| 424 |
function longToReflect(field, value) { |
| 425 |
switch (field.scalar) { |
| 426 |
case descriptors_js_1.ScalarType.INT64: |
| 427 |
case descriptors_js_1.ScalarType.SFIXED64: |
| 428 |
case descriptors_js_1.ScalarType.SINT64: |
| 429 |
if ("longAsString" in field && |
| 430 |
field.longAsString && |
| 431 |
typeof value == "string") { |
| 432 |
value = proto_int64_js_1.protoInt64.parse(value); |
| 433 |
} |
| 434 |
break; |
| 435 |
case descriptors_js_1.ScalarType.FIXED64: |
| 436 |
case descriptors_js_1.ScalarType.UINT64: |
| 437 |
if ("longAsString" in field && |
| 438 |
field.longAsString && |
| 439 |
typeof value == "string") { |
| 440 |
value = proto_int64_js_1.protoInt64.uParse(value); |
| 441 |
} |
| 442 |
break; |
| 443 |
} |
| 444 |
return value; |
| 445 |
} |
| 446 |
function longToLocal(field, value) { |
| 447 |
switch (field.scalar) { |
| 448 |
case descriptors_js_1.ScalarType.INT64: |
| 449 |
case descriptors_js_1.ScalarType.SFIXED64: |
| 450 |
case descriptors_js_1.ScalarType.SINT64: |
| 451 |
if ("longAsString" in field && field.longAsString) { |
| 452 |
value = String(value); |
| 453 |
} |
| 454 |
else if (typeof value == "string" || typeof value == "number") { |
| 455 |
value = proto_int64_js_1.protoInt64.parse(value); |
| 456 |
} |
| 457 |
break; |
| 458 |
case descriptors_js_1.ScalarType.FIXED64: |
| 459 |
case descriptors_js_1.ScalarType.UINT64: |
| 460 |
if ("longAsString" in field && field.longAsString) { |
| 461 |
value = String(value); |
| 462 |
} |
| 463 |
else if (typeof value == "string" || typeof value == "number") { |
| 464 |
value = proto_int64_js_1.protoInt64.uParse(value); |
| 465 |
} |
| 466 |
break; |
| 467 |
} |
| 468 |
return value; |
| 469 |
} |