Skip to content

Commit

Permalink
Merge remote-tracking branch 'KhaosT/master' into random-setup-codes
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/CameraCore.ts
#	src/lib/Accessory.ts
#	src/lib/HAPServer.ts
#	src/lib/model/AccessoryInfo.ts
#	src/lib/util/eventedhttp.ts
  • Loading branch information
samuelthomas2774 committed Nov 2, 2020
2 parents f6c89db + 5cacbd4 commit 5329ff4
Show file tree
Hide file tree
Showing 70 changed files with 7,248 additions and 3,909 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/node-persist-ignore.js
@@ -0,0 +1,18 @@
/**
* This script tried to solve the problem of having types collisions of node-persist types.
*/

const path = require("path");
const fs = require("fs");

const storageDefinition = "./dist/lib/model/HAPStorage.d.ts";
const resolved = path.resolve(storageDefinition);

if (!fs.existsSync(resolved)) {
throw new Error("Tried to update definition but could not find HAPStorage.d.ts!");
}

const rows = fs.readFileSync(resolved, "utf8").split("\n");
rows.unshift("// @ts-ignore");

fs.writeFileSync(resolved, rows.join("\n"));
18 changes: 13 additions & 5 deletions .github/workflows/nodejs-beta.yml
Expand Up @@ -7,12 +7,20 @@ on:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: 10
node-version: ${{ matrix.node-version }}
- name: npm install, build and test
run: |
npm ci
Expand All @@ -36,7 +44,7 @@ jobs:
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: node .github/workflows/prerelease.js
- run: npm --no-git-tag-version version prerelease --preid=beta
- run: npm --no-git-tag-version version pre --preid=beta
- run: npm publish --tag=beta
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 13.x]
node-version: [10.x, 12.x, 14.x]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
Expand Down
31 changes: 20 additions & 11 deletions .github/workflows/prerelease.js
Expand Up @@ -4,23 +4,32 @@ const fs = require('fs');
const semver = require('semver');
const child_process = require('child_process');

const packageJSON = JSON.parse(fs.readFileSync('package.json', 'utf8'));

function getTagVersionFromNpm(tag) {
try {
return child_process.execSync(`npm info ${package.name} version --tag="${tag}"`).toString('utf8').trim();
return child_process.execSync(`npm info ${packageJSON.name} version --tag="${tag}"`).toString('utf8').trim();
} catch (e) {
return null;
throw e;
}
}

// load package.json
const package = JSON.parse(fs.readFileSync('package.json', 'utf8'));
// betaVersion is a custom property we put into the package.json to indicate which release we want to tag
const projectBetaVersion = packageJSON.betaVersion;
if (!projectBetaVersion) {
throw new Error("Unable to calculate the next prerelease version. 'betaVersion' was not set in the package.json")
}

const latestReleaseBeta = getTagVersionFromNpm("beta"); // like 0.7.0-beta.12
const betaAsRelease = semver.inc(latestReleaseBeta, "patch"); // will produce 0.7.0 (needed for the equality check below)

// work out the correct tag
const currentLatest = getTagVersionFromNpm('latest') || '0.0.0';
const currentBeta = getTagVersionFromNpm('beta') || '0.0.0';
const latestNpmTag = semver.gt(currentBeta, currentLatest, { includePrerelease: true }) ? currentBeta : currentLatest;
const publishTag = semver.gt(package.version, latestNpmTag, { includePrerelease: true }) ? package.version : latestNpmTag;
let publishTag;
if (semver.eq(projectBetaVersion, betaAsRelease)) { // check if we are releasing another version for the latest beta
publishTag = latestReleaseBeta; // set the current latest beta to be incremented
} else {
publishTag = projectBetaVersion; // start of with a new beta version
}

