pubo-node 1.0.143 → 1.0.145

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.
@@ -1,268 +1,140 @@
1
1
  "use strict";
2
-
3
- var _excluded = ["driver"],
4
- _excluded2 = ["maxConnection"];
5
- function _await(value, then, direct) {
6
- if (direct) {
7
- return then ? then(value) : value;
8
- }
9
- if (!value || !value.then) {
10
- value = Promise.resolve(value);
11
- }
12
- return then ? value.then(then) : value;
13
- }
14
- function _invoke(body, then) {
15
- var result = body();
16
- if (result && result.then) {
17
- return result.then(then);
18
- }
19
- return then(result);
20
- }
21
- function _empty() {}
22
- function _awaitIgnored(value, direct) {
23
- if (!direct) {
24
- return value && value.then ? value.then(_empty) : Promise.resolve();
25
- }
26
- }
27
- function _async(f) {
28
- return function () {
29
- for (var args = [], i = 0; i < arguments.length; i++) {
30
- args[i] = arguments[i];
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FtpClientPool = exports.FtpClient = void 0;
4
+ const pubo_utils_1 = require("pubo-utils");
5
+ class FtpClient {
6
+ driver;
7
+ options;
8
+ state = { running: false, connected: false, destroyed: false, connecting: false };
9
+ client;
10
+ _len = 0;
11
+ id = (0, pubo_utils_1.random)();
12
+ constructor({ driver, ...options }) {
13
+ this.driver = driver;
14
+ this.options = options;
31
15
  }
32
- try {
33
- return Promise.resolve(f.apply(this, args));
34
- } catch (e) {
35
- return Promise.reject(e);
16
+ get len() {
17
+ return this._len;
36
18
  }
37
- };
38
- }
39
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
40
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
41
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
42
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
43
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
44
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
45
- Object.defineProperty(exports, "__esModule", {
46
- value: true
47
- });
48
- exports.FtpClientPool = exports.FtpClient = void 0;
49
- var pubo_utils_1 = require("pubo-utils");
50
- var FtpClient = /*#__PURE__*/function () {
51
- function FtpClient(_ref) {
52
- var driver = _ref.driver,
53
- options = _objectWithoutPropertiesLoose(_ref, _excluded);
54
- this.driver = void 0;
55
- this.options = void 0;
56
- this.state = {
57
- running: false,
58
- connected: false,
59
- destroyed: false,
60
- connecting: false
61
- };
62
- this.client = void 0;
63
- this._len = 0;
64
- this.id = (0, pubo_utils_1.random)();
65
- this.put = this.bind('put');
66
- this["delete"] = this.bind('delete');
67
- this.list = this.bind('list');
68
- this.rename = this.bind('rename');
69
- this.driver = driver;
70
- this.options = options;
71
- }
72
- var _proto = FtpClient.prototype;
73
- _proto.connect = function connect() {
74
- try {
75
- var _exit = false;
76
- var _this = this;
77
- if (!_this.client) {
78
- _this.client = new _this.driver();
79
- }
80
- _this.state.destroyed = false;
81
- return _await(_invoke(function () {
82
- if (_this.state.connecting) {
83
- return _await((0, pubo_utils_1.waitFor)(function () {
84
- return _this.state.connected;
85
- }, {
86
- checkTime: 1000,
87
- timeout: 10000
88
- }), function () {
89
- _exit = true;
90
- return 'connected';
91
- });
19
+ set len(n) {
20
+ this._len = n;
21
+ if (this._len < 1) {
22
+ this.close();
92
23
  }
93
- }, function (_result) {
94
- if (_exit) return _result;
95
- _this.state.connecting = true;
96
- return new Promise(function (resolve, reject) {
97
- _this.client.once('ready', function () {
98
- _this.state.connected = true;
99
- resolve('connected');
100
- _this.state.connecting = false;
101
- });
102
- _this.client.once('error', function (err) {
103
- reject(err);
104
- _this.close();
105
- });
106
- _this.client.connect(_extends({}, _this.options));
107
- });
108
- }));
109
- } catch (e) {
110
- return Promise.reject(e);
111
24
  }
112
- };
113
- _proto.close = function close() {
114
- this.client.end();
115
- this.state.connected = false;
116
- this.state.destroyed = true;
117
- this.state.connecting = false;
118
- this.client = null;
119
- };
120
- _proto.run = function run(_ref2) {
121
- var fn = _ref2.fn,
122
- args = _ref2.args;
123
- try {
124
- var _this2 = this;
125
- _this2.len += 1;
126
- return _await(_invoke(function () {
127
- if (!_this2.state.connected) {
128
- return _awaitIgnored(_this2.connect());
25
+ put = this.bind('put');
26
+ delete = this.bind('delete');
27
+ list = this.bind('list');
28
+ rename = this.bind('rename');
29
+ async connect() {
30
+ if (!this.client) {
31
+ this.client = new this.driver();
129
32
  }
130
- }, function () {
131
- return _invoke(function () {
132
- if (_this2.state.running) {
133
- return _awaitIgnored((0, pubo_utils_1.waitFor)(function () {
134
- return !_this2.state.running;
135
- }, {
136
- checkTime: 1000,
137
- timeout: 6000000
138
- }));
139
- }
140
- }, function () {
141
- _this2.state.running = true;
142
- return new Promise(function (resolve, reject) {
143
- var _this2$client;
144
- (_this2$client = _this2.client)[fn].apply(_this2$client, args.concat([function (err, res) {
145
- if (err) {
33
+ this.state.destroyed = false;
34
+ if (this.state.connecting) {
35
+ await (0, pubo_utils_1.waitFor)(() => this.state.connected, { checkTime: 1000, timeout: 10000 });
36
+ return 'connected';
37
+ }
38
+ this.state.connecting = true;
39
+ return new Promise((resolve, reject) => {
40
+ this.client.once('ready', () => {
41
+ this.state.connected = true;
42
+ resolve('connected');
43
+ this.state.connecting = false;
44
+ });
45
+ this.client.once('error', (err) => {
146
46
  reject(err);
147
- } else {
148
- resolve(res);
149
- }
150
- }]));
151
- });
47
+ this.close();
48
+ });
49
+ this.client.connect({ ...this.options });
152
50
  });
153
- }));
154
- } catch (e) {
155
- return Promise.reject(e);
156
51
  }
157
- };
158
- _proto.bind = function bind(fn) {
159
- var _this3 = this;
160
- return _async(function () {
161
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
162
- args[_key] = arguments[_key];
163
- }
164
- return _await(_this3.run({
165
- fn: fn,
166
- args: args
167
- }), function (res) {
168
- _this3.state.running = false;
169
- _this3.len -= 1;
170
- return res;
171
- });
172
- });
173
- };
174
- _proto.get = function get() {
175
- try {
176
- var _this4 = this;
177
- var res = Buffer.alloc(0);
178
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
179
- args[_key2] = arguments[_key2];
180
- }
181
- return _await(_this4.run({
182
- fn: 'get',
183
- args: args
184
- }), function (stream) {
185
- return new Promise(function (resolve) {
186
- stream.on('data', function (chunk) {
187
- res = Buffer.concat([res, chunk], res.byteLength + chunk.byteLength);
188
- });
189
- stream.on('end', function () {
190
- resolve(res);
191
- _this4.state.running = false;
192
- _this4.len -= 1;
193
- });
52
+ close() {
53
+ this.client.end();
54
+ this.state.connected = false;
55
+ this.state.destroyed = true;
56
+ this.state.connecting = false;
57
+ this.client = null;
58
+ }
59
+ async run({ fn, args }) {
60
+ this.len += 1;
61
+ if (!this.state.connected) {
62
+ await this.connect();
63
+ }
64
+ if (this.state.running) {
65
+ await (0, pubo_utils_1.waitFor)(() => !this.state.running, { checkTime: 1000, timeout: 6000000 });
66
+ }
67
+ this.state.running = true;
68
+ return new Promise((resolve, reject) => {
69
+ this.client[fn](...args, (err, res) => {
70
+ if (err) {
71
+ reject(err);
72
+ }
73
+ else {
74
+ resolve(res);
75
+ }
76
+ });
194
77
  });
195
- });
196
- } catch (e) {
197
- return Promise.reject(e);
198
78
  }
199
- };
200
- _createClass(FtpClient, [{
201
- key: "len",
202
- get: function get() {
203
- return this._len;
204
- },
205
- set: function set(n) {
206
- this._len = n;
207
- if (this._len < 1) {
208
- this.close();
209
- }
79
+ bind(fn) {
80
+ return async (...args) => {
81
+ const res = await this.run({ fn, args });
82
+ this.state.running = false;
83
+ this.len -= 1;
84
+ return res;
85
+ };
86
+ }
87
+ async get(...args) {
88
+ let res = Buffer.alloc(0);
89
+ const stream = await this.run({ fn: 'get', args });
90
+ return new Promise((resolve) => {
91
+ stream.on('data', (chunk) => {
92
+ res = Buffer.concat([res, chunk], res.byteLength + chunk.byteLength);
93
+ });
94
+ stream.on('end', () => {
95
+ resolve(res);
96
+ this.state.running = false;
97
+ this.len -= 1;
98
+ });
99
+ });
210
100
  }
211
- }]);
212
- return FtpClient;
213
- }();
101
+ }
214
102
  exports.FtpClient = FtpClient;
215
- var FtpClientPool = /*#__PURE__*/function () {
216
- function FtpClientPool(_ref3) {
217
- var _ref3$maxConnection = _ref3.maxConnection,
218
- maxConnection = _ref3$maxConnection === void 0 ? 5 : _ref3$maxConnection,
219
- options = _objectWithoutPropertiesLoose(_ref3, _excluded2);
220
- this.options = void 0;
221
- this.maxConnection = void 0;
222
- this.pool = [];
223
- this.get = this.bind('get');
224
- this.put = this.bind('put');
225
- this["delete"] = this.bind('delete');
226
- this.list = this.bind('list');
227
- this.rename = this.bind('rename');
228
- this.options = options;
229
- this.maxConnection = maxConnection;
230
- }
231
- var _proto2 = FtpClientPool.prototype;
232
- _proto2.bind = function bind(fn) {
233
- var _this5 = this;
234
- return _async(function () {
235
- var client = _this5.client;
236
- return _await(client[fn].apply(client, arguments), function (res) {
237
- if (client.len < 1) {
238
- var index = _this5.pool.findIndex(function (item) {
239
- return item.id === client.id;
240
- });
241
- _this5.pool.splice(index, 1);
103
+ class FtpClientPool {
104
+ options;
105
+ maxConnection;
106
+ pool = [];
107
+ constructor({ maxConnection = 5, ...options }) {
108
+ this.options = options;
109
+ this.maxConnection = maxConnection;
110
+ }
111
+ get = this.bind('get');
112
+ put = this.bind('put');
113
+ delete = this.bind('delete');
114
+ list = this.bind('list');
115
+ rename = this.bind('rename');
116
+ get len() {
117
+ return this.pool.length;
118
+ }
119
+ get client() {
120
+ if (this.pool.length < this.maxConnection) {
121
+ const client = new FtpClient(this.options);
122
+ this.pool.push(client);
123
+ return client;
242
124
  }
243
- return res;
244
- });
245
- });
246
- };
247
- _createClass(FtpClientPool, [{
248
- key: "len",
249
- get: function get() {
250
- return this.pool.length;
125
+ this.pool.sort((a, b) => a.len - b.len);
126
+ return this.pool[0];
251
127
  }
252
- }, {
253
- key: "client",
254
- get: function get() {
255
- if (this.pool.length < this.maxConnection) {
256
- var client = new FtpClient(this.options);
257
- this.pool.push(client);
258
- return client;
259
- }
260
- this.pool.sort(function (a, b) {
261
- return a.len - b.len;
262
- });
263
- return this.pool[0];
128
+ bind(fn) {
129
+ return async (...args) => {
130
+ const client = this.client;
131
+ const res = await client[fn](...args);
132
+ if (client.len < 1) {
133
+ const index = this.pool.findIndex((item) => item.id === client.id);
134
+ this.pool.splice(index, 1);
135
+ }
136
+ return res;
137
+ };
264
138
  }
265
- }]);
266
- return FtpClientPool;
267
- }();
268
- exports.FtpClientPool = FtpClientPool;
139
+ }
140
+ exports.FtpClientPool = FtpClientPool;