homebridge-alexa 0.7.0 → 0.7.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ #!/bin/env node
2
+
3
+ /**
4
+ * This scripts queries the npm registry to pull out the latest version for a given tag.
5
+ */
6
+
7
+ const fs = require("fs");
8
+ const semver = require("semver");
9
+ const child_process = require("child_process");
10
+ const assert = require("assert");
11
+
12
+ const BRANCH_VERSION_PATTERN = /^([A-Za-z]*)-(\d+.\d+.\d+)$/
13
+
14
+ // Load the contents of the package.json file
15
+ const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
16
+
17
+ let refArgument = process.argv[2];
18
+ let tagArgument = process.argv[3] || "latest";
19
+
20
+ if (refArgument == null) {
21
+ console.error("ref argument is missing");
22
+ console.error("Usage: npm-version-script.js <ref> [tag]");
23
+ process.exit(1);
24
+ }
25
+
26
+ /**
27
+ * Queries the NPM registry for the latest version for the provided tag.
28
+ * @param tag The tag to query for.
29
+ * @returns {string} Returns the version.
30
+ */
31
+ function getTagVersionFromNpm(tag) {
32
+ try {
33
+ return child_process.execSync(`npm info ${packageJSON.name} version --tag="${tag}"`).toString("utf8").trim();
34
+ } catch (e) {
35
+ console.error(`Failed to query the npm registry for the latest version for tag: ${tag}`);
36
+ // throw e;
37
+ return "0.0.0";
38
+ }
39
+ }
40
+
41
+ function desiredTargetVersion(ref) {
42
+ // ref is a GitHub action ref string
43
+ if (ref.startsWith("refs/pull/")) {
44
+ throw Error("The version script was executed inside a PR!");
45
+ }
46
+
47
+ assert(ref.startsWith("refs/heads/"))
48
+ let branchName = ref.slice("refs/heads/".length);
49
+
50
+ let results = branchName.match(BRANCH_VERSION_PATTERN);
51
+ if (results != null) {
52
+ if (results[1] !== tagArgument) {
53
+ console.warn(`The base branch name (${results[1]}) differs from the tag name ${tagArgument}`);
54
+ }
55
+
56
+ return results[2];
57
+ }
58
+
59
+ // legacy mode were we use the `betaVersion` property in the package.json
60
+ if (branchName === "beta" && packageJSON.betaVersion) {
61
+ return packageJSON.betaVersion
62
+ }
63
+
64
+ // legacy mode were we use the `alphaVersion` property in the package.json
65
+ if (branchName === "alpha" && packageJSON.alphaVersion) {
66
+ return packageJSON.alphaVersion
67
+ }
68
+
69
+ throw new Error(`Malformed branch name for ref: ${ref}. Can't derive the base version. Use a branch name like: beta-x.x.x or alpha-x.x.x`);
70
+ }
71
+
72
+ // derive the base version from the branch ref
73
+ const baseVersion = desiredTargetVersion(refArgument);
74
+
75
+ // query the npm registry for the latest version of the provided tag name
76
+ const latestReleasedVersion = getTagVersionFromNpm(tagArgument); // e.g. 0.7.0-beta.12
77
+ const latestReleaseBase = semver.inc(latestReleasedVersion, "patch"); // will produce 0.7.0 (removing the preid, needed for the equality check below)
78
+
79
+ let publishTag;
80
+ if (semver.eq(baseVersion, latestReleaseBase)) { // check if we are releasing another version for the latest beta or alpha
81
+ publishTag = latestReleasedVersion; // set the current latest beta or alpha to be incremented
82
+ } else {
83
+ publishTag = baseVersion; // start of with a new beta or alpha version
84
+ }
85
+
86
+ // save the package.json
87
+ packageJSON.version = publishTag;
88
+ fs.writeFileSync("package.json", JSON.stringify(packageJSON, null, 2));
89
+
90
+ // perform the same change to the package-lock.json
91
+ const packageLockJSON = JSON.parse(fs.readFileSync("package-lock.json", "utf8"));
92
+ packageLockJSON.version = publishTag;
93
+ fs.writeFileSync("package-lock.json", JSON.stringify(packageLockJSON, null, 2));
@@ -80,6 +80,8 @@ jobs:
80
80
  branch: ${{ github.ref }}
