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

Method _write

lib/agent.js:194–235  ·  view source on GitHub ↗
(data, encoding, cb)

Source from the content-addressed store, hash-verified

192 }
193 _read(n) {}
194 _write(data, encoding, cb) {
195 if (this.buffer === null) {
196 this.buffer = data;
197 } else {
198 const newBuffer = Buffer.allocUnsafe(this.buffer.length + data.length);
199 this.buffer.copy(newBuffer, 0);
200 data.copy(newBuffer, this.buffer.length);
201 this.buffer = newBuffer;
202 }
203 // Wait for at least all length bytes
204 if (this.buffer.length < 4)
205 return cb();
206
207 const len = readUInt32BE(this.buffer, 0);
208 // Make sure we have a full message before querying pageant
209 if ((this.buffer.length - 4) < len)
210 return cb();
211
212 data = this.buffer.slice(0, 4 + len);
213 if (this.buffer.length > (4 + len))
214 return cb(new Error('Unexpected multiple agent requests'));
215 this.buffer = null;
216
217 let error;
218 const proc = this.proc = spawn(EXEPATH, [ data.length ]);
219 proc.stdout.on('data', (data) => {
220 this.push(data);
221 });
222 proc.on('error', (err) => {
223 error = err;
224 cb(error);
225 });
226 proc.on('close', (code) => {
227 this.proc = undefined;
228 if (!error) {
229 if (error = ERROR[code])
230 return cb(error);
231 cb();
232 }
233 });
234 proc.stdin.end(data);
235 }
236 _final(cb) {
237 destroy(this);
238 cb();

Callers

nothing calls this directly

Calls 3

readUInt32BEFunction · 0.85
pushMethod · 0.80
endMethod · 0.45

Tested by

no test coverage detected