forked from hugomrdias/rabin-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
75 lines (69 loc) · 1.77 KB
/
lib.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
import * as Rabin from "./gen/wasm.js"
let wait = Rabin.activate().then(() => {
wait = {
then: fn => fn(),
}
})
/**
* @param {number} bits
* @param {number} minSize
* @param {number} maxSize
* @param {number} windowSize
*/
export const create = (bits, minSize, maxSize, windowSize) =>
wait.then(() => {
const rabin = Rabin.create(bits, minSize, maxSize, windowSize)
rabin.maxSize = maxSize
rabin.minSize = minSize
rabin.windowSize = windowSize
return rabin
})
/**
* @param {number} bits
* @param {number} minSize
* @param {number} maxSize
* @param {number} windowSize
*/
export const createWithPolynom = (
polynom,
bits,
minSize,
maxSize,
windowSize
) =>
wait.then(() => {
const rabin = Rabin.createWithPolynomial(
polynom,
bits,
minSize,
maxSize,
windowSize
)
rabin.maxSize = maxSize
rabin.minSize = minSize
rabin.windowSize = windowSize
return rabin
})
/**
* @param {Rabin} rabin
* @param {Uint8Array} bytes
* @param {boolean} [end=false]
*/
export const cut = (rabin, bytes, end = false) =>
// If we have less then `maxSize` of bytes & it's not the end, there is no
// point to copy bytes into wasm as we'll get no chunks.
!end & (bytes.byteLength < rabin.maxSize)
? none
: Rabin.cut(rabin, bytes, end)
/**
* @param {Rabin} rabin
* @param {{length:number, copyTo:(target:Uint8Array, offset:number) => unknown}} buffer
* @param {boolean} [end=false]
*/
export const cutBuffer = (rabin, buffer, end = false) =>
// If we have less then `maxSize` of bytes & it's not the end, there is no
// point to copy bytes into wasm as we'll get no chunks.
!end & (buffer.byteLength < rabin.maxSize)
? none
: Rabin.cut_buffer(rabin, buffer, end)
const none = new Uint32Array(0)