-
Notifications
You must be signed in to change notification settings - Fork 0
/
incremental.d.ts
251 lines (241 loc) · 9.34 KB
/
incremental.d.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* `murmurhash-native/incremental` module.
*
* Example:
*
* ```ts
* import { MurmurHash128x64 } from "murmurhash-native/incremental"
*
* let hasher = new MurmurHash128x64(42)
* hasher.update("hash ")
* hasher.update("me!")
* console.log(hasher.digest("hex"))
* ```
*
* This module hosts native implementations of incremental murmur hashes.
*
* @module incremental
*/
/***/
import { Encoding, OutputType } from "./index";
export { Encoding, OutputType };
/** An endianness type for the murmur hash incremental utilities */
export type Endianness = "BE"|"LE"|"platform";
/** A common interface to all of the murmur hash incremental utilities. */
export interface IMurHasher {
/** Size in bytes of the serialized hasher. */
readonly SERIAL_BYTE_LENGTH: number;
/**
* Copies the internal state onto the target utility.
*
* This method does not alter target endianness.
*
* @param target a different instance of a MurmurHash utility of the same type.
* @returns target.
*/
copy(target: IMurHasher): IMurHasher;
/**
* Generates the murmur hash of all of the data provided so far.
*
* The order of bytes written to a Buffer or encoded string depends on
* endianness property.
*
* @param output a Buffer object to write hash bytes to; the same object will be returned.
* @param offset start writing into the output at offset byte;
* negative offset starts from the end of the output buffer.
* @param length a number of bytes to write from calculated hash;
* negative length starts from the end of the hash;
* if absolute value of length is larger than the size of a calculated
* hash, bytes are written only up to the hash size.
* @returns murmur hash.
*/
digest(output: Buffer, offset?: number, length?: number): Buffer;
/**
* Generates the murmur hash of all of the data provided so far.
*
* If outputType is not provided a new Buffer instance is returned.
*
* The order of bytes written to a Buffer or encoded string depends on
* endianness property.
*
* @param outputType indicates the form and encoding of the returned hash.
* @returns murmur hash.
*/
digest(outputType?: OutputType): number|string|Buffer;
/**
* Serializes the internal state of the murmur hash utility instance
*
* The returned type depends on the implementation.
**/
toJSON(): any;
/**
* Serializes the internal state of the murmur hash utility instance
* into the provided Buffer.
*
* When output has not enough space for the serialized data
* at the given offset it throws an Error. You may consult the required
* byte length reading constant: SERIAL_BYTE_LENGTH
*
* @param output a Buffer to write serialized state to.
* @param offset offset at output.
*/
serialize(output: Buffer, offset?: number): Buffer;
/**
* Serializes the internal state of the murmur hash utility instance.
*
* The serial is generated as a base64 encoded string.
*/
serialize(): string;
/**
* Updates a internal state with the given data asynchronously.
*
* If data is a Buffer then encoding is being ignored.
*
* The hash will be updated asynchronously using libuv worker queue.
*
* @param data a chunk of data to calculate hash from.
* @param encoding of the data provided as a string.
* @param callback will be called when asynchronous operation completes.
*/
update(data: string|Buffer, encoding: Encoding, callback: (err: Error) => void): void;
/**
* Updates a internal state with the given data asynchronously.
*
* If the data is a string, an encoding of "utf8" is assumed.
*
* The hash will be updated asynchronously using libuv worker queue.
*
* @param data a chunk of data to calculate hash from.
* @param callback will be called when asynchronous operation completes.
*/
update(data: string|Buffer, callback: (err: Error) => void): void;
/**
* Updates a internal state with the given data.
*
* If the data is a string and encoding is not explicitly provided,
* an encoding of "utf8" is being assumed.
*
* @param data a chunk of data to calculate hash from.
* @param encoding of the data provided as a string.
*/
update(data: string|Buffer, encoding?: Encoding): this;
/** Digest byte order. */
endianness: Endianness;
/**
* True if asynchronous update is in progress.
*
* When this property is true, trying to update, calculate digest, serialize or copy state will result in
* an error thrown from the related method.
*/
readonly isBusy: boolean;
/** The total (modulo 2^32) bytes of data provided so far. */
readonly total: number;
}
/** A factory interface for murmurhash incremental utility */
export interface IMurHasherConstructor {
/** Size in bytes of the serialized hasher. */
readonly SERIAL_BYTE_LENGTH: number;
/**
* Creates MurmurHash utility.
*
* The default seed is 0 and the endianness is set to "BE".
*/
new(): IMurHasher;
/**
* Creates MurmurHash utility.
*
* If not provided, the endianness is set to "BE".
*
* @param seed initial murmur hash seed as an unsigned 32-bit integer.
* @param endianness digest byte order: "BE", "LE" or "platform", optional. Default is "BE".
*/
new(seed: number, endianness?: Endianness): IMurHasher;
/**
* Creates MurmurHash utility.
*
* The initial state is taken from the serialized state. Throws an error if serial is incorrect.
*
* @param serial serialized state of the same MurmurHash type.
* @param endianness digest byte order: "BE", "LE" or "platform", optional. Default is "BE".
*/
new(serial: string|Buffer, endianness?: Endianness): IMurHasher;
/**
* Creates MurmurHash utility.
*
* The initial state is taken from another instance of murmur hash utility.
* Throws an error if incompatible hash is provided.
*
* @param hash an instance of another MurmurHash.
* @param endianness digest byte order: "BE", "LE" or "platform", optional. Default is hash.endianness.
*/
new(hash: IMurHasher, endianness?: Endianness): IMurHasher;
}
/** @hidden An abstract base class for the murmurhash incremental utility. */
declare abstract class IMurHasherBase implements IMurHasher {
/** Size in bytes of the serialized hasher. */
static readonly SERIAL_BYTE_LENGTH: number;
readonly SERIAL_BYTE_LENGTH: number;
/**
* Creates MurmurHash utility.
*
* The default seed is 0 and the endianness is set to "BE".
*/
constructor();
/**
* Creates MurmurHash utility.
*
* If not provided, the endianness is set to "BE".
*
* @param seed initial murmur hash seed as an unsigned 32-bit integer.
* @param endianness digest byte order: "BE", "LE" or "platform", optional. Default is "BE".
*/
constructor(seed: number, endianness?: Endianness);
/**
* Creates MurmurHash utility.
*
* The initial state is taken from the serialized state. Throws an error if serial is incorrect.
*
* @param serial serialized state of the same MurmurHash type.
* @param endianness digest byte order: "BE", "LE" or "platform", optional. Default is "BE".
*/
constructor(serial: string|Buffer, endianness?: Endianness);
/**
* Creates MurmurHash utility.
*
* The initial state is taken from another instance of murmur hash utility.
* Throws an error if incompatible hash is provided.
*
* @param hash an instance of another MurmurHash.
* @param endianness digest byte order: "BE", "LE" or "platform", optional. Default is hash.endianness.
*/
constructor(hash: IMurHasher, endianness?: Endianness);
copy(target: IMurHasher): IMurHasher;
digest(output: Buffer, offset?: number, length?: number): Buffer;
digest(outputType?: OutputType): number|string|Buffer;
toJSON(): string;
serialize(output: Buffer, offset?: number): Buffer;
serialize(): string;
update(data: string|Buffer, encoding: Encoding, callback: (err: Error) => void): void;
update(data: string|Buffer, callback: (err: Error) => void): void;
update(data: string|Buffer, encoding?: Encoding): this;
endianness: Endianness;
readonly isBusy: boolean;
readonly total: number;
}
/** A murmurhash32 implementation of the murmur hash incremental utility */
export class MurmurHash extends IMurHasherBase {}
/** A murmurhash128 (os arch) implementation of the murmur hash incremental utility */
export class MurmurHash128 extends IMurHasherBase {
digest(output: Buffer, offset?: number, length?: number): Buffer;
digest(outputType?: OutputType): string|Buffer;
}
/** A murmurhash128x64 implementation of the murmur hash incremental utility */
export class MurmurHash128x64 extends IMurHasherBase {
digest(output: Buffer, offset?: number, length?: number): Buffer;
digest(outputType?: OutputType): string|Buffer;
}
/** A murmurhash128x86 implementation of the murmur hash incremental utility */
export class MurmurHash128x86 extends IMurHasherBase {
digest(output: Buffer, offset?: number, length?: number): Buffer;
digest(outputType?: OutputType): string|Buffer;
}