-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathplatform-target.js
217 lines (187 loc) · 5.08 KB
/
platform-target.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const http = require("https");
const fs = require("fs");
const os = require("os");
const { gunzip } = require("zlib");
const { bugs, version, name, repository } = require("./package.json");
const { debuglog, promisify } = require("util");
const PLATFORMS = [
{
TYPE: "Windows_NT",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-pc-windows-msvc",
},
{
TYPE: "Linux",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-unknown-linux-gnu",
},
{
TYPE: "Darwin",
ARCHITECTURE: "x64",
RUST_TARGET: "x86_64-apple-darwin",
},
{
TYPE: "Darwin",
ARCHITECTURE: "arm64",
RUST_TARGET: "aarch64-apple-darwin",
},
];
const BINARY_NAME = "index.node";
const log = debuglog("sandbox");
function error(message) {
console.error(`💥 ${message}`);
process.exit(1);
}
function getPlatform() {
const type = os.type();
const arch = os.arch();
for (let platform of PLATFORMS) {
if (type === platform.TYPE && arch === platform.ARCHITECTURE) {
return platform;
}
}
error(
`Platform "${type}" and architecture "${arch}" are not supported by ${name}. Please file an issue: ${bugs.url}`,
);
}
/**
* @callback BufferCallback
* @param {any} err - Error, if `undefined` the operation was successful.
* @param {Buffer} buffer - Result of operation.
* @returns {void}
*/
/**
* Download a given URL to a Buffer.
*
* @param {string} url - URL to download.
* @param {BufferCallback} callback - Buffer containing downloaded file.
* @returns {void}
*/
function download(url, callback) {
log("download: %s", url);
const chunks = [];
http
.get(url, function (response) {
log("download.response: %d", response.statusCode);
if (
response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers &&
response.headers.location
) {
download(response.headers.location, callback);
return;
}
if (response.statusCode !== 200) {
process.nextTick(callback, `Status ${response.statusCode} for ${url}`);
return;
}
response.on("data", function (chunk) {
chunks.push(chunk);
});
response.on("end", function () {
process.nextTick(callback, undefined, Buffer.concat(chunks));
});
response.on("error", callback);
})
.on("error", function (err) {
log("download.error: %O", err);
process.nextTick(callback, err);
});
}
/**
* Decompress a given Buffer.
*
* @param {Buffer} buffer - Compressed data.
* @param {BufferCallback} callback - Buffer containing decompressed data.
* @returns {void}
*/
function decompress(buffer, callback) {
log("decompress");
gunzip(buffer, function (err, decompressedBuffer) {
if (err) {
log("decompress.error: %O", err);
process.nextTick(callback, err);
return;
}
process.nextTick(callback, undefined, decompressedBuffer);
});
}
/**
* Save a buffer to the filesystem.
*
* @param {Buffer} buffer - File contents.
* @param {string} filename - File path and name.
* @param {Function} callback - Called with the saved filename if sucessful.
* @returns {void}
*/
function save(buffer, filename, callback) {
log("save: %s", filename);
const stream = fs.createWriteStream(filename);
const done = function () {
stream.end();
process.nextTick(callback, undefined, filename);
};
if (stream.write(buffer)) {
done();
return;
} else {
stream.once("drain", done);
}
}
/**
* Verify installation.
*
* @param {string} filename - File to verify.
* @param {Function} callback - Called with `true` to indicate the installation
* has been verified.
* @returns {void}
*/
function verify(filename, callback) {
log("verify: %s", filename);
fs.existsSync(filename)
? process.nextTick(callback, undefined, true)
: process.nextTick(callback, true);
}
/**
* Determine the current platform and attempt to download and install a
* pre-built binary.
*
* NOTE: The download will be skipped if an environment variable
* `SANDBOX_SKIP_DOWNLOAD` exists.
*
* @returns {void}
*/
async function install() {
if (process.env.SANDBOX_SKIP_DOWNLOAD) {
log("skipping install");
return;
}
const platform = getPlatform();
log("install: %O", platform);
if (fs.existsSync(BINARY_NAME)) {
log("install.exists: %s", BINARY_NAME);
console.log(`🎊 ${name} already downloaded!`);
return;
}
const repositoryUrl = repository.url
.replace("git+https", "https")
.replace(`${name}.git`, name);
const url = `${repositoryUrl}/releases/download/v${version}/${name}-v${version}-${platform.RUST_TARGET}.gz`;
log("install.url: %s", url);
try {
const buffer = await promisify(download)(url);
const binary = await promisify(decompress)(buffer);
const filename = await promisify(save)(binary, BINARY_NAME);
const verified = await promisify(verify)(filename);
if (!verified) {
throw new Error(`Unable to verify ${filename}`);
}
console.log(`🥂 Successfully downloaded ${name} ${version}`);
} catch (err) {
error(err);
}
}
module.exports = {
install,
};