Skip to content

Commit

Permalink
fix some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuancelin committed Jul 18, 2024
1 parent 2537a05 commit 3a6f7a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
22 changes: 15 additions & 7 deletions js-runtime/src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ function eval_externalApiCode(module, externalApiUrl, externalApiHeaders) {
}));
}
function raw_storage(operation, key, value) {
let finalValue = value;
function raw_storage(operation, key, value, opts) {
let finalValue = value || null;
if (finalValue && !(typeof finalValue === 'string' || finalValue instanceof String)) {
finalValue = JSON.stringify(finalValue);
}
const response = Http.request({
url: '${externalApiUrl}/apis/v1/storage/_rpc',
method: 'POST',
Expand All @@ -37,13 +40,14 @@ function eval_externalApiCode(module, externalApiUrl, externalApiHeaders) {
operation,
key,
value: finalValue,
opts: opts || {}
}));
let responseBody = response.body;
if (typeof responseBody === 'string' || responseBody instanceof String) {
responseBody = JSON.parse(responseBody);
}
if (response.status === 200 || response.status === 204) {
return responseBody.value || null;
if (response.status === 200 || response.status === 201 || response.status === 204) {
return responseBody ? responseBody.value || null : null;
} else {
throw new Error(responseBody.error, { cause: responseBody.error_description })
}
Expand Down Expand Up @@ -72,7 +76,7 @@ function eval_externalApiCode(module, externalApiUrl, externalApiHeaders) {
findItems: (pattern) => raw_storage('find', pattern, null),
getItem: (key) => raw_storage('get', key, null),
removeItem: (key) => raw_storage('remove', key, null),
setItem: (key, value) => raw_storage('set', key, value),
setItem: (key, value, opts) => raw_storage('set', key, value, opts),
clear: () => raw_storage('clear', null, null),
}
};
Expand All @@ -83,11 +87,15 @@ function eval_externalApiCode(module, externalApiUrl, externalApiHeaders) {
}
}

function eval_source(src, module, externalApiUrl, externalApiHeaders) {
function eval_source(src, module, env, externalApiUrl, externalApiHeaders) {
const externalApiCode = eval_externalApiCode(module, externalApiUrl, externalApiHeaders)
// TODO: remove all global objects from extism
const code = `(function() {
var exports = {};
var process = {
env: ${JSON.stringify(env)}
};
function FakeResolvedPromise(result) {
return {
Expand Down Expand Up @@ -220,7 +228,7 @@ function cloud_apim_module_plugin_execute(phase, idx) {
delete cleanedInput.module;
delete cleanedInput.externalApiUrl;
delete cleanedInput.externalApiHeaders;
const exps = eval_source(code, module, inputJson.externalApiUrl, inputJson.externalApiHeaders || {});
const exps = eval_source(code, module, inputJson.env || {}, inputJson.externalApiUrl, inputJson.externalApiHeaders || {});
if (exps[phase]) {
const result = exps[phase](cleanedInput);
if (result) {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ case class JsModulePluginConfig(
headers: Map[String, String],
externalApiUrl: Option[String],
externalApiHeaders: Option[Map[String, String]],
env: Map[String, String],
) extends NgPluginConfig {
override def json: JsValue = JsModulePluginConfig.format.writes(this)
def wasmConfig(): WasmConfig = {
Expand All @@ -88,13 +89,15 @@ object JsModulePluginConfig {
Map.empty,
None,
None,
Map.empty,
)
val format = new Format[JsModulePluginConfig] {
override def writes(o: JsModulePluginConfig): JsValue = Json.obj(
"runtime_ref" -> o.runtimeRef.map(JsString.apply).getOrElse(JsNull).asValue,
"module" -> o.module,
"module_path" -> o.modulePath,
"headers" -> o.headers,
"env" -> o.env,
"external_api_url" -> o.externalApiUrl.map(JsString.apply).getOrElse(JsNull).asValue,
"external_api_headers" -> o.externalApiHeaders.map(map => JsObject(map.mapValues(_.json))).getOrElse(JsNull).asValue,
)
Expand All @@ -106,7 +109,7 @@ object JsModulePluginConfig {
headers = json.select("headers").asOpt[Map[String, String]].getOrElse(Map.empty),
externalApiUrl = json.select("external_api_url").asOpt[String].filterNot(_.isBlank),
externalApiHeaders = json.select("external_api_headers").asOpt[Map[String, String]],

env = json.select("env").asOpt[Map[String, String]].getOrElse(Map.empty)
)
} match {
case Failure(e) => JsError(e.getMessage)
Expand Down Expand Up @@ -157,6 +160,7 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
"module_path",
"module",
"headers",
"env",
"external_api_url",
"external_api_headers",
)
Expand All @@ -178,6 +182,10 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
"type" -> "object",
"label" -> "HTTP headers",
),
"env" -> Json.obj(
"type" -> "object",
"label" -> "Env. variables",
),
"external_api_url" -> Json.obj(
"type" -> "string",
"label" -> "External API url",
Expand Down Expand Up @@ -407,6 +415,7 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
"cloud_apim_module_plugin_execute_on_validate",
Json.obj(
"code" -> code,
"env" -> pluginConfig.env,
"module" -> pluginConfig.modulePath,
"externalApiUrl" -> pluginConfig.externalApiUrl.map(_.json).getOrElse(JsNull).asValue,
"externalApiHeaders" -> pluginConfig.externalApiHeaders.map(map => JsObject(map.mapValues(_.json))).getOrElse(JsNull).asValue,
Expand Down Expand Up @@ -468,6 +477,7 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
"cloud_apim_module_plugin_execute_on_backend_call",
Json.obj(
"code" -> code,
"env" -> pluginConfig.env,
"module" -> pluginConfig.modulePath,
"externalApiUrl" -> pluginConfig.externalApiUrl.map(_.json).getOrElse(JsNull).asValue,
"externalApiHeaders" -> pluginConfig.externalApiHeaders.map(map => JsObject(map.mapValues(_.json))).getOrElse(JsNull).asValue,
Expand Down Expand Up @@ -526,6 +536,7 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
"cloud_apim_module_plugin_execute_on_request",
Json.obj(
"code" -> code,
"env" -> pluginConfig.env,
"module" -> pluginConfig.modulePath,
"externalApiUrl" -> pluginConfig.externalApiUrl.map(_.json).getOrElse(JsNull).asValue,
"externalApiHeaders" -> pluginConfig.externalApiHeaders.map(map => JsObject(map.mapValues(_.json))).getOrElse(JsNull).asValue,
Expand Down Expand Up @@ -595,6 +606,7 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
"cloud_apim_module_plugin_execute_on_response",
Json.obj(
"code" -> code,
"env" -> pluginConfig.env,
"module" -> pluginConfig.modulePath,
"externalApiUrl" -> pluginConfig.externalApiUrl.map(_.json).getOrElse(JsNull).asValue,
"externalApiHeaders" -> pluginConfig.externalApiHeaders.map(map => JsObject(map.mapValues(_.json))).getOrElse(JsNull).asValue,
Expand Down Expand Up @@ -662,6 +674,7 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
"cloud_apim_module_plugin_execute_on_error",
Json.obj(
"code" -> code,
"env" -> pluginConfig.env,
"module" -> pluginConfig.modulePath,
"externalApiUrl" -> pluginConfig.externalApiUrl.map(_.json).getOrElse(JsNull).asValue,
"externalApiHeaders" -> pluginConfig.externalApiHeaders.map(map => JsObject(map.mapValues(_.json))).getOrElse(JsNull).asValue,
Expand Down

0 comments on commit 3a6f7a7

Please sign in to comment.