-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
118 lines (98 loc) · 3.18 KB
/
install.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
/*
* Copyright 2018-2020 TON DEV SOLUTIONS LTD.
*
* Licensed under the SOFTWARE EVALUATION License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at:
*
* http://www.ton.dev/licenses
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific TON DEV software governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const http = require('http');
const zlib = require('zlib');
const {p, bv, bp} = require('./binaries');
let root = process.cwd();
const binariesHost = 'sdkbinaries-ws.tonlabs.io';
function downloadAndGunzip(dest, url) {
return new Promise((resolve, reject) => {
const request = http.get(url, response => {
if (response.statusCode !== 200) {
reject({
message: `Download from ${url} failed with ${response.statusCode}: ${response.statusMessage}`,
});
return;
}
let file = fs.createWriteStream(dest, { flags: "w" });
let opened = false;
const failed = (err) => {
if (file) {
file.close();
file = null;
fs.unlink(dest, () => {
});
reject(err);
}
};
const unzip = zlib.createGunzip();
unzip.pipe(file);
response.pipe(unzip);
request.on("error", err => {
failed(err);
});
file.on("finish", () => {
if (opened && file) {
resolve();
}
});
file.on("open", () => {
opened = true;
});
file.on("error", err => {
if (err.code === "EEXIST") {
file.close();
reject("File already exists");
} else {
failed(err);
}
});
});
});
}
async function dl(dst, src) {
const dst_path = path.join(root, dst);
const src_url = `http://${binariesHost}/${src}.gz`;
process.stdout.write(`Downloading from ${src_url} to ${dst_path} ...`);
await downloadAndGunzip(dst_path, src_url);
process.stdout.write('\n');
}
async function main() {
try {
const testFile = path.join(root, '__testwritable__.txt');
fs.writeFileSync(testFile, 'test writable', {encoding: 'utf8'});
fs.unlinkSync(testFile);
} catch {
root = bp;
if (!fs.existsSync(bp)) {
fs.mkdirSync(bp, { recursive: true });
}
}
await dl(`tonclient.node`, `tonclient_${bv}_nodejs_addon_${p}`);
if (p === 'darwin') {
await dl('libtonclientnodejs.dylib', `tonclient_${bv}_nodejs_dylib_${p}`);
}
}
(async () => {
try {
await main();
} catch (error) {
console.error(error);
process.exit(1);
}
})();