Skip to content

Commit 2496ac9

Browse files
committed
implement Download stream method
1 parent 44e0c8b commit 2496ac9

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/common.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert, layer } from "@effect/vitest";
2-
import { Effect, Fiber, Option, Stream } from "effect";
2+
import { Chunk, Effect, Fiber, Option, Stream } from "effect";
33
import { chromium } from "playwright-core";
44
import { PlaywrightBrowser } from "./browser";
55
import { PlaywrightEnvironment } from "./experimental";
@@ -140,6 +140,14 @@ layer(PlaywrightEnvironment.layer(chromium))("PlaywrightCommon", (it) => {
140140
assert((yield* download.suggestedFilename) === "test.txt");
141141
const url = yield* download.url;
142142
assert(url.startsWith("data:"));
143+
144+
const text = yield* download.stream.pipe(
145+
Stream.decodeText(),
146+
Stream.runCollect,
147+
Effect.map(Chunk.join("")),
148+
);
149+
150+
assert.strictEqual(text, "hello world");
143151
}).pipe(PlaywrightEnvironment.withBrowser),
144152
);
145153
});

src/common.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Readable } from "node:stream";
12
import { Data, Effect, Option, Stream } from "effect";
23
import 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";
1213
import { PlaywrightFrame, type PlaywrightFrameService } from "./frame";
1314
import { PlaywrightPage, type PlaywrightPageService } from "./page";
1415
import type { PageFunction } from "./playwright-types";
@@ -262,7 +263,12 @@ export class PlaywrightFileChooser extends Data.TaggedClass(
262263
*/
263264
export 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),

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"isolatedModules": true,
1515
"verbatimModuleSyntax": true,
1616
"skipLibCheck": true,
17-
"types": ["vitest/importMeta"],
17+
"types": ["vitest/importMeta", "node"],
1818
"paths": {
1919
"effect-playwright": ["./src/index.ts"],
2020
"effect-playwright/experimental": ["./src/experimental/index.ts"]

0 commit comments

Comments
 (0)