Skip to content

Commit 6b0d2d8

Browse files
committed
add mimeLookup and mimemap
1 parent 0a6de47 commit 6b0d2d8

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

mimemap.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict'
2+
3+
/**
4+
* Lightweight web framework for your serverless applications
5+
* @author Jeremy Daly <[email protected]>
6+
* @license MIT
7+
*/
8+
9+
// Minimal mime map for common file types
10+
11+
module.exports = {
12+
// images
13+
gif: 'image/gif',
14+
ico: 'image/x-icon',
15+
jpg: 'image/jpeg',
16+
jpeg: 'image/jpeg',
17+
png: 'image/png',
18+
svg: 'image/svg+xml',
19+
svgz: 'image/svg+xml',
20+
21+
// text
22+
atom: 'application/atom+xml',
23+
css: 'text/css',
24+
csv: 'text/csv',
25+
html: 'text/html',
26+
htm: 'text/html',
27+
js: 'application/javascript',
28+
json: 'application/json',
29+
map: 'application/json',
30+
rdf: 'application/rdf+xml',
31+
rss: 'application/rss+xml',
32+
webmanifest: 'application/manifest+json',
33+
xml: 'application/xml',
34+
xls: 'application/xml',
35+
36+
// other binary
37+
gz: 'application/gzip',
38+
pdf: 'application/pdf',
39+
zip: 'application/zip',
40+
41+
// fonts
42+
woff: 'application/font-woff',
43+
44+
// MS file Types
45+
doc: 'application/msword',
46+
dot: 'application/msword',
47+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
48+
dotx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
49+
docm: 'application/vnd.ms-word.document.macroEnabled.12',
50+
dotm: 'application/vnd.ms-word.template.macroEnabled.12',
51+
xls: 'application/vnd.ms-excel',
52+
xlt: 'application/vnd.ms-excel',
53+
xla: 'application/vnd.ms-excel',
54+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
55+
xltx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
56+
xlsm: 'application/vnd.ms-excel.sheet.macroEnabled.12',
57+
xltm: 'application/vnd.ms-excel.template.macroEnabled.12',
58+
xlam: 'application/vnd.ms-excel.addin.macroEnabled.12',
59+
xlsb: 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
60+
ppt: 'application/vnd.ms-powerpoint',
61+
pot: 'application/vnd.ms-powerpoint',
62+
pps: 'application/vnd.ms-powerpoint',
63+
ppa: 'application/vnd.ms-powerpoint',
64+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
65+
potx: 'application/vnd.openxmlformats-officedocument.presentationml.template',
66+
ppsx: 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
67+
ppam: 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
68+
pptm: 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
69+
potm: 'application/vnd.ms-powerpoint.template.macroEnabled.12',
70+
ppsm: 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
71+
mdb: 'application/vnd.ms-access'
72+
73+
}

utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,34 @@ module.exports.encodeUrl = url => String(url)
2626
.replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
2727
.replace(ENCODE_CHARS_REGEXP, encodeURI)
2828

29+
30+
2931
module.exports.encodeBody = body =>
3032
typeof body === 'object' ? JSON.stringify(body) : (body && typeof body !== 'string' ? body.toString() : (body ? body : ''))
3133

34+
35+
3236
module.exports.parseBody = body => {
3337
try {
3438
return JSON.parse(body)
3539
} catch(e) {
3640
return body;
3741
}
3842
}
43+
44+
45+
46+
const mimeMap = require('./mimemap.js') // MIME Map
47+
48+
module.exports.mimeLookup = (input,custom={}) => {
49+
let type = input.trim().replace(/^\./,'')
50+
51+
// If it contains a slash, return unmodified
52+
if (/.*\/.*/.test(type)) {
53+
return input.trim()
54+
} else {
55+
// Lookup mime type
56+
let mime = Object.assign(mimeMap,custom)[type]
57+
return mime ? mime : false
58+
}
59+
}

0 commit comments

Comments
 (0)