-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.js
executable file
·50 lines (39 loc) · 1.22 KB
/
build.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
#!/usr/bin/env node
/* eslint no-console: off */
import { promises as fs } from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
/**
* @param {"s"|"b"} mode
* @returns {Promise<void>}
*/
async function write (mode) {
let contents = `// File generated by build.js, do not edit
import blakejs from 'blakejs'
import { from } from 'multiformats/hashes/hasher'
import { bytes } from 'multiformats'
const { blake2${mode} } = blakejs
`
const bstart = 0xb201
const bend = 0xb240
const sstart = 0xb241
const send = 0xb260
for (let code = (mode === 'b' ? bstart : sstart); code <= (mode === 'b' ? bend : send); code++) {
const length = code - (mode === 'b' ? bstart : sstart) + 1
const bitLength = length * 8
contents += `
export const blake2${mode}${bitLength} = from({
name: 'blake2${mode}-${bitLength}',
code: 0x${code.toString(16)},
encode: (input) => bytes.coerce(blake2${mode}(input, undefined, ${length}))
})
`
}
return fs.writeFile(path.join(__dirname, `blake2${mode}.js`), contents, 'utf8')
}
Promise.all([write('b'), write('s')]).catch((err) => {
console.error(err)
process.exit(1)
})