gerdur-core 2.8.0 → 2.9.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/CHANGELOG.md +16 -0
- package/README.md +9 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/get-url.d.ts +5 -2
- package/dist/lib/get-url.js +14 -13
- package/dist/lib/session-augment.d.ts +20 -0
- package/dist/lib/session-augment.js +23 -0
- package/dist/lib/stream-download.d.ts +11 -0
- package/dist/lib/stream-download.js +26 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2.9.0 - 2026-08-31
|
|
4
|
+
|
|
5
|
+
Phase 3.2 (cont.) — downloads are session-aware. Additive.
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **`Session.getTrackDownloadUrl` / `resolveDownloadUrls` / `streamTrack` /
|
|
10
|
+
`getTrackBuffer`** — resolve and download **as a specific account**, so a
|
|
11
|
+
`createSession(arl)` client is now end-to-end usable.
|
|
12
|
+
- **`downloadTrackBuffer(track, quality, options?)`** — download + decrypt a
|
|
13
|
+
track fully into a `Buffer` (no tagging), free-function form of
|
|
14
|
+
`session.getTrackBuffer`.
|
|
15
|
+
- `getTrackDownloadUrl(track, quality, session?)`, `resolveDownloadUrls(tracks,
|
|
16
|
+
qualities?, session?)` and `streamTrackDownload(track, quality, {session})`
|
|
17
|
+
gained an optional session argument (defaults to the process default).
|
|
18
|
+
|
|
3
19
|
## 2.8.0 - 2026-08-31
|
|
4
20
|
|
|
5
21
|
Phase 3.2 (cont.) — a `Session` is now usable per-account. Additive; free
|
package/README.md
CHANGED
|
@@ -127,9 +127,18 @@ responses carry account-specific `TRACK_TOKEN`s) and its own query methods:
|
|
|
127
127
|
| `getArtistInfo(id)` / `getDiscography(id, nb?)` | artist metadata / discography |
|
|
128
128
|
| `getProfile(userId)` | a public profile |
|
|
129
129
|
| `searchMusic(query, types?, nb?)` | search (`deezer.pageSearch`) |
|
|
130
|
+
| `getTrackDownloadUrl(track, quality)` | resolve a CDN URL **as this account** |
|
|
131
|
+
| `resolveDownloadUrls(tracks, qualities?)` | batch-resolve, one request, as this account |
|
|
132
|
+
| `streamTrack(track, quality, opts?)` | constant-memory stream of decrypted audio |
|
|
133
|
+
| `getTrackBuffer(track, quality, opts?)` | download + decrypt fully into a `Buffer` |
|
|
130
134
|
| `gw(body, method)` / `gwLight(body, method)` / `gwGet(method, params?)` | the raw coalesced request channels |
|
|
131
135
|
| `init(arl?)` / `refreshApiToken()` / `loadUserData(force?)` / `invalidateUserData()` | lifecycle |
|
|
132
136
|
|
|
137
|
+
The free `getTrackDownloadUrl(track, quality, session?)` / `resolveDownloadUrls(…, session?)`
|
|
138
|
+
/ `streamTrackDownload(…, {session})` all take an optional session too;
|
|
139
|
+
`downloadTrackBuffer(track, quality, opts?)` is the free-function form of
|
|
140
|
+
`session.getTrackBuffer`.
|
|
141
|
+
|
|
133
142
|
```js
|
|
134
143
|
await initDeezerApi(arl); // default session, as before
|
|
135
144
|
const track = await getTrackInfo('3135556');
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.getStream = exports.getText = exports.getJson = exports.getBuffer = exports.httpsAgent = exports.httpAgent = exports.DeezerError = exports.DEFAULT_ARL = exports.RETRY_POLICY = exports.Session = exports.defaultSession = exports.createSession = exports.initDeezerApi = void 0;
|
|
18
|
+
require("./lib/session-augment"); // wires the download methods onto Session.prototype
|
|
18
19
|
var session_1 = require("./lib/session");
|
|
19
20
|
Object.defineProperty(exports, "initDeezerApi", { enumerable: true, get: function () { return session_1.initDeezerApi; } });
|
|
20
21
|
Object.defineProperty(exports, "createSession", { enumerable: true, get: function () { return session_1.createSession; } });
|
package/dist/lib/get-url.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Session } from '../lib/session';
|
|
1
2
|
import type { trackType } from '../types';
|
|
2
3
|
export declare class WrongLicense extends Error {
|
|
3
4
|
constructor(format: string);
|
|
@@ -34,8 +35,9 @@ export declare const formatName: (quality: Quality) => string;
|
|
|
34
35
|
/**
|
|
35
36
|
* @param track Track info json returned from `getTrackInfo`
|
|
36
37
|
* @param quality 1 = 128kbps, 3 = 320kbps and 9 = flac (around 1411kbps)
|
|
38
|
+
* @param session which account to resolve for — defaults to the process default
|
|
37
39
|
*/
|
|
38
|
-
export declare const getTrackDownloadUrl: (track: trackType, quality: number) => Promise<{
|
|
40
|
+
export declare const getTrackDownloadUrl: (track: trackType, quality: number, session?: Session) => Promise<{
|
|
39
41
|
trackUrl: string;
|
|
40
42
|
isEncrypted: boolean;
|
|
41
43
|
fileSize: number;
|
|
@@ -60,5 +62,6 @@ export interface ResolvedUrl {
|
|
|
60
62
|
*
|
|
61
63
|
* @param tracks from `getTrackInfo` / `parseInfo` (needs `TRACK_TOKEN`, `SNG_ID`, `FILESIZE_*`)
|
|
62
64
|
* @param qualities preference order — default `[9, 3, 1]` (FLAC → MP3 320 → MP3 128)
|
|
65
|
+
* @param session which account to resolve for — defaults to the process default
|
|
63
66
|
*/
|
|
64
|
-
export declare const resolveDownloadUrls: (tracks: trackType[], qualities?: Quality[]) => Promise<(ResolvedUrl | null)[]>;
|
|
67
|
+
export declare const resolveDownloadUrls: (tracks: trackType[], qualities?: Quality[], session?: Session) => Promise<(ResolvedUrl | null)[]>;
|
package/dist/lib/get-url.js
CHANGED
|
@@ -8,7 +8,6 @@ const delay_1 = __importDefault(require("delay"));
|
|
|
8
8
|
const decrypt_1 = require("../lib/decrypt");
|
|
9
9
|
const errors_1 = require("../lib/errors");
|
|
10
10
|
const http_1 = require("../lib/http");
|
|
11
|
-
const request_1 = __importDefault(require("../lib/request"));
|
|
12
11
|
const session_1 = require("../lib/session");
|
|
13
12
|
class WrongLicense extends Error {
|
|
14
13
|
constructor(format) {
|
|
@@ -92,11 +91,11 @@ const MEDIA_MAX_RETRIES = 3;
|
|
|
92
91
|
const formatName = (quality) => (0, exports.toFormat)(quality);
|
|
93
92
|
exports.formatName = formatName;
|
|
94
93
|
/** POST media.deezer.com/v1/get_url with re-auth + exponential-backoff retry on 403/429/5xx. */
|
|
95
|
-
const mediaGetUrl = async (track_tokens, formats, attempt = 0) => {
|
|
94
|
+
const mediaGetUrl = async (session, track_tokens, formats, attempt = 0) => {
|
|
96
95
|
var _a;
|
|
97
|
-
const user = await
|
|
96
|
+
const user = await session.loadUserData();
|
|
98
97
|
try {
|
|
99
|
-
const { data } = await
|
|
98
|
+
const { data } = await session.request('POST', 'https://media.deezer.com/v1/get_url', {
|
|
100
99
|
license_token: user.licenseToken,
|
|
101
100
|
media: [{ type: 'FULL', formats }],
|
|
102
101
|
track_tokens,
|
|
@@ -107,9 +106,9 @@ const mediaGetUrl = async (track_tokens, formats, attempt = 0) => {
|
|
|
107
106
|
const status = err instanceof http_1.HttpStatusError ? err.statusCode : 0;
|
|
108
107
|
if ((status === 403 || status === 429 || status >= 500) && attempt < MEDIA_MAX_RETRIES) {
|
|
109
108
|
// a stale license token is a common cause — force a refresh before the retry
|
|
110
|
-
await
|
|
109
|
+
await session.loadUserData(true);
|
|
111
110
|
await (0, delay_1.default)(500 * 2 ** attempt + Math.floor(Math.random() * 250));
|
|
112
|
-
return mediaGetUrl(track_tokens, formats, attempt + 1);
|
|
111
|
+
return mediaGetUrl(session, track_tokens, formats, attempt + 1);
|
|
113
112
|
}
|
|
114
113
|
throw err;
|
|
115
114
|
}
|
|
@@ -132,12 +131,12 @@ const parseMediaEntry = (entry, token, country) => {
|
|
|
132
131
|
};
|
|
133
132
|
/** Whether a track fetched with the given cipher needs Blowfish stripe decryption. */
|
|
134
133
|
const cipherIsEncrypted = (cipher, url) => cipher ? cipher !== 'NONE' : url.includes('/mobile/') || url.includes('/media/');
|
|
135
|
-
const getTrackUrlFromServer = async (track_token, format) => {
|
|
136
|
-
const user = await
|
|
134
|
+
const getTrackUrlFromServer = async (session, track_token, format) => {
|
|
135
|
+
const user = await session.loadUserData();
|
|
137
136
|
if ((format === 'FLAC' && !user.canStreamLossless) || (format === 'MP3_320' && !user.canStreamHq)) {
|
|
138
137
|
throw new WrongLicense(format);
|
|
139
138
|
}
|
|
140
|
-
const { data, country } = await mediaGetUrl([track_token], [{ format, cipher: 'BF_CBC_STRIPE' }]);
|
|
139
|
+
const { data, country } = await mediaGetUrl(session, [track_token], [{ format, cipher: 'BF_CBC_STRIPE' }]);
|
|
141
140
|
if (!data.length)
|
|
142
141
|
return null;
|
|
143
142
|
return parseMediaEntry(data[0], track_token, country);
|
|
@@ -145,8 +144,9 @@ const getTrackUrlFromServer = async (track_token, format) => {
|
|
|
145
144
|
/**
|
|
146
145
|
* @param track Track info json returned from `getTrackInfo`
|
|
147
146
|
* @param quality 1 = 128kbps, 3 = 320kbps and 9 = flac (around 1411kbps)
|
|
147
|
+
* @param session which account to resolve for — defaults to the process default
|
|
148
148
|
*/
|
|
149
|
-
const getTrackDownloadUrl = async (track, quality) => {
|
|
149
|
+
const getTrackDownloadUrl = async (track, quality, session = (0, session_1.defaultSession)()) => {
|
|
150
150
|
let wrongLicense = null;
|
|
151
151
|
let geoBlocked = null;
|
|
152
152
|
let expiredToken = null;
|
|
@@ -161,7 +161,7 @@ const getTrackDownloadUrl = async (track, quality) => {
|
|
|
161
161
|
else {
|
|
162
162
|
// Get URL with the official API
|
|
163
163
|
try {
|
|
164
|
-
const resolved = await getTrackUrlFromServer(track.TRACK_TOKEN, format);
|
|
164
|
+
const resolved = await getTrackUrlFromServer(session, track.TRACK_TOKEN, format);
|
|
165
165
|
if (resolved) {
|
|
166
166
|
return {
|
|
167
167
|
trackUrl: resolved.url,
|
|
@@ -237,12 +237,13 @@ const testUrl = async (url) => {
|
|
|
237
237
|
*
|
|
238
238
|
* @param tracks from `getTrackInfo` / `parseInfo` (needs `TRACK_TOKEN`, `SNG_ID`, `FILESIZE_*`)
|
|
239
239
|
* @param qualities preference order — default `[9, 3, 1]` (FLAC → MP3 320 → MP3 128)
|
|
240
|
+
* @param session which account to resolve for — defaults to the process default
|
|
240
241
|
*/
|
|
241
|
-
const resolveDownloadUrls = async (tracks, qualities = [9, 3, 1]) => {
|
|
242
|
+
const resolveDownloadUrls = async (tracks, qualities = [9, 3, 1], session = (0, session_1.defaultSession)()) => {
|
|
242
243
|
if (!tracks.length)
|
|
243
244
|
return [];
|
|
244
245
|
const formats = qualities.map((q) => ({ format: (0, exports.formatName)(q), cipher: 'BF_CBC_STRIPE' }));
|
|
245
|
-
const { data, country } = await mediaGetUrl(tracks.map((t) => t.TRACK_TOKEN), formats);
|
|
246
|
+
const { data, country } = await mediaGetUrl(session, tracks.map((t) => t.TRACK_TOKEN), formats);
|
|
246
247
|
return tracks.map((track, i) => {
|
|
247
248
|
let parsed;
|
|
248
249
|
try {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Quality, ResolvedUrl } from './get-url';
|
|
3
|
+
import type { StreamTrackOptions, TrackStream } from './stream-download';
|
|
4
|
+
import type { trackType } from '../types';
|
|
5
|
+
declare module './session' {
|
|
6
|
+
interface Session {
|
|
7
|
+
/** Resolve a downloadable URL for a track **as this account** (`1 | 3 | 9`). */
|
|
8
|
+
getTrackDownloadUrl(track: trackType, quality: number): Promise<{
|
|
9
|
+
trackUrl: string;
|
|
10
|
+
isEncrypted: boolean;
|
|
11
|
+
fileSize: number;
|
|
12
|
+
} | null>;
|
|
13
|
+
/** Batch-resolve download URLs for many tracks in one request, as this account. */
|
|
14
|
+
resolveDownloadUrls(tracks: trackType[], qualities?: Quality[]): Promise<(ResolvedUrl | null)[]>;
|
|
15
|
+
/** Download a track as a constant-memory stream of decrypted audio, as this account. */
|
|
16
|
+
streamTrack(track: trackType, quality: number, options?: Omit<StreamTrackOptions, 'session'>): Promise<TrackStream>;
|
|
17
|
+
/** Download + decrypt a track fully into a `Buffer`, as this account. */
|
|
18
|
+
getTrackBuffer(track: trackType, quality: number, options?: Omit<StreamTrackOptions, 'session' | 'resumeFrom'>): Promise<Buffer | null>;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Download methods on {@link Session}, kept in a separate module so `session.ts`
|
|
5
|
+
* doesn't have to import the download stack (which imports `session.ts` back).
|
|
6
|
+
* `import`ing this file for its side effect wires the methods onto the prototype;
|
|
7
|
+
* `src/index.ts` does that.
|
|
8
|
+
*/
|
|
9
|
+
const get_url_1 = require("./get-url");
|
|
10
|
+
const session_1 = require("./session");
|
|
11
|
+
const stream_download_1 = require("./stream-download");
|
|
12
|
+
session_1.Session.prototype.getTrackDownloadUrl = function (track, quality) {
|
|
13
|
+
return (0, get_url_1.getTrackDownloadUrl)(track, quality, this);
|
|
14
|
+
};
|
|
15
|
+
session_1.Session.prototype.resolveDownloadUrls = function (tracks, qualities) {
|
|
16
|
+
return (0, get_url_1.resolveDownloadUrls)(tracks, qualities, this);
|
|
17
|
+
};
|
|
18
|
+
session_1.Session.prototype.streamTrack = function (track, quality, options = {}) {
|
|
19
|
+
return (0, stream_download_1.streamTrackDownload)(track, quality, { ...options, session: this });
|
|
20
|
+
};
|
|
21
|
+
session_1.Session.prototype.getTrackBuffer = function (track, quality, options = {}) {
|
|
22
|
+
return (0, stream_download_1.downloadTrackBuffer)(track, quality, { ...options, session: this });
|
|
23
|
+
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import { Readable } from 'stream';
|
|
4
|
+
import type { Session } from './session';
|
|
3
5
|
import type { trackType } from '../types';
|
|
4
6
|
export interface StreamTrackOptions {
|
|
5
7
|
/**
|
|
@@ -10,6 +12,8 @@ export interface StreamTrackOptions {
|
|
|
10
12
|
resumeFrom?: number;
|
|
11
13
|
/** progress callback — `(bytesReceived, totalBytes)`; `total` is 0 when unknown */
|
|
12
14
|
onProgress?: (received: number, total: number) => void;
|
|
15
|
+
/** which account to download as — defaults to the process default session */
|
|
16
|
+
session?: Session;
|
|
13
17
|
}
|
|
14
18
|
export interface TrackStream {
|
|
15
19
|
/** decrypted audio bytes, ready to pipe to a file or a tag muxer */
|
|
@@ -35,3 +39,10 @@ export interface TrackStream {
|
|
|
35
39
|
* track+quality can't be resolved at all.
|
|
36
40
|
*/
|
|
37
41
|
export declare const streamTrackDownload: (track: trackType, quality: number, options?: StreamTrackOptions) => Promise<TrackStream>;
|
|
42
|
+
/**
|
|
43
|
+
* Download + decrypt a track fully into memory. Convenience over
|
|
44
|
+
* {@link streamTrackDownload} for callers that just want the bytes (no tagging —
|
|
45
|
+
* pipe through `addTrackTags` yourself). `null` when the track+quality can't be
|
|
46
|
+
* resolved. Does **not** support resume.
|
|
47
|
+
*/
|
|
48
|
+
export declare const downloadTrackBuffer: (track: trackType, quality: number, options?: Omit<StreamTrackOptions, 'resumeFrom'>) => Promise<Buffer | null>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.streamTrackDownload = void 0;
|
|
3
|
+
exports.downloadTrackBuffer = exports.streamTrackDownload = void 0;
|
|
4
4
|
const stream_1 = require("stream");
|
|
5
5
|
const decrypt_1 = require("./decrypt");
|
|
6
6
|
const http_1 = require("./http");
|
|
@@ -20,7 +20,7 @@ const CHUNK = 2048;
|
|
|
20
20
|
* track+quality can't be resolved at all.
|
|
21
21
|
*/
|
|
22
22
|
const streamTrackDownload = async (track, quality, options = {}) => {
|
|
23
|
-
const resolved = await (0, get_url_1.getTrackDownloadUrl)(track, quality);
|
|
23
|
+
const resolved = await (0, get_url_1.getTrackDownloadUrl)(track, quality, options.session);
|
|
24
24
|
if (!resolved) {
|
|
25
25
|
throw new Error(`Track ${track.SNG_ID} is unavailable at quality ${quality}`);
|
|
26
26
|
}
|
|
@@ -52,3 +52,27 @@ const streamTrackDownload = async (track, quality, options = {}) => {
|
|
|
52
52
|
};
|
|
53
53
|
};
|
|
54
54
|
exports.streamTrackDownload = streamTrackDownload;
|
|
55
|
+
/**
|
|
56
|
+
* Download + decrypt a track fully into memory. Convenience over
|
|
57
|
+
* {@link streamTrackDownload} for callers that just want the bytes (no tagging —
|
|
58
|
+
* pipe through `addTrackTags` yourself). `null` when the track+quality can't be
|
|
59
|
+
* resolved. Does **not** support resume.
|
|
60
|
+
*/
|
|
61
|
+
const downloadTrackBuffer = async (track, quality, options = {}) => {
|
|
62
|
+
let ts;
|
|
63
|
+
try {
|
|
64
|
+
ts = await (0, exports.streamTrackDownload)(track, quality, { onProgress: options.onProgress, session: options.session });
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
if (err instanceof Error && /unavailable at quality/.test(err.message)) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
const chunks = [];
|
|
73
|
+
for await (const chunk of ts.stream) {
|
|
74
|
+
chunks.push(chunk);
|
|
75
|
+
}
|
|
76
|
+
return Buffer.concat(chunks);
|
|
77
|
+
};
|
|
78
|
+
exports.downloadTrackBuffer = downloadTrackBuffer;
|