Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseTrackData(text: string): Result {
const items = Vector.ofIterable(text.split('|'))
const status = items.head()
// Get rid of fields that we don't care about
const otherValues = items.drop(3)
if (status.isNone() || otherValues.length() < trackInfoKeys.length - 1) {
return failure('Could not parse track data')
}
const values = otherValues.prepend(status.get())
const trackInfoEntries = Vector.zip(trackInfoKeys, values)
const trackInfo = HashMap.ofIterable(trackInfoEntries)
.filterKeys(k => !k.startsWith('unknown'))
.map((k, v) => [k, mapTrackInfoValue(k, v)])
.put('state', statusCodeToName(status.get()))
.toObjectDictionary(x => x)
socket.on('data', data => {
const state = stateWrapper.get()
const stringData = Vector.ofIterable(data.toString())
// get rid of CRLF
.dropRight(2)
.mkString('')
if (Action.guard(stringData)) {
const nextState = stateWrapper.set(update(state, stringData))
socket.write(
[
mockTrackInfoResponse(nextState.currentTrack),
mockVolumeResponse(nextState.currentVolume)
].join('\r\n')
)
return logger.debug('Received command', {
action: stringData,
function parseMessage(raw: string): Result {
const parseMessageFailure: Failure = failure('Could not parse message')
const messageCode = raw.substring(0, 3)
switch (statusCodeToName(messageCode)) {
case 'info':
return success({
type: 'info',
data: raw
})
case 'volumeChange':
const vol = Vector.ofIterable(raw.split('|'))
.filter(v => v !== '')
.last()
return vol.isSome()
? success(nextVolume(vol.get()))
: parseMessageFailure
case 'playing':
case 'paused':
case 'stopped':
const trackInfo = parseTrackData(raw)
return trackInfo.success
? mapSuccess(trackInfo, (value: TrackInfo) => ({
type: 'playback',
data: value
}))
export function parseControlData(text: string): Message[] {
const lines: string[] = text.split('\r\n')
const messageList: Vector = Vector.ofIterable(lines).mapOption(
l => {
const messageResult = parseMessage(l)
return messageResult.success
? Option.of(messageResult.value)
: Option.none()
}
)
if (messageList.allMatch(InfoMessage.guard)) {
return [
{
type: 'info',
data: messageList.foldLeft(
'',
(data, message) =>
`${data}${data.length === 0 ? '' : '\n'}${message.data}`