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

Function make

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

Source from the content-addressed store, hash-verified

581 * @since 4.0.0
582 */
583export const make = (
584 impl: Omit<FileSystem, typeof TypeId | "exists" | "glob" | "readFileString" | "stream" | "sink" | "writeFileString">
585): FileSystem =>
586 FileSystem.of({
587 ...impl,
588 [TypeId]: TypeId,
589 glob: Glob.make(impl, PathFilter),
590 exists: (path) =>
591 pipe(
592 impl.access(path),
593 Effect.as(true),
594 Effect.catchTag(
595 "PlatformError",
596 (e) => e.reason._tag === "NotFound" ? Effect.succeed(false) : Effect.fail(e)
597 )
598 ),
599 readFileString: (path, encoding) =>
600 Effect.flatMap(impl.readFile(path), (_) =>
601 Effect.try({
602 try: () => new TextDecoder(encoding).decode(_),
603 catch: (cause) =>
604 badArgument({
605 module: "FileSystem",
606 method: "readFileString",
607 description: "invalid encoding",
608 cause
609 })
610 })),
611 stream: Effect.fnUntraced(function*(path, options) {
612 const file = yield* impl.open(path, { flag: "r" })
613 const offset = options?.offset === undefined ? undefined : ByteSize.fromInputUnsafe(options.offset)
614 if (offset) {
615 yield* file.seek(offset, "start")
616 }
617 const bytesToRead = options?.bytesToRead === undefined
618 ? undefined
619 : ByteSize.fromInputUnsafe(options.bytesToRead)
620 let totalBytesRead = BigInt(0)
621 // Validate chunk sizes even for zero-byte reads.
622 const chunkSize = Number(BigInt(options?.chunkSize ?? 64 * 1024))
623 const readChunk = file.readAlloc(chunkSize)
624 return Stream.fromPull(Effect.succeed(
625 Effect.flatMap(
626 Effect.suspend((): Pull.Pull<Option.Option<Uint8Array>, PlatformError> => {
627 if (bytesToRead !== undefined && bytesToRead <= totalBytesRead) {
628 return Cause.done()
629 }
630 return bytesToRead !== undefined && (bytesToRead - totalBytesRead) < chunkSize
631 ? file.readAlloc(Number(bytesToRead - totalBytesRead))
632 : readChunk
633 }),
634 Option.match({
635 onNone: () => Cause.done(),
636 onSome: (buf) => {
637 totalBytesRead += BigInt(buf.length)
638 return Effect.succeed(Arr.of(buf))
639 }
640 })

Callers

nothing calls this directly

Calls 15

pipeFunction · 0.90
badArgumentFunction · 0.90
encodeMethod · 0.80
BigIntInterface · 0.70
NumberFunction · 0.70
ofMethod · 0.65
makeMethod · 0.65
failMethod · 0.65
forEachMethod · 0.65
succeedMethod · 0.45
decodeMethod · 0.45
openMethod · 0.45

Tested by

no test coverage detected