Skip to content

Commit ba1dd3f

Browse files
committed
Bug fixes
1 parent 9d612cb commit ba1dd3f

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

makemkv.js

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Interact with MakeMKV CLI */
22

33
const fs = require('fs'),
4+
os = require('os'),
45
EventEmitter = require('events').EventEmitter,
56
parseCsv = require('csv-parse/lib/sync'),
67
path = require('path'),
@@ -10,7 +11,6 @@ const fs = require('fs'),
1011

1112
const ALL_DRIVES = constants.ALL_DRIVES,
1213
DRIVE_STATE_FLAG = constants.DRIVE_STATE_FLAG,
13-
NEWLINE_CHAR = constants.NEWLINE_CHAR,
1414
TRACK_ATTRIBUTES = constants.TRACK_ATTRIBUTES;
1515

1616
const CONVERSION_PROFILE = settings.CONVERSION_PROFILE,
@@ -151,44 +151,61 @@ class MakeMkv {
151151

152152
let newCallback = (exitCode, stdErr, stdOut) => {
153153

154-
this.busyDevices.delete(ALL_DRIVES);
154+
console.debug(stdOut);
155+
console.debug(stdErr);
155156

156-
if (exitCode) {
157-
// @TODO: handle errors.
158-
}
157+
this.busyDevices.delete(ALL_DRIVES);
159158

160159
let discData = {},
161-
parsedOutput = parseCsv(stdOut);
160+
parsedOutput = parseCsv(
161+
stdOut.join(os.EOL),
162+
{relax_column_count: true}
163+
);
162164

163165
parsedOutput.map((dataRow) => {
164166

165167
dataRow = this._parseOutputRow(dataRow);
166168

167-
if (dataRow.attributeType !== this.ATTRIBUTE_TYPE_DRIVE_INFO) {
169+
if (!dataRow) {
168170
return;
169171
}
170-
if (!dataRow.driveId) {
172+
173+
console.debug(
174+
`AttributeType: ${dataRow.attributeType}
175+
DriveId: ${dataRow.driveId}`
176+
);
177+
178+
if (dataRow.attributeType == this.ATTRIBUTE_TYPE_DRIVE_INFO) {
179+
console.log(dataRow);
180+
}
181+
182+
if (
183+
!dataRow
184+
|| dataRow.attributeType !== this.ATTRIBUTE_TYPE_DRIVE_INFO
185+
|| !dataRow.driveId
186+
) {
171187
return;
172188
}
173189

174-
// Maintain an index => drive mapping.
175-
// @TODO: Is this even used? Feels very legacy to me...
176-
this.driveMap[dataRow.driveIndex] = dataRow.driveId;
190+
console.debug(dataRow);
177191

178192
// Assign the flags to string representations
179193
dataRow.driveState = DRIVE_STATE_FLAG[dataRow.driveState];
194+
180195
// @TODO: Bitwise determination of the disc filesystem flags.
181196

182197
// Add the processed row into the data.
183198
discData[dataRow.driveId] = dataRow;
184199

185200
});
186201

202+
console.debug("Got data for drives.");
203+
console.debug(discData);
187204
callback(discData);
188205

189206
};
190207

191-
this._spawnMakemkv(ALL_DRIVES, ['info'], newCallback);
208+
this._spawnMakemkv(ALL_DRIVES, ['info', 'disc:9999'], newCallback);
192209

193210
}
194211

@@ -275,18 +292,18 @@ class MakeMkv {
275292
The output lines appear in a certain order::
276293
277294
TCOUNT (track count)
278-
CINFO (Disc level information)
279-
...
280-
TINFO (Track level information)
295+
CINFO (Disc information)
281296
...
282-
SINFO (Stream level information)
297+
TINFO (Track 0 information)
283298
...
284-
TINFO (Next track level information
285-
...
286-
SINFO (Next stream level information)
299+
SINFO (Track 0, Stream 0 information)
287300
...
301+
SINFO (Track 0, Stream 1 information)
288302
...
303+
TINFO (Track 1 information)
289304
...
305+
SINFO (Track 1, Stream 0 information)
306+
... repeating ...
290307
291308
@param {int} exitCode MakeMKV exit code
292309
@param {array} stdErr Array of lines representing the error stream.
@@ -379,10 +396,12 @@ class MakeMkv {
379396

380397
let [attrType, attrFlag] = dataRow[0].split(':');
381398

399+
// Zip the keys and values into an obj and add the attribute type.
382400
let parseData = (keys) => {
383401
dataRow[0] = attrFlag;
384-
let data = keys.map((key, idx) => [key, dataRow[idx]]);
385-
data.attributeType = attrType;
402+
let data = {attributeType: attrType};
403+
keys.forEach((key, idx) => data[key] = dataRow[idx]);
404+
return data;
386405
};
387406

388407
switch(attrType) {
@@ -405,6 +424,9 @@ class MakeMkv {
405424
case this.ATTRIBUTE_TYPE_STREAM_INFO:
406425
return parseData(this.HEADERS_DISC_INFO_STREAM);
407426

427+
default:
428+
return false;
429+
408430
}
409431

410432
}
@@ -431,7 +453,7 @@ class MakeMkv {
431453
*/
432454
_spawn(args, binaryPath, callback) {
433455

434-
console.log(`Spawning ${binaryPath} with args ${args}`);
456+
console.log(`Spawning "${binaryPath}" with args: "${args}"`);
435457

436458
let process = spawn(binaryPath, args);
437459

@@ -446,12 +468,15 @@ class MakeMkv {
446468
process.stderr.setEncoding('utf-8');
447469

448470
process.stdout.on(
449-
'data', (data) => stdOut.push.apply(data.split(NEWLINE_CHAR))
471+
'data', (data) => { stdOut.push(...data.split(os.EOL)) }
450472
);
451473
process.stderr.on(
452-
'data', (data) => stdErr.push.apply(data.split(NEWLINE_CHAR))
474+
'data', (data) => { stdErr.push(...data.split(os.EOL)) }
453475
);
454-
process.on('exit', (code) => callback(code, stdErr, stdOut));
476+
process.on('exit', (code) => {
477+
console.debug(`MakeMKV exited with code ${code}.`);
478+
callback(code, stdErr, stdOut);
479+
});
455480

456481
}
457482

@@ -479,6 +504,7 @@ class MakeMkv {
479504
return this._waitAvailable(driveId, callback, ...args);
480505
}
481506
};
507+
console.debug(`Waiting for ${driveId} to become available.`);
482508
setTimeout(timer, 1000);
483509
}
484510

server.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Subscribable {
4242
}
4343

4444
subscribe(socketClient) {
45+
console.debug(`Subscribing client to ${this.name}`);
4546
this.clients.add(socketClient);
4647
socketClient.on('disconnect', () => this.disconnect(socketClient));
4748
this.emitData(socketClient);
@@ -52,6 +53,9 @@ class Subscribable {
5253
}
5354

5455
broadcastData() {
56+
console.debug(`Broadcasting to ${this.clients.length} clients.`);
57+
console.debug(this.clients);
58+
console.debug(this.data);
5559
this.clients.forEach((client) => this.emitData(client));
5660
}
5761

@@ -78,7 +82,7 @@ class Subscribable {
7882
dataObject = dataObject[currentKey];
7983
return this.update(futureKey, value, skipBroadcast, dataObject);
8084
}
81-
this.dataObject[key] = value;
85+
dataObject[key] = value;
8286
if (!skipBroadcast) {
8387
this.broadcastData();
8488
}
@@ -204,14 +208,16 @@ class MakeMkvServer {
204208

205209
initDevices() {
206210
this.makeMkv.scanDiscs(
207-
(driveInfo) => this.driveInfo.updateBulk(driveInfo)
211+
(driveInfo) => {
212+
this.driveInfo.updateBulk(driveInfo);
213+
}
208214
);
209215
}
210216

211217
doDiscInfo(data) {
212218
let driveId = data.driveId;
213219
let callback = (stdErr, discInfo) => {
214-
this.discInfo[driveId] = {};
220+
this.discInfo.update(driveId, {});
215221
this.discInfo.updateBulk(discInfo, this.discInfo[driveId]);
216222
};
217223
this.makeMkv.getDiscInfo(driveId, callback);

settings.example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@
1616
"PERMISSIONS": {
1717
"file": "0666",
1818
"directory": "0777"
19+
},
20+
"SSL": {
21+
"ENABLED": false,
22+
"KEY": "/path/to/ssl/key.pem",
23+
"CERT": "/path/to/ssl/cert.pem"
1924
}
2025
}

0 commit comments

Comments
 (0)