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

feat: refactor http/json support into fine-grained modules #41

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
49 changes: 49 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.extism.sdk</groupId>
<artifactId>chicory-sdk</artifactId>
<version>999-SNAPSHOT</version>
</parent>

<artifactId>chicory-sdk-core</artifactId>

<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<version>${chicory.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasi</artifactId>
<version>${chicory.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>aot-experimental</artifactId>
<version>${chicory.version}</version>
</dependency>
<dependency>
<groupId>org.extism.sdk</groupId>
<artifactId>http-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>${jimfs.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.experimental.aot.AotMachineFactory;
import com.dylibso.chicory.runtime.Instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.Parser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

/**
* A plugin that has been already processed, but not yet instantiated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

public class CurrentPlugin {
private final Plugin plugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.log.Logger;
import com.dylibso.chicory.runtime.ExportFunction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.extism.sdk.chicory.core;

public class ExtismConfigurationException extends ExtismException {

public ExtismConfigurationException(String message) {
super(message);
}

public ExtismConfigurationException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

public class ExtismException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

@FunctionalInterface
public interface ExtismFunction {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

public class ExtismFunctionException extends ExtismException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

public class ExtismTypeConversionException extends ExtismException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.wasm.types.ValueType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.wasm.types.ValueType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.wasm.types.Value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.wasm.types.Value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.log.Logger;
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.types.ValueType;
import org.extism.sdk.chicory.http.ExtismHttpException;
import org.extism.sdk.chicory.http.HttpClientAdapter;
import org.extism.sdk.chicory.http.HttpConfig;
import org.extism.sdk.chicory.http.HttpJsonCodec;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class HostEnv {

Expand All @@ -30,7 +33,7 @@ public HostEnv(Kernel kernel, ConfigProvider config, String[] allowedHosts, Http
this.config = new Config(config);
this.var = new Var();
this.log = new Log();
this.http = new Http(allowedHosts, httpConfig);
this.http = httpConfig == null ? null : new Http(allowedHosts, httpConfig);
}

public Log log() {
Expand All @@ -46,6 +49,12 @@ public Config config() {
}

public Http http() {
if (http == null) {
throw new ExtismConfigurationException(
"Http has not been configured properly. " +
"Verify you have added a dependency to the JSON deserializer (default: Jackson Databind) " +
"and you have have configure the HTTP client properly (default: java.net.http.HttpClient)");
}
return http;
}

Expand All @@ -55,7 +64,7 @@ public HostFunction[] toHostFunctions() {
log.toHostFunctions(),
var.toHostFunctions(),
config.toHostFunctions(),
http.toHostFunctions());
http == null ? new HostFunction[0] : http.toHostFunctions());
}

private HostFunction[] concat(HostFunction[]... hfs) {
Expand Down Expand Up @@ -116,7 +125,9 @@ long writeString(String s) {
public class Log {

private LogLevel logLevel = LogLevel.INFO;
private Log() {}

private Log() {
}

public void setLogLevel(LogLevel level) {
// We assume the Chicory logger is a j.u.l.Logger.
Expand Down Expand Up @@ -274,10 +285,11 @@ HostFunction[] toHostFunctions() {

}


public class Http {
private final HostPattern[] hostPatterns;
Lazy<HttpJsonCodec> jsonCodec;
Lazy<HttpClientAdapter> clientAdapter;
HttpJsonCodec jsonCodec;
HttpClientAdapter clientAdapter;

public Http(String[] allowedHosts, HttpConfig httpConfig) {
if (allowedHosts == null) {
Expand All @@ -287,16 +299,8 @@ public Http(String[] allowedHosts, HttpConfig httpConfig) {
for (int i = 0; i < allowedHosts.length; i++) {
this.hostPatterns[i] = new HostPattern(allowedHosts[i]);
}
this.jsonCodec = new Lazy<>(httpConfig.httpJsonCodec);
this.clientAdapter = new Lazy<>(httpConfig.httpClientAdapter);
}

public HttpJsonCodec jsonCodec() {
return jsonCodec.get();
}

public HttpClientAdapter clientAdapter() {
return clientAdapter.get();
this.jsonCodec = httpConfig.httpJsonCodec().get();
this.clientAdapter = httpConfig.httpClientAdapter().get();
}

long[] request(Instance instance, long... args) {
Expand All @@ -316,7 +320,7 @@ long[] request(Instance instance, long... args) {
kernel.free.apply(bodyOffset);
}

var requestMetadata = jsonCodec().decodeMetadata(requestJson);
var requestMetadata = jsonCodec.decodeMetadata(requestJson);

byte[] body = request(
requestMetadata.method(),
Expand All @@ -333,7 +337,7 @@ long[] request(Instance instance, long... args) {
return result;
}

byte[] request(String method, URI uri, Map<String, String> headers, byte[] requestBody) {
public byte[] request(String method, URI uri, Map<String, String> headers, byte[] requestBody) {
var host = uri.getHost();
if (host == null || host.isBlank()) {
throw new ExtismHttpException("HTTP request host is invalid for URI: " + uri);
Expand All @@ -342,24 +346,24 @@ byte[] request(String method, URI uri, Map<String, String> headers, byte[] reque
throw new ExtismHttpException(String.format("HTTP request to '%s' is not allowed", host));
}

return clientAdapter().request(method, uri, headers, requestBody);
return clientAdapter.request(method, uri, headers, requestBody);
}

long[] statusCode(Instance instance, long... args) {
return new long[]{statusCode()};
}

int statusCode() {
return clientAdapter().statusCode();
public int statusCode() {
return clientAdapter.statusCode();
}

long[] headers(Instance instance, long[] longs) {
var result = new long[1];
var headers = clientAdapter().headers();
var headers = clientAdapter.headers();
if (headers == null) {
return result;
}
var bytes = jsonCodec().encodeHeaders(Map.of());
var bytes = jsonCodec.encodeHeaders(Map.of());
result[0] = memory().writeBytes(bytes);
return result;
}
Expand Down Expand Up @@ -419,26 +423,4 @@ public boolean matches(String host) {
}
}

private static final class Lazy<T> {
final Supplier<T> supplier;
T t;
public Lazy(Supplier<T> supplier) {
this.supplier = supplier;
}
public T get() {
if (t == null) {
try {
t = supplier.get();
} catch (NoClassDefFoundError error) {
throw new ConfigurationException(
"Http has not been configured properly. " +
"Verify you have added a dependency to the JSON deserializer (default: Jackson Databind) " +
"and you have have configure the HTTP client properly (default: java.net.http.HttpClient)", error);
}
}
return t;
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.runtime.ExportFunction;
import com.dylibso.chicory.runtime.HostFunction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.log.Logger;
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasi.WasiOptions;
import com.dylibso.chicory.wasi.WasiPreview1;
import org.extism.sdk.chicory.http.HttpConfig;

import java.util.Arrays;
import java.util.Map;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.log.Logger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.wasi.WasiOptions;
import org.extism.sdk.chicory.http.HttpConfig;

import java.util.EnumSet;
import java.util.List;
Expand All @@ -18,7 +19,7 @@ public static class Options {
ConfigProvider config = ConfigProvider.empty();
WasiOptions wasiOptions = WasiOptions.builder().build();
String[] allowedHosts = new String[0];
HttpConfig httpConfig = HttpConfig.defaultConfig();
HttpConfig httpConfig = null;

public Options withAoT() {
return withAoT(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;


import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.log.Logger;
import com.dylibso.chicory.log.SystemLogger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.log.SystemLogger;
import com.dylibso.chicory.runtime.Instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.extism.sdk.chicory;
package org.extism.sdk.chicory.core;

import com.dylibso.chicory.log.SystemLogger;
import com.dylibso.chicory.runtime.HostFunction;
Expand Down
Loading