drop node-fetch dependency, use undici

undici's fetch implementation is included in node and has been considered stable since version 21.
for some time I think depending on the undici package instead of the built-in is a good idea so that older nodejs versions will still work correctly.
This commit is contained in:
hazycora
2024-07-30 14:43:25 -04:00
committed by hazel
parent ac1fcc1a44
commit d7ceecb8a5
7 changed files with 26 additions and 66 deletions

View File

@@ -1,4 +1,4 @@
import fetch from 'node-fetch'
import { fetch } from 'undici'
import {
Album,
Artist,
@@ -24,7 +24,7 @@ import {
parseArtist,
parseTrack
} from './parse.js'
import { Transform } from 'stream'
import { Readable, Transform } from 'stream'
import { Blowfish } from 'blowfish-cbc'
interface DeezerOptions {
@@ -406,7 +406,7 @@ export default class Deezer implements StreamerWithLogin {
})
return {
stream: streamResp.body!.pipe(decryption),
stream: Readable.fromWeb(streamResp.body!).pipe(decryption),
mimeType
}
}

View File

@@ -1,5 +1,5 @@
import crypto from 'crypto'
import fetch from 'node-fetch'
import { fetch } from 'undici'
import {
ItemType,
StreamerWithLogin,
@@ -11,6 +11,7 @@ import {
} from '../../types.js'
import { DEFAULT_HEADERS } from './constants.js'
import { parseAlbum, parseTrack, parseArtist, RawAlbum, RawArtist, RawTrack } from './parse.js'
import { Readable } from 'stream'
function headers(token?: string): HeadersInit {
const headers: HeadersInit = DEFAULT_HEADERS
@@ -182,7 +183,7 @@ export default class Qobuz implements StreamerWithLogin {
return {
mimeType: trackFileResponse.mime_type,
sizeBytes: parseInt(<string>streamResponse.headers.get('Content-Length')),
stream: <NodeJS.ReadableStream>streamResponse.body
stream: Readable.fromWeb(streamResponse.body!)
}
}

View File

@@ -1,4 +1,4 @@
import fetch, { HeadersInit } from 'node-fetch'
import { fetch, HeadersInit } from 'undici'
import { DEFAULT_HEADERS, SC_VERSION } from './constants.js'
import {
ItemType,
@@ -20,6 +20,7 @@ import {
RawArtist,
ScClient
} from './parse.js'
import { Readable } from 'stream'
function headers(oauthToken?: string | undefined): HeadersInit {
const headers: HeadersInit = DEFAULT_HEADERS
@@ -307,8 +308,8 @@ async function getStream(
const streamResp = await fetch(json.url)
return {
mimeType: transcoding.format.mime_type,
sizeBytes: parseInt(<string>streamResp.headers.get('Content-Length')),
stream: <NodeJS.ReadableStream>streamResp.body
sizeBytes: parseInt(streamResp.headers.get('Content-Length')!),
stream: Readable.fromWeb(streamResp.body!)
}
} else {
const container = transcoding.format.mime_type.split('/')[1].split(';')[0].split('+')[0]

View File

@@ -1,6 +1,6 @@
import { Artist, Album, Track } from '../../types.js'
import { spawn } from 'child_process'
import fetch from 'node-fetch'
import { fetch } from 'undici'
import { imageSize as sizeOf } from 'image-size'
import os from 'node:os'
import fs from 'node:fs'
@@ -9,11 +9,8 @@ import path from 'node:path'
async function parseCoverArtwork(url: string) {
const resp = await fetch(url)
if (!resp.body) throw new Error('No body on image')
const chunks = []
for await (const chunk of resp.body) {
chunks.push(Buffer.from(chunk))
}
const dimensions = sizeOf(Buffer.concat(chunks))
const body = await resp.arrayBuffer()
const dimensions = sizeOf(new Uint8Array(body))
if (!dimensions.width || !dimensions.height) throw new Error(`Couldn't get dimensions`)
return {
url,

View File

@@ -1,4 +1,4 @@
import fetch from 'node-fetch'
import { fetch } from 'undici'
import { spawn } from 'child_process'
import {
ItemType,
@@ -21,7 +21,7 @@ import {
parseMpd,
parseTrack
} from './parse.js'
import Stream from 'stream'
import Stream, { Readable } from 'stream'
interface TidalOptions {
tvToken: string
@@ -354,7 +354,7 @@ export default class Tidal implements Streamer {
return {
mimeType: manifest.mimeType,
sizeBytes: parseInt(<string>streamResponse.headers.get('Content-Length')),
stream: <NodeJS.ReadableStream>streamResponse.body
stream: Readable.fromWeb(streamResponse.body!)
}
}