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

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.
Files changed (46) hide show
  1. package/README.md +19 -14
  2. package/__commit.txt +1 -0
  3. package/bin/{napi-v3/darwin/arm64/libonnxruntime.1.21.0.dylib → napi-v6/darwin/arm64/libonnxruntime.1.22.0.dylib} +0 -0
  4. package/bin/napi-v6/darwin/arm64/onnxruntime_binding.node +0 -0
  5. package/bin/{napi-v3/darwin/x64/libonnxruntime.1.21.0.dylib → napi-v6/darwin/x64/libonnxruntime.1.22.0.dylib} +0 -0
  6. package/bin/napi-v6/darwin/x64/onnxruntime_binding.node +0 -0
  7. package/bin/{napi-v3 → napi-v6}/linux/arm64/libonnxruntime.so.1 +0 -0
  8. package/bin/napi-v6/linux/arm64/onnxruntime_binding.node +0 -0
  9. package/bin/napi-v6/linux/x64/libonnxruntime.so.1 +0 -0
  10. package/bin/napi-v6/linux/x64/onnxruntime_binding.node +0 -0
  11. package/bin/{napi-v3 → napi-v6}/win32/arm64/DirectML.dll +0 -0
  12. package/bin/{napi-v3/linux/x64/libonnxruntime.so.1 → napi-v6/win32/arm64/dxcompiler.dll} +0 -0
  13. package/bin/napi-v6/win32/arm64/dxil.dll +0 -0
  14. package/bin/{napi-v3/linux/x64/libonnxruntime.so.1.21.0 → napi-v6/win32/arm64/onnxruntime.dll} +0 -0
  15. package/bin/napi-v6/win32/arm64/onnxruntime_binding.node +0 -0
  16. package/bin/{napi-v3 → napi-v6}/win32/x64/DirectML.dll +0 -0
  17. package/bin/{napi-v3/linux/arm64/libonnxruntime.so.1.21.0 → napi-v6/win32/x64/dxcompiler.dll} +0 -0
  18. package/bin/napi-v6/win32/x64/dxil.dll +0 -0
  19. package/bin/{napi-v3/win32/arm64 → napi-v6/win32/x64}/onnxruntime.dll +0 -0
  20. package/bin/napi-v6/win32/x64/onnxruntime_binding.node +0 -0
  21. package/dist/backend.js +71 -2
  22. package/dist/backend.js.map +1 -1
  23. package/dist/binding.d.ts +9 -2
  24. package/dist/binding.js +1 -1
  25. package/dist/binding.js.map +1 -1
  26. package/dist/version.d.ts +1 -1
  27. package/dist/version.js +1 -1
  28. package/dist/version.js.map +1 -1
  29. package/lib/backend.ts +79 -2
  30. package/lib/binding.ts +10 -3
  31. package/lib/version.ts +1 -1
  32. package/package.json +4 -6
  33. package/script/build.js +92 -69
  34. package/script/install-metadata-versions.js +12 -0
  35. package/script/install-metadata.js +58 -0
  36. package/script/install-utils.js +306 -0
  37. package/script/install.js +94 -159
  38. package/script/prepack.js +49 -32
  39. package/bin/napi-v3/darwin/arm64/onnxruntime_binding.node +0 -0
  40. package/bin/napi-v3/darwin/x64/onnxruntime_binding.node +0 -0
  41. package/bin/napi-v3/linux/arm64/onnxruntime_binding.node +0 -0
  42. package/bin/napi-v3/linux/x64/libonnxruntime_providers_shared.so +0 -0
  43. package/bin/napi-v3/linux/x64/onnxruntime_binding.node +0 -0
  44. package/bin/napi-v3/win32/arm64/onnxruntime_binding.node +0 -0
  45. package/bin/napi-v3/win32/x64/onnxruntime.dll +0 -0
  46. package/bin/napi-v3/win32/x64/onnxruntime_binding.node +0 -0
package/script/install.js CHANGED
@@ -8,22 +8,20 @@
8
8
  // not always available.
9
9
 
10
10
  // The purpose of this script is to download the required binaries for the platform and architecture.
11
- // Currently, most of the binaries are already bundled in the package, except for the following:
12
- // - Linux/x64/CUDA 11
13
- // - Linux/x64/CUDA 12
11
+ // Currently, most of the binaries are already bundled in the package, except for the files that described in the file
12
+ // install-metadata.js.
14
13
  //
