blinkdog / telnet-stream

Transform streams that emit TELNET negotiations as events
GNU Affero General Public License v3.0
26 stars 5 forks source link

Type definitions for TypeScript #9

Closed Voakie closed 3 years ago

Voakie commented 4 years ago

The d.ts file documents the structure of the module and how to use it. That is not only useful for normal JavaScript projects, but also required for using the module in TypeScript projects (which is my use case).

It also allows for popups like this in VS Code for example:

I hope you find this useful 😊. Also, thanks for the great module.

willsmanley commented 3 years ago

@Voakie thanks for generating these! You probably also want to add "types": "telnet-stream.d.ts" to package.json.

willsmanley commented 3 years ago

In fact for me to get it to work, I had to put telnet-stream.d.ts inside of the lib directory. Otherwise, npm install did not include the types file.

So my package.json entry was: "types": "lib/telnet-stream.d.ts"

willsmanley commented 3 years ago

I also wrote a debugging extension that allows for logging the commands being written to the socket, accompanied by line endings. I kept communicating with mail servers and forgetting to include \r\n.

So I added this function:

/**
 * Replaces real line endings with representations of line
 * ending characters. Useful for logging in situations where
 * presence of certain line endings is important.
 *
 * @param string
 * @returns string
 */
const explicitLineEndings = (string) => {
  return string
    .replace(/\r\n/, '\\r\\n')
    .replace(/\n\r/, '\\n\\r')
    .replace(/\r/, '\\r')
    .replace(/\n/, '\\n');
}

module.exports = {explicitLineEndings}

And modified the write method thusly:

write() {
      if(process.env.NODE_ENV !== 'production'){
        for(let argument of arguments){
          console.log('WRITE: ', typeof argument === 'string' ? explicitLineEndings(argument) : argument)
        }
      }
      return this._out.write.apply(this._out, arguments);
    }

I figure that this debugging logic is outside the scope of this package, but LMK if you or anyone else would find it useful. It could be standardized across other methods and could use a debug variable instead of using NODE_ENV.

Voakie commented 3 years ago

@famouspotatoes I've added the types declaration to the package.json now, but I don't see how moving the file to lib would change anything. Could you check again if that actually was the issue? Also, lib is in .gitignore, so I don't think I should be adding anything there.

willsmanley commented 3 years ago

Sorry I meant to say that I put the file inside src/ while ultimately compiles into lib/.

I might have had some weird problems with npm caching because my behavior was unexpected. I would just stick with what you've got in the latest commit.