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.
Binary file
package/dist/index.d.ts CHANGED
@@ -1,8 +1,77 @@
1
1
  import { Playlist, Track as Track$1 } from '@mithron/deezer-music-metadata';
2
2
  import { Track, ExtractorStreamable, BaseExtractor, SearchQueryType, ExtractorSearchContext, Playlist as Playlist$1, ExtractorInfo, Player } from 'discord-player';
3
+ import { Readable, Writable, Transform, TransformCallback } from 'node:stream';
4
+ import { Readable as Readable$1 } from 'stream';
3
5
  import * as crypto from 'crypto';
4
- import { PassThrough } from 'stream';
5
- import Blowfish from 'blowfish-node';
6
+
7
+ /**
8
+ * Storage backend for decrypted audio.
9
+ *
10
+ * A cache is read before the track is fetched and written while the track is
11
+ * being streamed, so implementations should support concurrent streams.
12
+ */
13
+ interface IAudioCache {
14
+ /**
15
+ * Returns a readable stream for the cached track, or `null` when the key
16
+ * is not present.
17
+ */
18
+ read(key: string): Promise<Readable | null>;
19
+ /**
20
+ * Opens a writable for the given key. The entry is committed when the
21
+ * writable finishes and discarded if it is destroyed first.
22
+ */
23
+ write(key: string): Promise<Writable>;
24
+ /** Removes a single entry. Missing entries are ignored. */
25
+ delete(key: string): Promise<void>;
26
+ /** Applies the cache's age and size limits. */
27
+ cleanup(): Promise<void>;
28
+ /** Stops any background maintenance. Optional. */
29
+ close?(): void | Promise<void>;
30
+ }
31
+
32
+ interface DiskAudioCacheOptions {
33
+ /** Directory used to store decrypted tracks. */
34
+ directory?: string;
35
+ /**
36
+ * Maximum number of cached songs. Oldest entries are removed first.
37
+ * Set to `0` to disable the count limit.
38
+ */
39
+ maxSongs?: number;
40
+ /**
41
+ * Remove entries older than this many milliseconds.
42
+ * Set to `0` to disable age-based expiry.
43
+ */
44
+ maxAge?: number;
45
+ /**
46
+ * How often background cleanup runs, in milliseconds.
47
+ * Set to `0` to only clean up after writes.
48
+ */
49
+ cleanupInterval?: number;
50
+ }
51
+ /**
52
+ * Disk-backed audio cache. Keys are hashed, so arbitrary strings can be used
53
+ * without risking path traversal. Audio is written to a temporary file and
54
+ * renamed into place once complete, so partially downloaded tracks are never
55
+ * served.
56
+ */
57
+ declare class DiskAudioCache implements IAudioCache {
58
+ readonly directory: string;
59
+ readonly maxSongs: number;
60
+ readonly maxAge: number;
61
+ readonly cleanupInterval: number;
62
+ private readonly ready;
63
+ private cleaning?;
64
+ private timer?;
65
+ private closed;
66
+ constructor(options?: DiskAudioCacheOptions);
67
+ read(key: string): Promise<Readable | null>;
68
+ write(key: string): Promise<Writable>;
69
+ delete(key: string): Promise<void>;
70
+ cleanup(): Promise<void>;
71
+ close(): Promise<void>;
72
+ private pathFor;
73
+ private runCleanup;
74
+ }
6
75
 
