-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.js
More file actions
executable file
·116 lines (102 loc) · 3.04 KB
/
setup.js
File metadata and controls
executable file
·116 lines (102 loc) · 3.04 KB
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
#!/usr/bin/env node
const os = require('os');
const path = require('path');
const https = require('https');
const fs = require('fs');
const targz = require('targz');
const shajs = require('sha.js');
function getSha256() {
return new Promise((resolve, reject) => {
const req = https.get('https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz.sha256', (resp) => {
if (resp.statusCode !== 200) {
reject(new Error(`Server responded with error code ${resp.statusCode}`));
return;
}
resp.on('data', (d) => {
resolve(d.toString().split(' ')[0]);
});
resp.on('error', (err) => {
reject(err);
});
});
if (req.on('error', (err) => {
reject(err);
}));
});
}
function download() {
// Use a temporary file to create while downloading
const targetFile = path.resolve(os.tmpdir(), 'dynamodb.tar.gz');
// First retrieve the sha256 sum
const cache = getSha256().then(sha => new Promise((resolve) => {
// If we have an existing file, see if the sha match
if (fs.existsSync(targetFile)) {
const sha256 = shajs('sha256');
const inp = fs.createReadStream(targetFile);
inp.on('data', (chunk) => {
sha256.update(chunk);
});
inp.on('end', () => {
if (sha256.digest('hex') === sha) {
resolve(true);
} else {
resolve(sha);
}
});
} else {
resolve(sha);
}
}));
// Get the archive file
const archive = cache.then((isCached) => {
// isCached could be a shasum
if (isCached === true) {
return targetFile;
}
return new Promise((resolve, reject) => {
// Download the file
const file = fs.createWriteStream(targetFile);
https.get('https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz', (resp) => {
if (resp.statusCode !== 200) {
reject(new Error(`Server responded with ${resp.statusCode}`));
return;
}
const contentLength = resp.headers['content-length'];
let total = 0;
resp.pipe(file);
process.stdout.write(`Downloading ${Math.round(contentLength / 1024 / 1024, 1)}MB ...0`);
let progress = 5;
resp.on('data', (d) => {
total += d.length;
const p = Math.floor((total / contentLength) * 100);
if (p >= progress) {
process.stdout.write(`...${p}`);
progress += 5;
}
});
resp.on('end', () => {
process.stdout.write('\n');
resolve(targetFile);
});
resp.on('error', (err) => {
process.stdout.write('...Failed\n');
reject(err);
});
});
});
});
archive.then(() => new Promise((resolve, reject) => {
process.stdout.write('Extracting binaries.\n');
targz.decompress({
src: targetFile,
dest: path.resolve(__dirname, '.dynamodb'),
}, (err) => {
if (err) {
return reject(err);
}
return resolve(true);
});
}));
return archive;
}
download();