Registry / auth-security / spel2js

spel2js

JSON →
library0.2.9jsnpmunverified

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 spel2js
INSTALL
IMPORT
SIG · SPEL2JS
S
spel2js
auth-securityjavascriptv0.2.9
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

spel2js
import spel2js from 'spel2js';
import { spel2js } from 'spel2js';
The library exports a default singleton object containing `StandardContext` and `SpelExpressionEvaluator`.
StandardContext, SpelExpressionEvaluator
import { StandardContext, SpelExpressionEvaluator } from 'spel2js';
const { StandardContext, SpelExpressionEvaluator } = require('spel2js');
While CommonJS `require` might work in older Node.js environments, modern projects should use ES module imports for direct access to these properties from the default export object.
require('spel2js')
const spel2js = require('spel2js');
import spel2js from 'spel2js';
For CommonJS environments, the entire module is imported as a singleton object. Direct named imports (`import { Name } from 'pkg'`) may not work as expected in pure CJS without transpilation or specific interop settings.

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.

import { StandardContext, SpelExpressionEvaluator } from 'spel2js'; // Mock Spring Security Authentication object and a principal object const mockAuthentication = { details: { name: 'Darth Vader' }, isAuthenticated: true }; const mockPrincipal = { username: 'dvader', roles: ['SITH_LORD', 'EMPIRE_ADMIRAL'] }; // Locals represent specific objects in the expression context const locals = { toDoList: { owner: 'Darth Vader', items: ['Destroy Alderaan', 'Build Death Star'] } }; const expression = '#toDoList.owner == authentication.details.name && authentication.isAuthenticated'; // Create an evaluation context with mock data const spelContext = StandardContext.create(mockAuthentication, mockPrincipal); // Evaluate the expression directly const result = SpelExpressionEvaluator.eval(expression, spelContext, locals); console.log(`Expression: "${expression}"`); console.log(`Result: ${result}`); // Expected: true // Example of pre-compiling an expression for reuse const compiledExpression = SpelExpressionEvaluator.compile('#toDoList.items.size() > 1'); const compiledResult = compiledExpression.eval(spelContext, locals); console.log(`Compiled expression result: ${compiledResult}`); // Expected: true
Debug
Known issues
breakingThe license for spel2js was changed from MIT to Apache License. This change affects how the software can be used and distributed, requiring adherence to the Apache License 2.0 terms.
fix
Review and comply with the Apache License 2.0 terms. If prior versions under MIT were used, consider implications of updating.
affects: >=0.2.1
gotchaSpEL2JS aims to replicate Java SpEL behavior but may exhibit subtle differences for complex expressions due to inherent differences between Java and JavaScript runtimes. The project maintainer explicitly states, 'if you come accross an expression that behaves differently than you would expect then please open an issue.'
fix
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.
affects: >=0.1.0
gotchaThe project appears to be unmaintained, with the last significant release activity around 2016. This means it may not be compatible with modern JavaScript features, build tools, or Node.js versions beyond `8.x` (as specified in `engines`). It also implies potential unaddressed security vulnerabilities or bugs.
fix
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.
affects: >=0.1.0
gotchaThe `StandardContext.create()` method expects `authentication` and `principal` arguments that mimic Spring Security's `Authentication` class and a principal object. Developers must construct these JavaScript objects manually to match the expected structure, which can be error-prone if not accurately replicated.
fix
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`).
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use CommonJS `require()` syntax in a pure ES module environment (e.g., in a modern Node.js project with `"type": "module"` in `package.json` or in a browser via modern bundlers).
fix
Change `const spel2js = require('spel2js');` to `import spel2js from 'spel2js';` and `const { StandardContext, SpelExpressionEvaluator } = require('spel2js');` to `import { StandardContext, SpelExpressionEvaluator } from 'spel2js';`.
TypeError: spel2js is not a constructor
Attempting to instantiate `spel2js` using `new spel2js()`. The library exports a pre-made singleton object, not a class or constructor.
fix
Remove `new` keyword. Access its members directly, e.g., `spel2js.StandardContext` or `spel2js.SpelExpressionEvaluator`.
Error: Invalid argument type: null. Expected: [object Object]
Passing `null` or `undefined` for `authentication` or `principal` arguments to `StandardContext.create()` when the SpEL expression expects properties from these objects, or when the library internally expects object references.
fix
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.
Upgrade
Version history
0.2.9latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
spel2js — npm install spel2js · libregistry