Skip to content

Instantly share code, notes, and snippets.

@jssuttles
Created July 19, 2016 06:02
Show Gist options
  • Save jssuttles/97135cda67fc56308fbf37b95c2e06af to your computer and use it in GitHub Desktop.
Save jssuttles/97135cda67fc56308fbf37b95c2e06af to your computer and use it in GitHub Desktop.
murmurhash arguments
'use strict';
//var Murmurhash = require('murmurhash-native').murmurHash128x86
nw.Window.get().showDevTools();
var realFs = require('fs');
var fse = require('fs-extra');
fse.createOutputStream = require('create-output-stream');
fse.gracefulify(realFs);
var fs = require('bluebird').promisifyAll(fse);
var murmur = require('murmurhash-native/stream');
var Transform = require('stream').Transform;
var createOutputStream = require('create-output-stream');
global.mainURI = '';
global.jsFolder = '';
global.modelsFolder = '';
global.vendorFolder = '';
class HashTransform extends Transform {
constructor() {
super();
this.hasher = murmur.createHash('murmurHash128x86');
// the following works
//this.hasher = murmur.createHash('murmurHash128x86', {seed: 42, endianness: 'LE'});
this.createdHash = '';
}
_transform(chunk, enc, cb) {
var buffer = (Buffer.isBuffer(chunk)) ? chunk : new Buffer(chunk, enc);
this.hasher.update(buffer);
this.push(chunk);
cb();
}
_flush(cb) {
this.createdHash = this.hasher.digest('base64');
cb();
}
}
function copyAndChecksum(source, target) {
return new Promise(function(resolve, reject) {
var rd = fs.createReadStream(source);
var wr = fs.createOutputStream(target);
var doneCalled = false;
rd.on('error', function(err) {
done(err);
});
wr.on('error', function(err) {
done(err);
});
wr.on('close', function() {
done();
});
var hash = new HashTransform();
rd.pipe(hash).pipe(wr);
function done(err) {
if (!doneCalled) {
if (err) {
reject(err);
} else {
resolve(hash.createdHash);
}
doneCalled = true;
}
}
});
}
copyAndChecksum('./package.json', './package2.json')
.then(function(hash) {
console.log(hash);
// this stuff I was thinking would work based on
// murmurHash(data{String}, encoding|output_type[, callback])
// var stuff = Murmurhash('assafasfasfasddd', 'hex').substr(0, 10).toUpperCase();
// console.log(stuff);
process.exit(0);
});
<!DOCTYPE html>
<html>
<head>
<title>Simple APP</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
To run this:
sudo npm i -g [email protected]
Navigate to folder where this is saved:
nw .
This should pop up the chrome devtools and display the error in the console.
I attempted this on a Mac.
{
"name": "simple-app",
"main": "index.html",
"dependencies": {
"bluebird": "^3.4.1",
"create-output-stream": "0.0.1",
"fs-extra": "^0.30.0",
"murmurhash-native": "^3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment