commit 23d06f1e38dc0f056bcd8bdfa486385a558f7343 Author: kangkang520 Date: Tue Nov 27 22:20:14 2018 +0800 代码完成,初次提交 diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..572406bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules +package-lock.json \ No newline at end of file diff --git a/dist/favicon.js b/dist/favicon.js new file mode 100644 index 00000000..155711c4 --- /dev/null +++ b/dist/favicon.js @@ -0,0 +1,114 @@ +"use strict"; +/** + * favicon的获取顺序为: + * 1、 拉取指定网址,并检测html代码中header->link标签是否存在favicon,如果存在则使用它们。 + * 2、当第一步不满足的时候回进行猜测,一般猜测 favicon.ico、 favicon.png、 favicon.svg、favicon.gif,如果存在并且mime类型是图片则返回。 + * 3、如果上面两步都不满足,则表示未找到favicon + */ +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const url_1 = __importDefault(require("url")); +const request_1 = __importDefault(require("request")); +//处理link标签 +function parseLinkTag(uri, link, types) { + //内容读取 + link = ' ' + link.trim().replace(/(^<\s*link\s*)|(\s*\/?>$)/g, ''); + let match = link.match(/\srel\s*=\s*"([\s\S]+?)"/); + if (!match) + return null; + if (!match[1].trim().split(/\s+/).filter(t => types.indexOf(t) >= 0).length) + return null; + match = link.match(/\shref\s*=\s*"([\s\S]*?)"/); + if (!match) + return null; + let href = match[1]; + match = link.match(/\ssizes\s*=\s*"(\d+)[xX](\d+)"/); + const size = match ? { width: parseInt(match[1]), height: parseInt(match[2]) } : undefined; + //连接处理 + if (!(/https?:\/\//.test(href))) { + let { protocol, host, pathname } = url_1.default.parse(uri); + protocol = protocol || 'https:'; + if (/\/\//.test(href)) + href = protocol + href; + else { + if (!host) + return null; + if (href[0] == '/') + href = protocol + '//' + host + href; + else + href = protocol + '//' + host + (pathname || '').replace(/\/$/, '') + '/' + href; + } + } + return { url: href, size }; +} +//从HTML中获取faviconnpm install favicon +async function getFavIconFromHtml(uri, option) { + return new Promise(resolve => { + request_1.default.get(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body) => { + if (err || !body || !body.match) + resolve([]); + //获取所有link标签 + const match = body.match(//g); + if (!match) + return resolve([]); + const favicons = match.map(link => parseLinkTag(uri, link, option.types)).filter(res => !!res); + resolve(favicons); + }); + }); +} +//猜测favicon +async function guessFavIcon(uri, option) { + //用于测试请求一个地址,请求成功返回地址否则返回空 + const fetch = (uri) => new Promise(resolve => { + request_1.default(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body) => { + if (err || !body || !res.headers['content-type']) + resolve(null); + if (/^image\//.test(res.headers['content-type'])) + resolve(uri); + else + resolve(null); + }); + }); + //猜测某个网址 + const guessUri = async (uri) => { + const urls = await Promise.all(['ico', 'png', 'gif', 'svg'].map(suf => fetch(uri + '/favicon.' + suf))); + return urls.filter(s => !!s).map(s => ({ url: s })); + }; + const { protocol, port, host } = url_1.default.parse(uri); + if (!host) + return []; + //如果没有提供协议,则猜测http和https + if (!protocol) { + const [f1, f2] = await Promise.all([ + guessUri(`https://${host}`), + guessUri(`http://${host}`), + ]); + return [...f1, ...f2]; + } + //否则猜测给定的地址 + else + return guessUri(`${protocol}//${host}`); +} +/** + * get favicon from given url + * @param uri URL + * @param option Optional + */ +async function favicon(uri, option) { + //选项初始化 + option = option || {}; + option.types = option.types || ['icon']; + option.timeout = option.timeout || 5000; + option.headers = { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36', + ...option.headers || {}, + }; + //从网页获取数据 + let icons = await getFavIconFromHtml(uri, option); + if (!icons || !icons.length) + icons = await guessFavIcon(uri, option); + return icons; +} +exports.favicon = favicon; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 00000000..47d29117 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,5 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const favicon_1 = require("./favicon"); +exports.favicon = favicon_1.favicon; +module.exports = { favicon: favicon_1.favicon }; diff --git a/package.json b/package.json new file mode 100644 index 00000000..d2927480 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "favicon-trap", + "version": "1.0.1", + "description": "", + "main": "dist/index.js", + "types": "typing/index.d.ts", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "http://git.iemmp.com/yizhi/favicon-trap.git" + }, + "keywords": [ + "favicon", + "trap", + "fav", + "ico", + "shortcut", + "icon" + ], + "author": "", + "license": "ISC", + "devDependencies": { + "@types/node": "^10.12.10", + "@types/request": "^2.48.1" + }, + "dependencies": { + "request": "^2.88.0" + } +} \ No newline at end of file diff --git a/src/favicon.ts b/src/favicon.ts new file mode 100644 index 00000000..029ca815 --- /dev/null +++ b/src/favicon.ts @@ -0,0 +1,120 @@ +/** + * favicon的获取顺序为: + * 1、 拉取指定网址,并检测html代码中header->link标签是否存在favicon,如果存在则使用它们。 + * 2、当第一步不满足的时候回进行猜测,一般猜测 favicon.ico、 favicon.png、 favicon.svg、favicon.gif,如果存在并且mime类型是图片则返回。 + * 3、如果上面两步都不满足,则表示未找到favicon + */ + +import url from 'url' +import request from 'request' + +/** favicon option */ +export interface FaviconOption { + /** icon types that be allowed, such as: "icon"、"apple-touch-icon", default: ['icon'] */ + types?: Array + /** timeout of request, default: 5000 */ + timeout?: number + /** headers for http(s) request */ + headers?: { [i: string]: any } +} + +/** return value of favicon function */ +export interface FavIcon { + /** url of favicon */ + url: string + /** size of favicon, when: `` */ + size?: { + width: number + height: number + } +} + + +//处理link标签 +function parseLinkTag(uri: string, link: string, types: Array): FavIcon | null { + //内容读取 + link = ' ' + link.trim().replace(/(^<\s*link\s*)|(\s*\/?>$)/g, '') + let match = link.match(/\srel\s*=\s*"([\s\S]+?)"/) + if (!match) return null + if (!match[1].trim().split(/\s+/).filter(t => types!.indexOf(t) >= 0).length) return null + match = link.match(/\shref\s*=\s*"([\s\S]*?)"/) + if (!match) return null + let href = match[1] + match = link.match(/\ssizes\s*=\s*"(\d+)[xX](\d+)"/) + const size = match ? { width: parseInt(match[1]), height: parseInt(match[2]) } : undefined + //连接处理 + if (!(/https?:\/\//.test(href))) { + let { protocol, host, pathname } = url.parse(uri) + protocol = protocol || 'https:' + if (/\/\//.test(href)) href = protocol + href + else { + if (!host) return null + if (href[0] == '/') href = protocol + '//' + host + href + else href = protocol + '//' + host + (pathname || '').replace(/\/$/, '') + '/' + href + } + } + return { url: href, size } +} + +//从HTML中获取faviconnpm install favicon +async function getFavIconFromHtml(uri: string, option: FaviconOption): Promise> { + return new Promise>(resolve => { + request.get(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body: string) => { + if (err || !body || !body.match) resolve([]) + //获取所有link标签 + const match = body.match(//g) + if (!match) return resolve([]) + const favicons = match.map(link => parseLinkTag(uri, link, option.types!)).filter(res => !!res) + resolve(favicons as FavIcon[]) + }) + }) +} + +//猜测favicon +async function guessFavIcon(uri: string, option: FaviconOption): Promise> { + //用于测试请求一个地址,请求成功返回地址否则返回空 + const fetch = (uri: string) => new Promise(resolve => { + request(uri, { timeout: option.timeout, headers: option.headers }, (err, res, body) => { + if (err || !body || !res.headers['content-type']) resolve(null) + if (/^image\//.test(res.headers['content-type']!)) resolve(uri) + else resolve(null) + }) + }) + //猜测某个网址 + const guessUri = async (uri: string): Promise> => { + const urls = await Promise.all(['ico', 'png', 'gif', 'svg'].map(suf => fetch(uri + '/favicon.' + suf))) + return urls.filter(s => !!s).map(s => ({ url: s! })) + } + const { protocol, port, host } = url.parse(uri) + if (!host) return [] + //如果没有提供协议,则猜测http和https + if (!protocol) { + const [f1, f2] = await Promise.all([ + guessUri(`https://${host}`), + guessUri(`http://${host}`), + ]) + return [...f1, ...f2] + } + //否则猜测给定的地址 + else return guessUri(`${protocol}//${host}`) +} + +/** + * get favicon from given url + * @param uri URL + * @param option Optional + */ +export async function favicon(uri: string, option?: FaviconOption) { + //选项初始化 + option = option || {} + option.types = option.types || ['icon'] + option.timeout = option.timeout || 5000 + option.headers = { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36', + ...option.headers || {}, + } + //从网页获取数据 + let icons = await getFavIconFromHtml(uri, option) + if (!icons || !icons.length) icons = await guessFavIcon(uri, option) + return icons +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 00000000..ddfba10f --- /dev/null +++ b/src/index.ts @@ -0,0 +1,4 @@ +import { favicon, FavIcon, FaviconOption } from './favicon' + +export { favicon, FavIcon, FaviconOption } +module.exports = { favicon } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..bc44de65 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,61 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ + "lib": [ + "es2015", + "es2015.promise" + ], /* Specify library files to be included in the compilation. */ + // "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + "declaration": true, /* Generates corresponding '.d.ts' file. */ + "declarationDir": "./typing", /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ + "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + }, + "include": [ + "src/**/*.ts" + ] +} \ No newline at end of file diff --git a/typing/favicon.d.ts b/typing/favicon.d.ts new file mode 100644 index 00000000..2f68d9e0 --- /dev/null +++ b/typing/favicon.d.ts @@ -0,0 +1,33 @@ +/** + * favicon的获取顺序为: + * 1、 拉取指定网址,并检测html代码中header->link标签是否存在favicon,如果存在则使用它们。 + * 2、当第一步不满足的时候回进行猜测,一般猜测 favicon.ico、 favicon.png、 favicon.svg、favicon.gif,如果存在并且mime类型是图片则返回。 + * 3、如果上面两步都不满足,则表示未找到favicon + */ +/** favicon option */ +export interface FaviconOption { + /** icon types that be allowed, such as: "icon"、"apple-touch-icon", default: ['icon'] */ + types?: Array; + /** timeout of request, default: 5000 */ + timeout?: number; + /** headers for http(s) request */ + headers?: { + [i: string]: any; + }; +} +/** return value of favicon function */ +export interface FavIcon { + /** url of favicon */ + url: string; + /** size of favicon, when: `` */ + size?: { + width: number; + height: number; + }; +} +/** + * get favicon from given url + * @param uri URL + * @param option Optional + */ +export declare function favicon(uri: string, option?: FaviconOption): Promise; diff --git a/typing/index.d.ts b/typing/index.d.ts new file mode 100644 index 00000000..60d9b6ab --- /dev/null +++ b/typing/index.d.ts @@ -0,0 +1,2 @@ +import { favicon, FavIcon, FaviconOption } from './favicon'; +export { favicon, FavIcon, FaviconOption };