7
76
  declare class BaseDecryptor {
8
77
  key: Buffer;
@@ -27,6 +96,8 @@ type DeezerExtractorOptions = {
27
96
  priority?: number;
28
97
  arl: string;
29
98
  decryptor?: typeof BaseDecryptor;
99
+ /** Opt-in cache for decrypted audio. */
100
+ cache?: IAudioCache;
30
101
  };
31
102
  type DeezerUserInfo = {
32
103
  cookie: string;
@@ -53,7 +124,42 @@ declare class DeezerExtractor extends BaseExtractor<DeezerExtractorOptions> {
53
124
  stream(info: Track): Promise<ExtractorStreamable>;
54
125
  }
55
126
 
127
+ /**
128
+ * Passes audio through while mirroring it into a cache writable.
129
+ *
130
+ * The readable side is driven by the consumer, so enabling the cache does not
131
+ * make the download eager: if playback stalls or is aborted, the source is
132
+ * not drained into the cache.
133
+ */
134
+ declare class CacheTapStream extends Transform {
135
+ private readonly sink;
136
+ private sinkFailed;
137
+ private flushed;
138
+ constructor(sink: Writable);
139
+ _transform(chunk: Buffer, _encoding: BufferEncoding, callback: TransformCallback): void;
140
+ _flush(callback: TransformCallback): void;
141
+ _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
142
+ }
143
+
144
+ /**
145
+ * Decrypts a Deezer stripe stream as it flows through.
146
+ *
147
+ * Using a `Transform` means the HTTP body is only pulled when the consumer
148
+ * actually reads: if playback/ffmpeg slows down, backpressure pauses the
149
+ * download instead of buffering the whole track in memory.
150
+ */
151
+ declare class StripeDecryptStream extends Transform {
152
+ private stripeIndex;
153
+ private remainder;
154
+ private readonly decryptor;
155
+ constructor(decryptor: BaseDecryptor);
156
+ _transform(chunk: Buffer, _encoding: BufferEncoding, callback: TransformCallback): Promise<void>;
157
+ _flush(callback: TransformCallback): Promise<void>;
158
+ private decodeStripe;
159
+ }
160
+
56
161
  declare function getCrypto(): Promise<typeof crypto>;
162
+
57
163
  declare function isUrl(query: string): boolean;
58
164
  declare function search(query: string, limit?: number): Promise<DeezerSearchTrackResponse>;
59
165
  type ArrayOrObject<T> = T | T[];
@@ -77,7 +183,7 @@ interface DeezerSearchTrackResponse {
77
183
  declare function buildTrackFromSearch(track: DeezerSearchTrackResponse, player: Player, requestedBy?: Track["requestedBy"]): Track<unknown>[];
78
184
  declare function extractTrackId(query: string): string;
79
185
  declare function searchOneTrack(query: string): Promise<DeezerSearchTrackResponse | undefined>;
80
- declare function streamTrack(track: Track, ext: DeezerExtractor): Promise<PassThrough>;
186
+ declare function streamTrack(track: Track, ext: DeezerExtractor): Promise<Readable$1 | CacheTapStream | StripeDecryptStream>;
81
187
  declare const deezerRegex: {
82
188
  readonly track: RegExp;
83
189
  readonly playlistNalbums: RegExp;
@@ -85,15 +191,43 @@ declare const deezerRegex: {
85
191
  };
86
192
  declare function validate(query: string): boolean;
87
193
 
194
+ /**
195
+ * Blowfish-CBC decryptor for Deezer's BF_CBC_STRIPE streams.
196
+ *
197
+ * `blowfish-node`'s `decode()` allocates (and copies) several times per call
198
+ * and runs the block cipher through helper functions and array destructuring.
199
+ * Since this is the hot path for every encrypted 2048-byte stripe, we use the
200
+ * library only to run the key schedule once and then decrypt in place with a
201
+ * tight loop.
202
+ */
88
203
  declare class JSDecryptor extends BaseDecryptor {
89
- bfDecryptor: Blowfish;
204
+ private readonly p;
205
+ private readonly s0;
206
+ private readonly s1;
207
+ private readonly s2;
208
+ private readonly s3;
209
+ private readonly iv0;
210
+ private readonly iv1;
90
211
  constructor(...args: ConstructorParameters<typeof BaseDecryptor>);
91
212
  decrypt(chunk: Buffer): Promise<Buffer>;
92
213
  }
93
214
 
215
+ /**
216
+ * Native (`bf-cbc`) decryptor.
217
+ *
218
+ * A `Decipher` keeps its expanded key schedule for as long as it lives, but
219
+ * `createDecipheriv()` rebuilds that schedule on every call. Creating one per
220
+ * 2048-byte stripe is therefore slower than the pure-JS decryptor. Instead we
221
+ * keep a single cipher for the whole track and repair the CBC chain at stripe
222
+ * boundaries: Deezer restarts every stripe from the same IV, while the cipher
223
+ * carries the previous stripe's last ciphertext block forward. Only the first
224
+ * block of each stripe differs, and it can be fixed with one XOR.
225
+ */
94
226
  declare class NodeDecryptor extends BaseDecryptor {
95
- constructor(...args: ConstructorParameters<typeof BaseDecryptor>);
227
+ private decipher?;
228
+ private previousCipherBlock?;
229
+ private firstChunk;
96
230
  decrypt(chunk: Buffer): Promise<Buffer>;
97
231
  }
98
232
 
99
- export { type ArrayOrObject, BaseDecryptor, DeezerExtractor, type DeezerExtractorOptions, type DeezerSearchTrackResponse, type DeezerUserInfo, JSDecryptor, NodeDecryptor, Warnings, buildTrackFromSearch, deezerRegex, extractTrackId, getCrypto, isUrl, search, searchOneTrack, streamTrack, validate };
233
+ export { type ArrayOrObject, BaseDecryptor, DeezerExtractor, type DeezerExtractorOptions, type DeezerSearchTrackResponse, type DeezerUserInfo, DiskAudioCache, type DiskAudioCacheOptions, type IAudioCache, JSDecryptor, NodeDecryptor, StripeDecryptStream, Warnings, buildTrackFromSearch, deezerRegex, extractTrackId, getCrypto, isUrl, search, searchOneTrack, streamTrack, validate };