@@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818*/
1919
2020import { existsSync , readFileSync } from 'fs'
21- import { dirname , extname , isAbsolute , join , sep } from 'path'
21+ import { dirname , extname , isAbsolute , join , parse , sep } from 'path'
2222
2323export function isNonNullable < T > ( value : T ) : value is NonNullable < T > {
2424 // NonNullable: not null and not undefined
@@ -92,18 +92,41 @@ export function loadJsonFile (path: string): any {
9292
9393export type MimeType = string
9494
95+ const MIME_TEXT_PLAIN : MimeType = 'text/plain'
96+
9597const MAP_TEXT_EXTENSION_MIME : Readonly < Record < string , MimeType > > = {
96- '' : 'text/plain' ,
97- '.license' : 'text/plain' ,
98- '.licence' : 'text/plain' ,
98+ '' : MIME_TEXT_PLAIN ,
99+ // https://www.iana.org/assignments/media-types/media-types.xhtml
100+ '.csv' : 'text/csv' ,
101+ '.htm' : 'text/html' ,
102+ '.html' : 'text/html' ,
99103 '.md' : 'text/markdown' ,
104+ '.txt' : MIME_TEXT_PLAIN ,
100105 '.rst' : 'text/prs.fallenstein.rst' ,
101- '.txt' : 'text/plain' ,
102- '.xml' : 'text/xml' // not `application/xml` -- our scope is text!
106+ '.xml' : 'text/xml' , // not `application/xml` -- our scope is text!
107+ // add more mime types above this line. pull-requests welcome!
108+ // license-specific files
109+ '.license' : MIME_TEXT_PLAIN ,
110+ '.licence' : MIME_TEXT_PLAIN
103111} as const
104112
105113export function getMimeForTextFile ( filename : string ) : MimeType | undefined {
106114 return MAP_TEXT_EXTENSION_MIME [ extname ( filename ) . toLowerCase ( ) ]
107115}
108116
117+ const LICENSE_FILENAME_BASE = new Set ( [ 'licence' , 'license' ] )
118+ const LICENSE_FILENAME_EXT = new Set ( [
119+ '.apache' ,
120+ '.bsd' ,
121+ '.gpl' ,
122+ '.mit'
123+ ] )
124+
125+ export function getMimeForLicenseFile ( filename : string ) : MimeType | undefined {
126+ const { name, ext } = parse ( filename . toLowerCase ( ) )
127+ return LICENSE_FILENAME_BASE . has ( name ) && LICENSE_FILENAME_EXT . has ( ext )
128+ ? MIME_TEXT_PLAIN
129+ : MAP_TEXT_EXTENSION_MIME [ ext ]
130+ }
131+
109132// endregion MIME
0 commit comments