(handle, buf, off, len, position, cb)
| 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) { |
no test coverage detected