Skip to content

Commit 55c9e0c

Browse files
CLIS-77 Fix SDK on Vs Code environment
CLIS-77 Fix SDK on Vs Code environment
1 parent 3d71115 commit 55c9e0c

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scanoss",
3-
"version": "0.8.7",
3+
"version": "0.8.8",
44
"description": "The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",

Diff for: src/sdk/Utils/Utils.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,43 @@ import * as Http from 'http';
88
import * as Https from 'https';
99
import { Logger, logger } from '../Logger';
1010
import * as isInNet2 from 'pac-resolver/dist/isInNet';
11+
import fs from 'fs';
12+
import tls from 'tls';
1113

1214

1315
export class Utils {
1416
private static PackageJSON: any = null;
1517
private static PAC_FindProxyForURL: FindProxyForURL;
1618

19+
// This function takes inspiration from https://www.npmjs.com/package/syswide-cas
20+
// Copyright 2016 Capriza. Code released under the MIT license
21+
public static loadCaCertFromFile(file: string) {
22+
const rootCAs = [];
23+
24+
let content = fs.readFileSync(file, { encoding: "utf-8" }).trim();
25+
content = content.replace(/\r\n/g, "\n"); // Handles certificates that have been created in Windows
26+
const regex = /-----BEGIN CERTIFICATE-----\n[\s\S]+?\n-----END CERTIFICATE-----/g;
27+
const results = content.match(regex);
28+
if (!results) throw new Error("Could not parse certificate");
29+
30+
results.forEach((match)=> {
31+
const cert = match.trim();
32+
rootCAs.push(cert);
33+
});
34+
35+
const origCreateSecureContext = tls.createSecureContext;
36+
tls.createSecureContext = function(options) {
37+
var c = origCreateSecureContext.apply(null, arguments);
38+
if (!options.ca && rootCAs.length > 0) {
39+
rootCAs.forEach(function(ca) {
40+
// add to the created context our own root CAs
41+
c.context.addCACert(ca);
42+
});
43+
}
44+
return c;
45+
};
46+
}
47+
1748
public static getPackageVersion(): string {
1849
if (!this.PackageJSON) {
1950
const path = require('path');
@@ -29,7 +60,6 @@ export class Utils {
2960
}
3061
}
3162
return this.PackageJSON?.version ? this.PackageJSON.version : ''
32-
3363
}
3464

3565

Diff for: src/sdk/scanner/Dispatcher/Dispatcher.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { GlobalControllerAborter } from "./GlobalControllerAborter";
1111
import { DispatchableItem } from './DispatchableItem';
1212
import { HttpsProxyAgent } from 'https-proxy-agent';
1313
import { HttpProxyAgent } from 'http-proxy-agent';
14-
import * as syswideCa from "syswide-cas";
1514
import { Utils } from '../../Utils/Utils';
1615

1716
const MAX_CONCURRENT_REQUEST = 30;
@@ -31,6 +30,8 @@ export class Dispatcher extends EventEmitter {
3130

3231
private proxyAgent: HttpsProxyAgent | HttpProxyAgent;
3332

33+
private caCert: string;
34+
3435
constructor(scannerCfg = new ScannerCfg()) {
3536
super();
3637
this.scannerCfg = scannerCfg;
@@ -44,23 +45,19 @@ export class Dispatcher extends EventEmitter {
4445

4546
//Loads proxy from SDK config, if not, loads from env variables, if not, leave empty
4647
this.proxyAgent = null;
48+
this.caCert = null;
49+
4750
const proxyAddr = this.scannerCfg.PROXY || process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY || null;
51+
const caCertPath = this.scannerCfg.CA_CERT || process.env.NODE_EXTRA_CA_CERTS
52+
53+
if (caCertPath) Utils.loadCaCertFromFile(caCertPath);
54+
else if (this.scannerCfg.IGNORE_CERT_ERRORS || proxyAddr) process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
4855

4956
if (proxyAddr) {
5057
if (this.scannerCfg.API_URL.trim().startsWith('https')) this.proxyAgent = new HttpsProxyAgent(proxyAddr);
5158
else this.proxyAgent = new HttpProxyAgent(proxyAddr);
5259
}
5360

54-
//Loads certs stuff from SDK config
55-
const ca_cert = this.scannerCfg.CA_CERT || process.env.NODE_EXTRA_CA_CERTS
56-
if (ca_cert) {
57-
const syswidecas = require('syswide-cas');
58-
syswideCa.addCAs(ca_cert)
59-
} else {
60-
if (this.scannerCfg.IGNORE_CERT_ERRORS || proxyAddr)
61-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
62-
}
63-
6461
this.pQueue = new PQueue({
6562
concurrency: this.scannerCfg.CONCURRENCY_LIMIT,
6663
});

0 commit comments

Comments
 (0)