trigo / trigo-web /onnxruntime-node-redirect-fix.patch
k-l-lambda's picture
fix onnx model loading
4b26793
--- install-utils.js.backup 2025-11-21 07:38:06.154213581 +0000
+++ install-utils.js 2025-11-21 07:38:44.799438104 +0000
@@ -10,11 +10,27 @@
const os = require('os');
const AdmZip = require('adm-zip'); // Use adm-zip instead of spawn
-async function downloadFile(url, dest) {
+async function downloadFile(url, dest, maxRedirects = 5) {
return new Promise((resolve, reject) => {
+ if (maxRedirects < 0) {
+ reject(new Error('Too many redirects'));
+ return;
+ }
+
const file = fs.createWriteStream(dest);
https
.get(url, (res) => {
+ // Handle redirects (301, 302, 303, 307, 308)
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
+ file.close();
+ fs.unlinkSync(dest);
+ // Follow redirect
+ const redirectUrl = new URL(res.headers.location, url).toString();
+ console.log(`Following redirect to ${redirectUrl}`);
+ downloadFile(redirectUrl, dest, maxRedirects - 1).then(resolve).catch(reject);
+ return;
+ }
+
if (res.statusCode !== 200) {
file.close();
fs.unlinkSync(dest);
@@ -39,8 +55,13 @@
});
}
-async function downloadJson(url) {
+async function downloadJson(url, maxRedirects = 5) {
return new Promise((resolve, reject) => {
+ if (maxRedirects < 0) {
+ reject(new Error('Too many redirects'));
+ return;
+ }
+
https
.get(url, (res) => {
const { statusCode } = res;
@@ -50,6 +71,16 @@
reject(new Error('No response statud code from server.'));
return;
}
+
+ // Handle redirects (301, 302, 303, 307, 308)
+ if (statusCode >= 300 && statusCode < 400 && res.headers.location) {
+ // Follow redirect
+ const redirectUrl = new URL(res.headers.location, url).toString();
+ console.log(`Following redirect to ${redirectUrl}`);
+ downloadJson(redirectUrl, maxRedirects - 1).then(resolve).catch(reject);
+ return;
+ }
+
if (statusCode >= 400 && statusCode < 500) {
resolve(null);
return;