Skip to content

Commit 9791a3f

Browse files
author
Mint de Wit
committed
chore: update formatting
1 parent 90c5267 commit 9791a3f

File tree

6 files changed

+78
-99
lines changed

6 files changed

+78
-99
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"/LICENSE"
2020
],
2121
"scripts": {
22-
"build": "trash dist && build:main",
22+
"build": "npx trash dist && npm run build:main",
2323
"build:main": "tsc -p ./tsconfig.build.json",
2424
"changelog": "npx sofie-version",
2525
"release": "npm run lint && npm run changelog",
@@ -28,7 +28,7 @@
2828
"prepare": "husky",
2929
"lint:raw": "eslint",
3030
"lint": "npm run lint:raw .",
31-
"lint-fix": "npm run lint --fix",
31+
"lint-fix": "npm run lint -- --fix",
3232
"license-validate": "npx sofie-licensecheck"
3333
},
3434
"engines": {
@@ -63,4 +63,4 @@
6363
]
6464
},
6565
"packageManager": "[email protected]"
66-
}
66+
}

src/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class MultilineParser {
8888
return {
8989
raw: lines.join('\r\n'),
9090
name: msg,
91-
parameters: params
91+
parameters: params,
9292
}
9393
} else {
9494
const headerMatch = lines[0].match(/(.+?)(:|)$/im)
@@ -114,7 +114,7 @@ export class MultilineParser {
114114
const res: DeserializedCommand = {
115115
raw: lines.join('\r\n'),
116116
name: msg,
117-
parameters: params
117+
parameters: params,
118118
}
119119
return res
120120
}

src/server.ts

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
CommandNames,
99
ErrorCode,
1010
NotifyType,
11-
ResponseInterface
11+
ResponseInterface,
1212
} from './types.js'
1313
import { createServer, Server } from 'net'
1414

