edgeport - v1.0.3
    Preparing search index...

    Interface FtpSession

    An authenticated FTP session over a single control connection.

    Obtain one from connect. It is an AsyncDisposable, so it can be scoped with await using to guarantee a clean QUIT and socket close. Methods issue one command at a time and must not be called concurrently on the same session, since each data transfer opens its own passive connection while the control channel is in use.

    1.0.0

    interface FtpSession {
        "[asyncDispose]"(): PromiseLike<void>;
        close(): Promise<void>;
        cwd(path: string): Promise<void>;
        delete(path: string): Promise<void>;
        ensureDir(path: string): Promise<void>;
        exists(path: string): Promise<boolean>;
        get(
            path: string,
            opts?: FtpGetOptions,
        ): Promise<Uint8Array<ArrayBufferLike>>;
        getJson<T = unknown>(path: string): Promise<T>;
        getStream(
            path: string,
            opts?: FtpGetOptions,
        ): Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>;
        getText(path: string, opts?: FtpGetOptions): Promise<string>;
        list(path?: string): Promise<FtpEntry[]>;
        mkdir(path: string): Promise<void>;
        mtime(path: string): Promise<Date>;
        nameList(path?: string): Promise<string[]>;
        put(path: string, data: Uint8Array, opts?: FtpPutOptions): Promise<void>;
        putJson(
            path: string,
            value: unknown,
            opts?: { space?: number },
        ): Promise<void>;
        putText(path: string, content: string, opts?: FtpPutOptions): Promise<void>;
        pwd(): Promise<string>;
        removeAll(path: string): Promise<void>;
        rename(from: string, to: string): Promise<void>;
        rmdir(path: string): Promise<void>;
        size(path: string): Promise<number>;
    }

    Hierarchy

    • AsyncDisposable
      • FtpSession
    Index
    • Returns PromiseLike<void>

    • Sends QUIT and closes the control connection.

      Returns Promise<void>

      Resolves once the socket is closed.

      1.0.0

    • Changes the working directory via CWD.

      Parameters

      • path: string

        Directory to change into.

      Returns Promise<void>

      If the server rejects the command.

      1.0.0

      await session.cwd('/pub');
      
    • Deletes a file via DELE.

      Parameters

      • path: string

        Path of the file to delete.

      Returns Promise<void>

      If the server rejects the command.

      1.0.0

      await session.delete('/incoming/old.csv');
      
    • Creates a directory and all missing parents, like mkdir -p.

      FTP MKD is single-level, so this splits path on / and issues MKD for each cumulative segment, ignoring the 550 "already exists" failure on segments that are already present. A leading / is preserved so absolute paths create from the root; a relative path creates under the current working directory. This is a client-side walk: it makes one round trip per segment and is not atomic.

      Parameters

      • path: string

        Directory path to create, with / separating segments.

      Returns Promise<void>

      If the control connection fails.

      If a segment fails for a reason other than already existing.

      1.0.2

      await session.ensureDir('/incoming/2026/reports');
      
    • Reports whether a path exists by probing it with SIZE.

      Issues SIZE <path> and treats a 213 reply as existence and a 550 (or any other command failure surfaced as ProtocolError) as absence. Because SIZE is a file-oriented probe, some servers reject it for directories even when they exist; for a directory, prefer FtpSession.cwd or FtpSession.list. The path is resolved by the server relative to the current working directory unless it is absolute.

      Parameters

      • path: string

        Path to probe (absolute, or relative to the working directory).

      Returns Promise<boolean>

      true if the server reports a size for the path, false if it is missing.

      If the control connection fails.

      1.0.2

      if (await session.exists('/pub/readme.txt')) {
      const bytes = await session.get('/pub/readme.txt');
      }
    • Retrieves a file's bytes via RETR.

      Defaults to binary (TYPE I). Pass { type: 'ascii' } for a text-mode transfer (TYPE A, server line-ending conversion) or { offset } to resume an interrupted download from a byte offset (REST <offset> before RETR), in which case the returned bytes are the tail of the file from that offset.

      Parameters

      • path: string

        Path of the file to fetch.

      • Optionalopts: FtpGetOptions

        Optional transfer type and resume offset.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The file's raw bytes (the tail from opts.offset when set).

      If the server rejects the command.

      1.0.0

      const bytes = await session.get('/pub/readme.txt');
      const text = await session.get('/pub/readme.txt', { type: 'ascii' });
      const tail = await session.get('/pub/big.bin', { offset: 1024 });
    • Retrieves a file and parses it as JSON.

      A convenience over FtpSession.get that decodes the bytes as UTF-8 and runs JSON.parse. The type parameter T annotates the parsed shape; it is not validated at runtime.

      Type Parameters

      • T = unknown

        The expected shape of the parsed JSON.

      Parameters

      • path: string

        Path of the JSON file to fetch.

      Returns Promise<T>

      The parsed value, typed as T.

      If the server rejects the command.

      If the body is not valid JSON.

      1.0.2

      interface Config { name: string }
      const cfg = await session.getJson<Config>('/etc/app/config.json');
    • Retrieves a file as a stream via RETR, for payloads too large to buffer.

      The returned stream owns the data connection; read it to completion (or cancel it) so the underlying socket closes. Accepts the same type and offset options as FtpSession.get.

      Parameters

      • path: string

        Path of the file to fetch.

      • Optionalopts: FtpGetOptions

        Optional transfer type and resume offset.

      Returns Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>

      A readable stream of the file's bytes.

      If the server rejects the command.

      1.0.0

      const stream = await session.getStream('/var/log/big.log');
      await stream.pipeTo(someWritable);
    • Retrieves a file and decodes its bytes as UTF-8 text.

      A convenience over FtpSession.get that runs the same RETR transfer and decodes the result with TextDecoder. Accepts the same transfer options as get.

      Parameters

      • path: string

        Path of the file to fetch.

      • Optionalopts: FtpGetOptions

        Optional transfer type and resume offset (see FtpGetOptions).

      Returns Promise<string>

      The file contents decoded as UTF-8.

      If the server rejects the command.

      1.0.2

      const text = await session.getText('/pub/readme.txt');
      
    • Lists a directory via LIST, parsing Unix ls -l lines into FtpEntry records.

      Parameters

      • Optionalpath: string

        Directory to list; omit for the current working directory.

      Returns Promise<FtpEntry[]>

      One entry per line, with the raw line always preserved.

      If the server rejects the command.

      1.0.0

      for (const entry of await session.list('/pub')) {
      console.log(entry.isDirectory ? 'dir' : 'file', entry.name);
      }
    • Creates a directory via MKD.

      Parameters

      • path: string

        Path of the directory to create.

      Returns Promise<void>

      If the server rejects the command.

      1.0.0

      await session.mkdir('/incoming/2026');
      
    • Returns a file's last-modified time via MDTM.

      Issues MDTM <path> and parses the 213 YYYYMMDDHHMMSS reply as a UTC timestamp. Per RFC 3659 the MDTM time is always UTC. A fractional-seconds suffix (.sss), when the server sends one, is parsed too. The path is resolved by the server relative to the current working directory unless it is absolute.

      Parameters

      • path: string

        Path of the file to stat.

      Returns Promise<Date>

      The modification time as a Date.

      If the server rejects the command or returns a malformed timestamp.

      1.0.2

      const when = await session.mtime('/pub/readme.txt');
      
    • Lists bare names via NLST.

      Parameters

      • Optionalpath: string

        Directory to list; omit for the current working directory.

      Returns Promise<string[]>

      One name per line.

      If the server rejects the command.

      1.0.0

      const names = await session.nameList('/pub');
      
    • Stores bytes to a file via STOR (or APPE).

      Defaults to binary (TYPE I). Pass { type: 'ascii' } for a text-mode transfer, or resume an interrupted upload with { append: true } (use APPE, sending only the remaining bytes) or { offset } (issue REST <offset> before STOR, overwriting from that offset).

      Parameters

      • path: string

        Destination path.

      • data: Uint8Array

        The bytes to write.

      • Optionalopts: FtpPutOptions

        Optional transfer type and resume mode (append or offset).

      Returns Promise<void>

      If the server rejects the command.

      1.0.0

      await session.put('/incoming/report.csv', new TextEncoder().encode('a,b,c\n'));
      // resume an upload that died after the first 1024 bytes
      await session.put('/incoming/big.bin', rest, { append: true });
    • Serializes a value as JSON and stores it via STOR.

      A convenience over FtpSession.put that runs JSON.stringify, encodes the result as UTF-8, and uploads it.

      Parameters

      • path: string

        Destination path.

      • value: unknown

        The value to serialize; must be JSON-serializable.

      • Optionalopts: { space?: number }

        Optional formatting; space is forwarded to JSON.stringify for indentation.

      Returns Promise<void>

      If the server rejects the command.

      1.0.2

      await session.putJson('/etc/app/config.json', { name: 'edge' }, { space: 2 });
      
    • Encodes a string as UTF-8 and stores it via STOR.

      A convenience over FtpSession.put that encodes content with TextEncoder and uploads it. Accepts the same transfer options as put.

      Parameters

      • path: string

        Destination path.

      • content: string

        The text to write; encoded as UTF-8.

      • Optionalopts: FtpPutOptions

        Optional transfer type and resume mode (see FtpPutOptions).

      Returns Promise<void>

      If the server rejects the command.

      1.0.2

      await session.putText('/incoming/note.txt', 'hello world\n');
      
    • Returns the current working directory via PWD.

      Returns Promise<string>

      The absolute working directory reported by the server.

      If the server rejects the command.

      1.0.0

      const dir = await session.pwd();
      
    • Recursively deletes a directory tree (files then directories, depth-first).

      Walks path with LIST, deleting files with DELE and recursing into subdirectories, then removes each emptied directory with RMD. This is a client-side walk: it makes many round trips (O(N) for N entries) and is not atomic, so a failure partway through leaves the tree partly deleted. Rejects when path is empty or / as a guard against wiping the server root. The path is resolved by the server relative to the current working directory unless it is absolute.

      Parameters

      • path: string

        Directory tree to remove; must not be empty or /.

      Returns Promise<void>

      If path is empty or /, or the server rejects a command.

      If the control connection fails.

      1.0.2

      await session.removeAll('/incoming/2025');
      
    • Renames or moves a path via RNFR/RNTO.

      Parameters

      • from: string

        Existing path.

      • to: string

        New path.

      Returns Promise<void>

      If the server rejects either command.

      1.0.0

      await session.rename('/tmp/a.txt', '/tmp/b.txt');
      
    • Removes a directory via RMD.

      Parameters

      • path: string

        Path of the directory to remove.

      Returns Promise<void>

      If the server rejects the command.

      1.0.0

      await session.rmdir('/incoming/2025');
      
    • Returns a file's size in bytes via SIZE.

      Parameters

      • path: string

        Path of the file to measure.

      Returns Promise<number>

      The size in bytes.

      If the server rejects the command or returns a non-numeric size.

      1.0.0

      const n = await session.size('/pub/readme.txt');