MCPcopy Create free account
hub / github.com/Effect-TS/effect / make

Function make

packages/effect/src/FileSystem.ts:686–762  ·  view source on GitHub ↗
(
  impl: Omit<FileSystem, typeof TypeId | "exists" | "readFileString" | "stream" | "sink" | "writeFileString">
)

Source from the content-addressed store, hash-verified

684 * @since 4.0.0
685 */
686export 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(

Callers

nothing calls this directly

Calls 13

pipeFunction · 0.90
badArgumentFunction · 0.90
SizeFunction · 0.85
BigIntInterface · 0.70
ofMethod · 0.65
forEachMethod · 0.65
succeedMethod · 0.45
failMethod · 0.45
seekMethod · 0.45
readAllocMethod · 0.45
doneMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected