-
Notifications
You must be signed in to change notification settings - Fork 7
/
engine.js
188 lines (159 loc) · 4.76 KB
/
engine.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
const { pipeline } = require('stream/promises');
const { execFile } = require('child_process');
const { join, basename } = require('path');
const { createHash } = require('crypto');
const lock = require('proper-lockfile');
const EventEmitter = require('events');
const extract = require('extract-zip');
const { rimraf } = require('rimraf');
const fs = require('fs');
const { request, download } = require('./utils');
const { INVALID_ENGINE_ERROR } = require('./constants');
module.exports = class EngineService extends EventEmitter {
/**
* Create an instance of the `EngineService` class.
*
* @param {any} options - remote control options object.
* @constructor
*/
constructor(options) {
super();
this.options = options;
}
/**
* Asynchronously start the engine service with the specified port.
*
* @param {number} port - selected port number.
*/
async start(port) {
const zipFile = join(this.zipDir, `FastExecuteScriptProtected.x${ARCH}.zip`);
if (this.metadata && fs.existsSync(zipFile)) {
if (this.metadata.checksum !== await checksum(zipFile)) {
fs.rmSync(this.zipDir, { recursive: true });
}
}
if (!fs.existsSync(this.zipDir)) {
this.emit('beforeDownload');
fs.mkdirSync(this.zipDir, { recursive: true });
await this._downloadExecutable(zipFile);
}
if (!fs.existsSync(this.exeDir)) {
this.emit('beforeExtract');
fs.mkdirSync(this.exeDir, { recursive: true });
await this._extractExecutable(zipFile);
}
this._runEngineProcess(port);
this._clearRunDirectory();
}
/**
*
* @returns {Promise}
*/
async initialize() {
await request(`${SCRIPTS_URL}/${this.options.scriptName}/properties`)
.then((data) => {
if (!data.success) {
throw new Error('Script with selected name not exist');
}
if (!supported(data.engversion)) {
throw new Error('Script engine not supported (Required 22.4.2 or newer)');
}
return data;
})
.then((data) => {
this.exeDir = join(this._scriptDir, data.hash.slice(0, 5));
this.zipDir = join(this._engineDir, data.engversion);
});
this.metadata = await request(
`${DISTR_URL}/FastExecuteScriptProtected${ARCH}/${basename(
this.zipDir
)}/FastExecuteScriptProtected.x${ARCH}.zip.meta.json`
).then((result) => ({ url: result.Url, chunks: result.Chunks, checksum: result.Checksum }));
}
/**
* Download engine executable.
* @private
* @returns {Promise}
*/
_downloadExecutable(zipPath) {
return download(this.metadata.url, zipPath);
}
/**
* Extract engine executable.
* @private
* @returns {Promise}
*/
_extractExecutable(zipPath) {
return extract(zipPath, { dir: this.exeDir });
}
setWorkingFolder(folder) {
this._scriptDir = join(folder, 'run', this.options.scriptName);
this._engineDir = join(folder, 'engine');
}
_runEngineProcess(port) {
this._process = execFile(
join(this.exeDir, 'FastExecuteScript.exe'),
[`--remote-control-port=${port}`, '--remote-control', ...this.options.args],
{ cwd: this.exeDir },
(error) => {
if (error && error.code && error.code > 1) {
throw new Error(`Unable to start engine process (code: ${error.code})\n${INVALID_ENGINE_ERROR}`);
}
},
);
this._lock();
}
_clearRunDirectory() {
fs.readdirSync(this._scriptDir, { withFileTypes: true }).forEach((dirent) => {
if (dirent.isDirectory()) {
const path = join(this._scriptDir, dirent.name);
if (!lock.checkSync(join(path, '.lock'))) {
rimraf.sync(path);
}
}
});
}
_getLockPath() {
return join(this.exeDir, '.lock');
}
_lock() {
try {
fs.writeFileSync(this._getLockPath(), '');
lock.lockSync(this._getLockPath());
} catch (error) {
// ignore
}
}
/**
* [description]
* @returns {Promise}
*/
async close() {
if (!this._process) return;
await lock.unlock(this._getLockPath())
.finally(() => {
this._process.kill();
});
}
};
const checksum = async (file) => {
const input = fs.createReadStream(file);
const hash = createHash('sha1');
await pipeline(input, hash);
return hash.digest('hex');
};
const supported = (actual) => {
const minimal = '22.4.2';
const [majorA, minorA, patchA] = actual.split('.').map(Number);
const [majorB, minorB, patchB] = minimal.split('.').map(Number);
if (majorA !== majorB) {
return majorA > majorB;
}
if (minorA !== minorB) {
return minorA > minorB;
}
return patchA >= patchB;
};
const DISTR_URL = 'https://bablosoft.com/distr';
const SCRIPTS_URL = 'https://bablosoft.com/scripts';
const ARCH = process.arch.includes('32') ? '32' : '64';