cloudinary-video-player
Version:
Cloudinary Video Player
60 lines (52 loc) • 2.14 kB
JavaScript
function utf8ToBase64(str) {
const utf8Bytes = new TextEncoder().encode(str);
const binaryStr = String.fromCharCode(...utf8Bytes);
return btoa(binaryStr);
}
/**
* Detects http(s) profile/config URLs. Split from `video-source.const` so fetch-config
* does not pull MIME maps and related defaults into the async chunk.
*/
// eslint-disable-next-line no-control-regex
const URL_PATTERN = RegExp('https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)');
const isRawUrl = publicId => URL_PATTERN.test(publicId);
const appendQueryParams = (url, params) => {
if (!params || !Object.keys(params).length) {
return url;
}
const urlObj = new URL(url);
Object.entries(params).forEach(_ref => {
let [key, value] = _ref;
if (value != null) {
urlObj.searchParams.append(key, value);
}
});
return urlObj.href;
};
/**
* Cloudinary SDK config keys (snake_case). Duplicated from `video-player.const` so
* modules that only need this list do not import the full player constants table.
*/
const CLOUDINARY_CONFIG_PARAM = ['api_secret', 'auth_token', 'cdn_subdomain', 'cloud_name', 'cname', 'private_cdn', 'secure', 'secure_cdn_subdomain', 'secure_distribution', 'shorten', 'sign_url', 'url_suffix', 'use_root_path'];
const CLOUDINARY_CONFIG_KEYS = new Set(CLOUDINARY_CONFIG_PARAM);
/**
* Matches lodash `snakeCase` (avoids heavy lodash in lazy-player).
*/
const toSnakeCase = key => key.replace(/([a-z0-9])([A-Z])/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
const pickCloudinaryKeysFromOptions = options => {
const out = {};
for (const key of Object.keys(options)) {
const snake = toSnakeCase(key);
if (CLOUDINARY_CONFIG_KEYS.has(snake)) {
out[snake] = options[key];
}
}
return out;
};
const getCloudinaryConfigFromOptions = options => {
if (options.cloudinaryConfig) {
return options.cloudinaryConfig;
}
return Object.assign({}, pickCloudinaryKeysFromOptions(options));
};
export { CLOUDINARY_CONFIG_PARAM as C, appendQueryParams as a, getCloudinaryConfigFromOptions as g, isRawUrl as i, utf8ToBase64 as u };