allbitsnbytes / gulp-imageoptim

Gulp plugin to optimize images using ImageOptim
Other
22 stars 2 forks source link

TypeError: Data must be a string or a buffer #9

Closed terryupton closed 7 years ago

terryupton commented 7 years ago

I have setup to use imageOptim in my gulp project. Each time I run this I am getting the following errors;

crypto.js:67
  this._handle.update(data, encoding);
               ^

TypeError: Data must be a string or a buffer
    at TypeError (native)
    at Hash.update (crypto.js:67:16)
    ....

Any ideas on what is causing this or how to resolve it?

braginteractive commented 7 years ago

+1

HarrisJT commented 7 years ago

+1

OS : Windows 10 Node: v7.1.0 npm: 3.10.9

Humeira commented 7 years ago

Hello

I have this problem as well

crypto.js:74
  this._handle.update(data, encoding);
               ^

TypeError: Data must be a string or a buffer
    at TypeError (native)
    at Hash.update (crypto.js:74:16)

Here's my environment :

OS : MacOS Sierra Node : v6.9.2 Npm : 3.10.9

Any ideas about this?

tariq86 commented 7 years ago

The issue is coming from index.js:151:

var batchDir = crypto.createHash ('md5').update(Date.now()).digest ('base64'),

If you tack a .toString() onto the Date.now() call (to make sure it's a string, as the error states), the script still dies a little further down. I'll update again if I get it working

tariq86 commented 7 years ago

I can create a PR to fix the issue, but the code I'm getting from NPM (v1.0.8) does not match the code in this repo (v1.0.3).

I've never seen this before, so I'm not really sure where the newer 1.0.8 code is being pulled from?

HarrisJT commented 7 years ago

Last few updates to the npm were 2 months ago and the code from the repo is more like 10+ months old

mostafakarim commented 7 years ago

I have the same issue.

There's some news to fix it ?

kotazi commented 7 years ago

+1

bjosch commented 7 years ago

+1

MirtlBu commented 7 years ago

+1

jayhlee commented 7 years ago

+1

ginlime commented 7 years ago

+1 Is this project still alive?

manlikeemkay commented 7 years ago

+1

Ricksen commented 7 years ago

+1

sgnl commented 7 years ago

⚰️

kattsushi commented 7 years ago

same issue

patrickmatsumura commented 7 years ago

The main problems are the way the directory and files are being named. When using a base64 encoding in the digest function, you risk having a '/' in the folder or file name. I've added the updated batchOptimize function using hex encoding.

/**
 * Batch optimize files and push.
 *
 * @param {Array} files - Array of files to batch optimize
 * @param {Object} options - Options.  Currently status is supported as boolean indicating whether to display optmization result status or not
 * @param {Function} next - The callback to continue processing
 */
var batchOptimize = function (files, options, next) {
    if (files.length === 0)
        next (files)

    var batchDir = crypto.createHash ('md5').update (Date.now().toString()).digest ('hex'),
        jpegminiEnabled = options.jpegmini ? '--jpegmini' : '',
        scriptParams = path.normalize (batchDir) + ' ' + jpegminiEnabled

    // Make batch directory
    fs.mkdirSync (batchDir)

    // Add files to optimize to batch directory
    for (var i = 0, length = files.length; i < length; i++) {
        var file = files[i]

        file.batchFilePath = path.join (batchDir, crypto.createHash ('md5').update (file.path).digest ('hex') + path.extname (file.path))
        fs.writeFileSync (file.batchFilePath, file.contents)
    }

    // Optimize files
    exec ('bash node_modules/gulp-imageoptim/scripts/optimize.bash ' + scriptParams, function (error, stdout) {
        var result = {}

        if (error === null) {
            var savings = parseInt (stdout.replace (/.*\(([0-9]+)(\.[0-9]+)?\%\)/, '$1')),
                msg = ''

            if (savings > 0) {

                // Copy optimized file contents to original and remove file
                for (var i = 0, length = files.length; i < length; i++) {
                    var file = files[i]

                    file.contents = new Buffer(fs.readFileSync (file.batchFilePath))
                    stdout = stdout.replace (file.batchFilePath, path.basename (file.path))
                }

                msg = stdout.replace ('TOTAL was', 'Filesize total was')
            } else {
                msg = 'Saving: 0%\n'
            }

            result = {
                'files'     : files.length,
                'msg'       : msg,
                'savings'   : savings,
                'type'      : 'success'
            }
        } else {
            console.error (error)
        }

        // Delete batch directory
        removeDirectory (batchDir)

        // Display optmization result status?
        if (options.status) {
            displayOptimizationResults (result)
        }

        next (files)
    })
}

For some reason this repo and the version coming from npm are not the same. Could you please explain why, @allbitsnbytes? I'd would have gladly provided a pull request.

sgnl commented 7 years ago

someone needs to push to NPM and I'm guessing allbitnbytes has permissions. Might be time to fork and push a new version to NPM

HarrisJT commented 7 years ago

He must not use github anymore but kept updating the npm? I would take it over but I'm not sure how okay he is with it. I'll look for some sort of other contact information

ginlime commented 7 years ago

@HarrisJT His Twitter account seems to be still alive... https://twitter.com/allbitsnbytes

allbitsnbytes commented 7 years ago

Guys sorry for not getting to this earlier. I didn't get the github notifications about this thread. I've published the fix to NPM in version 1.0.9. I'll update the this repo later.

@ginlime thank you for alerting me on twitter.

@patrickmatsumura and @tariq86 thank you for looking into this issue.