Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, layer } from "@effect/vitest";
import { Effect, Fiber, Option, Stream } from "effect";
import { Chunk, Effect, Fiber, Option, Stream } from "effect";
import { chromium } from "playwright-core";
import { PlaywrightBrowser } from "./browser";
import { PlaywrightEnvironment } from "./experimental";
Expand Down Expand Up @@ -140,6 +140,14 @@ layer(PlaywrightEnvironment.layer(chromium))("PlaywrightCommon", (it) => {
assert((yield* download.suggestedFilename) === "test.txt");
const url = yield* download.url;
assert(url.startsWith("data:"));

const text = yield* download.stream.pipe(
Stream.decodeText(),
Stream.runCollect,
Effect.map(Chunk.join("")),
);

assert.strictEqual(text, "hello world");
}).pipe(PlaywrightEnvironment.withBrowser),
);
});
23 changes: 19 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Readable } from "node:stream";
import { Data, Effect, Option, Stream } from "effect";
import type {
Dialog,
Expand All @@ -8,7 +9,7 @@ import type {
Response,
Worker,
} from "playwright-core";
import type { PlaywrightError } from "./errors";
import { type PlaywrightError, wrapError } from "./errors";
import { PlaywrightFrame, type PlaywrightFrameService } from "./frame";
import { PlaywrightPage, type PlaywrightPageService } from "./page";
import type { PageFunction } from "./playwright-types";
Expand Down Expand Up @@ -262,7 +263,12 @@ export class PlaywrightFileChooser extends Data.TaggedClass(
*/
export class PlaywrightDownload extends Data.TaggedClass("PlaywrightDownload")<{
cancel: Effect.Effect<void, PlaywrightError>;
createReadStream: Stream.Stream<Uint8Array, PlaywrightError>;
/**
* Creates a stream of the download data.
* @category custom
* @since 0.2.0
*/
stream: Stream.Stream<Uint8Array, PlaywrightError>;
delete: Effect.Effect<void, PlaywrightError>;
failure: Effect.Effect<Option.Option<string | null>, PlaywrightError>;
page: () => PlaywrightPageService;
Expand All @@ -279,8 +285,17 @@ export class PlaywrightDownload extends Data.TaggedClass("PlaywrightDownload")<{

return new PlaywrightDownload({
cancel: use(() => download.cancel()),
/** TODO: implement createReadStream / effect wrapper for it */
createReadStream: Stream.empty,
stream: use(() =>
download.createReadStream().then((s) => Readable.toWeb(s)),
).pipe(
Effect.map((s) =>
Stream.fromReadableStream(
() => s as ReadableStream<Uint8Array>,
wrapError,
),
),
Stream.unwrap,
),
delete: use(() => download.delete()),
Comment thread
StefanWerW marked this conversation as resolved.
failure: use(() => download.failure()).pipe(
Effect.map(Option.fromNullable),
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"isolatedModules": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true,
"types": ["vitest/importMeta"],
"types": ["vitest/importMeta", "node"],
"paths": {
"effect-playwright": ["./src/index.ts"],
"effect-playwright/experimental": ["./src/experimental/index.ts"]
Expand Down