From 2a425b0f9bd1c8d17f13a23bc1b28fbc4add9acf Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 15 Apr 2024 14:22:00 +0530 Subject: [PATCH] Add duplex parameter to a streaming request Attempt to solve the following error in the browser's console when trying to make the request: [error] download and save failed: TypeError: Failed to construct 'Request': The `duplex` member must be specified for a request with a streaming body --- web/apps/photos/src/services/export/index.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts index 1a4d339aa5..99e2ac1a81 100644 --- a/web/apps/photos/src/services/export/index.ts +++ b/web/apps/photos/src/services/export/index.ts @@ -996,10 +996,26 @@ class ExportService { // TODO(MR): Productionalize if (isDevBuild) { console.log({ a: "will send req", updatedFileStream }); + // The duplex parameter needs to be set to 'half' when + // streaming requests. + // + // Currently browsers, and specifically in our case, + // since this code runs only within our desktop + // (Electron) app, Chromium, don't support 'full' duplex + // mode (i.e. streaming both the request and the + // response). + // + // https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests + // + // In another twist, the TypeScript libdom.d.ts does not + // include the "duplex" parameter, so we need to cast to + // get TypeScript to let this code through. e.g. see + // https://github.com/node-fetch/node-fetch/issues/1769 const req = new Request("stream://foo", { method: "POST", body: updatedFileStream, - }); + duplex: "half", + } as unknown as RequestInit); const res = await fetch(req); console.log({ a: "got res", res }); }