( impl: Omit<FileSystem, typeof TypeId | "exists" | "readFileString" | "stream" | "sink" | "writeFileString"> )
| 684 | * @since 4.0.0 |
| 685 | */ |
| 686 | export const make = ( |
| 687 | impl: Omit<FileSystem, typeof TypeId | "exists" | "readFileString" | "stream" | "sink" | "writeFileString"> |
| 688 | ): FileSystem => |
| 689 | FileSystem.of({ |
| 690 | ...impl, |
| 691 | [TypeId]: TypeId, |
| 692 | exists: (path) => |
| 693 | pipe( |
| 694 | impl.access(path), |
| 695 | Effect.as(true), |
| 696 | Effect.catchTag( |
| 697 | "PlatformError", |
| 698 | (e) => e.reason._tag === "NotFound" ? Effect.succeed(false) : Effect.fail(e) |
| 699 | ) |
| 700 | ), |
| 701 | readFileString: (path, encoding) => |
| 702 | Effect.flatMap(impl.readFile(path), (_) => |
| 703 | Effect.try({ |
| 704 | try: () => new TextDecoder(encoding).decode(_), |
| 705 | catch: (cause) => |
| 706 | badArgument({ |
| 707 | module: "FileSystem", |
| 708 | method: "readFileString", |
| 709 | description: "invalid encoding", |
| 710 | cause |
| 711 | }) |
| 712 | })), |
| 713 | stream: Effect.fnUntraced(function*(path, options) { |
| 714 | const file = yield* impl.open(path, { flag: "r" }) |
| 715 | if (options?.offset) { |
| 716 | yield* file.seek(options.offset, "start") |
| 717 | } |
| 718 | const bytesToRead = options?.bytesToRead !== undefined ? Size(options.bytesToRead) : undefined |
| 719 | let totalBytesRead = BigInt(0) |
| 720 | const chunkSize = Size(options?.chunkSize ?? 64 * 1024) |
| 721 | const readChunk = file.readAlloc(chunkSize) |
| 722 | return Stream.fromPull(Effect.succeed( |
| 723 | Effect.flatMap( |
| 724 | Effect.suspend((): Pull.Pull<Option.Option<Uint8Array>, PlatformError> => { |
| 725 | if (bytesToRead !== undefined && bytesToRead <= totalBytesRead) { |
| 726 | return Cause.done() |
| 727 | } |
| 728 | return bytesToRead !== undefined && (bytesToRead - totalBytesRead) < chunkSize |
| 729 | ? file.readAlloc(bytesToRead - totalBytesRead) |
| 730 | : readChunk |
| 731 | }), |
| 732 | Option.match({ |
| 733 | onNone: () => Cause.done(), |
| 734 | onSome: (buf) => { |
| 735 | totalBytesRead += BigInt(buf.length) |
| 736 | return Effect.succeed(Arr.of(buf)) |
| 737 | } |
| 738 | }) |
| 739 | ) |
| 740 | )) |
| 741 | }, Stream.unwrap), |
| 742 | sink: (path, options) => |
| 743 | pipe( |
nothing calls this directly
no test coverage detected