Bairdy
Bairdy16mo ago

Parsing Apache mime.types into dictionary Record<string, string>

This code parses the official Apache mime.types from GitHub and creates a dictionary Record<string, string> of all lines that have not been commented out. Perhaps it may help someone learning and using Deno serve so I wanted to share.
/**
* Run command:
* deno run --allow-net --check apache_getmime.ts
*
* Output example:
*
* const MIME_TYPES: Record<string, string> = {
* ".ext1": "type/subtype",
* ".ext2": "type/subtype",
* ".ext3": "type/subtype",
* };
*
*/
// Declare an empty dictionary object called MIME_TYPES
const MIME_TYPES: Record<string, string> = {};

// Define an async function to fetch and parse MIME types from a remote server
async function parseApacheMimeTypes(): Promise<void> {
// Fetch the MIME types file from the remote server
const response = await fetch("https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types");
// Extract the text content of the MIME types file
const text = await response.text();
// Split the text content into an array of individual lines
const lines = text.split("\n");
// Loop through each line in the array
for (const line of lines) {
// Skip lines that are commented out
if (line.startsWith("#")) {
continue;
}
// Extract the media type and extensions from the active line
const [mediaType, ...extensions] = line.trim().split(/\s+/);
// Loop through each extension and add it to the MIME_TYPES dictionary with the corresponding media type
for (const extension of extensions) {
MIME_TYPES["." + extension] = mediaType;
}
}
// Output the completed MIME_TYPES dictionary to the console
console.info(MIME_TYPES);
}

// Call the async function to retrieve and parse the MIME types
await parseApacheMimeTypes();
/**
* Run command:
* deno run --allow-net --check apache_getmime.ts
*
* Output example:
*
* const MIME_TYPES: Record<string, string> = {
* ".ext1": "type/subtype",
* ".ext2": "type/subtype",
* ".ext3": "type/subtype",
* };
*
*/
// Declare an empty dictionary object called MIME_TYPES
const MIME_TYPES: Record<string, string> = {};

// Define an async function to fetch and parse MIME types from a remote server
async function parseApacheMimeTypes(): Promise<void> {
// Fetch the MIME types file from the remote server
const response = await fetch("https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types");
// Extract the text content of the MIME types file
const text = await response.text();
// Split the text content into an array of individual lines
const lines = text.split("\n");
// Loop through each line in the array
for (const line of lines) {
// Skip lines that are commented out
if (line.startsWith("#")) {
continue;
}
// Extract the media type and extensions from the active line
const [mediaType, ...extensions] = line.trim().split(/\s+/);
// Loop through each extension and add it to the MIME_TYPES dictionary with the corresponding media type
for (const extension of extensions) {
MIME_TYPES["." + extension] = mediaType;
}
}
// Output the completed MIME_TYPES dictionary to the console
console.info(MIME_TYPES);
}

// Call the async function to retrieve and parse the MIME types
await parseApacheMimeTypes();
0 Replies
No replies yetBe the first to reply to this messageJoin