gerdur 1.0.0 → 1.0.2
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/CHANGELOG.md +7 -0
- package/dist/package.json +2 -3
- package/dist/src/gerdur.js +27 -0
- package/dist/src/lib/decrypt.d.ts +2 -1
- package/dist/src/lib/decrypt.js +12 -109
- package/dist/src/lib/download-track.d.ts +13 -1
- package/dist/src/lib/download-track.js +27 -9
- package/package.json +2 -3
- package/dist/src/hoenir.d.ts +0 -2
- package/dist/src/hoenir.js +0 -345
- package/dist/src/kyrie.d.ts +0 -2
- package/dist/src/kyrie.js +0 -345
- package/dist/src/lib/decrypt-pool.d.ts +0 -2
- package/dist/src/lib/decrypt-pool.js +0 -122
- package/dist/src/lib/decrypt-worker.d.ts +0 -1
- package/dist/src/lib/decrypt-worker.js +0 -17
- package/dist/src/q-fi.d.ts +0 -2
- package/dist/src/q-fi.js +0 -345
package/dist/src/kyrie.d.ts
DELETED
package/dist/src/kyrie.js
DELETED
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
-
};
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
const os_1 = require("os");
|
|
8
|
-
const fs_1 = require("fs");
|
|
9
|
-
const path_1 = require("path");
|
|
10
|
-
const commander_1 = require("commander");
|
|
11
|
-
const gradient_string_1 = __importDefault(require("gradient-string"));
|
|
12
|
-
const kyrie_core_1 = require("kyrie-core");
|
|
13
|
-
const prompts_1 = __importDefault(require("prompts"));
|
|
14
|
-
const log_update_1 = __importDefault(require("log-update"));
|
|
15
|
-
const p_queue_1 = __importDefault(require("p-queue"));
|
|
16
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
17
|
-
const true_case_path_1 = require("true-case-path");
|
|
18
|
-
const signale_1 = __importDefault(require("./lib/signale"));
|
|
19
|
-
const download_track_1 = __importDefault(require("./lib/download-track"));
|
|
20
|
-
const config_1 = __importDefault(require("./lib/config"));
|
|
21
|
-
const update_check_1 = __importDefault(require("./lib/update-check"));
|
|
22
|
-
const auto_updater_1 = __importDefault(require("./lib/auto-updater"));
|
|
23
|
-
const arl_setup_1 = require("./lib/arl-setup");
|
|
24
|
-
const util_1 = require("./lib/util");
|
|
25
|
-
const package_json_1 = __importDefault(require("../package.json"));
|
|
26
|
-
// App info
|
|
27
|
-
console.log((0, gradient_string_1.default)('red', 'yellow', 'orange')(` ♥ kyrie - ${package_json_1.default.version} ♥ `) +
|
|
28
|
-
'\n' +
|
|
29
|
-
(0, gradient_string_1.default)('orange', 'yellow', 'red')(' ──────────────────────────────────────────────') +
|
|
30
|
-
'\n' +
|
|
31
|
-
(0, gradient_string_1.default)('red', 'yellow', 'orange')(' │ repo https://github.com/soulwax/kyrie │ ') +
|
|
32
|
-
'\n' +
|
|
33
|
-
(0, gradient_string_1.default)('red', 'yellow', 'orange')(' │ github https://github.com/soulwax │ ') +
|
|
34
|
-
'\n' +
|
|
35
|
-
(0, gradient_string_1.default)('red', 'yellow', 'orange')(' ──────────────────────────────────────────────'));
|
|
36
|
-
const cmd = new commander_1.Command()
|
|
37
|
-
.option('-q, --quality <quality>', 'The quality of the files to download: 128/320/flac ')
|
|
38
|
-
.option('-o, --output <template>', 'Output filename template')
|
|
39
|
-
.option('-u, --url <url>', 'Deezer album/artist/playlist/track url')
|
|
40
|
-
.option('-i, --input-file <file>', 'Downloads all urls listed in text file')
|
|
41
|
-
.option('-c, --concurrency <number>', 'Download concurrency for album, artists and playlist')
|
|
42
|
-
.option('-a, --set-arl <string>', 'Set arl cookie')
|
|
43
|
-
.option('-s, --setup', 'Run guided first-time setup (enter your arl cookie)')
|
|
44
|
-
.option('--experimental-login', 'Deprecated: email/password login is now offered by default')
|
|
45
|
-
.option('-w, --overwrite', 'Re-download and overwrite files that already exist')
|
|
46
|
-
.option('-d, --headless', 'Run in headless mode for scripting automation', false)
|
|
47
|
-
.option('-conf, --config-file <file>', 'Custom location to your config file', 'kyrie.config.json')
|
|
48
|
-
.option('-rfp, --resolve-full-path', 'Use absolute path for playlists')
|
|
49
|
-
.option('-cp, --create-playlist', 'Force create a playlist file for non playlists');
|
|
50
|
-
if (process.pkg) {
|
|
51
|
-
cmd.option('-U, --update', 'Update this program to latest version');
|
|
52
|
-
}
|
|
53
|
-
const options = cmd.parse(process.argv).opts();
|
|
54
|
-
// Support a bare `kyrie setup` subcommand as an alias for `--setup`.
|
|
55
|
-
if (cmd.args[0] && cmd.args[0].toLowerCase() === 'setup') {
|
|
56
|
-
options.setup = true;
|
|
57
|
-
cmd.args.shift();
|
|
58
|
-
}
|
|
59
|
-
if (!options.url && cmd.args[0]) {
|
|
60
|
-
options.url = cmd.args[0];
|
|
61
|
-
}
|
|
62
|
-
if (options.headless && !options.quality) {
|
|
63
|
-
console.error(signale_1.default.error('Missing parameters --quality'));
|
|
64
|
-
console.error(signale_1.default.note('Quality must be provided with headless mode'));
|
|
65
|
-
process.exit(1);
|
|
66
|
-
}
|
|
67
|
-
if (options.headless && !options.url && !options.inputFile) {
|
|
68
|
-
console.error(signale_1.default.error('Missing parameters --url'));
|
|
69
|
-
console.error(signale_1.default.note('URL must be provided with headless mode'));
|
|
70
|
-
process.exit(1);
|
|
71
|
-
}
|
|
72
|
-
const conf = new config_1.default(options.configFile);
|
|
73
|
-
if (conf.userConfigLocation) {
|
|
74
|
-
console.log(signale_1.default.info('Config loaded --> ' + conf.userConfigLocation));
|
|
75
|
-
}
|
|
76
|
-
const queue = new p_queue_1.default({ concurrency: Number(options.concurrency || conf.get('concurrency')) });
|
|
77
|
-
const urlRegex = /https?:\/\/.*\w+\.\w+\/\w+/;
|
|
78
|
-
const onCancel = () => {
|
|
79
|
-
console.info(signale_1.default.note('Aborted!'));
|
|
80
|
-
process.exit();
|
|
81
|
-
};
|
|
82
|
-
const startDownload = async (saveLayout, url, skipPrompt) => {
|
|
83
|
-
try {
|
|
84
|
-
if (!options.quality) {
|
|
85
|
-
const { musicQuality } = await (0, prompts_1.default)([
|
|
86
|
-
{
|
|
87
|
-
type: 'select',
|
|
88
|
-
name: 'musicQuality',
|
|
89
|
-
message: 'Select music quality:',
|
|
90
|
-
choices: [
|
|
91
|
-
{ title: 'MP3 - 128 kbps', value: '128' },
|
|
92
|
-
{ title: 'MP3 - 320 kbps', value: '320' },
|
|
93
|
-
{ title: 'FLAC - 1411 kbps', value: 'flac' },
|
|
94
|
-
],
|
|
95
|
-
initial: 1,
|
|
96
|
-
},
|
|
97
|
-
], { onCancel });
|
|
98
|
-
options.quality = musicQuality;
|
|
99
|
-
}
|
|
100
|
-
if (!url) {
|
|
101
|
-
const { query } = await (0, prompts_1.default)([
|
|
102
|
-
{
|
|
103
|
-
type: 'text',
|
|
104
|
-
name: 'query',
|
|
105
|
-
message: 'Enter URL or search:',
|
|
106
|
-
validate: (value) => (value ? true : false),
|
|
107
|
-
},
|
|
108
|
-
], { onCancel });
|
|
109
|
-
url = query;
|
|
110
|
-
}
|
|
111
|
-
let searchData = null;
|
|
112
|
-
if (!url.match(urlRegex)) {
|
|
113
|
-
if (options.headless) {
|
|
114
|
-
throw new Error('Please provide a valid URL. Unknown URL: ' + url);
|
|
115
|
-
}
|
|
116
|
-
if (url.startsWith('artist:')) {
|
|
117
|
-
const { ARTIST } = await (0, kyrie_core_1.searchMusic)(url.replace('artist:', ''), ['ARTIST'], 50);
|
|
118
|
-
const choice = await (0, prompts_1.default)([
|
|
119
|
-
{
|
|
120
|
-
type: 'select',
|
|
121
|
-
name: 'items',
|
|
122
|
-
message: `Select one artist. (found ${ARTIST.data.length} artists)`,
|
|
123
|
-
choices: ARTIST.data.map((a) => ({
|
|
124
|
-
title: a.ART_NAME,
|
|
125
|
-
value: a,
|
|
126
|
-
description: `${a.NB_FAN} fans`,
|
|
127
|
-
})),
|
|
128
|
-
},
|
|
129
|
-
], { onCancel });
|
|
130
|
-
console.log(signale_1.default.info('Fetching data. Please hold on.'));
|
|
131
|
-
url = `https://deezer.com/us/artist/${choice.items.ART_ID}`;
|
|
132
|
-
}
|
|
133
|
-
else if (url.startsWith('album:')) {
|
|
134
|
-
const { ALBUM } = await (0, kyrie_core_1.searchMusic)(url.replace('album:', ''), ['ALBUM'], 50);
|
|
135
|
-
const choice = await (0, prompts_1.default)([
|
|
136
|
-
{
|
|
137
|
-
type: 'select',
|
|
138
|
-
name: 'items',
|
|
139
|
-
message: `Select one album. (found ${ALBUM.data.length} albums)`,
|
|
140
|
-
choices: ALBUM.data.map((a) => ({
|
|
141
|
-
title: a.ALB_TITLE,
|
|
142
|
-
value: a,
|
|
143
|
-
description: `by ${a.ART_NAME}, ${a.NUMBER_TRACK} tracks`,
|
|
144
|
-
})),
|
|
145
|
-
},
|
|
146
|
-
], { onCancel });
|
|
147
|
-
url = `https://deezer.com/us/album/${choice.items.ALB_ID}`;
|
|
148
|
-
}
|
|
149
|
-
else if (url.startsWith('playlist:')) {
|
|
150
|
-
const { PLAYLIST } = await (0, kyrie_core_1.searchMusic)(url.replace('playlist:', ''), ['PLAYLIST'], 50);
|
|
151
|
-
const choice = await (0, prompts_1.default)([
|
|
152
|
-
{
|
|
153
|
-
type: 'select',
|
|
154
|
-
name: 'items',
|
|
155
|
-
message: `Select one playlist. (found ${PLAYLIST.data.length} playlists)`,
|
|
156
|
-
choices: PLAYLIST.data.map((p) => ({
|
|
157
|
-
title: p.TITLE,
|
|
158
|
-
value: p,
|
|
159
|
-
description: `by ${p.PARENT_USERNAME}, ${p.NB_SONG} tracks`,
|
|
160
|
-
})),
|
|
161
|
-
},
|
|
162
|
-
], { onCancel });
|
|
163
|
-
url = `https://deezer.com/us/playlist/${choice.items.PLAYLIST_ID}`;
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
const { TRACK } = await (0, kyrie_core_1.searchMusic)(url, ['TRACK']);
|
|
167
|
-
searchData = {
|
|
168
|
-
info: { type: 'track', id: url },
|
|
169
|
-
linktype: 'track',
|
|
170
|
-
linkinfo: {},
|
|
171
|
-
tracks: TRACK.data.map((t) => {
|
|
172
|
-
if (t.VERSION && !t.SNG_TITLE.includes(t.VERSION)) {
|
|
173
|
-
t.SNG_TITLE += ' ' + t.VERSION;
|
|
174
|
-
}
|
|
175
|
-
return t;
|
|
176
|
-
}),
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
else if (url.match(/playlist|artist/)) {
|
|
181
|
-
console.log(signale_1.default.info('Fetching data. Please hold on.'));
|
|
182
|
-
}
|
|
183
|
-
const data = searchData ? searchData : await (0, kyrie_core_1.parseInfo)(url);
|
|
184
|
-
if (!options.headless && data.tracks.length > 1) {
|
|
185
|
-
const choices = await (0, prompts_1.default)([
|
|
186
|
-
{
|
|
187
|
-
type: 'multiselect',
|
|
188
|
-
name: 'items',
|
|
189
|
-
message: `Select songs to download. Total of ${data.tracks.length} tracks.`,
|
|
190
|
-
choices: data.tracks.map((t) => ({
|
|
191
|
-
title: t.SNG_TITLE,
|
|
192
|
-
value: t,
|
|
193
|
-
description: `Artist: ${t.ART_NAME}\nAlbum: ${t.ALB_TITLE}\nDuration: ${(0, util_1.formatSecondsReadable)(Number(t.DURATION))}`,
|
|
194
|
-
})),
|
|
195
|
-
},
|
|
196
|
-
], { onCancel });
|
|
197
|
-
data.tracks = choices.items;
|
|
198
|
-
}
|
|
199
|
-
if (data && data.tracks.length > 0) {
|
|
200
|
-
console.log(signale_1.default.info(`Proceeding to download ${data.tracks.length} tracks. Be patient.`));
|
|
201
|
-
if (data.linktype === 'playlist') {
|
|
202
|
-
const filteredTracks = data.tracks.filter((item, index, self) => index === self.findIndex((t) => t.SNG_ID === item.SNG_ID));
|
|
203
|
-
const duplicateTracks = data.tracks.length - filteredTracks.length;
|
|
204
|
-
if (duplicateTracks > 0) {
|
|
205
|
-
data.tracks = filteredTracks
|
|
206
|
-
.sort((a, b) => a.TRACK_POSITION - b.TRACK_POSITION)
|
|
207
|
-
.map((t, i) => {
|
|
208
|
-
t.TRACK_POSITION = i + 1;
|
|
209
|
-
return t;
|
|
210
|
-
});
|
|
211
|
-
console.log(signale_1.default.warn(`Removed ${duplicateTracks} duplicate ${duplicateTracks > 1 ? 'tracks' : 'track'}.`));
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
const coverSizes = conf.get('coverSize');
|
|
215
|
-
const trackNumber = conf.get('trackNumber', true);
|
|
216
|
-
const fallbackTrack = conf.get('fallbackTrack', true);
|
|
217
|
-
const fallbackQuality = conf.get('fallbackQuality', true);
|
|
218
|
-
const overwrite = options.overwrite ?? conf.get('overwrite', false);
|
|
219
|
-
const resolveFullPath = options.resolveFullPath ?? conf.get('playlist.resolveFullPath');
|
|
220
|
-
const savedFiles = [];
|
|
221
|
-
let m3u8 = [];
|
|
222
|
-
await queue.addAll(data.tracks.map((track, index) => {
|
|
223
|
-
return async () => {
|
|
224
|
-
const savedPath = await (0, download_track_1.default)({
|
|
225
|
-
track,
|
|
226
|
-
quality: options.quality,
|
|
227
|
-
info: data.linkinfo,
|
|
228
|
-
coverSizes,
|
|
229
|
-
path: options.output ? options.output : saveLayout[data.linktype],
|
|
230
|
-
totalTracks: data ? data.tracks.length : 10,
|
|
231
|
-
trackNumber,
|
|
232
|
-
fallbackTrack,
|
|
233
|
-
fallbackQuality,
|
|
234
|
-
overwrite,
|
|
235
|
-
message: `(${index}/${data.tracks.length})`,
|
|
236
|
-
});
|
|
237
|
-
// Add to saved list
|
|
238
|
-
if (savedPath) {
|
|
239
|
-
m3u8.push((0, path_1.resolve)(process.env.SIMULATE ? savedPath : (0, true_case_path_1.trueCasePathSync)(savedPath)));
|
|
240
|
-
savedFiles.push(savedPath);
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
}));
|
|
244
|
-
// Display downloaded location
|
|
245
|
-
if (savedFiles.length > 0) {
|
|
246
|
-
const savedIn = new Set(savedFiles.map((l) => (0, path_1.dirname)(l)));
|
|
247
|
-
console.log(signale_1.default.info('Saved in ' + [...savedIn].map((d) => chalk_1.default.bgGreen(d)).join(', ')));
|
|
248
|
-
}
|
|
249
|
-
if ((options.createPlaylist || data.linktype === 'playlist') && !process.env.SIMULATE && m3u8.length > 1) {
|
|
250
|
-
const playlistDir = (0, util_1.commonPath)([...new Set(savedFiles.map(path_1.dirname))]);
|
|
251
|
-
const playlistFile = (0, path_1.join)(playlistDir, (0, util_1.sanitizeFilename)(data.linkinfo.TITLE || data.linkinfo.ALB_TITLE));
|
|
252
|
-
if (!resolveFullPath) {
|
|
253
|
-
const resolvedPlaylistDir = (0, path_1.resolve)(playlistDir) + path_1.sep;
|
|
254
|
-
m3u8 = m3u8.map((file) => file.replace(resolvedPlaylistDir, ''));
|
|
255
|
-
}
|
|
256
|
-
const m3u8Content = '#EXTM3U' + os_1.EOL + m3u8.sort().join(os_1.EOL);
|
|
257
|
-
(0, fs_1.writeFileSync)(playlistFile + '.m3u8', m3u8Content, { encoding: 'utf-8' });
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
console.log(signale_1.default.info('No items to download!'));
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
catch (err) {
|
|
265
|
-
console.error(signale_1.default.error(err.message));
|
|
266
|
-
}
|
|
267
|
-
// Ask for new download
|
|
268
|
-
if (!options.headless && !skipPrompt) {
|
|
269
|
-
startDownload(saveLayout, '', skipPrompt);
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
/**
|
|
273
|
-
* Application init.
|
|
274
|
-
*/
|
|
275
|
-
const initApp = async () => {
|
|
276
|
-
if (options.setup) {
|
|
277
|
-
await (0, arl_setup_1.runSetup)(conf, options.headless, options.experimentalLogin);
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
if (options.setArl) {
|
|
281
|
-
const configPath = conf.set('cookies.arl', options.setArl);
|
|
282
|
-
console.log(signale_1.default.info('cookies.arl set to --> ' + options.setArl));
|
|
283
|
-
console.log(signale_1.default.note(configPath));
|
|
284
|
-
process.exit();
|
|
285
|
-
}
|
|
286
|
-
(0, log_update_1.default)(signale_1.default.pending('Initializing session...'));
|
|
287
|
-
let arl = await (0, arl_setup_1.ensureArl)(conf, options.headless, options.experimentalLogin);
|
|
288
|
-
const login = async (cookie) => {
|
|
289
|
-
(0, log_update_1.default)(signale_1.default.pending('Verifying session...'));
|
|
290
|
-
await (0, kyrie_core_1.initDeezerApi)(cookie);
|
|
291
|
-
return (0, kyrie_core_1.getUser)();
|
|
292
|
-
};
|
|
293
|
-
let user;
|
|
294
|
-
try {
|
|
295
|
-
user = await login(arl);
|
|
296
|
-
}
|
|
297
|
-
catch (err) {
|
|
298
|
-
log_update_1.default.clear();
|
|
299
|
-
const newArl = await (0, arl_setup_1.recoverFromLoginFailure)(conf, options.headless, err, options.experimentalLogin);
|
|
300
|
-
if (!newArl) {
|
|
301
|
-
throw err;
|
|
302
|
-
}
|
|
303
|
-
arl = newArl;
|
|
304
|
-
user = await login(arl);
|
|
305
|
-
}
|
|
306
|
-
(0, log_update_1.default)(signale_1.default.success('Logged in as ' + user.BLOG_NAME));
|
|
307
|
-
log_update_1.default.done();
|
|
308
|
-
const saveLayout = conf.get('saveLayout');
|
|
309
|
-
if (options.inputFile) {
|
|
310
|
-
const urls = (0, fs_1.readFileSync)(options.inputFile, 'utf-8')
|
|
311
|
-
.split(/\r?\n/)
|
|
312
|
-
.map((line) => line.trim())
|
|
313
|
-
.filter((line) => line.match(urlRegex));
|
|
314
|
-
if (!options.quality && !options.headless) {
|
|
315
|
-
for (const url of urls) {
|
|
316
|
-
console.log(signale_1.default.info('Starting download: ' + url));
|
|
317
|
-
await startDownload(saveLayout, url, true);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
else {
|
|
321
|
-
await Promise.all(urls.map((url) => {
|
|
322
|
-
console.log(signale_1.default.info('Starting download: ' + url));
|
|
323
|
-
return startDownload(saveLayout, url, true);
|
|
324
|
-
}));
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
else {
|
|
328
|
-
startDownload(saveLayout, options.url, false);
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
if (options.update) {
|
|
332
|
-
(0, auto_updater_1.default)(package_json_1.default).catch((err) => {
|
|
333
|
-
console.error(signale_1.default.error(err.message));
|
|
334
|
-
process.exit(1);
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
else {
|
|
338
|
-
// Check for update
|
|
339
|
-
(0, update_check_1.default)(package_json_1.default);
|
|
340
|
-
// Init interface
|
|
341
|
-
initApp().catch((err) => {
|
|
342
|
-
console.error(signale_1.default.error(err.message));
|
|
343
|
-
process.exit(1);
|
|
344
|
-
});
|
|
345
|
-
}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.decryptDownloadFile = void 0;
|
|
4
|
-
const os_1 = require("os");
|
|
5
|
-
const path_1 = require("path");
|
|
6
|
-
const worker_threads_1 = require("worker_threads");
|
|
7
|
-
const idleTimeoutMs = 5000;
|
|
8
|
-
const maxWorkers = Math.max(1, Math.min(4, (0, os_1.cpus)().length));
|
|
9
|
-
const workers = new Set();
|
|
10
|
-
const queue = [];
|
|
11
|
-
let nextJobId = 0;
|
|
12
|
-
const getWorkerOptions = () => {
|
|
13
|
-
if (__filename.endsWith('.ts')) {
|
|
14
|
-
return {
|
|
15
|
-
path: (0, path_1.join)(__dirname, 'decrypt-worker.ts'),
|
|
16
|
-
execArgv: ['-r', 'ts-node/register'],
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
return {
|
|
20
|
-
path: (0, path_1.join)(__dirname, 'decrypt-worker.js'),
|
|
21
|
-
execArgv: process.execArgv,
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
const toBuffer = (outFile) => {
|
|
25
|
-
if (Buffer.isBuffer(outFile)) {
|
|
26
|
-
return outFile;
|
|
27
|
-
}
|
|
28
|
-
return Buffer.from(outFile.buffer, outFile.byteOffset, outFile.byteLength);
|
|
29
|
-
};
|
|
30
|
-
const disposeWorker = (state) => {
|
|
31
|
-
if (state.idleTimer) {
|
|
32
|
-
clearTimeout(state.idleTimer);
|
|
33
|
-
}
|
|
34
|
-
workers.delete(state);
|
|
35
|
-
void state.worker.terminate();
|
|
36
|
-
};
|
|
37
|
-
const rejectCurrent = (state, err) => {
|
|
38
|
-
if (state.current) {
|
|
39
|
-
state.current.reject(err);
|
|
40
|
-
state.current = undefined;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
const scheduleIdleShutdown = (state) => {
|
|
44
|
-
if (state.idleTimer) {
|
|
45
|
-
clearTimeout(state.idleTimer);
|
|
46
|
-
}
|
|
47
|
-
state.idleTimer = setTimeout(() => disposeWorker(state), idleTimeoutMs);
|
|
48
|
-
};
|
|
49
|
-
const createWorker = () => {
|
|
50
|
-
const { path, execArgv } = getWorkerOptions();
|
|
51
|
-
const state = { worker: new worker_threads_1.Worker(path, { execArgv }) };
|
|
52
|
-
state.worker.on('message', ({ outFile, error, stack }) => {
|
|
53
|
-
const job = state.current;
|
|
54
|
-
state.current = undefined;
|
|
55
|
-
if (!job) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
if (error) {
|
|
59
|
-
const err = new Error(error);
|
|
60
|
-
err.stack = stack;
|
|
61
|
-
job.reject(err);
|
|
62
|
-
}
|
|
63
|
-
else if (outFile) {
|
|
64
|
-
job.resolve(toBuffer(outFile));
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
job.reject(new Error('Decrypt worker returned no output'));
|
|
68
|
-
}
|
|
69
|
-
runNextJob();
|
|
70
|
-
});
|
|
71
|
-
state.worker.on('error', (err) => {
|
|
72
|
-
rejectCurrent(state, err);
|
|
73
|
-
workers.delete(state);
|
|
74
|
-
runNextJob();
|
|
75
|
-
});
|
|
76
|
-
state.worker.on('exit', (code) => {
|
|
77
|
-
workers.delete(state);
|
|
78
|
-
if (code !== 0) {
|
|
79
|
-
rejectCurrent(state, new Error(`Decrypt worker exited with code ${code}`));
|
|
80
|
-
runNextJob();
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
workers.add(state);
|
|
84
|
-
return state;
|
|
85
|
-
};
|
|
86
|
-
const getAvailableWorker = () => {
|
|
87
|
-
for (const state of workers) {
|
|
88
|
-
if (!state.current) {
|
|
89
|
-
return state;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (workers.size < maxWorkers) {
|
|
93
|
-
return createWorker();
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
function runNextJob() {
|
|
97
|
-
const job = queue.shift();
|
|
98
|
-
if (!job) {
|
|
99
|
-
for (const state of workers) {
|
|
100
|
-
if (!state.current) {
|
|
101
|
-
scheduleIdleShutdown(state);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const state = getAvailableWorker();
|
|
107
|
-
if (!state) {
|
|
108
|
-
queue.unshift(job);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
if (state.idleTimer) {
|
|
112
|
-
clearTimeout(state.idleTimer);
|
|
113
|
-
state.idleTimer = undefined;
|
|
114
|
-
}
|
|
115
|
-
state.current = job;
|
|
116
|
-
state.worker.postMessage({ id: job.id, tmpfile: job.tmpfile, trackId: job.trackId });
|
|
117
|
-
}
|
|
118
|
-
const decryptDownloadFile = (tmpfile, trackId) => new Promise((resolve, reject) => {
|
|
119
|
-
queue.push({ id: nextJobId++, tmpfile, trackId, resolve, reject });
|
|
120
|
-
runNextJob();
|
|
121
|
-
});
|
|
122
|
-
exports.decryptDownloadFile = decryptDownloadFile;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const worker_threads_1 = require("worker_threads");
|
|
4
|
-
const decrypt_1 = require("./decrypt");
|
|
5
|
-
if (!worker_threads_1.parentPort) {
|
|
6
|
-
throw new Error('decrypt-worker must run inside a worker thread');
|
|
7
|
-
}
|
|
8
|
-
worker_threads_1.parentPort.on('message', ({ id, tmpfile, trackId }) => {
|
|
9
|
-
try {
|
|
10
|
-
const outFile = (0, decrypt_1.decryptDownloadFile)(tmpfile, trackId);
|
|
11
|
-
worker_threads_1.parentPort?.postMessage({ id, outFile }, [outFile.buffer]);
|
|
12
|
-
}
|
|
13
|
-
catch (err) {
|
|
14
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
15
|
-
worker_threads_1.parentPort?.postMessage({ id, error: error.message, stack: error.stack });
|
|
16
|
-
}
|
|
17
|
-
});
|
package/dist/src/q-fi.d.ts
DELETED