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/index.mjs CHANGED
@@ -164,32 +164,11 @@ import child_process from "child_process";
164
164
  import fs2 from "fs";
165
165
  import os from "os";
166
166
  import path2 from "path";
167
- import url from "url";
167
+ import { pipeline } from "stream/promises";
168
168
  import byline from "byline";
169
169
  import chalk2 from "chalk";
170
- import ProgressBar from "progress";
171
- import wget from "wget-improved-2";
172
170
  import yauzl from "yauzl";
173
171
  var doDebug2 = process.env.NODEOPCUAPKIDEBUG || false;
174
- function makeOptions() {
175
- const proxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || void 0;
176
- if (proxy) {
177
- const a = new url.URL(proxy);
178
- const auth = a.username ? `${a.username}:${a.password}` : void 0;
179
- const options = {
180
- proxy: {
181
- port: a.port ? parseInt(a.port, 10) : 80,
182
- protocol: a.protocol.replace(":", ""),
183
- host: a.hostname ?? "",
184
- proxyAuth: auth
185
- }
186
- };
187
- warningLog(chalk2.green("- using proxy "), proxy);
188
- warningLog(options);
189
- return options;
190
- }
191
- return {};
192
- }
193
172
  async function execute(cmd, cwd) {
194
173
  let output = "";
195
174
  const options = {
@@ -223,7 +202,7 @@ function quote2(str) {
223
202
  return `"${str.replace(/\\/g, "/")}"`;
224
203
  }
225
204
  function is_expected_openssl_version(strVersion) {
226
- return !!strVersion.match(/OpenSSL 1|3/);
205
+ return !!strVersion.match(/OpenSSL \d/);
227
206
  }
228
207
  async function getopensslExecPath() {
229
208
  let result1;
@@ -302,6 +281,31 @@ async function install_and_check_win32_openssl_version() {
302
281
  };
303
282
  }
304
283
  }
284
+ async function find_system_openssl_win32() {
285
+ try {
286
+ const result = await execute("where openssl");
287
+ if (result.exitCode !== 0) {
288
+ return void 0;
289
+ }
290
+ const opensslPath2 = result.output.split(/\r?\n/)[0].trim();
291
+ if (!opensslPath2 || !fs2.existsSync(opensslPath2)) {
292
+ return void 0;
293
+ }
294
+ const q4 = quote2(opensslPath2);
295
+ const versionResult = await execute(`${q4} version`);
296
+ const version = versionResult.output.trim();
297
+ if (versionResult.exitCode === 0 && is_expected_openssl_version(version)) {
298
+ warningLog(
299
+ chalk2.green("Using system OpenSSL: ") + chalk2.cyan(version) + chalk2.green(" at ") + chalk2.cyan(opensslPath2)
300
+ );
301
+ return opensslPath2;
302
+ }
303
+ warningLog(chalk2.yellow("System OpenSSL found but version not accepted: ") + version);
304
+ return void 0;
305
+ } catch (_err) {
306
+ return void 0;
307
+ }
308
+ }
305
309
  function win32or64() {
306
310
  if (process.env.PROCESSOR_ARCHITECTURE === "x86" && process.env.PROCESSOR_ARCHITEW6432) {
307
311
  return 64;
@@ -315,37 +319,39 @@ async function install_and_check_win32_openssl_version() {
315
319
  return 32;
316
320
  }
317
321
  async function download_openssl() {
318
- 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";
319
- const outputFilename = path2.join(downloadFolder, path2.basename(url2));
320
- warningLog(`downloading ${chalk2.yellow(url2)} to ${outputFilename}`);
322
+ 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";
323
+ const outputFilename = path2.join(downloadFolder, path2.basename(url));
324
+ warningLog(`downloading ${chalk2.yellow(url)} to ${outputFilename}`);
321
325
  if (fs2.existsSync(outputFilename)) {
322
326
  return { downloadedFile: outputFilename };
323
327
  }
324
- const options = makeOptions();
325
- const bar = new ProgressBar(chalk2.cyan("[:bar]") + chalk2.cyan(" :percent ") + chalk2.white(":etas"), {
326
- complete: "=",
327
- incomplete: " ",
328
- total: 100,
329
- width: 100
330
- });
331
- return await new Promise((resolve, reject) => {
332
- const download = wget.download(url2, outputFilename, options);
333
- download.on("error", (err) => {
334
- warningLog(err);
335
- setImmediate(() => {
336
- reject(err);
337
- });
338
- });
339
- download.on("end", (output) => {
340
- if (doDebug2) {
341
- warningLog(output);
328
+ const response = await fetch(url, { redirect: "follow" });
329
+ if (!response.ok || !response.body) {
330
+ throw new Error(`Failed to download OpenSSL from ${url}: ${response.status} ${response.statusText}`);
331
+ }
332
+ const contentLength = parseInt(response.headers.get("content-length") || "0", 10);
333
+ let downloaded = 0;
334
+ let lastPercent = -1;
335
+ const fileStream = fs2.createWriteStream(outputFilename);
336
+ const body = response.body;
337
+ body.on("data", (chunk) => {
338
+ downloaded += chunk.length;
339
+ if (contentLength > 0) {
340
+ const percent = Math.floor(downloaded / contentLength * 100);
341
+ if (percent !== lastPercent && percent % 10 === 0) {
342
+ lastPercent = percent;
343
+ warningLog(` download progress: ${percent}%`);
342
344
  }
343
- resolve({ downloadedFile: outputFilename });
344
- });
345
- download.on("progress", (progress) => {
346
- bar.update(progress);
347
- });
345
+ }
348
346
  });
347
+ await pipeline(body, fileStream);
348
+ const stat = fs2.statSync(outputFilename);
349
+ if (stat.size === 0) {
350
+ fs2.unlinkSync(outputFilename);
351
+ throw new Error(`Downloaded file is empty: ${outputFilename}`);
352
+ }
353
+ warningLog(chalk2.green("Download complete: ") + `${stat.size} bytes`);
354
+ return { downloadedFile: outputFilename };
349
355
  }
350
356
  async function unzip_openssl(zipFilename) {
351
357
  const opensslFolder2 = get_openssl_folder_win32();
@@ -394,6 +400,10 @@ async function install_and_check_win32_openssl_version() {
394
400
  });
395
401
  });
396
402
  }
403
+ const systemOpenssl = await find_system_openssl_win32();
404
+ if (systemOpenssl) {
405
+ return systemOpenssl;
406
+ }
397
407
  const opensslFolder = get_openssl_folder_win32();
398
408
  const opensslExecPath = get_openssl_exec_path_win32();
399
409
  if (!fs2.existsSync(opensslFolder)) {
@@ -982,32 +992,32 @@ function parseOpenSSLDate(dateStr) {
982
992
  const sec = raw.substring(10, 12);
983
993
  return `${year}-${month}-${day}T${hour}:${min}:${sec}Z`;
984
994
  }
985
- function validateRevocationUrl(url2, fieldName) {
986
- if (url2 === void 0) {
995
+ function validateRevocationUrl(url, fieldName) {
996
+ if (url === void 0) {
987
997
  return void 0;
988
998
  }
989
- if (url2 === "") {
999
+ if (url === "") {
990
1000
  throw new Error(`${fieldName} must not be empty \u2014 pass undefined to disable the extension`);
991
1001
  }
992
1002
  let parsed;
993
1003
  try {
994
- parsed = new URL(url2);
1004
+ parsed = new URL(url);
995
1005
  } catch {
996
- throw new Error(`${fieldName} is not a valid URL: ${url2}`);
1006
+ throw new Error(`${fieldName} is not a valid URL: ${url}`);
997
1007
  }
998
1008
  if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
999
- throw new Error(`${fieldName} must use http: or https: (got ${parsed.protocol} in ${url2})`);
1009
+ throw new Error(`${fieldName} must use http: or https: (got ${parsed.protocol} in ${url})`);
1000
1010
  }
1001
1011
  if (!parsed.pathname || parsed.pathname === "/") {
1002
- throw new Error(`${fieldName} must include a path component (got ${url2})`);
1012
+ throw new Error(`${fieldName} must include a path component (got ${url})`);
1003
1013
  }
1004
1014
  const isLoopback = parsed.hostname === "localhost" || parsed.hostname === "::1" || parsed.hostname.startsWith("127.");
1005
1015
  if (isLoopback) {
1006
1016
  console.warn(
1007
- `[node-opcua-pki] ${fieldName} points at loopback (${url2}) \u2014 certificates issued with this URL will be unreachable from any other host.`
1017
+ `[node-opcua-pki] ${fieldName} points at loopback (${url}) \u2014 certificates issued with this URL will be unreachable from any other host.`
1008
1018
  );
1009
1019
  }
1010
- return url2;
1020
+ return url;
1011
1021
  }
1012
1022
  var CertificateAuthority = class {
1013
1023
  /** RSA key size used when generating the CA private key. */
@@ -1069,8 +1079,8 @@ var CertificateAuthority = class {
1069
1079
  *
1070
1080
  * @see US-202
1071
1081
  */
1072
- setCrlDistributionUrl(url2) {
1073
- this._crlDistributionUrl = validateRevocationUrl(url2, "crlDistributionUrl");
1082
+ setCrlDistributionUrl(url) {
1083
+ this._crlDistributionUrl = validateRevocationUrl(url, "crlDistributionUrl");
1074
1084
  }
1075
1085
  /**
1076
1086
  * Configure the OCSP responder URL embedded as the `OCSP` leg of
@@ -1079,8 +1089,8 @@ var CertificateAuthority = class {
1079
1089
  *
1080
1090
  * @see US-202
1081
1091
  */
1082
- setOcspResponderUrl(url2) {
1083
- this._ocspResponderUrl = validateRevocationUrl(url2, "ocspResponderUrl");
1092
+ setOcspResponderUrl(url) {
1093
+ this._ocspResponderUrl = validateRevocationUrl(url, "ocspResponderUrl");
1084
1094
  }
1085
1095
  /**
1086
1096
  * Configure the caIssuers URL embedded as the `caIssuers` leg of
@@ -1089,8 +1099,8 @@ var CertificateAuthority = class {
1089
1099
  *
1090
1100
  * @see US-202
1091
1101
  */
1092
- setCaIssuersUrl(url2) {
1093
- this._caIssuersUrl = validateRevocationUrl(url2, "caIssuersUrl");
1102
+ setCaIssuersUrl(url) {
1103
+ this._caIssuersUrl = validateRevocationUrl(url, "caIssuersUrl");
1094
1104
  }
1095
1105
  /**
1096
1106
  * @internal