buffer-type
Advanced tools
Comparing version 0.0.2 to 1.0.0
1.0.0 / 2018-06-28 | ||
================== | ||
**others** | ||
* [[`e07ae1b`](http://github.com/node-modules/buffer-type/commit/e07ae1bf830afd369e892e8abeb722c75ccbaf80)] - * feat: support Exif - Exchange Image File Format image (fengmk2 <<fengmk2@gmail.com>>) | ||
0.0.2 / 2014-05-04 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -1,18 +0,4 @@ | ||
/**! | ||
* buffer-type - lib/buffer-type.js | ||
* | ||
* Copyright(c) fengmk2 and other contributors. | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com) | ||
*/ | ||
'use strict'; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* @see http://www.onicos.com/staff/iz/formats/gif.html | ||
@@ -41,3 +27,4 @@ * | ||
*/ | ||
var gif = function (buf) { | ||
function gif(buf) { | ||
if (buf.length < 13 || | ||
@@ -50,11 +37,11 @@ // "GIF" 0x47 0x49 0x46 | ||
} | ||
var width = buf.readUInt16LE(6); | ||
var height = buf.readUInt16LE(8); | ||
const width = buf.readUInt16LE(6); | ||
const height = buf.readUInt16LE(8); | ||
return { | ||
type: 'image/gif', | ||
extension: '.gif', | ||
width: width, | ||
height: height | ||
width, | ||
height, | ||
}; | ||
}; | ||
} | ||
@@ -73,3 +60,4 @@ /** | ||
*/ | ||
var png = function (buf) { | ||
function png(buf) { | ||
if (buf.length < 16 || | ||
@@ -86,6 +74,6 @@ buf[0] !== 0x89 || | ||
// 4 bytes 4 bytes Length bytes 4 bytes | ||
var length = buf.readUInt32BE(8); | ||
const length = buf.readUInt32BE(8); | ||
// var chunkType = buf.slice(12, 16).toString(); // should be 'IHDR' | ||
// console.log(length, chunkType, buf.slice(12, 16)) | ||
var chunkData = buf.slice(16, 16 + length); | ||
const chunkData = buf.slice(16, 16 + length); | ||
// Width: 4 bytes 0 | ||
@@ -98,9 +86,9 @@ // Height: 4 bytes 4 | ||
// Interlace method: 1 byte 12 | ||
var width = chunkData.readUInt32BE(0, true); | ||
var height = chunkData.readUInt32BE(4, true); | ||
const width = chunkData.readUInt32BE(0, true); | ||
const height = chunkData.readUInt32BE(4, true); | ||
return { | ||
type: 'image/png', | ||
extension: '.png', | ||
width: width, | ||
height: height, | ||
width, | ||
height, | ||
bit: chunkData.readUInt8(8, true), | ||
@@ -112,3 +100,3 @@ color: chunkData.readUInt8(9, true), | ||
}; | ||
}; | ||
} | ||
@@ -147,3 +135,4 @@ /** | ||
var jpeg = function (buf) { | ||
function jpeg(buf) { | ||
// JFIF - JPEG FILE Interchange Format | ||
if (buf.length < 20 || | ||
@@ -163,5 +152,6 @@ // 0xff 0xd8(SOI) | ||
} | ||
// APP0 | ||
var majorVersion = buf.readUInt8(11, true); | ||
var minorVersion = buf.readUInt8(12, true); | ||
// const majorVersion = buf.readUInt8(11, true); | ||
// const minorVersion = buf.readUInt8(12, true); | ||
// var units = buf.readUInt8(13, true); | ||
@@ -206,7 +196,7 @@ // var width = buf.readUInt16BE(14, true); | ||
// find SOF0 | ||
var offset = 20; | ||
var sof0 = null; | ||
let offset = 20; | ||
let sof0 = null; | ||
while (offset < buf.length) { | ||
var flag = buf.slice(offset, offset + 2); | ||
var size = buf.readUInt16BE(offset + 2); | ||
const flag = buf.slice(offset, offset + 2); | ||
const size = buf.readUInt16BE(offset + 2); | ||
if (flag[0] === 0xff && flag[1] === 0xc0) { | ||
@@ -219,3 +209,3 @@ sof0 = offset; | ||
var r = { | ||
const r = { | ||
type: 'image/jpeg', | ||
@@ -233,4 +223,82 @@ extension: '.jpg', | ||
return r; | ||
}; | ||
} | ||
// Exif - Exchange Image File Format | ||
// 这个标准是 Camera 产业联合会发布的,主要目的就是设计一种文件格式,方便交换照片文件的 metadata | ||
// https://blog.csdn.net/kickxxx/article/details/8173332 | ||
// Exif文件的layout | ||
// | ||
// SOI 0xFF, 0xD8 Start of frame | ||
// APP1 0xFF, 0xE1 Exif Attribute Information | ||
// APP2 0xFF, 0xE2 Flashpix Externsion data | ||
// ... | ||
// APPz 0xFF, 0xEn | ||
// DQT 0xFF, 0xDB | ||
// DHT 0xFF, 0xC4 | ||
// DRI 0xFF, 0xDD | ||
// SOF 0xFF, 0xC0(0xFF, 0xC2) | ||
// SOS 0xFF, 0xDA | ||
// Compressed Data | ||
// EOI 0xFF, 0xD9 | ||
function jpegExif(buf) { | ||
if (buf.length < 4 || | ||
// 0xff 0xd8(SOI) | ||
buf[0] !== 0xff || buf[1] !== 0xd8 || | ||
// 0xff 0xe0(APP1) | ||
buf[2] !== 0xff || buf[3] !== 0xe1) { | ||
return; | ||
} | ||
// APPn 应用程序保留标记 | ||
// 标记代码 marker 2 bytes 固定值0xFFE1 ~ 0xFFEF, n=1~15 | ||
// 数据长度 length 2 bytes APPn的总长度,不包括marker的2bytes | ||
// 详细信息 (length - 2) bytes 内容是应用特定的,比如Exif使用APP1来存放图片的metadata,Adobe Photoshop用APP1和APP13两个标记段分别存储了一副图像的副本。 | ||
// find SOF0 | ||
let offset = 2; | ||
let sof0 = null; | ||
while (offset < buf.length) { | ||
const flag = buf.slice(offset, offset + 2); | ||
const size = buf.readUInt16BE(offset + 2); | ||
if (flag[0] === 0xff && flag[1] === 0xc0) { | ||
sof0 = offset; | ||
break; | ||
} | ||
offset += 2 + size; | ||
} | ||
// SOF0,Start of Frame,帧图像开始 | ||
// u 标记代码 2字节 固定值0xFFC0 | ||
// u 包含9个具体字段: | ||
// ① 数据长度 2字节 ①~⑥六个字段的总长度 | ||
// 即不包括标记代码,但包括本字段 | ||
// ② 精度 1字节 每个数据样本的位数 | ||
// 通常是8位,一般软件都不支持 12位和16位 | ||
// ③ 图像高度 2字节 图像高度(单位:像素),如果不支持 DNL 就必须 >0 | ||
// ④ 图像宽度 2字节 图像宽度(单位:像素),如果不支持 DNL 就必须 >0 | ||
// ⑤ 颜色分量数 1字节 只有3个数值可选 | ||
// 1:灰度图;3:YCrCb或YIQ;4:CMYK | ||
// 而JFIF中使用YCrCb,故这里颜色分量数恒为3 | ||
// ⑥颜色分量信息 颜色分量数×3字节(通常为9字节) | ||
// a) 颜色分量ID 1字节 | ||
// b) 水平/垂直采样因子 1字节 高4位:水平采样因子 | ||
// 低4位:垂直采样因子 | ||
// (曾经看到某资料把这两者调转了) | ||
// c) 量化表 1字节 当前分量使用的量化表的ID | ||
// 本标记段中,字段⑥应该重复出现,有多少个颜色分量(字段⑤),就出现多少次(一般为3次)。 | ||
const r = { | ||
type: 'image/jpeg', | ||
extension: '.jpg', | ||
}; | ||
if (sof0) { | ||
// If found out SOF0, we can detect width and height | ||
offset = sof0 + 2 + 2 + 1; | ||
r.height = buf.readUInt16BE(offset, true); | ||
r.width = buf.readUInt16BE(offset + 2, true); | ||
} | ||
return r; | ||
} | ||
/** | ||
@@ -275,3 +343,3 @@ * @see http://www.onicos.com/staff/iz/formats/bmp.html | ||
var bmp = function (buf) { | ||
function bmp(buf) { | ||
if (buf.length < 36 || | ||
@@ -281,5 +349,6 @@ buf[0] !== 0x42 || buf[1] !== 0x4d) { | ||
} | ||
var headerSize = buf.readUInt32LE(14, true); | ||
var header = buf.slice(18, 18 + headerSize - 4); | ||
var width, height; | ||
const headerSize = buf.readUInt32LE(14, true); | ||
const header = buf.slice(18, 18 + headerSize - 4); | ||
let width, | ||
height; | ||
if (headerSize === 12) { | ||
@@ -295,6 +364,6 @@ width = header.readUInt16LE(0, true); | ||
extension: '.bmp', | ||
width: width, | ||
height: height | ||
width, | ||
height, | ||
}; | ||
}; | ||
} | ||
@@ -331,3 +400,3 @@ // webp format: https://developers.google.com/speed/webp/docs/riff_container | ||
// The file size in the header is the total size of the chunks that follow plus 4 bytes for the 'WEBP' FourCC. | ||
var size = buf.readUInt32LE(4); | ||
const size = buf.readUInt32LE(4); | ||
@@ -337,7 +406,7 @@ return { | ||
extension: '.webp', | ||
size: size + 8 | ||
size: size + 8, | ||
}; | ||
} | ||
var types = [gif, png, jpeg, bmp, webp]; | ||
const types = [ gif, png, jpeg, jpegExif, bmp, webp ]; | ||
@@ -349,4 +418,4 @@ function detect(buf) { | ||
for (var i = 0; i < types.length; i++) { | ||
var r = types[i](buf); | ||
for (let i = 0; i < types.length; i++) { | ||
const r = types[i](buf); | ||
if (r) { | ||
@@ -353,0 +422,0 @@ return r; |
This software is licensed under the MIT License. | ||
Copyright (C) 2013 fengmk2 <fengmk2@gmail.com> | ||
Copyright(c) 2013 fengmk2 <fengmk2@gmail.com> | ||
Copyright(c) 2013 - present node-modules and other contributors. | ||
@@ -5,0 +6,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "buffer-type", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "Detect content-type from Buffer data.", | ||
"main": "index.js", | ||
"main": "lib/buffer-type.js", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"test": "make test-all" | ||
"lint": "eslint lib test", | ||
"test-local": "egg-bin test", | ||
"test": "npm run lint && npm run test-local", | ||
"ci": "npm run lint && egg-bin cov" | ||
}, | ||
"config": { | ||
"blanket": { | ||
"pattern": "buffer-type/lib", | ||
"data-cover-flags": { | ||
"debug": false | ||
} | ||
}, | ||
"travis-cov": { | ||
"threshold": 100 | ||
} | ||
}, | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"should": "*", | ||
"blanket": "*", | ||
"travis-cov": "*", | ||
"coveralls": "*", | ||
"mocha-lcov-reporter": "*", | ||
"mime": "*", | ||
"mocha": "*" | ||
"egg-bin": "^4.7.0", | ||
"egg-ci": "^1.8.0", | ||
"eslint": "^5.0.1", | ||
"eslint-config-egg": "^7.0.0", | ||
"mime": "1" | ||
}, | ||
@@ -35,4 +26,3 @@ "homepage": "https://github.com/node-modules/buffer-type", | ||
"type": "git", | ||
"url": "git://github.com/node-modules/buffer-type.git", | ||
"web": "https://github.com/node-modules/buffer-type" | ||
"url": "git://github.com/node-modules/buffer-type.git" | ||
}, | ||
@@ -42,11 +32,18 @@ "keywords": [ | ||
"image-type", | ||
"content-type", "buffer", "type", | ||
"content-type", | ||
"buffer", | ||
"type", | ||
"mime", | ||
"filetype", "file extension", "buffer type" | ||
"filetype", | ||
"file extension", | ||
"buffer type" | ||
], | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
"node": ">= 8.0.0" | ||
}, | ||
"ci": { | ||
"version": "8, 10" | ||
}, | ||
"author": "fengmk2 <fengmk2@gmail.com>", | ||
"license": "MIT" | ||
} |
buffer-type | ||
======= | ||
[![Build Status](https://secure.travis-ci.org/node-modules/buffer-type.png)](http://travis-ci.org/node-modules/buffer-type) | ||
[![NPM version][npm-image]][npm-url] | ||
[![NPM quality][quality-image]][quality-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![David deps][david-image]][david-url] | ||
[![Known Vulnerabilities][snyk-image]][snyk-url] | ||
[![NPM download][download-image]][download-url] | ||
[![Coverage Status](https://coveralls.io/repos/node-modules/buffer-type/badge.png)](https://coveralls.io/r/node-modules/buffer-type) | ||
[npm-image]: https://img.shields.io/npm/v/buffer-type.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/buffer-type | ||
[quality-image]: http://npm.packagequality.com/shield/buffer-type.svg?style=flat-square | ||
[quality-url]: http://packagequality.com/#?package=buffer-type | ||
[travis-image]: https://img.shields.io/travis/node-modules/buffer-type.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/node-modules/buffer-type | ||
[codecov-image]: https://img.shields.io/codecov/c/github/node-modules/buffer-type.svg?style=flat-square | ||
[codecov-url]: https://codecov.io/gh/node-modules/buffer-type | ||
[david-image]: https://img.shields.io/david/node-modules/buffer-type.svg?style=flat-square | ||
[david-url]: https://david-dm.org/node-modules/buffer-type | ||
[snyk-image]: https://snyk.io/test/npm/buffer-type/badge.svg?style=flat-square | ||
[snyk-url]: https://snyk.io/test/npm/buffer-type | ||
[download-image]: https://img.shields.io/npm/dm/buffer-type.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/buffer-type | ||
[![NPM](https://nodei.co/npm/buffer-type.png?downloads=true&stars=true)](https://nodei.co/npm/buffer-type/) | ||
![logo](https://raw.github.com/node-modules/buffer-type/master/logo.png) | ||
Detect content-type from Buffer data. | ||
@@ -23,6 +38,6 @@ | ||
```js | ||
var bt = require('buffer-type'); | ||
var fs = require('fs'); | ||
const bt = require('buffer-type'); | ||
const fs = require('fs'); | ||
var info = bt(fs.readFileSync(__dirname + '/logo.png')); | ||
const info = bt(fs.readFileSync(__dirname + '/logo.png')); | ||
console.log(info); | ||
@@ -79,39 +94,4 @@ // { | ||
## Authors | ||
```bash | ||
$ git summary | ||
project : buffer-type | ||
repo age : 8 hours | ||
active : 2 days | ||
commits : 5 | ||
files : 17 | ||
authors : | ||
5 fengmk2 100.0% | ||
``` | ||
## License | ||
(The MIT License) | ||
Copyright (c) 2013 fengmk2 <fengmk2@gmail.com> | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
[MIT](LICENSE.txt) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20318
5
372
0
5
96