From 51f43b022c34b2a1c75db52cad5b4d14ac42d0c6 Mon Sep 17 00:00:00 2001 From: Eric Ellingson Date: Sat, 13 Jan 2018 15:32:34 -0800 Subject: [PATCH 1/4] add support for promises; closes #6 --- README.md | 31 +- base64-img.js | 70 ++++- package-lock.json | 742 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- test/index.js | 64 ++-- 5 files changed, 874 insertions(+), 36 deletions(-) create mode 100644 package-lock.json diff --git a/README.md b/README.md index 533a0a4..e305162 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,20 @@ npm install base64-img --save mocha ``` ## API -### .base64(filename, callback) +Note: All methods which take a callback, if omitted, will return a promise +### .base64(filename[, callback]) Convert image file to image base64 data * {string} ``filename`` required The image path -* {function} ``callback(err, data)`` required +* {function} ``callback(err, data)`` optional Callback with image base64 data ```js base64Img.base64('path/demo.png', function(err, data) {}) + +/* promise */ +base64Img.base64('path/demo.png').then(function(data) { + +}) ``` ### .base64Sync(filename) @@ -28,18 +34,24 @@ The api same as base64, but it's synchronous var data = base64Img.base64Sync('path/demo.png'); ``` -### .requestBase64(url, callback) +### .requestBase64(url[, callback]) * {string} ``url`` required -* {function} ``callback(err, res, body)`` required +* {function} ``callback(err, res, body)`` optional Callback with http request ```js var url = 'http://../demo.png'; -base64Img.requestBase64(url, function(err, res, body) { +base64Img.requestBase64(url, function(err, res, data) { +}); + +/* promise */ +var url = 'http://../demo.png'; +base64Img.requestBase64(url).then(function({res, data}) { + }); ``` -### .img(data, destpath, name, callback) +### .img(data, destpath, name[, callback]) Convert image base64 data to image * {string} ``data`` required Image base64 data @@ -47,9 +59,14 @@ Image base64 data Dest path, if the destpath is root, pass empty string * {string} ``name`` required The image's filename -* {function} ``callback(err, filepath)`` required +* {function} ``callback(err, filepath)`` optional ```js base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {}); + +/* promise */ +base64Img.img('data:image/png;base64,...', 'dest', '1').then(function(filepath) { + +}); ``` ### .imgSync(data, destpath, name) diff --git a/base64-img.js b/base64-img.js index 10bb7c3..aeff99e 100644 --- a/base64-img.js +++ b/base64-img.js @@ -5,7 +5,7 @@ var request = require('ajax-request'); function base64(filename, data) { var extname = path.extname(filename).substr(1); extname = extname || 'png'; - + return 'data:image/' + extname + ';base64,' + data.toString('base64'); } @@ -28,16 +28,56 @@ function img(data) { }; } +function base64Promise(filename) { + return new Promise((resolve, reject) => { + fs.readFile(filename, function (err, data) { + if (err) reject(err); + + resolve(base64(filename, data)); + }); + }); +}; + +function requestBase64Promise(url) { + return new Promise((resolve, reject) => { + request({ + url: url, + isBuffer: true + }, function (err, res, body) { + if (err) return reject(err); + + var data = 'data:' + res.headers['content-type'] + ';base64,' + body.toString('base64'); + resolve({ res, data }); + }); + }); +}; + +function imgPromise(data, destpath, name) { + return new Promise((resolve, reject) => { + var result = img(data); + var filepath = path.join(destpath, name + result.extname); + + fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function (err) { + if (err) return reject(err); + resolve(filepath); + }); + }); +}; + /** * @description * Get image file base64 data * @example * base64Img.base64('path/demo.png', function(err, data) {}) + * @example + * base64Img.base64('path/demo.png').then(function(data) { + * + * }); */ -exports.base64 = function(filename, callback) { - if (!callback) callback = util.noop; +exports.base64 = function (filename, callback) { + if (!callback) return base64Promise(filename); - fs.readFile(filename, function(err, data) { + fs.readFile(filename, function (err, data) { if (err) return callback(err); callback(null, base64(filename, data)); @@ -50,7 +90,7 @@ exports.base64 = function(filename, callback) { * @example * var data = base64Img.base64Sync('path/demo.png'); */ -exports.base64Sync = function(filename) { +exports.base64Sync = function (filename) { var data = fs.readFileSync(filename); return base64(filename, data); @@ -66,8 +106,16 @@ exports.base64Sync = function(filename) { * * } * ); + * @example + * request.base64( + * 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png', + * ).then(function({res, data}) { + * + * }); */ -exports.requestBase64 = function(url, callback) { +exports.requestBase64 = function (url, callback) { + if (!callback) return requestBase64Promise(url); + request({ url: url, isBuffer: true @@ -84,12 +132,16 @@ exports.requestBase64 = function(url, callback) { * Convert image base64 data to img * @example * base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {}); + * @example + * base64Img.img('data:image/png;base64,...', 'dest', '1').then(function(filepath) {}); */ -exports.img = function(data, destpath, name, callback) { +exports.img = function (data, destpath, name, callback) { + if (!callback) return imgPromise(data, destpath, name); + var result = img(data); var filepath = path.join(destpath, name + result.extname); - fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function(err) { + fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function (err) { callback(err, filepath); }); }; @@ -100,7 +152,7 @@ exports.img = function(data, destpath, name, callback) { * @example * var filepath = base64Img.imgSync('data:image/png;base64,...', 'dest', '1'); */ -exports.imgSync = function(data, destpath, name) { +exports.imgSync = function (data, destpath, name) { var result = img(data); var filepath = path.join(destpath, name + result.extname); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..ebf0af2 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,742 @@ +{ + "name": "base64-img", + "version": "1.0.3", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "abbrev": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", + "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", + "dev": true + }, + "ajax-request": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/ajax-request/-/ajax-request-1.2.3.tgz", + "integrity": "sha1-mfy+wdbSeS+F+pSVNTMr0U9fN5A=", + "requires": { + "file-system": "2.2.2", + "utils-extend": "1.0.8" + } + }, + "argparse": { + "version": "0.1.16", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-0.1.16.tgz", + "integrity": "sha1-z9AeD7uj1srtBJ+9dY1A9lGW9Xw=", + "dev": true, + "requires": { + "underscore": "1.7.0", + "underscore.string": "2.4.0" + }, + "dependencies": { + "underscore.string": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-2.4.0.tgz", + "integrity": "sha1-jN2PusTi0uoefi6Al8QvRCKA+Fs=", + "dev": true + } + } + }, + "async": { + "version": "0.1.22", + "resolved": "https://registry.npmjs.org/async/-/async-0.1.22.tgz", + "integrity": "sha1-D8GqoIig4+8Ovi2IMbqw3PiEUGE=", + "dev": true + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", + "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=", + "dev": true, + "requires": { + "balanced-match": "1.0.0", + "concat-map": "0.0.1" + } + }, + "browser-stdout": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz", + "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=", + "dev": true + }, + "cli": { + "version": "0.6.6", + "resolved": "https://registry.npmjs.org/cli/-/cli-0.6.6.tgz", + "integrity": "sha1-Aq1Eo4Cr8nraxebwzdewQ9dMU+M=", + "dev": true, + "requires": { + "exit": "0.1.2", + "glob": "3.2.11" + }, + "dependencies": { + "glob": { + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz", + "integrity": "sha1-Spc/Y1uRkPcV0QmH1cAP0oFevj0=", + "dev": true, + "requires": { + "inherits": "2.0.3", + "minimatch": "0.3.0" + } + }, + "minimatch": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz", + "integrity": "sha1-J12O2qxPG7MyZHIInnlJyDlGmd0=", + "dev": true, + "requires": { + "lru-cache": "2.7.3", + "sigmund": "1.0.1" + } + } + } + }, + "coffee-script": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/coffee-script/-/coffee-script-1.3.3.tgz", + "integrity": "sha1-FQ1rTLUiiUNp7+1qIQHCC8f0pPQ=", + "dev": true + }, + "colors": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/colors/-/colors-0.6.2.tgz", + "integrity": "sha1-JCP+ZnisDF2uiFLl0OW+CMmXq8w=", + "dev": true + }, + "commander": { + "version": "2.11.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", + "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==", + "dev": true + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "console-browserify": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz", + "integrity": "sha1-8CQcRXMKn8YyOyBtvzjtx0HQuxA=", + "dev": true, + "requires": { + "date-now": "0.1.4" + } + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", + "dev": true + }, + "date-now": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz", + "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=", + "dev": true + }, + "dateformat": { + "version": "1.0.2-1.2.3", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-1.0.2-1.2.3.tgz", + "integrity": "sha1-sCIMAt6YYXQztyhRz0fePfLNvuk=", + "dev": true + }, + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "diff": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz", + "integrity": "sha512-MKPHZDMB0o6yHyDryUOScqZibp914ksXwAMYMTHj6KO8UeKsRYNJD3oNCKjTqZon+V488P7N/HzXF8t7ZR95ww==", + "dev": true + }, + "dom-serializer": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.1.0.tgz", + "integrity": "sha1-BzxpdUbOB4DOI75KKOKT5AvDDII=", + "dev": true, + "requires": { + "domelementtype": "1.1.3", + "entities": "1.1.1" + }, + "dependencies": { + "domelementtype": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.1.3.tgz", + "integrity": "sha1-vSh3PiZCiBrsUVRJJCmcXNgiGFs=", + "dev": true + }, + "entities": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.1.tgz", + "integrity": "sha1-blwtClYhtdra7O+AuQ7ftc13cvA=", + "dev": true + } + } + }, + "domelementtype": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.0.tgz", + "integrity": "sha1-sXrtguirWeUt2cGbF1bg/BhyBMI=", + "dev": true + }, + "domhandler": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.3.0.tgz", + "integrity": "sha1-LeWaCCLVAn+r/28DLCsloqir5zg=", + "dev": true, + "requires": { + "domelementtype": "1.3.0" + } + }, + "domutils": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.5.1.tgz", + "integrity": "sha1-3NhIiib1Y9YQeeSMn3t+Mjc2gs8=", + "dev": true, + "requires": { + "dom-serializer": "0.1.0", + "domelementtype": "1.3.0" + } + }, + "entities": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-1.0.0.tgz", + "integrity": "sha1-sph6o4ITR/zeZCsk/fyeT7cSvyY=", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "esprima": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-1.0.4.tgz", + "integrity": "sha1-n1V+CPw7TSbs6d00+Pv0drYlha0=", + "dev": true + }, + "eventemitter2": { + "version": "0.4.14", + "resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-0.4.14.tgz", + "integrity": "sha1-j2G3XN4BKy6esoTUVFWDtWQ7Yas=", + "dev": true + }, + "exit": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", + "integrity": "sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=", + "dev": true + }, + "file-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/file-match/-/file-match-1.0.2.tgz", + "integrity": "sha1-ycrSZdLIrfOoFHWw30dYWQafrvc=", + "requires": { + "utils-extend": "1.0.8" + } + }, + "file-system": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/file-system/-/file-system-2.2.2.tgz", + "integrity": "sha1-fWWDPjojR9zZVqgTxncVPtPt2Yc=", + "requires": { + "file-match": "1.0.2", + "utils-extend": "1.0.8" + } + }, + "findup-sync": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/findup-sync/-/findup-sync-0.1.3.tgz", + "integrity": "sha1-fz56l7gjksZTvwZYm9hRkOk8NoM=", + "dev": true, + "requires": { + "glob": "3.2.11", + "lodash": "2.4.2" + }, + "dependencies": { + "glob": { + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz", + "integrity": "sha1-Spc/Y1uRkPcV0QmH1cAP0oFevj0=", + "dev": true, + "requires": { + "inherits": "2.0.3", + "minimatch": "0.3.0" + } + }, + "lodash": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-2.4.2.tgz", + "integrity": "sha1-+t2DS5aDBz2hebPq5tnA0VBT9z4=", + "dev": true + }, + "minimatch": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz", + "integrity": "sha1-J12O2qxPG7MyZHIInnlJyDlGmd0=", + "dev": true, + "requires": { + "lru-cache": "2.7.3", + "sigmund": "1.0.1" + } + } + } + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "getobject": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/getobject/-/getobject-0.1.0.tgz", + "integrity": "sha1-BHpEl4n6Fg0Bj1SG7ZEyC27HiFw=", + "dev": true + }, + "glob": { + "version": "3.1.21", + "resolved": "https://registry.npmjs.org/glob/-/glob-3.1.21.tgz", + "integrity": "sha1-0p4KBV3qUTj00H7UDomC6DwgZs0=", + "dev": true, + "requires": { + "graceful-fs": "1.2.3", + "inherits": "1.0.2", + "minimatch": "0.2.14" + }, + "dependencies": { + "inherits": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-1.0.2.tgz", + "integrity": "sha1-ykMJ2t7mtUzAuNJH6NfHoJdb3Js=", + "dev": true + } + } + }, + "graceful-fs": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-1.2.3.tgz", + "integrity": "sha1-FaSAaldUfLLS2/J/QuiajDRRs2Q=", + "dev": true + }, + "growl": { + "version": "1.10.3", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.3.tgz", + "integrity": "sha512-hKlsbA5Vu3xsh1Cg3J7jSmX/WaW6A5oBeqzM88oNbCRQFz+zUaXm6yxS4RVytp1scBoJzSYl4YAEOQIt6O8V1Q==", + "dev": true + }, + "grunt": { + "version": "0.4.5", + "resolved": "https://registry.npmjs.org/grunt/-/grunt-0.4.5.tgz", + "integrity": "sha1-VpN81RlDJK3/bSB2MYMqnWuk5/A=", + "dev": true, + "requires": { + "async": "0.1.22", + "coffee-script": "1.3.3", + "colors": "0.6.2", + "dateformat": "1.0.2-1.2.3", + "eventemitter2": "0.4.14", + "exit": "0.1.2", + "findup-sync": "0.1.3", + "getobject": "0.1.0", + "glob": "3.1.21", + "grunt-legacy-log": "0.1.3", + "grunt-legacy-util": "0.2.0", + "hooker": "0.2.3", + "iconv-lite": "0.2.11", + "js-yaml": "2.0.5", + "lodash": "0.9.2", + "minimatch": "0.2.14", + "nopt": "1.0.10", + "rimraf": "2.2.8", + "underscore.string": "2.2.1", + "which": "1.0.9" + } + }, + "grunt-contrib-jshint": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/grunt-contrib-jshint/-/grunt-contrib-jshint-0.10.0.tgz", + "integrity": "sha1-V+vMyofo8yevZkXYo8WG1IReTYE=", + "dev": true, + "requires": { + "hooker": "0.2.3", + "jshint": "2.5.11" + } + }, + "grunt-legacy-log": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/grunt-legacy-log/-/grunt-legacy-log-0.1.3.tgz", + "integrity": "sha1-7ClCboAwIa9ZAp+H0vnNczWgVTE=", + "dev": true, + "requires": { + "colors": "0.6.2", + "grunt-legacy-log-utils": "0.1.1", + "hooker": "0.2.3", + "lodash": "2.4.2", + "underscore.string": "2.3.3" + }, + "dependencies": { + "lodash": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-2.4.2.tgz", + "integrity": "sha1-+t2DS5aDBz2hebPq5tnA0VBT9z4=", + "dev": true + }, + "underscore.string": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-2.3.3.tgz", + "integrity": "sha1-ccCL9rQosRM/N+ePo6Icgvcymw0=", + "dev": true + } + } + }, + "grunt-legacy-log-utils": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/grunt-legacy-log-utils/-/grunt-legacy-log-utils-0.1.1.tgz", + "integrity": "sha1-wHBrndkGThFvNvI/5OawSGcsD34=", + "dev": true, + "requires": { + "colors": "0.6.2", + "lodash": "2.4.2", + "underscore.string": "2.3.3" + }, + "dependencies": { + "lodash": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-2.4.2.tgz", + "integrity": "sha1-+t2DS5aDBz2hebPq5tnA0VBT9z4=", + "dev": true + }, + "underscore.string": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-2.3.3.tgz", + "integrity": "sha1-ccCL9rQosRM/N+ePo6Icgvcymw0=", + "dev": true + } + } + }, + "grunt-legacy-util": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/grunt-legacy-util/-/grunt-legacy-util-0.2.0.tgz", + "integrity": "sha1-kzJIhNv343qf98Am3/RR2UqeVUs=", + "dev": true, + "requires": { + "async": "0.1.22", + "exit": "0.1.2", + "getobject": "0.1.0", + "hooker": "0.2.3", + "lodash": "0.9.2", + "underscore.string": "2.2.1", + "which": "1.0.9" + } + }, + "has-flag": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz", + "integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=", + "dev": true + }, + "he": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", + "dev": true + }, + "hooker": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/hooker/-/hooker-0.2.3.tgz", + "integrity": "sha1-uDT3I8xKJCqmWWNFnfbZhMXT2Vk=", + "dev": true + }, + "htmlparser2": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.8.3.tgz", + "integrity": "sha1-mWwosZFRaovoZQGn15dX5ccMEGg=", + "dev": true, + "requires": { + "domelementtype": "1.3.0", + "domhandler": "2.3.0", + "domutils": "1.5.1", + "entities": "1.0.0", + "readable-stream": "1.1.14" + } + }, + "iconv-lite": { + "version": "0.2.11", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.2.11.tgz", + "integrity": "sha1-HOYKOleGSiktEyH/RgnKS7llrcg=", + "dev": true + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "1.4.0", + "wrappy": "1.0.2" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=", + "dev": true + }, + "js-yaml": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-2.0.5.tgz", + "integrity": "sha1-olrmUJmZ6X3yeMZxnaEb0Gh3Q6g=", + "dev": true, + "requires": { + "argparse": "0.1.16", + "esprima": "1.0.4" + } + }, + "jshint": { + "version": "2.5.11", + "resolved": "https://registry.npmjs.org/jshint/-/jshint-2.5.11.tgz", + "integrity": "sha1-4tlYWLuxqngwAQii6BCZ+wlWIuA=", + "dev": true, + "requires": { + "cli": "0.6.6", + "console-browserify": "1.1.0", + "exit": "0.1.2", + "htmlparser2": "3.8.3", + "minimatch": "1.0.0", + "shelljs": "0.3.0", + "strip-json-comments": "1.0.4", + "underscore": "1.6.0" + }, + "dependencies": { + "minimatch": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-1.0.0.tgz", + "integrity": "sha1-4N0hILSeG3JM6NcUxSCCKpQ4V20=", + "dev": true, + "requires": { + "lru-cache": "2.7.3", + "sigmund": "1.0.1" + } + }, + "underscore": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz", + "integrity": "sha1-izixDKze9jM3uLJOT/htRa6lKag=", + "dev": true + } + } + }, + "lodash": { + "version": "0.9.2", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-0.9.2.tgz", + "integrity": "sha1-jzSZxSRdNG1oLlsNO0B2fgnxqSw=", + "dev": true + }, + "lru-cache": { + "version": "2.7.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz", + "integrity": "sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=", + "dev": true + }, + "minimatch": { + "version": "0.2.14", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.2.14.tgz", + "integrity": "sha1-x054BXT2PG+aCQ6Q775u9TpqdWo=", + "dev": true, + "requires": { + "lru-cache": "2.7.3", + "sigmund": "1.0.1" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-4.1.0.tgz", + "integrity": "sha512-0RVnjg1HJsXY2YFDoTNzcc1NKhYuXKRrBAG2gDygmJJA136Cs2QlRliZG1mA0ap7cuaT30mw16luAeln+4RiNA==", + "dev": true, + "requires": { + "browser-stdout": "1.3.0", + "commander": "2.11.0", + "debug": "3.1.0", + "diff": "3.3.1", + "escape-string-regexp": "1.0.5", + "glob": "7.1.2", + "growl": "1.10.3", + "he": "1.1.1", + "mkdirp": "0.5.1", + "supports-color": "4.4.0" + }, + "dependencies": { + "glob": { + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "dev": true, + "requires": { + "fs.realpath": "1.0.0", + "inflight": "1.0.6", + "inherits": "2.0.3", + "minimatch": "3.0.4", + "once": "1.4.0", + "path-is-absolute": "1.0.1" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "1.1.8" + } + } + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "nopt": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz", + "integrity": "sha1-bd0hvSoxQXuScn3Vhfim83YI6+4=", + "dev": true, + "requires": { + "abbrev": "1.1.1" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1.0.2" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "dev": true, + "requires": { + "core-util-is": "1.0.2", + "inherits": "2.0.3", + "isarray": "0.0.1", + "string_decoder": "0.10.31" + } + }, + "rimraf": { + "version": "2.2.8", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.2.8.tgz", + "integrity": "sha1-5Dm+Kq7jJzIZUnMPmaiSnk/FBYI=", + "dev": true + }, + "shelljs": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.3.0.tgz", + "integrity": "sha1-NZbmMHp4FUT1kfN9phg2DzHbV7E=", + "dev": true + }, + "sigmund": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz", + "integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=", + "dev": true + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", + "dev": true + }, + "strip-json-comments": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-1.0.4.tgz", + "integrity": "sha1-HhX7ysl9Pumb8tc7TGVrCCu6+5E=", + "dev": true + }, + "supports-color": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.4.0.tgz", + "integrity": "sha512-rKC3+DyXWgK0ZLKwmRsrkyHVZAjNkfzeehuFWdGGcqGDTZFH73+RH6S/RDAAxl9GusSjZSUWYLmT9N5pzXFOXQ==", + "dev": true, + "requires": { + "has-flag": "2.0.0" + } + }, + "underscore": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.7.0.tgz", + "integrity": "sha1-a7rwh3UA02vjTsqlhODbn+8DUgk=", + "dev": true + }, + "underscore.string": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-2.2.1.tgz", + "integrity": "sha1-18D6KvXVoaZ/QlPa7pgTLnM/Dxk=", + "dev": true + }, + "utils-extend": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/utils-extend/-/utils-extend-1.0.8.tgz", + "integrity": "sha1-zP17ZFQPjpDuIe7Fd2nQZRyril8=" + }, + "which": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/which/-/which-1.0.9.tgz", + "integrity": "sha1-RgwdoPgQED0DIam2M6+eV15kSG8=", + "dev": true + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + } + } +} diff --git a/package.json b/package.json index 592a61c..206bfc8 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "devDependencies": { "grunt": "^0.4.5", - "grunt-contrib-jshint": "^0.10.0" + "grunt-contrib-jshint": "^0.10.0", + "mocha": "^4.1.0" } } diff --git a/test/index.js b/test/index.js index 9717ed2..b29ebfb 100644 --- a/test/index.js +++ b/test/index.js @@ -11,35 +11,47 @@ function readFileSync(filepath, encoding) { return fs.readFileSync(path.join(__dirname, filepath), { encoding: encoding || 'base64' }); } -describe('Test', function() { - it('All test', function(done) { +describe('Test', function () { + it('All test', function (done) { var demo = readFileSync('img/demo.png'); - base64Img.base64(getpath('img/demo.png'), function(err, data) { - base64Img.img(data, getpath('dest'), '1', function(err, filepath) { + base64Img.base64(getpath('img/demo.png'), function (err, data) { + base64Img.img(data, getpath('dest'), '1', function (err, filepath) { assert.equal(filepath, getpath('dest/1.png')); assert.equal(demo, readFileSync('dest/1.png')); done(); - }); + }); + }); + }); + + it('All test (promise)', function (done) { + var demo = readFileSync('img/demo.png'); + + base64Img.base64(getpath('img/demo.png')).then(function (data) { + base64Img.img(data, getpath('dest'), '1').then(function (filepath) { + assert.equal(filepath, getpath('dest/1.png')); + assert.equal(demo, readFileSync('dest/1.png')); + done(); + }); }); }); - it('All test sync', function() { + it('All test sync', function () { var demo = readFileSync('img/demo.png'); var data = base64Img.base64Sync(getpath('img/demo.png')); - var filepath = base64Img.imgSync(data, getpath('dest'), '2'); - - assert.equal(filepath, getpath('dest/2.png')); - assert.equal(demo, readFileSync('dest/2.png')); + var filepath = base64Img.imgSync(data, getpath('dest'), '2'); + + assert.equal(filepath, getpath('dest/2.png')); + assert.equal(demo, readFileSync('dest/2.png')); }); - describe('img', function() { - it('Image base64 data error', function() { + describe('img', function () { + it('Image base64 data error', function () { assert.throws( - function() { + function () { base64Img.imgSync('data:'); }, - function(err) { + function (err) { if (/base64/.test(err)) { return true; } @@ -47,18 +59,18 @@ describe('Test', function() { ); }); - it('jpeg', function() { + it('jpeg', function () { var data = base64Img.base64Sync(getpath('img/demo.png')).replace('image/png', 'image/jpeg'); var filepath = base64Img.imgSync(data, getpath('dest'), '3'); - assert.equal('.jpg', path.extname(filepath)); + assert.equal('.jpg', path.extname(filepath)); }); - it('request', function(done) { + it('request', function (done) { var url = 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png'; var demo = readFileSync('img/demo.png'); var data = base64Img.base64Sync(getpath('img/demo.png')); - - base64Img.requestBase64(url, function(err, res, body) { + + base64Img.requestBase64(url, function (err, res, body) { if (err) { done(); return console.log(err); @@ -68,5 +80,19 @@ describe('Test', function() { done(); }); }); + + it('request (promise)', function (done) { + var url = 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png'; + var demo = readFileSync('img/demo.png'); + var _data = base64Img.base64Sync(getpath('img/demo.png')); + + base64Img.requestBase64(url).then(function ({ res, data }) { + assert.equal(data.substr(0, 10), _data.substr(0, 10)); + done(); + }).catch(function (err) { + done(); + console.log(err); + }); + }); }); }); \ No newline at end of file From b24217e26a0a3d0fd462687599bc592184191b55 Mon Sep 17 00:00:00 2001 From: Eric Ellingson Date: Wed, 24 Jan 2018 09:07:30 -0800 Subject: [PATCH 2/4] update package.json & readme --- README.md | 88 +++++++++++++++++++++++++++++------------------ package-lock.json | 4 +-- package.json | 23 ++++++------- 3 files changed, 67 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index e305162..0193429 100644 --- a/README.md +++ b/README.md @@ -1,76 +1,98 @@ -# base64-img +# base64-img-promise + Convert img to base64, or convert base64 to img + ```js -var base64Img = require('base64-img'); +var base64Img = require('base64-img-promise'); ``` + ## install + ``` -npm install base64-img --save +npm install base64-img-promise --save ``` + ## test + ``` mocha ``` + ## API + Note: All methods which take a callback, if omitted, will return a promise + ### .base64(filename[, callback]) + Convert image file to image base64 data -* {string} ``filename`` required -The image path -* {function} ``callback(err, data)`` optional -Callback with image base64 data + +* {string} `filename` required + The image path +* {function} `callback(err, data)` optional + Callback with image base64 data + ```js -base64Img.base64('path/demo.png', function(err, data) {}) +base64Img.base64('path/demo.png', function(err, data) {}); /* promise */ -base64Img.base64('path/demo.png').then(function(data) { - -}) +base64Img.base64('path/demo.png').then(function(data) {}); ``` ### .base64Sync(filename) -The api same as base64, but it's synchronous + +The api same as base64, but it's synchronous + ```js var data = base64Img.base64Sync('path/demo.png'); ``` ### .requestBase64(url[, callback]) -* {string} ``url`` required -* {function} ``callback(err, res, body)`` optional -Callback with http request + +* {string} `url` required +* {function} `callback(err, res, body)` optional + Callback with http request + ```js var url = 'http://../demo.png'; -base64Img.requestBase64(url, function(err, res, data) { - -}); +base64Img.requestBase64(url, function(err, res, data) {}); /* promise */ var url = 'http://../demo.png'; -base64Img.requestBase64(url).then(function({res, data}) { - -}); +base64Img.requestBase64(url).then(function({ res, data }) {}); ``` ### .img(data, destpath, name[, callback]) + Convert image base64 data to image -* {string} ``data`` required -Image base64 data -* {string} ``destpath`` required -Dest path, if the destpath is root, pass empty string -* {string} ``name`` required -The image's filename -* {function} ``callback(err, filepath)`` optional + +* {string} `data` required + Image base64 data +* {string} `destpath` required + Dest path, if the destpath is root, pass empty string +* {string} `name` required + The image's filename +* {function} `callback(err, filepath)` optional + ```js -base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {}); +base64Img.img('data:image/png;base64,...', 'dest', '1', function( + err, + filepath +) {}); /* promise */ -base64Img.img('data:image/png;base64,...', 'dest', '1').then(function(filepath) { - -}); +base64Img + .img('data:image/png;base64,...', 'dest', '1') + .then(function(filepath) {}); ``` ### .imgSync(data, destpath, name) + The api same as img, but it's synchronous + ```js var filepath = base64Img.imgSync('data:image/png;base64,...', '', '2'); -``` \ No newline at end of file +``` + +--- + +This is a fork of https://github.com/douzi8/base64-img diff --git a/package-lock.json b/package-lock.json index ebf0af2..a862b6f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "base64-img", - "version": "1.0.3", + "name": "base64-img-promise", + "version": "1.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 206bfc8..fb4c84e 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,24 @@ { - "name": "base64-img", - "version": "1.0.3", - "description": "Convert img to base64, or convert base64 to img", + "name": "base64-img-promise", + "version": "1.0.4", + "description": + "Convert img to base64, or convert base64 to img, with the option of using promises", "main": "base64-img.js", "scripts": { "test": "mocha" }, "repository": { "type": "git", - "url": "https://github.com/douzi8/base64-img.git" + "url": "https://github.com/e-e/base64-img-promise.git" }, - "keywords": [ - "base64", - "img", - "image", - "img base64" - ], - "author": "douzi ", + "keywords": ["base64", "img", "image", "img base64", "promise"], + "author": "Eric Ellingson ", + "original_author": "douzi ", "license": "ISC", "bugs": { - "url": "https://github.com/douzi8/base64-img/issues" + "url": "https://github.com/e-e/base64-img-promise/issues" }, - "homepage": "https://github.com/douzi8/base64-img", + "homepage": "https://github.com/e-e/base64-img-promise", "dependencies": { "ajax-request": "^1.2.0", "file-system": "^2.1.0" From 653fe33c008e24071960a0fa1da314704f44beee Mon Sep 17 00:00:00 2001 From: Eric Ellingson Date: Thu, 25 Jan 2018 08:47:22 -0800 Subject: [PATCH 3/4] promise examples first --- README.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0193429..1e8e0cf 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,11 @@ Convert image file to image base64 data Callback with image base64 data ```js -base64Img.base64('path/demo.png', function(err, data) {}); - /* promise */ base64Img.base64('path/demo.png').then(function(data) {}); + +/* callback */ +base64Img.base64('path/demo.png', function(err, data) {}); ``` ### .base64Sync(filename) @@ -53,12 +54,12 @@ var data = base64Img.base64Sync('path/demo.png'); Callback with http request ```js -var url = 'http://../demo.png'; -base64Img.requestBase64(url, function(err, res, data) {}); - /* promise */ var url = 'http://../demo.png'; base64Img.requestBase64(url).then(function({ res, data }) {}); + +var url = 'http://../demo.png'; +base64Img.requestBase64(url, function(err, res, data) {}); ``` ### .img(data, destpath, name[, callback]) @@ -74,15 +75,15 @@ Convert image base64 data to image * {function} `callback(err, filepath)` optional ```js -base64Img.img('data:image/png;base64,...', 'dest', '1', function( - err, - filepath -) {}); - /* promise */ base64Img .img('data:image/png;base64,...', 'dest', '1') .then(function(filepath) {}); + +base64Img.img('data:image/png;base64,...', 'dest', '1', function( + err, + filepath +) {}); ``` ### .imgSync(data, destpath, name) From e63306c935822bea70867f6a45c6fb501242cb56 Mon Sep 17 00:00:00 2001 From: Eric Ellingson Date: Thu, 25 Jan 2018 08:47:38 -0800 Subject: [PATCH 4/4] 1.0.5 --- package-lock.json | 2 +- package.json | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index a862b6f..70f2fa7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "base64-img-promise", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fb4c84e..ec8aff7 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,7 @@ { "name": "base64-img-promise", - "version": "1.0.4", - "description": - "Convert img to base64, or convert base64 to img, with the option of using promises", + "version": "1.0.5", + "description": "Convert img to base64, or convert base64 to img, with the option of using promises", "main": "base64-img.js", "scripts": { "test": "mocha" @@ -11,7 +10,13 @@ "type": "git", "url": "https://github.com/e-e/base64-img-promise.git" }, - "keywords": ["base64", "img", "image", "img base64", "promise"], + "keywords": [ + "base64", + "img", + "image", + "img base64", + "promise" + ], "author": "Eric Ellingson ", "original_author": "douzi ", "license": "ISC",