spel2js is a JavaScript library designed to parse and evaluate Spring Expression Language (SpEL) expressions within a defined context. Its primary purpose is to allow single-page applications to mirror server-side authorization logic, reducing duplication and inconsistencies in UI-related permissions. The library provides a JavaScript implementation of the SpEL parser, aiming to mimic the behavior documented for Spring Framework. It exports a singleton object containing `StandardContext` for creating evaluation contexts (which manage `authentication` and `principal` objects) and `SpelExpressionEvaluator` for compiling and executing SpEL expressions. The latest published version is 0.2.9, but significant development activity appears to have ceased around 2016 (judging by the release notes and `bower` references), indicating it is not actively maintained. It targets Node.js environments `>=8` but its age suggests potential compatibility challenges with modern JavaScript ecosystems and build tools.
npm install spel2jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up an evaluation context, define local variables, and then evaluate a Spring Expression Language (SpEL) expression using `SpelExpressionEvaluator.eval()` or a pre-compiled expression.
Review and comply with the Apache License 2.0 terms. If prior versions under MIT were used, consider implications of updating.
Thoroughly test complex SpEL expressions in JavaScript to ensure they produce identical results to their Java counterparts, especially for security-sensitive logic. Open issues for discrepancies.
Exercise caution when using in new projects. Consider forking and updating for modern compatibility, or carefully vetting for security issues. Expect manual intervention for ESM compatibility in many build environments.
Ensure the `authentication` and `principal` objects passed to `StandardContext.create()` precisely match the structure and properties expected by `spel2js` as documented or reverse-engineered from its source code, especially for properties accessed within SpEL expressions (e.g., `authentication.details.name`).
Change `const spel2js = require('spel2js');` to `import spel2js from 'spel2js';` and `const { StandardContext, SpelExpressionEvaluator } = require('spel2js');` to `import { StandardContext, SpelExpressionEvaluator } from 'spel2js';`.Remove `new` keyword. Access its members directly, e.g., `spel2js.StandardContext` or `spel2js.SpelExpressionEvaluator`.
Ensure that `authentication` and `principal` passed to `StandardContext.create()` are always valid object structures, even if empty, to avoid type validation errors within the library. Initialize them as `{}` if no specific data is available.No dependency data recorded yet.