@@ -9,6 +9,7 @@ import * as Effect from "effect/Effect";
99import * as Layer from "effect/Layer" ;
1010import * as Option from "effect/Option" ;
1111import { vi } from "vite-plus/test" ;
12+ import type * as Electron from "electron" ;
1213
1314const { focusedWebContents, ownerWindow } = vi . hoisted ( ( ) => ( {
1415 focusedWebContents : vi . fn ( ) ,
@@ -33,6 +34,7 @@ import {
3334 installRelayTelemetryEnabled ,
3435 pickProjectFavicon ,
3536 probeRemoteEditors ,
37+ readMedia ,
3638} from "./window.ts" ;
3739
3840const readyWslConfig : DesktopBackendManager . DesktopBackendStartConfig = {
@@ -234,6 +236,54 @@ describe("installRelayTelemetryEnabled", () => {
234236 ) ;
235237} ) ;
236238
239+ describe ( "readMedia" , ( ) => {
240+ it . effect ( "reads http(s) media only for the main window" , ( ) =>
241+ Effect . gen ( function * ( ) {
242+ const mib = new Uint8Array ( 1024 * 1024 ) ;
243+ const fetch = vi . fn ( async ( url : string ) => {
244+ if ( url . endsWith ( "/missing.png" ) ) return new Response ( "Not Found" , { status : 404 } ) ;
245+ if ( url . endsWith ( "/small.png" ) ) return new Response ( new Uint8Array ( [ 1 , 2 ] ) ) ;
246+ let sent = 0 ;
247+ // 257 MiB from one reused chunk: over the cap without allocating it.
248+ const huge = new ReadableStream < Uint8Array > ( {
249+ pull : ( controller ) => ( sent ++ < 257 ? controller . enqueue ( mib ) : controller . close ( ) ) ,
250+ } ) ;
251+ return new Response ( huge ) ;
252+ } ) ;
253+ const main = {
254+ webContents : { id : 1 , session : { fetch } } ,
255+ } as unknown as Electron . BrowserWindow ;
256+ const read = ( url : string , senderId : number ) =>
257+ readMedia
258+ . handler ( url , { sender : { id : senderId } } )
259+ . pipe (
260+ Effect . provide (
261+ Layer . mock ( ElectronWindow . ElectronWindow ) ( { main : Effect . succeedSome ( main ) } ) ,
262+ ) ,
263+ ) ;
264+
265+ assert . strictEqual ( yield * read ( "https://example.com/a.png" , 2 ) , null ) ;
266+ assert . strictEqual ( yield * read ( "file:///etc/passwd" , 1 ) , null ) ;
267+ assert . strictEqual ( fetch . mock . calls . length , 0 ) ;
268+ assert . deepEqual ( yield * read ( "https://example.com/small.png" , 1 ) , {
269+ status : 200 ,
270+ mimeType : "" ,
271+ data : Buffer . from ( [ 1 , 2 ] ) ,
272+ } ) ;
273+ assert . deepEqual ( yield * read ( "https://example.com/missing.png" , 1 ) , {
274+ status : 404 ,
275+ mimeType : "text/plain;charset=UTF-8" ,
276+ data : new Uint8Array ( ) ,
277+ } ) ;
278+ assert . deepEqual ( yield * read ( "https://example.com/huge.mp4" , 1 ) , {
279+ status : 413 ,
280+ mimeType : "" ,
281+ data : new Uint8Array ( ) ,
282+ } ) ;
283+ } ) ,
284+ ) ;
285+ } ) ;
286+
237287describe ( "pickProjectFavicon" , ( ) => {
238288 const pickerLayer = (
239289 pickFiles : ( ) => Effect . Effect < Array < string > > ,
0 commit comments