Enable ts strict mode for app

This commit is contained in:
hensm
2019-05-24 17:02:59 +01:00
parent db647e2295
commit 9c381c763a
7 changed files with 63 additions and 46 deletions

View File

@@ -46,7 +46,7 @@ export default class Media {
this.sendMessageCallback = sendMessageCallback; this.sendMessageCallback = sendMessageCallback;
this.session.createChannel(MEDIA_NAMESPACE); this.session.createChannel(MEDIA_NAMESPACE);
this.channel = this.session.channelMap.get(MEDIA_NAMESPACE); this.channel = this.session.channelMap.get(MEDIA_NAMESPACE)!;
this.channel.on("message", (data: any) => { this.channel.on("message", (data: any) => {
if (data && data.type === "MEDIA_STATUS" if (data && data.type === "MEDIA_STATUS"

View File

@@ -20,16 +20,16 @@ export default class Session {
private referenceId: string; private referenceId: string;
private client: Client; private client: Client;
private clientConnection: Channel; private clientConnection?: Channel;
private clientHeartbeat: Channel; private clientHeartbeat?: Channel;
private clientReceiver: Channel; private clientReceiver?: Channel;
private clientHeartbeatIntervalId: NodeJS.Timer; private clientHeartbeatIntervalId?: NodeJS.Timer;
private isSessionCreated = false; private isSessionCreated = false;
private clientId: string; private clientId?: string;
private transportId: string; private transportId?: string;
private transportConnection: Channel; private transportConnection?: Channel;
private app: any; private app: any;
constructor ( constructor (
@@ -67,7 +67,7 @@ export default class Session {
transportHeartbeat.send({ type: "PING" }); transportHeartbeat.send({ type: "PING" });
} }
this.clientHeartbeat.send({ type: "PING" }); this.clientHeartbeat!.send({ type: "PING" });
}, 5000); }, 5000);
this.clientReceiver.send({ this.clientReceiver.send({
@@ -94,7 +94,7 @@ export default class Session {
// Close session // Close session
this.sendMessage("shim:/session/stopped"); this.sendMessage("shim:/session/stopped");
this.client.close(); this.client.close();
clearInterval(this.clientHeartbeatIntervalId); clearInterval(this.clientHeartbeatIntervalId!);
return; return;
} }
@@ -106,10 +106,10 @@ export default class Session {
`client-${Math.floor(Math.random() * 10e5)}`; `client-${Math.floor(Math.random() * 10e5)}`;
this.transportConnection = this.client.createChannel( this.transportConnection = this.client.createChannel(
this.clientId, this.transportId this.clientId, this.transportId!
, NS_CONNECTION, "JSON"); , NS_CONNECTION, "JSON");
transportHeartbeat = this.client.createChannel( transportHeartbeat = this.client.createChannel(
this.clientId, this.transportId this.clientId, this.transportId!
, NS_HEARTBEAT, "JSON"); , NS_HEARTBEAT, "JSON");
this.transportConnection.send({ type: "CONNECT" }); this.transportConnection.send({ type: "CONNECT" });
@@ -167,12 +167,12 @@ export default class Session {
if (!this.channelMap.has(namespace)) { if (!this.channelMap.has(namespace)) {
this.channelMap.set(namespace this.channelMap.set(namespace
, this.client.createChannel( , this.client.createChannel(
this.clientId, this.transportId, namespace, "JSON")); this.clientId!, this.transportId!, namespace, "JSON"));
} }
} }
public close () { public close () {
this.clientConnection.send({ type: "CLOSE" }); this.clientConnection!.send({ type: "CLOSE" });
if (this.transportConnection) { if (this.transportConnection) {
this.transportConnection.send({ type: "CLOSE" }); this.transportConnection.send({ type: "CLOSE" });
} }
@@ -188,7 +188,7 @@ export default class Session {
private _impl_addMessageListener (namespace: string) { private _impl_addMessageListener (namespace: string) {
this.createChannel(namespace); this.createChannel(namespace);
this.channelMap.get(namespace).on("message", (data: any) => { this.channelMap.get(namespace)!.on("message", (data: any) => {
this.sendMessage("shim:/session/impl_addMessageListener", { this.sendMessage("shim:/session/impl_addMessageListener", {
namespace namespace
, data: JSON.stringify(data) , data: JSON.stringify(data)
@@ -205,7 +205,7 @@ export default class Session {
try { try {
this.createChannel(namespace); this.createChannel(namespace);
this.channelMap.get(namespace).send(message); this.channelMap.get(namespace)!.send(message);
} catch (err) { } catch (err) {
error = true; error = true;
} }
@@ -221,7 +221,7 @@ export default class Session {
let error = false; let error = false;
try { try {
this.clientReceiver.send({ this.clientReceiver!.send({
type: "SET_VOLUME" type: "SET_VOLUME"
, volume: { muted } , volume: { muted }
, requestId: 0 , requestId: 0
@@ -241,7 +241,7 @@ export default class Session {
let error = false; let error = false;
try { try {
this.clientReceiver.send({ this.clientReceiver!.send({
type: "SET_VOLUME" type: "SET_VOLUME"
, volume: { level: newLevel } , volume: { level: newLevel }
, requestId: 0 , requestId: 0
@@ -260,7 +260,7 @@ export default class Session {
let error = false; let error = false;
try { try {
this.clientReceiver.send({ this.clientReceiver!.send({
type: "STOP" type: "STOP"
, sessionId: this.sessionId , sessionId: this.sessionId
, requestId: 0 , requestId: 0
@@ -271,7 +271,7 @@ export default class Session {
this.client.close(); this.client.close();
clearInterval(this.clientHeartbeatIntervalId); clearInterval(this.clientHeartbeatIntervalId!);
this.sendMessage("shim:/session/impl_stop", { this.sendMessage("shim:/session/impl_stop", {
stopId stopId

View File

@@ -14,8 +14,8 @@ const NS_RECEIVER = "urn:x-cast:com.google.cast.receiver";
*/ */
export default class StatusListener extends EventEmitter { export default class StatusListener extends EventEmitter {
private client: Client; private client: Client;
private clientReceiver: Channel; private clientReceiver?: Channel;
private clientHeartbeatIntervalId: number; private clientHeartbeatIntervalId?: number;
constructor ( constructor (
private host: string private host: string
@@ -35,7 +35,10 @@ export default class StatusListener extends EventEmitter {
* Closes status listener connection. * Closes status listener connection.
*/ */
public deregister (): void { public deregister (): void {
this.clientReceiver.send({ type: "CLOSE" }); if (this.clientReceiver) {
this.clientReceiver.send({ type: "CLOSE" });
}
this.client.close(); this.client.close();
} }

View File

@@ -26,10 +26,15 @@ export class AirPlayAuthCredentials {
public clientSk: Uint8Array; public clientSk: Uint8Array;
public clientPk: Uint8Array; public clientPk: Uint8Array;
constructor (clientId: string, clientSk: Uint8Array) { constructor (
if (clientId && clientSk) { clientId?: string
, clientSk?: Uint8Array
, clientPk?: Uint8Array) {
if (clientId && clientSk && clientPk) {
this.clientId = clientId; this.clientId = clientId;
this.clientSk = clientSk; this.clientSk = clientSk;
this.clientPk = clientPk;
} else { } else {
// If specified without arguments, generate new credentials // If specified without arguments, generate new credentials
const keyPair = nacl.sign.keyPair(); const keyPair = nacl.sign.keyPair();
@@ -190,7 +195,7 @@ export class AirPlayAuth {
}); });
// Append Content-Type header if request has body // Append Content-Type header if request has body
if (data) { if (data && contentType) {
requestHeaders.append("Content-Type", contentType); requestHeaders.append("Content-Type", contentType);
} }

View File

@@ -82,22 +82,26 @@ let receiverSelectorApp: child_process.ChildProcess;
*/ */
async function handleMessage (message: Message) { async function handleMessage (message: Message) {
if (message.subject.startsWith("bridge:/media/")) { if (message.subject.startsWith("bridge:/media/")) {
if (existingMedia.has(message._id)) { const mediaId = message._id!;
if (existingMedia.has(mediaId)) {
// Forward message to instance message handler // Forward message to instance message handler
existingMedia.get(message._id).messageHandler(message); existingMedia.get(mediaId)!.messageHandler(message);
} else { } else {
if (message.subject.endsWith("/initialize")) { if (message.subject.endsWith("/initialize")) {
// Get Session object media belongs to // Get Session object media belongs to
const parentSession = existingSessions.get( const parentSession = existingSessions.get(
message.data._internalSessionId); message.data._internalSessionId);
// Create Media if (parentSession) {
existingMedia.set(message._id, new Media( // Create Media
message.data.sessionId existingMedia.set(mediaId, new Media(
, message.data.mediaSessionId message.data.sessionId
, message._id , message.data.mediaSessionId
, parentSession , mediaId
, sendMessage)); , parentSession
, sendMessage));
}
} }
} }
@@ -105,18 +109,20 @@ async function handleMessage (message: Message) {
} }
if (message.subject.startsWith("bridge:/session/")) { if (message.subject.startsWith("bridge:/session/")) {
if (existingSessions.has(message._id)) { const sessionId = message._id!;
if (existingSessions.has(sessionId)) {
// Forward message to instance message handler // Forward message to instance message handler
existingSessions.get(message._id).messageHandler(message); existingSessions.get(sessionId)!.messageHandler(message);
} else { } else {
if (message.subject.endsWith("/initialize")) { if (message.subject.endsWith("/initialize")) {
// Create Session // Create Session
existingSessions.set(message._id, new Session( existingSessions.set(sessionId, new Session(
message.data.address message.data.address
, message.data.port , message.data.port
, message.data.appId , message.data.appId
, message.data.sessionId , message.data.sessionId
, message._id , sessionId
, sendMessage)); , sendMessage));
} }
} }
@@ -161,8 +167,8 @@ async function handleMessage (message: Message) {
path.join(process.cwd(), "selector") path.join(process.cwd(), "selector")
, [ receiverSelectorData ]); , [ receiverSelectorData ]);
receiverSelectorApp.stdout.setEncoding("utf8"); receiverSelectorApp.stdout!.setEncoding("utf8");
receiverSelectorApp.stdout.on("data", data => { receiverSelectorApp.stdout!.on("data", data => {
sendMessage({ sendMessage({
subject: "main:/receiverSelector/selected" subject: "main:/receiverSelector/selected"
, data: JSON.parse(data) , data: JSON.parse(data)
@@ -280,7 +286,7 @@ function initialize (options: InitializeOptions) {
} }
}; };
if ("applications" in status) { if (status.applications && status.applications.length) {
const application = status.applications[0]; const application = status.applications[0];
receiverStatusMessage.data.status.application = { receiverStatusMessage.data.status.application = {
@@ -300,7 +306,7 @@ function initialize (options: InitializeOptions) {
const { id } = service.txt; const { id } = service.txt;
if (statusListeners.has(id)) { if (statusListeners.has(id)) {
statusListeners.get(id).deregister(); statusListeners.get(id)!.deregister();
statusListeners.delete(id); statusListeners.delete(id);
} }
} }

View File

@@ -43,7 +43,7 @@ export class ResponseTransform extends Transform {
export class DecodeTransform extends Transform { export class DecodeTransform extends Transform {
// Message data // Message data
private _messageBuffer = Buffer.alloc(0); private _messageBuffer = Buffer.alloc(0);
private _messageLength: number = null; private _messageLength?: number;
constructor () { constructor () {
super({ super({
@@ -64,7 +64,7 @@ export class DecodeTransform extends Transform {
]); ]);
for (;;) { for (;;) {
if (this._messageLength === null) { if (this._messageLength === undefined) {
if (this._messageBuffer.length >= 4) { if (this._messageBuffer.length >= 4) {
// Read message length and offset buffer // Read message length and offset buffer
this._messageLength = this._messageBuffer.readUInt32LE(0); this._messageLength = this._messageBuffer.readUInt32LE(0);
@@ -85,7 +85,7 @@ export class DecodeTransform extends Transform {
// Offset buffer to start of next message // Offset buffer to start of next message
this._messageBuffer = this._messageBuffer.slice( this._messageBuffer = this._messageBuffer.slice(
this._messageLength); this._messageLength);
this._messageLength = null; this._messageLength = undefined;
// Next message // Next message
continue; continue;

View File

@@ -4,4 +4,7 @@
"./src/**/*" "./src/**/*"
, "./@types/**/*" , "./@types/**/*"
] ]
, "compilerOptions": {
"strict": true
}
} }