Skip to content

Commit

Permalink
refactor: copy/past mime-helpers
Browse files Browse the repository at this point in the history
make implementation a copy-past of exisitng art: <https://github.com/CycloneDX/cyclonedx-node-yarn/blob/main/src/_helpers.ts>

Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Dec 18, 2024
1 parent b067232 commit de32197
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 116 deletions.
53 changes: 37 additions & 16 deletions src/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import { readFileSync, writeSync } from 'fs'
import { parse } from 'path'
import { extname, parse } from 'path'

export function loadJsonFile (path: string): any {
return JSON.parse(readFileSync(path, 'utf8'))
Expand Down Expand Up @@ -58,24 +58,45 @@ export function tryRemoveSecretsFromUrl (url: string): string {
}
}

export const LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i
// region MIME

export type MimeType = string

const MIME_TEXT_PLAIN: MimeType = 'text/plain'

const MAP_TEXT_EXTENSION_MIME: Readonly<Record<string, MimeType>> = {
'': MIME_TEXT_PLAIN,
// https://www.iana.org/assignments/media-types/media-types.xhtml
'.csv': 'text/csv',
'.htm': 'text/html',
'.html': 'text/html',
'.md': 'text/markdown',
'.txt': MIME_TEXT_PLAIN,
'.rst': 'text/prs.fallenstein.rst',
'.xml': 'text/xml', // not `application/xml` -- our scope is text!
// add more mime types above this line. pull-requests welcome!
// license-specific files
'.license': MIME_TEXT_PLAIN,
'.licence': MIME_TEXT_PLAIN
} as const

export function getMimeForTextFile (filename: string): MimeType | undefined {
return MAP_TEXT_EXTENSION_MIME[extname(filename).toLowerCase()]
}

const LICENSE_FILENAME_BASE = new Set(['licence', 'license'])
const LICENSE_FILENAME_EXT = new Set(['.apache', '.bsd', '.gpl', '.mit'])
const MAP_TEXT_EXTENSION_MIME = new Map([
['', 'text/plain'],
['.htm', 'text/html'],
['.html', 'text/html'],
['.md', 'text/markdown'],
['.txt', 'text/plain'],
['.rst', 'text/prs.fallenstein.rst'],
['.xml', 'text/xml'],
['.license', 'text/plain'],
['.licence', 'text/plain']
const LICENSE_FILENAME_EXT = new Set([
'.apache',
'.bsd',
'.gpl',
'.mit'
])

export function getMimeForLicenseFile (filename: string): string | undefined {
export function getMimeForLicenseFile (filename: string): MimeType | undefined {
const { name, ext } = parse(filename.toLowerCase())
return LICENSE_FILENAME_BASE.has(name) && LICENSE_FILENAME_EXT.has(ext)
? 'text/plain'
: MAP_TEXT_EXTENSION_MIME.get(ext)
? MIME_TEXT_PLAIN
: MAP_TEXT_EXTENSION_MIME[ext]
}

// endregion MIME
11 changes: 4 additions & 7 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ import * as normalizePackageData from 'normalize-package-data'
import * as path from 'path'
import { join } from 'path'

import {
getMimeForLicenseFile,
isString,
LICENSE_FILENAME_PATTERN,
loadJsonFile, tryRemoveSecretsFromUrl
} from './_helpers'
import { getMimeForLicenseFile, isString, loadJsonFile, tryRemoveSecretsFromUrl } from './_helpers'
import { makeNpmRunner, type runFunc } from './npmRunner'
import { PropertyNames, PropertyValueBool } from './properties'
import { versionCompare } from './versionCompare'
Expand Down Expand Up @@ -637,10 +632,12 @@ export class BomBuilder {
}
}

readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i

private * fetchLicenseEvidence (path: string): Generator<Models.License | null, void, void> {
const files = readdirSync(path)
for (const file of files) {
if (!LICENSE_FILENAME_PATTERN.test(file)) {
if (!this.#LICENSE_FILENAME_PATTERN.test(file)) {
continue
}

Expand Down
1 change: 1 addition & 0 deletions tests/_data/dummy_projects/no-lockfile/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy license file content
27 changes: 3 additions & 24 deletions tests/unit/_helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,17 @@ SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

const { getMimeForLicenseFile, LICENSE_FILENAME_PATTERN } = require('../../dist/_helpers')
const { getMimeForLicenseFile } = require('../../dist/_helpers')
const { describe, expect, test } = require('@jest/globals')

describe('LICENSE_FILENAME_PATTERN', () => {
test.each([
'LICENCE',
'licence',
'LICENSE',
'license',
'NOTICE',
'UNLICENCE',
'UNLICENSE'])('valid name: %s', (fileName) => {
const value = LICENSE_FILENAME_PATTERN.test(fileName)
expect(value).toBeTruthy()
})
test.each([
'my-license',
'my_license',
'myNotice',
'the-LICENSE'])('invalid name: %s', (fileName) => {
const value = LICENSE_FILENAME_PATTERN.test(fileName)
expect(value).toBeFalsy()
})
})

describe('getMimeForLicenseFile', () => {
test.each([
['LICENCE', 'text/plain'],
['site.html', 'text/html'],
['license.md', 'text/markdown'],
['info.xml', 'text/xml'],
['UNKNOWN', 'text/plain']
['UNKNOWN', 'text/plain'],
['LICENCE.MIT', 'text/plain']
])('check %s', (fileName, expected) => {
const value = getMimeForLicenseFile(fileName)
expect(value).toBe(expected)
Expand Down
69 changes: 0 additions & 69 deletions tests/unit/builders.BomBuilder.spec.js

This file was deleted.

0 comments on commit de32197

Please sign in to comment.