15
- // The CUDA binaries are not bundled because they are too large to be allowed in the npm registry. Instead, they are
16
- // downloaded from the GitHub release page of ONNX Runtime. The script will download the binaries if they are not
17
- // already present in the package.
14
+ // Some files (eg. the CUDA EP binaries) are not bundled because they are too large to be allowed in the npm registry.
15
+ // Instead, they are downloaded from the Nuget feed. The script will download the binaries if they are not already
16
+ // present in the NPM package.
18
17
 
19
18
  // Step.1: Check if we should exit early
20
19
  const os = require('os');
21
- const fs = require('fs');
22
- const https = require('https');
23
20
  const path = require('path');
24
- const tar = require('tar');
25
- const { execFileSync } = require('child_process');
26
21
  const { bootstrap: globalAgentBootstrap } = require('global-agent');
22
+ const { installPackages, parseInstallFlag } = require('./install-utils.js');
23
+
24
+ const INSTALL_METADATA = require('./install-metadata.js');
27
25
 
28
26
  // Bootstrap global-agent to honor the proxy settings in
29
27
  // environment variables, e.g. GLOBAL_AGENT_HTTPS_PROXY.
@@ -31,169 +29,106 @@ const { bootstrap: globalAgentBootstrap } = require('global-agent');
31
29
  globalAgentBootstrap();
32
30
 
33
31
  // commandline flag:
34
- // --onnxruntime-node-install-cuda Force install the CUDA EP binaries. Try to detect the CUDA version.
35
- // --onnxruntime-node-install-cuda=v11 Force install the CUDA EP binaries for CUDA 11.
36
- // --onnxruntime-node-install-cuda=v12 Force install the CUDA EP binaries for CUDA 12.
32
+ //
33
+ // --onnxruntime-node-install Force install the files that are not bundled in the package.
34
+ //
35
+ // --onnxruntime-node-install=skip Skip the installation of the files that are not bundled in the package.
36
+ //
37
+ // --onnxruntime-node-install=cuda12 Force install the CUDA EP binaries for CUDA 12.
38
+ //
39
+ // --onnxruntime-node-install-cuda Force install the CUDA EP binaries.
40
+ // (deprecated, use --onnxruntime-node-install=cuda12)
41
+ //
37
42
  // --onnxruntime-node-install-cuda=skip Skip the installation of the CUDA EP binaries.
43
+ // (deprecated, use --onnxruntime-node-install=skip)
38
44
  //
39
- // Alternatively, use environment variable "ONNXRUNTIME_NODE_INSTALL_CUDA"
40
45
  //
41
- // If the flag is not provided, the script will only install the CUDA EP binaries when:
42
- // - The platform is Linux/x64.
43
- // - The binaries are not already present in the package.
44
- // - The installation is not a local install (when used inside ONNX Runtime repo).
46
+ // Alternatively, use environment variable "ONNXRUNTIME_NODE_INSTALL" or "ONNXRUNTIME_NODE_INSTALL_CUDA" (deprecated).
45
47
  //
