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] Support non unix file paths #11805

Merged
merged 1 commit into from
Dec 17, 2021
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,7 +14,6 @@

import static org.openhab.core.automation.module.script.ScriptEngineFactory.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.SeekableByteChannel;
Expand Down Expand Up @@ -66,7 +65,7 @@ public class OpenhabGraalJSScriptEngine extends InvocationInterceptingScriptEngi
private static final String GLOBAL_REQUIRE = "require(\"@jsscripting-globals\");";
private static final String REQUIRE_WRAPPER_NAME = "__wraprequire__";
// final CommonJS search path for our library
private static final Path LOCAL_NODE_PATH = Paths.get(File.separator + "node_modules");
private static final Path NODE_DIR = Paths.get("node_modules");

// these fields start as null because they are populated on first use
private @NonNullByDefault({}) String engineIdentifier;
Expand Down Expand Up @@ -116,10 +115,11 @@ public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> o
if (scriptDependencyListener != null) {
scriptDependencyListener.accept(path.toString());
}

if (path.toString().endsWith(".js")) {
SeekableByteChannel sbc = null;
if (path.startsWith(LOCAL_NODE_PATH)) {
InputStream is = getClass().getResourceAsStream(path.toString());
if (isRootNodePath(path)) {
InputStream is = getClass().getResourceAsStream(nodeFileToResource(path));
if (is == null) {
throw new IOException("Could not read " + path.toString());
}
Expand All @@ -137,8 +137,8 @@ public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> o
@Override
public void checkAccess(Path path, Set<? extends AccessMode> modes,
LinkOption... linkOptions) throws IOException {
if (path.startsWith(LOCAL_NODE_PATH)) {
if (getClass().getResource(path.toString()) == null) {
if (isRootNodePath(path)) {
if (getClass().getResource(nodeFileToResource(path)) == null) {
throw new NoSuchFileException(path.toString());
}
} else {
Expand All @@ -149,15 +149,15 @@ public void checkAccess(Path path, Set<? extends AccessMode> modes,
@Override
public Map<String, Object> readAttributes(Path path, String attributes,
LinkOption... options) throws IOException {
if (path.startsWith(LOCAL_NODE_PATH)) {
if (isRootNodePath(path)) {
return Collections.singletonMap("isRegularFile", true);
}
return super.readAttributes(path, attributes, options);
}

@Override
public Path toRealPath(Path path, LinkOption... linkOptions) throws IOException {
if (path.startsWith(LOCAL_NODE_PATH)) {
if (isRootNodePath(path)) {
return path;
}
return super.toRealPath(path, linkOptions);
Expand Down Expand Up @@ -210,4 +210,24 @@ protected void beforeInvocation() {
LOGGER.error("Could not inject global script", e);
}
}

/**
* Tests if this is a root node directory, `/node_modules`, `C:\node_modules`, etc...
*
* @param path
* @return
*/
private boolean isRootNodePath(Path path) {
return path.startsWith(path.getRoot().resolve(NODE_DIR));
}

/**
* Converts a root node path to a class resource path for loading local modules
*
* @param path
* @return
*/
private String nodeFileToResource(Path path) {
return "/" + NODE_DIR + "/" + path.getFileName();
}
}