Skip to content
Permalink
Browse files

Breaking: Move `content-type` util from `hint` to `@hint/utils`

Also update `content-type` to reference trimmed `mime-db`.
  • Loading branch information...
antross committed Apr 11, 2019
1 parent 549bd32 commit a01fbca4f09b6de97edc698839d64910cb40f76a
@@ -31,8 +31,8 @@ import { fork, ChildProcess } from 'child_process';

import { JSDOM, ResourceLoader, VirtualConsole } from 'jsdom';

import { debug as d, dom, HTMLElement, HTMLDocument, HttpHeaders, network } from '@hint/utils';
import { Engine, Event, FetchEnd, FetchError, IConnector, NetworkData, utils } from 'hint';
import { contentType, debug as d, dom, HTMLElement, HTMLDocument, HttpHeaders, network } from '@hint/utils';
import { Engine, Event, FetchEnd, FetchError, IConnector, NetworkData } from 'hint';
import { Requester } from '@hint/utils-connector-tools';

import CustomResourceLoader from './resource-loader';
@@ -45,7 +45,7 @@ import { beforeParse } from './before-parse';
*/

const { createHTMLDocument, traverse } = dom;
const { contentType: { getContentTypeData, getType } } = utils;
const { getContentTypeData, getType } = contentType;
const { isHTMLDocument } = network;
const debug: debug.IDebugger = d(__filename);

@@ -1,15 +1,14 @@
import { URL } from 'url';

import { debug as d } from '@hint/utils';
import { contentType, dom, debug as d, HTMLDocument } from '@hint/utils';
import { ResourceLoader } from 'jsdom';

import { HTMLDocument, dom } from '@hint/utils';
import { FetchEnd, FetchError, NetworkData, utils } from 'hint';
import { FetchEnd, FetchError, NetworkData } from 'hint';

import JSDOMConnector from './connector';

const { createHTMLDocument, getElementByUrl } = dom;
const { contentType: { getContentTypeData, getType } } = utils;
const { getContentTypeData, getType } = contentType;

const debug: debug.IDebugger = d(__filename);

@@ -44,7 +44,8 @@ import * as chokidar from 'chokidar';
import globby from 'globby';
import { JSDOM } from 'jsdom';

import { dom, fs, HTMLDocument, HTMLElement, logger, network } from '@hint/utils';
import { contentType, dom, fs, HTMLDocument, HTMLElement, logger, network } from '@hint/utils';

