创建npm账号
创建npm包
npm init
package name: (validate-image)
version: (1.0.0)
description: validate real image
entry point: (index.js)
test command:
git repository:
keywords: image validate
author: kris_zhang
license: (ISC)
About to write to /Users/zhangjiewen/Desktop/projects/study/npm/validate-image/package.json:
{
"name": "validate-image",
"version": "1.0.0",
"description": "validate real image",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"image",
"validate"
],
"author": "kris_zhang",
"license": "ISC"
}
Npm config package.json
图片格式
读取文件Hex内容
vscode插件:Hex Editor
hexInfo.json
{
"jpg": "FF D8 FF",
"png": "89 50 4E 47 0D 0A 1A 0A",
"gif": ["47 49 46 38 39 61", "47 49 46 38 37 61"],
"webp": "52 49 46 46",
"ico": "00 00 01 00",
"bmp": "42 4D"
}
index.js
import hexInfo from './util/hexInfo.json'
const getHexNumber = (file) => {
return new Promise((file, callback) => {
const reader = new FileReader();
reader.onloadend = function (e) {
const arr = new Uint8Array(e.target.result).subarray(0, 4);
let hexNumber = "";
for (let i = 0; i < arr.length; i++) {
hexNumber += arr[i].toString(16).padStart(2, "0").toUpperCase();
}
callback(hexNumber);
};
reader.readAsArrayBuffer(file.slice(0, 4));
});
};
const validateImage = (file, imageType) => {
return hexInfo[imageType].includes(getHexNumber(file))
}
export default {
getHexNumber,
validateImage
}
发布npm包
zhangjiewen@zhangjiewendeMacBook-Pro validate-image % npm config set registry https://registry.npmjs.org/
zhangjiewen@zhangjiewendeMacBook-Pro validate-image % npm login
npm WARN adduser `adduser` will be split into `login` and `register` in a future version. `adduser` will become an alias of `register`. `login` (currently an alias) will become its own command.
npm notice Log in on https://registry.npmjs.org/
Username: zhangjiewen
Password:
Email: (this IS public) 474350393@qq.com
npm notice Please check your email for a one-time password (OTP)
Enter one-time password: 91832573
Logged in as zhangjiewen on https://registry.npmjs.org/.
zhangjiewen@zhangjiewendeMacBook-Pro validate-image % npm publish
npm notice
npm notice 📦 validate-image@1.0.0
npm notice === Tarball Contents ===
npm notice 662B index.js
npm notice 288B package.json
npm notice 191B util/hexInfo.json
npm notice === Tarball Details ===
npm notice name: validate-image
npm notice version: 1.0.0
npm notice filename: validate-image-1.0.0.tgz
npm notice package size: 765 B
npm notice unpacked size: 1.1 kB
npm notice shasum: edf0e0b5ddd3e9e7e0eeabf319dcab33cdf96578
npm notice integrity: sha512-Pyx6NjgFXhu4x[...]FRhqBma86GWyA==
npm notice total files: 3
npm notice
npm notice Publishing to https://registry.npmjs.org/
+ validate-image@1.0.0