bilibili/flv.js · error · InvalidArgumentException

MediaDataSource must has type field to indicate video file t

Error message

MediaDataSource must has type field to indicate video file type!

What it means

createPlayer() also requires the mediaDataSource object to have a 'type' field that selects the player implementation (e.g. 'flv'). The object exists but is missing the discriminator, so the factory cannot decide which player to construct and throws InvalidArgumentException. The switch on mds.type (with 'flv' handled) sits directly after this check.

Source

Thrown at src/flv.js:43

import {ErrorTypes, ErrorDetails} from './player/player-errors.js';
import LoggingControl from './utils/logging-control.js';
import {InvalidArgumentException} from './utils/exception.js';

// here are all the interfaces

// install polyfills
Polyfill.install();


// factory method
function createPlayer(mediaDataSource, optionalConfig) {
    let mds = mediaDataSource;
    if (mds == null || typeof mds !== 'object') {
        throw new InvalidArgumentException('MediaDataSource must be an javascript object!');
    }

    if (!mds.hasOwnProperty('type')) {
        throw new InvalidArgumentException('MediaDataSource must has type field to indicate video file type!');
    }

    switch (mds.type) {
        case 'flv':
            return new FlvPlayer(mds, optionalConfig);
        default:
            return new NativePlayer(mds, optionalConfig);
    }
}


// feature detection
function isSupported() {
    return Features.supportMSEH264Playback();
}

function getFeatureList() {
    return Features.getFeatureList();

View on GitHub (pinned to 42343088f2)

Solutions

  1. Add type:'flv' (or the supported type) to the mediaDataSource object
  2. Check the field spelling is exactly lowercase 'type'
  3. Validate the config shape before calling createPlayer

Example fix

// before
createPlayer({isLive: true, url: 'https://example.com/live.flv'}, config); // throws
// after
createPlayer({type: 'flv', isLive: true, url: 'https://example.com/live.flv'}, config);
Defensive patterns

Strategy: validation

Validate before calling

if (typeof mediaDataSource === 'object' && mediaDataSource !== null && !Object.prototype.hasOwnProperty.call(mediaDataSource, 'type')) {
  throw new TypeError('mediaDataSource must include a "type" field (e.g. "flv")');
}

Type guard

function hasPlayerType(v) {
  return typeof v === 'object' && v !== null && 'type' in v && typeof v.type === 'string' && v.type.length > 0;
}

Try / catch

try {
  const player = createPlayer(mediaDataSource, config);
} catch (e) {
  if (e.name === 'InvalidArgumentException') {
    console.error('mediaDataSource missing "type" field:', mediaDataSource);
  } else { throw e; }
}

Prevention

When it happens

Trigger: Passing {url:'...'} or {isLive:true, url:'...'} without type; misspelling the field (e.g. Type or filetype); building the config dynamically and dropping the key; passing a config object from an older schema version.

Common situations: Hand-written config objects missing the discriminator; server-supplied configs that omit type; upgrading the library where a previously defaulted field is now mandatory; destructuring/rebuilding the config and forgetting type.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of bilibili/flv.js@42343088f2 (2026-09-01). Data as JSON: /api/errors/4c933c6a7b751d19. Report an issue: GitHub.