Skip to content

Commit 3258dcb

Browse files
committed
infer codec in main.rs, only check for v2
1 parent a804934 commit 3258dcb

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/engine.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ pub struct ProfileOpts {
2020
#[folder = "providers/"]
2121
struct StandardProviders;
2222

23-
fn uses_msgpack_provider(module: &Module) -> bool {
23+
pub fn uses_msgpack_provider(module: &Module) -> bool {
2424
let imported_modules: HashSet<String> =
2525
module.imports().map(|i| i.module().to_string()).collect();
2626

27-
imported_modules.iter().any(|name| {
28-
name.starts_with("shopify_function_v")
29-
|| (name.starts_with("shopify_functions_javy_v") && name != "shopify_functions_javy_v1")
30-
})
27+
imported_modules
28+
.iter()
29+
.any(|name| name.starts_with("shopify_function_v") || name == "shopify_functions_javy_v2")
3130
}
3231

3332
fn import_modules<T>(
@@ -139,13 +138,8 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
139138
let module = Module::from_file(&engine, &function_path)
140139
.map_err(|e| anyhow!("Couldn't load the Function {:?}: {}", &function_path, e))?;
141140

142-
let uses_msgpack = uses_msgpack_provider(&module);
143-
let input_stream = if input.codec == Codec::Messagepack || uses_msgpack {
144-
let msgpack_bytes = rmp_serde::to_vec(&input.json_value.as_ref().unwrap())?;
145-
MemoryInputPipe::new(msgpack_bytes)
146-
} else {
147-
MemoryInputPipe::new(input.raw.clone())
148-
};
141+
// We now infer the codec in main.rs, so the input should already have the right format
142+
let input_stream = MemoryInputPipe::new(input.raw.clone());
149143
let output_stream = MemoryOutputPipe::new(usize::MAX);
150144
let error_stream = MemoryOutputPipe::new(usize::MAX);
151145

@@ -220,18 +214,14 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
220214

221215
logs.extend_from_slice(error_logs.as_bytes());
222216

223-
// When codec is not specified, we infer it from the input
224-
let inferered_codec = if uses_msgpack {
225-
Codec::Messagepack
226-
} else {
227-
input.codec
228-
};
217+
// The input codec is already inferred in main.rs, use it directly
218+
let output_codec = input.codec;
229219
let raw_output = output_stream
230220
.try_into_inner()
231221
.expect("Output stream reference still exists");
232222
let output = BytesContainer::new(
233223
BytesContainerType::Output,
234-
inferered_codec,
224+
output_codec,
235225
raw_output.to_vec(),
236226
)?;
237227

src/main.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use function_runner::{BytesContainer, BytesContainerType, Codec};
2+
use wasmtime::{Config, Engine, Module};
23

34
use std::{
45
fs::File,
@@ -135,7 +136,26 @@ fn main() -> Result<()> {
135136

136137
let query_string = opts.read_query_to_string().transpose()?;
137138

138-
let input = BytesContainer::new(BytesContainerType::Input, opts.codec, buffer)?;
139+
let engine = Engine::new(
140+
Config::new()
141+
.wasm_multi_memory(true)
142+
.wasm_threads(false)
143+
.consume_fuel(true)
144+
.epoch_interruption(true),
145+
)?;
146+
147+
// Load the module to check if it uses messagepack
148+
let module = Module::from_file(&engine, &opts.function)
149+
.map_err(|e| anyhow!("Couldn't load the Function {:?}: {}", &opts.function, e))?;
150+
151+
// Infer codec from the module if it uses msgpack, otherwise use the codec from the command line
152+
let inferred_codec = if function_runner::engine::uses_msgpack_provider(&module) {
153+
Codec::Messagepack
154+
} else {
155+
opts.codec
156+
};
157+
158+
let input = BytesContainer::new(BytesContainerType::Input, inferred_codec, buffer)?;
139159
let scale_factor = if let (Some(schema_string), Some(query_string), Some(json_value)) =
140160
(schema_string, query_string, input.json_value.clone())
141161
{

0 commit comments

Comments
 (0)