ftp-service
Advanced tools
Comparing version 1.1.1 to 2.0.0
@@ -0,1 +1,10 @@ | ||
# [2.0.0](https://github.com/nfroidure/ftp-service/compare/v1.1.1...v2.0.0) (2021-11-02) | ||
### Features | ||
* **types:** switch to strict types ([d99977e](https://github.com/nfroidure/ftp-service/commit/d99977eb0dae9abf6eb6c4d400f44dec27323d48)) | ||
## [1.1.1](https://github.com/nfroidure/ftp-service/compare/v1.1.0...v1.1.1) (2021-04-11) | ||
@@ -2,0 +11,0 @@ |
@@ -5,8 +5,7 @@ /// <reference types="node" /> | ||
import type { DelayService, LogService } from 'common-services'; | ||
import type { Provider } from 'knifecycle'; | ||
export declare const DEFAULT_FTP_PASSWORD_ENV_NAME = "FTP_PASSWORD"; | ||
export declare type FTPConfig = { | ||
FTP?: Parameters<InstanceType<typeof FTPClient>['access']>[0]; | ||
FTP_TIMEOUT?: ConstructorParameters<typeof FTPClient>[0]; | ||
FTP_POOL?: Parameters<typeof createPool>[1]; | ||
FTP_PASSWORD_ENV_NAME?: string; | ||
export declare type FTP_ENV<T extends string extends T ? never : string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME> = Record<T, string>; | ||
export declare type FTPConfig<T extends string extends T ? never : string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME> = { | ||
FTP: NonNullable<Parameters<InstanceType<typeof FTPClient>['access']>[0]>; | ||
FTP_CONFIG: { | ||
@@ -19,7 +18,10 @@ base: string; | ||
}; | ||
FTP_POOL?: Parameters<typeof createPool>[1]; | ||
FTP_TIMEOUT?: ConstructorParameters<typeof FTPClient>[0]; | ||
FTP_PASSWORD_ENV_NAME?: T; | ||
}; | ||
export declare type FTPDependencies = FTPConfig & { | ||
ENV: Record<string, string>; | ||
export declare type FTPDependencies<T extends string extends T ? never : string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME> = FTPConfig<T> & { | ||
ENV: FTP_ENV<T>; | ||
delay: DelayService; | ||
log?: LogService; | ||
log: LogService; | ||
}; | ||
@@ -32,3 +34,52 @@ export declare type FTPService = { | ||
}; | ||
declare const _default: import("knifecycle").ProviderInitializer<FTPDependencies, FTPService>; | ||
declare const _default: typeof initFTPService; | ||
export default _default; | ||
/** | ||
* Instantiate the FTP service | ||
* @name initFTPService | ||
* @function | ||
* @param {Object} services | ||
* The services to inject | ||
* @param {Object} [services.ENV] | ||
* An environment object | ||
* @param {Function} services.FTP | ||
* The configuration object as given to `basic-ftp` | ||
* client `access` method | ||
* @param {Function} services.FTP_CONFIG | ||
* The FTP service configuration object | ||
* @param {Function} [services.FTP_POOL] | ||
* The FTP pool configuration object as given to | ||
* `generic-pool`. | ||
* @param {Function} [services.FTP_TIMEOUT] | ||
* The FTP service timeout as given to `basic-ftp` | ||
* client constructor | ||
* @param {Function} [services.FTP_PASSWORD_ENV_NAME] | ||
* The environment variable name in which to pick-up the | ||
* FTP password | ||
* @param {Function} services.log | ||
* A logging function | ||
* @param {Function} [services.delay] | ||
* A service to manage delays | ||
* @return {Promise<FTPService>} | ||
* A promise of the FTP service | ||
* @example | ||
* import initFTPService from 'ftp-service'; | ||
* import { initDelayService } from 'common-services'; | ||
* | ||
* const delay = await initDelayService({ | ||
* log: console.log.bind(console), | ||
* }) | ||
* const jwt = await initFTPService({ | ||
* FTP: { | ||
* host: 'localhost', | ||
* user: 'user', | ||
* pasword: 'pwd', | ||
* }, | ||
* FTP_CONFIG: { base: '' }, | ||
* delay, | ||
* log: console.log.bind(console), | ||
* }); | ||
* | ||
* const files = await ftp.list('/); | ||
*/ | ||
declare function initFTPService<T extends string extends T ? never : string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME>({ FTP, FTP_CONFIG, FTP_POOL, FTP_TIMEOUT, FTP_PASSWORD_ENV_NAME, ENV, delay, log, }: FTPDependencies<T>): Promise<Provider<FTPService>>; |
@@ -22,3 +22,3 @@ "use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
@@ -38,7 +38,3 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
*/ | ||
var _default = (0, _knifecycle.initializer)({ | ||
name: 'ftp', | ||
type: 'provider', | ||
inject: ['FTP', 'FTP_CONFIG', '?FTP_POOL', '?FTP_TIMEOUT', '?FTP_PASSWORD_ENV_NAME', '?ENV', 'delay', 'log'] | ||
}, initFTPService); | ||
var _default = (0, _knifecycle.provider)(initFTPService, 'ftp', ['FTP', 'FTP_CONFIG', '?FTP_POOL', '?FTP_TIMEOUT', '?FTP_PASSWORD_ENV_NAME', '?ENV', 'delay', 'log']); | ||
/** | ||
@@ -66,3 +62,3 @@ * Instantiate the FTP service | ||
* FTP password | ||
* @param {Function} [services.log] | ||
* @param {Function} services.log | ||
* A logging function | ||
@@ -103,3 +99,3 @@ * @param {Function} [services.delay] | ||
FTP_PASSWORD_ENV_NAME = DEFAULT_FTP_PASSWORD_ENV_NAME, | ||
ENV = {}, | ||
ENV, | ||
delay, | ||
@@ -137,3 +133,4 @@ log | ||
try { | ||
log('debug', await finalFTPClient.destroy()); | ||
log('debug', '💾 - Disconnecting a FTP service instance.'); | ||
await finalFTPClient.destroy(); | ||
} catch (err) { | ||
@@ -253,4 +250,4 @@ const wrappedErr = _yerror.default.wrap(err, 'E_FTP_DISCONNECT'); | ||
}, pool, fn, attempts = 0) { | ||
let finalErr; | ||
let ftpClient; | ||
let finalErr = undefined; | ||
let ftpClient = undefined; | ||
@@ -277,3 +274,4 @@ try { | ||
log('error', '💾 - Could not realease the FTP client.', wrappedReleaseErr.stack); | ||
log('error', '💾 - Could not release the FTP client.'); | ||
log('error-stack', wrappedReleaseErr.stack); | ||
finalErr = finalErr || wrappedReleaseErr; | ||
@@ -285,3 +283,4 @@ } | ||
if (FTP_CONFIG.retry && attempts < FTP_CONFIG.retry.attempts) { | ||
log('info', `💾 - Retrying an FTP work (attempt ${attempts + 1}/${FTP_CONFIG.retry.attempts})`, finalErr.stack); | ||
log('info', `💾 - Retrying an FTP work (attempt ${attempts + 1}/${FTP_CONFIG.retry.attempts})`); | ||
log('debug-stack', finalErr.stack); | ||
await delay.create(FTP_CONFIG.retry.delay || 0); | ||
@@ -297,4 +296,7 @@ return doFTPWork({ | ||
throw _yerror.default.wrap(finalErr, 'E_FTP_PUT', FTP.host); | ||
} | ||
} // Not supposed to reach that code | ||
throw new _yerror.default('E_FTP_ERROR'); | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -13,3 +13,3 @@ "use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
@@ -21,3 +21,2 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
describe('FTP service', () => { | ||
const ENV = {}; | ||
const CONFIG = { | ||
@@ -32,3 +31,3 @@ FTP: { | ||
min: 0, | ||
max: 2, | ||
max: 1, | ||
maxWaitingClients: 10, | ||
@@ -48,3 +47,6 @@ evictionRunIntervalMillis: 20 | ||
const ftp = await (0, _.default)(_objectSpread(_objectSpread({}, CONFIG), {}, { | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { | ||
FTP_PASSWORD: 'password' | ||
}, | ||
delay, | ||
@@ -61,2 +63,3 @@ log | ||
`); | ||
ftp.dispose && (await ftp.dispose()); | ||
}); | ||
@@ -69,3 +72,6 @@ test('should list files', async () => { | ||
} = await (0, _.default)(_objectSpread(_objectSpread({}, CONFIG), {}, { | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD2', | ||
ENV: { | ||
FTP_PASSWORD2: 'password' | ||
}, | ||
delay, | ||
@@ -75,3 +81,3 @@ log | ||
const files = await ftp.list('/'); | ||
await dispose(); | ||
dispose && (await dispose()); | ||
expect({ | ||
@@ -100,2 +106,6 @@ files, | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -111,3 +121,6 @@ } | ||
} = await (0, _.default)(_objectSpread(_objectSpread({}, CONFIG), {}, { | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { | ||
FTP_PASSWORD: 'password' | ||
}, | ||
delay, | ||
@@ -117,3 +130,3 @@ log | ||
const fileContent = (await ftp.get('/testfile.txt')).toString(); | ||
await dispose(); | ||
dispose && (await dispose()); | ||
expect({ | ||
@@ -140,2 +153,6 @@ fileContent, | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -151,3 +168,6 @@ } | ||
} = await (0, _.default)(_objectSpread(_objectSpread({}, CONFIG), {}, { | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { | ||
FTP_PASSWORD: '' | ||
}, | ||
delay, | ||
@@ -169,3 +189,3 @@ log | ||
}); | ||
await dispose(); | ||
dispose && (await dispose()); | ||
expect({ | ||
@@ -203,2 +223,6 @@ exists, | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -214,3 +238,6 @@ } | ||
} = await (0, _.default)(_objectSpread(_objectSpread({}, CONFIG), {}, { | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { | ||
FTP_PASSWORD: 'password' | ||
}, | ||
delay, | ||
@@ -231,3 +258,3 @@ log | ||
const exists = (0, _fs.existsSync)(_path.default.join(__dirname, '..', 'fixtures', 'testfile3.txt')); | ||
await dispose(); | ||
dispose && (await dispose()); | ||
expect({ | ||
@@ -255,2 +282,6 @@ exists, | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -257,0 +288,0 @@ } |
{ | ||
"name": "ftp-service", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "A simple wrapper for a simpler FTP client surface API with pool and retry management.", | ||
@@ -75,34 +75,33 @@ "main": "dist/index", | ||
"dependencies": { | ||
"@types/generic-pool": "^3.1.9", | ||
"@types/generic-pool": "^3.1.10", | ||
"basic-ftp": "^4.6.6", | ||
"common-services": "^9.0.1", | ||
"generic-pool": "^3.7.8", | ||
"knifecycle": "^11.1.1", | ||
"common-services": "^10.0.0", | ||
"generic-pool": "^3.8.2", | ||
"knifecycle": "^12.0.1", | ||
"yerror": "^6.0.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.13.14", | ||
"@babel/core": "^7.13.15", | ||
"@babel/eslint-parser": "^7.13.14", | ||
"@babel/plugin-proposal-class-properties": "^7.13.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.13.8", | ||
"@babel/preset-env": "^7.13.15", | ||
"@babel/preset-typescript": "^7.13.0", | ||
"@babel/register": "^7.13.14", | ||
"@types/jest": "^26.0.22", | ||
"@typescript-eslint/eslint-plugin": "^4.21.0", | ||
"@typescript-eslint/parser": "^4.21.0", | ||
"babel-plugin-knifecycle": "^5.0.1", | ||
"commitizen": "^4.2.3", | ||
"@babel/cli": "^7.16.0", | ||
"@babel/core": "^7.16.0", | ||
"@babel/eslint-parser": "^7.16.0", | ||
"@babel/plugin-proposal-class-properties": "^7.16.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.16.0", | ||
"@babel/preset-env": "^7.16.0", | ||
"@babel/preset-typescript": "^7.16.0", | ||
"@babel/register": "^7.16.0", | ||
"@types/jest": "^27.0.2", | ||
"@typescript-eslint/eslint-plugin": "^5.2.0", | ||
"@typescript-eslint/parser": "^5.2.0", | ||
"commitizen": "^4.2.4", | ||
"conventional-changelog-cli": "^2.1.1", | ||
"coveralls": "^3.1.0", | ||
"coveralls": "^3.1.1", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"eslint": "^7.23.0", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"jest": "^26.6.3", | ||
"jsarch": "^4.0.1", | ||
"jsdoc-to-markdown": "^7.0.1", | ||
"eslint": "^8.1.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.3.1", | ||
"jsarch": "^5.0.0", | ||
"jsdoc-to-markdown": "^7.1.0", | ||
"metapak": "^4.0.2", | ||
"metapak-nfroidure": "11.0.7", | ||
"prettier": "^2.2.1", | ||
"metapak-nfroidure": "11.1.0", | ||
"prettier": "^2.4.1", | ||
"rimraf": "^3.0.2", | ||
@@ -109,0 +108,0 @@ "typescript": "^4.2.4" |
@@ -12,7 +12,2 @@ [//]: # ( ) | ||
[![Coverage Status](https://coveralls.io/repos/github/nfroidure/ftp-service/badge.svg?branch=master)](https://coveralls.io/github/nfroidure/ftp-service?branch=master) | ||
[![NPM version](https://badge.fury.io/js/ftp-service.svg)](https://npmjs.org/package/ftp-service) | ||
[![Dependency Status](https://david-dm.org/nfroidure/ftp-service.svg)](https://david-dm.org/nfroidure/ftp-service) | ||
[![devDependency Status](https://david-dm.org/nfroidure/ftp-service/dev-status.svg)](https://david-dm.org/nfroidure/ftp-service#info=devDependencies) | ||
[![Package Quality](https://npm.packagequality.com/shield/ftp-service.svg)](https://packagequality.com/#?package=ftp-service) | ||
[![Code Climate](https://codeclimate.com/github/nfroidure/ftp-service.svg)](https://codeclimate.com/github/nfroidure/ftp-service) | ||
@@ -44,3 +39,3 @@ | ||
| [services.FTP_PASSWORD_ENV_NAME] | <code>function</code> | The environment variable name in which to pick-up the FTP password | | ||
| [services.log] | <code>function</code> | A logging function | | ||
| services.log | <code>function</code> | A logging function | | ||
| [services.delay] | <code>function</code> | A service to manage delays | | ||
@@ -47,0 +42,0 @@ |
@@ -8,3 +8,2 @@ import initFTPService from '.'; | ||
describe('FTP service', () => { | ||
const ENV = {}; | ||
const CONFIG: FTPConfig = { | ||
@@ -19,3 +18,3 @@ FTP: { | ||
min: 0, | ||
max: 2, | ||
max: 1, | ||
maxWaitingClients: 10, | ||
@@ -38,3 +37,4 @@ evictionRunIntervalMillis: 20, | ||
...CONFIG, | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { FTP_PASSWORD: 'password' }, | ||
delay, | ||
@@ -52,2 +52,4 @@ log, | ||
`); | ||
ftp.dispose && (await ftp.dispose()); | ||
}); | ||
@@ -59,3 +61,4 @@ | ||
...CONFIG, | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD2', | ||
ENV: { FTP_PASSWORD2: 'password' }, | ||
delay, | ||
@@ -67,3 +70,3 @@ log, | ||
await dispose(); | ||
dispose && (await dispose()); | ||
@@ -93,2 +96,6 @@ expect({ | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -103,3 +110,4 @@ } | ||
...CONFIG, | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { FTP_PASSWORD: 'password' }, | ||
delay, | ||
@@ -111,3 +119,3 @@ log, | ||
await dispose(); | ||
dispose && (await dispose()); | ||
@@ -135,2 +143,6 @@ expect({ | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -145,3 +157,4 @@ } | ||
...CONFIG, | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { FTP_PASSWORD: '' }, | ||
delay, | ||
@@ -169,3 +182,3 @@ log, | ||
await dispose(); | ||
dispose && (await dispose()); | ||
@@ -204,2 +217,6 @@ expect({ | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -214,3 +231,4 @@ } | ||
...CONFIG, | ||
ENV, | ||
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD', | ||
ENV: { FTP_PASSWORD: 'password' }, | ||
delay, | ||
@@ -240,3 +258,3 @@ log, | ||
await dispose(); | ||
dispose && (await dispose()); | ||
@@ -265,2 +283,6 @@ expect({ | ||
], | ||
Array [ | ||
"debug", | ||
"💾 - Disconnecting a FTP service instance.", | ||
], | ||
], | ||
@@ -267,0 +289,0 @@ } |
141
src/index.ts
import path from 'path'; | ||
import { initializer, Provider } from 'knifecycle'; | ||
import { provider } from 'knifecycle'; | ||
import { createPool } from 'generic-pool'; | ||
@@ -9,10 +9,23 @@ import { Client as FTPClient } from 'basic-ftp'; | ||
import type { Pool } from 'generic-pool'; | ||
import type { | ||
ProviderInitializer, | ||
Dependencies, | ||
Service, | ||
Provider, | ||
} from 'knifecycle'; | ||
export const DEFAULT_FTP_PASSWORD_ENV_NAME = 'FTP_PASSWORD'; | ||
export type FTPConfig = { | ||
FTP?: Parameters<InstanceType<typeof FTPClient>['access']>[0]; | ||
FTP_TIMEOUT?: ConstructorParameters<typeof FTPClient>[0]; | ||
FTP_POOL?: Parameters<typeof createPool>[1]; | ||
FTP_PASSWORD_ENV_NAME?: string; | ||
export type FTP_ENV< | ||
T extends string extends T | ||
? never | ||
: string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME, | ||
> = Record<T, string>; | ||
export type FTPConfig< | ||
T extends string extends T | ||
? never | ||
: string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME, | ||
> = { | ||
FTP: NonNullable<Parameters<InstanceType<typeof FTPClient>['access']>[0]>; | ||
FTP_CONFIG: { | ||
@@ -25,7 +38,14 @@ base: string; | ||
}; | ||
FTP_POOL?: Parameters<typeof createPool>[1]; | ||
FTP_TIMEOUT?: ConstructorParameters<typeof FTPClient>[0]; | ||
FTP_PASSWORD_ENV_NAME?: T; | ||
}; | ||
export type FTPDependencies = FTPConfig & { | ||
ENV: Record<string, string>; | ||
export type FTPDependencies< | ||
T extends string extends T | ||
? never | ||
: string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME, | ||
> = FTPConfig<T> & { | ||
ENV: FTP_ENV<T>; | ||
delay: DelayService; | ||
log?: LogService; | ||
log: LogService; | ||
}; | ||
@@ -51,19 +71,16 @@ export type FTPService = { | ||
export default initializer( | ||
{ | ||
name: 'ftp', | ||
type: 'provider', | ||
inject: [ | ||
'FTP', | ||
'FTP_CONFIG', | ||
'?FTP_POOL', | ||
'?FTP_TIMEOUT', | ||
'?FTP_PASSWORD_ENV_NAME', | ||
'?ENV', | ||
'delay', | ||
'log', | ||
], | ||
}, | ||
initFTPService, | ||
); | ||
export default provider( | ||
initFTPService as ProviderInitializer<Dependencies, Service>, | ||
'ftp', | ||
[ | ||
'FTP', | ||
'FTP_CONFIG', | ||
'?FTP_POOL', | ||
'?FTP_TIMEOUT', | ||
'?FTP_PASSWORD_ENV_NAME', | ||
'?ENV', | ||
'delay', | ||
'log', | ||
], | ||
) as typeof initFTPService; | ||
@@ -92,3 +109,3 @@ /** | ||
* FTP password | ||
* @param {Function} [services.log] | ||
* @param {Function} services.log | ||
* A logging function | ||
@@ -119,3 +136,7 @@ * @param {Function} [services.delay] | ||
*/ | ||
async function initFTPService({ | ||
async function initFTPService< | ||
T extends string extends T | ||
? never | ||
: string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME, | ||
>({ | ||
FTP, | ||
@@ -125,7 +146,7 @@ FTP_CONFIG, | ||
FTP_TIMEOUT, | ||
FTP_PASSWORD_ENV_NAME = DEFAULT_FTP_PASSWORD_ENV_NAME, | ||
ENV = {}, | ||
FTP_PASSWORD_ENV_NAME = DEFAULT_FTP_PASSWORD_ENV_NAME as T, | ||
ENV, | ||
delay, | ||
log, | ||
}: FTPDependencies): Promise<Provider<FTPService>> { | ||
}: FTPDependencies<T>): Promise<Provider<FTPService>> { | ||
/* Architecture Note #1.1: Pool | ||
@@ -161,4 +182,8 @@ | ||
} catch (err) { | ||
log('error', '💾 - FTP connection failure:', err.stack); | ||
throw YError.wrap(err, 'E_FTP_CONNECT'); | ||
log( | ||
'error', | ||
'💾 - FTP connection failure:', | ||
(err as Error).stack as string, | ||
); | ||
throw YError.wrap(err as Error, 'E_FTP_CONNECT'); | ||
} | ||
@@ -169,6 +194,11 @@ }, | ||
try { | ||
log('debug', await finalFTPClient.destroy()); | ||
log('debug', '💾 - Disconnecting a FTP service instance.'); | ||
await finalFTPClient.destroy(); | ||
} catch (err) { | ||
const wrappedErr = YError.wrap(err, 'E_FTP_DISCONNECT'); | ||
log('error', '💾 - FTP disconnection failure:', wrappedErr.stack); | ||
const wrappedErr = YError.wrap(err as Error, 'E_FTP_DISCONNECT'); | ||
log( | ||
'error', | ||
'💾 - FTP disconnection failure:', | ||
wrappedErr.stack as string, | ||
); | ||
throw wrappedErr; | ||
@@ -203,3 +233,3 @@ } | ||
throw YError.wrap( | ||
err, | ||
err as Error, | ||
'E_FTP_GET', | ||
@@ -221,3 +251,3 @@ FTP.host, | ||
new Promise<Buffer>((resolve, reject) => { | ||
const chunks = []; | ||
const chunks: Buffer[] = []; | ||
@@ -227,3 +257,3 @@ stream.once('end', () => resolve(Buffer.concat(chunks))); | ||
stream.on('readable', () => { | ||
let data; | ||
let data: Buffer; | ||
while ((data = stream.read())) { | ||
@@ -248,3 +278,3 @@ chunks.push(data); | ||
throw YError.wrap( | ||
err, | ||
err as Error, | ||
'E_FTP_GET', | ||
@@ -290,3 +320,3 @@ FTP.host, | ||
throw YError.wrap( | ||
err, | ||
err as Error, | ||
'E_FTP_PUT', | ||
@@ -318,3 +348,3 @@ FTP.host, | ||
throw YError.wrap( | ||
err, | ||
err as Error, | ||
'E_FTP_GET', | ||
@@ -345,3 +375,3 @@ FTP.host, | ||
R, | ||
T extends (ftpClient: PoolFTPService) => Promise<R> | ||
T extends (ftpClient: PoolFTPService) => Promise<R>, | ||
>( | ||
@@ -363,4 +393,4 @@ { | ||
): Promise<R> { | ||
let finalErr: Error; | ||
let ftpClient: PoolFTPService; | ||
let finalErr: Error | undefined = undefined; | ||
let ftpClient: PoolFTPService | undefined = undefined; | ||
@@ -371,25 +401,22 @@ try { | ||
} catch (err) { | ||
throw YError.wrap(err, 'E_FTP_CONNECT', FTP.host); | ||
throw YError.wrap(err as Error, 'E_FTP_CONNECT', FTP.host); | ||
} | ||
return await fn(ftpClient); | ||
} catch (err) { | ||
finalErr = err; | ||
finalErr = err as Error; | ||
} finally { | ||
try { | ||
if (finalErr) { | ||
await pool.destroy(ftpClient); | ||
await pool.destroy(ftpClient as PoolFTPService); | ||
} else { | ||
await pool.release(ftpClient); | ||
await pool.release(ftpClient as PoolFTPService); | ||
} | ||
} catch (releaseErr) { | ||
const wrappedReleaseErr = YError.wrap( | ||
releaseErr, | ||
releaseErr as Error, | ||
'E_FTP_RELEASE', | ||
FTP.host, | ||
); | ||
log( | ||
'error', | ||
'💾 - Could not realease the FTP client.', | ||
wrappedReleaseErr.stack, | ||
); | ||
log('error', '💾 - Could not release the FTP client.'); | ||
log('error-stack', wrappedReleaseErr.stack as string); | ||
finalErr = finalErr || wrappedReleaseErr; | ||
@@ -405,4 +432,4 @@ } | ||
})`, | ||
finalErr.stack, | ||
); | ||
log('debug-stack', finalErr.stack as string); | ||
await delay.create(FTP_CONFIG.retry.delay || 0); | ||
@@ -413,2 +440,4 @@ return doFTPWork({ FTP, FTP_CONFIG, delay, log }, pool, fn, attempts + 1); | ||
} | ||
// Not supposed to reach that code | ||
throw new YError('E_FTP_ERROR'); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127801
25
1715
68
+ Added@sinonjs/commons@2.0.0(transitive)
+ Added@sinonjs/fake-timers@9.1.2(transitive)
+ Added@sinonjs/samsam@7.0.1(transitive)
+ Added@types/node@17.0.45(transitive)
+ Addedcommon-services@10.0.3(transitive)
+ Addeddiff@5.2.0(transitive)
+ Addedknifecycle@12.0.4(transitive)
+ Addedsinon@14.0.2(transitive)
+ Addedtype-fest@2.19.0(transitive)
- Removed@sinonjs/fake-timers@7.1.2(transitive)
- Removed@sinonjs/samsam@6.1.3(transitive)
- Removed@types/node@14.18.63(transitive)
- Removedcommon-services@9.0.1(transitive)
- Removeddiff@4.0.2(transitive)
- Removedknifecycle@11.1.1(transitive)
- Removedsinon@10.0.1(transitive)
- Removedtype-fest@1.4.0(transitive)
Updated@types/generic-pool@^3.1.10
Updatedcommon-services@^10.0.0
Updatedgeneric-pool@^3.8.2
Updatedknifecycle@^12.0.1