Skip to content

Commit

Permalink
Move auth to createSocket (#30)
Browse files Browse the repository at this point in the history
Move auth to connection options
  • Loading branch information
balloob authored Aug 9, 2018
1 parent fbf8230 commit 468918c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function connect() {
return;
}
}
const connection = await createConnection(auth);
const connection = await createConnection({ auth });
subscribeEntities(connection, ent => console.log(ent));
}

Expand Down
2 changes: 1 addition & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
return;
}
}
const connection = await createConnection(auth);
const connection = await createConnection({ auth });
subscribeEntities(connection, entities => renderEntities(connection, entities));
})();

Expand Down
13 changes: 5 additions & 8 deletions lib/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
HassServices,
HassConfig
} from "./types";
import { Auth } from "./auth";
import createSocket from "./socket";

const DEBUG = false;
Expand Down Expand Up @@ -53,7 +52,6 @@ type WebSocketResponse =
| WebSocketResultErrorResponse;

export class Connection {
auth: Auth;
options: ConnectionOptions;
commandId: number;
commands: {
Expand All @@ -65,10 +63,10 @@ export class Connection {
closeRequested: boolean;
socket: WebSocket;

constructor(auth: Auth, options: ConnectionOptions) {
this.auth = auth;
constructor(options: ConnectionOptions) {
// connection options
// - setupRetry: amount of ms to retry when unable to connect on initial setup
// - createSocket: create a new Socket connection
this.options = options;
// id if next command to send
this.commandId = 1;
Expand Down Expand Up @@ -279,7 +277,7 @@ export class Connection {
console.log("Trying to reconnect");
}
try {
const socket = await options.createSocket(this.auth, options);
const socket = await options.createSocket(options);
this.setSocket(socket);
} catch (err) {
if (err === ERR_INVALID_AUTH) {
Expand All @@ -306,16 +304,15 @@ const defaultConnectionOptions: ConnectionOptions = {
};

export default async function createConnection(
auth: Auth,
options?: Partial<ConnectionOptions>
) {
const connOptions: ConnectionOptions = Object.assign(
{},
defaultConnectionOptions,
options
);
const socket = await options.createSocket(auth, connOptions);
const conn = new Connection(auth, connOptions);
const socket = await connOptions.createSocket(connOptions);
const conn = new Connection(connOptions);
conn.setSocket(socket);
return conn;
}
9 changes: 4 additions & 5 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
} from "./const";
import { MSG_TYPE_AUTH_INVALID } from "./const";
import { ConnectionOptions } from "./types";
import { authAccessToken } from "./messages";

const DEBUG = false;

export default function createSocket(
auth,
options: ConnectionOptions
): Promise<WebSocket> {
const auth = options.auth;

// Convert from http:// -> ws://, https:// -> wss://
const url = `ws${auth.hassUrl.substr(4)}/api/websocket`;

Expand All @@ -36,7 +38,6 @@ export default function createSocket(
const closeMessage = () => {
// If we are in error handler make sure close handler doesn't also fire.
socket.removeEventListener("close", closeMessage);

if (invalidAuth) {
promReject(ERR_INVALID_AUTH);
return;
Expand Down Expand Up @@ -72,9 +73,7 @@ export default function createSocket(
case MSG_TYPE_AUTH_REQUIRED:
try {
if (auth.expired) await auth.refreshAccessToken();
socket.send(
JSON.stringify(message.authAccessToken(auth.access_token))
);
socket.send(JSON.stringify(authAccessToken(auth.access_token)));
} catch (err) {
// Refresh token failed
invalidAuth = true;
Expand Down
3 changes: 2 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export type UnsubscribeFunc = () => void;

export type ConnectionOptions = {
setupRetry: number;
createSocket: (auth: Auth, options: ConnectionOptions) => Promise<WebSocket>;
auth?: Auth;
createSocket: (options: ConnectionOptions) => Promise<WebSocket>;
};

export type HassEvent = {
Expand Down

0 comments on commit 468918c

Please sign in to comment.