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/src/utils/util.ts CHANGED
@@ -1,20 +1,14 @@
1
1
  import { type Player, Track, Util } from "discord-player";
2
- import { PassThrough, Readable } from "stream";
2
+ import { Readable } from "stream";
3
+ import { CacheTapStream } from "../Cache/CacheTapStream";
3
4
  import { JSDecryptor } from "../Crypto/JSDecryptor";
5
+ import { StripeDecryptStream } from "../Crypto/StripeDecryptStream";
4
6
  import type { DeezerExtractor } from "../DeezerExtractor";
7
+ import { getCrypto } from "./crypto";
5
8
 
6
- const IV = Buffer.from(Array.from({ length: 8 }, (_, x) => x));
7
-
8
- let rawCrypto: typeof import("crypto");
9
-
10
- export async function getCrypto() {
11
- if (!rawCrypto) {
12
- rawCrypto = await import("crypto");
13
- return rawCrypto;
14
- }
9
+ export { getCrypto } from "./crypto";
15
10
 
16
- return rawCrypto;
17
- }
11
+ const IV = Buffer.from(Array.from({ length: 8 }, (_, x) => x));
18
12
 
19
13
  export function isUrl(query: string) {
20
14
  try {
@@ -125,11 +119,26 @@ export async function searchOneTrack(query: string) {
125
119
  }
126
120
 
127
121
  export async function streamTrack(track: Track, ext: DeezerExtractor) {
122
+ const cache = ext.options.cache;
123
+ const trackId = extractTrackId(track.url);
124
+
125
+ if (cache) {
126
+ try {
127
+ const cached = await cache.read(trackId);
128
+
129
+ if (cached) {
130
+ ext.context.player.debug(`DEEZER CACHE HIT ${trackId}`);
131
+ return cached;
132
+ }
133
+ } catch (error) {
134
+ ext.context.player.debug(`DEEZER CACHE READ ERROR ${String(error)}`);
135
+ }
136
+ }
137
+
128
138
  const decryptionKey = ext.options.decryptionKey;
129
139
  if (!decryptionKey) throw new Error("MISSING DEEZER DECRYPTION KEY"); // cant decrypt due to missing key
130
140
 
131
141
  const crypto = await getCrypto();
132
- const trackId = extractTrackId(track.url);
133
142
 
134
143
  const trackHash = crypto
135
144
  .createHash("md5")
@@ -140,9 +149,10 @@ export async function streamTrack(track: Track, ext: DeezerExtractor) {
140
149
  const Decryptor = ext.options.decryptor || JSDecryptor;
141
150
 
142
151
  for (let iter = 0; iter < 16; iter++)
143
- /* tslint:disable-next-line no-bitwise */
144
152
  trackKey.writeInt8(
153
+ /* tslint:disable-next-line no-bitwise */
145
154
  trackHash[iter].charCodeAt(0) ^
155
+ /* tslint:disable-next-line no-bitwise */
146
156
  trackHash[iter + 16].charCodeAt(0) ^
147
157
  decryptionKey[iter].charCodeAt(0),
148
158
  iter
@@ -205,48 +215,47 @@ export async function streamTrack(track: Track, ext: DeezerExtractor) {
205
215
  const trackReq = await ext.fetch(trackMediaUrl);
206
216
  // @ts-ignore
207
217
  const readable = Readable.fromWeb(trackReq.body);
208
- let buffer = Buffer.alloc(0);
209
- let i = 0;
210
218
 
211
- const bufferSize = 2048; // 2mb
219
+ const decryptor = new Decryptor(trackKey, IV);
220
+ const stream = new StripeDecryptStream(decryptor);
212
221
 
213
- const passThrough = new PassThrough();
222
+ // Piping (instead of draining in a "readable" listener) applies
223
+ // backpressure: the body is only pulled as the player consumes it, so a
224
+ // slow consumer won't cause the whole track to be downloaded into memory.
225
+ readable.on("error", (error) => stream.destroy(error));
226
+ readable.pipe(stream);
214
227
 
215
- const decryptor = new Decryptor(trackKey, IV);
228
+ // Release the HTTP body if the consumer aborts before the track ends.
229
+ stream.once("close", () => {
230
+ if (!stream.readableEnded && !readable.destroyed) readable.destroy();
231
+ });
216
232
 
217
- readable.on("readable", async () => {
218
- let chunk = null;
219
- while (true) {
220
- chunk = readable.read(bufferSize);
221
-
222
- if (!chunk) {
223
- if (readable.readableLength) {
224
- chunk = readable.read(readable.readableLength);
225
- buffer = Buffer.concat([buffer, chunk]);
226
- }
227
- break;
228
- } else {
229
- buffer = Buffer.concat([buffer, chunk]);
230
- }
233
+ if (!cache) return stream;
231
234
 
232
- while (buffer.length >= bufferSize) {
233
- const bufferSized = buffer.subarray(0, bufferSize);
235
+ try {
236
+ const sink = await cache.write(trackId);
234
237
 
235
- if (i % 3 === 0) {
236
- const decipher = await decryptor.decrypt(bufferSized);
237
- passThrough.write(decipher);
238
- } else {
239
- passThrough.write(bufferSized);
240
- }
238
+ sink.on("error", (error) => {
239
+ ext.context.player.debug(`DEEZER CACHE WRITE ERROR ${String(error)}`);
240
+ });
241
241
 
242
- i++;
242
+ const output = new CacheTapStream(sink);
243
+ stream.on("error", (error) => output.destroy(error));
243
244
 
244
- buffer = buffer.subarray(bufferSize);
245
- }
246
- }
247
- });
245
+ // On abort, discard the partial cache entry and tear down the pipeline.
246
+ output.once("close", () => {
247
+ if (output.readableEnded) return;
248
+ if (!stream.destroyed) stream.destroy();
249
+ if (!readable.destroyed) readable.destroy();
250
+ });
251
+
252
+ stream.pipe(output);
248
253
 
249
- return passThrough;
254
+ return output;
255
+ } catch (error) {
256
+ ext.context.player.debug(`DEEZER CACHE WRITE ERROR ${String(error)}`);
257
+ return stream;
258
+ }
250
259
  }
251
260
 
252
261
  export const deezerRegex = {