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

Method writeSync

lib/protocol/zlib.js:70–168  ·  view source on GitHub ↗
(chunk, retChunks)

Source from the content-addressed store, hash-verified

68 }
69
70 writeSync(chunk, retChunks) {
71 const handle = this._handle;
72 if (!handle)
73 throw new Error('Invalid Zlib instance');
74
75 let availInBefore = chunk.length;
76 let availOutBefore = this._chunkSize - this._outOffset;
77 let inOff = 0;
78 let availOutAfter;
79 let availInAfter;
80
81 let buffers;
82 let nread = 0;
83 const state = this._writeState;
84 let buffer = this._outBuffer;
85 let offset = this._outOffset;
86 const chunkSize = this._chunkSize;
87
88 while (true) {
89 handle.writeSync(Z_PARTIAL_FLUSH,
90 chunk, // in
91 inOff, // in_off
92 availInBefore, // in_len
93 buffer, // out
94 offset, // out_off
95 availOutBefore); // out_len
96 if (this._err)
97 throw this._err;
98
99 availOutAfter = state[0];
100 availInAfter = state[1];
101
102 const inDelta = availInBefore - availInAfter;
103 const have = availOutBefore - availOutAfter;
104
105 if (have > 0) {
106 const out = (offset === 0 && have === buffer.length
107 ? buffer
108 : buffer.slice(offset, offset + have));
109 offset += have;
110 if (!buffers)
111 buffers = out;
112 else if (buffers.push === undefined)
113 buffers = [buffers, out];
114 else
115 buffers.push(out);
116 nread += out.byteLength;
117
118 if (nread > this._maxOutputLength) {
119 _close(this);
120 throw new Error(
121 `Output length exceeded maximum of ${this._maxOutputLength}`
122 );
123 }
124 } else if (have !== 0) {
125 throw new Error('have should not go down');
126 }
127

Callers 2

finalizeMethod · 0.80
readMethod · 0.80

Calls 3

_closeFunction · 0.85
pushMethod · 0.80
allocMethod · 0.45

Tested by

no test coverage detected