@@ -25,25 +25,17 @@ export class HyperdeckServer {
2525
onRecord?: (command: DeserializedCommands.RecordCommand) => Promise<void>
2626
onStop?: (command: DeserializedCommand) => Promise<void>
2727
onClipsCount?: (command: DeserializedCommand) => Promise<ResponseInterface.ClipsCount>
28-
onClipsGet?: (
29-
command: DeserializedCommands.ClipsGetCommand
30-
) => Promise<ResponseInterface.ClipsGet>
28+
onClipsGet?: (command: DeserializedCommands.ClipsGetCommand) => Promise<ResponseInterface.ClipsGet>
3129
onClipsAdd?: (command: DeserializedCommands.ClipsAddCommand) => Promise<void>
3230
onClipsClear?: (command: DeserializedCommand) => Promise<void>
3331
onTransportInfo?: (command: DeserializedCommand) => Promise<ResponseInterface.TransportInfo>
34-
onSlotInfo?: (
35-
command: DeserializedCommands.SlotInfoCommand
36-
) => Promise<ResponseInterface.SlotInfo>
32+
onSlotInfo?: (command: DeserializedCommands.SlotInfoCommand) => Promise<ResponseInterface.SlotInfo>
3733
onSlotSelect?: (command: DeserializedCommands.SlotSelectCommand) => Promise<void>
3834
onGoTo?: (command: DeserializedCommands.GoToCommand) => Promise<void>
3935
onJog?: (command: DeserializedCommands.JogCommand) => Promise<void>
4036
onShuttle?: (command: DeserializedCommands.ShuttleCommand) => Promise<void>
41-
onRemote?: (
42-
command: DeserializedCommands.RemoteCommand
43-
) => Promise<ResponseInterface.RemoteOptions>
44-
onConfiguration?: (
45-
command: DeserializedCommands.ConfigurationCommand
46-
) => Promise<ResponseInterface.Configuration>
37+
onRemote?: (command: DeserializedCommands.RemoteCommand) => Promise<ResponseInterface.RemoteOptions>
38+
onConfiguration?: (command: DeserializedCommands.ConfigurationCommand) => Promise<ResponseInterface.Configuration>
4739
onUptime?: (command: DeserializedCommand) => Promise<ResponseInterface.Uptime>
4840
onFormat?: (command: DeserializedCommands.FormatCommand) => Promise<ResponseInterface.Format>
4941
onIdentify?: (command: DeserializedCommands.IdentifyCommand) => Promise<void>
@@ -52,9 +44,7 @@ export class HyperdeckServer {
5244
constructor(ip?: string, port = 9993, maxConnections = 1) {
5345
this._server = createServer((socket) => {
5446
const socketId = Math.random().toString(35).substr(-6)
55-
this._sockets[socketId] = new HyperdeckSocket(socket, async (cmd) =>
56-
this._receivedCommand(cmd)
57-
)
47+
this._sockets[socketId] = new HyperdeckSocket(socket, async (cmd) => this._receivedCommand(cmd))
5848
this._sockets[socketId].on('disconnected', () => {
5949
delete this._sockets[socketId]
6050
})
@@ -87,97 +77,96 @@ export class HyperdeckServer {
8777
if (err) return new TResponse(err.code, err.msg)
8878
else return new TResponse(ErrorCode.InternalError, 'internal error')
8979
}
90-
let executor:
91-
| ((command: DeserializedCommand) => Promise<typeof ResponseInterface | void>)
92-
| undefined
80+
let executor: ((command: DeserializedCommand) => Promise<typeof ResponseInterface | void>) | undefined
9381
let resHandler: ((res: Hash<string> | void) => TResponse) | undefined
9482

95-
if (cmd.name === CommandNames.DeviceInfoCommand) {
83+
const commandName: CommandNames = cmd.name as CommandNames
84+
85+
if (commandName === CommandNames.DeviceInfoCommand) {
9686
executor = this.onDeviceInfo
9787
resHandler = (res) => new TResponse(SynchronousCode.DeviceInfo, 'device info', res)
98-
} else if (cmd.name === CommandNames.DiskListCommand) {
88+
} else if (commandName === CommandNames.DiskListCommand) {
9989
executor = this.onDiskList
10090
resHandler = (res) => new TResponse(SynchronousCode.DiskList, 'disk list', res)
101-
} else if (cmd.name === CommandNames.PreviewCommand) {
91+
} else if (commandName === CommandNames.PreviewCommand) {
10292
executor = this.onPreview
10393
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
104-
} else if (cmd.name === CommandNames.PlayCommand) {
94+
} else if (commandName === CommandNames.PlayCommand) {
10595
executor = this.onPlay
10696
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
107-
} else if (cmd.name === CommandNames.PlayrangeSetCommand) {
97+
} else if (commandName === CommandNames.PlayrangeSetCommand) {
10898
executor = this.onPlayrangeSet
10999
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
110-
} else if (cmd.name === CommandNames.PlayrangeClearCommand) {
100+
} else if (commandName === CommandNames.PlayrangeClearCommand) {
111101
executor = this.onPlayrangeClear
112102
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
113-
} else if (cmd.name === CommandNames.RecordCommand) {
103+
} else if (commandName === CommandNames.RecordCommand) {
114104
executor = this.onRecord
115105
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
116-
} else if (cmd.name === CommandNames.StopCommand) {
106+
} else if (commandName === CommandNames.StopCommand) {
117107
executor = this.onStop
118108
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
119-
} else if (cmd.name === CommandNames.ClipsCountCommand) {
109+
} else if (commandName === CommandNames.ClipsCountCommand) {
120110
executor = this.onClipsCount
121111
resHandler = (res) => new TResponse(SynchronousCode.ClipsCount, 'clips count', res)
122-
} else if (cmd.name === CommandNames.ClipsGetCommand) {
112+
} else if (commandName === CommandNames.ClipsGetCommand) {
123113
executor = this.onClipsGet
124114
resHandler = (res) => new TResponse(SynchronousCode.ClipsInfo, 'clips info', res)
125-
} else if (cmd.name === CommandNames.ClipsAddCommand) {
115+
} else if (commandName === CommandNames.ClipsAddCommand) {
126116
executor = this.onClipsAdd
127117
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
128-
} else if (cmd.name === CommandNames.ClipsClearCommand) {
118+
} else if (commandName === CommandNames.ClipsClearCommand) {
129119
executor = this.onClipsClear
130120
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
131-
} else if (cmd.name === CommandNames.TransportInfoCommand) {
121+
} else if (commandName === CommandNames.TransportInfoCommand) {
132122
executor = this.onTransportInfo
133-
resHandler = (res) =>
134-
new TResponse(SynchronousCode.TransportInfo, 'transport info', res)
135-
} else if (cmd.name === CommandNames.SlotInfoCommand) {
123+
resHandler = (res) => new TResponse(SynchronousCode.TransportInfo, 'transport info', res)
124+
} else if (commandName === CommandNames.SlotInfoCommand) {
136125
executor = this.onSlotInfo
137126
resHandler = (res) => new TResponse(SynchronousCode.SlotInfo, 'slot info', res)
138-
} else if (cmd.name === CommandNames.SlotSelectCommand) {
127+
} else if (commandName === CommandNames.SlotSelectCommand) {
139128
executor = this.onSlotSelect
140129
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
141-
} else if (cmd.name === CommandNames.NotifyCommand) {
130+
} else if (commandName === CommandNames.NotifyCommand) {
142131
// implemented in socket.ts
143132
return new TResponse(SynchronousCode.OK, 'ok')
144-
} else if (cmd.name === CommandNames.GoToCommand) {
133+
} else if (commandName === CommandNames.GoToCommand) {
145134
executor = this.onGoTo
146135
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
147-
} else if (cmd.name === CommandNames.JogCommand) {
136+
} else if (commandName === CommandNames.JogCommand) {
148137
executor = this.onJog
149138
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
150-
} else if (cmd.name === CommandNames.ShuttleCommand) {
139+
} else if (commandName === CommandNames.ShuttleCommand) {
151140
executor = this.onShuttle
152141
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
153-
} else if (cmd.name === CommandNames.RemoteCommand) {
142+
} else if (commandName === CommandNames.RemoteCommand) {
154143
executor = this.onRemote
155144
resHandler = (res?) => {
156145
if (!res) return new TResponse(SynchronousCode.OK, 'ok')
157146
else return new TResponse(SynchronousCode.Remote, 'remote', res)
158147
}
159-
} else if (cmd.name === CommandNames.ConfigurationCommand) {
148+
} else if (commandName === CommandNames.ConfigurationCommand) {
160149
executor = this.onConfiguration
161150
resHandler = (res) => {
162151
if (res) return new TResponse(SynchronousCode.Configuration, 'configuration', res)
163152
else return new TResponse(SynchronousCode.OK, 'ok')
164153
}
165-
} else if (cmd.name === CommandNames.UptimeCommand) {
154+
} else if (commandName === CommandNames.UptimeCommand) {
166155
executor = this.onUptime
167156
resHandler = (res) => new TResponse(SynchronousCode.Uptime, 'uptime', res)
168-
} else if (cmd.name === CommandNames.FormatCommand) {
157+
} else if (commandName === CommandNames.FormatCommand) {
169158
executor = this.onFormat
170159
resHandler = (res?) => {
171160
if (res) return new TResponse(SynchronousCode.FormatReady, 'format ready', res)
172161
else return new TResponse(SynchronousCode.OK, 'ok')
173162
}
174-
} else if (cmd.name === CommandNames.IdentifyCommand) {
163+
} else if (commandName === CommandNames.IdentifyCommand) {
175164
executor = this.onIdentify
176165
resHandler = () => new TResponse(SynchronousCode.OK, 'ok')
177-
} else if (cmd.name === CommandNames.WatchdogCommand) {
166+
} else if (commandName === CommandNames.WatchdogCommand) {
178167
// implemented in socket.ts
179168
return new TResponse(SynchronousCode.OK, 'ok')
180-
} else if (cmd.name === CommandNames.PingCommand) {
169+
} else if (commandName === CommandNames.PingCommand) {
181170
// implemented in socket.ts
182171
return new TResponse(SynchronousCode.OK, 'ok')
183172
}
@@ -186,6 +175,6 @@ export class HyperdeckServer {
186175
return executor(cmd).then(resHandler, intErrorCatch)
187176
}
188177

189-
return Promise.reject()
178+
return Promise.reject(new Error('Unknown command'))
190179
}
191180
}

src/socket.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
DeserializedCommands,
1010
NotifyType,
1111
Hash,
12-
SynchronousCode
12+
SynchronousCode,
1313
} from './types.js'
1414
import { MultilineParser } from './parser.js'
1515

@@ -18,14 +18,14 @@ export class HyperdeckSocket extends EventEmitter {
1818
private _parser: MultilineParser
1919
private _receivedCommand: (cmd: DeserializedCommand) => Promise<TResponse>
2020
private _lastReceived?: number
21-
private _watchdogTimer?: NodeJS.Timer
21+
private _watchdogTimer?: NodeJS.Timeout
2222

2323
private _notifySettings = {
2424
slot: false,
2525
transport: false,
2626
remote: false,
2727
configuration: false,
28-
'dropped frames': false // @todo: implement
28+
'dropped frames': false, // @todo: implement
2929
}
3030

3131
constructor(socket: Socket, receivedCommand: (cmd: DeserializedCommand) => Promise<TResponse>) {
@@ -45,7 +45,7 @@ export class HyperdeckSocket extends EventEmitter {
4545
this.sendResponse(
4646
new TResponse(AsynchronousCode.ConnectionInfo, 'connection info', {
4747
'protocol version': '1.8',
48-
model: 'NodeJS Hyperdeck Server Library'
48+
model: 'NodeJS Hyperdeck Server Library',
4949
})
5050
)
5151
}
@@ -56,31 +56,32 @@ export class HyperdeckSocket extends EventEmitter {
5656
const cmds = this._parser.receivedString(data)
5757

5858
for (const cmd of cmds) {
59+
const commandName = cmd.name as CommandNames
5960
// special cases
60-
if (cmd.name === CommandNames.WatchdogCommand) {
61+
if (commandName === CommandNames.WatchdogCommand) {
6162
if (this._watchdogTimer) clearInterval(this._watchdogTimer)
6263

6364
const watchdogCmd = cmd as DeserializedCommands.WatchdogCommand
6465
if (watchdogCmd.parameters.period) {
65-
this._watchdogTimer = setInterval(() => {
66-
if (
67-
this._lastReceived &&
68-
Date.now() - this._lastReceived >
69-
Number(watchdogCmd.parameters.period) * 1000
70-
) {
71-
this._socket.destroy()
72-
this.emit('disconnected')
73-
clearInterval(this._watchdogTimer)
74-
}
75-
}, Number(watchdogCmd.parameters.period) * 1000)
66+
this._watchdogTimer = setInterval(
67+
() => {
68+
if (
69+
this._lastReceived &&
70+
Date.now() - this._lastReceived > Number(watchdogCmd.parameters.period) * 1000
71+
) {
72+
this._socket.destroy()
73+
this.emit('disconnected')
74+
clearInterval(this._watchdogTimer)
75+
}
76+
},
77+
Number(watchdogCmd.parameters.period) * 1000
78+
)
7679
}
77-
} else if (cmd.name === CommandNames.NotifyCommand) {
80+
} else if (commandName === CommandNames.NotifyCommand) {
7881
const notifyCmd = cmd as DeserializedCommands.NotifyCommand
7982

8083
if (Object.keys(notifyCmd.parameters).length > 0) {
81-
for (const param of Object.keys(notifyCmd.parameters) as Array<
82-
keyof typeof notifyCmd.parameters
83-
>) {
84+
for (const param of Object.keys(notifyCmd.parameters) as Array<keyof typeof notifyCmd.parameters>) {
8485
if (this._notifySettings[param] !== undefined) {
8586
this._notifySettings[param] = notifyCmd.parameters[param] === 'true'
8687
}
@@ -117,17 +118,13 @@ export class HyperdeckSocket extends EventEmitter {
117118

118119
notify(type: NotifyType, params: Hash<string>): void {
119120
if (type === NotifyType.Configuration && this._notifySettings.configuration) {
120-
this.sendResponse(
121-
new TResponse(AsynchronousCode.ConfigurationInfo, 'configuration info', params)
122-
)
121+
this.sendResponse(new TResponse(AsynchronousCode.ConfigurationInfo, 'configuration info', params))
123122
} else if (type === NotifyType.Remote && this._notifySettings.remote) {
124123
this.sendResponse(new TResponse(AsynchronousCode.RemoteInfo, 'remote info', params))
125124
} else if (type === NotifyType.Slot && this._notifySettings.slot) {
126125
this.sendResponse(new TResponse(AsynchronousCode.SlotInfo, 'slot info', params))
127126
} else if (type === NotifyType.Transport && this._notifySettings.transport) {
128-
this.sendResponse(
129-
new TResponse(AsynchronousCode.TransportInfo, 'transport info', params)
130-
)
127+
this.sendResponse(new TResponse(AsynchronousCode.TransportInfo, 'transport info', params))
131128
}
132129
}
133130
}

0 commit comments

Comments
 (0)