mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-24 12:24:01 +02:00
Address response bodies by response id instead of a filesystem path (#550)
This commit is contained in:
@@ -49,6 +49,9 @@ export const platform: Platform = {
|
||||
get files() {
|
||||
return host().files;
|
||||
},
|
||||
get blobs() {
|
||||
return host().blobs;
|
||||
},
|
||||
rpc: (cmd, payload) => host().rpc(cmd, payload),
|
||||
rpcStream: (cmd, payload, onMessage) => host().rpcStream(cmd, payload, onMessage),
|
||||
listen: (event, callback) => host().listen(event, callback),
|
||||
|
||||
@@ -104,6 +104,18 @@ async function rpc<T>(cmd: string, payload?: RpcPayload): Promise<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Where this host keeps the bytes stored under an id, or null if it has none.
|
||||
*
|
||||
* The desktop stores them as files the engine wrote, so answering means asking
|
||||
* the backend. Callers hold ids and nothing else; the path exists for exactly
|
||||
* as long as it takes to open the file, which is the one thing a desktop host
|
||||
* can do that a tab cannot.
|
||||
*/
|
||||
function storedBodyPath(id: string): Promise<string | null> {
|
||||
return rpc<string | null>("cmd_http_response_body_path", { responseId: id });
|
||||
}
|
||||
|
||||
export function createTauriPlatform(): Platform {
|
||||
const window = createWindow();
|
||||
|
||||
@@ -124,13 +136,24 @@ export function createTauriPlatform(): Platform {
|
||||
},
|
||||
|
||||
files: {
|
||||
readFile: (path) => readFile(path),
|
||||
readDir: (path) => readDir(path),
|
||||
url: (path) => convertFileSrc(path),
|
||||
basename: (path) => basename(path),
|
||||
resolveResource: (path) => resolveResource(path),
|
||||
},
|
||||
|
||||
blobs: {
|
||||
async read(id) {
|
||||
const path = await storedBodyPath(id);
|
||||
return path == null ? null : readFile(path);
|
||||
},
|
||||
|
||||
async url(id) {
|
||||
const path = await storedBodyPath(id);
|
||||
return path == null ? null : convertFileSrc(path);
|
||||
},
|
||||
},
|
||||
|
||||
rpc,
|
||||
|
||||
async rpcStream<T, M>(
|
||||
|
||||
@@ -135,11 +135,10 @@ export interface PlatformDialog {
|
||||
* File-ish operations, keyed by paths the backend handed us.
|
||||
*
|
||||
* A path here is an opaque handle, not something to parse or construct: the UI
|
||||
* only ever passes one back to `readFile` or `url`. A host without a filesystem
|
||||
* only ever passes one back to `url` or `readDir`. A host without a filesystem
|
||||
* can mint handles of its own (a blob id, a URL) and stay compatible.
|
||||
*/
|
||||
export interface PlatformFiles {
|
||||
readFile(path: string): Promise<Uint8Array<ArrayBuffer>>;
|
||||
readDir(path: string): Promise<DirEntry[]>;
|
||||
|
||||
/** A URL the page can load a file from, for `<img>`, `<video>`, and friends. */
|
||||
@@ -151,12 +150,38 @@ export interface PlatformFiles {
|
||||
resolveResource(path: string): Promise<string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Content the backend stored, addressed by the id it stored it under.
|
||||
*
|
||||
* Where those bytes actually live is the one thing every host answers
|
||||
* differently — a file the engine wrote, a row in a database, a URL on the
|
||||
* other end of a socket — so finding them is the host's job and nobody else's.
|
||||
* The caller passes an id and never a location, which is also what keeps a page
|
||||
* from naming something the backend never wrote.
|
||||
*
|
||||
* What the content *means* is the app's business, not this package's.
|
||||
*/
|
||||
export interface PlatformBlobs {
|
||||
/** The bytes, or null if the host has nothing stored under that id. */
|
||||
read(id: string): Promise<Uint8Array<ArrayBuffer> | null>;
|
||||
|
||||
/**
|
||||
* A URL an element can load the content from, for `<img>`, `<video>` and
|
||||
* friends, or null if the host has nothing stored under that id.
|
||||
*
|
||||
* Asynchronous because the host may have to ask its backend where the bytes
|
||||
* are, and it only does that the moment before it reads them.
|
||||
*/
|
||||
url(id: string): Promise<string | null>;
|
||||
}
|
||||
|
||||
export interface Platform {
|
||||
readonly capabilities: PlatformCapabilities;
|
||||
readonly window: PlatformWindow;
|
||||
readonly clipboard: PlatformClipboard;
|
||||
readonly dialog: PlatformDialog;
|
||||
readonly files: PlatformFiles;
|
||||
readonly blobs: PlatformBlobs;
|
||||
|
||||
/** Call a backend command and await its result. */
|
||||
rpc<T>(cmd: string, payload?: RpcPayload): Promise<T>;
|
||||
|
||||
Reference in New Issue
Block a user