Skip to content

Commit c0c3f7d

Browse files
authored
refactor: copy/past mime-helpers (#1246)
followup of #1243 make implementation a copy-past of existing art: <https://github.com/CycloneDX/cyclonedx-node-yarn/blob/main/src/_helpers.ts> this should make it easier for CycloneDX/cyclonedx-javascript-library#1162 --------- Signed-off-by: Jan Kowalleck <[email protected]>
1 parent b067232 commit c0c3f7d

File tree

503 files changed

+88925
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

503 files changed

+88925
-186
lines changed

src/_helpers.ts

+37-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

2020
import { readFileSync, writeSync } from 'fs'
21-
import { parse } from 'path'
21+
import { extname, parse } from 'path'
2222

2323
export function loadJsonFile (path: string): any {
2424
return JSON.parse(readFileSync(path, 'utf8'))
@@ -58,24 +58,45 @@ export function tryRemoveSecretsFromUrl (url: string): string {
5858
}
5959
}
6060

61-
export const LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i
61+
// region MIME
62+
63+
export type MimeType = string
64+
65+
const MIME_TEXT_PLAIN: MimeType = 'text/plain'
66+
67+
const MAP_TEXT_EXTENSION_MIME: Readonly<Record<string, MimeType>> = {
68+
'': MIME_TEXT_PLAIN,
69+
// https://www.iana.org/assignments/media-types/media-types.xhtml
70+
'.csv': 'text/csv',
71+
'.htm': 'text/html',
72+
'.html': 'text/html',
73+
'.md': 'text/markdown',
74+
'.txt': MIME_TEXT_PLAIN,
75+
'.rst': 'text/prs.fallenstein.rst',
76+
'.xml': 'text/xml', // not `application/xml` -- our scope is text!
77+
// add more mime types above this line. pull-requests welcome!
78+
// license-specific files
79+
'.license': MIME_TEXT_PLAIN,
80+
'.licence': MIME_TEXT_PLAIN
81+
} as const
82+
83+
export function getMimeForTextFile (filename: string): MimeType | undefined {
84+
return MAP_TEXT_EXTENSION_MIME[extname(filename).toLowerCase()]
85+
}
86+
6287
const LICENSE_FILENAME_BASE = new Set(['licence', 'license'])
63-
const LICENSE_FILENAME_EXT = new Set(['.apache', '.bsd', '.gpl', '.mit'])
64-
const MAP_TEXT_EXTENSION_MIME = new Map([
65-
['', 'text/plain'],
66-
['.htm', 'text/html'],
67-
['.html', 'text/html'],
68-
['.md', 'text/markdown'],
69-
['.txt', 'text/plain'],
70-
['.rst', 'text/prs.fallenstein.rst'],
71-
['.xml', 'text/xml'],
72-
['.license', 'text/plain'],
73-
['.licence', 'text/plain']
88+
const LICENSE_FILENAME_EXT = new Set([
89+
'.apache',
90+
'.bsd',
91+
'.gpl',
92+
'.mit'
7493
])
7594

76-
export function getMimeForLicenseFile (filename: string): string | undefined {
95+
export function getMimeForLicenseFile (filename: string): MimeType | undefined {
7796
const { name, ext } = parse(filename.toLowerCase())
7897
return LICENSE_FILENAME_BASE.has(name) && LICENSE_FILENAME_EXT.has(ext)
79-
? 'text/plain'
80-
: MAP_TEXT_EXTENSION_MIME.get(ext)
98+
? MIME_TEXT_PLAIN
99+
: MAP_TEXT_EXTENSION_MIME[ext]
81100
}
101+
102+
// endregion MIME

src/builders.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ import * as normalizePackageData from 'normalize-package-data'
2323
import * as path from 'path'
2424
import { join } from 'path'
2525

26-
import {
27-
getMimeForLicenseFile,
28-
isString,
29-
LICENSE_FILENAME_PATTERN,
30-
loadJsonFile, tryRemoveSecretsFromUrl
31-
} from './_helpers'
26+
import { getMimeForLicenseFile, isString, loadJsonFile, tryRemoveSecretsFromUrl } from './_helpers'
3227
import { makeNpmRunner, type runFunc } from './npmRunner'
3328
import { PropertyNames, PropertyValueBool } from './properties'
3429
import { versionCompare } from './versionCompare'
@@ -637,10 +632,12 @@ export class BomBuilder {
637632
}
638633
}
639634

635+
readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i
636+
640637
private * fetchLicenseEvidence (path: string): Generator<Models.License | null, void, void> {
641638
const files = readdirSync(path)
642639
for (const file of files) {
643-
if (!LICENSE_FILENAME_PATTERN.test(file)) {
640+
if (!this.#LICENSE_FILENAME_PATTERN.test(file)) {
644641
continue
645642
}
646643

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
* linguist-vendored -text
3+
**/* linguist-vendored -text
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*
2+
!/.gitignore
3+
!/.gitattributes
4+
5+
!/node_modules
6+
!/node_modules/*
7+
!/node_modules/**/*
8+
!/package.json
9+
!/package-lock.json
10+
11+
!/setup.sh
12+
!/README.md
13+
!/LICENCE.mit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a dummy license file.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# setup with prepared data and structure
2+
3+
this setup actually vendors some 3rd-party data in the `node_modules` dir:
4+
- package manifests
5+
- license files
6+
7+
this dir is already set up as expected.
8+
no need to run `npm install`.
9+
10+
if this setup is to be changed or recreated, run `./setup.sh`.

0 commit comments

Comments
 (0)