DefinitelyTyped / DefinitelyTyped/DefinitelyTyped

[@types/ssh2] Issue with readdir callback retuning FileEntry

Open
#67,521 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
51.4k
Forks
30.4k
Avg merge
3d 21h
Merged PRs (30d)
108

Description

Discussed in https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/67360

Originally posted by gdelory November 9, 2023
Hi,

I feel like there is something wrong with sftp.readir callback type. We are in the process of migrating to TypeScripr (from pure Javascript), and the code we had using the attrs.isDirectory() and attrs.isSymbolicLink() and is not compiling, while it works in Javascript.

Here the code failing (ssh2-issue.ts):

import { Client } from 'ssh2'

const main = async () => {
  const conn = new Client()
  conn.on('ready', () => {
    conn.sftp(async (err, sftp) => {
      if (err) {
        console.error(err)
      } else {
        sftp.readdir('/home/g/d/gdelory', (err, res) => {
          for (let i = 0; i < res.length; i++) {
            const e = res[i]
            console.log(e.attrs.isDirectory())
            console.log(e.attrs.isSymbolicLink())
          }
          conn.end()
        })
      }
    })
  }).on('error', console.error).connect({
    host: 'rtpgsa.ibm.com',
    port: 22,
    username: 'gdelory',
    password: '*****************'
  })
}
main()

With the following errors:

src/ssh2-issue.ts(13,33): error TS2339: Property 'isDirectory' does not exist on type 'Attributes'.
src/ssh2-issue.ts(14,33): error TS2339: Property 'isSymbolicLink' does not exist on type 'Attributes'.

Note that the exact same code works fine in JS, and if I run this following snippet:

import { Client } from 'ssh2'

const main = async () => {
  const conn = new Client()
  conn.on('ready', () => {
    conn.sftp(async (err, sftp) => {
      if (err) {
        console.error(err)
      } else {
        sftp.readdir('/home/g/d/gdelory', (err, res) => {
          for (let i = 0; i < res.length; i++) {
            const e = res[i]
            console.log(e.attrs)
          }
          conn.end()
        })
      }
    })
  }).on('error', console.error).connect({
    host: 'rtpgsa.ibm.com',
    port: 22,
    username: 'gdelory',
    password: '***************'
  })
}
main()

It outputs the following:

Stats {
  mode: 33571312,
  uid: 544506,
  gid: 988871,
  size: 4096,
  atime: 1699529953,
  mtime: 1673417659,
  extended: undefined
}
Stats {
  mode: 33572288,
  uid: 544506,
  gid: 988871,
  size: 4096,
  atime: 1699529953,
  mtime: 1646028984,
  extended: undefined
}
Stats {
  mode: 33572349,
  uid: 544506,
  gid: 988871,
  size: 4096,
  atime: 1699529953,
  mtime: 1646028986,
  extended: undefined
}

Proving that attrs is of type Stats, not Attrs.

Also the code I found in the rpo seems to say the same. Here an instance of Stats is created, in the readAttrs function which seems to be called here when using readdir.

So I think these lines:

export interface FileEntry {
    filename: string;
    longname: string;
    attrs: Attributes;
}

Should be

export interface FileEntry {
    filename: string;
    longname: string;
    attrs: Stats;
}

What you do think?

Version info:

I'm on the last version of ssh2 (1.1.14):

~/wfm$ npm list ssh2
wfm@2.0.0 /home/gui/wfm
└── ssh2@1.14.0

and I think the last version the type module:

~/wfm$ npm list @types/ssh2
wfm@2.0.0 /home/gui/wfm
└── @types/ssh2@1.11.16

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with types/ssh2/index.d.ts, especially the FileEntry and readdir declarations, then compare them with ssh2's SFTP.js readAttrs and readdir implementation. Update the declaration if the runtime object is a Stats instance rather than Attributes, and verify that the provided TypeScript example accepts isDirectory() and isSymbolicLink().

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.