// save the package.json
package.version = publishTag;
fs.writeFileSync('package.json', JSON.stringify(package, null, 4));
packageJSON.version = publishTag;
fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 4));
2 changes: 1 addition & 1 deletion @types/bonjour-hap.d.ts
@@ -1,6 +1,6 @@
declare module 'bonjour-hap' {

export enum Protocols {
export const enum Protocols {
TCP = 'tcp',
UDP = 'udp',
}
Expand Down
15 changes: 0 additions & 15 deletions @types/fast-srp-hap.d.ts

This file was deleted.

54 changes: 54 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,54 @@
# Contributing

## Guidelines

<!-- Add that once ESLint is set up
* **Coding Standard:** Linting errors are checked by [ESLint][link-eslint].
Keeping a consistent style throughout the codebase keeps the cognitive load low for all
contributors and keeps the code style homogeneous.
-->

* **Node 10 LTS:** `HAP-NodeJS` has a minimum Node version requirement of 10.17.0.
Pull requests MUST NOT require a Node version greater than that unless the feature is
enabled/backported via [TypeScript][link-typescript].

* **Add tests:** All pull requests SHOULD include unit tests to ensure the change works as
expected and to prevent regressions.
Any pull request containing a bug fix SHOULD include a regression test for the given fix.

* **Document any change in behaviour:** Make sure any documentation is kept up-to-date
(JSDoc as well as possible documentation in the [Wiki][wiki]).

* **Consider our release cycle:** Before doing any pull request, please read through our concept for
[release cycles][release-cycle]. Especially the section regarding our [Git Workflow][git-workflow].

* **One pull request per feature:** If you want to do more than one thing, send multiple pull requests.
Otherwise, your pull request could be rejected.

* **Send coherent history:** Make sure each individual commit in your pull request is meaningful.
If you had to make multiple intermediate commits while developing,
please [rebase or squash them][link-git-rewrite] before submitting.

## Running tests

In order to contribute, you'll need to checkout the source from GitHub and
install dependencies using npm:

```bash
git clone https://github.com/homebridge/HAP-NodeJS.git
cd HAP-NodeJS
npm install
npm test
```

## Reporting a security vulnerability

See [SECURITY.md](SECURITY.md)

**Happy coding**!

[link-eslint]: https://eslint.org/
[wiki]: https://github.com/homebridge/HAP-NodeJS/wiki
[release-cycle]: https://github.com/homebridge/HAP-NodeJS/wiki/Release-Cycle
[git-workflow]: https://github.com/homebridge/HAP-NodeJS/wiki/Release-Cycle#git-workflow
[link-git-rewrite]: http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages
116 changes: 59 additions & 57 deletions README.md
@@ -1,80 +1,82 @@
<span align="center">

# HAP-NodeJS

<a href="https://www.npmjs.com/package/hap-nodejs"><img title="npm version" src="https://badgen.net/npm/v/hap-nodejs" ></a>
<a href="https://www.npmjs.com/package/hap-nodejs"><img title="npm downloads" src="https://badgen.net/npm/dt/hap-nodejs" ></a>
<a href="https://github.com/KhaosT/HAP-NodeJS/actions?query=workflow%3A%22Node-CI%22"><img title="npm downloads" src="https://github.com/homebridge/HAP-NodeJS/workflows/Node-CI/badge.svg" ></a>

</p>

<a href="https://www.npmjs.com/package/hap-nodejs"><img title="npm version" src="https://badgen.net/npm/v/hap-nodejs" ></a>
<a href="https://www.npmjs.com/package/hap-nodejs/v/beta"><img title="npm version beta" src="https://badgen.net/npm/v/hap-nodejs/beta" ></a>
<a href="https://www.npmjs.com/package/hap-nodejs"><img title="npm downloads" src="https://badgen.net/npm/dt/hap-nodejs" ></a>
<a href="https://github.com/KhaosT/HAP-NodeJS/actions?query=workflow%3A%22Node-CI%22"><img title="node ci" src="https://github.com/homebridge/HAP-NodeJS/workflows/Node-CI/badge.svg" ></a>

</span>

HAP-NodeJS is a Node.js implementation of the HomeKit Accessory Server.

With this project, you should be able to create your own HomeKit Accessory on a Raspberry Pi, Intel Edison, or any other platform that can run Node.js :)

The implementation may not 100% follow the HAP MFi Specification since the MFi program doesn't allow individual developers to join.

Remember to run `npm install` and `npm install --only=dev` before actually running the server.
HAP-NodeJS is an implementation of the HomeKit Accessory Server as specified in the HomeKit Accessory Protocol (HAP),
which is defined by Apple as part of the HomeKit Framework.

Users can define their own accessories in: accessories/[name]_accessory.ts files, where [name] is a short description of the accessory. All defined accessories get loaded on server start. You can define accessories using an object literal notation (see [Fan_accessory.ts](src/accessories/Fan_accessory.ts) for an example) or you can use the API (see below).
HAP-NodeJS is intended to be used as a library to easily create your own HomeKit Accessory on a Raspberry Pi,
Intel Edison, or any other platform that can run Node.js :)
If you are searching for a pluggable HomeKit bridge with over a thousand community driven plugins to bring HomeKit
support to devices which do not support HomeKit out of the box, you may want to look at the
[homebridge][project-homebridge] project (which also uses HAP-NodeJS internally).

