mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-24 20:34:05 +02:00
Return the body of a send that saved nothing
Replaces the response-directory fallback with what the frontend already does for ephemeral sends: the engine hands the body back, because it is the only copy. Guessing at a file named for an id was the store reaching around its own abstraction, and it is exactly what must not survive the move to blob storage. The send reply carries the bytes when the engine returned them, and the runtime holds them for the rest of the call that sent them, so ctx.httpResponse.body() answers for a saved and an unsaved response the same way. Unsaved ones now report a real contentType too, taken from the response the send already handed back.
This commit is contained in:
+10
-1
@@ -557,7 +557,16 @@ export type RenderPurpose = "send" | "preview";
|
||||
|
||||
export type SendHttpRequestRequest = { httpRequest: Partial<HttpRequest>, };
|
||||
|
||||
export type SendHttpRequestResponse = { httpResponse: HttpResponse, };
|
||||
export type SendHttpRequestResponse = { httpResponse: HttpResponse,
|
||||
/**
|
||||
* The body, base64, when the send saved nothing.
|
||||
*
|
||||
* A request with no id behind it produces a response the model store never
|
||||
* sees, so it cannot be read back by id later the way a saved one can.
|
||||
* This is the only copy of it. `None` means the body was stored and should
|
||||
* be read with `read_http_response_body_chunk_request`.
|
||||
*/
|
||||
body?: string | null, };
|
||||
|
||||
export type SetKeyValueRequest = { key: string, value: string, };
|
||||
|
||||
|
||||
@@ -184,6 +184,11 @@ export interface Context {
|
||||
* Read a response's body by id. Where the host keeps the bytes — files on
|
||||
* a desktop, rows in a database, somewhere else later — is not something a
|
||||
* plugin sees or should depend on.
|
||||
*
|
||||
* Works for any response the host saved, and for one that `send` returned
|
||||
* without saving — those are held for the rest of the call that sent them,
|
||||
* since nothing else has a copy. Reaching for an unsaved response's id in a
|
||||
* later call throws, because by then its bytes are gone.
|
||||
*/
|
||||
body(args: GetHttpResponseBodyInfoRequest): Promise<HttpResponseBody>;
|
||||
};
|
||||
|
||||
@@ -609,6 +609,14 @@ export class PluginInstance {
|
||||
}
|
||||
|
||||
#newCtx(context: PluginContext): Context {
|
||||
// Bodies of sends that saved nothing, keyed by the response id they were
|
||||
// handed back with.
|
||||
//
|
||||
// A ctx is built per incoming call, so these last exactly as long as the
|
||||
// plugin invocation that produced them — which is the whole life of an
|
||||
// unsaved response. Nothing else can reach one: the host has no row for it.
|
||||
const unsavedBodies = new Map<string, { bytes: Uint8Array; contentType: string | null }>();
|
||||
|
||||
const _windowInfo = async () => {
|
||||
if (context.label == null) {
|
||||
throw new Error("Can't get window context without an active window");
|
||||
@@ -763,6 +771,18 @@ export class PluginInstance {
|
||||
return httpResponses;
|
||||
},
|
||||
body: async ({ responseId }) => {
|
||||
const unsaved = unsavedBodies.get(responseId);
|
||||
if (unsaved != null) {
|
||||
return createResponseBody(
|
||||
{
|
||||
responseId,
|
||||
contentLength: unsaved.bytes.byteLength,
|
||||
contentType: unsaved.contentType,
|
||||
},
|
||||
async (offset, length) => unsaved.bytes.slice(offset, offset + length),
|
||||
);
|
||||
}
|
||||
|
||||
const info = await this.#sendForReply<GetHttpResponseBodyInfoResponse>(
|
||||
context,
|
||||
{ type: "get_http_response_body_info_request", responseId },
|
||||
@@ -816,10 +836,23 @@ export class PluginInstance {
|
||||
type: "send_http_request_request",
|
||||
...args,
|
||||
} as const;
|
||||
const { httpResponse } = await this.#sendForReply<SendHttpRequestResponse>(
|
||||
const { httpResponse, body } = await this.#sendForReply<SendHttpRequestResponse>(
|
||||
context,
|
||||
payload,
|
||||
);
|
||||
|
||||
// A send with no request behind it saves nothing, so this reply is
|
||||
// the only copy of its body. Hold it so ctx.httpResponse.body() can
|
||||
// answer for it the same way it answers for a saved response.
|
||||
if (body != null) {
|
||||
unsavedBodies.set(httpResponse.id, {
|
||||
bytes: decodeBase64Chunk(body),
|
||||
contentType:
|
||||
httpResponse.headers.find((h) => h.name.toLowerCase() === "content-type")?.value ??
|
||||
null,
|
||||
});
|
||||
}
|
||||
|
||||
return httpResponse;
|
||||
},
|
||||
render: async (args) => {
|
||||
|
||||
Reference in New Issue
Block a user