-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.js
163 lines (140 loc) · 4.06 KB
/
stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use strict";
var util = require("util");
var binding = require("./incremental");
var algorithms = {};
exports.getHashes = function () {
return Object.keys(binding)
.filter(function (name) {
return "function" === typeof binding[name];
})
.map(function (name) {
return name.toLowerCase();
});
};
Object.keys(binding).forEach(function (name) {
if (binding.hasOwnProperty(name) && "function" === typeof binding[name]) {
algorithms[name.toLowerCase()] = binding[name];
}
});
algorithms.murmurhash3a = algorithms.murmurhash32 = algorithms.murmurhash32x86 =
algorithms.murmurhash;
/* from nodejs lib/crypto.js */
var LazyTransform = require("./lazy_transform");
/**
* Creates and returns a MurmurHash object that can be used to generate murmurhash digests.
*
* Except murmur's `seed` and `endianness` options, the rest of the options are passed to
* stream.Transform constructor.
*
* @param {string|MurmurHash} algorithm|hasher - one of available algorithms
* or a murmur hasher instance
* @param {number|object} seed|options - hasher options
**/
exports.createHash = exports.MurmurHash = MurmurHash;
function MurmurHash(algorithm, options) {
var seed, endianness;
if (!(this instanceof MurmurHash)) return new MurmurHash(algorithm, options);
if (options && "object" === typeof options) {
seed = options.seed;
endianness = options.endianness;
} else {
seed = options;
options = undefined;
}
if (algorithm instanceof MurmurHash) {
this._handle = new algorithm._handle.constructor(
algorithm._handle,
endianness
);
} else if (algorithm) {
// handle object from json
if ("object" === typeof algorithm) {
seed = algorithm.seed;
algorithm = algorithm.type;
}
var Handle = algorithms[algorithm.toLowerCase()];
if (Handle) {
this._handle = new Handle(seed, endianness);
} else {
throw new Error("Algorithm not supported");
}
} else {
throw new TypeError(
"Must give algorithm string, a serialized state or a MurmurHash instance"
);
}
LazyTransform.call(this, options);
}
util.inherits(MurmurHash, LazyTransform);
MurmurHash.prototype._transform = function (chunk, encoding, callback) {
if (chunk.length < 8192) {
// this constant was chosen experimentally
this._handle.update(chunk, encoding);
callback();
} else {
this._handle.update(chunk, encoding, callback);
}
};
MurmurHash.prototype._flush = function (callback) {
this.push(this._handle.digest());
callback();
};
MurmurHash.prototype.update = function () {
var handle = this._handle;
return handle.update.apply(handle, arguments) && this;
};
MurmurHash.prototype.digest = function () {
var handle = this._handle;
return handle.digest.apply(handle, arguments);
};
MurmurHash.prototype.serialize = function (type, offset) {
return this._handle.serialize(type, offset);
};
MurmurHash.prototype.copy = function (target) {
this._handle.copy(target && target._handle);
return target;
};
MurmurHash.prototype.toJSON = function () {
var handle = this._handle;
return {
type: handle.constructor.name,
seed: handle.toJSON(),
};
};
Object.defineProperty(MurmurHash.prototype, "isBusy", {
get: function () {
return this._handle.isBusy;
},
enumerable: false,
configurable: false,
});
Object.defineProperty(MurmurHash.prototype, "total", {
get: function () {
return this._handle.total;
},
enumerable: true,
configurable: true,
});
Object.defineProperty(MurmurHash.prototype, "endianness", {
get: function () {
return this._handle.endianness;
},
set: function (value) {
this._handle.endianness = value;
},
enumerable: true,
configurable: false,
});
Object.defineProperty(MurmurHash.prototype, "SERIAL_BYTE_LENGTH", {
get: function () {
Object.defineProperty(this, "SERIAL_BYTE_LENGTH", {
enumerable: true,
writable: true,
configurable: true,
value: this._handle.SERIAL_BYTE_LENGTH,
});
return this.SERIAL_BYTE_LENGTH;
},
enumerable: true,
configurable: true,
});