Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"myhtml",
"configurated",
"mycustom",
"mrmime",
"commitlint",
"nosniff",
"deoptimize",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Default: `undefined`
This property allows a user to register custom mime types or extension mappings.
eg. `mimeTypes: { phtml: 'text/html' }`.

Please see the documentation for [`mime-types`](https://github.com/jshttp/mime-types) for more information.
Please see the documentation for [`mrmime`](https://github.com/lukeed/mrmime) for more information.

### mimeTypeDefault

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"memfs": "^4.43.1",
"mime-types": "^3.0.1",
"mrmime": "^2.0.1",
"on-finished": "^2.4.1",
"range-parser": "^1.2.1"
},
Expand All @@ -55,7 +55,6 @@
"@rstest/core": "0.9.2",
"@types/connect": "^3.4.35",
"@types/express": "^5.0.2",
"@types/mime-types": "^3.0.1",
"@types/node": "^22.3.0",
"@types/on-finished": "^2.3.4",
"@types/range-parser": "^1.2.7",
Expand Down
20 changes: 9 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mime = require("mime-types");
const { mimes } = require("mrmime");

const middleware = require("./middleware");
const getFilenameFromUrl = require("./utils/getFilenameFromUrl");
Expand Down Expand Up @@ -203,12 +203,11 @@ function wdm(compiler, options = {}) {
const { mimeTypes } = options;

if (mimeTypes) {
const { types } = mime;

// mimeTypes from user provided options should take priority
// over existing, known types
// @ts-expect-error
mime.types = { ...types, ...mimeTypes };
// mrmime.lookup closes over the exported dictionary, so overrides
// need to update the object in place.
for (const [extension, type] of Object.entries(mimeTypes)) {
mimes[extension.toLowerCase()] = type;
}
}

/**
Expand Down
34 changes: 31 additions & 3 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require("node:path");

const mime = require("mime-types");

const { lookup, mimes } = require("mrmime");
const onFinishedStream = require("on-finished");

const {
Expand Down Expand Up @@ -34,6 +33,35 @@ const ready = require("./utils/ready");
/** @typedef {import("fs").ReadStream} ReadStream */

const BYTES_RANGE_REGEXP = /^ *bytes/i;
const UTF8_CHARSET_MIME_TYPES = new Set([
"application/javascript",
"application/json",
"application/manifest+json",
]);

mimes.usdz = "model/vnd.usdz+zip";

/**
* @param {string} filename filename or extension
* @returns {string | undefined} content type header value
*/
function getContentType(filename) {
const mimeType = lookup(filename);

if (!mimeType) {
return undefined;
}

if (mimeType.includes(";")) {
return mimeType;
}

if (mimeType.startsWith("text/") || UTF8_CHARSET_MIME_TYPES.has(mimeType)) {
return `${mimeType}; charset=utf-8`;
}

return mimeType;
}

/**
* @param {"bytes"} type type
Expand Down Expand Up @@ -688,7 +716,7 @@ function wrapper(context) {
) {
removeResponseHeader(res, "Content-Type");
// content-type name (like application/javascript; charset=utf-8) or false
const contentType = mime.contentType(path.extname(filename));
const contentType = getContentType(path.extname(filename));

// Only set content-type header if media type is known
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
Expand Down
26 changes: 24 additions & 2 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import finalhandler from "finalhandler";
import { Hono } from "hono";
import koa from "koa";
import memfs, { Volume, createFsFromVolume } from "memfs";
import mime from "mime-types";
import { lookup, mimes } from "mrmime";
import router from "router";
import request from "supertest";
import { Stats } from "@rspack/core";
Expand All @@ -34,6 +34,14 @@ import getCompilerHooks from "./helpers/getCompilerHooks";
// Suppress unnecessary stats output
rs.spyOn(globalThis.console, "log").mockImplementation();

const UTF8_CHARSET_MIME_TYPES = new Set([
"application/javascript",
"application/json",
"application/manifest+json",
]);

mimes.usdz = "model/vnd.usdz+zip";

async function startServer(name, app) {
return new Promise((resolve, reject) => {
if (name === "router") {
Expand Down Expand Up @@ -270,7 +278,21 @@ function get404ContentTypeHeader(name) {
}

function getContentTypeHeader(name, ext = "js") {
return mime.contentType(ext);
const mimeType = lookup(ext);

if (!mimeType) {
return undefined;
}

if (mimeType.includes(";")) {
return mimeType;
}

if (mimeType.startsWith("text/") || UTF8_CHARSET_MIME_TYPES.has(mimeType)) {
return `${mimeType}; charset=utf-8`;
}

return mimeType;
}

function applyTestMiddleware(name, middlewares) {
Expand Down