src/vs/base/common/mime.ts
136 LOC · 126 covered · 10 uncovered · 9 ranges · 5282 concepts · 5 introducers · 2651 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
mime.ts ×5
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { extname } from './path.js';
export const Mimes = Object.freeze({
text: 'text/plain',
binary: 'application/octet-stream',
unknown: 'application/unknown',
markdown: 'text/markdown',
latex: 'text/latex',
uriList: 'text/uri-list',
html: 'text/html',
});
interface MapExtToMediaMimes {
[index: string]: string | string[];
}
const mapExtToTextMimes: Record<string, string> = {
'.css': 'text/css',
'.csv': 'text/csv',
'.htm': 'text/html',
'.html': 'text/html',
'.ics': 'text/calendar',
'.js': 'text/javascript',
'.mjs': 'text/javascript',
'.txt': 'text/plain',
'.xml': 'text/xml'
};
// Known media mimes that we can handle
const mapExtToMediaMimes: MapExtToMediaMimes = {
'.aac': 'audio/x-aac',
'.avi': 'video/x-msvideo',
'.bmp': 'image/bmp',
'.flv': 'video/x-flv',
'.gif': 'image/gif',
'.ico': 'image/x-icon',
'.jpe': ['image/jpg', 'image/jpeg'],
'.jpeg': ['image/jpg', 'image/jpeg'],
'.jpg': ['image/jpg', 'image/jpeg'],
'.m1v': 'video/mpeg',
'.m2a': 'audio/mpeg',
'.m2v': 'video/mpeg',
'.m3a': 'audio/mpeg',
'.mid': 'audio/midi',
'.midi': 'audio/midi',
'.mk3d': 'video/x-matroska',
'.mks': 'video/x-matroska',
'.mkv': 'video/x-matroska',
'.mov': 'video/quicktime',
'.movie': 'video/x-sgi-movie',
'.mp2': 'audio/mpeg',
'.mp2a': 'audio/mpeg',
'.mp3': 'audio/mpeg',
'.mp4': 'video/mp4',
'.mp4a': 'audio/mp4',
'.mp4v': 'video/mp4',
'.mpe': 'video/mpeg',
'.mpeg': 'video/mpeg',
'.mpg': 'video/mpeg',
'.mpg4': 'video/mp4',
'.mpga': 'audio/mpeg',
'.oga': 'audio/ogg',
'.ogg': 'audio/ogg',
'.opus': 'audio/opus',
'.ogv': 'video/ogg',
'.png': 'image/png',
'.psd': 'image/vnd.adobe.photoshop',
'.qt': 'video/quicktime',
'.spx': 'audio/ogg',
'.svg': 'image/svg+xml',
'.tga': 'image/x-tga',
'.tif': 'image/tiff',
'.tiff': 'image/tiff',
'.wav': 'audio/x-wav',
'.webm': 'video/webm',
'.webp': 'image/webp',
'.wma': 'audio/x-ms-wma',
'.wmv': 'video/x-ms-wmv',
'.woff': 'application/font-woff',
};
export function getMediaOrTextMime(path: string): string | undefined {
const ext = extname(path);
const textMime = mapExtToTextMimes[ext.toLowerCase()];
if (textMime !== undefined) {
return textMime;
} else {
return getMediaMime(path);
}
}
export function getMediaMime(path: string): string | undefined {
const mimeType = mapExtToMediaMimes[ext.toLowerCase()];
return Array.isArray(mimeType) ? mimeType[0] : mimeType;
}
export function getExtensionForMimeType(mimeType: string): string | undefined {
const value = mapExtToMediaMimes[extension];
if (Array.isArray(value) ? value.includes(mimeType) : value === mimeType) {
return extension;
}
}
return undefined;
}
const _simplePattern = /^(.+)\/(.+?)(;.+)?$/;
export function normalizeMimeType(mimeType: string): string;
export function normalizeMimeType(mimeType: string, strict: true): string | undefined;
export function normalizeMimeType(mimeType: string, strict?: true): string | undefined {
const match = _simplePattern.exec(mimeType);
if (!match) {
return strict
? undefined
: mimeType;
}
// https://datatracker.ietf.org/doc/html/rfc2045#section-5.1
// media and subtype must ALWAYS be lowercase, parameter not
return `${match[1].toLowerCase()}/${match[2].toLowerCase()}${match[3] ?? ''}`;
}
/**
* Whether the provided mime type is a text stream like `stdout`, `stderr`.
*/
export function isTextStreamMime(mimeType: string) {
return ['application/vnd.code.notebook.stdout', 'application/vnd.code.notebook.stderr'].includes(mimeType);
}