Skip to content

Commit

Permalink
Merge pull request #83 from turingsecure/80-typescript-types
Browse files Browse the repository at this point in the history
chore: generate typescript types automatically on build
  • Loading branch information
Fubinator authored Jul 16, 2021
2 parents 4aa9dc4 + 248909e commit 4643192
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 22 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">cvss.js by <a href="https://turingpoint.eu" target="_blank">turingpoint.</a></h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-1.4.5-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-1.4.6-blue.svg?cacheSeconds=2592000" />
<a href="#" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
</a>
Expand All @@ -20,9 +20,9 @@ yarn add @turingpointde/cvss.js
Import the library to use it in your code:

```js
const CVSS = require("@turingpointde/cvss.js");
const CVSS = require('@turingpointde/cvss.js');
// or
import CVSS from "@turingpointde/cvss.js";
import CVSS from '@turingpointde/cvss.js';
```

You can also use the library directly from the **CDN** (instead of yarn or npm):
Expand All @@ -37,28 +37,30 @@ After importing the library, the CVSS function must first be called with the vec

```js
// Vector only with base score
const vector1 = CVSS("CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L");
const vector1 = CVSS('CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L');
// Vector with temporal score
const vector2 = CVSS("CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R");
const vector2 = CVSS(
'CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R',
);
// Vector with environmental score
const vector3 = CVSS(
"CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L"
'CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L',
);
```

It is possible to pass in an object as well

```js
const vectorObject = {
CVSS: "3.0",
AV: "N",
AC: "H",
PR: "H",
UI: "R",
S: "U",
C: "H",
I: "N",
A: "N"
CVSS: '3.0',
AV: 'N',
AC: 'H',
PR: 'H',
UI: 'R',
S: 'U',
C: 'H',
I: 'N',
A: 'N',
};

console.log(CVSS(vectorObject).vector); // "CVSS:3.0/AV:N/AC:H/PR:H/UI:R/S:U/C:H/I:N/A:N"
Expand All @@ -69,7 +71,7 @@ To get the scores, simply call the respective function.
```js
// Create a vector
const vector = CVSS(
"CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/E:P/RL:O/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L"
'CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/E:P/RL:O/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L',
);

console.log(vector.getScore()); // 3.6
Expand All @@ -80,7 +82,7 @@ console.log(vector.getEnvironmentalScore()); // 5.1
Sometimes it is useful to get a qualitative rating of a score

```js
const vector = CVSS("CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L");
const vector = CVSS('CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L');

console.log(vector.getRating()); // Medium
console.log(vector.getTemporalRating()); // Medium
Expand All @@ -90,7 +92,7 @@ console.log(vector.getEnvironmentalRating()); // Low
A few useful variables/functions to work with the vectors:

```js
const vector = CVSS("CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L");
const vector = CVSS('CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L');

console.log(vector.isValid); // true
console.log(vector.vector); // CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L
Expand All @@ -101,7 +103,7 @@ The following functions are suitable for displaying the vector in a human-readab

```js
const vector = CVSS(
"CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R/MAC:X/MUI:X/MA:X/MI:X"
'CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R/MAC:X/MUI:X/MA:X/MI:X',
);

console.log(vector.getVectorObject()); // { CVSS: "3.0", AV: "N", AC: "H", PR: "L", UI: "R", S: "C", C: "L", I: "L", A: "L", E: "U", RL: "T", RC: "R", CR: "X", IR: "X", AR: "X", MAV: "X", MAC: "X", MPR: "X", MUI: "X", MS: "X" , MC: "X", MI: "X", MA: "X" }
Expand Down
20 changes: 20 additions & 0 deletions dist/cvss.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export = CVSS;
/**
* Creates a new CVSS object
*
* @param {String} vector
*/
declare function CVSS(vector: string): {
vector: string;
getScore: () => number;
getTemporalScore: () => number;
getEnvironmentalScore: () => number;
getRating: () => string;
getTemporalRating: () => string;
getEnvironmentalRating: () => string;
getVectorObject: () => any;
getDetailedVectorObject: () => any;
getVersion: () => string;
getCleanVectorString: () => string;
isValid: true;
};
2 changes: 1 addition & 1 deletion dist/production.min.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions dist/score.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Parses the vector to a number score
*
* @returns {Number} Calculated Score
*/
export function getScore(vector: any): number;
/**
* Parses the vector to the temporal score
*
* @returns {Number} Temporal Score
*/
export function getTemporalScore(vector: any): number;
/**
* Parses the vector to the environmental score
*
* @returns {Number} Environmental Score
*/
export function getEnvironmentalScore(vector: any): number;
74 changes: 74 additions & 0 deletions dist/util.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @param {Number} num The number to round
*
* @returns The rounded number
*/
export function roundUpExact(num: number): number;
/**
* @param {Number} num The number to round
* @param {Number} precision The number of decimal places to preserve
*
* @returns The rounded number
*/
export function roundUpApprox(num: number, precision: number): number;
/**
* Retrieves an object of vector's metrics
*
* @param {String} vector
* @returns {Object} Abbreviations & Vector Value pair
*/
export function getVectorObject(vector: string): any;
/**
* Retrieves an object of vector's metrics
*
* @param {String} vector
* @returns {Object} Abbreviations & Vectors Detailed Values
*/
export function getDetailedVectorObject(vector: string): any;
/**
* Finds the vector's metric by it's abbreviation
*
* @param {String} abbr
*/
export function findMetric(abbr: string): any;
/**
* Finds the vector's value for a specific metric
*
* @param {String} abbr
* @param {Object} vectorObject
*/
export function findMetricValue(abbr: string, vectorObject: any): any;
/**
* Calculates the rating of the given vector
*
* @param Score calculated score from getScore() in cvss.js
* @returns {String} returns one of the five possible ratings
*/
export function getRating(score: any): string;
/**
* Checks whether the vector passed is valid
*
* @param {String} vector
* @returns {Boolean} result with whether the vector is valid or not
*/
export function isVectorValid(vector: string): boolean;
/**
* This transforms an object in the format of getVectorObject()
* and parses it to a CVSS comaptible string
*
* @param {Object} obj
*/
export function parseVectorObjectToString(obj: any): string;
/**
* Retrives the version from the vector string
*
* @return {String} returns the version number
*/
export function getVersion(vector: any): string;
/**
* Returns a vector without undefined values
*
* @param {String} vector
* @returns {String} Vector without undefined values
*/
export function getCleanVectorString(vector: string): string;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@turingpointde/cvss.js",
"version": "1.4.5",
"version": "1.4.6",
"description": "A tiny library to work with cvss vectors",
"scripts": {
"build": "webpack",
"build": "webpack && tsc",
"start": "node index.js",
"lint": "npx eslint ./",
"test": "jest --detectOpenHandles --forceExit"
Expand All @@ -14,6 +14,7 @@
"devDependencies": {
"eslint": "^7.30.0",
"jest": "^27.0.6",
"typescript": "^4.3.5",
"webpack": "^5.44.0",
"webpack-cli": "^4.7.2"
},
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": ["lib/**/*"],
"compilerOptions": {
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist"
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3383,6 +3383,11 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"

typescript@^4.3.5:
version "4.3.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.5.tgz#4d1c37cc16e893973c45a06886b7113234f119f4"
integrity sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==

universalify@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
Expand Down

0 comments on commit 4643192

Please sign in to comment.