Skip to content

Commit fae227e

Browse files
author
Ludwig DUBOS
committed
use pathlike for uri arguments
1 parent 1747765 commit fae227e

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

lib/index.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { EventEmitter } from 'events';
2+
import { URL } from 'url';
3+
import fs from 'fs';
24
import http from 'http';
35
import querystring from 'querystring';
46

@@ -95,7 +97,7 @@ type StatusStopped = StatusBase & {
9597

9698
type Status = StatusPaused | StatusPlaying | StatusStopped;
9799

98-
const enum CommandScope {
100+
enum CommandScope {
99101
BROWSE = '/requests/browse.json',
100102
STATUS = '/requests/status.json',
101103
PLAYLIST = '/requests/playlist.json'
@@ -144,6 +146,16 @@ type Playlist = PlaylistNode | PlaylistLeaf;
144146
const waitFor = (ms: number): Promise<void> =>
145147
new Promise(resolve => setTimeout(resolve, ms));
146148

149+
const pathlikeToString = (path: fs.PathLike) => {
150+
if (Buffer.isBuffer(path)) {
151+
return path.toString('utf8');
152+
} else if (path instanceof URL) {
153+
return path.href;
154+
}
155+
156+
return path;
157+
};
158+
147159
export type VLCOptions = {
148160
host?: string;
149161
port?: number;
@@ -240,7 +252,7 @@ function equal(a: any, b: any) {
240252
return true;
241253
}
242254

243-
return false
255+
return false;
244256
}
245257

246258
export class VLC extends EventEmitter {
@@ -437,30 +449,30 @@ export class VLC extends EventEmitter {
437449
* Add `uri` to playlist and start playback.
438450
*/
439451
public addToQueueAndPlay(
440-
uri: string,
452+
uri: fs.PathLike,
441453
option?: 'noaudio' | 'novideo'
442454
): Promise<Status> {
443-
const options = {
444-
input: uri,
455+
return this._sendCommand(CommandScope.STATUS, 'in_play', {
456+
input: pathlikeToString(uri),
445457
option
446-
};
447-
448-
return this._sendCommand(CommandScope.STATUS, 'in_play', options);
458+
});
449459
}
450460

451461
/**
452462
* Add `uri` to playlist.
453463
*/
454-
public addToQueue(uri: string): Promise<Status> {
455-
return this._sendCommand(CommandScope.STATUS, 'in_enqueue', { input: uri });
464+
public addToQueue(uri: fs.PathLike): Promise<Status> {
465+
return this._sendCommand(CommandScope.STATUS, 'in_enqueue', {
466+
input: pathlikeToString(uri)
467+
});
456468
}
457469

458470
/**
459471
* Add subtitle to currently playing file.
460472
*/
461-
public addSubtitle(uri: string): Promise<Status> {
473+
public addSubtitle(uri: fs.PathLike): Promise<Status> {
462474
return this._sendCommand(CommandScope.STATUS, 'addsubtitle', {
463-
input: uri
475+
input: pathlikeToString(uri)
464476
});
465477
}
466478

0 commit comments

Comments
 (0)