Skip to content

Commit

Permalink
fix(env): allow env to be undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Aug 17, 2023
1 parent 606f3c9 commit b404538
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
6 changes: 3 additions & 3 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The `ftp` service creates easily usable FTP features
by exposing `list`, `put`, `get` and `delete`
methods and handling any unecessary complexity.

[See in context](./src/index.ts#L62-L67)
[See in context](./src/index.ts#L63-L68)



Expand All @@ -27,7 +27,7 @@ The `ftp` service creates easily usable FTP features
The service uses a pool to allow several parallel connections
to a FTP server.

[See in context](./src/index.ts#L147-L151)
[See in context](./src/index.ts#L161-L165)



Expand All @@ -36,5 +36,5 @@ The service uses a pool to allow several parallel connections
One can configure the FTP service to retry several times
before abandonnating the requested operation.

[See in context](./src/index.ts#L355-L359)
[See in context](./src/index.ts#L367-L371)

66 changes: 36 additions & 30 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('FTP service', () => {
host: process.env.FTP_HOST || 'localhost',
port: process.env.FTP_PORT ? parseInt(process.env.FTP_PORT, 10) : 21,
user: 'user',
password: 'password',
},
FTP_TIMEOUT: 30000,
FTP_POOL: {
Expand All @@ -34,7 +33,6 @@ describe('FTP service', () => {
const delay = (await initDelayService({})).service;
const ftp = await initFTPService({
...CONFIG,
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD',
ENV: { FTP_PASSWORD: 'password' },
delay,
log,
Expand Down Expand Up @@ -101,9 +99,13 @@ describe('FTP service', () => {
test('should retrieve files', async () => {
const delay = (await initDelayService({})).service;
const { service: ftp, dispose } = await initFTPService({
...CONFIG,
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD',
ENV: { FTP_PASSWORD: 'password' },
...{
...CONFIG,
FTP: {
...CONFIG.FTP,
password: 'password',
},
},
delay,
log,
});
Expand All @@ -116,38 +118,42 @@ describe('FTP service', () => {
fileContent,
logCalls: log.mock.calls,
}).toMatchInlineSnapshot(`
{
"fileContent": "This is a simple text file!",
"logCalls": [
[
"debug",
"💾 - FTP Successfully connected!",
],
[
"debug",
"💾 - Retrieved a file from FTP:",
"/testfile.txt",
27,
],
[
"debug",
"💾 - Shutting down the FTP pool.",
],
[
"debug",
"💾 - Disconnecting a FTP service instance.",
],
],
}
`);
{
"fileContent": "This is a simple text file!",
"logCalls": [
[
"warning",
"⚠️ - Setting the password in the FTP config is unsafe.",
],
[
"debug",
"💾 - FTP Successfully connected!",
],
[
"debug",
"💾 - Retrieved a file from FTP:",
"/testfile.txt",
27,
],
[
"debug",
"💾 - Shutting down the FTP pool.",
],
[
"debug",
"💾 - Disconnecting a FTP service instance.",
],
],
}
`);
});

test('should send files', async () => {
const delay = (await initDelayService({})).service;
const { service: ftp, dispose } = await initFTPService({
...CONFIG,
FTP_PASSWORD_ENV_NAME: 'FTP_PASSWORD',
ENV: { FTP_PASSWORD: '' },
ENV: { FTP_PASSWORD: 'password' },
delay,
log,
});
Expand Down
26 changes: 19 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import type {
} from 'knifecycle';

export const DEFAULT_FTP_PASSWORD_ENV_NAME = 'FTP_PASSWORD';
export const DEFAULT_ENV = {};

export type FTP_ENV<
export type FTPEnvVars<
T extends string extends T
? never
: string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME,
> = Record<T, string>;
> = Partial<Record<T, string>>;

export type FTPConfig<
T extends string extends T
Expand All @@ -43,7 +44,7 @@ export type FTPDependencies<
? never
: string = typeof DEFAULT_FTP_PASSWORD_ENV_NAME,
> = FTPConfig<T> & {
ENV: FTP_ENV<T>;
ENV?: FTPEnvVars<T>;
delay: DelayService;
log: LogService;
};
Expand Down Expand Up @@ -140,10 +141,23 @@ async function initFTPService<
FTP_POOL,
FTP_TIMEOUT,
FTP_PASSWORD_ENV_NAME = DEFAULT_FTP_PASSWORD_ENV_NAME as T,
ENV,
ENV = DEFAULT_ENV,
delay,
log,
}: FTPDependencies<T>): Promise<Provider<FTPService>> {
const ftpPasswordName = (FTP_PASSWORD_ENV_NAME ||
DEFAULT_FTP_PASSWORD_ENV_NAME) as T;
const ftpPassword = ENV?.[ftpPasswordName];

if ('password' in FTP) {
log('warning', `⚠️ - Setting the password in the FTP config is unsafe.`);
}

if (!('password' in FTP) && !ftpPassword) {
log('error', `❌ - No "${ftpPasswordName}" env var set.`);
throw new YError('E_BAD_FTPEnvVars', ftpPasswordName);
}

/* Architecture Note #1.1: Pool
The service uses a pool to allow several parallel connections
Expand All @@ -157,9 +171,7 @@ async function initFTPService<

await ftpClient.access({
...FTP,
...(ENV[FTP_PASSWORD_ENV_NAME]
? { password: ENV[FTP_PASSWORD_ENV_NAME] }
: {}),
...(ftpPassword ? { password: ftpPassword } : {}),
});

const finalFTPClient: PoolFTPService = {
Expand Down

0 comments on commit b404538

Please sign in to comment.