MCPcopy Create free account
hub / github.com/mscdex/ssh2 / readFile

Method readFile

lib/protocol/SFTP.js:514–638  ·  view source on GitHub ↗
(path, options, callback_)

Source from the content-addressed store, hash-verified

512 fastXfer(fs, this, localPath, remotePath, opts, cb);
513 }
514 readFile(path, options, callback_) {
515 if (this.server)
516 throw new Error('Client-only method called in server mode');
517
518 let callback;
519 if (typeof callback_ === 'function') {
520 callback = callback_;
521 } else if (typeof options === 'function') {
522 callback = options;
523 options = undefined;
524 }
525
526 if (typeof options === 'string')
527 options = { encoding: options, flag: 'r' };
528 else if (!options)
529 options = { encoding: null, flag: 'r' };
530 else if (typeof options !== 'object')
531 throw new TypeError('Bad arguments');
532
533 const encoding = options.encoding;
534 if (encoding && !Buffer.isEncoding(encoding))
535 throw new Error(`Unknown encoding: ${encoding}`);
536
537 // First stat the file, so we know the size.
538 let size;
539 let buffer; // Single buffer with file data
540 let buffers; // List for when size is unknown
541 let pos = 0;
542 let handle;
543
544 // SFTPv3 does not support using -1 for read position, so we have to track
545 // read position manually
546 let bytesRead = 0;
547
548 const flag = options.flag || 'r';
549
550 const read = () => {
551 if (size === 0) {
552 buffer = Buffer.allocUnsafe(8192);
553 this.read(handle, buffer, 0, 8192, bytesRead, afterRead);
554 } else {
555 this.read(handle, buffer, pos, size - pos, bytesRead, afterRead);
556 }
557 };
558
559 const afterRead = (er, nbytes) => {
560 let eof;
561 if (er) {
562 eof = (er.code === STATUS_CODE.EOF);
563 if (!eof) {
564 return this.close(handle, () => {
565 return callback && callback(er);
566 });
567 }
568 } else {
569 eof = false;
570 }
571

Callers 2

poly1305.jsFile · 0.80
test-sftp.jsFile · 0.80

Calls 3

openMethod · 0.95
fstatMethod · 0.95
callbackFunction · 0.85

Tested by

no test coverage detected