davsclaus commented on code in PR #25778: URL: https://github.com/apache/camel/pull/25778#discussion_r3868981689
########## components/camel-quickjs/src/main/java/org/apache/camel/language/quickjs/QuickjsLanguage.java: ########## @@ -0,0 +1,190 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.language.quickjs; + +import java.io.ByteArrayOutputStream; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicInteger; + +import io.roastedroot.quickjs4j.core.Engine; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.Predicate; +import org.apache.camel.Service; +import org.apache.camel.spi.ScriptingLanguage; +import org.apache.camel.spi.annotations.Language; +import org.apache.camel.support.TypedLanguageSupport; + +/** + * Camel expression language for JavaScript via <a href="https://github.com/roastedroot/quickjs4j">QuickJS4J</a>. + * + * <p> + * Scripts see only JSON-serializable data bindings: {@code body}, {@code headers}, {@code properties}, and + * {@code exchangeId}. Live {@code Exchange}, {@code Message}, and {@code CamelContext} objects are not bound, so + * {@code exchange.getMessage()} is a JavaScript {@code ReferenceError} rather than Java interop. + * </p> + */ +@Language("quickjs") +public class QuickjsLanguage extends TypedLanguageSupport implements ScriptingLanguage, Service { + + private final AtomicInteger generation = new AtomicInteger(); + private final ConcurrentLinkedQueue<Engine> engines = new ConcurrentLinkedQueue<>(); + private final ThreadLocal<EngineState> engine = new ThreadLocal<>(); + + /** + * Helper for use in the Java route DSL, e.g. {@code .filter(QuickjsLanguage.quickjs("body == 'Hello'"))}. + */ + public static QuickjsExpression quickjs(String script) { + return new QuickjsExpression(script); + } + + @Override + public void start() { + // Engines are created lazily per worker thread. + } + + @Override + public void stop() { + // Invalidate ThreadLocal caches on other worker threads before closing so a later + // get() cannot reuse a closed Engine (ThreadLocal.remove() only affects this thread). + generation.incrementAndGet(); + Engine next; + while ((next = engines.poll()) != null) { Review Comment: Possible narrow shutdown race: `stop()` bumps `generation` and then drains+closes everything currently in `engines`. If another worker thread is concurrently inside `currentEngine()` creating a new engine for the *new* generation, it can add that engine to this queue right after this drain loop finishes polling — that engine would never get closed. Given `stop()` is normally called once routes have quiesced this is low-probability/low-impact (a leaked off-heap WASM engine, not a security issue), but worth a sanity check on the intended concurrency contract here. ########## components/camel-quickjs/src/main/java/org/apache/camel/language/quickjs/QuickjsHelper.java: ########## @@ -0,0 +1,258 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.language.quickjs; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.Reader; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.roastedroot.quickjs4j.core.Engine; +import io.roastedroot.quickjs4j.core.GuestException; +import io.roastedroot.quickjs4j.core.GuestFunction; +import io.roastedroot.quickjs4j.core.Invokables; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.ExpressionEvaluationException; +import org.apache.camel.ExpressionIllegalSyntaxException; +import org.apache.camel.Message; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.StreamCache; + +/** + * Helpers for evaluating JavaScript with QuickJS4J using JSON-serializable Exchange bindings. + */ +final class QuickjsHelper { + + static final String MODULE_NAME = "camelQuickjs"; + static final String FUNCTION_NAME = "camelEval"; + + /** + * Evaluates {@code script} with a copy of {@code bindings} as function parameters so {@code var} declarations do + * not leak into {@code globalThis} between Camel exchanges. Binding names must be valid JavaScript identifiers. Review Comment: This constraint (binding names must be valid JS identifiers) only affects callers of the generic `ScriptingLanguage.evaluate(script, bindings, resultType)` API — the route-DSL path via `exchangeBindings()` always uses the fixed identifiers `body`/`headers`/`properties`/`exchangeId` so it's unaffected. But for the `evaluate(bindings)` entry point, a caller-supplied key that isn't a valid identifier (hyphen, leading digit, reserved word) will surface as a raw JS `SyntaxError` from `new Function(...)` rather than a clean Camel exception. Worth documenting on the public `evaluate()` method (or in `quickjs-language.adoc`) rather than only here, and/or validating keys defensively with a clearer error message. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
