-
Notifications
You must be signed in to change notification settings - Fork 43
/
postinstall.js
executable file
·167 lines (144 loc) · 3.88 KB
/
postinstall.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
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
#!/usr/bin/env node
/* eslint no-bitwise:0 */
/* eslint no-cond-assign:0 */
/* eslint no-use-before-define:0 */
"use strict";
var fs = require("fs");
var path = require("path");
var parse = require("url").parse;
var format = require("url").format;
var crypto = require("crypto");
var https = require("https");
var async = require("async");
var windows = process.platform === "win32";
var enclose = require("./bin/enclose.js");
var downloads = enclose.downloads();
var bucket = "https://enclosejs.s3.amazonaws.com";
process.stdout.write("Downloading precompiled binaries. Please wait ...\n");
async.mapSeries(downloads, function(download, cb) {
var name = download.name;
process.stdout.write("Downloading " + name + " ...");
var fsname = path.join(__dirname, "bin", name);
async.waterfall([
function(next) {
var signature = "";
function finish(error) {
next(error, signature);
}
var loc = location(".sha256");
https.get(loc, function(res) {
if (res.statusCode !== 200) {
return finish(new Error([
res.statusCode, loc
]));
}
res.on("data", function(chunk) {
signature += chunk.toString();
}).on("end", function() {
finish();
});
}).on("error", function(error) {
finish(error);
});
},
function(signature, next) {
var file = fs.createWriteStream(fsname);
var hash = crypto.createHash("sha256");
hash.setEncoding("hex");
var read = 0;
var thresholds = [
10, 20, 30, 40, 50, 60, 70, 80, 90
].map(function(t) {
return {
caption: t.toString() + "%",
value: t * download.size / 100 | 0
};
});
function progress(chunk) {
var was = read;
read += chunk.length;
thresholds.some(function(t) {
if (t.value > read) {
return true;
}
if (t.value >= was) {
process.stdout.write(" " + t.caption);
}
});
}
function remove(error) {
fs.unlink(fsname, function() {
next(error);
});
}
function end(back) {
file.end(function() {
hash.end(function() {
back();
});
});
}
function finish(error) {
end(function() {
if (error) {
return remove(error);
}
var have = hash.read();
if (have !== signature) {
return remove(new Error([
name, have, signature
]));
}
process.stdout.write("\n");
next();
});
}
file.on("open", function() {
var loc = location();
https.get(loc, function(res) {
if (res.statusCode !== 200) {
return finish(new Error([
res.statusCode, loc
]));
}
res.on("data", function(chunk) {
file.write(chunk);
hash.write(chunk);
progress(chunk);
}).on("end", function() {
finish();
});
}).on("error", function(error) {
finish(error);
});
});
},
function(next) {
if (windows) {
return next();
} else {
fs.stat(fsname, function(error, stat) {
if (error) return next(error);
var plusx = (stat.mode | 64 | 8).toString(8).slice(-3);
fs.chmod(fsname, plusx, next);
});
}
}
], cb);
function location(append) {
var url = bucket + "/" + name + (append || "");
var p = parse(url), key, key_part;
if (key = process.env.ENCLOSEJS_KEY) {
if (key_part = key.split(".")[0]) {
p.pathname = "/" + key_part + p.pathname;
url = format(p);
}
}
return url;
}
}, function(error) {
if (error) {
process.stdout.write("\n");
throw error;
}
});