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

Method write

lib/protocol/SFTP.js:422–497  ·  view source on GitHub ↗
(handle, buf, off, len, position, cb)

Source from the content-addressed store, hash-verified

420 this.read(handle, buf, off, len, position, cb);
421 }
422 write(handle, buf, off, len, position, cb) {
423 if (this.server)
424 throw new Error('Client-only method called in server mode');
425
426 if (!Buffer.isBuffer(handle))
427 throw new Error('handle is not a Buffer');
428 if (!Buffer.isBuffer(buf))
429 throw new Error('buffer is not a Buffer');
430 if (off > buf.length)
431 throw new Error('offset is out of bounds');
432 if (off + len > buf.length)
433 throw new Error('length extends beyond buffer');
434 if (position === null)
435 throw new Error('null position currently unsupported');
436
437 if (!len) {
438 cb && process.nextTick(cb, undefined, 0);
439 return;
440 }
441
442 const maxDataLen = this._maxWriteLen;
443 const overflow = Math.max(len - maxDataLen, 0);
444 const origPosition = position;
445
446 if (overflow)
447 len = maxDataLen;
448
449 /*
450 uint32 id
451 string handle
452 uint64 offset
453 string data
454 */
455 const handleLen = handle.length;
456 let p = 9;
457 const out = Buffer.allocUnsafe(4 + 1 + 4 + 4 + handleLen + 8 + 4 + len);
458
459 writeUInt32BE(out, out.length - 4, 0);
460 out[4] = REQUEST.WRITE;
461 const reqid = this._writeReqid = (this._writeReqid + 1) & MAX_REQID;
462 writeUInt32BE(out, reqid, 5);
463
464 writeUInt32BE(out, handleLen, p);
465 out.set(handle, p += 4);
466 p += handleLen;
467 for (let i = 7; i >= 0; --i) {
468 out[p + i] = position & 0xFF;
469 position /= 256;
470 }
471 writeUInt32BE(out, len, p += 8);
472 bufferCopy(buf, out, off, off + len, p += 4);
473
474 this._requests[reqid] = {
475 cb: (err) => {
476 if (err) {
477 if (typeof cb === 'function')
478 cb(err);
479 } else if (overflow) {

Callers 15

writeDataMethod · 0.95
connectMethod · 0.80
constructorMethod · 0.80
_onconnectMethod · 0.80
_ondataMethod · 0.80
dataMethod · 0.80
onreadFunction · 0.80
writeAllFunction · 0.80
SFTP.jsFile · 0.80
test-openssh.jsFile · 0.80
test-sftp.jsFile · 0.80

Calls 3

writeUInt32BEFunction · 0.85
bufferCopyFunction · 0.85
sendOrBufferFunction · 0.85

Tested by

no test coverage detected