Commit d604203b authored by rockyl's avatar rockyl

rewrite by ts

parent c27e2db2
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var FormData = _interopDefault(require('form-data'));
var fs = _interopDefault(require('fs-extra'));
"use strict";
/**
* Created by rockyl on 2020-02-13.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.compressFile = exports.compressBuffer = void 0;
const form_data_1 = require("form-data");
const fs_extra_1 = require("fs-extra");
const is_png_1 = require("is-png");
const is_jpg_1 = require("is-jpg");
const tinifyUrl = 'http://tinify.duiba.com.cn/tinify';
async function compressBuffer(buffer) {
return new Promise((resolve, reject) => {
if (buffer && buffer.length > 0) {
let form = new FormData();
form.append('file', buffer, {
filename: 'image.png',
contentType: 'image/png',
});
form.submit(tinifyUrl, function (err, res) {
if (err) {
reject(err);
} else {
res.resume();
let resBuffer = Buffer.alloc(0);
res.on('data', (d) => {
resBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);
});
res.on('end', () => {
if (resBuffer.length > 256) {
resolve(resBuffer);
} else {
let str = resBuffer.toString();
let json = JSON.parse(str);
reject(json.msg);
}
});
res.on('abort', () => {
reject('abort');
});
}
});
} else {
reject('empty buffer');
}
})
return new Promise((resolve, reject) => {
if (buffer && buffer.length > 0) {
let extname = is_png_1.default(buffer) ? 'png' : 'jpg';
let form = new form_data_1.default();
form.append('file', buffer, {
filename: 'image.' + extname,
contentType: 'image/' + extname,
});
form.submit(tinifyUrl, function (err, res) {
if (err) {
reject(err);
}
else {
res.resume();
let resBuffer = Buffer.alloc(0);
res.on('data', (d) => {
resBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);
});
res.on('end', () => {
if (is_png_1.default(resBuffer) || is_jpg_1.default(resBuffer)) {
resolve(resBuffer);
}
else {
let str = resBuffer.toString();
let json = JSON.parse(str);
reject(json.msg);
}
});
res.on('abort', () => {
reject('abort');
});
}
});
}
else {
reject('empty buffer');
}
});
}
exports.compressBuffer = compressBuffer;
async function compressFile(file) {
let buffer = await fs.readFile(file);
return compressBuffer(buffer);
let buffer = await fs_extra_1.default.readFile(file);
return compressBuffer(buffer);
}
exports.compressBuffer = compressBuffer;
exports.compressFile = compressFile;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map
\ No newline at end of file
{"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport FormData from 'form-data';\nimport fs from 'fs-extra';\n\nconst tinifyUrl = 'http://tinify.duiba.com.cn/tinify';\n\nexport async function compressBuffer(buffer) {\n\treturn new Promise((resolve, reject) => {\n\t\tif (buffer && buffer.length > 0) {\n\t\t\tlet form = new FormData();\n\t\t\tform.append('file', buffer, {\n\t\t\t\tfilename: 'image.png',\n\t\t\t\tcontentType: 'image/png',\n\t\t\t});\n\n\t\t\tform.submit(tinifyUrl, function (err, res) {\n\t\t\t\tif (err) {\n\t\t\t\t\treject(err);\n\t\t\t\t} else {\n\t\t\t\t\tres.resume();\n\t\t\t\t\tlet resBuffer = Buffer.alloc(0);\n\t\t\t\t\tres.on('data', (d) => {\n\t\t\t\t\t\tresBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);\n\t\t\t\t\t});\n\t\t\t\t\tres.on('end', () => {\n\t\t\t\t\t\tif (resBuffer.length > 256) {\n\t\t\t\t\t\t\tresolve(resBuffer);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlet str = resBuffer.toString();\n\t\t\t\t\t\t\tlet json = JSON.parse(str);\n\t\t\t\t\t\t\treject(json.msg);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t\tres.on('abort', () => {\n\t\t\t\t\t\treject('abort')\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t});\n\t\t} else {\n\t\t\treject('empty buffer')\n\t\t}\n\t})\n}\n\nexport async function compressFile(file) {\n\tlet buffer = await fs.readFile(file);\n\treturn compressB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AC,CAAC,CAAC,CAAC;oBAChC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE;wBACpB,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;oBACxE,CAAC,CAAC,CAAC;oBACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;wBAClB,IAAG,gBAAK,CAAC,SAAS,CAAC,IAAI,gBAAK,CAAC,SAAS,CAAC,EAAC;4BACvC,OAAO,CAAC,SAAS,CAAC,CAAC;yBACnB;6BAAM;4BACN,IAAI,GAAG,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAC;4BAC/B,IAAI,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;4BAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;yBACjB;oBACF,CAAC,CAAC,CAAC;oBACH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;wBACpB,MAAM,CAAC,OAAO,CAAC,CAAA;oBAChB,CAAC,CAAC,CAAC;iBACH;YACF,CAAC,CAAC,CAAC;SACH;aAAM;YACN,MAAM,CAAC,cAAc,CAAC,CAAA;SACtB;IACF,CAAC,CAAC,CAAA;AACH,CAAC;AArCD,wCAqCC;AAEM,KAAK,UAAU,YAAY,CAAC,IAAI;IACtC,IAAI,MAAM,GAAG,MAAM,kBAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACrC,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC;AAHD,oCAGC"}
\ No newline at end of file
......@@ -2,18 +2,20 @@
"name": "tiny-image",
"version": "1.0.0",
"main": "dist/index.js",
"types": "types.d.ts",
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"form-data": "^3.0.0",
"fs-extra": "^8.1.0",
"node-fetch": "^2.6.0"
"node-fetch": "^2.6.0",
"is-jpg": "^2.0.0",
"is-png": "^2.0.0"
},
"scripts": {
"dev": "rollup -c -w",
"build": "rollup -c"
"dev": "tsc -w",
"build": "tsc"
},
"devDependencies": {
"rollup-plugin-progress": "^1.1.1"
"@types/fs-extra": "^9.0.2"
}
}
/**
* Created by rockyl on 2018/11/16.
*/
const progress = require('rollup-plugin-progress');
const options = {
input: 'src/index.js',
output: [
{
file: `dist/index.js`,
format: 'cjs',
sourcemap: true,
}
],
plugins: [
progress(),
],
};
export default options;
......@@ -4,16 +4,19 @@
import FormData from 'form-data';
import fs from 'fs-extra';
import isPng from "is-png";
import isJpg from "is-jpg";
const tinifyUrl = 'http://tinify.duiba.com.cn/tinify';
export async function compressBuffer(buffer) {
return new Promise((resolve, reject) => {
if (buffer && buffer.length > 0) {
let extname = isPng(buffer) ? 'png' : 'jpg';
let form = new FormData();
form.append('file', buffer, {
filename: 'image.png',
contentType: 'image/png',
filename: 'image.' + extname,
contentType: 'image/' + extname,
});
form.submit(tinifyUrl, function (err, res) {
......@@ -26,7 +29,7 @@ export async function compressBuffer(buffer) {
resBuffer = Buffer.concat([resBuffer, d], resBuffer.length + d.length);
});
res.on('end', () => {
if (resBuffer.length > 256) {
if(isPng(resBuffer) || isJpg(resBuffer)){
resolve(resBuffer);
} else {
let str = resBuffer.toString();
......
......@@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"allowJs": true,
"experimentalDecorators": true,
"lib": ["esnext", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"noImplicitAny": false,
"noUnusedLocals": false,
"outDir": "dist/",
"preserveConstEnums": true,
"removeComments": false,
"sourceMap": true,
"strictNullChecks": true,
"target": "ES2017",
"traceResolution": false,
"types" : ["node"]
},
"include": [
"./src"
]
}
/**
* Created by rockyl on 2020-02-13.
*/
declare namespace e{
export function compressBuffer(buffer);
}
export = e;
\ No newline at end of file
......@@ -2,39 +2,23 @@
# yarn lockfile v1
ansi-styles@^3.2.1:
version "3.2.1"
resolved "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
integrity sha1-QfuyAkPlCxK+DwS43tvwdSDOhB0=
"@types/fs-extra@^9.0.2":
version "9.0.2"
resolved "http://npm.dui88.com:80/@types%2ffs-extra/-/fs-extra-9.0.2.tgz#e1e1b578c48e8d08ae7fc36e552b94c6f4621609"
integrity sha1-4eG1eMSOjQiuf8NuVSuUxvRiFgk=
dependencies:
color-convert "^1.9.0"
"@types/node" "*"
"@types/node@*":
version "14.14.5"
resolved "http://npm.dui88.com:80/@types%2fnode/-/node-14.14.5.tgz#e92d3b8f76583efa26c1a63a21c9d3c1143daa29"
integrity sha1-6S07j3ZYPvomwaY6IcnTwRQ9qik=
asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.npm.taobao.org/asynckit/download/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=
chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.npm.taobao.org/chalk/download/chalk-2.4.2.tgz?cache=0&sync_timestamp=1573282918610&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fchalk%2Fdownload%2Fchalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha1-zUJUFnelQzPPVBpJEIwUMrRMlCQ=
dependencies:
ansi-styles "^3.2.1"
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"
color-convert@^1.9.0:
version "1.9.3"
resolved "https://registry.npm.taobao.org/color-convert/download/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
integrity sha1-u3GFBpDh8TZWfeYp0tVHHe2kweg=
dependencies:
color-name "1.1.3"
color-name@1.1.3:
version "1.1.3"
resolved "https://registry.npm.taobao.org/color-name/download/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=
combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.npm.taobao.org/combined-stream/download/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
......@@ -47,11 +31,6 @@ delayed-stream@~1.0.0:
resolved "https://registry.npm.taobao.org/delayed-stream/download/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=
escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fescape-string-regexp%2Fdownload%2Fescape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
form-data@^3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/form-data/download/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
......@@ -75,10 +54,15 @@ graceful-fs@^4.1.6, graceful-fs@^4.2.0:
resolved "https://registry.npm.taobao.org/graceful-fs/download/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
integrity sha1-ShL/G2A3bvCYYsIJPt2Qgyi+hCM=
has-flag@^3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/has-flag/download/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=
is-jpg@^2.0.0:
version "2.0.0"
resolved "http://npm.dui88.com:80/is-jpg/-/is-jpg-2.0.0.tgz#2e1997fa6e9166eaac0242daae443403e4ef1d97"
integrity sha1-LhmX+m6RZuqsAkLarkQ0A+TvHZc=
is-png@^2.0.0:
version "2.0.0"
resolved "http://npm.dui88.com:80/is-png/-/is-png-2.0.0.tgz#ee8cbc9e9b050425cedeeb4a6fb74a649b0a4a8d"
integrity sha1-7oy8npsFBCXO3utKb7dKZJsKSo0=
jsonfile@^4.0.0:
version "4.0.0"
......@@ -104,20 +88,6 @@ node-fetch@^2.6.0:
resolved "https://registry.npm.taobao.org/node-fetch/download/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
integrity sha1-5jNFY4bUqlWGP2dqerDaqP3ssP0=
rollup-plugin-progress@^1.1.1:
version "1.1.1"
resolved "https://registry.npm.taobao.org/rollup-plugin-progress/download/rollup-plugin-progress-1.1.1.tgz#fcce2f915d545014ac89ea0927ee97cb48052f60"
integrity sha1-/M4vkV1UUBSsieoJJ+6Xy0gFL2A=
dependencies:
chalk "^2.4.2"
supports-color@^5.3.0:
version "5.5.0"
resolved "https://registry.npm.taobao.org/supports-color/download/supports-color-5.5.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fsupports-color%2Fdownload%2Fsupports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
integrity sha1-4uaaRKyHcveKHsCzW2id9lMO/I8=
dependencies:
has-flag "^3.0.0"
universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.npm.taobao.org/universalify/download/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment