Skip to content

Commit 2981d38

Browse files
committed
fix: refactoring & fixes after code review
1 parent 0429d1e commit 2981d38

8 files changed

Lines changed: 661 additions & 619 deletions

File tree

shared/packages/api/src/inputApi.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,16 @@ export namespace Accessor {
438438
type: AccessType.FTP
439439

440440
/** The type of FTP server:
441-
* - 'ftp' for unsecure FTP
442-
* - 'ftps' for FTP over TLS
443-
* - 'sftp' for SFTP/SSH
441+
* - 'ftp': plain ftp
442+
* - 'ftps': FTP over TLS (explicit / "AUTH TLS" extension)
443+
* - 'ftp'-ssl: FTP over SSL (implicit)
444+
* - 'sftp': SFTP over SSH
444445
*/
445-
serverType: 'ftp' | 'ftps' | 'sftp'
446+
serverType:
447+
| 'ftp' // plain ftp
448+
| 'ftps' // FTP over TLS (explicit / "AUTH TLS" extension)
449+
| 'ftp-ssl' // FTP over SSL (implicit)
450+
| 'sftp' // SFTP over SSH
446451

447452
/** Hostname/IP Address to the host server */
448453
host: string

shared/packages/worker/src/worker/accessorHandlers/ftp.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ import { MonitorInProgress } from '../lib/monitorInProgress'
3030
import { defaultCheckHandleRead, defaultCheckHandleWrite, defaultDoYouSupportAccess } from './lib/lib'
3131

3232
import { isEqual } from '../lib/lib'
33-
import { FTPClient, FTPClientBase, FTPOptions, SFTPClient } from './lib/FTPClient'
3433
import { GenericFileOperationsHandler } from './lib/GenericFileOperations'
3534
import { GenericFileHandler } from './lib/GenericFileHandler'
3635
import { JSONWriteFilesBestEffortHandler } from './lib/json-write-file'
36+
import { createFTPClient, FTPClientBase, FTPOptions } from './lib/FTPClient/index'
3737

3838
export interface Content {
3939
/** This is set when the class-instance is only going to be used for PackageContainer access.*/
@@ -221,22 +221,15 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
221221
const pResponse = ftp.upload(sourceStream, fullPath)
222222

223223
pResponse
224-
.then((response) => {
225-
if (response !== null) {
226-
streamWrapper.emit('error', new Error(`FTP upload failed: ${response}`))
227-
} else {
228-
// All good:
229-
streamWrapper.emit('close')
230-
}
224+
.then(() => {
225+
streamWrapper.emit('close')
231226
})
232227
.catch((err) => {
233-
streamWrapper.emit('error', new Error(`FTP upload threw for path "${fullPath}": ${err}`))
228+
const err2 = new Error(`FTP upload threw for path "${fullPath}": ${err}`)
229+
if (err instanceof Error) err2.stack += `Original stack:\n${err.stack}`
230+
streamWrapper.emit('error', err2)
234231
})
235232

236-
// Pipe any events from the writeStream right into the wrapper:
237-
// writeStream.on('error', (err) => streamWrapper.emit('error', err))
238-
// writeStream.on('close', () => streamWrapper.emit('close'))
239-
240233
return streamWrapper
241234
}
242235
async getPackageReadInfo(): Promise<{ readInfo: PackageReadInfo; cancel: () => void }> {
@@ -378,7 +371,13 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
378371
} {
379372
const serverType = this.accessor.serverType
380373
const host = this.accessor.host
381-
const port = this.accessor.port ?? (this.accessor.serverType === 'ftp' ? 21 : 22) // default port for FTP is 21, SFTP is 22
374+
const port =
375+
this.accessor.port ??
376+
(this.accessor.serverType === 'ftp' || this.accessor.serverType === 'ftps'
377+
? 21
378+
: this.accessor.serverType === 'sftp'
379+
? 22
380+
: 990) // default port for FTP (and FTP + AUTH TLS) is 21, SFTP is 22, FTPS (implicit FTP over TLS/SSL) is 990
382381
const username = this.accessor.username
383382
const password = this.accessor.password
384383

@@ -411,10 +410,12 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
411410
// Note: this is untested:
412411
if (ftpOptions.serverType === 'ftp') {
413412
url = `ftp://`
414-
} else if (ftpOptions.serverType === 'sftp') {
415-
url = `sftp://`
413+
} else if (ftpOptions.serverType === 'ftp-ssl') {
414+
url = `ftps://`
416415
} else if (ftpOptions.serverType === 'ftps') {
417416
url = `ftps://`
417+
} else if (ftpOptions.serverType === 'sftp') {
418+
url = `sftp://`
418419
} else {
419420
assertNever(ftpOptions.serverType)
420421
throw new Error(`Unsupported FTP server type "${ftpOptions.serverType}"`)
@@ -460,13 +461,7 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
460461
}
461462
if (!cachedClient) {
462463
// Set up a new FTP client:
463-
if (ftpOptions.serverType === 'ftp' || ftpOptions.serverType === 'ftps') {
464-
cachedClient = new FTPClient(this.worker.logger, options)
465-
} else if (ftpOptions.serverType === 'sftp') {
466-
cachedClient = new SFTPClient(this.worker.logger, options)
467-
} else {
468-
assertNever(ftpOptions.serverType)
469-
}
464+
cachedClient = createFTPClient(ftpOptions.serverType, this.worker.logger, options)
470465
}
471466

472467
if (cachedClient) {

0 commit comments

Comments
 (0)