filefive 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
+ const node_fs_1 = require("node:fs");
40
+ const node_os_1 = require("node:os");
41
+ const node_path_1 = require("node:path");
39
42
  const ReferenceCountMap_1 = __importDefault(require("./utils/ReferenceCountMap"));
40
43
  const types_1 = require("./types");
41
44
  const URI_1 = require("./utils/URI");
@@ -49,14 +52,22 @@ class default_1 {
49
52
  static initialize() {
50
53
  this.shared.set(types_1.LocalFileSystemID, new Local_1.default);
51
54
  }
52
- static async open(scheme, user, host, port, password) {
55
+ static async open(scheme, user, host, port, password, privatekey) {
53
56
  const id = (0, URI_1.connectionID)(scheme, user, host, port);
54
57
  const attrs = (scheme == 'sftp') ? SFtp_1.ATTRIBUTES : Ftp_1.ATTRIBUTES;
55
58
  if (this.shared.inc(id)) {
56
59
  return attrs;
57
60
  }
58
- this.credentials.set(id, password);
59
- const conn = await this.create(id, scheme, user, host, port);
61
+ if (privatekey) {
62
+ if (privatekey.startsWith('~')) {
63
+ privatekey = (0, node_path_1.join)((0, node_os_1.homedir)(), privatekey.substring(1));
64
+ }
65
+ this.credentials.set(id, ['key', (0, node_fs_1.readFileSync)(privatekey)]);
66
+ }
67
+ else {
68
+ this.credentials.set(id, ['password', password]);
69
+ }
70
+ const conn = await this.create(id);
60
71
  await conn.open();
61
72
  this.shared.set(id, conn);
62
73
  return attrs;
@@ -105,7 +116,7 @@ class default_1 {
105
116
  const poolId = (0, uniqid_1.default)();
106
117
  try {
107
118
  const onClose = () => this.pools.get(id)?.delete(poolId);
108
- const fs = await this.createFromId(id, onClose);
119
+ const fs = await this.create(id, onClose);
109
120
  await fs.open();
110
121
  this.pools.get(id).set(poolId, { fs, idle: false });
111
122
  resolve([fs, poolId]);
@@ -159,28 +170,30 @@ class default_1 {
159
170
  static getLimit(id) {
160
171
  return this.limits.has(id) ? this.limits.get(id) : 1024; // for vsftpd max_per_ip in /etc/vsftpd.conf
161
172
  }
162
- static async create(id, scheme, user, host, port, onClose = () => { }) {
173
+ static async create(id, onClose = () => { }) {
163
174
  if (options_1.default.log) {
164
- return new log_1.LogFS(id, await this.createFS(scheme, user, host, port, this.credentials.get(id), onClose));
175
+ return new log_1.LogFS(id, await this.createFS(id, onClose));
165
176
  }
166
- return this.createFS(scheme, user, host, port, this.credentials.get(id), onClose);
177
+ return this.createFS(id, onClose);
167
178
  }
168
- static async createFS(scheme, user, host, port, password, onClose) {
179
+ static async createFS(id, onClose) {
180
+ const { scheme, user, host, port } = (0, URI_1.parseURI)(id);
181
+ if (!this.credentials.has(id)) {
182
+ onClose?.();
183
+ return;
184
+ }
185
+ const [authType, credential] = this.credentials.get(id);
169
186
  switch (scheme) {
170
187
  case 'sftp': {
171
- return new SFtp_1.default(host, user, password, port, error => log_1.default.error('SFTP error:', error), onClose);
188
+ return new SFtp_1.default(host, user, authType == 'password' ? credential : '', authType == 'key' ? credential : null, port, error => log_1.default.error('SFTP error:', error), onClose);
172
189
  }
173
190
  case 'ftp': {
174
- return new Ftp_1.default(host, user, password, port, error => log_1.default.error('FTP error:', error), onClose);
191
+ return new Ftp_1.default(host, user, credential, port, error => log_1.default.error('FTP error:', error), onClose);
175
192
  }
176
193
  default:
177
194
  throw new Error(`Unsupported scheme ${scheme}`);
178
195
  }
179
196
  }
180
- static async createFromId(id, onClose = () => { }) {
181
- const { scheme, user, host, port } = (0, URI_1.parseURI)(id);
182
- return this.create(id, scheme, user, host, port, onClose);
183
- }
184
197
  }
185
198
  default_1.numOfStartups = 0;
186
199
  default_1.maxStartups = 7; // for SFTP see MaxStartups in /etc/ssh/sshd_config
@@ -56,17 +56,19 @@ async function default_1(file, onError) {
56
56
  }
57
57
  const id = (0, URI_1.connectionID)(config.scheme, config.user, config.host, config.port);
58
58
  let password = '';
59
- try {
60
- password = await Password_1.default.get(id);
61
- }
62
- catch (e) {
63
- return false;
64
- }
65
- if (!password) {
66
- return false;
59
+ if (!config.privatekey) {
60
+ try {
61
+ password = await Password_1.default.get(id);
62
+ }
63
+ catch (e) {
64
+ return false;
65
+ }
66
+ if (!password) {
67
+ return false;
68
+ }
67
69
  }
68
70
  try {
69
- const attributes = await Connection_1.default.open(config.scheme, config.user, config.host, config.port, password);
71
+ const attributes = await Connection_1.default.open(config.scheme, config.user, config.host, config.port, password, config.privatekey);
70
72
  const pwd = await Connection_1.default.get(id).pwd();
71
73
  const settings = {
72
74
  name: (0, path_1.parse)(file).name,
@@ -29,7 +29,7 @@ async function default_1(path, settings) {
29
29
  const id = (0, URI_1.connectionID)(settings.scheme, settings.user, settings.host, settings.port);
30
30
  config = { ...config, ...(0, ramda_1.omit)(['password', 'savePassword'], settings) };
31
31
  Password_1.default.delete(id, true);
32
- if (settings.password.length) {
32
+ if (settings.password && !settings.privatekey) {
33
33
  Password_1.default.set(id, settings.password, true, settings.savePassword);
34
34
  }
35
35
  }
package/dist/fs/SFtp.js CHANGED
@@ -30,11 +30,12 @@ exports.ATTRIBUTES = [
30
30
  }
31
31
  ];
32
32
  class SFtp extends FileSystem_1.FileSystem {
33
- constructor(host, user, password, port = 22, onError, onClose = () => { }) {
33
+ constructor(host, user, password, privateKey = null, port = 22, onError, onClose = () => { }) {
34
34
  super();
35
35
  this.host = host;
36
36
  this.user = user;
37
37
  this.password = password;
38
+ this.privateKey = privateKey;
38
39
  this.port = port;
39
40
  this.onError = onError;
40
41
  this.onClose = onClose;
@@ -74,8 +75,10 @@ class SFtp extends FileSystem_1.FileSystem {
74
75
  this.connection.connect({
75
76
  host: this.host,
76
77
  username: this.user,
77
- password: this.password,
78
78
  port: this.port,
79
+ ...(this.privateKey ?
80
+ { privateKey: this.privateKey } :
81
+ { password: this.password })
79
82
  // debug: s => console.log('DEBUG', s)
80
83
  });
81
84
  }