node-opcua-pki 6.16.0 → 6.18.0

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.
package/dist/bin/pki.mjs CHANGED
@@ -2007,31 +2007,10 @@ import child_process from "child_process";
2007
2007
  import fs5 from "fs";
2008
2008
  import os2 from "os";
2009
2009
  import path3 from "path";
2010
- import url from "url";
2010
+ import { pipeline } from "stream/promises";
2011
2011
  import byline from "byline";
2012
2012
  import chalk4 from "chalk";
2013
- import ProgressBar from "progress";
2014
- import wget from "wget-improved-2";
2015
2013
  import yauzl from "yauzl";
2016
- function makeOptions() {
2017
- const proxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || void 0;
2018
- if (proxy) {
2019
- const a = new url.URL(proxy);
2020
- const auth = a.username ? `${a.username}:${a.password}` : void 0;
2021
- const options = {
2022
- proxy: {
2023
- port: a.port ? parseInt(a.port, 10) : 80,
2024
- protocol: a.protocol.replace(":", ""),
2025
- host: a.hostname ?? "",
2026
- proxyAuth: auth
2027
- }
2028
- };
2029
- warningLog(chalk4.green("- using proxy "), proxy);
2030
- warningLog(options);
2031
- return options;
2032
- }
2033
- return {};
2034
- }
2035
2014
  async function execute(cmd, cwd) {
2036
2015
  let output = "";
2037
2016
  const options = {
@@ -2065,7 +2044,7 @@ function quote2(str) {
2065
2044
  return `"${str.replace(/\\/g, "/")}"`;
2066
2045
  }
2067
2046
  function is_expected_openssl_version(strVersion) {
2068
- return !!strVersion.match(/OpenSSL 1|3/);
2047
+ return !!strVersion.match(/OpenSSL \d/);
2069
2048
  }
2070
2049
  async function getopensslExecPath() {
2071
2050
  let result1;
@@ -2144,6 +2123,31 @@ async function install_and_check_win32_openssl_version() {
2144
2123
  };
2145
2124
  }
2146
2125
  }
2126
+ async function find_system_openssl_win32() {
2127
+ try {
2128
+ const result = await execute("where openssl");
2129
+ if (result.exitCode !== 0) {
2130
+ return void 0;
2131
+ }
2132
+ const opensslPath2 = result.output.split(/\r?\n/)[0].trim();
2133
+ if (!opensslPath2 || !fs5.existsSync(opensslPath2)) {
2134
+ return void 0;
2135
+ }
2136
+ const q5 = quote2(opensslPath2);
2137
+ const versionResult = await execute(`${q5} version`);
2138
+ const version = versionResult.output.trim();
2139
+ if (versionResult.exitCode === 0 && is_expected_openssl_version(version)) {
2140
+ warningLog(
2141
+ chalk4.green("Using system OpenSSL: ") + chalk4.cyan(version) + chalk4.green(" at ") + chalk4.cyan(opensslPath2)
2142
+ );
2143
+ return opensslPath2;
2144
+ }
2145
+ warningLog(chalk4.yellow("System OpenSSL found but version not accepted: ") + version);
2146
+ return void 0;
2147
+ } catch (_err) {
2148
+ return void 0;
2149
+ }
2150
+ }
2147
2151
  function win32or64() {
2148
2152
  if (process.env.PROCESSOR_ARCHITECTURE === "x86" && process.env.PROCESSOR_ARCHITEW6432) {
2149
2153
  return 64;
@@ -2157,37 +2161,39 @@ async function install_and_check_win32_openssl_version() {
2157
2161
  return 32;
2158
2162
  }
2159
2163
  async function download_openssl() {
2160
- const url2 = win32or64() === 64 ? "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-x64_86-win64.zip" : "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-i386-win32.zip";
2161
- const outputFilename = path3.join(downloadFolder, path3.basename(url2));
2162
- warningLog(`downloading ${chalk4.yellow(url2)} to ${outputFilename}`);
2164
+ const url = win32or64() === 64 ? "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-x64_86-win64.zip" : "https://github.com/node-opcua/node-opcua-pki/releases/download/2.14.2/openssl-1.0.2u-i386-win32.zip";
2165
+ const outputFilename = path3.join(downloadFolder, path3.basename(url));
2166
+ warningLog(`downloading ${chalk4.yellow(url)} to ${outputFilename}`);
2163
2167
  if (fs5.existsSync(outputFilename)) {
2164
2168
  return { downloadedFile: outputFilename };
2165
2169
  }
2166
- const options = makeOptions();
2167
- const bar = new ProgressBar(chalk4.cyan("[:bar]") + chalk4.cyan(" :percent ") + chalk4.white(":etas"), {
2168
- complete: "=",
2169
- incomplete: " ",
2170
- total: 100,
2171
- width: 100
2172
- });
2173
- return await new Promise((resolve, reject) => {
2174
- const download = wget.download(url2, outputFilename, options);
2175
- download.on("error", (err) => {
2176
- warningLog(err);
2177
- setImmediate(() => {
2178
- reject(err);
2179
- });
2180
- });
2181
- download.on("end", (output) => {
2182
- if (doDebug2) {
2183
- warningLog(output);
2170
+ const response = await fetch(url, { redirect: "follow" });
2171
+ if (!response.ok || !response.body) {
2172
+ throw new Error(`Failed to download OpenSSL from ${url}: ${response.status} ${response.statusText}`);
2173
+ }
2174
+ const contentLength = parseInt(response.headers.get("content-length") || "0", 10);
2175
+ let downloaded = 0;
2176
+ let lastPercent = -1;
2177
+ const fileStream = fs5.createWriteStream(outputFilename);
2178
+ const body = response.body;
2179
+ body.on("data", (chunk) => {
2180
+ downloaded += chunk.length;
2181
+ if (contentLength > 0) {
2182
+ const percent = Math.floor(downloaded / contentLength * 100);
2183
+ if (percent !== lastPercent && percent % 10 === 0) {
2184
+ lastPercent = percent;
2185
+ warningLog(` download progress: ${percent}%`);
2184
2186
  }
2185
- resolve({ downloadedFile: outputFilename });
2186
- });
2187
- download.on("progress", (progress) => {
2188
- bar.update(progress);
2189
- });
2187
+ }
2190
2188
  });
2189
+ await pipeline(body, fileStream);
2190
+ const stat = fs5.statSync(outputFilename);
2191
+ if (stat.size === 0) {
2192
+ fs5.unlinkSync(outputFilename);
2193
+ throw new Error(`Downloaded file is empty: ${outputFilename}`);
2194
+ }
2195
+ warningLog(chalk4.green("Download complete: ") + `${stat.size} bytes`);
2196
+ return { downloadedFile: outputFilename };
2191
2197
  }
2192
2198
  async function unzip_openssl(zipFilename) {
2193
2199
  const opensslFolder2 = get_openssl_folder_win32();
@@ -2236,6 +2242,10 @@ async function install_and_check_win32_openssl_version() {
2236
2242
  });
2237
2243
  });
2238
2244
  }
2245
+ const systemOpenssl = await find_system_openssl_win32();
2246
+ if (systemOpenssl) {
2247
+ return systemOpenssl;
2248
+ }
2239
2249
  const opensslFolder = get_openssl_folder_win32();
2240
2250
  const opensslExecPath = get_openssl_exec_path_win32();
2241
2251
  if (!fs5.existsSync(opensslFolder)) {
@@ -2866,32 +2876,32 @@ function parseOpenSSLDate(dateStr) {
2866
2876
  const sec = raw.substring(10, 12);
2867
2877
  return `${year}-${month}-${day}T${hour}:${min}:${sec}Z`;
2868
2878
  }
2869
- function validateRevocationUrl(url2, fieldName) {
2870
- if (url2 === void 0) {
2879
+ function validateRevocationUrl(url, fieldName) {
2880
+ if (url === void 0) {
2871
2881
  return void 0;
2872
2882
  }
2873
- if (url2 === "") {
2883
+ if (url === "") {
2874
2884
  throw new Error(`${fieldName} must not be empty \u2014 pass undefined to disable the extension`);
2875
2885
  }
2876
2886
  let parsed;
2877
2887
  try {
2878
- parsed = new URL(url2);
2888
+ parsed = new URL(url);
2879
2889
  } catch {
2880
- throw new Error(`${fieldName} is not a valid URL: ${url2}`);
2890
+ throw new Error(`${fieldName} is not a valid URL: ${url}`);
2881
2891
  }
2882
2892
  if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
2883
- throw new Error(`${fieldName} must use http: or https: (got ${parsed.protocol} in ${url2})`);
2893
+ throw new Error(`${fieldName} must use http: or https: (got ${parsed.protocol} in ${url})`);
2884
2894
  }
2885
2895
  if (!parsed.pathname || parsed.pathname === "/") {
2886
- throw new Error(`${fieldName} must include a path component (got ${url2})`);
2896
+ throw new Error(`${fieldName} must include a path component (got ${url})`);
2887
2897
  }
2888
2898
  const isLoopback = parsed.hostname === "localhost" || parsed.hostname === "::1" || parsed.hostname.startsWith("127.");
2889
2899
  if (isLoopback) {
2890
2900
  console.warn(
2891
- `[node-opcua-pki] ${fieldName} points at loopback (${url2}) \u2014 certificates issued with this URL will be unreachable from any other host.`
2901
+ `[node-opcua-pki] ${fieldName} points at loopback (${url}) \u2014 certificates issued with this URL will be unreachable from any other host.`
2892
2902
  );
2893
2903
  }
2894
- return url2;
2904
+ return url;
2895
2905
  }
2896
2906
  var defaultSubject, configurationFileTemplate, configurationFileSimpleTemplate2, config3, n5, q4, CertificateAuthority;
2897
2907
  var init_certificate_authority = __esm({
@@ -2974,8 +2984,8 @@ var init_certificate_authority = __esm({
2974
2984
  *
2975
2985
  * @see US-202
2976
2986
  */
2977
- setCrlDistributionUrl(url2) {
2978
- this._crlDistributionUrl = validateRevocationUrl(url2, "crlDistributionUrl");
2987
+ setCrlDistributionUrl(url) {
2988
+ this._crlDistributionUrl = validateRevocationUrl(url, "crlDistributionUrl");
2979
2989
  }
2980
2990
  /**
2981
2991
  * Configure the OCSP responder URL embedded as the `OCSP` leg of
@@ -2984,8 +2994,8 @@ var init_certificate_authority = __esm({
2984
2994
  *
2985
2995
  * @see US-202
2986
2996
  */
2987
- setOcspResponderUrl(url2) {
2988
- this._ocspResponderUrl = validateRevocationUrl(url2, "ocspResponderUrl");
2997
+ setOcspResponderUrl(url) {
2998
+ this._ocspResponderUrl = validateRevocationUrl(url, "ocspResponderUrl");
2989
2999
  }
2990
3000
  /**
2991
3001
  * Configure the caIssuers URL embedded as the `caIssuers` leg of
@@ -2994,8 +3004,8 @@ var init_certificate_authority = __esm({
2994
3004
  *
2995
3005
  * @see US-202
2996
3006
  */
2997
- setCaIssuersUrl(url2) {
2998
- this._caIssuersUrl = validateRevocationUrl(url2, "caIssuersUrl");
3007
+ setCaIssuersUrl(url) {
3008
+ this._caIssuersUrl = validateRevocationUrl(url, "caIssuersUrl");
2999
3009
  }
3000
3010
  /**
3001
3011
  * @internal