Skip to content

Commit

Permalink
Update packages/crypto/src/lib/crypto.ts
Browse files Browse the repository at this point in the history
Co-authored-by: Daryl Collins <[email protected]>
Signed-off-by: Anson <[email protected]>
  • Loading branch information
Ansonhkg and MaximusHaximus authored Apr 15, 2024
1 parent bcf633a commit 3e8b8bb
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions packages/crypto/src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,28 +377,30 @@ async function getAmdCert(url: string): Promise<Uint8Array> {
`[getAmdCert] Fetching AMD cert using proxy URL ${proxyUrl} due to CORS restrictions.`
);

let response;

try {
response = await fetch(proxyUrl);
if (!response.ok) {
async function fetchAsUint8Array(targetUrl) {
const res = await fetch(targetUrl);
if (!res.ok) {
throw new Error(`[getAmdCert] HTTP error! status: ${response.status}`);
}
const arrayBuffer = await res.arrayBuffer();
return new Uint8Array(arrayBuffer);
}

try {
return await fetchAsUint8Array(proxyUrl);
} catch (e) {
log(`[getAmdCert] Failed to fetch AMD cert from proxy:`, e);

// Try direct fetch only if proxy fails
log('[getAmdCert] Attempting to fetch directly without proxy.');
try {
response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (e) {
log('[getAmdCert] Direct fetch also failed:', e);
throw e; // Re-throw to signal that both methods failed
}
log(`[getAmdCert] Failed to fetch AMD cert from proxy:`, e);
}

// Try direct fetch only if proxy fails
log('[getAmdCert] Attempting to fetch directly without proxy.');
try {
return await fetchAsUint8Array(url);
} catch (e) {
log('[getAmdCert] Direct fetch also failed:', e);
throw e; // Re-throw to signal that both methods failed
}


const arrayBuffer = await response.arrayBuffer();
return new Uint8Array(arrayBuffer);
Expand Down

0 comments on commit 3e8b8bb

Please sign in to comment.