-
Notifications
You must be signed in to change notification settings - Fork 9
/
bootstrap.ts
109 lines (92 loc) · 3.12 KB
/
bootstrap.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
/*tslint:disable:no-console*/
/**
* Script to download hunspell wasm binary from https://github.com/kwonoj/docker-hunspell-wasm.
*/
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import { exec, mkdir, rm } from 'shelljs';
import { promisify } from 'util';
//tslint:disable-next-line: no-require-imports no-var-requires
const { config } = require('./package.json');
// Package.json defines `hunspell-version` under `config` section to find corresponding release version
const version = config['hunspell-version'];
const readFile = promisify(fs.readFile);
const asyncExec = promisify(exec);
/**
* Generate sha512 checksum from given string.
*/
const calculateChecksumFromFile = async (filePath: string) =>
crypto
.createHash('sha512')
.update(await readFile(filePath))
.digest('hex');
/**
* Get remote release checksum.
*/
const getRemoteChecksum = (url: string) => {
const { stdout } = exec(`wget -qO- ${url}.sha512`, { silent: true });
return (stdout as string).slice(0, (stdout as string).indexOf(' '));
};
/**
* Compare checksum of given file between remote.
*/
const validateBinaries = async (binaryFiles: Array<{ url: string; localBinaryPath: string }>) => {
for (const binaryFile of binaryFiles) {
const { url, localBinaryPath } = binaryFile;
//Create checksum validator
const remoteChecksum = getRemoteChecksum(url);
const validateBinary = async () => (await calculateChecksumFromFile(localBinaryPath)) === remoteChecksum;
const isBinaryExists = () => fs.existsSync(localBinaryPath);
if (isBinaryExists() && (await validateBinary())) {
continue;
} else {
return false;
}
}
return true;
};
/**
* Actually download binary from remote. This is direct invocation to wget, need local wget installation.
*
*/
const downloadSingleBinary = async (
libPath: string,
binaryFile: { url: string; binaryType: string; localBinaryPath: string }
) => {
const { url, binaryType, localBinaryPath } = binaryFile;
const outPath = path.join(libPath, binaryType);
mkdir(outPath);
await asyncExec(`wget -O ${localBinaryPath} ${url}`);
if (!validateBinaries([binaryFile])) {
throw new Error(`Downloaded binary checksum mismatch, cannot complete bootstrap`);
}
};
/**
* Main script execution
*/
(async () => {
try {
const libPath = path.resolve('./src/lib');
const binaryFiles = ['node', 'browser'].map((binaryType) => {
const fileName = `hunspell_${binaryType}.js`;
return {
url: `https://github.com/kwonoj/docker-hunspell-wasm/releases/download/${version}/${fileName}`,
localBinaryPath: path.join(libPath, binaryType, 'hunspell.js'),
binaryType,
type: path.extname(fileName) === '.js' ? 'hex' : 'binary',
};
});
const isBinaryValid = await validateBinaries(binaryFiles);
if (!isBinaryValid) {
rm('-rf', libPath);
mkdir(libPath);
console.log(`Downloading hunspell wasm binary version '${version}'`);
for (const singleFile of binaryFiles) {
await downloadSingleBinary(libPath, singleFile);
}
}
} catch (e) {
console.log(e);
}
})();