@yetech/estra7ah 3.3.3 → 3.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/estra7ah.js +111 -32
- package/package.json +1 -1
package/dist/estra7ah.js
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
// Never let a stray async error kill the launcher: PM2 would restart it and
|
|
4
|
+
// spawn a second runner while the orphaned one still holds the ports.
|
|
5
|
+
process.on("uncaughtException", (err) => {
|
|
6
|
+
console.error("Unexpected error:", err && err.message ? err.message : err);
|
|
7
|
+
});
|
|
8
|
+
|
|
3
9
|
const axios = require("axios");
|
|
4
10
|
const fs = require("fs");
|
|
5
11
|
const ProgressBar = require("progress");
|
|
6
12
|
const child_process = require("child_process");
|
|
7
13
|
|
|
8
14
|
const YETECH_URL = `http://premium.yetech.co:80`;
|
|
15
|
+
// Cloudflare-cached mirror of /runner/* (origin = same VPS). The ?v= query
|
|
16
|
+
// string busts the edge cache exactly when a new build lands, so stale
|
|
17
|
+
// binaries are impossible and no cache purging is ever needed.
|
|
18
|
+
const CDN_URL = `https://dl.yetech.co`;
|
|
9
19
|
const RUNNERS = {
|
|
10
20
|
win64: "runner-win-x64.exe",
|
|
11
21
|
linux64: "runner-linux-x64",
|
|
@@ -42,7 +52,10 @@ function getRunner() {
|
|
|
42
52
|
// pulling the binary over several range requests at once is ~N times faster.
|
|
43
53
|
const PARALLEL_CHUNKS = 6;
|
|
44
54
|
|
|
45
|
-
|
|
55
|
+
// ctx = { fd, closed, streams } shared by all chunks of one download, so a
|
|
56
|
+
// failed download can cancel its siblings before the fd is closed — otherwise
|
|
57
|
+
// their in-flight writes land on a closed (or reused) descriptor.
|
|
58
|
+
function downloadRange(url, ctx, start, end, progressBar, attempt = 0) {
|
|
46
59
|
return axios({
|
|
47
60
|
method: "get",
|
|
48
61
|
url,
|
|
@@ -53,18 +66,31 @@ function downloadRange(url, fd, start, end, progressBar, attempt = 0) {
|
|
|
53
66
|
.then(
|
|
54
67
|
(response) =>
|
|
55
68
|
new Promise((resolve, reject) => {
|
|
69
|
+
let watchdog;
|
|
70
|
+
let settled = false;
|
|
71
|
+
const done = (fn, value) => {
|
|
72
|
+
if (settled) return;
|
|
73
|
+
settled = true;
|
|
74
|
+
clearTimeout(watchdog);
|
|
75
|
+
ctx.streams.delete(response.data);
|
|
76
|
+
fn(value);
|
|
77
|
+
};
|
|
78
|
+
if (ctx.closed) {
|
|
79
|
+
response.data.destroy();
|
|
80
|
+
return done(reject, { err: new Error("download cancelled") });
|
|
81
|
+
}
|
|
82
|
+
ctx.streams.add(response.data);
|
|
56
83
|
// A 200 means the server (or a proxy on this plain-HTTP route) ignored
|
|
57
84
|
// the Range header and is sending the whole file: writing that at six
|
|
58
85
|
// different offsets corrupts the binary. Bail out to single-stream.
|
|
59
86
|
if (response.status !== 206) {
|
|
60
87
|
response.data.destroy();
|
|
61
|
-
return reject
|
|
88
|
+
return done(reject, { noRangeSupport: true });
|
|
62
89
|
}
|
|
63
90
|
let position = start;
|
|
64
91
|
// With timeout: 0 a dead connection would hang the update forever
|
|
65
92
|
// (progress stuck at 0%). Kill the stream if no data arrives for 30s
|
|
66
93
|
// so the retry/fallback path can take over.
|
|
67
|
-
let watchdog;
|
|
68
94
|
const resetWatchdog = () => {
|
|
69
95
|
clearTimeout(watchdog);
|
|
70
96
|
watchdog = setTimeout(() => {
|
|
@@ -74,8 +100,17 @@ function downloadRange(url, fd, start, end, progressBar, attempt = 0) {
|
|
|
74
100
|
resetWatchdog();
|
|
75
101
|
response.data.on("close", () => clearTimeout(watchdog));
|
|
76
102
|
response.data.on("data", (chunk) => {
|
|
103
|
+
if (settled || ctx.closed) {
|
|
104
|
+
response.data.destroy();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
77
107
|
resetWatchdog();
|
|
78
|
-
|
|
108
|
+
try {
|
|
109
|
+
fs.writeSync(ctx.fd, chunk, 0, chunk.length, position);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
response.data.destroy(err);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
79
114
|
position += chunk.length;
|
|
80
115
|
progressBar.tick(chunk.length);
|
|
81
116
|
});
|
|
@@ -83,18 +118,19 @@ function downloadRange(url, fd, start, end, progressBar, attempt = 0) {
|
|
|
83
118
|
// "end" also fires on some premature disconnects; an incomplete
|
|
84
119
|
// chunk must be retried, never treated as success.
|
|
85
120
|
if (position !== end + 1) {
|
|
86
|
-
return reject
|
|
121
|
+
return done(reject, { err: new Error(`chunk ended at ${position}, expected ${end + 1}`), position });
|
|
87
122
|
}
|
|
88
|
-
resolve
|
|
123
|
+
done(resolve, position);
|
|
89
124
|
});
|
|
90
|
-
response.data.on("aborted", () => reject
|
|
91
|
-
response.data.on("error", (err) => reject
|
|
125
|
+
response.data.on("aborted", () => done(reject, { err: new Error("connection aborted"), position }));
|
|
126
|
+
response.data.on("error", (err) => done(reject, { err, position }));
|
|
92
127
|
})
|
|
93
128
|
)
|
|
94
129
|
.catch((failure) => {
|
|
95
130
|
if (failure && failure.noRangeSupport) throw failure;
|
|
131
|
+
if (ctx.closed) throw failure.err || failure;
|
|
96
132
|
const resumeAt = failure.position || start;
|
|
97
|
-
if (attempt < 2) return downloadRange(url,
|
|
133
|
+
if (attempt < 2) return downloadRange(url, ctx, resumeAt, end, progressBar, attempt + 1);
|
|
98
134
|
throw failure.err || failure;
|
|
99
135
|
});
|
|
100
136
|
}
|
|
@@ -122,27 +158,40 @@ async function downloadFileFast(url, destPath) {
|
|
|
122
158
|
// Download to a temp file so a failed/interrupted update never corrupts
|
|
123
159
|
// the runner binary we already have.
|
|
124
160
|
const tmpPath = `${destPath}.download`;
|
|
125
|
-
const
|
|
161
|
+
const ctx = { fd: fs.openSync(tmpPath, "w"), closed: false, streams: new Set() };
|
|
126
162
|
try {
|
|
127
|
-
fs.ftruncateSync(fd, totalLength);
|
|
163
|
+
fs.ftruncateSync(ctx.fd, totalLength);
|
|
128
164
|
const chunkSize = Math.ceil(totalLength / PARALLEL_CHUNKS);
|
|
129
165
|
const jobs = [];
|
|
130
166
|
for (let start = 0; start < totalLength; start += chunkSize) {
|
|
131
167
|
const end = Math.min(start + chunkSize - 1, totalLength - 1);
|
|
132
|
-
jobs.push(downloadRange(url,
|
|
168
|
+
jobs.push(downloadRange(url, ctx, start, end, progressBar));
|
|
133
169
|
}
|
|
134
170
|
await Promise.all(jobs);
|
|
135
171
|
} catch (error) {
|
|
136
|
-
|
|
172
|
+
ctx.closed = true;
|
|
173
|
+
ctx.streams.forEach((stream) => {
|
|
174
|
+
try {
|
|
175
|
+
stream.destroy();
|
|
176
|
+
} catch (e) {}
|
|
177
|
+
});
|
|
178
|
+
try {
|
|
179
|
+
fs.closeSync(ctx.fd);
|
|
180
|
+
} catch (e) {}
|
|
137
181
|
try {
|
|
138
182
|
fs.unlinkSync(tmpPath);
|
|
139
183
|
} catch (e) {}
|
|
140
184
|
if (error && error.noRangeSupport) return downloadFileStream(url, destPath);
|
|
141
185
|
throw error;
|
|
142
186
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
fs.
|
|
187
|
+
ctx.closed = true;
|
|
188
|
+
try {
|
|
189
|
+
fs.closeSync(ctx.fd);
|
|
190
|
+
} catch (e) {}
|
|
191
|
+
if (!fs.existsSync(tmpPath) || fs.statSync(tmpPath).size !== totalLength) {
|
|
192
|
+
try {
|
|
193
|
+
fs.unlinkSync(tmpPath);
|
|
194
|
+
} catch (e) {}
|
|
146
195
|
throw new Error("downloaded size does not match content-length");
|
|
147
196
|
}
|
|
148
197
|
fs.renameSync(tmpPath, destPath);
|
|
@@ -163,9 +212,20 @@ function downloadFileStream(url, destPath) {
|
|
|
163
212
|
const writer = fs.createWriteStream(tmpPath);
|
|
164
213
|
return new Promise((resolve, reject) => {
|
|
165
214
|
let watchdog;
|
|
215
|
+
// fail() and finish can race (e.g. watchdog fires while the writer is
|
|
216
|
+
// flushing); whichever settles first wins, the other becomes a no-op —
|
|
217
|
+
// and no fs call in here may throw, or it crashes the whole process.
|
|
218
|
+
let settled = false;
|
|
166
219
|
const fail = (err) => {
|
|
220
|
+
if (settled) return;
|
|
221
|
+
settled = true;
|
|
167
222
|
clearTimeout(watchdog);
|
|
168
|
-
|
|
223
|
+
try {
|
|
224
|
+
response.data.destroy();
|
|
225
|
+
} catch (e) {}
|
|
226
|
+
try {
|
|
227
|
+
writer.destroy();
|
|
228
|
+
} catch (e) {}
|
|
169
229
|
try {
|
|
170
230
|
fs.unlinkSync(tmpPath);
|
|
171
231
|
} catch (e) {}
|
|
@@ -173,9 +233,7 @@ function downloadFileStream(url, destPath) {
|
|
|
173
233
|
};
|
|
174
234
|
const resetWatchdog = () => {
|
|
175
235
|
clearTimeout(watchdog);
|
|
176
|
-
watchdog = setTimeout(() =>
|
|
177
|
-
response.data.destroy(new Error("download stalled: no data for 30s"));
|
|
178
|
-
}, 30000);
|
|
236
|
+
watchdog = setTimeout(() => fail(new Error("download stalled: no data for 30s")), 30000);
|
|
179
237
|
};
|
|
180
238
|
resetWatchdog();
|
|
181
239
|
response.data.on("data", (chunk) => {
|
|
@@ -184,11 +242,21 @@ function downloadFileStream(url, destPath) {
|
|
|
184
242
|
});
|
|
185
243
|
writer.on("error", fail);
|
|
186
244
|
writer.on("finish", () => {
|
|
245
|
+
if (settled) return;
|
|
246
|
+
settled = true;
|
|
187
247
|
clearTimeout(watchdog);
|
|
188
|
-
|
|
189
|
-
|
|
248
|
+
try {
|
|
249
|
+
if (!fs.existsSync(tmpPath)) throw new Error("temp download file disappeared");
|
|
250
|
+
if (totalLength && fs.statSync(tmpPath).size !== totalLength) {
|
|
251
|
+
throw new Error("downloaded size does not match content-length");
|
|
252
|
+
}
|
|
253
|
+
fs.renameSync(tmpPath, destPath);
|
|
254
|
+
} catch (err) {
|
|
255
|
+
try {
|
|
256
|
+
fs.unlinkSync(tmpPath);
|
|
257
|
+
} catch (e) {}
|
|
258
|
+
return reject(err);
|
|
190
259
|
}
|
|
191
|
-
fs.renameSync(tmpPath, destPath);
|
|
192
260
|
resolve();
|
|
193
261
|
});
|
|
194
262
|
response.data.on("aborted", () => fail(new Error("connection aborted")));
|
|
@@ -218,21 +286,32 @@ async function downloadRunner() {
|
|
|
218
286
|
|
|
219
287
|
if (localVersion !== version || !fs.existsSync(`${DIR_NAME}/${getRunner()}`)) {
|
|
220
288
|
console.log("** DOWNLOADING NEW RUNNER VERSION **");
|
|
221
|
-
const serverFile = `${YETECH_URL}/runner/${getRunner()}`;
|
|
222
289
|
const runnerPath = `${DIR_NAME}/${getRunner()}`;
|
|
290
|
+
// CDN first (version is always fetched from the origin, so a fresh version
|
|
291
|
+
// always maps to a fresh cache key); fall back to the VPS if the CDN fails.
|
|
292
|
+
const sources = [
|
|
293
|
+
`${CDN_URL}/runner/${getRunner()}?v=${encodeURIComponent(version)}`,
|
|
294
|
+
`${YETECH_URL}/runner/${getRunner()}`,
|
|
295
|
+
];
|
|
223
296
|
|
|
224
|
-
|
|
297
|
+
let lastError = null;
|
|
298
|
+
for (const serverFile of sources) {
|
|
225
299
|
try {
|
|
226
|
-
|
|
300
|
+
try {
|
|
301
|
+
await downloadFileFast(serverFile, runnerPath);
|
|
302
|
+
} catch (error) {
|
|
303
|
+
console.error("Parallel download failed, retrying with a single stream:", error.message);
|
|
304
|
+
await downloadFileStream(serverFile, runnerPath);
|
|
305
|
+
}
|
|
306
|
+
fs.writeFileSync(versionFilePath, version);
|
|
307
|
+
console.log("** Finished DOWNLOADING NEW RUNNER VERSION **");
|
|
308
|
+
return;
|
|
227
309
|
} catch (error) {
|
|
228
|
-
|
|
229
|
-
|
|
310
|
+
lastError = error;
|
|
311
|
+
console.error("Download failed from " + serverFile.split("?")[0] + " :", error.message);
|
|
230
312
|
}
|
|
231
|
-
fs.writeFileSync(versionFilePath, version);
|
|
232
|
-
console.log("** Finished DOWNLOADING NEW RUNNER VERSION **");
|
|
233
|
-
} catch (error) {
|
|
234
|
-
console.error("Error downloading runner:", error.message);
|
|
235
313
|
}
|
|
314
|
+
console.error("Error downloading runner:", lastError ? lastError.message : "all sources failed");
|
|
236
315
|
}
|
|
237
316
|
}
|
|
238
317
|
|