Skip to content

[WIP] Add TypeScript 2.1 typings #547

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 6 commits into from
Closed

Conversation

sublimator
Copy link
Member

@sublimator sublimator commented Feb 20, 2017

These headers are exported from a branch where I converted the lib/**/*.js to TypeScript. The tests all passed, when I modified them slightly to make sure they referenced package.json main pointing to the tsconfig.compilerOptions.outDir.

@sublimator
Copy link
Member Author

re: #536

I extended the client options from Url, because of the funny parsing done, but actually the port is more generally a number so need to fix that.

Also TODO: remove the _\w+ private functions

on(event: 'error', cb: OnErrorCallback): this;
on(event: string, cb: Function): any;
once(event: 'message', cb: OnMessageCallback): this;
once(event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

image

* _reconnect - implement reconnection
* @api privateish
*/
private _reconnect();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to kill these off

/**
* MQTT CLIENT
*/
export interface ClientOptions extends SecureClientOptions, Url {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just inline the Url options ... playing around

@sublimator
Copy link
Member Author

@sublimator sublimator mentioned this pull request Feb 28, 2017
@codecov
Copy link

codecov bot commented Mar 8, 2017

Codecov Report

Merging #547 into master will increase coverage by 0.31%.
The diff coverage is 87.71%.

@@            Coverage Diff             @@
##           master     #547      +/-   ##
==========================================
+ Coverage   92.62%   92.93%   +0.31%     
==========================================
  Files           8        8              
  Lines         637      665      +28     
  Branches      151      156       +5     
==========================================
+ Hits          590      618      +28     
  Misses         47       47
Impacted Files Coverage Δ
lib/connect/tcp.js 100% <100%> (ø)
lib/connect/tls.js 95.45% <100%> (+1.01%)
lib/connect/ws.js 80.43% <37.5%> (+3.35%)
lib/connect/index.js 95.91% <95.12%> (-1.31%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6d8091d...18f6a81. Read the comment docs.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid adding those for the private methods?
Also, is there a way to automatically test those?

@sublimator
Copy link
Member Author

sublimator commented Mar 8, 2017 via email

@RangerMauve
Copy link
Contributor

With MQTT in the wild, I haven't ever needed to extend it directly, favoring composition over inheritance when adding new capabilities. I would vote for not exposing the private variables for simplicity. (for autocomplete, for example)

@sublimator
Copy link
Member Author

So it seems like the consensus is to remove the private variables. There was some discussion about having an option to omit them in the automatically generated .d.ts files (which is what these are) but seems like MS dug their heels in on this one. I'll just remove them manually.

@mcollina As far as tests go, we'd need to have a typescript compiler added to the dev-dependencies, which is not really a biggie. What sort of testing did you have in mind? Just simple tests (connect, subscribe, publish, receive etc) or more extensive ones to somehow notice when the declarations ones get out of sync?

@mcollina
Copy link
Member

mcollina commented Mar 9, 2017

At this point, just making sure if those are valid it's good.

@mcollina
Copy link
Member

mcollina commented Mar 9, 2017

I mean linting them.

@sublimator
Copy link
Member Author

Yeah, that seems like a good simple step 👍
Ok, I will remove the private variables, install tslint and add it to the CI build :)

Probably tomorrow

@@ -84,6 +86,9 @@
"snazzy": "^6.0.0",
"standard": "^8.6.0",
"through2": "^2.0.3",
"tslint": "^4.5.1",
"tslint-config-standard": "^4.0.0",
"typescript": "^2.2.1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescript is a peer dependency of tslint

import { QoS } from './types'

export interface IClientOptions extends ISecureClientOptions {
// The options are extended by url.parse() fields in connect() function
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina

What do you want exposed here? And once we've decided, should we only extend the IClientOptions with those, rather than
just grabbing the kitchen sink?

> u.parse('mqtt://www.broker.com/mqtt')
Url {
  protocol: 'mqtt:',
  slashes: true,
  auth: null,
  host: 'www.broker.com',
  port: null,
  hostname: 'www.broker.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/mqtt',
  path: '/mqtt',
  href: 'mqtt://www.broker.com/mqtt' }

I'll switch to TypeScript branch and find usages and report back

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina I had a crack at cleaning up the url.parse/xtend section. Take a look

* (see Connection#connect)
*/
export declare class MqttClient extends events.EventEmitter {
// public connackTimer: NodeJS.Timer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these weren't prefixed with a _ so I marked them public (default in TypeScript if not specifide) (the first tslint configuration, before I used the standard variant wanted explicit so I just used the --fix options)

In any case, I guess we don't really want them exposed?

opts.clientId = opts.query.clientId
}
// Someone out in the wild might be using {auth: 'user:pass'} out in the wild
// Keep old behaviour? Tests actually pass without this.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd just remove this ...

opts = xtend(parsed, opts)

if (opts.protocol === null) {
// Options object always override brokerUrl specified options
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way due to a test:

    it('should throw an error when called with no protocol specified - with options', function () {
      (function () {
        mqtt.connect('tcp://foo.bar.com', { protocol: null })
      }).should.throw('Missing protocol')
    })

// protocol
// clientId
{
hostname: parsed.hostname,
Copy link
Member Author

@sublimator sublimator Mar 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that the servers option uses {host, port} keys:

      opts.host = opts.servers[client._reconnectCount].host
      opts.port = opts.servers[client._reconnectCount].port
      opts.hostname = opts.host

Perhaps at some point it would be nice just to use one or the other (of hostname/host)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean use only one of host or hostname consistently

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrmmm, ok, so I looked up the url.parse() function and

image

host is supposedly the hosname + ':' + port

@sublimator
Copy link
Member Author

@mcollina

Ok, I removed private variables, and deprecated the hostname option in lieu of host which is used in the servers: [{host: 'localhost', port: 1337},...] option, for consistency. I also removed most of the jibberish url parse options (hash, query, etc), reducing it to just port (as a number) host, path, protocol and clientId and username / password.

@sublimator
Copy link
Member Author

The tests on Travis pass, but because of Js code changes related to hostname, the coverage has gone down by a fraction so it's marked as failed.

// protocol
// clientId
{
// hostname: parsed.hostname,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could argue that to be consistent with deprecating, should leave the hostname until it's completely removed. Assuming you guys are down with that direction.

@mcollina
Copy link
Member

Can you please do not touch/remove/alter any existing functionality in this PR? I'm not sure what you want to achieve, and that is a semver-major change, while this is not.

I'll comment on the typescript questions later on.

@sublimator
Copy link
Member Author

sublimator commented Mar 10, 2017 via email

@sublimator
Copy link
Member Author

sublimator commented Mar 10, 2017 via email

@sublimator sublimator closed this Mar 10, 2017
@mcollina
Copy link
Member

@sublimator are you ok in closing this? Or it's a mistake?

@sublimator
Copy link
Member Author

sublimator commented Mar 10, 2017 via email

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants