diff --git a/package-lock.json b/package-lock.json index 3fd03789..a9056b0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11081,6 +11081,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "@nodesecure/mama": "^1.0.0", "@openally/result": "^1.2.1", "fastest-levenshtein": "^1.0.16", "spdx-expression-parse": "^4.0.0" diff --git a/workspaces/conformance/package.json b/workspaces/conformance/package.json index 39d4844e..42b0c85c 100644 --- a/workspaces/conformance/package.json +++ b/workspaces/conformance/package.json @@ -40,6 +40,7 @@ "node-estree": "^4.0.0" }, "dependencies": { + "@nodesecure/mama": "^1.0.0", "@openally/result": "^1.2.1", "fastest-levenshtein": "^1.0.16", "spdx-expression-parse": "^4.0.0" diff --git a/workspaces/conformance/src/extract.ts b/workspaces/conformance/src/extract.ts index 1dc05700..8730f7d4 100644 --- a/workspaces/conformance/src/extract.ts +++ b/workspaces/conformance/src/extract.ts @@ -3,6 +3,9 @@ import * as path from "node:path"; import * as fsSync from "node:fs"; import * as fs from "node:fs/promises"; +// Import Third-party Dependencies +import { ManifestManager } from "@nodesecure/mama"; + // Import Internal Dependencies import * as utils from "./utils/index.js"; import { @@ -14,6 +17,7 @@ import { // CONSTANTS const kManifestFileName = "package.json"; +const kInvalidLicense = "invalid license"; export interface ExtractAsyncOptions { fsEngine?: typeof fs; @@ -25,16 +29,15 @@ export async function extractLicenses( ): Promise { const { fsEngine = fs } = options; - const packageStr = await fsEngine.readFile( - path.join(location, kManifestFileName), "utf-8" + const mama = await ManifestManager.fromPackageJSON( + location ); - const packageJSON = JSON.parse(packageStr); - const licenseData = new LicenseResult(); - licenseData.addLicenseID( - utils.parsePackageLicense(packageJSON), - kManifestFileName - ); + const licenseData = new LicenseResult() + .addLicenseID( + mama.license ?? kInvalidLicense, + kManifestFileName + ); const dirents = await fsEngine.readdir(location, { withFileTypes: true @@ -66,10 +69,11 @@ export function extractLicensesSync( path.join(location, kManifestFileName), "utf-8" ); const packageJSON = JSON.parse(packageStr); + const mama = new ManifestManager(packageJSON); const licenseData = new LicenseResult(); licenseData.addLicenseID( - utils.parsePackageLicense(packageJSON), + mama.license ?? kInvalidLicense, kManifestFileName ); diff --git a/workspaces/conformance/src/utils/index.ts b/workspaces/conformance/src/utils/index.ts index 2733ffdb..d27b64e5 100644 --- a/workspaces/conformance/src/utils/index.ts +++ b/workspaces/conformance/src/utils/index.ts @@ -1,3 +1,2 @@ -export * from "./parsePackageLicense.js"; export * from "./extractDirentLicenses.js"; export * from "./spdx.js"; diff --git a/workspaces/conformance/src/utils/parsePackageLicense.ts b/workspaces/conformance/src/utils/parsePackageLicense.ts deleted file mode 100644 index 1cbb6d70..00000000 --- a/workspaces/conformance/src/utils/parsePackageLicense.ts +++ /dev/null @@ -1,45 +0,0 @@ -// CONSTANTS -const kInvalidLicense = "invalid license"; - -export interface PackageJSONLicense { - type?: string | undefined | null; -} - -export interface PackageJSON { - license?: string | PackageJSONLicense; - licenses?: PackageJSONLicense[] | PackageJSONLicense; -} - -// code from https://github.com/cutenode/liblice/blob/master/lib/parseLicense.js -export function parsePackageLicense( - packageJSON: PackageJSON -): string { - if (packageJSON.license !== undefined) { - if (typeof packageJSON.license === "string") { - return handleUndefinedAndNull(packageJSON.license); - } - - if (typeof packageJSON.license === "object") { - return handleUndefinedAndNull(packageJSON.license.type); - } - } - - if (packageJSON.licenses !== undefined) { - if (Array.isArray(packageJSON.licenses)) { - return handleUndefinedAndNull(packageJSON.licenses[0].type); - } - - if (typeof packageJSON.licenses === "object") { - return handleUndefinedAndNull(packageJSON.licenses.type); - } - } - - return kInvalidLicense; -} - -export function handleUndefinedAndNull( - licenseString?: string | null | undefined -): string { - // eslint-disable-next-line no-eq-null - return licenseString == null ? kInvalidLicense : licenseString; -} diff --git a/workspaces/conformance/test/utils.spec.ts b/workspaces/conformance/test/utils.spec.ts index 6df3b31b..c4582004 100644 --- a/workspaces/conformance/test/utils.spec.ts +++ b/workspaces/conformance/test/utils.spec.ts @@ -65,73 +65,6 @@ describe("extractDirentLicenses", () => { }); }); -describe("parsePackageLicense", () => { - it("should return 'MIT' for parsePackageLicense license MIT", () => { - const result = utils.parsePackageLicense({ - license: "MIT" - }); - assert.strictEqual(result, "MIT"); - }); - - it("should return 'MIT AND (CC0-1.0 OR ISC)' for parsePackageLicense of Object", () => { - const result = utils.parsePackageLicense({ - license: { - type: "MIT AND (CC0-1.0 OR ISC)" - } - }); - assert.strictEqual(result, "MIT AND (CC0-1.0 OR ISC)"); - }); - - test("parsePackageLicense of payload with licenses property", () => { - const result = utils.parsePackageLicense({ - licenses: { - type: "MIT AND (CC0-1.0 OR ISC)" - } - }); - assert.strictEqual(result, "MIT AND (CC0-1.0 OR ISC)"); - }); - - test("parsePackageLicense of payload with licenses property as Array", () => { - const result = utils.parsePackageLicense({ - licenses: [ - { - type: "ISC" - } - ] - }); - assert.strictEqual(result, "ISC"); - }); - - it("should return 'invalid license' for a PackageJSON with no licenses", () => { - const result = utils.parsePackageLicense({}); - assert.strictEqual(result, "invalid license"); - }); -}); - -describe("handleUndefinedAndNull", () => { - it("should return 'invalid license' for null or undefined", () => { - assert.strictEqual( - utils.handleUndefinedAndNull(), - "invalid license" - ); - assert.strictEqual( - utils.handleUndefinedAndNull(undefined), - "invalid license" - ); - assert.strictEqual( - utils.handleUndefinedAndNull(null), - "invalid license" - ); - }); - - it("should return provided string primitive", () => { - assert.strictEqual( - utils.handleUndefinedAndNull("MIT"), - "MIT" - ); - }); -}); - function createDirent(name: string, isFile = true) { return { isFile: () => isFile, diff --git a/workspaces/conformance/tsconfig.json b/workspaces/conformance/tsconfig.json index 310a9030..0e1e4ee0 100644 --- a/workspaces/conformance/tsconfig.json +++ b/workspaces/conformance/tsconfig.json @@ -5,5 +5,9 @@ "outDir": "dist", }, "include": ["src"], - "references": [] + "references": [ + { + "path": "../mama" + } + ] }