1+ import { Readable } from "node:stream" ;
12import { Data , Effect , Option , Stream } from "effect" ;
23import type {
34 Dialog ,
@@ -8,7 +9,7 @@ import type {
89 Response ,
910 Worker ,
1011} from "playwright-core" ;
11- import type { PlaywrightError } from "./errors" ;
12+ import { type PlaywrightError , wrapError } from "./errors" ;
1213import { PlaywrightFrame , type PlaywrightFrameService } from "./frame" ;
1314import { PlaywrightPage , type PlaywrightPageService } from "./page" ;
1415import type { PageFunction } from "./playwright-types" ;
@@ -262,7 +263,12 @@ export class PlaywrightFileChooser extends Data.TaggedClass(
262263 */
263264export class PlaywrightDownload extends Data . TaggedClass ( "PlaywrightDownload" ) < {
264265 cancel : Effect . Effect < void , PlaywrightError > ;
265- createReadStream : Stream . Stream < Uint8Array , PlaywrightError > ;
266+ /**
267+ * Creates a stream of the download data.
268+ * @category custom
269+ * @since 0.2.0
270+ */
271+ stream : Stream . Stream < Uint8Array , PlaywrightError > ;
266272 delete : Effect . Effect < void , PlaywrightError > ;
267273 failure : Effect . Effect < Option . Option < string | null > , PlaywrightError > ;
268274 page : ( ) => PlaywrightPageService ;
@@ -279,8 +285,17 @@ export class PlaywrightDownload extends Data.TaggedClass("PlaywrightDownload")<{
279285
280286 return new PlaywrightDownload ( {
281287 cancel : use ( ( ) => download . cancel ( ) ) ,
282- /** TODO: implement createReadStream / effect wrapper for it */
283- createReadStream : Stream . empty ,
288+ stream : use ( ( ) =>
289+ download . createReadStream ( ) . then ( ( s ) => Readable . toWeb ( s ) ) ,
290+ ) . pipe (
291+ Effect . map ( ( s ) =>
292+ Stream . fromReadableStream (
293+ ( ) => s as ReadableStream < Uint8Array > ,
294+ wrapError ,
295+ ) ,
296+ ) ,
297+ Stream . unwrap ,
298+ ) ,
284299 delete : use ( ( ) => download . delete ( ) ) ,
285300 failure : use ( ( ) => download . failure ( ) ) . pipe (
286301 Effect . map ( Option . fromNullable ) ,
0 commit comments