| 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.create = create; |
| 17 |
const is_message_js_1 = require("./is-message.js"); |
| 18 |
const descriptors_js_1 = require("./descriptors.js"); |
| 19 |
const scalar_js_1 = require("./reflect/scalar.js"); |
| 20 |
const guard_js_1 = require("./reflect/guard.js"); |
| 21 |
const wrappers_js_1 = require("./wkt/wrappers.js"); |
| 22 |
// bootstrap-inject google.protobuf.Edition.EDITION_PROTO3: const $name = $number; |
| 23 |
const EDITION_PROTO3 = 999; |
| 24 |
// bootstrap-inject google.protobuf.Edition.EDITION_PROTO2: const $name = $number; |
| 25 |
const EDITION_PROTO2 = 998; |
| 26 |
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name = $number; |
| 27 |
const IMPLICIT = 2; |
| 28 |
/** |
| 29 |
* Create a new message instance. |
| 30 |
* |
| 31 |
* The second argument is an optional initializer object, where all fields are |
| 32 |
* optional. |
| 33 |
*/ |
| 34 |
function create(schema, init) { |
| 35 |
if ((0, is_message_js_1.isMessage)(init, schema)) { |
| 36 |
return init; |
| 37 |
} |
| 38 |
return compiledCreate(schema)(init); |
| 39 |
} |
| 40 |
const compiledCreates = new WeakMap(); |
| 41 |
/** |
| 42 |
* Return the compiled create function for a message, compiling it on first use. */ |
| 43 |
function compiledCreate(desc) { |
| 44 |
let compiled = compiledCreates.get(desc); |
| 45 |
if (compiled === undefined) { |
| 46 |
compiled = compileCreate(desc); |
| 47 |
compiledCreates.set(desc, compiled); |
| 48 |
} |
| 49 |
return compiled; |
| 50 |
} |
| 51 |
/** Singular field: scalar, enum, or message. */ |
| 52 |
const INIT_SINGULAR = 0; |
| 53 |
/** List field: a zero message has a fresh empty array. */ |
| 54 |
const INIT_LIST = 1; |
| 55 |
/** Map field: a zero message has a fresh empty object. */ |
| 56 |
const INIT_MAP = 2; |
| 57 |
/** Oneof group: the ADT is always stored, cases convert by case name. */ |
| 58 |
const INIT_ONEOF = 3; |
| 59 |
/* Compile the create function for this message type. */ |
| 60 |
function compileCreate(desc) { |
| 61 |
const typeName = desc.typeName; |
| 62 |
const { properties, prototype } = compileInitMessage(desc); |
| 63 |
return (init) => { |
| 64 |
let message; |
| 65 |
if (prototype !== undefined) { |
| 66 |
message = Object.create(prototype); |
| 67 |
message.$typeName = typeName; |
| 68 |
} |
| 69 |
else { |
| 70 |
message = { $typeName: typeName }; |
| 71 |
} |
| 72 |
for (let i = 0; i < properties.length; i++) { |
| 73 |
const property = properties[i]; |
| 74 |
const name = property.name; |
| 75 |
const initValue = init === null || init === void 0 ? void 0 : init[name]; |
| 76 |
switch (property.kind) { |
| 77 |
case INIT_SINGULAR: |
| 78 |
if (initValue != null) { |
| 79 |
message[name] = |
| 80 |
property.convert !== undefined |
| 81 |
? property.convert(initValue) |
| 82 |
: initValue; |
| 83 |
} |
| 84 |
else if (property.constant !== undefined) { |
| 85 |
message[name] = property.constant; |
| 86 |
} |
| 87 |
break; |
| 88 |
case INIT_LIST: |
| 89 |
message[name] = |
| 90 |
property.convert !== undefined && Array.isArray(initValue) |
| 91 |
? initValue.map(property.convert) |
| 92 |
: (initValue !== null && initValue !== void 0 ? initValue : []); |
| 93 |
break; |
| 94 |
case INIT_MAP: |
| 95 |
// Object.create(null) would be desirable for the fresh map, but is |
| 96 |
// unsupported by React: |
| 97 |
// https://react.dev/reference/react/use-server#serializable-parameters-and-return-values |
| 98 |
if (property.convert === undefined || !(0, guard_js_1.isObject)(initValue)) { |
| 99 |
message[name] = initValue !== null && initValue !== void 0 ? initValue : {}; |
| 100 |
} |
| 101 |
else { |
| 102 |
const converted = {}; |
| 103 |
const keys = Object.keys(initValue); |
| 104 |
for (let k = 0; k < keys.length; k++) { |
| 105 |
converted[keys[k]] = property.convert(initValue[keys[k]]); |
| 106 |
} |
| 107 |
message[name] = converted; |
| 108 |
} |
| 109 |
break; |
| 110 |
case INIT_ONEOF: { |
| 111 |
const oneofValue = initValue; |
| 112 |
if ((oneofValue === null || oneofValue === void 0 ? void 0 : oneofValue.case) != null) { |
| 113 |
const convert = property.convert.get(oneofValue.case); |
| 114 |
if (convert !== undefined) { |
| 115 |
message[name] = { |
| 116 |
case: oneofValue.case, |
| 117 |
value: convert(oneofValue.value), |
| 118 |
}; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
message[name] = { case: undefined }; |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
return message; |
| 128 |
}; |
| 129 |
} |
| 130 |
/** |
| 131 |
* Classify every member once, so that creating a message is a walk over a |
| 132 |
* compact list instead of a walk over the descriptor. |
| 133 |
*/ |
| 134 |
function compileInitMessage(desc) { |
| 135 |
var _a, _b; |
| 136 |
const properties = []; |
| 137 |
const prototype = {}; |
| 138 |
const usePrototype = needsPrototypeChain(desc); |
| 139 |
for (const member of desc.members) { |
| 140 |
const name = member.localName; |
| 141 |
if (member.kind == "oneof") { |
| 142 |
properties.push({ |
| 143 |
name, |
| 144 |
kind: INIT_ONEOF, |
| 145 |
constant: undefined, |
| 146 |
convert: compileConvertOneof(member), |
| 147 |
}); |
| 148 |
continue; |
| 149 |
} |
| 150 |
switch (member.fieldKind) { |
| 151 |
case "message": { |
| 152 |
// Singular message fields are absent from a zero message. |
| 153 |
properties.push({ |
| 154 |
name, |
| 155 |
kind: INIT_SINGULAR, |
| 156 |
constant: undefined, |
| 157 |
convert: compileConvertMessage(member), |
| 158 |
}); |
| 159 |
break; |
| 160 |
} |
| 161 |
case "list": { |
| 162 |
properties.push({ |
| 163 |
name, |
| 164 |
kind: INIT_LIST, |
| 165 |
constant: undefined, |
| 166 |
convert: member.listKind == "message" |
| 167 |
? ((_a = compileConvertMessage(member)) !== null && _a !== void 0 ? _a : ((value) => value)) |
| 168 |
: member.scalar == descriptors_js_1.ScalarType.BYTES |
| 169 |
? toU8Arr |
| 170 |
: undefined, |
| 171 |
}); |
| 172 |
break; |
| 173 |
} |
| 174 |
case "map": { |
| 175 |
properties.push({ |
| 176 |
name, |
| 177 |
kind: INIT_MAP, |
| 178 |
constant: undefined, |
| 179 |
convert: member.mapKind == "message" |
| 180 |
? ((_b = compileConvertMessage(member)) !== null && _b !== void 0 ? _b : ((value) => value)) |
| 181 |
: member.scalar == descriptors_js_1.ScalarType.BYTES |
| 182 |
? toU8Arr |
| 183 |
: undefined, |
| 184 |
}); |
| 185 |
break; |
| 186 |
} |
| 187 |
default: { |
| 188 |
const zeroValue = createZeroValue(member); |
| 189 |
properties.push({ |
| 190 |
name, |
| 191 |
kind: INIT_SINGULAR, |
| 192 |
constant: member.presence == IMPLICIT ? zeroValue : undefined, |
| 193 |
convert: member.fieldKind == "scalar" && member.scalar == descriptors_js_1.ScalarType.BYTES |
| 194 |
? toU8Arr |
| 195 |
: undefined, |
| 196 |
}); |
| 197 |
if (usePrototype) { |
| 198 |
prototype[name] = zeroValue; |
| 199 |
} |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
return { |
| 205 |
properties, |
| 206 |
prototype: usePrototype ? prototype : undefined, |
| 207 |
}; |
| 208 |
} |
| 209 |
/** |
| 210 |
* Compile the conversion of each case of a oneof group, keyed by case name. |
| 211 |
*/ |
| 212 |
function compileConvertOneof(oneof) { |
| 213 |
const converters = new Map(); |
| 214 |
for (const field of oneof.fields) { |
| 215 |
let convert; |
| 216 |
if (field.fieldKind == "message") { |
| 217 |
convert = compileConvertMessage(field); |
| 218 |
} |
| 219 |
else if (field.fieldKind == "scalar" && |
| 220 |
field.scalar == descriptors_js_1.ScalarType.BYTES) { |
| 221 |
convert = toU8Arr; |
| 222 |
} |
| 223 |
converters.set(field.localName, convert !== null && convert !== void 0 ? convert : ((value) => value)); |
| 224 |
} |
| 225 |
return converters; |
| 226 |
} |
| 227 |
/** |
| 228 |
* Compile the conversion of an init value for a message field, a message |
| 229 |
* list item, or a message map value. Returns undefined if values are used |
| 230 |
* as-is. |
| 231 |
*/ |
| 232 |
function compileConvertMessage(field) { |
| 233 |
if (field.fieldKind == "message" && |
| 234 |
!field.oneof && |
| 235 |
(0, wrappers_js_1.isWrapperDesc)(field.message)) { |
| 236 |
// Types from google/protobuf/wrappers.proto are unwrapped when used in |
| 237 |
// a singular field that is not part of a oneof group. |
| 238 |
return field.message.fields[0].scalar == descriptors_js_1.ScalarType.BYTES |
| 239 |
? toU8Arr |
| 240 |
: undefined; |
| 241 |
} |
| 242 |
if (field.message.typeName == "google.protobuf.Struct" && |
| 243 |
field.parent.typeName !== "google.protobuf.Value") { |
| 244 |
// google.protobuf.Struct is represented with JsonObject when used in a |
| 245 |
// field, except when used in google.protobuf.Value. |
| 246 |
return undefined; |
| 247 |
} |
| 248 |
const messageDesc = field.message; |
| 249 |
// Resolved on first use, not here: the message type can be this very field's |
| 250 |
// parent, whose create function is still being compiled. |
| 251 |
let compiled; |
| 252 |
return (value) => { |
| 253 |
if (!(0, guard_js_1.isObject)(value) || (0, is_message_js_1.isMessage)(value, messageDesc)) { |
| 254 |
return value; |
| 255 |
} |
| 256 |
compiled !== null && compiled !== void 0 ? compiled : (compiled = compiledCreate(messageDesc)); |
| 257 |
return compiled(value); |
| 258 |
}; |
| 259 |
} |
| 260 |
// converts any ArrayLike<number> to Uint8Array if necessary. |
| 261 |
function toU8Arr(value) { |
| 262 |
return Array.isArray(value) ? new Uint8Array(value) : value; |
| 263 |
} |
| 264 |
/** |
| 265 |
* Do we need the prototype chain to track field presence? |
| 266 |
*/ |
| 267 |
function needsPrototypeChain(desc) { |
| 268 |
switch (desc.file.edition) { |
| 269 |
case EDITION_PROTO3: |
| 270 |
// proto3 always uses implicit presence, we never need the prototype chain. |
| 271 |
return false; |
| 272 |
case EDITION_PROTO2: |
| 273 |
// proto2 never uses implicit presence, we always need the prototype chain. |
| 274 |
return true; |
| 275 |
default: |
| 276 |
// If a message uses scalar or enum fields with explicit presence, we need |
| 277 |
// the prototype chain to track presence. This rule does not apply to fields |
| 278 |
// in a oneof group - they use a different mechanism to track presence. |
| 279 |
return desc.fields.some((f) => f.presence != IMPLICIT && f.fieldKind != "message" && !f.oneof); |
| 280 |
} |
| 281 |
} |
| 282 |
/** |
| 283 |
* Returns the zero value for a scalar or enum field. Scalar and enum fields |
| 284 |
* can have default values. |
| 285 |
*/ |
| 286 |
function createZeroValue(field) { |
| 287 |
const defaultValue = field.getDefaultValue(); |
| 288 |
if (defaultValue !== undefined) { |
| 289 |
return field.fieldKind == "scalar" && field.longAsString |
| 290 |
? defaultValue.toString() |
| 291 |
: defaultValue; |
| 292 |
} |
| 293 |
return field.fieldKind == "scalar" |
| 294 |
? (0, scalar_js_1.scalarZeroValue)(field.scalar, field.longAsString) |
| 295 |
: field.enum.values[0].number; |
| 296 |
} |