81
81
 
82
82
  publish_prod_release:
83
+ permissions:
84
+ id-token: write
83
85
  needs: [get_tags, create_documentation]
84
86
  name: Publish Release Version
85
87
  if: ${{ needs.get_tags.outputs.BRANCH_NAME == 'main' }}
@@ -90,6 +92,8 @@ jobs:
90
92
  npm_auth_token: ${{ secrets.npm_token }}
91
93
 
92
94
  publish_test_release:
95
+ permissions:
96
+ id-token: write
93
97
  needs: [get_tags, create_documentation]
94
98
  name: Publish Test Version - ${{ needs.get_tags.outputs.BRANCH_NAME }}
95
99
  if: ${{ needs.get_tags.outputs.BRANCH_NAME != 'main' }}
package/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. This project uses [Semantic Versioning](https://semver.org/).
4
4
 
5
+ ## 0.7.1 (2023-05-30)
6
+
7
+ ## [Version 0.7.1](https://github.com/northernman54/homebridge-alexa/compare/v0.7.0...v0.7.1)
8
+
9
+ #### Changes
10
+
11
+ - Update `filter` to accept a list of addresses and ports ie "192.168.1.11:51551, 192.168.1.11:46047"
12
+
5
13
  ## 0.7.0 (2023-05-30)
6
14
 
7
15
  ## [Version 0.7.0](https://github.com/northernman54/homebridge-alexa/compare/v0.6.9...v0.7.0)
@@ -143,7 +143,7 @@
143
143
  "title": "<b>Homebridge Instance Filter</b>",
144
144
  "type": "string",
145
145
  "placeholder": "eg. 192.168.1.122:51826",
146
- "description": "Limits accessories shared with Alexa to a single homebridge instance. Uses ip address and port of homebridge instance.",
146
+ "description": "Limits accessories shared with Alexa to particular homebridge instances. Uses ip address and port of homebridge instance, eg. '192.168.1.122:51826' or '192.168.1.11:51551, 192.168.1.11:46047'.",
147
147
  "pattern": "^[^{}/ :\\\\]+(?::\\d+)?$"
148
148
  },
149
149
  "deviceListHandling": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "moduleFileExtensions": ["js", "json", "ts"],
3
- "rootDir": "../",
3
+ "rootDir": "./",
4
4
  "testEnvironment": "node",
5
5
  "testRegex": ".test.ts$",
6
6
  "testTimeout": 10000,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-alexa",
3
- "version": "0.7.0",
3
+ "version": "0.7.1-beta.1",
4
4
  "description": "Control your Homebridge devices with Amazon Alexa.",
5
5
  "main": "plugin.js",
