Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jsscripting] Use application/javascript as MIME type #14096

Merged
merged 4 commits into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.script.ScriptEngine;

Expand All @@ -29,6 +31,8 @@
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;

import com.oracle.truffle.js.scriptengine.GraalJSEngineFactory;

/**
* An implementation of {@link ScriptEngineFactory} with customizations for GraalJS ScriptEngines.
*
Expand All @@ -42,23 +46,21 @@
public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
private static final String CFG_INJECTION_ENABLED = "injectionEnabled";
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
private boolean injectionEnabled = true;

/*
* Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
* disrupting Nashorn scripts. When Nashorn is removed, we take over the standard
* JS runtime.
*/
private static final GraalJSEngineFactory factory = new GraalJSEngineFactory();

// GraalJSEngineFactory graalJSEngineFactory = new GraalJSEngineFactory();
//
// scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
// scriptTypes.addAll(graalJSEngineFactory.getExtensions());

public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021";
private static final String ALT_MIME_TYPE = "text/javascript;version=ECMAScript-2021";
public static final String MIME_TYPE = "application/javascript";
private static final String ALIAS = "graaljs";
private static final List<String> SCRIPT_TYPES = List.of(MIME_TYPE, ALT_MIME_TYPE, ALIAS);
private static final List<String> SCRIPT_TYPES = createScriptTypes();

private static List<String> createScriptTypes() {
// Add those for backward compatibility (existing scripts may rely on those MIME types)
List<String> backwardCompat = List.of("application/javascript;version=ECMAScript-2021", ALIAS);
return Stream.of(factory.getMimeTypes(), factory.getExtensions(), backwardCompat).flatMap(List::stream)
.collect(Collectors.toUnmodifiableList());
}

private boolean injectionEnabled = true;

private final JSScriptServiceUtil jsScriptServiceUtil;
private final JSDependencyTracker jsDependencyTracker;
Expand Down