46
- const INSTALL_CUDA_FLAG = parseInstallCudaFlag();
47
- const NO_INSTALL = INSTALL_CUDA_FLAG === 'skip';
48
- const FORCE_INSTALL = !NO_INSTALL && INSTALL_CUDA_FLAG;
49
-
50
- const IS_LINUX_X64 = os.platform() === 'linux' && os.arch() === 'x64';
51
- const BIN_FOLDER = path.join(__dirname, '..', 'bin/napi-v3/linux/x64');
52
- const BIN_FOLDER_EXISTS = fs.existsSync(BIN_FOLDER);
53
- const CUDA_DLL_EXISTS = fs.existsSync(path.join(BIN_FOLDER, 'libonnxruntime_providers_cuda.so'));
54
- const ORT_VERSION = require('../package.json').version;
55
-
56
- const npm_config_local_prefix = process.env.npm_config_local_prefix;
57
- const npm_package_json = process.env.npm_package_json;
58
- const SKIP_LOCAL_INSTALL =
59
- npm_config_local_prefix && npm_package_json && path.dirname(npm_package_json) === npm_config_local_prefix;
60
-
61
- const shouldInstall = FORCE_INSTALL || (!SKIP_LOCAL_INSTALL && IS_LINUX_X64 && BIN_FOLDER_EXISTS && !CUDA_DLL_EXISTS);
62
- if (NO_INSTALL || !shouldInstall) {
48
+ // If the flag is not provided, the script will look up the metadata file to determine the manifest.
49
+ //
50
+
51
+ /**
52
+ * Possible values:
53
+ * - undefined: the default behavior. This is the value when no installation flag is specified.
54
+ *
55
+ * - false: skip installation. This is the value when the installation flag is set to "skip":
56
+ * --onnxruntime-node-install=skip
57
+ *
58
+ * - true: force installation. This is the value when the installation flag is set with no value:
59
+ * --onnxruntime-node-install
60
+ *
61
+ * - string: the installation flag is set to a specific value:
62
+ * --onnxruntime-node-install=cuda12
63
+ */
64
+ const INSTALL_FLAG = parseInstallFlag();
65
+
66
+ // if installation is skipped, exit early
67
+ if (INSTALL_FLAG === false) {
63
68
  process.exit(0);
64
69
  }
65
-
66
- // Step.2: Download the required binaries
67
- const artifactUrl = {
68
- get 11() {
69
- // TODO: support ORT Cuda v11 binaries
70
- throw new Error(`CUDA 11 binaries are not supported by this script yet.
71
-
72
- To use ONNX Runtime Node.js binding with CUDA v11 support, please follow the manual steps:
73
-
74
- 1. Use "--onnxruntime-node-install-cuda=skip" to skip the auto installation.
75
- 2. Navigate to https://aiinfra.visualstudio.com/PublicPackages/_artifacts/feed/onnxruntime-cuda-11
76
- 3. Download the binaries for your platform and architecture
77
- 4. Extract the following binaries to "node_modules/onnxruntime-node/bin/napi-v3/linux/x64:
78
- - libonnxruntime_providers_tensorrt.so
79
- - libonnxruntime_providers_shared.so
80
- - libonnxruntime.so.${ORT_VERSION}
81
- - libonnxruntime_providers_cuda.so
82
- `);
83
- },
84
- 12: `https://github.com/microsoft/onnxruntime/releases/download/v${ORT_VERSION}/onnxruntime-linux-x64-gpu-${
85
- ORT_VERSION
86
- }.tgz`,
87
- }[INSTALL_CUDA_FLAG || tryGetCudaVersion()];
88
- console.log(`Downloading "${artifactUrl}"...`);
89
-
90
- const FILES = new Set([
91
- 'libonnxruntime_providers_tensorrt.so',
92
- 'libonnxruntime_providers_shared.so',
93
- `libonnxruntime.so.${ORT_VERSION}`,
94
- 'libonnxruntime_providers_cuda.so',
95
- ]);
96
-
97
- downloadAndExtract(artifactUrl, BIN_FOLDER, FILES);
98
-
99
- async function downloadAndExtract(url, dest, files) {
100
- return new Promise((resolve, reject) => {
101
- https.get(url, (res) => {
102
- const { statusCode } = res;
103
- const contentType = res.headers['content-type'];
104
-
105
- if (statusCode === 301 || statusCode === 302) {
106
- downloadAndExtract(res.headers.location, dest, files).then(
107
- (value) => resolve(value),
108
- (reason) => reject(reason),
109
- );
110
- return;
111
- } else if (statusCode !== 200) {
112
- throw new Error(`Failed to download the binaries: ${res.statusCode} ${res.statusMessage}.
113
-
114
- 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.`);
115
- }
116
-
117
- if (!contentType || !/^application\/octet-stream/.test(contentType)) {
118
- throw new Error(`unexpected content type: ${contentType}`);
119
- }
120
-
121
- res
122
- .pipe(
123
- tar.t({
124
- strict: true,
125
- onentry: (entry) => {
126
- const filename = path.basename(entry.path);
127
- if (entry.type === 'File' && files.has(filename)) {
128
- console.log(`Extracting "${filename}" to "${dest}"...`);
129
- entry.pipe(fs.createWriteStream(path.join(dest, filename)));
130
- entry.on('finish', () => {
131
- console.log(`Finished extracting "${filename}".`);
132
- });
133
- }
134
- },
135
- }),
136
- )
137
- .on('error', (err) => {
138
- throw new Error(`Failed to extract the binaries: ${err.message}.
139
-
140
- 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.`);
141
- });
142
- });
143
- });
70
+ // if installation is not specified, exit early when the installation is local (e.g. `npm ci` in <ORT_ROOT>/js/node/)
71
+ if (INSTALL_FLAG === undefined) {
72
+ const npm_config_local_prefix = process.env.npm_config_local_prefix;
73
+ const npm_package_json = process.env.npm_package_json;
74
+ const IS_LOCAL_INSTALL =
75
+ npm_config_local_prefix && npm_package_json && path.dirname(npm_package_json) === npm_config_local_prefix;
76
+ if (IS_LOCAL_INSTALL) {
77
+ process.exit(0);
78
+ }
144
79
  }
145
80
 
146
- function tryGetCudaVersion() {
147
- // Should only return 11 or 12.
148
-
149
- // try to get the CUDA version from the system ( `nvcc --version` )
150
- let ver = 12;
151
- try {
152
- const nvccVersion = execFileSync('nvcc', ['--version'], { encoding: 'utf8' });
153
- const match = nvccVersion.match(/release (\d+)/);
154
- if (match) {
155
- ver = parseInt(match[1]);
156
- if (ver !== 11 && ver !== 12) {
157
- throw new Error(`Unsupported CUDA version: ${ver}`);
158
- }
159
- }
160
- } catch (e) {
161
- if (e?.code === 'ENOENT') {
162
- console.warn('`nvcc` not found. Assuming CUDA 12.');
163
- } else {
164
- console.warn('Failed to detect CUDA version from `nvcc --version`:', e.message);
81
+ const PLATFORM = `${os.platform()}/${os.arch()}`;
82
+ let INSTALL_MANIFEST_NAMES = INSTALL_METADATA.requirements[PLATFORM] ?? [];
83
+
84
+ // if installation is specified explicitly, validate the manifest
85
+ if (typeof INSTALL_FLAG === 'string') {
86
+ const installations = INSTALL_FLAG.split(',').map((x) => x.trim());
87
+ for (const installation of installations) {
88
+ if (INSTALL_MANIFEST_NAMES.indexOf(installation) === -1) {
89
+ throw new Error(`Invalid installation: ${installation} for platform: ${PLATFORM}`);
165
90
  }
166
91
  }
92
+ INSTALL_MANIFEST_NAMES = installations;
93
+ }
94
+
95
+ const BIN_FOLDER = path.join(__dirname, '..', 'bin/napi-v6', PLATFORM);
96
+ const INSTALL_MANIFESTS = [];
97
+
98
+ const PACKAGES = new Set();
99
+ for (const name of INSTALL_MANIFEST_NAMES) {
100
+ const manifest = INSTALL_METADATA.manifests[`${PLATFORM}:${name}`];
101
+ if (!manifest) {
102
+ throw new Error(`Manifest not found: ${name} for platform: ${PLATFORM}`);
103
+ }
167
104
 
168
- // assume CUDA 12 if failed to detect
169
- return ver;
105
+ for (const [filename, { package: pkg, path: pathInPackage }] of Object.entries(manifest)) {
106
+ const packageCandidates = INSTALL_METADATA.packages[pkg];
107
+ if (!packageCandidates) {
108
+ throw new Error(`Package information not found: ${pkg}`);
109
+ }
110
+ PACKAGES.add(packageCandidates);
111
+
112
+ INSTALL_MANIFESTS.push({
113
+ filepath: path.normalize(path.join(BIN_FOLDER, filename)),
114
+ packagesInfo: packageCandidates,
115
+ pathInPackage,
116
+ });
117
+ }
170
118
  }
171
119
 
172
- function parseInstallCudaFlag() {
173
- let flag = process.env.ONNXRUNTIME_NODE_INSTALL_CUDA || process.env.npm_config_onnxruntime_node_install_cuda;
174
- if (!flag) {
175
- for (let i = 0; i < process.argv.length; i++) {
176
- if (process.argv[i].startsWith('--onnxruntime-node-install-cuda=')) {
177
- flag = process.argv[i].split('=')[1];
178
- break;
179
- } else if (process.argv[i] === '--onnxruntime-node-install-cuda') {
180
- flag = 'true';
181
- }
120
+ // If the installation flag is not specified, we do a check to see if the files are already installed.
121
+ if (INSTALL_FLAG === undefined) {
122
+ let hasMissingFiles = false;
123
+ for (const { filepath } of INSTALL_MANIFESTS) {
124
+ if (!require('fs').existsSync(filepath)) {
125
+ hasMissingFiles = true;
126
+ break;
182
127
  }
183
128
  }
184
- switch (flag) {
185
- case 'true':
186
- case '1':
187
- case 'ON':
188
- return tryGetCudaVersion();
189
- case 'v11':
190
- return 11;
191
- case 'v12':
192
- return 12;
193
- case 'skip':
194
- case undefined:
195
- return flag;
196
- default:
197
- throw new Error(`Invalid value for --onnxruntime-node-install-cuda: ${flag}`);
129
+ if (!hasMissingFiles) {
130
+ process.exit(0);
198
131
  }
199
132
  }
133
+
134
+ void installPackages(PACKAGES, INSTALL_MANIFESTS, INSTALL_METADATA.feeds);
package/script/prepack.js CHANGED
@@ -1,42 +1,59 @@
1
- "use strict";
1
+ 'use strict';
2
2
  // Copyright (c) Microsoft Corporation. All rights reserved.
3
3
  // Licensed under the MIT License.
4
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
- if (k2 === undefined) k2 = k;
6
- var desc = Object.getOwnPropertyDescriptor(m, k);
7
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
- desc = { enumerable: true, get: function() { return m[k]; } };
9
- }
10
- Object.defineProperty(o, k2, desc);
11
- }) : (function(o, m, k, k2) {
12
- if (k2 === undefined) k2 = k;
13
- o[k2] = m[k];
14
- }));
15
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
- Object.defineProperty(o, "default", { enumerable: true, value: v });
17
- }) : function(o, v) {
18
- o["default"] = v;
19
- });
20
- var __importStar = (this && this.__importStar) || function (mod) {
4
+ var __createBinding =
5
+ (this && this.__createBinding) ||
6
+ (Object.create
7
+ ? function (o, m, k, k2) {
8
+ if (k2 === undefined) k2 = k;
9
+ var desc = Object.getOwnPropertyDescriptor(m, k);
10
+ if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
+ desc = {
12
+ enumerable: true,
13
+ get: function () {
14
+ return m[k];
15
+ },
16
+ };
17
+ }
18
+ Object.defineProperty(o, k2, desc);
19
+ }
20
+ : function (o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ });
24
+ var __setModuleDefault =
25
+ (this && this.__setModuleDefault) ||
26
+ (Object.create
27
+ ? function (o, v) {
28
+ Object.defineProperty(o, 'default', { enumerable: true, value: v });
29
+ }
30
+ : function (o, v) {
31
+ o['default'] = v;
32
+ });
33
+ var __importStar =
34
+ (this && this.__importStar) ||
35
+ function (mod) {
21
36
  if (mod && mod.__esModule) return mod;
22
37
  var result = {};
23
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
+ if (mod != null)
39
+ for (var k in mod)
40
+ if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
41
  __setModuleDefault(result, mod);
25
42
  return result;
26
- };
27
- Object.defineProperty(exports, "__esModule", { value: true });
28
- const fs = __importStar(require("fs-extra"));
29
- const path = __importStar(require("path"));
43
+ };
44
+ Object.defineProperty(exports, '__esModule', { value: true });
45
+ const fs = __importStar(require('fs-extra'));
46
+ const path = __importStar(require('path'));
30
47
  function updatePackageJson() {
31
- const commonPackageJsonPath = path.join(__dirname, '..', '..', 'common', 'package.json');
32
- const selfPackageJsonPath = path.join(__dirname, '..', 'package.json');
33
- console.log(`=== start to update package.json: ${selfPackageJsonPath}`);
34
- const packageCommon = fs.readJSONSync(commonPackageJsonPath);
35
- const packageSelf = fs.readJSONSync(selfPackageJsonPath);
36
- const version = packageCommon.version;
37
- packageSelf.dependencies['onnxruntime-common'] = `${version}`;
38
- fs.writeJSONSync(selfPackageJsonPath, packageSelf, { spaces: 2 });
39
- console.log('=== finished updating package.json.');
48
+ const commonPackageJsonPath = path.join(__dirname, '..', '..', 'common', 'package.json');
49
+ const selfPackageJsonPath = path.join(__dirname, '..', 'package.json');
50
+ console.log(`=== start to update package.json: ${selfPackageJsonPath}`);
51
+ const packageCommon = fs.readJSONSync(commonPackageJsonPath);
52
+ const packageSelf = fs.readJSONSync(selfPackageJsonPath);
53
+ const version = packageCommon.version;
54
+ packageSelf.dependencies['onnxruntime-common'] = `${version}`;
55
+ fs.writeJSONSync(selfPackageJsonPath, packageSelf, { spaces: 2 });
56
+ console.log('=== finished updating package.json.');
40
57
  }
41
58
  // update version of dependency "onnxruntime-common" before packing
42
59
  updatePackageJson();