Skip to content

Commit

Permalink
otoroshi wasm response transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jun 19, 2024
1 parent c6024ea commit d7bfcb3
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
export function execute() {
let context = JSON.parse(Host.inputString());

if (context.request.headers["foo"] === "bar") {
const out = {
result: true
};
Host.outputString(JSON.stringify(out));
} else {
const error = {
result: false,
error: {
message: "you're not authorized",
status: 401
}
};
Host.outputString(JSON.stringify(error));
}

return 0;
}
let context = JSON.parse(Host.inputString())

Host.outputString(JSON.stringify({
...context,
status: 200,
headers: {
...context.otoroshi_response.headers,
OTOROSHI_WASM_PLUGIN_ID: "OTOROSHI_WASM_RESPONSE_TRANSFORMER",
"Content-Type": "application/json"
},
body_json: {
foo: "bar"
},
// cookies
}))

return 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { WasmRequestTransformerContext, WasmTransformerResponse } from 'otoroshi-ts-types';

export declare var Host: any;

export function execute() {
let context = JSON.parse(Host.inputString()) as WasmRequestTransformerContext;

const error: WasmTransformerResponse = {
...context,
status: 200,
headers: {
...context.otoroshi_request.headers,
OTOROSHI_WASM_PLUGIN_ID: "OTOROSHI_WASM_RESPONSE_TRANSFORMER",
"Content-Type": "application/json"
},
body_json: {
foo: "bar"
}
}
Host.outputString(JSON.stringify(error));

return 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use extism_pdk::*;
use otoroshi_rust_types::types;

#[plugin_fn]
pub fn execute(
Json(context): Json<types::WasmResponseTransformerContext>,
) -> FnResult<Json<types::WasmResponse>> {
let mut out_headers = context.request.headers;
out_headers.insert(
"OTOROSHI_WASM_PLUGIN_ID".to_string(),
"OTOROSHI_WASM_RESPONSE_TRANSFORMER".to_string(),
);
out_headers.insert("Content-Type".to_string(), "text/plain".to_string());

let out = types::WasmResponse {
status: Some(200),
error: None,
body_str: Some("{ \"foo\": \"bar\" }".to_string()),
headers: out_headers,
cookies: serde_json::Value::Null,
body_base64: None,
body_bytes: None,
body_json: None,
};

Ok(Json(out))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"encoding/json"

"github.com/buger/jsonparser"
"github.com/extism/go-pdk"
// "github.com/MAIF/otoroshi-go-types"
)

//export execute
func execute() int32 {
input := pdk.Input()

var headers, dataType, offset, err = jsonparser.Get(input, "request", "headers")

_ = dataType
_ = offset

var outHeaders map[string]interface{}

// Unmarshal the JSON data into the map
err = json.Unmarshal(headers, &outHeaders)
if err != nil {

}

outHeaders["Content-Type"] = "application/json"
outHeaders["OTOROSHI_WASM_PLUGIN_ID"] = "OTOROSHI_WASM_RESPONSE_TRANSFORMER"

if err != nil {
}

jsonHeaders, marshallingError := json.Marshal(outHeaders)
if marshallingError != nil {

}

output := `{
"status": 200,
"headers": ` + string(jsonHeaders) + `,
"body_json": { "foo": "bar" }
}`

mem := pdk.AllocateString(output)
pdk.OutputMemory(mem)

return 0
}

func main() {}
2 changes: 1 addition & 1 deletion server/templates/otoroshi/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ wasi = false
extism-pdk = "1.0.1"
serde = "1.0.152"
serde_json = "1.0.91"
otoroshi_rust_types = "0.0.5"
otoroshi_rust_types = "1.0.0"

[lib]
crate_type = ["cdylib"]
Expand Down
2 changes: 1 addition & 1 deletion server/templates/otoroshi/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "@@PLUGIN_VERSION@@",
"devDependencies": {
"esbuild": "^0.17.9",
"otoroshi-ts-types": "1.0.0"
"otoroshi-ts-types": "1.0.2"
}
}
1 change: 1 addition & 0 deletions server/templates/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ wasi = false
extism-pdk = "1.0.1"
serde = "1.0.152"
serde_json = "1.0.91"
otoroshi_rust_types = "1.0.0"

[lib]
crate_type = ["cdylib"]
Expand Down
2 changes: 1 addition & 1 deletion server/templates/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "@@PLUGIN_VERSION@@",
"devDependencies": {
"esbuild": "^0.17.9",
"otoroshi-ts-types": "1.0.0"
"otoroshi-ts-types": "1.0.2"
}
}

0 comments on commit d7bfcb3

Please sign in to comment.