onnxruntime-node 1.22.0-dev.20250418-c19a49615b → 1.22.0-rev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,306 +1,306 @@
1
- // Copyright (c) Microsoft Corporation. All rights reserved.
2
- // Licensed under the MIT License.
3
-
4
- 'use strict';
5
-
6
- const fs = require('fs');
7
- const https = require('https');
8
- const { execFileSync } = require('child_process');
9
- const path = require('path');
10
- const os = require('os');
11
- const AdmZip = require('adm-zip'); // Use adm-zip instead of spawn
12
-
13
- async function downloadFile(url, dest) {
14
- return new Promise((resolve, reject) => {
15
- const file = fs.createWriteStream(dest);
16
- https
17
- .get(url, (res) => {
18
- if (res.statusCode !== 200) {
19
- file.close();
20
- fs.unlinkSync(dest);
21
- reject(new Error(`Failed to download from ${url}. HTTP status code = ${res.statusCode}`));
22
- return;
23
- }
24
-
25
- res.pipe(file);
26
- file.on('finish', () => {
27
- file.close();
28
- resolve();
29
- });
30
- file.on('error', (err) => {
31
- fs.unlinkSync(dest);
32
- reject(err);
33
- });
34
- })
35
- .on('error', (err) => {
36
- fs.unlinkSync(dest);
37
- reject(err);
38
- });
39
- });
40
- }
41
-
42
- async function downloadJson(url) {
43
- return new Promise((resolve, reject) => {
44
- https
45
- .get(url, (res) => {
46
- const { statusCode } = res;
47
- const contentType = res.headers['content-type'];
48
-
49
- if (!statusCode) {
50
- reject(new Error('No response statud code from server.'));
51
- return;
52
- }
53
- if (statusCode >= 400 && statusCode < 500) {
54
- resolve(null);
55
- return;
56
- } else if (statusCode !== 200) {
57
- reject(new Error(`Failed to download build list. HTTP status code = ${statusCode}`));
58
- return;
59
- }
60
- if (!contentType || !/^application\/json/.test(contentType)) {
61
- reject(new Error(`unexpected content type: ${contentType}`));
62
- return;
63
- }
64
- res.setEncoding('utf8');
65
- let rawData = '';
66
- res.on('data', (chunk) => {
67
- rawData += chunk;
68
- });
69
- res.on('end', () => {
70
- try {
71
- resolve(JSON.parse(rawData));
72
- } catch (e) {
73
- reject(e);
74
- }
75
- });
76
- res.on('error', (err) => {
77
- reject(err);
78
- });
79
- })
80
- .on('error', (err) => {
81
- reject(err);
82
- });
83
- });
84
- }
85
-
86
- async function installPackages(packages, manifests, feeds) {
87
- // Step.1: resolve packages
88
- const resolvedPackages = new Map();
89
- for (const packageCandidates of packages) {
90
- // iterate all candidates from packagesInfo and try to find the first one that exists
91
- for (const { feed, version } of packageCandidates.versions) {
92
- const { type, index } = feeds[feed];
93
- const pkg = await resolvePackage(type, index, packageCandidates.name, version);
94
- if (pkg) {
95
- resolvedPackages.set(packageCandidates, pkg);
96
- break;
97
- }
98
- }
99
- if (!resolvedPackages.has(packageCandidates)) {
100
- throw new Error(`Failed to resolve package. No package exists for: ${JSON.stringify(packageCandidates)}`);
101
- }
102
- }
103
-
104
- // Step.2: download packages
105
- for (const [pkgInfo, pkg] of resolvedPackages) {
106
- const manifestsForPackage = manifests.filter((x) => x.packagesInfo === pkgInfo);
107
- await pkg.download(manifestsForPackage);
108
- }
109
- }
110
-
111
- async function resolvePackage(type, index, packageName, version) {
112
- // https://learn.microsoft.com/en-us/nuget/api/overview
113
- const nugetPackageUrlResolver = async (index, packageName, version) => {
114
- // STEP.1 - get Nuget package index
115
- const nugetIndex = await downloadJson(index);
116
- if (!nugetIndex) {
117
- throw new Error(`Failed to download Nuget index from ${index}`);
118
- }
119
-
120
- // STEP.2 - get the base url of "PackageBaseAddress/3.0.0"
121
- const packageBaseUrl = nugetIndex.resources.find((x) => x['@type'] === 'PackageBaseAddress/3.0.0')?.['@id'];
122
- if (!packageBaseUrl) {
123
- throw new Error(`Failed to find PackageBaseAddress in Nuget index`);
124
- }
125
-
126
- // STEP.3 - get the package version info
127
- const packageInfo = await downloadJson(`${packageBaseUrl}${packageName.toLowerCase()}/index.json`);
128
- if (!packageInfo.versions.includes(version.toLowerCase())) {
129
- throw new Error(`Failed to find specific package versions for ${packageName} in ${index}`);
130
- }
131
-
132
- // STEP.4 - generate the package URL
133
- const packageUrl = `${packageBaseUrl}${packageName.toLowerCase()}/${version.toLowerCase()}/${packageName.toLowerCase()}.${version.toLowerCase()}.nupkg`;
134
- const packageFileName = `${packageName.toLowerCase()}.${version.toLowerCase()}.nupkg`;
135
-
136
- return {
137
- download: async (manifests) => {
138
- if (manifests.length === 0) {
139
- return;
140
- }
141
-
142
- // Create a temporary directory
143
- const tempDir = path.join(os.tmpdir(), `onnxruntime-node-pkgs_${Date.now()}`);
144
- fs.mkdirSync(tempDir, { recursive: true });
145
-
146
- try {
147
- const packageFilePath = path.join(tempDir, packageFileName);
148
-
149
- // Download the NuGet package
150
- console.log(`Downloading ${packageUrl}`);
151
- await downloadFile(packageUrl, packageFilePath);
152
-
153
- // Load the NuGet package (which is a ZIP file)
154
- let zip;
155
- try {
156
- zip = new AdmZip(packageFilePath);
157
- } catch (err) {
158
- throw new Error(`Failed to open NuGet package: ${err.message}`);
159
- }
160
-
161
- // Extract only the needed files from the package
162
- const extractDir = path.join(tempDir, 'extracted');
163
- fs.mkdirSync(extractDir, { recursive: true });
164
-
165
- // Process each manifest and extract/copy files to their destinations
166
- for (const manifest of manifests) {
167
- const { filepath, pathInPackage } = manifest;
168
-
169
- // Create directory for the target file
170
- const targetDir = path.dirname(filepath);
171
- fs.mkdirSync(targetDir, { recursive: true });
172
-
173
- // Check if the file exists directly in the zip
174
- const zipEntry = zip.getEntry(pathInPackage);
175
- if (!zipEntry) {
176
- throw new Error(`Failed to find ${pathInPackage} in NuGet package`);
177
- }
178
-
179
- console.log(`Extracting ${pathInPackage} to ${filepath}`);
180
-
181
- // Extract just this entry to a temporary location
182
- const extractedFilePath = path.join(extractDir, path.basename(pathInPackage));
183
- zip.extractEntryTo(zipEntry, extractDir, false, true);
184
-
185
- // Copy to the final destination
186
- fs.copyFileSync(extractedFilePath, filepath);
187
- }
188
- } finally {
189
- // Clean up the temporary directory - always runs even if an error occurs
190
- try {
191
- fs.rmSync(tempDir, { recursive: true });
192
- } catch (e) {
193
- console.warn(`Failed to clean up temporary directory: ${tempDir}`, e);
194
- // Don't rethrow this error as it would mask the original error
195
- }
196
- }
197
- },
198
- };
199
- };
200
-
201
- switch (type) {
202
- case 'nuget':
203
- return await nugetPackageUrlResolver(index, packageName, version);
204
- default:
205
- throw new Error(`Unsupported package type: ${type}`);
206
- }
207
- }
208
-
209
- function tryGetCudaVersion() {
210
- // Should only return 11 or 12.
211
-
212
- // try to get the CUDA version from the system ( `nvcc --version` )
213
- let ver = 12;
214
- try {
215
- const nvccVersion = execFileSync('nvcc', ['--version'], { encoding: 'utf8' });
216
- const match = nvccVersion.match(/release (\d+)/);
217
- if (match) {
218
- ver = parseInt(match[1]);
219
- if (ver !== 11 && ver !== 12) {
220
- throw new Error(`Unsupported CUDA version: ${ver}`);
221
- }
222
- }
223
- } catch (e) {
224
- if (e?.code === 'ENOENT') {
225
- console.warn('`nvcc` not found. Assuming CUDA 12.');
226
- } else {
227
- console.warn('Failed to detect CUDA version from `nvcc --version`:', e.message);
228
- }
229
- }
230
-
231
- // assume CUDA 12 if failed to detect
232
- return ver;
233
- }
234
-
235
- function parseInstallFlag() {
236
- let flag = process.env.ONNXRUNTIME_NODE_INSTALL || process.env.npm_config_onnxruntime_node_install;
237
- if (!flag) {
238
- for (let i = 0; i < process.argv.length; i++) {
239
- if (process.argv[i].startsWith('--onnxruntime-node-install=')) {
240
- flag = process.argv[i].split('=')[1];
241
- break;
242
- } else if (process.argv[i] === '--onnxruntime-node-install') {
243
- flag = 'true';
244
- }
245
- }
246
- }
247
- switch (flag) {
248
- case 'true':
249
- case '1':
250
- case 'ON':
251
- return true;
252
- case 'skip':
253
- return false;
254
- case undefined: {
255
- flag = parseInstallCudaFlag();
256
- if (flag === 'skip') {
257
- return false;
258
- }
259
- if (flag === 11) {
260
- throw new Error('CUDA 11 is no longer supported. Please consider using CPU or upgrade to CUDA 12.');
261
- }
262
- if (flag === 12) {
263
- return 'cuda12';
264
- }
265
- return undefined;
266
- }
267
- default:
268
- if (!flag || typeof flag !== 'string') {
269
- throw new Error(`Invalid value for --onnxruntime-node-install: ${flag}`);
270
- }
271
- }
272
- }
273
-
274
- function parseInstallCudaFlag() {
275
- let flag = process.env.ONNXRUNTIME_NODE_INSTALL_CUDA || process.env.npm_config_onnxruntime_node_install_cuda;
276
- if (!flag) {
277
- for (let i = 0; i < process.argv.length; i++) {
278
- if (process.argv[i].startsWith('--onnxruntime-node-install-cuda=')) {
279
- flag = process.argv[i].split('=')[1];
280
- break;
281
- } else if (process.argv[i] === '--onnxruntime-node-install-cuda') {
282
- flag = 'true';
283
- }
284
- }
285
- }
286
- switch (flag) {
287
- case 'true':
288
- case '1':
289
- case 'ON':
290
- return tryGetCudaVersion();
291
- case 'v11':
292
- return 11;
293
- case 'v12':
294
- return 12;
295
- case 'skip':
296
- case undefined:
297
- return flag;
298
- default:
299
- throw new Error(`Invalid value for --onnxruntime-node-install-cuda: ${flag}`);
300
- }
301
- }
302
-
303
- module.exports = {
304
- installPackages,
305
- parseInstallFlag,
306
- };
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ 'use strict';
5
+
6
+ const fs = require('fs');
7
+ const https = require('https');
8
+ const { execFileSync } = require('child_process');
9
+ const path = require('path');
10
+ const os = require('os');
11
+ const AdmZip = require('adm-zip'); // Use adm-zip instead of spawn
12
+
13
+ async function downloadFile(url, dest) {
14
+ return new Promise((resolve, reject) => {
15
+ const file = fs.createWriteStream(dest);
16
+ https
17
+ .get(url, (res) => {
18
+ if (res.statusCode !== 200) {
19
+ file.close();
20
+ fs.unlinkSync(dest);
21
+ reject(new Error(`Failed to download from ${url}. HTTP status code = ${res.statusCode}`));
22
+ return;
23
+ }
24
+
25
+ res.pipe(file);
26
+ file.on('finish', () => {
27
+ file.close();
28
+ resolve();
29
+ });
30
+ file.on('error', (err) => {
31
+ fs.unlinkSync(dest);
32
+ reject(err);
33
+ });
34
+ })
35
+ .on('error', (err) => {
36
+ fs.unlinkSync(dest);
37
+ reject(err);
38
+ });
39
+ });
40
+ }
41
+
42
+ async function downloadJson(url) {
43
+ return new Promise((resolve, reject) => {
44
+ https
45
+ .get(url, (res) => {
46
+ const { statusCode } = res;
47
+ const contentType = res.headers['content-type'];
48
+
49
+ if (!statusCode) {
50
+ reject(new Error('No response statud code from server.'));
51
+ return;
52
+ }
53
+ if (statusCode >= 400 && statusCode < 500) {
54
+ resolve(null);
55
+ return;
56
+ } else if (statusCode !== 200) {
57
+ reject(new Error(`Failed to download build list. HTTP status code = ${statusCode}`));
58
+ return;
59
+ }
60
+ if (!contentType || !/^application\/json/.test(contentType)) {
61
+ reject(new Error(`unexpected content type: ${contentType}`));
62
+ return;
63
+ }
64
+ res.setEncoding('utf8');
65
+ let rawData = '';
66
+ res.on('data', (chunk) => {
67
+ rawData += chunk;
68
+ });
69
+ res.on('end', () => {
70
+ try {
71
+ resolve(JSON.parse(rawData));
72
+ } catch (e) {
73
+ reject(e);
74
+ }
75
+ });
76
+ res.on('error', (err) => {
77
+ reject(err);
78
+ });
79
+ })
80
+ .on('error', (err) => {
81
+ reject(err);
82
+ });
83
+ });
84
+ }
85
+
86
+ async function installPackages(packages, manifests, feeds) {
87
+ // Step.1: resolve packages
88
+ const resolvedPackages = new Map();
89
+ for (const packageCandidates of packages) {
90
+ // iterate all candidates from packagesInfo and try to find the first one that exists
91
+ for (const { feed, version } of packageCandidates.versions) {
92
+ const { type, index } = feeds[feed];
93
+ const pkg = await resolvePackage(type, index, packageCandidates.name, version);
94
+ if (pkg) {
95
+ resolvedPackages.set(packageCandidates, pkg);
96
+ break;
97
+ }
98
+ }
99
+ if (!resolvedPackages.has(packageCandidates)) {
100
+ throw new Error(`Failed to resolve package. No package exists for: ${JSON.stringify(packageCandidates)}`);
101
+ }
102
+ }
103
+
104
+ // Step.2: download packages
105
+ for (const [pkgInfo, pkg] of resolvedPackages) {
106
+ const manifestsForPackage = manifests.filter((x) => x.packagesInfo === pkgInfo);
107
+ await pkg.download(manifestsForPackage);
108
+ }
109
+ }
110
+
111
+ async function resolvePackage(type, index, packageName, version) {
112
+ // https://learn.microsoft.com/en-us/nuget/api/overview
113
+ const nugetPackageUrlResolver = async (index, packageName, version) => {
114
+ // STEP.1 - get Nuget package index
115
+ const nugetIndex = await downloadJson(index);
116
+ if (!nugetIndex) {
117
+ throw new Error(`Failed to download Nuget index from ${index}`);
118
+ }
119
+
120
+ // STEP.2 - get the base url of "PackageBaseAddress/3.0.0"
121
+ const packageBaseUrl = nugetIndex.resources.find((x) => x['@type'] === 'PackageBaseAddress/3.0.0')?.['@id'];
122
+ if (!packageBaseUrl) {
123
+ throw new Error(`Failed to find PackageBaseAddress in Nuget index`);
124
+ }
125
+
126
+ // STEP.3 - get the package version info
127
+ const packageInfo = await downloadJson(`${packageBaseUrl}${packageName.toLowerCase()}/index.json`);
128
+ if (!packageInfo.versions.includes(version.toLowerCase())) {
129
+ throw new Error(`Failed to find specific package versions for ${packageName} in ${index}`);
130
+ }
131
+
132
+ // STEP.4 - generate the package URL
133
+ const packageUrl = `${packageBaseUrl}${packageName.toLowerCase()}/${version.toLowerCase()}/${packageName.toLowerCase()}.${version.toLowerCase()}.nupkg`;
134
+ const packageFileName = `${packageName.toLowerCase()}.${version.toLowerCase()}.nupkg`;
135
+
136
+ return {
137
+ download: async (manifests) => {
138
+ if (manifests.length === 0) {
139
+ return;
140
+ }
141
+
142
+ // Create a temporary directory
143
+ const tempDir = path.join(os.tmpdir(), `onnxruntime-node-pkgs_${Date.now()}`);
144
+ fs.mkdirSync(tempDir, { recursive: true });
145
+
146
+ try {
147
+ const packageFilePath = path.join(tempDir, packageFileName);
148
+
149
+ // Download the NuGet package
150
+ console.log(`Downloading ${packageUrl}`);
151
+ await downloadFile(packageUrl, packageFilePath);
152
+
153
+ // Load the NuGet package (which is a ZIP file)
154
+ let zip;
155
+ try {
156
+ zip = new AdmZip(packageFilePath);
157
+ } catch (err) {
158
+ throw new Error(`Failed to open NuGet package: ${err.message}`);
159
+ }
160
+
161
+ // Extract only the needed files from the package
162
+ const extractDir = path.join(tempDir, 'extracted');
163
+ fs.mkdirSync(extractDir, { recursive: true });
164
+
165
+ // Process each manifest and extract/copy files to their destinations
166
+ for (const manifest of manifests) {
167
+ const { filepath, pathInPackage } = manifest;
168
+
169
+ // Create directory for the target file
170
+ const targetDir = path.dirname(filepath);
171
+ fs.mkdirSync(targetDir, { recursive: true });
172
+
173
+ // Check if the file exists directly in the zip
174
+ const zipEntry = zip.getEntry(pathInPackage);
175
+ if (!zipEntry) {
176
+ throw new Error(`Failed to find ${pathInPackage} in NuGet package`);
177
+ }
178
+
179
+ console.log(`Extracting ${pathInPackage} to ${filepath}`);
180
+
181
+ // Extract just this entry to a temporary location
182
+ const extractedFilePath = path.join(extractDir, path.basename(pathInPackage));
183
+ zip.extractEntryTo(zipEntry, extractDir, false, true);
184
+
185
+ // Copy to the final destination
186
+ fs.copyFileSync(extractedFilePath, filepath);
187
+ }
188
+ } finally {
189
+ // Clean up the temporary directory - always runs even if an error occurs
190
+ try {
191
+ fs.rmSync(tempDir, { recursive: true });
192
+ } catch (e) {
193
+ console.warn(`Failed to clean up temporary directory: ${tempDir}`, e);
194
+ // Don't rethrow this error as it would mask the original error
195
+ }
196
+ }
197
+ },
198
+ };
199
+ };
200
+
201
+ switch (type) {
202
+ case 'nuget':
203
+ return await nugetPackageUrlResolver(index, packageName, version);
204
+ default:
205
+ throw new Error(`Unsupported package type: ${type}`);
206
+ }
207
+ }
208
+
209
+ function tryGetCudaVersion() {
210
+ // Should only return 11 or 12.
211
+
212
+ // try to get the CUDA version from the system ( `nvcc --version` )
213
+ let ver = 12;
214
+ try {
215
+ const nvccVersion = execFileSync('nvcc', ['--version'], { encoding: 'utf8' });
216
+ const match = nvccVersion.match(/release (\d+)/);
217
+ if (match) {
218
+ ver = parseInt(match[1]);
219
+ if (ver !== 11 && ver !== 12) {
220
+ throw new Error(`Unsupported CUDA version: ${ver}`);
221
+ }
222
+ }
223
+ } catch (e) {
224
+ if (e?.code === 'ENOENT') {
225
+ console.warn('`nvcc` not found. Assuming CUDA 12.');
226
+ } else {
227
+ console.warn('Failed to detect CUDA version from `nvcc --version`:', e.message);
228
+ }
229
+ }
230
+
231
+ // assume CUDA 12 if failed to detect
232
+ return ver;
233
+ }
234
+
235
+ function parseInstallFlag() {
236
+ let flag = process.env.ONNXRUNTIME_NODE_INSTALL || process.env.npm_config_onnxruntime_node_install;
237
+ if (!flag) {
238
+ for (let i = 0; i < process.argv.length; i++) {
239
+ if (process.argv[i].startsWith('--onnxruntime-node-install=')) {
240
+ flag = process.argv[i].split('=')[1];
241
+ break;
242
+ } else if (process.argv[i] === '--onnxruntime-node-install') {
243
+ flag = 'true';
244
+ }
245
+ }
246
+ }
247
+ switch (flag) {
248
+ case 'true':
249
+ case '1':
250
+ case 'ON':
251
+ return true;
252
+ case 'skip':
253
+ return false;
254
+ case undefined: {
255
+ flag = parseInstallCudaFlag();
256
+ if (flag === 'skip') {
257
+ return false;
258
+ }
259
+ if (flag === 11) {
260
+ throw new Error('CUDA 11 is no longer supported. Please consider using CPU or upgrade to CUDA 12.');
261
+ }
262
+ if (flag === 12) {
263
+ return 'cuda12';
264
+ }
265
+ return undefined;
266
+ }
267
+ default:
268
+ if (!flag || typeof flag !== 'string') {
269
+ throw new Error(`Invalid value for --onnxruntime-node-install: ${flag}`);
270
+ }
271
+ }
272
+ }
273
+
274
+ function parseInstallCudaFlag() {
275
+ let flag = process.env.ONNXRUNTIME_NODE_INSTALL_CUDA || process.env.npm_config_onnxruntime_node_install_cuda;
276
+ if (!flag) {
277
+ for (let i = 0; i < process.argv.length; i++) {
278
+ if (process.argv[i].startsWith('--onnxruntime-node-install-cuda=')) {
279
+ flag = process.argv[i].split('=')[1];
280
+ break;
281
+ } else if (process.argv[i] === '--onnxruntime-node-install-cuda') {
282
+ flag = 'true';
283
+ }
284
+ }
285
+ }
286
+ switch (flag) {
287
+ case 'true':
288
+ case '1':
289
+ case 'ON':
290
+ return tryGetCudaVersion();
291
+ case 'v11':
292
+ return 11;
293
+ case 'v12':
294
+ return 12;
295
+ case 'skip':
296
+ case undefined:
297
+ return flag;
298
+ default:
299
+ throw new Error(`Invalid value for --onnxruntime-node-install-cuda: ${flag}`);
300
+ }
301
+ }
302
+
303
+ module.exports = {
304
+ installPackages,
305
+ parseInstallFlag,
306
+ };