discord-player-deezer 2.5.0 → 3.0.0-dev.0
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/.yarn/install-state.gz +0 -0
- package/dist/index.d.ts +140 -6
- package/dist/index.js +369 -50
- package/package.json +1 -1
- package/readme.md +23 -1
- package/src/Cache/CacheTapStream.ts +81 -0
- package/src/Cache/DiskAudioCache.ts +232 -0
- package/src/Cache/IAudioCache.ts +30 -0
- package/src/Cache/index.ts +2 -0
- package/src/Crypto/JSDecryptor.ts +119 -18
- package/src/Crypto/NodeDecryptor.ts +70 -17
- package/src/Crypto/StripeDecryptStream.ts +73 -0
- package/src/Crypto/index.ts +2 -1
- package/src/DeezerExtractor.ts +5 -1
- package/src/index.ts +2 -1
- package/src/utils/crypto.ts +10 -0
- package/src/utils/util.ts +56 -47
package/dist/index.js
CHANGED
|
@@ -28,12 +28,14 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
33
|
BaseDecryptor: () => BaseDecryptor,
|
|
34
34
|
DeezerExtractor: () => DeezerExtractor,
|
|
35
|
+
DiskAudioCache: () => DiskAudioCache,
|
|
35
36
|
JSDecryptor: () => JSDecryptor,
|
|
36
37
|
NodeDecryptor: () => NodeDecryptor,
|
|
38
|
+
StripeDecryptStream: () => StripeDecryptStream,
|
|
37
39
|
Warnings: () => Warnings,
|
|
38
40
|
buildTrackFromSearch: () => buildTrackFromSearch,
|
|
39
41
|
deezerRegex: () => deezerRegex,
|
|
@@ -45,7 +47,7 @@ __export(src_exports, {
|
|
|
45
47
|
streamTrack: () => streamTrack,
|
|
46
48
|
validate: () => validate
|
|
47
49
|
});
|
|
48
|
-
module.exports = __toCommonJS(
|
|
50
|
+
module.exports = __toCommonJS(index_exports);
|
|
49
51
|
|
|
50
52
|
// src/DeezerExtractor.ts
|
|
51
53
|
var import_deezer_music_metadata = require("@mithron/deezer-music-metadata");
|
|
@@ -56,6 +58,57 @@ var import_promises = require("timers/promises");
|
|
|
56
58
|
var import_discord_player = require("discord-player");
|
|
57
59
|
var import_stream = require("stream");
|
|
58
60
|
|
|
61
|
+
// src/Cache/CacheTapStream.ts
|
|
62
|
+
var import_node_stream = require("stream");
|
|
63
|
+
var CacheTapStream = class extends import_node_stream.Transform {
|
|
64
|
+
constructor(sink) {
|
|
65
|
+
super();
|
|
66
|
+
this.sinkFailed = false;
|
|
67
|
+
this.flushed = false;
|
|
68
|
+
this.sink = sink;
|
|
69
|
+
this.sink.once("error", () => {
|
|
70
|
+
this.sinkFailed = true;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
_transform(chunk, _encoding, callback) {
|
|
74
|
+
this.push(chunk);
|
|
75
|
+
if (this.sinkFailed || this.sink.destroyed || this.sink.writableEnded) {
|
|
76
|
+
callback();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (this.sink.write(chunk)) {
|
|
80
|
+
callback();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const done = () => {
|
|
84
|
+
this.sink.off("drain", done);
|
|
85
|
+
this.sink.off("error", done);
|
|
86
|
+
callback();
|
|
87
|
+
};
|
|
88
|
+
this.sink.once("drain", done);
|
|
89
|
+
this.sink.once("error", done);
|
|
90
|
+
}
|
|
91
|
+
_flush(callback) {
|
|
92
|
+
this.flushed = true;
|
|
93
|
+
if (this.sinkFailed || this.sink.destroyed || this.sink.writableEnded) {
|
|
94
|
+
callback();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const done = () => {
|
|
98
|
+
this.sink.off("finish", done);
|
|
99
|
+
this.sink.off("error", done);
|
|
100
|
+
callback();
|
|
101
|
+
};
|
|
102
|
+
this.sink.once("finish", done);
|
|
103
|
+
this.sink.once("error", done);
|
|
104
|
+
this.sink.end();
|
|
105
|
+
}
|
|
106
|
+
_destroy(error, callback) {
|
|
107
|
+
if (!this.flushed && !this.sink.destroyed) this.sink.destroy();
|
|
108
|
+
callback(error);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
59
112
|
// src/Crypto/JSDecryptor.ts
|
|
60
113
|
var import_blowfish_node = __toESM(require("blowfish-node"));
|
|
61
114
|
|
|
@@ -74,18 +127,107 @@ var BaseDecryptor = class {
|
|
|
74
127
|
var JSDecryptor = class extends BaseDecryptor {
|
|
75
128
|
constructor(...args) {
|
|
76
129
|
super(...args);
|
|
77
|
-
|
|
78
|
-
|
|
130
|
+
const schedule = new import_blowfish_node.default(
|
|
131
|
+
new Uint8Array(this.key),
|
|
132
|
+
import_blowfish_node.MODE.CBC,
|
|
133
|
+
import_blowfish_node.PADDING.NULL
|
|
134
|
+
);
|
|
135
|
+
this.p = Uint32Array.from(schedule.p);
|
|
136
|
+
this.s0 = Uint32Array.from(schedule.s[0]);
|
|
137
|
+
this.s1 = Uint32Array.from(schedule.s[1]);
|
|
138
|
+
this.s2 = Uint32Array.from(schedule.s[2]);
|
|
139
|
+
this.s3 = Uint32Array.from(schedule.s[3]);
|
|
140
|
+
this.iv0 = this.iv[0] << 24 | this.iv[1] << 16 | this.iv[2] << 8 | this.iv[3];
|
|
141
|
+
this.iv1 = this.iv[4] << 24 | this.iv[5] << 16 | this.iv[6] << 8 | this.iv[7];
|
|
79
142
|
}
|
|
80
143
|
async decrypt(chunk) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
144
|
+
if (chunk.length % 8 !== 0) {
|
|
145
|
+
throw new Error("Decoded data should be multiple of 8 bytes");
|
|
146
|
+
}
|
|
147
|
+
const out = Buffer.from(chunk);
|
|
148
|
+
const view = new DataView(
|
|
149
|
+
out.buffer,
|
|
150
|
+
out.byteOffset,
|
|
151
|
+
out.byteLength
|
|
152
|
+
);
|
|
153
|
+
const { p, s0, s1, s2, s3 } = this;
|
|
154
|
+
let prevL = this.iv0;
|
|
155
|
+
let prevR = this.iv1;
|
|
156
|
+
for (let offset = 0; offset < out.length; offset += 8) {
|
|
157
|
+
let l = view.getUint32(offset, false);
|
|
158
|
+
let r = view.getUint32(offset + 4, false);
|
|
159
|
+
const cipherL = l;
|
|
160
|
+
const cipherR = r;
|
|
161
|
+
for (let round = 17; round > 1; round--) {
|
|
162
|
+
l ^= p[round];
|
|
163
|
+
let f = (s0[l >>> 24 & 255] + s1[l >>> 16 & 255] | 0) ^ s2[l >>> 8 & 255];
|
|
164
|
+
f = f + s3[l & 255] | 0;
|
|
165
|
+
r ^= f;
|
|
166
|
+
const tmp = l;
|
|
167
|
+
l = r;
|
|
168
|
+
r = tmp;
|
|
169
|
+
}
|
|
170
|
+
const swap = l;
|
|
171
|
+
l = r;
|
|
172
|
+
r = swap;
|
|
173
|
+
r ^= p[1];
|
|
174
|
+
l ^= p[0];
|
|
175
|
+
l ^= prevL;
|
|
176
|
+
r ^= prevR;
|
|
177
|
+
prevL = cipherL;
|
|
178
|
+
prevR = cipherR;
|
|
179
|
+
view.setUint32(offset, l, false);
|
|
180
|
+
view.setUint32(offset + 4, r, false);
|
|
181
|
+
}
|
|
182
|
+
return out;
|
|
84
183
|
}
|
|
85
184
|
};
|
|
86
185
|
|
|
87
|
-
// src/
|
|
88
|
-
var
|
|
186
|
+
// src/Crypto/StripeDecryptStream.ts
|
|
187
|
+
var import_node_stream2 = require("stream");
|
|
188
|
+
var STRIPE_SIZE = 2048;
|
|
189
|
+
var ENCRYPTED_STRIPE_INTERVAL = 3;
|
|
190
|
+
var StripeDecryptStream = class extends import_node_stream2.Transform {
|
|
191
|
+
constructor(decryptor) {
|
|
192
|
+
super();
|
|
193
|
+
this.stripeIndex = 0;
|
|
194
|
+
this.remainder = Buffer.alloc(0);
|
|
195
|
+
this.decryptor = decryptor;
|
|
196
|
+
}
|
|
197
|
+
async _transform(chunk, _encoding, callback) {
|
|
198
|
+
try {
|
|
199
|
+
const data = this.remainder.length ? Buffer.concat([this.remainder, chunk]) : chunk;
|
|
200
|
+
let offset = 0;
|
|
201
|
+
while (data.length - offset >= STRIPE_SIZE) {
|
|
202
|
+
const stripe = data.subarray(offset, offset + STRIPE_SIZE);
|
|
203
|
+
offset += STRIPE_SIZE;
|
|
204
|
+
this.push(await this.decodeStripe(stripe));
|
|
205
|
+
}
|
|
206
|
+
this.remainder = Buffer.from(data.subarray(offset));
|
|
207
|
+
callback();
|
|
208
|
+
} catch (error) {
|
|
209
|
+
callback(error);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async _flush(callback) {
|
|
213
|
+
try {
|
|
214
|
+
if (this.remainder.length) {
|
|
215
|
+
this.push(await this.decodeStripe(this.remainder));
|
|
216
|
+
this.remainder = Buffer.alloc(0);
|
|
217
|
+
}
|
|
218
|
+
callback();
|
|
219
|
+
} catch (error) {
|
|
220
|
+
callback(error);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async decodeStripe(stripe) {
|
|
224
|
+
const encrypted = this.stripeIndex++ % ENCRYPTED_STRIPE_INTERVAL === 0;
|
|
225
|
+
if (!encrypted || stripe.length % 8 !== 0) return stripe;
|
|
226
|
+
return this.decryptor.decrypt(stripe);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// src/utils/crypto.ts
|
|
89
231
|
var rawCrypto;
|
|
90
232
|
async function getCrypto() {
|
|
91
233
|
if (!rawCrypto) {
|
|
@@ -94,6 +236,9 @@ async function getCrypto() {
|
|
|
94
236
|
}
|
|
95
237
|
return rawCrypto;
|
|
96
238
|
}
|
|
239
|
+
|
|
240
|
+
// src/utils/util.ts
|
|
241
|
+
var IV = Buffer.from(Array.from({ length: 8 }, (_, x) => x));
|
|
97
242
|
function isUrl(query) {
|
|
98
243
|
try {
|
|
99
244
|
const url = new URL(query);
|
|
@@ -149,16 +294,30 @@ async function searchOneTrack(query) {
|
|
|
149
294
|
return trackData;
|
|
150
295
|
}
|
|
151
296
|
async function streamTrack(track, ext) {
|
|
297
|
+
const cache = ext.options.cache;
|
|
298
|
+
const trackId = extractTrackId(track.url);
|
|
299
|
+
if (cache) {
|
|
300
|
+
try {
|
|
301
|
+
const cached = await cache.read(trackId);
|
|
302
|
+
if (cached) {
|
|
303
|
+
ext.context.player.debug(`DEEZER CACHE HIT ${trackId}`);
|
|
304
|
+
return cached;
|
|
305
|
+
}
|
|
306
|
+
} catch (error) {
|
|
307
|
+
ext.context.player.debug(`DEEZER CACHE READ ERROR ${String(error)}`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
152
310
|
const decryptionKey = ext.options.decryptionKey;
|
|
153
311
|
if (!decryptionKey) throw new Error("MISSING DEEZER DECRYPTION KEY");
|
|
154
312
|
const crypto = await getCrypto();
|
|
155
|
-
const trackId = extractTrackId(track.url);
|
|
156
313
|
const trackHash = crypto.createHash("md5").update(trackId, "ascii").digest("hex");
|
|
157
314
|
const trackKey = Buffer.alloc(16);
|
|
158
315
|
const Decryptor = ext.options.decryptor || JSDecryptor;
|
|
159
316
|
for (let iter = 0; iter < 16; iter++)
|
|
160
317
|
trackKey.writeInt8(
|
|
161
|
-
|
|
318
|
+
/* tslint:disable-next-line no-bitwise */
|
|
319
|
+
trackHash[iter].charCodeAt(0) ^ /* tslint:disable-next-line no-bitwise */
|
|
320
|
+
trackHash[iter + 16].charCodeAt(0) ^ decryptionKey[iter].charCodeAt(0),
|
|
162
321
|
iter
|
|
163
322
|
);
|
|
164
323
|
const trackInfoRes = await ext.fetch(
|
|
@@ -211,38 +370,32 @@ async function streamTrack(track, ext) {
|
|
|
211
370
|
ext.context.player.debug("DEEZER FOUND MEDIA URL " + trackMediaUrl);
|
|
212
371
|
const trackReq = await ext.fetch(trackMediaUrl);
|
|
213
372
|
const readable = import_stream.Readable.fromWeb(trackReq.body);
|
|
214
|
-
let buffer = Buffer.alloc(0);
|
|
215
|
-
let i = 0;
|
|
216
|
-
const bufferSize = 2048;
|
|
217
|
-
const passThrough = new import_stream.PassThrough();
|
|
218
373
|
const decryptor = new Decryptor(trackKey, IV);
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (readable.readableLength) {
|
|
225
|
-
chunk = readable.read(readable.readableLength);
|
|
226
|
-
buffer = Buffer.concat([buffer, chunk]);
|
|
227
|
-
}
|
|
228
|
-
break;
|
|
229
|
-
} else {
|
|
230
|
-
buffer = Buffer.concat([buffer, chunk]);
|
|
231
|
-
}
|
|
232
|
-
while (buffer.length >= bufferSize) {
|
|
233
|
-
const bufferSized = buffer.subarray(0, bufferSize);
|
|
234
|
-
if (i % 3 === 0) {
|
|
235
|
-
const decipher = await decryptor.decrypt(bufferSized);
|
|
236
|
-
passThrough.write(decipher);
|
|
237
|
-
} else {
|
|
238
|
-
passThrough.write(bufferSized);
|
|
239
|
-
}
|
|
240
|
-
i++;
|
|
241
|
-
buffer = buffer.subarray(bufferSize);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
374
|
+
const stream = new StripeDecryptStream(decryptor);
|
|
375
|
+
readable.on("error", (error) => stream.destroy(error));
|
|
376
|
+
readable.pipe(stream);
|
|
377
|
+
stream.once("close", () => {
|
|
378
|
+
if (!stream.readableEnded && !readable.destroyed) readable.destroy();
|
|
244
379
|
});
|
|
245
|
-
return
|
|
380
|
+
if (!cache) return stream;
|
|
381
|
+
try {
|
|
382
|
+
const sink = await cache.write(trackId);
|
|
383
|
+
sink.on("error", (error) => {
|
|
384
|
+
ext.context.player.debug(`DEEZER CACHE WRITE ERROR ${String(error)}`);
|
|
385
|
+
});
|
|
386
|
+
const output = new CacheTapStream(sink);
|
|
387
|
+
stream.on("error", (error) => output.destroy(error));
|
|
388
|
+
output.once("close", () => {
|
|
389
|
+
if (output.readableEnded) return;
|
|
390
|
+
if (!stream.destroyed) stream.destroy();
|
|
391
|
+
if (!readable.destroyed) readable.destroy();
|
|
392
|
+
});
|
|
393
|
+
stream.pipe(output);
|
|
394
|
+
return output;
|
|
395
|
+
} catch (error) {
|
|
396
|
+
ext.context.player.debug(`DEEZER CACHE WRITE ERROR ${String(error)}`);
|
|
397
|
+
return stream;
|
|
398
|
+
}
|
|
246
399
|
}
|
|
247
400
|
var deezerRegex = {
|
|
248
401
|
track: /(^https:)\/\/(www\.)?deezer.com\/([a-zA-Z]+\/)?track\/[0-9]+/,
|
|
@@ -340,6 +493,7 @@ var DeezerExtractor = class extends import_discord_player2.BaseExtractor {
|
|
|
340
493
|
async deactivate() {
|
|
341
494
|
if (this.__interval) clearInterval(this.__interval);
|
|
342
495
|
this.protocols = [];
|
|
496
|
+
await this.options.cache?.close?.();
|
|
343
497
|
}
|
|
344
498
|
async validate(query, type) {
|
|
345
499
|
return validate(query) || type === "deezer" || !isUrl(query);
|
|
@@ -392,7 +546,7 @@ var DeezerExtractor = class extends import_discord_player2.BaseExtractor {
|
|
|
392
546
|
url: track.url,
|
|
393
547
|
source: "arbitrary",
|
|
394
548
|
thumbnail: track.thumbnail[0].url,
|
|
395
|
-
duration: import_discord_player2.Util.buildTimeCode(import_discord_player2.Util.parseMS(track.duration)),
|
|
549
|
+
duration: import_discord_player2.Util.buildTimeCode(import_discord_player2.Util.parseMS(track.duration * 1e3)),
|
|
396
550
|
requestedBy
|
|
397
551
|
});
|
|
398
552
|
}
|
|
@@ -417,24 +571,189 @@ DeezerExtractor.identifier = "com.retrouser955.discord-player.deezr-ext";
|
|
|
417
571
|
|
|
418
572
|
// src/Crypto/NodeDecryptor.ts
|
|
419
573
|
var NodeDecryptor = class extends BaseDecryptor {
|
|
420
|
-
constructor(
|
|
421
|
-
super(...
|
|
422
|
-
|
|
423
|
-
if (!(await getCrypto()).getCiphers().includes("bf-cbc")) throw new Error("Cannot find bf-cbc");
|
|
424
|
-
})();
|
|
574
|
+
constructor() {
|
|
575
|
+
super(...arguments);
|
|
576
|
+
this.firstChunk = true;
|
|
425
577
|
}
|
|
426
578
|
async decrypt(chunk) {
|
|
579
|
+
if (chunk.length % 8 !== 0) {
|
|
580
|
+
throw new Error("Decoded data should be multiple of 8 bytes");
|
|
581
|
+
}
|
|
582
|
+
if (chunk.length === 0) return Buffer.alloc(0);
|
|
427
583
|
const crypto = await getCrypto();
|
|
428
|
-
|
|
429
|
-
|
|
584
|
+
if (!this.decipher) {
|
|
585
|
+
try {
|
|
586
|
+
this.decipher = crypto.createDecipheriv(
|
|
587
|
+
"bf-cbc",
|
|
588
|
+
this.key,
|
|
589
|
+
this.iv
|
|
590
|
+
).setAutoPadding(false);
|
|
591
|
+
} catch {
|
|
592
|
+
throw new Error(
|
|
593
|
+
"Cannot find bf-cbc. Start Node with --openssl-legacy-provider or use JSDecryptor."
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
this.previousCipherBlock = Buffer.alloc(8);
|
|
597
|
+
}
|
|
598
|
+
const out = this.decipher.update(chunk);
|
|
599
|
+
if (!this.firstChunk && this.previousCipherBlock && out.length >= 8) {
|
|
600
|
+
for (let i = 0; i < 8; i++) {
|
|
601
|
+
out[i] ^= this.previousCipherBlock[i] ^ this.iv[i];
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
this.firstChunk = false;
|
|
605
|
+
this.previousCipherBlock = Buffer.from(
|
|
606
|
+
chunk.subarray(chunk.length - 8)
|
|
607
|
+
);
|
|
608
|
+
return out;
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
// src/Cache/DiskAudioCache.ts
|
|
613
|
+
var import_node_crypto = require("crypto");
|
|
614
|
+
var import_node_fs = require("fs");
|
|
615
|
+
var import_promises2 = require("fs/promises");
|
|
616
|
+
var import_node_os = require("os");
|
|
617
|
+
var import_node_path = require("path");
|
|
618
|
+
var CACHE_EXTENSION = ".audio";
|
|
619
|
+
var TEMP_EXTENSION = ".part";
|
|
620
|
+
var TEMP_MAX_AGE = 24 * 60 * 60 * 1e3;
|
|
621
|
+
var DEFAULT_MAX_SONGS = 100;
|
|
622
|
+
var DEFAULT_MAX_AGE = 7 * 24 * 60 * 60 * 1e3;
|
|
623
|
+
var DEFAULT_CLEANUP_INTERVAL = 60 * 60 * 1e3;
|
|
624
|
+
var DiskAudioCache = class {
|
|
625
|
+
constructor(options = {}) {
|
|
626
|
+
this.closed = false;
|
|
627
|
+
this.directory = options.directory ?? (0, import_node_path.join)((0, import_node_os.tmpdir)(), "discord-player-deezer-cache");
|
|
628
|
+
this.maxSongs = options.maxSongs ?? DEFAULT_MAX_SONGS;
|
|
629
|
+
this.maxAge = options.maxAge ?? DEFAULT_MAX_AGE;
|
|
630
|
+
this.cleanupInterval = options.cleanupInterval ?? DEFAULT_CLEANUP_INTERVAL;
|
|
631
|
+
this.ready = (0, import_promises2.mkdir)(this.directory, { recursive: true }).then(
|
|
632
|
+
() => void 0
|
|
633
|
+
);
|
|
634
|
+
if (this.cleanupInterval > 0) {
|
|
635
|
+
this.timer = setInterval(() => {
|
|
636
|
+
void this.cleanup().catch(() => void 0);
|
|
637
|
+
}, this.cleanupInterval);
|
|
638
|
+
this.timer.unref();
|
|
639
|
+
}
|
|
640
|
+
void this.cleanup().catch(() => void 0);
|
|
641
|
+
}
|
|
642
|
+
async read(key) {
|
|
643
|
+
await this.ready;
|
|
644
|
+
const file = this.pathFor(key);
|
|
645
|
+
try {
|
|
646
|
+
const handle = await (0, import_promises2.open)(file, "r");
|
|
647
|
+
const now = /* @__PURE__ */ new Date();
|
|
648
|
+
await (0, import_promises2.utimes)(file, now, now).catch(() => void 0);
|
|
649
|
+
const stream = handle.createReadStream({ autoClose: false });
|
|
650
|
+
let released = false;
|
|
651
|
+
const release = () => {
|
|
652
|
+
if (released) return;
|
|
653
|
+
released = true;
|
|
654
|
+
void handle.close().catch(() => void 0);
|
|
655
|
+
};
|
|
656
|
+
stream.once("end", release);
|
|
657
|
+
stream.once("error", release);
|
|
658
|
+
stream.once("close", release);
|
|
659
|
+
return stream;
|
|
660
|
+
} catch (error) {
|
|
661
|
+
if (error.code === "ENOENT") return null;
|
|
662
|
+
throw error;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
async write(key) {
|
|
666
|
+
if (this.closed) throw new Error("DiskAudioCache is closed");
|
|
667
|
+
await this.ready;
|
|
668
|
+
const target = this.pathFor(key);
|
|
669
|
+
const temp = `${target}.${(0, import_node_crypto.randomBytes)(8).toString("hex")}${TEMP_EXTENSION}`;
|
|
670
|
+
const out = (0, import_node_fs.createWriteStream)(temp);
|
|
671
|
+
let committed = false;
|
|
672
|
+
out.on("error", () => {
|
|
673
|
+
void (0, import_promises2.unlink)(temp).catch(() => void 0);
|
|
674
|
+
});
|
|
675
|
+
out.once("finish", () => {
|
|
676
|
+
committed = true;
|
|
677
|
+
void (0, import_promises2.rename)(temp, target).then(() => this.cleanup()).catch(async () => {
|
|
678
|
+
await (0, import_promises2.unlink)(temp).catch(() => void 0);
|
|
679
|
+
});
|
|
680
|
+
});
|
|
681
|
+
out.once("close", () => {
|
|
682
|
+
if (!committed) void (0, import_promises2.unlink)(temp).catch(() => void 0);
|
|
683
|
+
});
|
|
684
|
+
return out;
|
|
685
|
+
}
|
|
686
|
+
async delete(key) {
|
|
687
|
+
await this.ready;
|
|
688
|
+
await (0, import_promises2.unlink)(this.pathFor(key)).catch((error) => {
|
|
689
|
+
if (error.code !== "ENOENT") throw error;
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
cleanup() {
|
|
693
|
+
if (!this.cleaning) {
|
|
694
|
+
this.cleaning = this.runCleanup().finally(() => {
|
|
695
|
+
this.cleaning = void 0;
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
return this.cleaning;
|
|
699
|
+
}
|
|
700
|
+
async close() {
|
|
701
|
+
this.closed = true;
|
|
702
|
+
if (this.timer) {
|
|
703
|
+
clearInterval(this.timer);
|
|
704
|
+
this.timer = void 0;
|
|
705
|
+
}
|
|
706
|
+
if (this.cleaning) await this.cleaning.catch(() => void 0);
|
|
707
|
+
}
|
|
708
|
+
pathFor(key) {
|
|
709
|
+
const hash = (0, import_node_crypto.createHash)("sha256").update(key).digest("hex");
|
|
710
|
+
return (0, import_node_path.join)(this.directory, hash + CACHE_EXTENSION);
|
|
711
|
+
}
|
|
712
|
+
async runCleanup() {
|
|
713
|
+
await this.ready;
|
|
714
|
+
const now = Date.now();
|
|
715
|
+
const entries = await (0, import_promises2.readdir)(this.directory, { withFileTypes: true });
|
|
716
|
+
const songs = [];
|
|
717
|
+
for (const entry of entries) {
|
|
718
|
+
if (!entry.isFile()) continue;
|
|
719
|
+
const isSong = entry.name.endsWith(CACHE_EXTENSION);
|
|
720
|
+
const isTemp = entry.name.endsWith(TEMP_EXTENSION);
|
|
721
|
+
if (!isSong && !isTemp) continue;
|
|
722
|
+
const file = (0, import_node_path.join)(this.directory, entry.name);
|
|
723
|
+
let info;
|
|
724
|
+
try {
|
|
725
|
+
info = await (0, import_promises2.stat)(file);
|
|
726
|
+
} catch {
|
|
727
|
+
continue;
|
|
728
|
+
}
|
|
729
|
+
if (isTemp) {
|
|
730
|
+
if (now - info.mtimeMs > TEMP_MAX_AGE) {
|
|
731
|
+
await (0, import_promises2.unlink)(file).catch(() => void 0);
|
|
732
|
+
}
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
if (this.maxAge > 0 && now - info.mtimeMs > this.maxAge) {
|
|
736
|
+
await (0, import_promises2.unlink)(file).catch(() => void 0);
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
songs.push({ file, mtimeMs: info.mtimeMs });
|
|
740
|
+
}
|
|
741
|
+
if (this.maxSongs > 0 && songs.length > this.maxSongs) {
|
|
742
|
+
songs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
743
|
+
for (let i = this.maxSongs; i < songs.length; i++) {
|
|
744
|
+
await (0, import_promises2.unlink)(songs[i].file).catch(() => void 0);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
430
747
|
}
|
|
431
748
|
};
|
|
432
749
|
// Annotate the CommonJS export names for ESM import in node:
|
|
433
750
|
0 && (module.exports = {
|
|
434
751
|
BaseDecryptor,
|
|
435
752
|
DeezerExtractor,
|
|
753
|
+
DiskAudioCache,
|
|
436
754
|
JSDecryptor,
|
|
437
755
|
NodeDecryptor,
|
|
756
|
+
StripeDecryptStream,
|
|
438
757
|
Warnings,
|
|
439
758
|
buildTrackFromSearch,
|
|
440
759
|
deezerRegex,
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -32,4 +32,26 @@ const player = new Player(...)
|
|
|
32
32
|
await player.extractors.register(DeezerExtractor, {
|
|
33
33
|
decryptionKey: "master-decryption-key"
|
|
34
34
|
})
|
|
35
|
-
```
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
# Caching (optional)
|
|
38
|
+
|
|
39
|
+
Pass a `cache` to store decrypted tracks on disk. Caching is disabled unless you provide one; cached tracks skip both the download and the decryption step on repeat plays.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { DeezerExtractor, DiskAudioCache } from "discord-player-deezer"
|
|
43
|
+
|
|
44
|
+
const cache = new DiskAudioCache({
|
|
45
|
+
directory: "./deezer-cache", // default: <tmp>/discord-player-deezer-cache
|
|
46
|
+
maxSongs: 100, // oldest entries are removed first
|
|
47
|
+
maxAge: 7 * 24 * 60 * 60 * 1000, // expire entries after 7 days (0 = never)
|
|
48
|
+
cleanupInterval: 60 * 60 * 1000 // run cleanup hourly (0 = only after writes)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
await player.extractors.register(DeezerExtractor, {
|
|
52
|
+
decryptionKey: "master-decryption-key",
|
|
53
|
+
cache
|
|
54
|
+
})
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can also implement `IAudioCache` to use a different storage backend.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Transform, type TransformCallback } from "node:stream";
|
|
2
|
+
import type { Writable } from "node:stream";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Passes audio through while mirroring it into a cache writable.
|
|
6
|
+
*
|
|
7
|
+
* The readable side is driven by the consumer, so enabling the cache does not
|
|
8
|
+
* make the download eager: if playback stalls or is aborted, the source is
|
|
9
|
+
* not drained into the cache.
|
|
10
|
+
*/
|
|
11
|
+
export class CacheTapStream extends Transform {
|
|
12
|
+
private readonly sink: Writable;
|
|
13
|
+
private sinkFailed = false;
|
|
14
|
+
private flushed = false;
|
|
15
|
+
|
|
16
|
+
constructor(sink: Writable) {
|
|
17
|
+
super();
|
|
18
|
+
this.sink = sink;
|
|
19
|
+
|
|
20
|
+
// Keep a permanent listener so a cache failure cannot become an
|
|
21
|
+
// unhandled 'error' event.
|
|
22
|
+
this.sink.once("error", () => {
|
|
23
|
+
this.sinkFailed = true;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
_transform(
|
|
28
|
+
chunk: Buffer,
|
|
29
|
+
_encoding: BufferEncoding,
|
|
30
|
+
callback: TransformCallback
|
|
31
|
+
): void {
|
|
32
|
+
this.push(chunk);
|
|
33
|
+
|
|
34
|
+
if (this.sinkFailed || this.sink.destroyed || this.sink.writableEnded) {
|
|
35
|
+
callback();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (this.sink.write(chunk)) {
|
|
40
|
+
callback();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const done = () => {
|
|
45
|
+
this.sink.off("drain", done);
|
|
46
|
+
this.sink.off("error", done);
|
|
47
|
+
callback();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
this.sink.once("drain", done);
|
|
51
|
+
this.sink.once("error", done);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_flush(callback: TransformCallback): void {
|
|
55
|
+
this.flushed = true;
|
|
56
|
+
|
|
57
|
+
if (this.sinkFailed || this.sink.destroyed || this.sink.writableEnded) {
|
|
58
|
+
callback();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const done = () => {
|
|
63
|
+
this.sink.off("finish", done);
|
|
64
|
+
this.sink.off("error", done);
|
|
65
|
+
callback();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
this.sink.once("finish", done);
|
|
69
|
+
this.sink.once("error", done);
|
|
70
|
+
this.sink.end();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_destroy(
|
|
74
|
+
error: Error | null,
|
|
75
|
+
callback: (error?: Error | null) => void
|
|
76
|
+
): void {
|
|
77
|
+
// Only discard the cache entry when the audio stream did not finish.
|
|
78
|
+
if (!this.flushed && !this.sink.destroyed) this.sink.destroy();
|
|
79
|
+
callback(error);
|
|
80
|
+
}
|
|
81
|
+
}
|