From 56ce9c9c6b9bfef2f65505b7dc7d7901403ab0da Mon Sep 17 00:00:00 2001 From: Dylan Jeffers Date: Tue, 14 Apr 2026 17:44:54 -0700 Subject: [PATCH] Fix download stem --- .../createStemsArchive/createStemsArchive.ts | 40 ++++--------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/apps/archiver/src/workers/createStemsArchive/createStemsArchive.ts b/apps/archiver/src/workers/createStemsArchive/createStemsArchive.ts index def7494..63aea06 100644 --- a/apps/archiver/src/workers/createStemsArchive/createStemsArchive.ts +++ b/apps/archiver/src/workers/createStemsArchive/createStemsArchive.ts @@ -13,31 +13,12 @@ import { import path from 'path' import { WorkerServices } from '../services' import { createUtils } from './utils' -import fetch from 'node-fetch' type StemsArchiveWorkerListener = WorkerListener< StemsArchiveJobData, StemsArchiveJobResult > -const getRedirectUrl = async (url: string) => { - try { - const response = await fetch(url, { - method: 'GET', - redirect: 'manual' - }) - - if (response.status >= 300 && response.status < 400) { - const redirectUrl = response.headers.get('Location') - return redirectUrl ?? url - } else { - return url - } - } catch (error) { - throw new Error(`Fetch failed: ${(error as Error).message}`) - } -} - export const createStemsArchiveWorker = (services: WorkerServices) => { const { config, spaceManager, fs, sdk } = services const workerLogger = services.logger.child({ @@ -166,11 +147,16 @@ export const createStemsArchiveWorker = (services: WorkerServices) => { // Start all downloads immediately so we can await allSettled after a failure: // if one rejects, Promise.all would run the outer catch and removeTempFiles while // siblings are still writing — ENOENT on other WriteStreams and process crash. + // The API's /tracks/{id}/download endpoint already calls tryFindWorkingUrl + // to pick a content node mirror that actually serves this file, and + // responds with a 302 redirect to it. node-fetch follows 3xx by default, + // so we can just point downloadFile at the API URL and let it land on the + // verified-working host. Do NOT rewrite the host — forcing every download + // through a single node (e.g. creatornode2) bypasses that mirror selection + // and produces 404s on files that exist, just not on that node. const downloadPromises = filesToDownload.map( async (stem: { id: string; origFilename?: string }) => { - let url - - const downloadUrl = await sdk.tracks.getTrackDownloadUrl({ + const url = await sdk.tracks.getTrackDownloadUrl({ trackId: stem.id, userId: hashedUserId, userSignature: signatureHeader, @@ -178,16 +164,6 @@ export const createStemsArchiveWorker = (services: WorkerServices) => { filename: stem.origFilename ?? '' }) - if (config.environment === 'prod') { - url = downloadUrl - const redirectUrl = await getRedirectUrl(downloadUrl) - const modifiedUrl = new URL(redirectUrl) - modifiedUrl.host = 'creatornode2.audius.co' - url = modifiedUrl.toString() - } else { - url = downloadUrl - } - const filePath = path.join(jobTempDir, stem.origFilename ?? 'file') return downloadFile({ url,