"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = exports.ScpClient = void 0;
var events_1 = require("events");
var fs_1 = require("fs");
var path_1 = require("path");
var ssh2_1 = require("ssh2");
var constant_1 = require("./constant");
var utils = __importStar(require("./utils"));
var ScpClient = /** @class */ (function (_super) {
    __extends(ScpClient, _super);
    function ScpClient(options) {
        var _this = _super.call(this) || this;
        _this.sftpWrapper = null;
        _this.sshClient = null;
        _this.remotePathSep = path_1.posix.sep;
        _this.endCalled = false;
        _this.errorHandled = false;
        var ssh = new ssh2_1.Client();
        ssh.on('connect', function () {
            _this.emit('connect');
        });
        ssh.on('ready', function () {
            ssh.sftp(function (err, sftp) {
                if (err) {
                    throw err;
                }
                // save for reuse
                _this.sftpWrapper = sftp;
                _this.emit('ready');
            });
        });
        ssh.on('error', function (err) {
            _this.emit('error', err);
        });
        ssh.on('end', function () {
            _this.emit('end');
        });
        ssh.on('close', function () {
            if (!_this.endCalled) {
                _this.sftpWrapper = null;
            }
            _this.emit('close');
        });
        ssh.on('keyboard-interactive', function (name, instructions, instructionsLang, prompts, finish) {
            _this.emit('keyboard-interactive', name, instructions, instructionsLang, prompts, finish);
        });
        ssh.on('change password', function (message, language, done) {
            _this.emit('change password', message, language, done);
        });
        ssh.on('tcp connection', function (details, accept, reject) {
            _this.emit('tcp connection', details, accept, reject);
        });
        ssh.connect(options);
        _this.sshClient = ssh;
        if (options.remoteOsType === 'win32') {
            _this.remotePathSep = path_1.win32.sep;
        }
        return _this;
    }
    ScpClient.prototype.uploadFile = function (localPath, remotePath) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                utils.haveConnection(this, 'uploadFile');
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        _this.sftpWrapper.fastPut(localPath, remotePath, function (err) {
                            if (err) {
                                reject(err);
                            }
                            else {
                                resolve();
                            }
                        });
                    })];
            });
        });
    };
    ScpClient.prototype.downloadFile = function (remotePath, localPath) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                utils.haveConnection(this, 'downloadFile');
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        _this.sftpWrapper.fastGet(remotePath, localPath, function (err) {
                            if (err) {
                                reject(err);
                            }
                            else {
                                resolve();
                            }
                        });
                    })];
            });
        });
    };
    ScpClient.prototype.emptyDir = function (dir) {
        return __awaiter(this, void 0, void 0, function () {
            var isExist, error_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        utils.haveConnection(this, 'uploadDir');
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 8, , 9]);
                        return [4 /*yield*/, this.exists(dir)];
                    case 2:
                        isExist = _a.sent();
                        if (!!isExist) return [3 /*break*/, 4];
                        return [4 /*yield*/, this.mkdir(dir)];
                    case 3:
                        _a.sent();
                        return [3 /*break*/, 7];
                    case 4:
                        if (!(isExist === 'd')) return [3 /*break*/, 7];
                        return [4 /*yield*/, this.rmdir(dir)];
                    case 5:
                        _a.sent();
                        return [4 /*yield*/, this.mkdir(dir)];
                    case 6:
                        _a.sent();
                        _a.label = 7;
                    case 7: return [3 /*break*/, 9];
                    case 8:
                        error_1 = _a.sent();
                        throw error_1;
                    case 9: return [2 /*return*/];
                }
            });
        });
    };
    ScpClient.prototype.uploadDir = function (src, dest) {
        return __awaiter(this, void 0, void 0, function () {
            var isExist, dirEntries, _i, dirEntries_1, e, newSrc, newDst, newSrc, newDst, error_2;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        utils.haveConnection(this, 'uploadDir');
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 11, , 12]);
                        return [4 /*yield*/, this.exists(dest)];
                    case 2:
                        isExist = _a.sent();
                        if (!!isExist) return [3 /*break*/, 4];
                        return [4 /*yield*/, this.mkdir(dest)];
                    case 3:
                        _a.sent();
                        _a.label = 4;
                    case 4:
                        dirEntries = fs_1.readdirSync(src, {
                            encoding: 'utf8',
                            withFileTypes: true,
                        });
                        _i = 0, dirEntries_1 = dirEntries;
                        _a.label = 5;
                    case 5:
                        if (!(_i < dirEntries_1.length)) return [3 /*break*/, 10];
                        e = dirEntries_1[_i];
                        if (!e.isDirectory()) return [3 /*break*/, 7];
                        newSrc = path_1.join(src, e.name);
                        newDst = utils.joinRemote(this, dest, e.name);
                        return [4 /*yield*/, this.uploadDir(newSrc, newDst)];
                    case 6:
                        _a.sent();
                        return [3 /*break*/, 9];
                    case 7:
                        if (!e.isFile()) return [3 /*break*/, 9];
                        newSrc = path_1.join(src, e.name);
                        newDst = utils.joinRemote(this, dest, e.name);
                        return [4 /*yield*/, this.uploadFile(newSrc, newDst)
                            // this.client.emit('upload', {source: src, destination: dst})
                        ];
                    case 8:
                        _a.sent();
                        _a.label = 9;
                    case 9:
                        _i++;
                        return [3 /*break*/, 5];
                    case 10: return [3 /*break*/, 12];
                    case 11:
                        error_2 = _a.sent();
                        throw error_2;
                    case 12: return [2 /*return*/];
                }
            });
        });
    };
    ScpClient.prototype.downloadDir = function (remotePath, localPath) {
        return __awaiter(this, void 0, void 0, function () {
            var remoteInfo, localInfo, fileList, _i, fileList_1, f, newSrc, newDst, src, dst, err_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        utils.haveConnection(this, 'downloadDir');
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 12, , 13]);
                        return [4 /*yield*/, utils.checkRemotePath(this, remotePath, constant_1.targetType.readDir)];
                    case 2:
                        remoteInfo = _a.sent();
                        if (!remoteInfo.valid) {
                            throw new Error(remoteInfo.msg);
                        }
                        if (!fs_1.existsSync(localPath)) {
                            fs_1.mkdirSync(localPath);
                        }
                        return [4 /*yield*/, utils.checkLocalPath(localPath, constant_1.targetType.writeDir)];
                    case 3:
                        localInfo = _a.sent();
                        if (localInfo.valid && !localInfo.type) {
                            fs_1.mkdirSync(localInfo.path, { recursive: true });
                        }
                        if (!localInfo.valid) {
                            throw new Error(localInfo.msg);
                        }
                        return [4 /*yield*/, this.list(remoteInfo.path)];
                    case 4:
                        fileList = _a.sent();
                        _i = 0, fileList_1 = fileList;
                        _a.label = 5;
                    case 5:
                        if (!(_i < fileList_1.length)) return [3 /*break*/, 11];
                        f = fileList_1[_i];
                        if (!(f.type === 'd')) return [3 /*break*/, 7];
                        newSrc = remoteInfo.path + this.remotePathSep + f.name;
                        newDst = path_1.join(localInfo.path, f.name);
                        return [4 /*yield*/, this.downloadDir(newSrc, newDst)];
                    case 6:
                        _a.sent();
                        return [3 /*break*/, 10];
                    case 7:
                        if (!(f.type === '-')) return [3 /*break*/, 9];
                        src = remoteInfo.path + this.remotePathSep + f.name;
                        dst = path_1.join(localInfo.path, f.name);
                        return [4 /*yield*/, this.downloadFile(src, dst)];
                    case 8:
                        _a.sent();
                        this.sshClient.emit('download', { source: src, destination: dst });
                        return [3 /*break*/, 10];
                    case 9:
                        console.log("downloadDir: File ignored: " + f.name + " not regular file");
                        _a.label = 10;
                    case 10:
                        _i++;
                        return [3 /*break*/, 5];
                    case 11: return [2 /*return*/, remoteInfo.path + " downloaded to " + localInfo.path];
                    case 12:
                        err_1 = _a.sent();
                        throw new Error(err_1);
                    case 13: return [2 /*return*/];
                }
            });
        });
    };
    ScpClient.prototype.stat = function (remotePath) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                utils.haveConnection(this, 'stat');
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        _this.sftpWrapper.stat(remotePath, function (err, stats) {
                            if (err) {
                                reject(err);
                            }
                            else {
                                resolve(stats);
                            }
                        });
                    })];
            });
        });
    };
    ScpClient.prototype.unlink = function (remotePath) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                utils.haveConnection(this, 'unlink');
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        _this.sftpWrapper.unlink(remotePath, function (err) {
                            if (err) {
                                reject(err);
                            }
                            else {
                                resolve();
                            }
                        });
                    })];
            });
        });
    };
    // _rmdir - only works with an empty directory
    ScpClient.prototype._rmdir = function (remotePath) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
                        return __generator(this, function (_a) {
                            this.sftpWrapper.rmdir(remotePath, function (err) {
                                if (err) {
                                    reject(err);
                                }
                                else {
                                    resolve();
                                }
                            });
                            return [2 /*return*/];
                        });
                    }); })];
            });
        });
    };
    ScpClient.prototype.rmdir = function (remotePath) {
        return __awaiter(this, void 0, void 0, function () {
            var files, _i, files_1, file, fullFilename;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.list(remotePath)];
                    case 1:
                        files = _a.sent();
                        _i = 0, files_1 = files;
                        _a.label = 2;
                    case 2:
                        if (!(_i < files_1.length)) return [3 /*break*/, 7];
                        file = files_1[_i];
                        fullFilename = utils.joinRemote(this, remotePath, file.name);
                        if (!(file.type === 'd')) return [3 /*break*/, 4];
                        return [4 /*yield*/, this.rmdir(fullFilename)];
                    case 3:
                        _a.sent();
                        return [3 /*break*/, 6];
                    case 4: return [4 /*yield*/, this.unlink(fullFilename)];
                    case 5:
                        _a.sent();
                        _a.label = 6;
                    case 6:
                        _i++;
                        return [3 /*break*/, 2];
                    case 7: return [4 /*yield*/, this._rmdir(remotePath)];
                    case 8:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    ScpClient.prototype.mkdir = function (remotePath) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                utils.haveConnection(this, 'mkdir');
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        _this.sftpWrapper.mkdir(remotePath, function (err) {
                            if (err) {
                                reject(err);
                            }
                            else {
                                resolve();
                            }
                        });
                    })];
            });
        });
    };
    ScpClient.prototype.exists = function (remotePath) {
        return __awaiter(this, void 0, void 0, function () {
            var stats, error_3;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        utils.haveConnection(this, 'exists');
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.stat(remotePath)];
                    case 2:
                        stats = _a.sent();
                        if (stats.isDirectory()) {
                            return [2 /*return*/, 'd'];
                        }
                        if (stats.isSymbolicLink()) {
                            return [2 /*return*/, 'l'];
                        }
                        if (stats.isFile()) {
                            return [2 /*return*/, '-'];
                        }
                        return [2 /*return*/, false];
                    case 3:
                        error_3 = _a.sent();
                        return [2 /*return*/, false];
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    ScpClient.prototype.close = function () {
        if (this.sftpWrapper) {
            this.sftpWrapper.end();
            this.sftpWrapper = null;
        }
        if (this.sshClient) {
            this.sshClient.end();
            this.sshClient = null;
        }
        this.endCalled = true;
    };
    ScpClient.prototype.list = function (remotePath, pattern) {
        if (pattern === void 0) { pattern = /.*/; }
        return __awaiter(this, void 0, void 0, function () {
            var _list, pathInfo, err_2;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _list = function (aPath, filter) {
                            return new Promise(function (resolve, reject) {
                                var reg = /-/gi;
                                _this.sftpWrapper.readdir(aPath, function (err, fileList) {
                                    if (err) {
                                        reject(err);
                                    }
                                    else {
                                        var newList = [];
                                        // reset file info
                                        if (fileList) {
                                            newList = fileList.map(function (item) {
                                                return {
                                                    type: item.longname.substr(0, 1),
                                                    name: item.filename,
                                                    size: item.attrs.size,
                                                    modifyTime: item.attrs.mtime * 1000,
                                                    accessTime: item.attrs.atime * 1000,
                                                    rights: {
                                                        user: item.longname.substr(1, 3).replace(reg, ''),
                                                        group: item.longname.substr(4, 3).replace(reg, ''),
                                                        other: item.longname.substr(7, 3).replace(reg, '')
                                                    },
                                                    owner: item.attrs.uid,
                                                    group: item.attrs.gid
                                                };
                                            });
                                        }
                                        // provide some compatibility for auxList
                                        var regex_1;
                                        if (filter instanceof RegExp) {
                                            regex_1 = filter;
                                        }
                                        else {
                                            var newPattern = filter.replace(/\*([^*])*?/gi, '.*');
                                            regex_1 = new RegExp(newPattern);
                                        }
                                        resolve(newList.filter(function (item) { return regex_1.test(item.name); }));
                                    }
                                });
                            });
                        };
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        utils.haveConnection(this, 'list');
                        return [4 /*yield*/, utils.checkRemotePath(this, remotePath, constant_1.targetType.readDir)];
                    case 2:
                        pathInfo = _a.sent();
                        if (!pathInfo.valid) {
                            throw new Error('Remote path is invalid');
                        }
                        return [2 /*return*/, _list(pathInfo.path, pattern)];
                    case 3:
                        err_2 = _a.sent();
                        throw new Error(err_2);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    ScpClient.prototype.realPath = function (remotePath) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            var closeListener = utils.makeCloseListener(_this, reject, 'realPath');
            _this.sshClient.prependListener('close', closeListener);
            var errorListener = utils.makeErrorListener(reject, _this, 'realPath');
            _this.sshClient.prependListener('error', errorListener);
            if (utils.haveConnection(_this, 'realPath', reject)) {
                _this.sftpWrapper.realpath(remotePath, function (err, absPath) {
                    if (err) {
                        if (err.code === 2) {
                            resolve('');
                        }
                        else {
                            reject(utils.formatError(err.message + " " + remotePath, 'realPath', err.code));
                        }
                    }
                    resolve(absPath);
                    _this.removeListener('error', errorListener);
                    _this.removeListener('close', closeListener);
                });
            }
        });
    };
    return ScpClient;
}(events_1.EventEmitter));
exports.ScpClient = ScpClient;
function Client(options) {
    return __awaiter(this, void 0, void 0, function () {
        var client;
        return __generator(this, function (_a) {
            client = new ScpClient(options);
            return [2 /*return*/, new Promise(function (resolve, reject) {
                    client.on('ready', function () {
                        resolve(client);
                    });
                    client.on('error', function (err) {
                        reject(err);
                    });
                })];
        });
    });
}
exports.Client = Client;
exports.default = Client;
