AI-native design editor. Open-source Figma alternative.
fix(tauri): preserve request and abort semantics
Danila Poyarkovc2812ba1366c
diff --git a/src/app/tauri/http.ts b/src/app/tauri/http.tsindex 057548160..f3b15df11 100644--- a/src/app/tauri/http.ts+++ b/src/app/tauri/http.ts@@ -27,7 +27,10 @@ function abortReason(signal: AbortSignal): Error {}export function withAbortSignal<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {- if (signal.aborted) return Promise.reject(abortReason(signal))+ if (signal.aborted) {+ void promise.catch(() => undefined)+ return Promise.reject(abortReason(signal))+ }return new Promise<T>((resolve, reject) => {const cleanup = () => signal.removeEventListener('abort', onAbort)@@ -53,20 +56,6 @@ export function withAbortSignal<T>(promise: Promise<T>, signal: AbortSignal): Pr})}-async function bodyToBytes(body: BodyInit | null | undefined): Promise<number[] | undefined> {- if (body == null) return undefined- if (typeof body === 'string') return [...new TextEncoder().encode(body)]- if (body instanceof ArrayBuffer) return [...new Uint8Array(body)]- if (ArrayBuffer.isView(body))- return [...new Uint8Array(body.buffer, body.byteOffset, body.byteLength)]- if (body instanceof Blob) return [...new Uint8Array(await body.arrayBuffer())]- if (body instanceof URLSearchParams) return [...new TextEncoder().encode(body.toString())]- if (body instanceof FormData) {- return [...new Uint8Array(await new Response(body).arrayBuffer())]- }- throw new TypeError('Streaming request bodies are not supported by the desktop HTTP bridge yet')-}-export async function tauriFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {const request = new Request(input, init)request.signal.throwIfAborted()@@ -75,7 +64,7 @@ export async function tauriFetch(input: RequestInfo | URL, init?: RequestInit):url: request.url,method: request.method,headers: headersToProxyHeaders(request.headers),- body: await bodyToBytes(init?.body)+ body: request.body == null ? undefined : [...new Uint8Array(await request.arrayBuffer())]}request.signal.throwIfAborted()const response = await withAbortSignal(diff --git a/tests/engine/app/clipboard/figma-images.test.ts b/tests/engine/app/clipboard/figma-images.test.tsindex 69c968df6..93b57c759 100644--- a/tests/engine/app/clipboard/figma-images.test.ts+++ b/tests/engine/app/clipboard/figma-images.test.ts@@ -16,6 +16,10 @@ function pendingResponseUntilAbort(_input: RequestInfo | URL, init?: RequestInitreject(new Error('Expected request signal'))return}+ if (signal.aborted) {+ reject(signal.reason)+ return+ }signal.addEventListener('abort', () => reject(signal.reason), { once: true })})}diff --git a/tests/engine/app/tauri/http.test.ts b/tests/engine/app/tauri/http.test.tsindex db024da00..29906c3a7 100644--- a/tests/engine/app/tauri/http.test.ts+++ b/tests/engine/app/tauri/http.test.ts@@ -14,9 +14,12 @@ describe('withAbortSignal', () => {const reason = new Error('cancelled')controller.abort(reason)- await expect(- withAbortSignal(Promise.withResolvers<string>().promise, controller.signal)- ).rejects.toBe(reason)+ const pending = Promise.withResolvers<string>()+ const result = withAbortSignal(pending.promise, controller.signal)++ await expect(result).rejects.toBe(reason)+ pending.reject(new Error('late request failure'))+ await Promise.resolve()})test('rejects a pending promise when the signal aborts', async () => {