MCPcopy Index your code
hub / github.com/jvilk/BrowserFS / readFile

Method readFile

src/core/file_system.ts:635–671  ·  view source on GitHub ↗
(fname: string, encoding: string | null, flag: FileFlag, cb: BFSCallback<string | Buffer>)

Source from the content-addressed store, hash-verified

633 }
634 }
635 public readFile(fname: string, encoding: string | null, flag: FileFlag, cb: BFSCallback<string | Buffer>): void {
636 // Wrap cb in file closing code.
637 const oldCb = cb;
638 // Get file.
639 this.open(fname, flag, 0x1a4, (err, fd) => {
640 if (err) {
641 return cb(err);
642 }
643 cb = function(err?: ApiError | null, arg?: string | Buffer) {
644 fd!.close(function(err2: any) {
645 if (!err) {
646 err = err2;
647 }
648 return oldCb(err, arg);
649 });
650 };
651 fd!.stat((err, stat?) => {
652 if (err) {
653 return cb(err);
654 }
655 // Allocate buffer.
656 const buf = Buffer.alloc(stat!.size);
657 fd!.read(buf, 0, stat!.size, 0, (err?: ApiError | null) => {
658 if (err) {
659 return cb(err);
660 } else if (encoding === null) {
661 return cb(err, buf);
662 }
663 try {
664 cb(null, buf.toString(encoding));
665 } catch (e) {
666 cb(e);
667 }
668 });
669 });
670 });
671 }
672 public readFileSync(fname: string, encoding: string | null, flag: FileFlag): any {
673 // Get file.
674 const fd = this.openSync(fname, flag, 0x1a4);

Callers

nothing calls this directly

Calls 6

openMethod · 0.95
cbFunction · 0.85
toStringMethod · 0.80
closeMethod · 0.65
statMethod · 0.65
readMethod · 0.65

Tested by

no test coverage detected