Skip to content

Commit

Permalink
change event delegate interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiChou committed Jun 13, 2023
1 parent 3e819bb commit 8642a85
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { basename, join } from "path";
import { findEncryptedBinaries } from './lib/scan.js';
import { Pull, quote } from './lib/scp.js';
import { connect } from './lib/ssh.js';
import { debug, directoryExists, passthrough, readAgent } from './lib/utils.js';
import { debug, directoryExists, readAgent } from './lib/utils.js';
import zip from './lib/zip.js';


Expand Down Expand Up @@ -46,10 +46,15 @@ export class BagBak extends EventEmitter {
async #copyToLocal(src, dest) {
const client = await connect(this.#device);

const pull = new Pull(client, src, dest, true);
const events = ['download', 'mkdir', 'progress', 'done'];
for (const event of events) {
// delegate events
pull.receiver.on(event, (...args) => this.emit(event, ...args));
}

try {
const pull = new Pull(client, src, dest, true);
passthrough(pull, this); // delegate events
await pull.start();
await pull.execute();
} finally {
client.end();
}
Expand Down
22 changes: 12 additions & 10 deletions lib/scp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { promisify } from 'util';

import { Client } from 'ssh2';

import { enableDebug, passthrough } from './utils.js';
import { enableDebug } from './utils.js';


const State = {
Expand Down Expand Up @@ -245,12 +245,12 @@ export class Pull extends EventEmitter {
*/
#remote;


/**
* @private
* @type {PathLike}
* @default '.'
* @public
* @type {SCPReceiver}
*/
#local;
receiver;

/**
*
Expand All @@ -265,19 +265,21 @@ export class Pull extends EventEmitter {
this.#client = client;
this.#recursive = recursive;
this.#remote = quote(remote);
this.#local = local;

this.receiver = new SCPReceiver(local, recursive);
}

async start() {
/**
* @returns {Promise<void>}
*/
async execute() {
const exec = promisify(this.#client.exec.bind(this.#client));
const stream = await exec(`scp -v -f -p ${this.#recursive ? '-r' : ''} ${this.#remote}`);

const receiver = new SCPReceiver(this.#local, this.#recursive);
const receiver = this.receiver;
stream.stdout.pipe(receiver);
receiver.pipe(stream.stdin);

passthrough(receiver, this);

if (enableDebug()) {
stream.stderr.pipe(process.stderr);
}
Expand Down
18 changes: 6 additions & 12 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,9 @@ export async function enumerateApps(device) {
}));
}

/**
* forward download events
* @param {EventEmitter} source
* @param {EventEmitter} destination
* @returns {void}
*/
export function passthrough(source, destination) {
const events = ['download', 'mkdir', 'progress', 'done'];
for (const event of events) {
source.on(event, (...args) => destination.emit(event, ...args));
}
}

/**
* read source code of agent
* @returns {Promise<string>}
*/
export function readAgent() {
Expand All @@ -75,6 +64,11 @@ export function readAgent() {


let __debug = 'DEBUG' in process.env;

/**
* debug log
* @param {...any} args
*/
export function debug() {
if (__debug)
console.log(...arguments);
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class BagBak extends EventEmitter {
*/
pack(suggested?: PathLike): Promise<string>;

// todo: rename events
on(event: 'download', listener: (src: string, size: number) => void): this;
on(event: 'mkdir', listener: (path: string) => void): this;
on(event: 'progress', listener: (src: string, written: number, size: number) => void): this;
Expand Down

0 comments on commit 8642a85

Please sign in to comment.