"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/packages/snyk-protect/src/lib/snyk-file.ts" (20 Feb 2024, 1985 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import { VulnIdAndPackageName } from './types';
    2 import { deQuote } from './utils';
    3 
    4 const lineRegex = /^(\s*)(.*):(?:$| )+(.*)$/i;
    5 
    6 export function extractPatchMetadata(
    7   dotSnykFileContent: string,
    8 ): VulnIdAndPackageName[] {
    9   let writingPatches = false;
   10   let writingTo: string;
   11 
   12   // .snyk parsing => snyk-policy ( or js-yaml )
   13   const patches: { [vulnId: string]: string[] } = dotSnykFileContent
   14     .split('\n')
   15     .filter((l) => l.length && !l.trimStart().startsWith('#'))
   16     .map((line) => lineRegex.exec(line.trimEnd()))
   17     .filter(Boolean)
   18     .reduce((acc, thing) => {
   19       const [, prefix, key, value] = thing as RegExpExecArray;
   20       if (writingPatches && prefix === '') {
   21         writingPatches = false;
   22       } else if (prefix === '' && key === 'patch' && value === '') {
   23         writingPatches = true;
   24       } else if (writingPatches) {
   25         if (prefix.length === 2) {
   26           writingTo = key;
   27           acc[key] = [];
   28         } else {
   29           if (key.startsWith('-')) {
   30             const destination = key
   31               .substring(1)
   32               .split('>')
   33               .pop()
   34               ?.trim()
   35               ?.replace(/['"]/g, '');
   36             if (!acc[writingTo].includes(destination)) {
   37               acc[writingTo].push(destination);
   38             }
   39           }
   40         }
   41       }
   42       return acc;
   43     }, {});
   44 
   45   const vulnIdAndPackageNames: VulnIdAndPackageName[] = [];
   46   for (const vulnId of Object.keys(patches)) {
   47     const packageNames = patches[vulnId];
   48     if (packageNames.length === 0) {
   49       throw new Error(
   50         'should never have no package names for a vulnId in a .snyk file',
   51       );
   52     } else if (packageNames.length > 1) {
   53       throw new Error(
   54         'should never have more than one package name for a vulnId in a .snyk file',
   55       );
   56     } else {
   57       vulnIdAndPackageNames.push({
   58         vulnId: deQuote(vulnId.trim()),
   59         packageName: packageNames[0],
   60       });
   61     }
   62   }
   63 
   64   return vulnIdAndPackageNames;
   65 }