Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/equals.js

raw
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.equals = equals;
17 const scalar_js_1 = require("./reflect/scalar.js");
18 const reflect_js_1 = require("./reflect/reflect.js");
19 const descriptors_js_1 = require("./descriptors.js");
20 const index_js_1 = require("./wkt/index.js");
21 const extensions_js_1 = require("./extensions.js");
22 /**
23 * Compare two messages of the same type for equality.
24 *
25 * Fields are compared one by one, but only when set: a field set in one message
26 * but not the other makes them unequal, while fields set in both are compared
27 * by value. Extensions and unknown fields are disregarded by default.
28 *
29 * Float and double values are compared with IEEE semantics: NaN does not equal
30 * NaN, and -0 equals 0 (with the exception for singular fields with implicit
31 * presence: -0 is set, +0 is unset).
32 */
33 function equals(schema, a, b, options) {
34 if (a.$typeName != schema.typeName || b.$typeName != schema.typeName) {
35 return false;
36 }
37 if (a === b) {
38 return true;
39 }
40 return reflectEquals((0, reflect_js_1.reflect)(schema, a), (0, reflect_js_1.reflect)(schema, b), options);
41 }
42 function reflectEquals(a, b, opts) {
43 if (a.desc.typeName === "google.protobuf.Any" && (opts === null || opts === void 0 ? void 0 : opts.unpackAny) == true) {
44 return anyUnpackedEquals(a.message, b.message, opts);
45 }
46 for (const f of a.fields) {
47 if (!fieldEquals(f, a, b, opts)) {
48 return false;
49 }
50 }
51 if ((opts === null || opts === void 0 ? void 0 : opts.unknown) == true && !unknownEquals(a, b, opts.registry)) {
52 return false;
53 }
54 if ((opts === null || opts === void 0 ? void 0 : opts.extensions) == true && !extensionsEquals(a, b, opts)) {
55 return false;
56 }
57 return true;
58 }
59 // TODO(tstamm) add an option to consider NaN equal to NaN?
60 function fieldEquals(f, a, b, opts) {
61 if (!a.isSet(f) && !b.isSet(f)) {
62 return true;
63 }
64 if (!a.isSet(f) || !b.isSet(f)) {
65 return false;
66 }
67 switch (f.fieldKind) {
68 case "scalar":
69 return (0, scalar_js_1.scalarEquals)(f.scalar, a.get(f), b.get(f));
70 case "enum":
71 return a.get(f) === b.get(f);
72 case "message":
73 return reflectEquals(a.get(f), b.get(f), opts);
74 case "map": {
75 // TODO(tstamm) can't we compare sizes first?
76 const mapA = a.get(f);
77 const mapB = b.get(f);
78 const keys = [];
79 for (const k of mapA.keys()) {
80 if (!mapB.has(k)) {
81 return false;
82 }
83 keys.push(k);
84 }
85 for (const k of mapB.keys()) {
86 if (!mapA.has(k)) {
87 return false;
88 }
89 }
90 for (const key of keys) {
91 const va = mapA.get(key);
92 const vb = mapB.get(key);
93 if (va === vb) {
94 continue;
95 }
96 switch (f.mapKind) {
97 case "enum":
98 return false;
99 case "message":
100 if (!reflectEquals(va, vb, opts)) {
101 return false;
102 }
103 break;
104 case "scalar":
105 if (!(0, scalar_js_1.scalarEquals)(f.scalar, va, vb)) {
106 return false;
107 }
108 break;
109 }
110 }
111 break;
112 }
113 case "list": {
114 const listA = a.get(f);
115 const listB = b.get(f);
116 if (listA.size != listB.size) {
117 return false;
118 }
119 for (let i = 0; i < listA.size; i++) {
120 const va = listA.get(i);
121 const vb = listB.get(i);
122 if (va === vb) {
123 continue;
124 }
125 switch (f.listKind) {
126 case "enum":
127 return false;
128 case "message":
129 if (!reflectEquals(va, vb, opts)) {
130 return false;
131 }
132 break;
133 case "scalar":
134 if (!(0, scalar_js_1.scalarEquals)(f.scalar, va, vb)) {
135 return false;
136 }
137 break;
138 }
139 }
140 break;
141 }
142 }
143 return true;
144 }
145 function anyUnpackedEquals(a, b, opts) {
146 if (a.typeUrl !== b.typeUrl) {
147 return false;
148 }
149 const unpackedA = (0, index_js_1.anyUnpack)(a, opts.registry);
150 const unpackedB = (0, index_js_1.anyUnpack)(b, opts.registry);
151 if (unpackedA && unpackedB) {
152 const schema = opts.registry.getMessage(unpackedA.$typeName);
153 if (schema) {
154 return equals(schema, unpackedA, unpackedB, opts);
155 }
156 }
157 return (0, scalar_js_1.scalarEquals)(descriptors_js_1.ScalarType.BYTES, a.value, b.value);
158 }
159 function unknownEquals(a, b, registry) {
160 function getTrulyUnknown(msg, registry) {
161 var _a;
162 const u = (_a = msg.getUnknown()) !== null && _a !== void 0 ? _a : [];
163 return registry
164 ? u.filter((uf) => !registry.getExtensionFor(msg.desc, uf.no))
165 : u;
166 }
167 const unknownA = getTrulyUnknown(a, registry);
168 const unknownB = getTrulyUnknown(b, registry);
169 if (unknownA.length != unknownB.length) {
170 return false;
171 }
172 for (let i = 0; i < unknownA.length; i++) {
173 const a = unknownA[i];
174 const b = unknownB[i];
175 if (a.no != b.no) {
176 return false;
177 }
178 if (a.wireType != b.wireType) {
179 return false;
180 }
181 if (!(0, scalar_js_1.scalarEquals)(descriptors_js_1.ScalarType.BYTES, a.data, b.data)) {
182 return false;
183 }
184 }
185 return true;
186 }
187 function extensionsEquals(a, b, opts) {
188 function getSetExtensions(msg, registry) {
189 var _a;
190 return ((_a = msg.getUnknown()) !== null && _a !== void 0 ? _a : [])
191 .map((uf) => registry.getExtensionFor(msg.desc, uf.no))
192 .filter((e) => e != undefined)
193 .filter((e, index, arr) => arr.indexOf(e) === index);
194 }
195 const extensionsA = getSetExtensions(a, opts.registry);
196 const extensionsB = getSetExtensions(b, opts.registry);
197 if (extensionsA.length != extensionsB.length ||
198 extensionsA.some((e) => !extensionsB.includes(e))) {
199 return false;
200 }
201 for (const extension of extensionsA) {
202 const [containerA, field] = (0, extensions_js_1.createExtensionContainer)(extension, (0, extensions_js_1.getExtension)(a.message, extension));
203 const [containerB] = (0, extensions_js_1.createExtensionContainer)(extension, (0, extensions_js_1.getExtension)(b.message, extension));
204 if (!fieldEquals(field, containerA, containerB, opts)) {
205 return false;
206 }
207 }
208 return true;
209 }