From ed1cc660b3b36bcfe9de9c3d1394b7b8e1d4ed75 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Thu, 25 Apr 2019 22:48:31 -0400 Subject: [PATCH] Use content-type and media-typer for type validation closes #39 --- HISTORY.md | 3 ++- index.js | 13 +++++++------ package.json | 1 + test/test.js | 4 ++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 4631e06..51e28e1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,7 +2,8 @@ unreleased ========== * Drop node <18 - * deps: replace media-typer with content-type + * Use `content-type` and `media-typer` for type validation + - No behavior changes, upgrades `media-typer` * deps: mime-types@^3.0.0 - Add `application/toml` with extension `.toml` - Add `application/ubjson` with extension `.ubj` diff --git a/index.js b/index.js index 792ea88..a5f1109 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,9 @@ * @private */ -var typer = require('content-type') +var contentType = require('content-type') var mime = require('mime-types') +var typer = require('media-typer') /** * Module exports. @@ -237,13 +238,13 @@ function mimeMatch (expected, actual) { function normalizeType (value) { // parse the type - var type = typer.parse(value) + var type = contentType.parse(value).type - // remove the parameters - type.parameters = undefined + if (!typer.test(type)) { + return null + } - // reformat it - return typer.format(type) + return type } /** diff --git a/package.json b/package.json index 67d27cf..fe9d654 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "repository": "jshttp/type-is", "dependencies": { "content-type": "^1.0.5", + "media-typer": "~1.1.0", "mime-types": "^3.0.0" }, "devDependencies": { diff --git a/test/test.js b/test/test.js index 999a32b..4443196 100644 --- a/test/test.js +++ b/test/test.js @@ -18,7 +18,7 @@ describe('typeis(req, types)', function () { assert.strictEqual(typeis(req, ['text/*']), 'text/html') }) - it.skip('should fail invalid type', function () { + it('should fail invalid type', function () { var req = createRequest('text/html**') assert.strictEqual(typeis(req, ['text/*']), false) }) @@ -186,7 +186,7 @@ describe('typeis.is(mediaType, types)', function () { assert.strictEqual(typeis.is('text/HTML', ['text/*']), 'text/html') }) - it.skip('should fail invalid type', function () { + it('should fail invalid type', function () { assert.strictEqual(typeis.is('text/html**', ['text/*']), false) })