router
router copied to clipboard
defer: add a `map_deferred_response` in rhai
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
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.
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
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?
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