6
6
  "scripts": {
@@ -34,25 +34,25 @@
34
34
  "devDependencies": {
35
35
  "@oznu/hap-client": "^1.9.0",
36
36
  "@types/jest": "^29.5.10",
37
- "@types/node": "^20.14.10",
37
+ "@types/node": "^22.2.0",
38
38
  "babel-jest": "^29.7.0",
39
39
  "documentation": "^14.0.3",
40
40
  "jest": "^29.7.0",
41
41
  "mqtt-connection": "^4.1.0",
42
42
  "nodemon": "^3.1.4",
43
43
  "rimraf": "^6.0.1",
44
- "semver": "^7.5.4",
45
- "ts-jest": "^29.2.2",
44
+ "semver": "^7.6.3",
45
+ "ts-jest": "^29.2.4",
46
46
  "ts-node": "^10.9.1",
47
- "typescript": "^5.5.3",
47
+ "typescript": "^5.5.4",
48
48
  "ws": "^8.14.2"
49
49
  },
50
50
  "dependencies": {
51
51
  "bottleneck": "2.19.5",
52
- "debug": "4.3.5",
53
- "hap-node-client": "0.2.7",
52
+ "debug": "4.3.6",
53
+ "hap-node-client": "0.2.8-beta.12",
54
54
  "is-my-json-valid": "2.20.6",
55
- "mqtt": "5.8.0"
55
+ "mqtt": "5.9.1"
56
56
  },
57
57
  "author": "NorthernMan54",
58
58
  "license": "Apache-2.0",
@@ -1,55 +1,77 @@
1
+ import { Server } from 'net';
2
+ import { MqttServer, MqttSecureServer, MqttServerListener } from './server'
1
3
  import serverBuilder from './server_helpers_for_client_tests';
2
4
  var AlexaLocal = require('../lib/alexaLocal.js').alexaLocal;
3
5
 
4
6
  jest.setTimeout(30000);
5
7
 
8
+ // const ports = getPorts(2)
9
+ var server: MqttServer;
10
+ var alexa: any;
6
11
 
7
12
  describe('MQTT Client', () => {
8
13
  beforeAll(async () => {
9
-
10
- var version;
11
- let client = null
12
- let publishCount = 0
13
- const server2 = serverBuilder('mqtt', (serverClient) => {
14
+ var version: number;
15
+ server = serverBuilder('mqtt', (serverClient) => {
14
16
  serverClient.on('connect', () => {
15
- console.log('connect');
16
17
  const connack =
17
18
  version === 5 ? { reasonCode: 0 } : { returnCode: 0 }
18
19
  serverClient.connack(connack)
19
20
  })
20
- serverClient.on('publish', (packet) => {
21
+ serverClient.on('publish', (packet: { payload: { toString: () => any; }; qos: number; messageId: any; }) => {
21
22
  console.log('publish', packet.payload.toString());
22
23
  if (packet.qos !== 0) {
23
24
  serverClient.puback({ messageId: packet.messageId })
24
25
  }
25
26
  })
27
+ serverClient.on('auth', (packet: any) => {
28
+ console.log('auth');
29
+ if (serverClient.writable) return false
30
+ const rc = 'reasonCode'
31
+ const connack = {}
32
+ connack[rc] = 0
33
+ serverClient.connack(connack)
34
+ })
35
+ serverClient.on('subscribe', (packet: { subscriptions: any[]; messageId: any; }) => {
36
+ console.log('subscribe', packet.subscriptions);
37
+ if (!serverClient.writable) return false
38
+ serverClient.suback({
39
+ messageId: packet.messageId,
40
+ granted: packet.subscriptions.map((e: { qos: any; }) => e.qos),
41
+ })
42
+ })
26
43
  })
44
+ console.log('MQTT Server starting listener')
45
+ await server.listen(1883)
46
+ console.log('MQTT Server listening')
47
+ var options = {
48
+ // MQTT Options
49
+ log: console.log,
50
+ mqttURL: 'mqtt://localhost:1883',
51
+ transport: 'mqtt',
52
+ mqttOptions: {
53
+ username: 'TEST',
54
+ password: 'PASSWORD',
55
+ reconnectPeriod: 33, // Increased reconnect period to allow DDOS protection to reset
56
+ keepalive: 55,
57
+ rejectUnauthorized: false
58
+ },
59
+ };
60
+ alexa = new AlexaLocal(options);
27
61
  }, 300000);
28
62
 
29
63
  afterAll(async () => {
30
-
31
- // await sleep(10000)
32
-
64
+ console.log('MQTT Server exiting', server.listening)
65
+ if (server.listening) {
66
+ await server.close()
67
+ }
33
68
  });
34
69
 
35
70
  describe('Validate Inital Startup', () => {
36
- test('getconf GNU_LIBC_VERSION', async () => {
37
- var options = {
38
- // MQTT Options
39
- log: console.log,
40
- mqttURL: 'mqtt://localhost:1883',
41
- transport: 'mqtt',
42
- mqttOptions: {
43
- username: 'TEST',
44
- password: 'PASSWORD',
45
- reconnectPeriod: 33, // Increased reconnect period to allow DDOS protection to reset
46
- keepalive: 55,
47
- rejectUnauthorized: false
48
- },
49
-
50
- };
51
- var alexa = new AlexaLocal(options);
52
- await sleep(10000)
71
+ test('Discover Devices', async () => {
72
+ server.publish('command/TEST/1', {"directive":{"header":{"namespace":"Alexa.Discovery","name":"Discover","payloadVersion":"3","messageId":"e68a6720-1222-4030-b044-f01360935f18"},"payload":{}}});
73
+ expect(server).toReceiveMessage('junk');
74
+ // await sleep(10000)
53
75
  });
54
76
 
55
77
 
@@ -58,6 +80,6 @@ describe('MQTT Client', () => {
58
80
 
59
81
  });
60
82
 
61
- async function sleep(ms) {
83
+ async function sleep(ms: number) {
62
84
  return new Promise(resolve => setTimeout(resolve, ms));
63
85
  }
@@ -0,0 +1,166 @@
1
+ import { diff } from "jest-diff";
2
+ import WS from "./websocket";
3
+ import { DeserializedMessage } from "./websocket";
4
+
5
+ type ReceiveMessageOptions = {
6
+ timeout?: number;
7
+ };
8
+
9
+ declare global {
10
+ namespace jest {
11
+ interface Matchers<R, T> {
12
+ toReceiveMessage<TMessage = object>(
13
+ message: DeserializedMessage<TMessage>,
14
+ options?: ReceiveMessageOptions,
15
+ ): Promise<R>;
16
+ toHaveReceivedMessages<TMessage = object>(
17
+ messages: Array<DeserializedMessage<TMessage>>,
18
+ ): R;
19
+ }
20
+ }
21
+ }
22
+
23
+ const WAIT_DELAY = 1000;
24
+ const TIMEOUT = Symbol("timoeut");
25
+
26
+ const makeInvalidWsMessage = function makeInvalidWsMessage(
27
+ this: jest.MatcherUtils,
28
+ ws: WS,
29
+ matcher: string,
30
+ ) {
31
+ return (
32
+ this.utils.matcherHint(
33
+ this.isNot ? `.not.${matcher}` : `.${matcher}`,
34
+ "WS",
35
+ "expected",
36
+ ) +
37
+ "\n\n" +
38
+ `Expected the websocket object to be a valid WS mock.\n` +
39
+ `Received: ${typeof ws}\n` +
40
+ ` ${this.utils.printReceived(ws)}`
41
+ );
42
+ };
43
+
44
+ expect.extend({
45
+ async toReceiveMessage(
46
+ ws: WS,
47
+ expected: DeserializedMessage,
48
+ options?: ReceiveMessageOptions,
49
+ ) {
50
+ const isWS = ws instanceof WS;
51
+ if (!isWS) {
52
+ return {
53
+ pass: !!this.isNot, // always fail
54
+ message: makeInvalidWsMessage.bind(this, ws, "toReceiveMessage"),
55
+ };
56
+ }
57
+
58
+ const waitDelay = options?.timeout ?? WAIT_DELAY;
59
+
60
+ let timeoutId;
61
+ const messageOrTimeout = await Promise.race([
62
+ ws.nextMessage,
63
+ new Promise((resolve) => {
64
+ timeoutId = setTimeout(() => resolve(TIMEOUT), waitDelay);
65
+ }),
66
+ ]);
67
+ clearTimeout(timeoutId);
68
+
69
+ if (messageOrTimeout === TIMEOUT) {
70
+ return {
71
+ pass: !!this.isNot, // always fail
72
+ message: () =>
73
+ this.utils.matcherHint(
74
+ this.isNot ? ".not.toReceiveMessage" : ".toReceiveMessage",
75
+ "WS",
76
+ "expected",
77
+ ) +
78
+ "\n\n" +
79
+ `Expected the websocket server to receive a message,\n` +
80
+ `but it didn't receive anything in ${waitDelay}ms.`,
81
+ };
82
+ }
83
+ const received = messageOrTimeout;
84
+
85
+ const pass = this.equals(received, expected);
86
+
87
+ const message = pass
88
+ ? () =>
89
+ this.utils.matcherHint(".not.toReceiveMessage", "WS", "expected") +
90
+ "\n\n" +
91
+ `Expected the next received message to not equal:\n` +
92
+ ` ${this.utils.printExpected(expected)}\n` +
93
+ `Received:\n` +
94
+ ` ${this.utils.printReceived(received)}`
95
+ : () => {
96
+ const diffString = diff(expected, received, { expand: this.expand });
97
+ return (
98
+ this.utils.matcherHint(".toReceiveMessage", "WS", "expected") +
99
+ "\n\n" +
100
+ `Expected the next received message to equal:\n` +
101
+ ` ${this.utils.printExpected(expected)}\n` +
102
+ `Received:\n` +
103
+ ` ${this.utils.printReceived(received)}\n\n` +
104
+ `Difference:\n\n${diffString}`
105
+ );
106
+ };
107
+
108
+ return {
109
+ actual: received,
110
+ expected,
111
+ message,
112
+ name: "toReceiveMessage",
113
+ pass,
114
+ };
115
+ },
116
+
117
+ toHaveReceivedMessages(ws: WS, messages: Array<DeserializedMessage>) {
118
+ const isWS = ws instanceof WS;
119
+ if (!isWS) {
120
+ return {
121
+ pass: !!this.isNot, // always fail
122
+ message: makeInvalidWsMessage.bind(this, ws, "toHaveReceivedMessages"),
123
+ };
124
+ }
125
+
126
+ const received = messages.map((expected) =>
127
+ // object comparison to handle JSON protocols
128
+ ws.messages.some((actual) => this.equals(actual, expected)),
129
+ );
130
+ const pass = this.isNot ? received.some(Boolean) : received.every(Boolean);
131
+ const message = pass
132
+ ? () =>
133
+ this.utils.matcherHint(
134
+ ".not.toHaveReceivedMessages",
135
+ "WS",
136
+ "expected",
137
+ ) +
138
+ "\n\n" +
139
+ `Expected the WS server to not have received the following messages:\n` +
140
+ ` ${this.utils.printExpected(messages)}\n` +
141
+ `But it received:\n` +
142
+ ` ${this.utils.printReceived(ws.messages)}`
143
+ : () => {
144
+ return (
145
+ this.utils.matcherHint(
146
+ ".toHaveReceivedMessages",
147
+ "WS",
148
+ "expected",
149
+ ) +
150
+ "\n\n" +
151
+ `Expected the WS server to have received the following messages:\n` +
152
+ ` ${this.utils.printExpected(messages)}\n` +
153
+ `Received:\n` +
154
+ ` ${this.utils.printReceived(ws.messages)}\n\n`
155
+ );
156
+ };
157
+
158
+ return {
159
+ actual: ws.messages,
160
+ expected: messages,
161
+ message,
162
+ name: "toHaveReceivedMessages",
163
+ pass,
164
+ };
165
+ },
166
+ });
package/test/server.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as net from 'net'
2
2
  import { TlsOptions } from 'tls'
3
3
  import * as tls from 'tls'
4
- import Connection from 'mqtt-connection'
4
+ import * as Connection from 'mqtt-connection'
5
5
  import { Duplex } from 'stream'
6
6
 
7
7
  export type MqttServerListener = (client: Connection) => void
@@ -29,7 +29,7 @@ export class MqttServer extends net.Server {
29
29
  this.on('client', listener)
30
30
  }
31
31
  }
32
- }
32
+ }
33
33
 
34
34
  /**
35
35
  * MqttServerNoWait (w/o waiting for initialization)
@@ -25,15 +25,21 @@ export default function serverBuilder(
25
25
  handler?: MqttServerListener,
26
26
  ): Server {
27
27
  const sockets = []
28
+
29
+
28
30
  const defaultHandler: MqttServerListener = (serverClient) => {
31
+
29
32
  sockets.push(serverClient)
33
+
30
34
  serverClient.on('auth', (packet) => {
35
+ console.log('auth');
31
36
  if (serverClient.writable) return false
32
37
  const rc = 'reasonCode'
33
38
  const connack = {}
34
39
  connack[rc] = 0
35
40
  serverClient.connack(connack)
36
41
  })
42
+
37
43
  serverClient.on('connect', (packet) => {
38
44
  if (!serverClient.writable) return false
39
45
  let rc = 'returnCode'
@@ -63,6 +69,7 @@ export default function serverBuilder(
63
69
  })
64
70
 
65
71
  serverClient.on('publish', (packet) => {
72
+ console.log('publish');
66
73
  if (!serverClient.writable) return false
67
74
  setImmediate(() => {
68
75
  switch (packet.qos) {