Skip to content

Commit

Permalink
runtimes/js: better handling of void returns (encoredev#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
eandre authored Nov 1, 2024
1 parent 162662b commit d97f9af
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion runtimes/js/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ impl PromiseHandler for APIPromiseHandler {
let Some(val) = val else {
return Ok(None);
};

match parse_pvalues(val) {
Ok(val) => Ok(Some(val)),
Ok(val) => Ok(val),
Err(err) => self.error(env, err),
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtimes/js/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl PromiseHandler for AuthPromiseHandler {
fn resolve(&self, env: Env, val: Option<napi::JsUnknown>) -> Self::Output {
let Some(val) = val else { return Ok(None) };
match parse_pvalues(val) {
Ok(val) => Ok(Some(val)),
Ok(val) => Ok(val),
Err(err) => self.error(env, err),
}
}
Expand Down
8 changes: 7 additions & 1 deletion runtimes/js/src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ impl PubSubTopic {
body: JsUnknown,
source: Option<&Request>,
) -> napi::Result<JsObject> {
let payload = parse_pvalues(body).context("failed to parse payload")?;
let Some(payload) = parse_pvalues(body).context("failed to parse payload")? else {
return Err(Error::new(
Status::InvalidArg,
"no message payload provided",
));
};

let source = source.map(|s| s.inner.clone());
let fut = self.topic.publish(payload, source);
let fut = async move {
Expand Down
11 changes: 9 additions & 2 deletions runtimes/js/src/pvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ pub fn parse_pvalue(val: JsUnknown) -> Result<PValue> {
Ok(val.0)
}

pub fn parse_pvalues(val: JsUnknown) -> Result<PValues> {
pub fn parse_pvalues(val: JsUnknown) -> Result<Option<PValues>> {
if val
.get_type()
.is_ok_and(|t| t == napi::ValueType::Undefined || t == napi::ValueType::Null)
{
return Ok(None);
}

let val = PVals::from_unknown(val)?;
Ok(val.0)
Ok(Some(val.0))
}

#[allow(dead_code)]
Expand Down
10 changes: 8 additions & 2 deletions runtimes/js/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ impl Runtime {
payload: Option<JsUnknown>,
source: Option<&Request>,
) -> napi::Result<JsObject> {
let payload = payload.map(parse_pvalues).transpose()?;
let payload = match payload {
Some(payload) => parse_pvalues(payload)?,
None => None,
};
let endpoint = encore_runtime_core::EndpointName::new(service, endpoint);

let fut = self.do_api_call(endpoint, payload, source);
Expand Down Expand Up @@ -237,7 +240,10 @@ impl Runtime {
payload: Option<JsUnknown>,
source: Option<&Request>,
) -> napi::Result<JsObject> {
let payload = payload.map(parse_pvalues).transpose()?;
let payload = match payload {
Some(payload) => parse_pvalues(payload)?,
None => None,
};
let endpoint = encore_runtime_core::EndpointName::new(service, endpoint);
let source = source.map(|s| s.inner.clone());
let fut = self.runtime.api().stream(endpoint, payload, source);
Expand Down
8 changes: 7 additions & 1 deletion runtimes/js/src/websocket_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ impl WebSocketClient {
#[napi]
#[allow(dead_code)]
pub fn send(&self, msg: JsUnknown) -> napi::Result<()> {
let msg = parse_pvalues(msg)?;
let Some(msg) = parse_pvalues(msg)? else {
return Err(napi::Error::new(
napi::Status::InvalidArg,
"no message data provided",
));
};

self.inner
.send(msg)
.map_err(|e| napi::Error::new(napi::Status::Unknown, e))?;
Expand Down

0 comments on commit d97f9af

Please sign in to comment.