Skip to content

Commit

Permalink
[js/node] update install script to allow use proxy (#23242)
Browse files Browse the repository at this point in the history
### Description

Use `https.get` instead of `fetch` in ORT Nodejs binding package install
script.

### Motivation and Context

According to discussions in #23232, the package `global-agent` cannot
work with `fetch` API. To make it work with the proxy agent, this PR
replaces the `fetch` API with `https.get` in the install script.
  • Loading branch information
fs-eire authored and snnn committed Jan 8, 2025
1 parent 1d4fec2 commit 21eb8d4
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions js/node/script/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Step.1: Check if we should exit early
const os = require('os');
const fs = require('fs');
const https = require('https');
const path = require('path');
const tar = require('tar');
const { execFileSync } = require('child_process');
Expand Down Expand Up @@ -80,44 +81,62 @@ To use ONNX Runtime Node.js binding with CUDA v11 support, please follow the man
}.tgz`,
}[INSTALL_CUDA_FLAG || tryGetCudaVersion()];
console.log(`Downloading "${artifactUrl}"...`);
fetch(artifactUrl).then((res) => {
if (!res.ok) {
throw new Error(`Failed to download the binaries: ${res.status} ${res.statusText}.

const FILES = new Set([
'libonnxruntime_providers_tensorrt.so',
'libonnxruntime_providers_shared.so',
`libonnxruntime.so.${ORT_VERSION}`,
'libonnxruntime_providers_cuda.so',
]);

downloadAndExtract(artifactUrl, BIN_FOLDER, FILES);

async function downloadAndExtract(url, dest, files) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];

if (statusCode === 301 || statusCode === 302) {
downloadAndExtract(res.headers.location, dest, files).then(
(value) => resolve(value),
(reason) => reject(reason),
);
return;
} else if (statusCode !== 200) {
throw new Error(`Failed to download the binaries: ${res.statusCode} ${res.statusMessage}.
Use "--onnxruntime-node-install-cuda=skip" to skip the installation. You will still be able to use ONNX Runtime, but the CUDA EP will not be available.`);
}
}

// Extract the binaries

const FILES = new Set([
'libonnxruntime_providers_tensorrt.so',
'libonnxruntime_providers_shared.so',
`libonnxruntime.so.${ORT_VERSION}`,
'libonnxruntime_providers_cuda.so',
]);

Readable.fromWeb(res.body)
.pipe(
tar.t({
strict: true,
onentry: (entry) => {
const filename = path.basename(entry.path);
if (entry.type === 'File' && FILES.has(filename)) {
console.log(`Extracting "${filename}" to "${BIN_FOLDER}"...`);
entry.pipe(fs.createWriteStream(path.join(BIN_FOLDER, filename)));
entry.on('finish', () => {
console.log(`Finished extracting "${filename}".`);
});
}
},
}),
)
.on('error', (err) => {
throw new Error(`Failed to extract the binaries: ${err.message}.
if (!contentType || !/^application\/octet-stream/.test(contentType)) {
throw new Error(`unexpected content type: ${contentType}`);
}

res
.pipe(
tar.t({
strict: true,
onentry: (entry) => {
const filename = path.basename(entry.path);
if (entry.type === 'File' && files.has(filename)) {
console.log(`Extracting "${filename}" to "${dest}"...`);
entry.pipe(fs.createWriteStream(path.join(dest, filename)));
entry.on('finish', () => {
console.log(`Finished extracting "${filename}".`);
});
}
},
}),
)
.on('error', (err) => {
throw new Error(`Failed to extract the binaries: ${err.message}.
Use "--onnxruntime-node-install-cuda=skip" to skip the installation. You will still be able to use ONNX Runtime, but the CUDA EP will not be available.`);
});
});
});
});
}

function tryGetCudaVersion() {
// Should only return 11 or 12.
Expand Down

0 comments on commit 21eb8d4

Please sign in to comment.