/**
* Playback manifest client of the audio player.
*
* It owns the transport concerns of a playback manifest: expanding the manifest route template with the Ids of a play
* descriptor, appending the capability query, applying the request timeout, following the player's redirect policy, and
* validating the stream path before a request that sends the session cookie is made. The playback engine and the later
* normalized delivery pipeline both consume it, so the route, capability and same-origin rules are defined once.
*/
(function (LuminaAudioPlayer) {
'use strict';
/**
* The timeout, in milliseconds, of a playback manifest request.
* @readonly
* @type {number}
*/
const MANIFEST_REQUEST_TIMEOUT_MS = 20000;
/**
* Checks whether a value is a Guid, the only form the manifest route accepts for its Ids.
* @param {*} value - The value to check.
* @returns {boolean} True when the value is a Guid string.
*/
function isGuid(value) {
return typeof value === 'string'
&& /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
}
/**
* Gets the origin this module resolves relative URLs against. The page origin is used in a browser; a fixed local
* origin keeps the route and origin checks testable in a host without a DOM.
* @returns {string} The base origin.
*/
function getBaseOrigin() {
return typeof window !== 'undefined' && window.location && window.location.origin
? window.location.origin
: 'http://localhost';
}
/**
* Checks whether the module can compare a URL against the page origin.
* @returns {boolean} True when a page origin is available.
*/
function hasWindowOrigin() {
return typeof window !== 'undefined' && !!window.location && !!window.location.origin;
}
/**
* Resolves and validates a path that a request carrying the session cookie will fetch.
*
* The value is built by the server, but it is validated again here as defense in depth: it must be a root relative
* path on this origin, with no backslash, control character, fragment, userinfo, dot segment, or encoded path
* separator that a browser could normalize into another location. The resolved absolute URL is returned, pinned to
* this origin by construction, so a base element can never redirect the fetch to another origin.
* @param {*} value - The path to validate.
* @returns {{ href: string, url: URL }} The validated resolved path and its parsed URL.
* @throws {LuminaAudioPlayer.PlayerError} When the path is not a safe same-origin root relative path.
*/
function resolveSameOriginUrl(value) {
if (typeof value !== 'string' || value.length === 0)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The request carries no path.');
if (!value.startsWith('/') || value.startsWith('//'))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The path is not a root relative path.');
if (/[\u0000-\u001f\u007f\\#]/.test(value))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The path carries a forbidden character.');
const queryIndex = value.indexOf('?');
const path = queryIndex >= 0 ? value.slice(0, queryIndex) : value;
// Decode every segment before judging it. The browser normalizes %2e%2e, %2f and %5c after the string is accepted,
// so checking only the raw text would let an encoded separator or dot segment through.
for (const segment of path.split('/')) {
if (segment === '')
continue;
let decoded;
try {
decoded = decodeURIComponent(segment);
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The path carries an invalid escape.', { cause: error });
}
if (decoded === '.' || decoded === '..' || decoded.includes('/') || decoded.includes('\\')
|| /[\u0000-\u001f\u007f]/.test(decoded))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The path carries a traversal segment.');
}
// Resolve last, so the returned href is absolute and a base element cannot change where the fetch goes. A host
// without a DOM uses the synthetic base only for parsing and keeps the raw path.
let resolved;
try {
resolved = new URL(value, getBaseOrigin());
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The path is not a valid URL.', { cause: error });
}
// A request with credentials or a non-http scheme is not one this player is allowed to make, and an origin other
// than the page origin would send the session cookie somewhere it must never go.
if ((resolved.protocol !== 'http:' && resolved.protocol !== 'https:')
|| (hasWindowOrigin() && resolved.origin !== window.location.origin)
|| resolved.username !== ''
|| resolved.password !== '')
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The path is not on this origin.');
return { href: hasWindowOrigin() ? resolved.href : value, url: resolved };
}
/**
* Checks that a resolved stream path matches the stream route template this application emitted and belongs to the
* expected track and library. The template is the single source of truth for the route's static segments, their order
* and its placeholders, so a server route rename or an inserted segment does not need a matching client edit. The
* same-origin check alone is not enough: the session cookie and the Range header are sent before the response is
* inspected, so a state changing GET route on this origin must never be reachable as a stream.
* @param {URL} url - The resolved stream URL.
* @param {Object} descriptor - The play descriptor the manifest belongs to.
* @param {Object} configuration - The endpoints configuration of the player.
* @throws {LuminaAudioPlayer.PlayerError} When the route template is missing, is on another origin, or the path does not match the route or the expected track and library Ids.
*/
function validateStreamRoute(url, descriptor, configuration) {
const templateValue = configuration && configuration.streamTrackRouteTemplate;
if (typeof templateValue !== 'string' || templateValue.length === 0)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream route template is not configured.');
let template;
try {
template = new URL(templateValue, getBaseOrigin());
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream route template is not a valid URL.', { cause: error });
}
if ((template.protocol !== 'http:' && template.protocol !== 'https:')
|| (hasWindowOrigin() && template.origin !== window.location.origin)
|| template.origin !== url.origin
|| template.username !== ''
|| template.password !== '')
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream route template is not on this origin.');
const expectedIds = {
artistId: descriptor && descriptor.artistId,
albumId: descriptor && descriptor.albumId,
trackId: descriptor && descriptor.trackId
};
const templateSegments = template.pathname.split('/');
const streamSegments = url.pathname.split('/');
if (templateSegments.length !== streamSegments.length)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream path does not match the stream route.');
for (let index = 0; index < templateSegments.length; index++) {
let templateSegment;
try {
// The URL parser percent-encodes braces, so a template segment has to be decoded before it can be recognized as a placeholder.
templateSegment = decodeURIComponent(templateSegments[index]);
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream route template carries an invalid escape.', { cause: error });
}
const placeholder = /^\{([A-Za-z0-9_]+)\}$/.exec(templateSegment);
if (placeholder) {
const name = placeholder[1];
if (!Object.prototype.hasOwnProperty.call(expectedIds, name))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream route template carries an unknown placeholder.');
const expected = expectedIds[name];
if (!isGuid(expected))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, `The play descriptor carries no valid ${name}.`, { field: name });
const actual = streamSegments[index];
// The path must describe the track of this descriptor, not merely another track of the same library.
if (!isGuid(actual) || actual.toLowerCase() !== expected.toLowerCase())
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream path does not belong to the expected track.');
continue;
}
let actualSegment;
try {
// Decode the actual segment too, so a path base or a static segment that contains an encoded character still compares equal to the template it came from.
actualSegment = decodeURIComponent(streamSegments[index]);
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream path carries an invalid escape.', { cause: error });
}
if (templateSegment !== actualSegment)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream path does not match the stream route.');
}
const libraryId = descriptor && descriptor.libraryId;
if (!isGuid(libraryId))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, 'The play descriptor carries no valid library Id.', { field: 'libraryId' });
const requestedLibraryIds = url.searchParams.getAll('libraryId');
if (requestedLibraryIds.length !== 1 || requestedLibraryIds[0].toLowerCase() !== libraryId.toLowerCase())
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The stream path does not belong to the expected library.');
}
/**
* Resolves the stream path of a playback manifest into the URL the player fetches, after checking that it points at
* the stream route of the expected track and library.
* @param {Object} manifest - The playback manifest.
* @param {Object} descriptor - The play descriptor the manifest belongs to.
* @param {Object} configuration - The endpoints configuration of the player.
* @returns {string} The validated resolved stream URL.
* @throws {LuminaAudioPlayer.PlayerError} When the manifest carries no safe stream path or the path is not the
* stream route of the expected track and library.
*/
function resolveStreamUrl(manifest, descriptor, configuration) {
const resolved = resolveSameOriginUrl(manifest && manifest.streamPath);
validateStreamRoute(resolved.url, descriptor, configuration);
return resolved.href;
}
/**
* Builds the URL of the playback manifest endpoint for a descriptor, including the capability list.
* @param {Object} configuration - The endpoints configuration of the player.
* @param {Object} descriptor - The play descriptor of the track.
* @param {string} capabilities - The comma separated container and codec capability list, or an empty string.
* @returns {string} The absolute manifest URL, pinned to this origin.
* @throws {LuminaAudioPlayer.PlayerError} When the descriptor or the configuration is incomplete, or the endpoint is not on this origin.
*/
function buildManifestUrl(configuration, descriptor, capabilities) {
const template = configuration && configuration.getPlaybackManifestUrl;
if (!template)
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The playback manifest endpoint is not configured.');
const ids = {
artistId: descriptor.artistId,
albumId: descriptor.albumId,
trackId: descriptor.trackId,
libraryId: descriptor.libraryId
};
for (const name of Object.keys(ids)) {
if (!ids[name])
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, `The play descriptor carries no ${name}.`, { field: name });
// The manifest route only accepts Guids, and a value that is not a Guid must never be interpolated into a path the browser resolves against this origin.
if (!isGuid(ids[name]))
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.INVALID_DESCRIPTOR, `The play descriptor carries an invalid ${name}.`, { field: name });
}
let url = String(template)
.split('{artistId}').join(encodeURIComponent(ids.artistId))
.split('{albumId}').join(encodeURIComponent(ids.albumId))
.split('{trackId}').join(encodeURIComponent(ids.trackId));
const separator = url.includes('?') ? '&' : '?';
url = `${url}${separator}libraryId=${encodeURIComponent(ids.libraryId)}`;
if (capabilities)
url = `${url}&capabilities=${encodeURIComponent(capabilities)}`;
// The manifest template is request derived, and the manifest is the response that controls the next fetches, so
// it is resolved against this origin and a different origin is refused instead of being requested.
let resolved;
try {
resolved = new URL(url, getBaseOrigin());
} catch (error) {
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The playback manifest endpoint is not a valid URL.', { cause: error });
}
if ((resolved.protocol !== 'http:' && resolved.protocol !== 'https:')
|| (hasWindowOrigin() && resolved.origin !== window.location.origin)
|| resolved.username !== ''
|| resolved.password !== '')
throw new LuminaAudioPlayer.PlayerError(LuminaAudioPlayer.PlayerErrorCode.ENGINE_LOAD_FAILED, 'The playback manifest endpoint is not on this origin.');
return resolved.href;
}
/**
* Creates an AbortSignal for a manifest request that aborts on the external signal or after the manifest timeout.
* @param {AbortSignal} [externalSignal] - The signal of the caller, for example the load or prefetch signal.
* @returns {{ signal: AbortSignal|undefined, dispose: Function }} The signal to use, and the cleanup that clears the timeout and the external listener.
*/
function createTimedRequestSignal(externalSignal) {
// A host without AbortController cannot combine a timeout with the caller's signal, so it passes that signal through. Cancellation still belongs to the caller in that case.
if (typeof AbortController !== 'function')
return { signal: externalSignal, dispose: () => {} };
const controller = new AbortController();
const abort = () => {
try {
controller.abort();
} catch (error) {
console.debug('Audio player: the manifest request could not be aborted.', error);
}
};
const onExternalAbort = () => abort();
if (externalSignal) {
if (externalSignal.aborted)
abort();
else
// A cancelled load or prefetch must cancel the manifest request too, otherwise a superseded prefetch keeps
// a connection and its timeout alive.
externalSignal.addEventListener('abort', onExternalAbort, { once: true });
}
// A manifest that never answers must not hold a load forever. The deadline is independent of the caller's signal, so a stalled server cannot leave the player loading indefinitely.
const timer = setTimeout(abort, MANIFEST_REQUEST_TIMEOUT_MS);
return {
signal: controller.signal,
dispose: () => {
clearTimeout(timer);
// Remove the listener on both success and failure, so a long lived external signal does not retain this request after it settled.
if (externalSignal)
externalSignal.removeEventListener('abort', onExternalAbort);
}
};
}
/**
* Fetches the playback manifest of a track from the web application.
* @param {Object} options - The request options.
* @param {Object} options.configuration - The endpoints configuration of the player.
* @param {Object} options.descriptor - The play descriptor of the track.
* @param {string} options.capabilities - The comma separated capability list, or an empty string.
* @param {Function} options.transport - The JSON transport, in the shape of the player's fetchJsonAsync. It receives the
* URL, an optional signal, a label and a request options object with redirect: 'manual', so a redirect target is validated before the browser follows it.
* @param {AbortSignal} [options.signal] - An optional signal that cancels the request, used by a cancelled prefetch.
* @returns {Promise