You can use the following command to start the HAP Server in Bridged mode:
The implementation tries to follow the HAP specification as close as it can, but may differ in some cases.
HAP-NodeJS is not an Apple certified HAP implementation, as this is only available to members of the MFi program.

```sh
ts-node --files src/BridgedCore.ts
```
## Getting started

Or, if you wish to host each Accessory as an independent HomeKit device:
You may start by having a look at our [Wiki][wiki], especially have a look at the
[Important HomeKit Terminology][hk-terminology] used in this project.

```sh
ts-node --files src/Core.ts
```
There is also a pretty detailed guide on [how to start developing with HAP-NodeJS][dev-guide].
Or you may just have a look at our [examples][examples-repo] repository
(or some of the old [accessory examples][example-accessories]).

The HAP-NodeJS library uses the [debug](https://github.com/visionmedia/debug) library for log output. You can print some or all of the logs by setting the `DEBUG` environment variable. For instance, to see all debug logs while running the server:
See the FAQ on how to enable [debug output][faq-debug] for HAP-NodeJS.

```sh
DEBUG=* ts-node --files src/BridgedCore.ts
```
If you wish to do a contribution please read through our [CONTRIBUTING][contributing] guide.

HOMEKIT PROTOCOL
================
## Projects based on HAP-NodeJS

Hint: the Homekit Application Protocol (HAP) allows that you can pair a Homekit device with one device. As soon as the Homekit device is paired, its not possible to pair with another iOS device anymore.
- [Homebridge][project-homebridge] - HomeKit support for the impatient - Pluggable HomeKit Bridge.
Plugins available for e.g. Pilight, Telldus TDtool, Savant, Netatmo, Open Pixel Control, HomeWizard, Fritz!Box,
LG WebOS TV, Home Assistant, HomeMatic and many more.
- [OpenHAB-HomeKit-Bridge][project-openhab-homekit-bridge] - OpenHAB HomeKit Bridge bridges openHAB items to
Apples HomeKit Accessory Protocol.
- [homekit2mqtt][project-homekit2mqtt] - HomeKit to MQTT bridge.
- [pimatic-hap][project-pimatic-hap] - Pimatic homekit bridge.
- [node-red-contrib-homekit][project-node-red-contrib-homekit] - Node-RED nodes to simulate Apple HomeKit devices.
- [ioBroker.homekit][project-ioBroker-homekit] - connect ioBroker to HomeKit.
- [AccessoryServer][project-accessoryserver] - HomeKit integration for IR/RF/IP-devices

API
===
## Notes

HAP-NodeJS provides a set of classes you can use to construct Accessories programatically. For an example implementation, see [Lock_accessory.ts](src/accessories/Lock_accessory.ts).
Special thanks to [Alex Skalozub][link-alex-skalozub], who reverse-engineeredthe server side HAP.
~~You can find his research [here][link-homekit-research].~~
(Sadly, on Nov 4, Apple sent the [DMCA][link-apple-dmca] request to Github to remove the research.)

The key classes intended for use by API consumers are:

* [Accessory](src/lib/Accessory.ts): Represents a HomeKit device that can be published on your local network.
* [Bridge](src/lib/Bridge.ts): A kind of Accessory that can host other Accessories "behind" it while only publishing a single device.
* [Service](src/lib/Service.ts): Represents a set of grouped values necessary to provide a logical function. Most of the time, when you think of a supported HomeKit device like "Thermostat" or "Door Lock", you're actualy thinking of a Service. Accessories can expose multiple services.
* [Characteristic](src/lib/Characteristic.ts): Represents a particular typed variable assigned to a Service, for instance the `LockMechanism` Service contains a `CurrentDoorState` Characteristic describing whether the door is currently locked.

All known built-in Service and Characteristic types that HomeKit supports are exposed as a separate subclass in [HomeKitTypes](src/lib/gen/HomeKit.ts).

See each of the corresponding class files for more explanation and notes.
[There](http://instagram.com/p/t4cPlcDksQ/) is a video demo running this project on Intel Edison.

Notes
=====
If you are interested in HAP over BTLE, you might want to check [this][link-hap-over-btle].

Special thanks to [Alex Skalozub](https://twitter.com/pieceofsummer), who reverse engineered the server side HAP. ~~You can find his research at [here](https://gist.github.com/pieceofsummer/13272bf76ac1d6b58a30).~~ (Sadly, on Nov 4, Apple sent the [DMCA](https://github.com/github/dmca/blob/master/2014/2014-11-04-Apple.md) request to Github to remove the research.)
<!-- links -->

[There](http://instagram.com/p/t4cPlcDksQ/) is a video demo running this project on Intel Edison.
[wiki]: https://github.com/homebridge/HAP-NodeJS/wiki
[hk-terminology]: https://github.com/homebridge/HAP-NodeJS/wiki/HomeKit-Terminology
[dev-guide]: https://github.com/homebridge/HAP-NodeJS/wiki/Using-HAP-NodeJS-as-a-library
[faq-debug]: https://github.com/homebridge/HAP-NodeJS/wiki/FAQ#debug-mode
[contributing]: https://github.com/homebridge/HAP-NodeJS/blob/master/CONTRIBUTING.md

If you are interested in HAP over BTLE, you might want to check [this](https://gist.github.com/KhaosT/6ff09ba71d306d4c1079).
[examples-repo]: https://github.com/homebridge/HAP-NodeJS-examples
[example-accessories]: https://github.com/homebridge/HAP-NodeJS/tree/master/src/accessories

Projects based on HAP-NodeJS
============================
[project-homebridge]: https://github.com/homebridge/homebridge
[project-openhab-homekit-bridge]: https://github.com/htreu/OpenHAB-HomeKit-Bridge
[project-homekit2mqtt]: https://github.com/hobbyquaker/homekit2mqtt
[project-pimatic-hap]: https://github.com/michbeck100/pimatic-hap
[project-node-red-contrib-homekit]: https://github.com/NRCHKB/node-red-contrib-homekit-bridged
[project-ioBroker-homekit]: https://github.com/ioBroker/ioBroker.homekit2
[project-accessoryserver]: https://github.com/Appyx/AccessoryServer

* [Homebridge](https://github.com/nfarina/homebridge) - HomeKit support for the impatient - Pluggable HomeKit Bridge. Plugins available for e.g. Pilight, Telldus TDtool, Savant, Netatmo, Open Pixel Control, HomeWizard, Fritz!Box, LG WebOS TV, Home Assistant, HomeMatic and many many more.
* [OpenHAB-HomeKit-Bridge](https://github.com/htreu/OpenHAB-HomeKit-Bridge) - OpenHAB HomeKit Bridge bridges openHAB items to Apple´s HomeKit Accessory Protocol.
* [homekit2mqtt](https://github.com/hobbyquaker/homekit2mqtt) - HomeKit to MQTT bridge.
* [pimatic-hap](https://github.com/michbeck100/pimatic-hap) - Pimatic homekit bridge.
* [node-red-contrib-homekit](https://github.com/NRCHKB/node-red-contrib-homekit-bridged) - Node-RED nodes to simulate Apple HomeKit devices.
* [ioBroker.homekit](https://github.com/ioBroker/ioBroker.homekit2) - connect ioBroker to HomeKit.
* [AccessoryServer](https://github.com/Appyx/AccessoryServer) - HomeKit integration for IR/RF/IP-devices
[link-alex-skalozub]: https://twitter.com/pieceofsummer
[link-homekit-research]: https://gist.github.com/pieceofsummer/13272bf76ac1d6b58a30
[link-apple-dmca]: https://github.com/github/dmca/blob/master/2014/2014-11-04-Apple.md
[link-hap-over-btle]: https://gist.github.com/KhaosT/6ff09ba71d306d4c1079
16 changes: 16 additions & 0 deletions SECURITY.md
@@ -0,0 +1,16 @@
# Security Policy

## Reporting a Vulnerability

We want to ensure that `HAP-NodeJS` is secure for everyone. If you've discovered a security vulnerability,
we appreciate your help in disclosing it to us in a [responsible manner][link-responsible-disclosure].

Publicly disclosing a vulnerability can put the entire community at risk. If you've discovered a security concern,
please email us at [mail@anderl-bauer.de](mailto:mail@anderl-bauer.de) with [SECURITY] in the subject line.
We'll work with you to make sure we understand the scope of the issue, and that we fully address your concern.
We consider correspondence sent to this email address our highest priority,
and work to address any issues that arise as quickly as possible.

After a security vulnerability has been corrected, a security hotfix release will be deployed as soon as possible.

[link-responsible-disclosure]: http://en.wikipedia.org/wiki/Responsible_disclosure
2 changes: 2 additions & 0 deletions __mocks__/node-persist.ts
Expand Up @@ -3,6 +3,8 @@ class Storage {
setItemSync = jest.fn();
persistSync = jest.fn();
removeItemSync = jest.fn();
initSync = jest.fn();
create = jest.fn().mockImplementation(() => new Storage());
}

export default new Storage();

0 comments on commit 5329ff4

Please sign in to comment.