UNPKG

@bufbuild/cel

Version:

A CEL evaluator for ECMAScript

67 lines (66 loc) 2.02 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.setEvalContext = setEvalContext; exports.getEvalContext = getEvalContext; exports.withEvalContext = withEvalContext; exports.getMsgDesc = getMsgDesc; const contextStack = []; /** * Sets the EvalContext for the current execution scope. */ function setEvalContext(context) { contextStack.push(context); return () => contextStack.pop(); } /** * Gets the current EvalContext. * * Throws an error if there isn't one. */ function getEvalContext() { if (contextStack.length === 0) { throw new Error("cannot use `getEvalContext` outside of an evaluation"); } return contextStack[contextStack.length - 1]; } /** * Returns an Interpretable that sets the context for the given call. */ function withEvalContext(context, next) { return { id: next.id, eval(ctx) { const unset = setEvalContext(context); try { return next.eval(ctx); } finally { unset(); } }, }; } /** * Returns a message descriptor with the matching type name * from the evaluation context. */ function getMsgDesc(typeName) { const schema = getEvalContext().registry.getMessage(typeName); if (!schema) { throw new Error(`Message ${typeName} not found in registry`); } return schema; }