-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[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
Conversation
re: #536 I extended the client options from Url, because of the funny parsing done, but actually the Also TODO: remove the |
typings/lib/client.d.ts
Outdated
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typings/lib/client.d.ts
Outdated
* _reconnect - implement reconnection | ||
* @api privateish | ||
*/ | ||
private _reconnect(); |
There was a problem hiding this comment.
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
typings/lib/client-options.d.ts
Outdated
/** | ||
* MQTT CLIENT | ||
*/ | ||
export interface ClientOptions extends SecureClientOptions, Url { |
There was a problem hiding this comment.
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
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this 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?
Removing the private members was my initial impulse also, but one of the m$
devs made a point that it helps when extending the class to not
accidentally over-ride private members. The tsc compiler will balk on any
attempts to use the members, even on extended classes, where `protected`
must be used.
Having said that, you could argue it's less a maintenance burden to only
expose the public API.
Thoughts?
I'm easy either way really. Out at the moment so can't make any changes.
One other thing to consider is how much of the `url.parse` options do we
actually want in the ClientOptions. The 'slashes' etc ... it's kinda messy.
|
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) |
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? |
At this point, just making sure if those are valid it's good. |
I mean linting them. |
Yeah, that seems like a good simple step 👍 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", |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 ...
lib/connect/index.js
Outdated
opts = xtend(parsed, opts) | ||
|
||
if (opts.protocol === null) { | ||
// Options object always override brokerUrl specified options |
There was a problem hiding this comment.
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')
})
lib/connect/index.js
Outdated
// protocol | ||
// clientId | ||
{ | ||
hostname: parsed.hostname, |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I removed private variables, and deprecated the |
The tests on Travis pass, but because of Js code changes related to hostname, the coverage has gone |
lib/connect/index.js
Outdated
// protocol | ||
// clientId | ||
{ | ||
// hostname: parsed.hostname, |
There was a problem hiding this comment.
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.
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. |
My intention was to clean up the messy extending of the IClientOptions with
*ALL* the url.parse variables. It's quite glaring once you see it all
mapped out in TypeScript declarations, so I got a bit carried away making
alterations, while removing the url jibberish like `hash`/`query` etc. We
use hostname/host variously, while in server we use `host`.
I added a deprecation warning for `hostname`, for later removal but if
removing the rest warrants a sem-ver change then so be it. Is that an
issue? I can separate the changes into multiple pull requests if you'd like.
|
^ while in the *servers* config array we use `host`.
|
@sublimator are you ok in closing this? Or it's a mistake? |
I will open another one later on :) Out at the moment
|
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.