From 6133c486c867fca55e43525ce7a883007a39e895 Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 24 Aug 2026 14:40:39 +0530 Subject: [PATCH] Add JavaScript media type shortcut --- README.md | 6 ++++++ src/index.spec.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 13 +++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index de1845d..8afd548 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,9 @@ Each type in the `types` array can be one of the following: `*/vnd+json` or `application/*+json`. The full mime type will be returned if matched. +The JavaScript extensions `js` and `.js` match `application/javascript`, +`application/x-javascript`, and `text/javascript`. + Some examples to illustrate the inputs and returned value: ```js @@ -98,6 +101,9 @@ Each type in the `types` array can be one of the following: `*/vnd+json` or `application/*+json`. The full mime type will be returned if matched. +The JavaScript extensions `js` and `.js` match `application/javascript`, +`application/x-javascript`, and `text/javascript`. + Some examples to illustrate the inputs and returned value: ```js diff --git a/src/index.spec.ts b/src/index.spec.ts index 889b066..361fcb1 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -166,6 +166,42 @@ describe("request(req, types)", () => { assert.strictEqual(request(req, ["multipart"]), "multipart"); }); }); + + describe("when Content-Type is JavaScript", () => { + it('should match "js"', () => { + assert.strictEqual( + request(createRequest("application/javascript"), ["js"]), + "js", + ); + assert.strictEqual( + request(createRequest("application/x-javascript"), ["js"]), + "js", + ); + assert.strictEqual( + request(createRequest("text/javascript"), ["js"]), + "js", + ); + assert.strictEqual( + request(createRequest("application/json"), ["js"]), + false, + ); + }); + + it('should match ".js"', () => { + assert.strictEqual( + request(createRequest("application/javascript"), [".js"]), + ".js", + ); + assert.strictEqual( + request(createRequest("application/x-javascript"), [".js"]), + ".js", + ); + assert.strictEqual( + request(createRequest("text/javascript"), [".js"]), + ".js", + ); + }); + }); }); describe("hasBody(req)", () => { @@ -340,6 +376,21 @@ describe("is(mediaType, types)", () => { assert.strictEqual(is("multipart/form-data", ["multipart"]), "multipart"); }); }); + + describe("when media type is JavaScript", () => { + it('should match "js"', () => { + assert.strictEqual(is("application/javascript", ["js"]), "js"); + assert.strictEqual(is("application/x-javascript", ["js"]), "js"); + assert.strictEqual(is("text/javascript", ["js"]), "js"); + assert.strictEqual(is("application/json", ["js"]), false); + }); + + it('should match ".js"', () => { + assert.strictEqual(is("application/javascript", [".js"]), ".js"); + assert.strictEqual(is("application/x-javascript", [".js"]), ".js"); + assert.strictEqual(is("text/javascript", [".js"]), ".js"); + }); + }); }); describe("match(expected, actual)", () => { diff --git a/src/index.ts b/src/index.ts index f2fc4fd..b63b2fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,12 @@ import { parse } from "content-type"; import { lookup } from "mime-types"; import { test } from "media-typer"; +const JAVASCRIPT_MEDIA_TYPES = new Set([ + "application/javascript", + "application/x-javascript", + "text/javascript", +]); + /** * Node.js HTTP request shape. */ @@ -58,7 +64,12 @@ export function is( for (const type of types) { const normalized = normalize(type); - if (match(normalized, val)) { + const extension = type.startsWith(".") ? type.slice(1) : type; + + if ( + (extension === "js" && JAVASCRIPT_MEDIA_TYPES.has(val)) || + match(normalized, val) + ) { return type[0] === "+" || type.indexOf("*") !== -1 ? val : type; } }