router icon indicating copy to clipboard operation
router copied to clipboard

defer: add a `map_deferred_response` in rhai

Open Geal opened this issue 3 years ago • 4 comments
trafficstars

rhai runs synchronously, so it cannot apply to all of the deferred responses at once. The current plan emerging from #1206 is to apply the current map_response behaviour on the headers and the first response (which should be directly available in most cases), then support another rhai callback that can be applied on deferred responses. In this way we keep the current behaviour for normal queries

Geal avatar Jun 10 '22 10:06 Geal

I don't understand why rhai would need this function, but other plugins wouldn't. Is it to allow for async cancellation?

The bit I'm struggling with is this: we'd never know (when we write a plugin) whether we should expect a query to have deferred elements or not, so preserving the current behaviour doesn't seem to yield much benefit. A plugin auther (rhai or otherwise) is always going to need to handle potentially deferred queries.

garypen avatar Jun 13 '22 12:06 garypen

rhai needs it because it is incapable of managing async code, and it won't have the same capabilities between the first response and the deferred ones: on the first response headers are still editable, while in deferred responses they will not. My first idea was actually to separate the headers and response mapping, but doing it instead with headers+first response and then adding a callback will not break the current rhai plugins

Geal avatar Jun 13 '22 13:06 Geal

Presumably the ability to edit response headers in subsequent deferred responses will also not be possible with native Rust plugins. How will we handle that in map_response for native plugins?

garypen avatar Jun 13 '22 13:06 garypen

in native plugins we can edit the headers, then map over the stream, and return a new response with the edited headers and the mapped stream

Geal avatar Jun 13 '22 13:06 Geal