Skip to content

Commit 8f8342d

Browse files
fix(go-feature-flag): fix loading wasm files in a jar mode.
Before this pull request the loading of the wasm module was not working when packaged in a jar. The actual 1.0.0 is not able to load the wasm file.
1 parent 1c3e1e8 commit 8f8342d

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

providers/go-feature-flag/download-wasm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
# This script copy the wasm module from the git submodule repository and adds it to the build.
44
wasm_version="v1.45.4" # {{wasm_version}}
5-
mv ./wasm-releases/evaluation/gofeatureflag-evaluation_$wasm_version.wasi ./src/main/resources/wasm/gofeatureflag-evaluation_$wasm_version.wasi
5+
cp ./wasm-releases/evaluation/gofeatureflag-evaluation_$wasm_version.wasi ./src/main/resources/wasm/gofeatureflag-evaluation.wasi

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/wasm/EvaluationWasm.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import dev.openfeature.sdk.ErrorCode;
1818
import dev.openfeature.sdk.Reason;
1919
import java.io.File;
20+
import java.io.FileOutputStream;
21+
import java.io.IOException;
22+
import java.io.InputStream;
2023
import java.net.URL;
2124
import java.nio.charset.StandardCharsets;
2225
import java.nio.file.Files;
@@ -29,7 +32,7 @@
2932
* EvaluationWasm is a class that represents the evaluation of a feature flag
3033
* it calls an external WASM module to evaluate the feature flag.
3134
*/
32-
public final class EvaluationWasm {
35+
public class EvaluationWasm {
3336
private final Instance instance;
3437
private final ExportFunction evaluate;
3538
private final ExportFunction malloc;
@@ -58,27 +61,39 @@ public EvaluationWasm() throws WasmFileNotFound {
5861
/**
5962
* getWasmFile is a function that returns the path to the WASM file.
6063
* It looks for the file in the classpath under the directory "wasm".
64+
* This method handles both file system resources and JAR-packaged resources.
6165
*
6266
* @return the path to the WASM file
6367
* @throws WasmFileNotFound - if the file is not found
6468
*/
65-
private File getWasmFile() throws WasmFileNotFound {
69+
private InputStream getWasmFile() throws WasmFileNotFound {
6670
try {
6771
ClassLoader classLoader = EvaluationWasm.class.getClassLoader();
6872
URL directoryUrl = classLoader.getResource("wasm");
6973
if (directoryUrl == null) {
7074
throw new RuntimeException("Directory not found");
7175
}
72-
Path dirPath = Paths.get(directoryUrl.toURI());
73-
try (val files = Files.list(dirPath)) {
74-
return files.filter(path -> path.getFileName().toString().startsWith("gofeatureflag-evaluation")
75-
&& (path.getFileName().toString().endsWith(".wasi")
76-
|| path.getFileName().toString().endsWith(".wasm")))
77-
.findFirst()
78-
.map(Path::toFile)
79-
.orElseThrow(
80-
() -> new RuntimeException("No file starting with 'gofeatureflag-evaluation' found"));
76+
77+
val wasmResources = classLoader.getResources("wasm");
78+
while (wasmResources.hasMoreElements()) {
79+
URL resourceUrl = wasmResources.nextElement();
80+
if ("jar".equals(resourceUrl.getProtocol()) && resourceUrl.getPath().contains("!")) {
81+
val jarPath = resourceUrl.getPath();
82+
val path = jarPath.substring(jarPath.indexOf("!") + 1);
83+
val resourceName = path + "/gofeatureflag-evaluation.wasi";
84+
val inputStream = classLoader.getResourceAsStream(resourceName);
85+
if (inputStream != null) {
86+
return inputStream;
87+
}
88+
}
89+
val inputStream = classLoader.getResourceAsStream("wasm/gofeatureflag-evaluation.wasi");
90+
if (inputStream != null) {
91+
return inputStream;
92+
}
8193
}
94+
throw new WasmFileNotFound("impossible to find the wasm file");
95+
} catch(WasmFileNotFound e) {
96+
throw e;
8297
} catch (Exception e) {
8398
throw new WasmFileNotFound(e);
8499
}
Binary file not shown.

0 commit comments

Comments
 (0)