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 17, 2024
1 parent b067232 commit b0224e1
Show file tree
Hide file tree
Showing 15 changed files with 111,980 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
1 change: 1 addition & 0 deletions tests/_data/dummy_projects/no-manifest/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy license file content
1 change: 1 addition & 0 deletions tests/_data/dummy_projects/with-lockfile/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy license file content
1 change: 1 addition & 0 deletions tests/_data/dummy_projects/with-shrinkwrap/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy license file content
Loading

0 comments on commit b0224e1

Please sign in to comment.