UNPKG

@bufbuild/cel

Version:

A CEL evaluator for ECMAScript

116 lines (115 loc) 4.09 kB
"use strict"; // Copyright 2024-2025 Buf Technologies, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. Object.defineProperty(exports, "__esModule", { value: true }); exports.equals = equals; const reflect_1 = require("@bufbuild/protobuf/reflect"); const eval_js_1 = require("./eval.js"); const protobuf_1 = require("@bufbuild/protobuf"); const list_js_1 = require("./list.js"); const map_js_1 = require("./map.js"); const uint_js_1 = require("./uint.js"); const type_js_1 = require("./type.js"); /** * Checks for equality of two CEL values. It follows the following rules: * * - Numeric values (`int`, `uint`, and `double`) are compared across types. * - `NaN` does not equal `NaN`. * - Other scalar values (`bool`, `string`, `bytes`, and `type`) are only equal if type and value are identical. * - `google.protobuf.Any` is unpacked before comparison. * - Lists are equal if lengths match and each element matches the corresponding element in the other one. * - Maps are equal if both of them have the same set of keys and corresponding values. * - If the types don't match it returns false. * * Ref: https://github.com/google/cel-spec/blob/v0.24.0/doc/langdef.md#equality */ function equals(lhs, rhs) { // Fast path for Int, Double, Bool, and String types. if (lhs === rhs) { return true; } if ((0, uint_js_1.isCelUint)(lhs)) { lhs = lhs.value; } if ((0, uint_js_1.isCelUint)(rhs)) { rhs = rhs.value; } // Check for scalars that are of different type and unwrapped values if ((typeof lhs === "number" || typeof lhs === "bigint") && (typeof rhs === "number" || typeof rhs === "bigint")) { return lhs == rhs; } // For values whose types must be equal for them to be equal. switch (true) { case lhs instanceof Uint8Array: return rhs instanceof Uint8Array && equalsBytes(lhs, rhs); case (0, list_js_1.isCelList)(lhs): return (0, list_js_1.isCelList)(rhs) && equalsList(lhs, rhs); case (0, map_js_1.isCelMap)(lhs): return (0, map_js_1.isCelMap)(rhs) && equalsMap(lhs, rhs); case (0, type_js_1.isCelType)(lhs): return (0, type_js_1.isCelType)(rhs) && lhs.name === rhs.name; } // Messages if ((0, reflect_1.isReflectMessage)(lhs)) { if (!(0, reflect_1.isReflectMessage)(rhs)) { return false; } if (lhs.desc.typeName !== rhs.desc.typeName) { return false; } // see https://github.com/bufbuild/protobuf-es/pull/1029 return (0, protobuf_1.equals)(lhs.desc, lhs.message, rhs.message, { registry: (0, eval_js_1.getEvalContext)().registry, unpackAny: true, unknown: true, extensions: true, }); } return false; } function equalsList(lhs, rhs) { if (lhs.size !== rhs.size) { return false; } for (let i = 0; i < lhs.size; i++) { if (!equals(lhs.get(i), rhs.get(i))) { return false; } } return true; } function equalsMap(lhs, rhs) { if (lhs.size !== rhs.size) { return false; } for (const [k, v] of lhs) { const rv = rhs.get(k); if (rv === undefined || !equals(v, rv)) { return false; } } return true; } function equalsBytes(lhs, rhs) { if (lhs.length !== rhs.length) { return false; } for (let i = 0; i < lhs.length; i++) { if (lhs[i] !== rhs[i]) { return false; } } return true; }