snyk-nuget-plugin
Version:
Snyk CLI NuGet plugin
101 lines • 4.37 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = generate;
exports.tearDown = tearDown;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const debug_1 = __importDefault(require("debug"));
const node_cache_1 = __importDefault(require("node-cache"));
const crypto = __importStar(require("crypto"));
const debug = (0, debug_1.default)('snyk');
const cache = new node_cache_1.default();
// Use a simple, fast and not secure hashing algorithm just to ensure we don't return the same cached location for
// different types of files. This is mostly to speed up performance of tests, but in theory will also affect customers
// when scanning for multiple TargetFrameworks.
function generateCacheKey(files) {
const hash = crypto.createHash('sha256');
files
.map((f) => f.contents)
.forEach((content) => {
hash.update(content);
});
return hash.digest('hex');
}
// Importing .NET code from Typescript is not trivial and a bit lose cannon programming. However, we also want to keep
// this project dependent on as few packages as possible, so instead of opting into some "run .NET in Typescript" package,
// we do the simplest, which is this. Makes C# debugging a bit harder, but it's a compromise.
// Further, we also utilize this for our test fixtures. Running tests in parallel can cause race conditions for fixtures
// at-rest, if two tests are `dotnet publish`'ing to the same fixture folder. So we supply a generator for populating
// fixtures in temporary folders to keep the test stateless while ensuring parallelization.
function generate(tempDirNameSpace, files) {
const key = generateCacheKey(files);
const cached = cache.get(key);
if (cached) {
return cached;
}
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), `snyk-nuget-plugin-test-${tempDirNameSpace}-`));
let tempFilePath;
files.forEach((file) => {
tempFilePath = path.join(tempDir, file.name);
fs.writeFileSync(tempFilePath, file.contents);
});
debug(`Generated temporary CS files (${files
.map((f) => f.name)
.join(',')}) in ${tempDir}`);
cache.set(key, tempDir);
return tempDir;
}
function tearDown(dirs) {
debug(`Attempting to delete temporary CS files in ${dirs.join(',')}`);
for (const dir of dirs) {
if (!dir) {
// No tempDir to tear down. Assuming the test failed somewhere.
// Jest won't throw an error anyway if the operation fails.
return;
}
try {
fs.rmSync(dir, { recursive: true });
}
catch {
// Ignore it, test was tearing down anyway, and it seems Windows boxes especially don't like this.
}
}
}
//# sourceMappingURL=generator.js.map