Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/xtream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,15 @@ export class Xtream<T extends CustomSerializers = CustomSerializers> {
}

// Check if requested format is allowed for the user
// Fall back to first allowed format if not
if (!this.userProfile!.allowed_output_formats.includes(stream.extension)) {
format = this.userProfile!.allowed_output_formats[0];
// If not, fall back to first allowed format for the user
// Otherwise, if no allowed formats are specified, fall back to requested format
// Some services may return empty objects for userProfile
if (
!this.userProfile?.allowed_output_formats.includes(stream.extension)
) {
format =
this.userProfile?.allowed_output_formats[0] ?? stream.extension;
}

return `${this.baseUrl}/live/${this.#username}/${this.#password}/${stream.streamId}.${format}`;
}

Expand Down
50 changes: 50 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,56 @@ describe('Xtream API', () => {

expect(url).toBe('http://example.com/timeshift/test/password/60/2021-01-01:00-00/1.ts');
});

test('Falls back to requested extension when userProfile has empty allowed_output_formats', async () => {
const xtream = new Xtream({
url: 'http://example.com',
username: 'test',
password: 'password',
});

// Set userProfile with empty allowed_output_formats array
xtream.userProfile = {
username: 'test',
password: 'password',
message: '',
auth: 1,
status: 'Active',
exp_date: '1767542400',
is_trial: '0',
active_cons: 0,
created_at: '1735084800',
max_connections: '5',
allowed_output_formats: [],
};

const url = xtream.generateStreamUrl({
type: 'channel',
streamId: '123',
extension: 'm3u8',
});

expect(url).toBe('http://example.com/live/test/password/123.m3u8');
});

test('Falls back to requested extension when userProfile is undefined', () => {
const xtream = new Xtream({
url: 'http://example.com',
username: 'test',
password: 'password',
});

// Ensure userProfile is undefined
xtream.userProfile = undefined;

const url = xtream.generateStreamUrl({
type: 'channel',
streamId: '456',
extension: 'ts',
});

expect(url).toBe('http://example.com/live/test/password/456.ts');
});
});

describe('JSON:API serializer', () => {
Expand Down
Loading