import {
CanEvaluateScript,
Engine,
@@ -53,8 +54,7 @@ import {
IConnector,
IFetchOptions,
NetworkData,
ScanEnd,
utils
ScanEnd
} from 'hint';
import { HTMLParse, HTMLEvents } from '@hint/parser-html';

@@ -65,7 +65,7 @@ import { HTMLParse, HTMLEvents } from '@hint/parser-html';
*/

const { traverse } = dom;
const { contentType: { getContentTypeData, getType, isTextMediaType } } = utils;
const { getContentTypeData, getType, isTextMediaType } = contentType;
const { cwd, isFile, readFileAsync } = fs;
const { asPathString, getAsUri } = network;
const defaultOptions = {};
@@ -2,11 +2,8 @@ import * as url from 'url';

import { Engine } from 'hint';
import { HttpHeaders } from '@hint/utils/dist/src/types/http-header';
import { HTMLDocument, HTMLElement } from '@hint/utils/dist/src/dom/html';
import { createHTMLDocument } from '@hint/utils/dist/src/dom/create-html-document';
import { traverse } from '@hint/utils/dist/src/dom/traverse';
import { getElementByUrl } from '@hint/utils/dist/src/dom/get-element-by-url';
import { getContentTypeData, getType } from 'hint/dist/src/lib/utils/content-type';
import { createHTMLDocument, getElementByUrl, HTMLDocument, HTMLElement, traverse } from '@hint/utils/dist/src/dom';
import { getContentTypeData, getType } from '@hint/utils/dist/src/content-type';
import {
ConnectorOptionsConfig,
IConnector,
@@ -16,7 +16,7 @@ import { normalizeString } from '@hint/utils/dist/src/misc/normalize-string';
import { isDataURI } from '@hint/utils/dist/src/network/is-data-uri';
import { normalizeHeaderValue } from '@hint/utils/dist/src/network/normalize-header-value';
import { IHint, FetchEnd } from 'hint/dist/src/lib/types';
import { isTextMediaType } from 'hint/dist/src/lib/utils/content-type';
import { isTextMediaType } from '@hint/utils/dist/src/content-type';
import { HintContext } from 'hint/dist/src/lib/hint-context';

import meta from './meta';
@@ -11,14 +11,14 @@ import { promisify } from 'util';

import * as brotli from 'iltorb';

import { asyncTry, HTMLElement, HttpHeaders, misc, network } from '@hint/utils';
import { FetchEnd, HintContext, IHint, NetworkData, Response, utils } from 'hint';
import { asyncTry, contentType, HTMLElement, HttpHeaders, misc, network } from '@hint/utils';
import { FetchEnd, HintContext, IHint, NetworkData, Response } from 'hint';

import { CompressionCheckOptions } from './types';

import meta from './meta';

const { contentType: { getFileExtension, isTextMediaType } } = utils;
const { getFileExtension, isTextMediaType } = contentType;
const { isHTTP, isRegularProtocol, normalizeHeaderValue } = network;
const { normalizeString } = misc;
const decompressBrotli = promisify(brotli.decompress) as (buffer: Buffer) => Promise<Buffer>;
@@ -1,12 +1,12 @@
/**
* @fileoverview Warns against having the BOM character at the beginning of a text file
*/
import { FetchEnd, HintContext, IHint, NetworkData, utils } from 'hint';
import { asyncTry, debug as d, network } from '@hint/utils';
import { FetchEnd, HintContext, IHint, NetworkData } from 'hint';
import { asyncTry, contentType, debug as d, network } from '@hint/utils';

import meta from './meta';

const { contentType: { isTextMediaType } } = utils;
const { isTextMediaType } = contentType;
const { isRegularProtocol } = network;
const debug: debug.IDebugger = d(__filename);

@@ -25,7 +25,6 @@
"is-svg": "^4.1.0",
"jsonc-parser": "^2.1.0",
"lodash": "^4.17.11",
"mime-db": "1.35.0",
"optionator": "^0.8.2",
"ora": "^3.4.0",
"semver": "^6.0.0",
@@ -36,7 +35,6 @@
"@types/async": "^2.4.1",
"@types/content-type": "^1.1.3",
"@types/lodash": "^4.14.123",
"@types/mime-db": "^1.27.0",
"@types/node": "^11.13.4",
"@types/semver": "^6.0.0",
"@types/sinon": "^7.0.11",
@@ -1,13 +1,11 @@
import * as packagesUtils from './packages';
import * as contentTypeUtils from './content-type';
import * as resourceLoaderUtils from './resource-loader';
import * as schemaValidatorUtils from './schema-validator';
import * as jsonParserUtils from './json-parser';

export * from './resource-loader';

export const packages = packagesUtils;
export const contentType = contentTypeUtils;
export const resourceLoader = resourceLoaderUtils;
export const schemaValidator = schemaValidatorUtils;
export const jsonParser = jsonParserUtils;

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -2,9 +2,9 @@ import { Options, parse, tokenizer } from 'acorn';
import * as acornWalk from 'acorn-walk';
import * as ESTree from 'estree';

import { HTMLElement } from '@hint/utils/dist/src/dom/html';
import { HTMLElement } from '@hint/utils/dist/src/dom';
import * as logger from '@hint/utils/dist/src/logging';
import { determineMediaTypeForScript } from 'hint/dist/src/lib/utils/content-type';
import { determineMediaTypeForScript } from '@hint/utils/dist/src/content-type';
import { ElementFound, FetchEnd, Parser as WebhintParser } from 'hint/dist/src/lib/types';
import { Engine } from 'hint/dist/src/lib/engine';

@@ -16,14 +16,14 @@ import * as request from 'request';
import * as iconv from 'iconv-lite';
import parseDataURL = require('data-urls'); // Using `require` as `data-urls` exports a function.

import { debug as d, HttpHeaders, misc, network } from '@hint/utils';
import { contentType, debug as d, HttpHeaders, misc, network } from '@hint/utils';

import { NetworkData, utils } from 'hint';
import { NetworkData } from 'hint';
import { RedirectManager } from './redirects';

interface IDecompressor { (content: Buffer): Promise<Buffer> }

const { contentType: { getContentTypeData } } = utils;
const { getContentTypeData } = contentType;
const { normalizeHeaderValue } = network;
const { toLowerCaseKeys } = misc;
const debug = d(__filename);
@@ -22,18 +22,19 @@ import filter = require('lodash/filter');

import { Crdp } from 'chrome-remote-debug-protocol';

import { debug as d, dom, HTMLDocument, HTMLElement, HttpHeaders, misc, network } from '@hint/utils';
import { contentType, debug as d, dom, HTMLDocument, HTMLElement, HttpHeaders, misc, network } from '@hint/utils';

import {
BrowserInfo, IConnector, Engine,
Event, FetchEnd, FetchError, ILauncher,
Response, Request, NetworkData, utils
Response, Request, NetworkData
} from 'hint';
import { normalizeHeaders, Requester } from '@hint/utils-connector-tools';

import { RequestResponse } from './request-response';

const { createHTMLDocument, getElementByUrl, traverse } = dom;
const { contentType: { getType } } = utils;
const { getType } = contentType;
const { cutString, delay } = misc;
const { isHTMLDocument } = network;
const debug: debug.IDebugger = d(__filename);
@@ -1,13 +1,14 @@
import { atob } from 'abab';
import { Crdp } from 'chrome-remote-debug-protocol';

import { debug as d, HTMLElement, HttpHeaders } from '@hint/utils';
import { Response, utils } from 'hint';
import { contentType, debug as d, HTMLElement, HttpHeaders } from '@hint/utils';
import { Response } from 'hint';

import { normalizeHeaders, Requester } from '@hint/utils-connector-tools';

const debug: debug.IDebugger = d(__filename);

const { contentType: { getContentTypeData } } = utils;
const { getContentTypeData } = contentType;

export enum RequestStatus {
willBeSent = 'willBeSent',
@@ -38,6 +38,24 @@ and exception.
* `get`: Get the value from the config store.
* `set`: Set a value in the config store.

### contentType

* `determineMediaTypeBasedOnFileExtension`: Get a mime-type associated
with the specified file extension.
* `determineMediaTypeBasedOnFileName`: Get a mime-type associated with
the specified filename.
* `determineMediaTypeForScript`: Check if the provided mime-type is a
recognized mime-type for JavaScript, returning the recommended
mime-type if so, `null` otherwise.
* `getContentTypeData`: Try to determine the correct mime-type for a
response.
* `getFileExtension`: Try to determine a resource's file extension.
* `getType`: Returns the group to which the mediaType belongs to. E.g.:
`image`, `font`, `script`, `css`, `html`, `manifest`, `xml` or
`unknown`.
* `isTextMediaType`: Check if a mime-type represents a text-based
resource.

### dom

* `createHTMLDocument`: Create an HTMLDocument object from an string.
@@ -26,6 +26,7 @@
"@types/configstore": "^4.0.0",
"@types/debug": "^4.1.4",
"@types/file-url": "^2.0.0",
"@types/mime-db": "^1.27.0",
"@types/parse5": "^5.0.0",
"@types/parse5-htmlparser2-tree-adapter": "^5.0.1",
"@types/request": "^2.48.1",
@@ -39,7 +40,7 @@
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-markdown": "^1.0.0",
"eventemitter2": "^5.0.1",
"mime-db": "^1.39.0",
"mime-db": "1.35.0",
"npm-run-all": "^4.1.5",
"nyc": "^13.3.0",
"proxyquire": "2.0.0",
@@ -1,13 +1,14 @@
import fileType from 'file-type';
import isSvg from 'is-svg';
import * as mimeDB from 'mime-db';

import { parse, MediaType } from 'content-type';

import { HttpHeaders } from '@hint/utils/dist/src/types/http-header';
import { debug as d } from '@hint/utils/dist/src/debug';
import { fileExtension as getFileExtension, fileName as getFileName } from '@hint/utils/dist/src/fs';
import { normalizeString } from '@hint/utils/dist/src/misc/normalize-string';
import { HTMLElement } from '@hint/utils/dist/src/dom/html';
import { HTMLElement } from './dom';
import { debug as d } from './debug';
import mimeDB from './mime-db';
import { HttpHeaders } from './types/http-header';
import { fileExtension as getFileExtension, fileName as getFileName } from './fs';
import { normalizeString } from './misc/normalize-string';

const debug = d(__filename);

@@ -18,7 +19,7 @@ const debug = d(__filename);
*/
const getMediaTypeBasedOnFileExtension = (fileExtension: string): string | null => {
return fileExtension && Object.keys(mimeDB).find((key) => {
return (mimeDB as any)[key].extensions && (mimeDB as any)[key].extensions.includes(fileExtension);
return !!mimeDB[key].extensions && mimeDB[key].extensions!.includes(fileExtension);
}) || null; // if nothing is found, we return null to be consistent
};

@@ -2,6 +2,7 @@ import * as appInsightsUtils from './app-insights';
import { asyncTry } from './async-wrapper';
import * as caniuseUtils from './caniuse';
import * as configStoreUtils from './config-store';
import * as contentTypeUtils from './content-type';
import { debug } from './debug';
import * as fsUtils from './fs';
import * as loggingUtils from './logging';
@@ -27,6 +28,7 @@ export const npm = npmUtils;
export const appInsights = appInsightsUtils;
export const caniuse = caniuseUtils;
export const configStore = configStoreUtils;
export const contentType = contentTypeUtils;
export const test = testUtils;
export const dom = domUtils;

@@ -1,6 +1,6 @@
import test from 'ava';

import * as contentType from '../../../src/lib/utils/content-type';
import * as contentType from '../src/content-type';
import * as fs from 'fs';

test('determineMediaTypeBasedOnFileExtension determines the right mime type based on the extension', (t) => {

0 comments on commit a01fbca

Please sign in to comment.
You can’t perform that action at this time.