Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)", () => {
Expand Down Expand Up @@ -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)", () => {
Expand Down
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
}
Expand Down