Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactor to typescript #691

Merged
merged 25 commits into from Aug 18, 2019
Merged

Refactor to typescript #691

merged 25 commits into from Aug 18, 2019

Conversation

@hassankhan
Copy link
Contributor

@hassankhan hassankhan commented Jul 28, 2019

Hi there,

As part of homebridge#2266, I wanted to give this project a bit of Typescript love too.

I've converted everything except the lib/gen/import.js, since I'm not quite sure if that file will continue to live on. I tried running the script, and it didn't generate as many devices as the existing file had 😕

I've also added Jest and started writing some very basic unit tests, which I'll add on later.

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 2, 2019

The PR is now ready for review + testing 👍

@KhaosT
Copy link
Contributor

@KhaosT KhaosT commented Aug 2, 2019

Thanks I’ll try to take a look this weekend 🙏

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 2, 2019

Really appreciate it @KhaosT 👍.

Once you check out the branch, it'd be good to run yarn and yarn build to update the dependencies and compile the Typescript respectively. Then run node dist/BridgedCore.js (for example) and you should see some output in the CLI.

@KhaosT
Copy link
Contributor

@KhaosT KhaosT commented Aug 4, 2019

@hassankhan I tired to run the built version and when trying to pair with the accessory, I run into

/dist/lib/util/tlv.js:22
    if (data.length <= 255) {
             ^

TypeError: Cannot read property 'length' of undefined
src/lib/util/tlv.ts Outdated Show resolved Hide resolved
@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 4, 2019

Thanks for taking a look at the branch, @KhaosT 👍 I was wondering if you had a "test plan" that I could use myself?

@KhaosT
Copy link
Contributor

@KhaosT KhaosT commented Aug 4, 2019

@hassankhan There isn't a formalized test plan 😅 In general for changes like this I'll probably test the following:

  1. Start a fresh accessory (not paired with HomeKit before), try pair with it from iOS device, control it and make sure sample accessories work as expected.
  2. Shutdown the instance, and restart it, verify iOS can re-establish secure session.

Then from homebridge side, test with one or two simple plugins, make sure API matches what original implementation is.

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 5, 2019

Hi @KhaosT, no worries about a formal test plan 😄 , it'd be really nice to one day have some sort of integration tests, but I'm not sure how (without messing around with the iOS simulator).

So far, I've tried the following and things seem to be working fine:

  • yarn && yarn build
  • node dist/BridgedCore.js
  • Open Home app on iOS and add new Accessory
  • Use pincode from src/BridgedCore.ts
  • Once accessory is paired, try changing values on all the bridged sample accessories
  • Killed the running session with Ctrl-C and then restarted it, once again tried changing values on all bridged sample accessories

So far, all of the above worked perfectly. Should I be testing Core.ts and CameraCore.ts in the same way?

I haven't yet tested Homebridge using this branch, that will be next.

So with a few minor changes, it seems to work with Homebridge. I've made the changes in the Typescript branch I opened there.

@KhaosT KhaosT merged commit bb0c5b9 into homebridge:master Aug 18, 2019
@KhaosT
Copy link
Contributor

@KhaosT KhaosT commented Aug 18, 2019

v0.5.0 is released for this change 🎉

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 18, 2019

Thank you so much @KhaosT! Do let me know if anything comes up, more than happy to fix any issues I may have introduced 👍

@hassankhan hassankhan deleted the hassankhan:refactor-to-typescript branch Aug 18, 2019
@KhaosT
Copy link
Contributor

@KhaosT KhaosT commented Aug 18, 2019

@hassankhan thank you for converting the project to typescript 🙏 I think the only thing need to fix is that buffer-shims type definition lookup with ts-node. And maybe add instruction for people to who want to run Core.js directly (it's rare but I think some people use it in that way)? Otherwise I tried a few projects linking directly to hap-nodejs and they all function fine.

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 19, 2019

... And maybe add instruction for people to who want to run Core.js directly (it's rare but I think some people use it in that way)?

Am one of those ‘rare’ people that still use Core.js but will continue using the older HAP-Node.js version until I truly understand the benefit that typescript brings to HAP-node specifically. Regardless, thanks and respect to all your combined efforts for maintaining this awesome repo.

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 19, 2019

v0.5.0 is released for this change 🎉

Can’t find v0.5.0 under releases ? https://github.com/KhaosT/HAP-NodeJS/releases. I guess you are only labelling some commits using release names but not creating the releases. https://github.com/KhaosT/HAP-NodeJS/commits/master

@KhaosT
Copy link
Contributor

@KhaosT KhaosT commented Aug 19, 2019

@aboulfad For some reasons, npm version stopped tagging the release. I'll look into fixing that when I have time ^^

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 19, 2019

@aboulfad For some reasons, npm version stopped tagging the release. I'll look into fixing that when I have time ^^

Thanks, no rush :-) in the meantime one can alway checkout older commits that are labelled using release tags (e.g git checkout tags/<tag_name> -b <branch_name> )

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 19, 2019

until I truly understand the benefit that typescript brings to HAP-node specifically.

Hi @aboulfad, I just thought I'd address your (valid) concern about TypeScript. Ultimately, to an end-user of this project, it wouldn't bring any direct benefits, at least in the short-term.

The groups of people who would see an immediate benefit are (a) HAP-NodeJS contributors, and (b) projects that use HAP-NodeJS as a core component. Thanks to most of the types now being declared (and TypeScript's code-completion functionality) you can now at a glance understand what data types are being passed around. This is extremely useful for new HAP-NodeJS contributors who may never have interacted with the codebase before, and would not otherwise (easily) be able to understand those same data types. It is also very useful for any downstream projects, since any changes/additions/removals are easily communicated through the types file (although obviously they should also be communicated via documentation etc.)

Regardless, thanks and respect to all your combined efforts for maintaining this awesome repo.

Thank you for your kind words 😃, big thanks to @KhaosT for being awesome and merging this in so quickly 👍

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 19, 2019

Thank you @hassankhan for a nice explanation. Now you peeked my curiously, I read quickly about typescript so maybe I’ll give the master branch a chance and see how easy is to migrate to using ts. From the look of it, it should be transparent in theory ...

Ok I spoke too fast, guys there are some API changes that will affect end-users using HAP-NodeJS directly, for example the .set/get/updatevalue. The changes aren't major, but will force those using HAP-Node to understand those changes and learn a bit typescript ...

I compared an accessory file from 0.4.53 to master to show the differences.

@n0rt0nthec4t
Copy link
Contributor

@n0rt0nthec4t n0rt0nthec4t commented Aug 20, 2019

I'm also one of those users who run Core.js. Had a look this morning and now unsure what is required to migrate previously developed accessories to this new codebase... Guess now need to understand what is typescript and all that it brings

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 20, 2019

@n0rt0nthec4t , actually the impact is independent of Core or BridgedCore. It affects any accessories developed using HAP-Node. We have the choice to stay on pre ts, or learn and embark on this journey. Check my above post with the diff between pre and post ts.

@n0rt0nthec4t
Copy link
Contributor

@n0rt0nthec4t n0rt0nthec4t commented Aug 20, 2019

@aboulfad Thanks. I think for the moment will stay on the pre-ts and see how things shake out. Too may accessories to update and don't quite have the time to learn typescript

@OliverS79
Copy link

@OliverS79 OliverS79 commented Aug 22, 2019

@KhaosT @hassankhan - Dear both, Thanks for the TypeScript version. As I am still new to TS, I have the following challenge. I follow the readme instruction and try to run ts-node Core.ts in the src folder. However, I get the following error: "Could not find a declaration file for module 'buffer-shims'"

npm install @types/buffer-shims does not work either.

Can you give me some hint on how to get this one work? Have I missed something in the readme file?

Best,
Oliver

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 23, 2019

@OliverS79 FYI, your issue is referenced twice in this thread but no mention of a fix has been delivered or a workaround.

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 23, 2019

Hi @aboulfad, @OliverS79,

I've been extremely busy with work during the week, I will try and get a PR to hopefully resolve some of these issues for you.

@aboulfad The diff link you posted above does not work for me. If I can get an example of what broke, I can try and fix it 😄

As a workaround, can you try running node dist/Core.js instead?

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 23, 2019

@hassankhan , I fixed the differences link in my above post.
Though I never said the ts changes broke something, rather that they change somewhat the syntax for common HAP-Node methods/functions. As a consequence existing JS accessory files will have to change. I only quickly glanced at this, but it is evident that basic JS users will have to learn a bit about TS to modify their scripts IF they want to use HAP-NodeTS.

Example:
Using JS:

lightAccessory
  .addService(Service.Lightbulb, LightController.name) 
  .getCharacteristic(Characteristic.On)	
  .on('set', function(value, callback)

becomes using TS:

lightAccessory
  .addService(Service.Lightbulb, LightController.name) 
  .getCharacteristic(Characteristic.On)
  .on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
    LightController.setPower(value);
@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 23, 2019

@aboulfad thanks for updating the link.

I think I should probably change the documentation somewhat... no-one should need to convert their projects to start using Typescript, you should be able to continue using existing Javascript syntax with no change in behaviour. The only time you should start seeing any differences is when you (re)write your code in Typescript.

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 23, 2019

@hassankhan , my bad ... looking at the accessory examples led me to believe that one would have to make those changes. So you changed all accessory files as an example how to write an accessory using TS ?

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Aug 23, 2019

@aboulfad Yes, unfortunately I did. Again, totally my fault for not updating the documentation - sorry for the trouble it caused you 😞

@aboulfad
Copy link

@aboulfad aboulfad commented Aug 23, 2019

^^^ No trouble at all, it was just a simple quick diff that showed whats going on :-) And now we have examples for those that want to migrate their scripts to TS :-)

Your work is very much appreciated as it improves this project's codebase and pushes us junior/hobbyists to learn !

@n0rt0nthec4t
Copy link
Contributor

@n0rt0nthec4t n0rt0nthec4t commented Sep 13, 2019

@KhaosT @hassankhan - I get the following error: "Could not find a declaration file for module 'buffer-shims'"

npm install @types/buffer-shims does not work either.

Been trying to install this new typescript beast with teh latest code base and still get the error as above.

I'm very much in two minds about this change to the code base, from an end-user perspective it seems very much retrograde step. No fast startup for Core.js(ts) amore, as ts-node takes forever to verify everything sigh

@hassankhan
Copy link
Contributor Author

@hassankhan hassankhan commented Sep 17, 2019

Hi all, sorry for the delay in responding - had some IRL issues to iron out.

@n0rt0nthec4t I'm terribly sorry for the issues you're experiencing ... I've got a branch with some documentation updates, hopefully I can help clarify things there. Do you also run your own fork of HAP-NodeJS or are you using it as a dependency?

@Supereg
Copy link
Member

@Supereg Supereg commented Sep 17, 2019

@hassankhan
I'm using HAP-NodeJS as a dependency in my project. Get the following two error messages when I try to compile my typescript project:

node_modules/hap-nodejs/dist/lib/Advertiser.d.ts:1:55 - error TS7016: Could not find a declaration file for module 'bonjour-hap'. '/project-path/node_modules/bonjour-hap/index.js' implicitly has an 'any' type.
  Try `npm install @types/bonjour-hap` if it exists or add a new declaration (.d.ts) file containing `declare module 'bonjour-hap';`

1 import { BonjourHap, MulticastOptions, Service } from 'bonjour-hap';
                                                        ~~~~~~~~~~~~~

node_modules/hap-nodejs/dist/lib/HAPServer.d.ts:2:17 - error TS7016: Could not find a declaration file for module 'fast-srp-hap'. '/project-path/node_modules/fast-srp-hap/index.js' implicitly has an 'any' type.
  Try `npm install @types/fast-srp-hap` if it exists or add a new declaration (.d.ts) file containing `declare module 'fast-srp-hap';`

2 import srp from 'fast-srp-hap';
                  ~~~~~~~~~~~~~~


Found 2 errors.
@n0rt0nthec4t
Copy link
Contributor

@n0rt0nthec4t n0rt0nthec4t commented Sep 17, 2019

Hi all, sorry for the delay in responding - had some IRL issues to iron out.

@n0rt0nthec4t I'm terribly sorry for the issues you're experiencing ... I've got a branch with some documentation updates, hopefully I can help clarify things there. Do you also run your own fork of HAP-NodeJS or are you using it as a dependency?

Just run as a dependency for my accessories, previously node Core.js would start up instantly but not with the typescript thing, takes around 1+ min on a rpi zero. So for me, this is very much backwards code base. Will be staying on 0.4.x atm

@Supereg
Copy link
Member

@Supereg Supereg commented Sep 17, 2019

@n0rt0nthec4t this is the thing with typescript. Before every run it transpiles to javascript and then gets executed (when running with ts-node). You could potentially compile it once and then only run the compiled java script code. Although on every update you need to compile again.

@n0rt0nthec4t
Copy link
Contributor

@n0rt0nthec4t n0rt0nthec4t commented Sep 18, 2019

@n0rt0nthec4t this is the thing with typescript. Before every run it transpiles to javascript and then gets executed (when running with ts-node). You could potentially compile it once and then only run the compiled java script code. Although on every update you need to compile again.

Guessing then the compile to JS would only be needed if you update the HAP-NodeJS code, and if your accessories are still in JS, then no issue?

Perhaps some more documentation/guidance is needed for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

7 participants
You can’t perform that action at this time.