DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Originally published at thinkthroo.com

Mime package in Nitro codebase.

In this article, we review how Mime package is used in Nitro codebase. We will look at:

  1. What is Mime package?

  2. mime.getType in Nitro codebase.

I study patterns used in an open source project found on Github Trending. For this week, I reviewed some parts of Nitro codebase and wrote this article.

What is Mime package?

Mime provides an API for MIME type information.

Installation

npm install mime
Enter fullscreen mode Exit fullscreen mode

Quick start

import mime from 'mime';

mime.getType('txt');                    // ⇨ 'text/plain'
mime.getExtension('text/plain');        // ⇨ 'txt'
Enter fullscreen mode Exit fullscreen mode

For a comprehensive list of API, checkout Mime documentation.

mime.getType in Nitro codebase.

In nitro/src/prerender/prerender.ts, you will find the following import:

import mime from "mime";
Enter fullscreen mode Exit fullscreen mode

and this is used as shown below at L287:

// Allow overriding content-type in `prerender:generate` hook
const inferredContentType = mime.getType(_route.fileName) || "text/plain";

Enter fullscreen mode Exit fullscreen mode

Here mime.getType is used. In the documentation, I found this below information:

mime.getType('js');             // ⇨ 'text/javascript'
mime.getType('json');           // ⇨ 'application/json'

mime.getType('txt');            // ⇨ 'text/plain'
mime.getType('dir/text.txt');   // ⇨ 'text/plain'
mime.getType('dir\\text.txt');  // ⇨ 'text/plain'
mime.getType('.text.txt');      // ⇨ 'text/plain'
mime.getType('.txt');           // ⇨ 'text/plain'
Enter fullscreen mode Exit fullscreen mode

About me:

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

Study the patterns used in large OSS projects at Think Throo.

References:

  1. https://github.com/nitrojs/nitro/blob/main/src/prerender/prerender.ts#L287

  2. https://github.com/nitrojs/nitro/blob/main/src/prerender/prerender.ts#L4

  3. https://www.npmjs.com/package/mime

Top comments (0)