add dispatcher support for all platforms

This commit is contained in:
aria
2024-08-05 01:04:01 -04:00
parent 99df8075b1
commit d1c262e31e
4 changed files with 65 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
import crypto from 'crypto'
import { fetch } from 'undici'
import { fetch, Dispatcher } from 'undici'
import {
ItemType,
StreamerWithLogin,
@@ -27,6 +27,7 @@ interface QobuzOptions {
appSecret: string
appId: string
token?: string
dispatcher?: Dispatcher
}
interface LoginResponse {
@@ -66,11 +67,13 @@ export default class Qobuz implements StreamerWithLogin {
token?: string
appSecret: string
appId: string
dispatcher: Dispatcher | undefined
fetch?(url: URL | RequestInfo, init?: RequestInit): Promise<Response>
constructor(options: QobuzOptions) {
this.appSecret = options.appSecret
this.appId = options.appId
if (options.token) this.token = options.token
if (options.dispatcher) this.dispatcher = options.dispatcher
}
async #get(url: string, params: { [key: string]: string | number }) {
@@ -83,7 +86,8 @@ export default class Qobuz implements StreamerWithLogin {
)}`,
{
method: 'get',
headers: headers(this.token)
headers: headers(this.token),
dispatcher: this.dispatcher
}
)
if (!response.ok) {
@@ -179,7 +183,7 @@ export default class Qobuz implements StreamerWithLogin {
)
if (trackFileResponse.sample == true)
throw new Error(`Could not get non-sample file. Make sure the track isn't purchase-only.`)
const streamResponse = await fetch(trackFileResponse.url)
const streamResponse = await fetch(trackFileResponse.url, { dispatcher: this.dispatcher })
return {
mimeType: trackFileResponse.mime_type,
sizeBytes: parseInt(<string>streamResponse.headers.get('Content-Length')),