mime-types-no-nodejs 0.9.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +24 -0
- package/.vscode/launch.json +27 -0
- package/README.md +32 -12
- package/db.json +428 -0
- package/{index.js → index.ts} +86 -94
- package/mocha.config.js +15 -4
- package/package.json +8 -5
- package/test/no-node.ts +34 -0
- package/tsconfig.json +18 -0
- package/test/no-node.js +0 -14
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"root": true,
|
|
3
|
+
"parser": "@typescript-eslint/parser",
|
|
4
|
+
"parserOptions": {
|
|
5
|
+
"ecmaVersion": 6,
|
|
6
|
+
"sourceType": "module"
|
|
7
|
+
},
|
|
8
|
+
"plugins": [
|
|
9
|
+
"@typescript-eslint"
|
|
10
|
+
],
|
|
11
|
+
"rules": {
|
|
12
|
+
"@typescript-eslint/naming-convention": "warn",
|
|
13
|
+
"@typescript-eslint/semi": "warn",
|
|
14
|
+
"curly": "off",
|
|
15
|
+
"eqeqeq": "warn",
|
|
16
|
+
"no-throw-literal": "warn",
|
|
17
|
+
"semi": "warn"
|
|
18
|
+
},
|
|
19
|
+
"ignorePatterns": [
|
|
20
|
+
"out",
|
|
21
|
+
"dist",
|
|
22
|
+
"**/*.d.ts"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"name": "Attach to Chrome",
|
|
9
|
+
"port": 9222,
|
|
10
|
+
"request": "attach",
|
|
11
|
+
"type": "pwa-chrome",
|
|
12
|
+
"webRoot": "${workspaceFolder}"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"type": "pwa-node",
|
|
16
|
+
"request": "launch",
|
|
17
|
+
"name": "Launch Program",
|
|
18
|
+
"skipFiles": [
|
|
19
|
+
"<node_internals>/**"
|
|
20
|
+
],
|
|
21
|
+
"program": "${workspaceFolder}/index.ts",
|
|
22
|
+
"outFiles": [
|
|
23
|
+
"${workspaceFolder}/**/*.js"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
package/README.md
CHANGED
|
@@ -10,23 +10,25 @@ Reference:
|
|
|
10
10
|
|
|
11
11
|
## Credits
|
|
12
12
|
|
|
13
|
-
Source modified from [jshttp/mime-types](https://github.com/jshttp/mime-types)
|
|
13
|
+
Source modified from [jshttp/mime-types](https://github.com/jshttp/mime-types).
|
|
14
14
|
|
|
15
15
|
## Why?
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
1. Removed Node.js dependency.
|
|
18
18
|
|
|
19
19
|
See [#50](https://github.com/jshttp/mime-types/issues/50#issuecomment-442916069).
|
|
20
20
|
|
|
21
|
+
2. Re-write in typescript.
|
|
22
|
+
|
|
21
23
|
# Qick start
|
|
22
24
|
|
|
23
|
-
install
|
|
25
|
+
install dependency
|
|
24
26
|
|
|
25
27
|
```
|
|
26
|
-
npm install mime-types-no-
|
|
28
|
+
npm install mime-types-no-nodejs
|
|
27
29
|
```
|
|
28
30
|
|
|
29
|
-
test
|
|
31
|
+
run test (with source)
|
|
30
32
|
|
|
31
33
|
```
|
|
32
34
|
npm install
|
|
@@ -35,8 +37,14 @@ test
|
|
|
35
37
|
|
|
36
38
|
# API
|
|
37
39
|
|
|
40
|
+
```
|
|
41
|
+
import Mime from 'mime-types-no-nodejs';
|
|
42
|
+
```
|
|
43
|
+
|
|
38
44
|
- lookup()
|
|
39
45
|
|
|
46
|
+
Lookup the MIME type for a file / path extension.
|
|
47
|
+
|
|
40
48
|
```
|
|
41
49
|
/**
|
|
42
50
|
* @param {string} path
|
|
@@ -45,21 +53,17 @@ test
|
|
|
45
53
|
function lookup (path);
|
|
46
54
|
```
|
|
47
55
|
|
|
48
|
-
Lookup the MIME type for a file path/extension.
|
|
49
|
-
|
|
50
56
|
- extension()
|
|
51
57
|
|
|
58
|
+
Get the default extension for a MIME type.
|
|
59
|
+
|
|
52
60
|
```
|
|
53
61
|
/**
|
|
54
62
|
* @param {string} type
|
|
55
|
-
* @return {boolean|string}
|
|
63
|
+
* @return {boolean|string} doc type or false if failed
|
|
56
64
|
*/
|
|
57
65
|
function extension (type);
|
|
58
66
|
```
|
|
59
|
-
Get the default extension for a MIME type.
|
|
60
|
-
```
|
|
61
|
-
import Mime from 'mime-types-no-nodejs';
|
|
62
|
-
```
|
|
63
67
|
|
|
64
68
|
Testing code snippet:
|
|
65
69
|
|
|
@@ -75,3 +79,19 @@ Testing code snippet:
|
|
|
75
79
|
assert.equal(Mime.lookup('png'), 'image/png');
|
|
76
80
|
});
|
|
77
81
|
```
|
|
82
|
+
|
|
83
|
+
# Types (since 0.10.0)
|
|
84
|
+
|
|
85
|
+
The types are in [db.json](db.json), in [mime-db](https://github.com/jshttp/mime-db)'s shape
|
|
86
|
+
(source, charset, compressible, extensions; the first extension is the type's default), but
|
|
87
|
+
only those of Anclient / Portfolio files: documents, archives, images, video, audio, and
|
|
88
|
+
semantic-doc's committed docs:
|
|
89
|
+
|
|
90
|
+
| type | extension | what |
|
|
91
|
+
|---|---|---|
|
|
92
|
+
| application/vnd.semantic-doc.html+zip | sdoc | a doc tree's html package |
|
|
93
|
+
| application/vnd.semantic-doc.story+json | story | a committed story's page |
|
|
94
|
+
|
|
95
|
+
A type not listed answers undefined, as an unknown type always did. Add a type to db.json
|
|
96
|
+
when a new kind of file is uploaded or committed. 0.9.x imported the whole mime-db (157 KiB,
|
|
97
|
+
~2,000 types) into every bundle.
|
package/db.json
ADDED
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
{
|
|
2
|
+
"//": {
|
|
3
|
+
"note": "The types of Anclient / Portfolio files only, in mime-db's shape (jshttp/mime-db: source, charset, compressible, extensions; the first extension is the type's default). Add a type here when a new kind of file is uploaded or committed; the vnd.semantic-doc types are semantic-doc's committed docs."
|
|
4
|
+
},
|
|
5
|
+
"application/vnd.semantic-doc.html+zip": {
|
|
6
|
+
"compressible": false,
|
|
7
|
+
"extensions": [
|
|
8
|
+
"sdoc"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
"application/vnd.semantic-doc.story+json": {
|
|
12
|
+
"charset": "UTF-8",
|
|
13
|
+
"compressible": true,
|
|
14
|
+
"extensions": [
|
|
15
|
+
"story"
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
"application/pdf": {
|
|
19
|
+
"source": "iana",
|
|
20
|
+
"compressible": false,
|
|
21
|
+
"extensions": [
|
|
22
|
+
"pdf"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"application/msword": {
|
|
26
|
+
"source": "iana",
|
|
27
|
+
"compressible": false,
|
|
28
|
+
"extensions": [
|
|
29
|
+
"doc",
|
|
30
|
+
"dot"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": {
|
|
34
|
+
"source": "iana",
|
|
35
|
+
"compressible": false,
|
|
36
|
+
"extensions": [
|
|
37
|
+
"docx"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
"application/vnd.ms-excel": {
|
|
41
|
+
"source": "iana",
|
|
42
|
+
"compressible": false,
|
|
43
|
+
"extensions": [
|
|
44
|
+
"xls",
|
|
45
|
+
"xlm",
|
|
46
|
+
"xla",
|
|
47
|
+
"xlc",
|
|
48
|
+
"xlt",
|
|
49
|
+
"xlw"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
|
|
53
|
+
"source": "iana",
|
|
54
|
+
"compressible": false,
|
|
55
|
+
"extensions": [
|
|
56
|
+
"xlsx"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
"application/vnd.ms-powerpoint": {
|
|
60
|
+
"source": "iana",
|
|
61
|
+
"compressible": false,
|
|
62
|
+
"extensions": [
|
|
63
|
+
"ppt",
|
|
64
|
+
"pps",
|
|
65
|
+
"pot"
|
|
66
|
+
]
|
|
67
|
+
},
|
|
68
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": {
|
|
69
|
+
"source": "iana",
|
|
70
|
+
"compressible": false,
|
|
71
|
+
"extensions": [
|
|
72
|
+
"pptx"
|
|
73
|
+
]
|
|
74
|
+
},
|
|
75
|
+
"application/rtf": {
|
|
76
|
+
"source": "iana",
|
|
77
|
+
"compressible": true,
|
|
78
|
+
"extensions": [
|
|
79
|
+
"rtf"
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
"application/json": {
|
|
83
|
+
"source": "iana",
|
|
84
|
+
"charset": "UTF-8",
|
|
85
|
+
"compressible": true,
|
|
86
|
+
"extensions": [
|
|
87
|
+
"json",
|
|
88
|
+
"map"
|
|
89
|
+
]
|
|
90
|
+
},
|
|
91
|
+
"application/xml": {
|
|
92
|
+
"source": "iana",
|
|
93
|
+
"compressible": true,
|
|
94
|
+
"extensions": [
|
|
95
|
+
"xml",
|
|
96
|
+
"xsl",
|
|
97
|
+
"xsd",
|
|
98
|
+
"rng"
|
|
99
|
+
]
|
|
100
|
+
},
|
|
101
|
+
"text/plain": {
|
|
102
|
+
"source": "iana",
|
|
103
|
+
"compressible": true,
|
|
104
|
+
"extensions": [
|
|
105
|
+
"txt",
|
|
106
|
+
"text",
|
|
107
|
+
"conf",
|
|
108
|
+
"def",
|
|
109
|
+
"list",
|
|
110
|
+
"log",
|
|
111
|
+
"in",
|
|
112
|
+
"ini"
|
|
113
|
+
]
|
|
114
|
+
},
|
|
115
|
+
"text/csv": {
|
|
116
|
+
"source": "iana",
|
|
117
|
+
"compressible": true,
|
|
118
|
+
"extensions": [
|
|
119
|
+
"csv"
|
|
120
|
+
]
|
|
121
|
+
},
|
|
122
|
+
"text/markdown": {
|
|
123
|
+
"source": "iana",
|
|
124
|
+
"compressible": true,
|
|
125
|
+
"extensions": [
|
|
126
|
+
"md",
|
|
127
|
+
"markdown"
|
|
128
|
+
]
|
|
129
|
+
},
|
|
130
|
+
"text/x-rst": {
|
|
131
|
+
"compressible": true,
|
|
132
|
+
"extensions": [
|
|
133
|
+
"rst"
|
|
134
|
+
]
|
|
135
|
+
},
|
|
136
|
+
"text/html": {
|
|
137
|
+
"source": "iana",
|
|
138
|
+
"compressible": true,
|
|
139
|
+
"extensions": [
|
|
140
|
+
"html",
|
|
141
|
+
"htm",
|
|
142
|
+
"shtml"
|
|
143
|
+
]
|
|
144
|
+
},
|
|
145
|
+
"text/css": {
|
|
146
|
+
"source": "iana",
|
|
147
|
+
"charset": "UTF-8",
|
|
148
|
+
"compressible": true,
|
|
149
|
+
"extensions": [
|
|
150
|
+
"css"
|
|
151
|
+
]
|
|
152
|
+
},
|
|
153
|
+
"application/zip": {
|
|
154
|
+
"source": "iana",
|
|
155
|
+
"compressible": false,
|
|
156
|
+
"extensions": [
|
|
157
|
+
"zip"
|
|
158
|
+
]
|
|
159
|
+
},
|
|
160
|
+
"application/x-7z-compressed": {
|
|
161
|
+
"source": "apache",
|
|
162
|
+
"compressible": false,
|
|
163
|
+
"extensions": [
|
|
164
|
+
"7z"
|
|
165
|
+
]
|
|
166
|
+
},
|
|
167
|
+
"application/x-rar-compressed": {
|
|
168
|
+
"source": "apache",
|
|
169
|
+
"compressible": false,
|
|
170
|
+
"extensions": [
|
|
171
|
+
"rar"
|
|
172
|
+
]
|
|
173
|
+
},
|
|
174
|
+
"application/gzip": {
|
|
175
|
+
"source": "iana",
|
|
176
|
+
"compressible": false,
|
|
177
|
+
"extensions": [
|
|
178
|
+
"gz"
|
|
179
|
+
]
|
|
180
|
+
},
|
|
181
|
+
"application/x-tar": {
|
|
182
|
+
"source": "apache",
|
|
183
|
+
"compressible": true,
|
|
184
|
+
"extensions": [
|
|
185
|
+
"tar"
|
|
186
|
+
]
|
|
187
|
+
},
|
|
188
|
+
"application/vnd.android.package-archive": {
|
|
189
|
+
"source": "apache",
|
|
190
|
+
"compressible": false,
|
|
191
|
+
"extensions": [
|
|
192
|
+
"apk"
|
|
193
|
+
]
|
|
194
|
+
},
|
|
195
|
+
"application/octet-stream": {
|
|
196
|
+
"source": "iana",
|
|
197
|
+
"compressible": false,
|
|
198
|
+
"extensions": [
|
|
199
|
+
"bin",
|
|
200
|
+
"dms",
|
|
201
|
+
"lrf",
|
|
202
|
+
"mar",
|
|
203
|
+
"so",
|
|
204
|
+
"dist",
|
|
205
|
+
"distz",
|
|
206
|
+
"pkg",
|
|
207
|
+
"bpk",
|
|
208
|
+
"dump",
|
|
209
|
+
"elc",
|
|
210
|
+
"deploy",
|
|
211
|
+
"exe",
|
|
212
|
+
"dll",
|
|
213
|
+
"deb",
|
|
214
|
+
"dmg",
|
|
215
|
+
"iso",
|
|
216
|
+
"img",
|
|
217
|
+
"msi",
|
|
218
|
+
"msp",
|
|
219
|
+
"msm",
|
|
220
|
+
"buffer"
|
|
221
|
+
]
|
|
222
|
+
},
|
|
223
|
+
"image/jpeg": {
|
|
224
|
+
"source": "iana",
|
|
225
|
+
"compressible": false,
|
|
226
|
+
"extensions": [
|
|
227
|
+
"jpeg",
|
|
228
|
+
"jpg",
|
|
229
|
+
"jpe"
|
|
230
|
+
]
|
|
231
|
+
},
|
|
232
|
+
"image/png": {
|
|
233
|
+
"source": "iana",
|
|
234
|
+
"compressible": false,
|
|
235
|
+
"extensions": [
|
|
236
|
+
"png"
|
|
237
|
+
]
|
|
238
|
+
},
|
|
239
|
+
"image/gif": {
|
|
240
|
+
"source": "iana",
|
|
241
|
+
"compressible": false,
|
|
242
|
+
"extensions": [
|
|
243
|
+
"gif"
|
|
244
|
+
]
|
|
245
|
+
},
|
|
246
|
+
"image/webp": {
|
|
247
|
+
"source": "iana",
|
|
248
|
+
"compressible": false,
|
|
249
|
+
"extensions": [
|
|
250
|
+
"webp"
|
|
251
|
+
]
|
|
252
|
+
},
|
|
253
|
+
"image/bmp": {
|
|
254
|
+
"source": "iana",
|
|
255
|
+
"compressible": true,
|
|
256
|
+
"extensions": [
|
|
257
|
+
"bmp",
|
|
258
|
+
"dib"
|
|
259
|
+
]
|
|
260
|
+
},
|
|
261
|
+
"image/svg+xml": {
|
|
262
|
+
"source": "iana",
|
|
263
|
+
"compressible": true,
|
|
264
|
+
"extensions": [
|
|
265
|
+
"svg",
|
|
266
|
+
"svgz"
|
|
267
|
+
]
|
|
268
|
+
},
|
|
269
|
+
"image/tiff": {
|
|
270
|
+
"source": "iana",
|
|
271
|
+
"compressible": false,
|
|
272
|
+
"extensions": [
|
|
273
|
+
"tif",
|
|
274
|
+
"tiff"
|
|
275
|
+
]
|
|
276
|
+
},
|
|
277
|
+
"image/heic": {
|
|
278
|
+
"source": "iana",
|
|
279
|
+
"compressible": false,
|
|
280
|
+
"extensions": [
|
|
281
|
+
"heic"
|
|
282
|
+
]
|
|
283
|
+
},
|
|
284
|
+
"image/heif": {
|
|
285
|
+
"source": "iana",
|
|
286
|
+
"compressible": false,
|
|
287
|
+
"extensions": [
|
|
288
|
+
"heif"
|
|
289
|
+
]
|
|
290
|
+
},
|
|
291
|
+
"image/avif": {
|
|
292
|
+
"source": "iana",
|
|
293
|
+
"compressible": false,
|
|
294
|
+
"extensions": [
|
|
295
|
+
"avif"
|
|
296
|
+
]
|
|
297
|
+
},
|
|
298
|
+
"image/x-icon": {
|
|
299
|
+
"source": "apache",
|
|
300
|
+
"compressible": true,
|
|
301
|
+
"extensions": [
|
|
302
|
+
"ico"
|
|
303
|
+
]
|
|
304
|
+
},
|
|
305
|
+
"video/mp4": {
|
|
306
|
+
"source": "iana",
|
|
307
|
+
"compressible": false,
|
|
308
|
+
"extensions": [
|
|
309
|
+
"mp4",
|
|
310
|
+
"mp4v",
|
|
311
|
+
"mpg4"
|
|
312
|
+
]
|
|
313
|
+
},
|
|
314
|
+
"video/quicktime": {
|
|
315
|
+
"source": "iana",
|
|
316
|
+
"compressible": false,
|
|
317
|
+
"extensions": [
|
|
318
|
+
"qt",
|
|
319
|
+
"mov"
|
|
320
|
+
]
|
|
321
|
+
},
|
|
322
|
+
"video/webm": {
|
|
323
|
+
"source": "apache",
|
|
324
|
+
"compressible": false,
|
|
325
|
+
"extensions": [
|
|
326
|
+
"webm"
|
|
327
|
+
]
|
|
328
|
+
},
|
|
329
|
+
"video/x-matroska": {
|
|
330
|
+
"source": "apache",
|
|
331
|
+
"compressible": false,
|
|
332
|
+
"extensions": [
|
|
333
|
+
"mkv",
|
|
334
|
+
"mk3d",
|
|
335
|
+
"mks"
|
|
336
|
+
]
|
|
337
|
+
},
|
|
338
|
+
"video/3gpp": {
|
|
339
|
+
"source": "iana",
|
|
340
|
+
"compressible": false,
|
|
341
|
+
"extensions": [
|
|
342
|
+
"3gp",
|
|
343
|
+
"3gpp"
|
|
344
|
+
]
|
|
345
|
+
},
|
|
346
|
+
"video/x-msvideo": {
|
|
347
|
+
"source": "apache",
|
|
348
|
+
"compressible": false,
|
|
349
|
+
"extensions": [
|
|
350
|
+
"avi"
|
|
351
|
+
]
|
|
352
|
+
},
|
|
353
|
+
"video/mpeg": {
|
|
354
|
+
"source": "iana",
|
|
355
|
+
"compressible": false,
|
|
356
|
+
"extensions": [
|
|
357
|
+
"mpeg",
|
|
358
|
+
"mpg",
|
|
359
|
+
"mpe",
|
|
360
|
+
"m1v",
|
|
361
|
+
"m2v"
|
|
362
|
+
]
|
|
363
|
+
},
|
|
364
|
+
"audio/mpeg": {
|
|
365
|
+
"source": "iana",
|
|
366
|
+
"compressible": false,
|
|
367
|
+
"extensions": [
|
|
368
|
+
"mpga",
|
|
369
|
+
"mp2",
|
|
370
|
+
"mp2a",
|
|
371
|
+
"mp3",
|
|
372
|
+
"m2a",
|
|
373
|
+
"m3a"
|
|
374
|
+
]
|
|
375
|
+
},
|
|
376
|
+
"audio/mp4": {
|
|
377
|
+
"source": "iana",
|
|
378
|
+
"compressible": false,
|
|
379
|
+
"extensions": [
|
|
380
|
+
"m4a",
|
|
381
|
+
"mp4a"
|
|
382
|
+
]
|
|
383
|
+
},
|
|
384
|
+
"audio/aac": {
|
|
385
|
+
"source": "iana",
|
|
386
|
+
"compressible": false,
|
|
387
|
+
"extensions": [
|
|
388
|
+
"aac"
|
|
389
|
+
]
|
|
390
|
+
},
|
|
391
|
+
"audio/ogg": {
|
|
392
|
+
"source": "iana",
|
|
393
|
+
"compressible": false,
|
|
394
|
+
"extensions": [
|
|
395
|
+
"oga",
|
|
396
|
+
"ogg",
|
|
397
|
+
"spx",
|
|
398
|
+
"opus"
|
|
399
|
+
]
|
|
400
|
+
},
|
|
401
|
+
"audio/wav": {
|
|
402
|
+
"compressible": false,
|
|
403
|
+
"extensions": [
|
|
404
|
+
"wav"
|
|
405
|
+
]
|
|
406
|
+
},
|
|
407
|
+
"audio/webm": {
|
|
408
|
+
"source": "apache",
|
|
409
|
+
"compressible": false,
|
|
410
|
+
"extensions": [
|
|
411
|
+
"weba"
|
|
412
|
+
]
|
|
413
|
+
},
|
|
414
|
+
"audio/x-flac": {
|
|
415
|
+
"source": "apache",
|
|
416
|
+
"compressible": false,
|
|
417
|
+
"extensions": [
|
|
418
|
+
"flac"
|
|
419
|
+
]
|
|
420
|
+
},
|
|
421
|
+
"audio/amr": {
|
|
422
|
+
"source": "iana",
|
|
423
|
+
"compressible": false,
|
|
424
|
+
"extensions": [
|
|
425
|
+
"amr"
|
|
426
|
+
]
|
|
427
|
+
}
|
|
428
|
+
}
|
package/{index.js → index.ts}
RENAMED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/** @module 'mime-type-no-nodejs';
|
|
2
|
+
*
|
|
3
|
+
* Credits: mime-types
|
|
4
|
+
*
|
|
3
5
|
* Copyright(c) 2014 Jonathan Ong
|
|
4
6
|
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
7
|
+
* Copyright(c) 2021 Ody Zhou
|
|
5
8
|
* MIT Licensed
|
|
6
9
|
*/
|
|
7
10
|
|
|
8
|
-
'use strict'
|
|
9
|
-
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
12
|
+
* The types known, in mime-db's shape: only those of Anclient / Portfolio files, and the
|
|
13
|
+
* types of semantic-doc's committed docs (since 0.10.0; mime-db before, the full database).
|
|
13
14
|
*/
|
|
14
|
-
|
|
15
|
-
var db = require('mime-db')
|
|
15
|
+
import db from './db.json';
|
|
16
16
|
|
|
17
17
|
function extname(pth) {
|
|
18
18
|
let reg = /\.([\w\d]+)\s*$/;
|
|
@@ -21,45 +21,73 @@ function extname(pth) {
|
|
|
21
21
|
else return '';
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/** the singleton */
|
|
25
|
+
const mimetypes = {};
|
|
26
|
+
const docextensions = {};
|
|
27
|
+
|
|
28
|
+
// Populate the extensions/types maps
|
|
29
|
+
populateMaps(docextensions, mimetypes);
|
|
30
|
+
|
|
24
31
|
/**
|
|
25
32
|
* Module variables.
|
|
26
33
|
* @private
|
|
27
34
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var TEXT_TYPE_REGEXP = /^text\//i
|
|
35
|
+
const EXTRACT_TYPE_REGEXP = /^\s*([^;\s]*)(?:;|\s|$)/
|
|
36
|
+
const TEXT_TYPE_REGEXP = /^text\//i
|
|
31
37
|
|
|
32
38
|
/**
|
|
33
|
-
*
|
|
34
|
-
* @
|
|
39
|
+
* Populate the extensions and types maps.
|
|
40
|
+
* @private
|
|
35
41
|
*/
|
|
42
|
+
function populateMaps (extensions: object, types: object) {
|
|
43
|
+
// source preference (least -> most)
|
|
44
|
+
let preference = ['nginx', 'apache', undefined, 'iana']
|
|
36
45
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
exports.extension = extension
|
|
41
|
-
exports.extensions = Object.create(null)
|
|
42
|
-
exports.lookup = lookup
|
|
43
|
-
exports.types = Object.create(null)
|
|
46
|
+
Object.keys(db).forEach(function forEachMimeType (type) {
|
|
47
|
+
let mime = db[type] as { source?: string, extensions?: string[] }
|
|
48
|
+
let exts = mime.extensions
|
|
44
49
|
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
if (!exts || !exts.length) {
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// mime -> extensions
|
|
55
|
+
extensions[type] = exts
|
|
56
|
+
|
|
57
|
+
// extension -> mime
|
|
58
|
+
for (let i = 0; i < exts.length; i++) {
|
|
59
|
+
let extension = exts[i]
|
|
60
|
+
|
|
61
|
+
if (types[extension]) {
|
|
62
|
+
let from = preference.indexOf(db[types[extension]].source)
|
|
63
|
+
let to = preference.indexOf(mime.source)
|
|
64
|
+
|
|
65
|
+
if (types[extension] !== 'application/octet-stream' &&
|
|
66
|
+
(from > to || (from === to && types[extension].substr(0, 12) === 'application/'))) {
|
|
67
|
+
// skip the remapping
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// set the extension -> mime
|
|
73
|
+
types[extension] = type
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
}
|
|
47
77
|
|
|
48
78
|
/**
|
|
49
79
|
* Get the default charset for a MIME type.
|
|
50
80
|
*
|
|
51
|
-
* @param
|
|
52
|
-
* @return
|
|
81
|
+
* @param type mime
|
|
82
|
+
* @return charset
|
|
53
83
|
*/
|
|
54
|
-
|
|
55
|
-
function charset (type) {
|
|
84
|
+
function charset(type: string): undefined | string {
|
|
56
85
|
if (!type || typeof type !== 'string') {
|
|
57
|
-
return
|
|
86
|
+
return;
|
|
58
87
|
}
|
|
59
88
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
var mime = match && db[match[1].toLowerCase()]
|
|
89
|
+
let match = EXTRACT_TYPE_REGEXP.exec(type)
|
|
90
|
+
let mime = match && db[match[1].toLowerCase()]
|
|
63
91
|
|
|
64
92
|
if (mime && mime.charset) {
|
|
65
93
|
return mime.charset
|
|
@@ -69,119 +97,83 @@ function charset (type) {
|
|
|
69
97
|
if (match && TEXT_TYPE_REGEXP.test(match[1])) {
|
|
70
98
|
return 'UTF-8'
|
|
71
99
|
}
|
|
72
|
-
|
|
73
|
-
return false
|
|
74
100
|
}
|
|
75
101
|
|
|
76
102
|
/**
|
|
77
103
|
* Create a full Content-Type header given a MIME type or extension.
|
|
78
104
|
*
|
|
79
|
-
* @param
|
|
80
|
-
* @return
|
|
105
|
+
* @param str mime type
|
|
106
|
+
* @return header for the type
|
|
81
107
|
*/
|
|
82
|
-
function contentType
|
|
108
|
+
function contentType(str: string): string | undefined {
|
|
83
109
|
// TODO: should this even be in this module?
|
|
84
110
|
if (!str || typeof str !== 'string') {
|
|
85
|
-
return
|
|
111
|
+
return;
|
|
86
112
|
}
|
|
87
113
|
|
|
88
|
-
|
|
89
|
-
?
|
|
114
|
+
let mime = str.indexOf('/') === -1
|
|
115
|
+
? lookup(str)
|
|
90
116
|
: str
|
|
91
117
|
|
|
92
118
|
if (!mime) {
|
|
93
|
-
return
|
|
119
|
+
return;
|
|
94
120
|
}
|
|
95
121
|
|
|
96
122
|
// TODO: use content-type or other module
|
|
97
123
|
if (mime.indexOf('charset') === -1) {
|
|
98
|
-
|
|
99
|
-
if (
|
|
124
|
+
let chars = charset(mime)
|
|
125
|
+
if (chars) mime += '; charset=' + chars.toLowerCase()
|
|
100
126
|
}
|
|
101
127
|
|
|
102
128
|
return mime
|
|
103
129
|
}
|
|
104
130
|
|
|
105
131
|
/**
|
|
132
|
+
*
|
|
106
133
|
* Get the default extension for a MIME type.
|
|
107
134
|
*
|
|
108
|
-
* @param
|
|
109
|
-
* @return
|
|
135
|
+
* @param type doctype
|
|
136
|
+
* @return mime type
|
|
110
137
|
*/
|
|
111
|
-
function extension
|
|
138
|
+
function extension(type: string): undefined | string {
|
|
112
139
|
if (!type || typeof type !== 'string') {
|
|
113
|
-
return
|
|
140
|
+
return;
|
|
114
141
|
}
|
|
115
142
|
|
|
116
|
-
|
|
117
|
-
var match = EXTRACT_TYPE_REGEXP.exec(type)
|
|
143
|
+
let match = EXTRACT_TYPE_REGEXP.exec(type)
|
|
118
144
|
|
|
119
145
|
// get extensions
|
|
120
|
-
|
|
146
|
+
let exts = match && docextensions[match[1].toLowerCase()]
|
|
121
147
|
|
|
122
148
|
if (!exts || !exts.length) {
|
|
123
|
-
return
|
|
149
|
+
return;
|
|
124
150
|
}
|
|
125
151
|
|
|
126
152
|
return exts[0]
|
|
127
153
|
}
|
|
128
154
|
|
|
155
|
+
|
|
129
156
|
/**
|
|
130
157
|
* Lookup the MIME type for a file path/extension.
|
|
131
158
|
*
|
|
132
|
-
* @param
|
|
133
|
-
* @return
|
|
159
|
+
* @param path doc path
|
|
160
|
+
* @return mime type
|
|
134
161
|
*/
|
|
135
|
-
function lookup (path) {
|
|
162
|
+
function lookup (path: string): string | undefined {
|
|
136
163
|
if (!path || typeof path !== 'string') {
|
|
137
|
-
return
|
|
164
|
+
return;
|
|
138
165
|
}
|
|
139
166
|
|
|
140
167
|
let extension = extname('x.' + path).toLowerCase();
|
|
141
168
|
|
|
142
169
|
if (!extension) {
|
|
143
|
-
return
|
|
170
|
+
return;
|
|
144
171
|
}
|
|
145
172
|
|
|
146
|
-
return
|
|
173
|
+
return mimetypes[extension];
|
|
147
174
|
}
|
|
148
175
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
function populateMaps (extensions, types) {
|
|
154
|
-
// source preference (least -> most)
|
|
155
|
-
var preference = ['nginx', 'apache', undefined, 'iana']
|
|
156
|
-
|
|
157
|
-
Object.keys(db).forEach(function forEachMimeType (type) {
|
|
158
|
-
var mime = db[type]
|
|
159
|
-
var exts = mime.extensions
|
|
160
|
-
|
|
161
|
-
if (!exts || !exts.length) {
|
|
162
|
-
return
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// mime -> extensions
|
|
166
|
-
extensions[type] = exts
|
|
167
|
-
|
|
168
|
-
// extension -> mime
|
|
169
|
-
for (var i = 0; i < exts.length; i++) {
|
|
170
|
-
var extension = exts[i]
|
|
171
|
-
|
|
172
|
-
if (types[extension]) {
|
|
173
|
-
var from = preference.indexOf(db[types[extension]].source)
|
|
174
|
-
var to = preference.indexOf(mime.source)
|
|
175
|
-
|
|
176
|
-
if (types[extension] !== 'application/octet-stream' &&
|
|
177
|
-
(from > to || (from === to && types[extension].substr(0, 12) === 'application/'))) {
|
|
178
|
-
// skip the remapping
|
|
179
|
-
continue
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// set the extension -> mime
|
|
184
|
-
types[extension] = type
|
|
185
|
-
}
|
|
186
|
-
})
|
|
187
|
-
}
|
|
176
|
+
export default {
|
|
177
|
+
extension,
|
|
178
|
+
lookup,
|
|
179
|
+
}
|
package/mocha.config.js
CHANGED
|
@@ -5,7 +5,7 @@ var webpack = require('webpack');
|
|
|
5
5
|
|
|
6
6
|
var config = {
|
|
7
7
|
mode: 'development',
|
|
8
|
-
entry: './test/no-node.
|
|
8
|
+
entry: './test/no-node.ts',
|
|
9
9
|
output: {
|
|
10
10
|
filename: 'testBundle.js'
|
|
11
11
|
},
|
|
@@ -14,16 +14,27 @@ var config = {
|
|
|
14
14
|
plugins: [ ],
|
|
15
15
|
|
|
16
16
|
resolve: {
|
|
17
|
-
extensions: ['.js']
|
|
17
|
+
extensions: ['.ts', '.js']
|
|
18
18
|
},
|
|
19
19
|
|
|
20
20
|
module: {
|
|
21
21
|
rules: [
|
|
22
|
-
{
|
|
22
|
+
{ test: /\.ts$/,
|
|
23
23
|
loader: 'babel-loader',
|
|
24
24
|
options: {
|
|
25
|
-
presets: [
|
|
25
|
+
presets: [
|
|
26
|
+
'@babel/preset-typescript',
|
|
27
|
+
'@babel/preset-env'
|
|
28
|
+
] }
|
|
26
29
|
},
|
|
30
|
+
{ test: /\.js$/,
|
|
31
|
+
loader: 'babel-loader',
|
|
32
|
+
options: {
|
|
33
|
+
presets: [
|
|
34
|
+
'@babel/preset-env'
|
|
35
|
+
] }
|
|
36
|
+
},
|
|
37
|
+
|
|
27
38
|
]
|
|
28
39
|
}
|
|
29
40
|
};
|
package/package.json
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mime-types-no-nodejs",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "index.
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "Typescript mime types to doc type mapping.",
|
|
5
|
+
"main": "index.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"debug": "webpack-cli --config mocha.config.js && mocha -- --inspect inspect dist/testBundle.js ",
|
|
8
8
|
"test": "webpack-cli --config mocha.config.js && mocha dist/testBundle.js "
|
|
9
9
|
},
|
|
10
10
|
"keywords": [
|
|
11
11
|
"mime-types",
|
|
12
|
-
"
|
|
12
|
+
"typescript"
|
|
13
13
|
],
|
|
14
14
|
"author": "Ody Zhou",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"homepage": "https://github.com/odys-z/Mime-type-no-nodejs/blob/main/README.md",
|
|
17
17
|
"devDependencies": {
|
|
18
|
+
"@babel/preset-typescript": "^7.15.0",
|
|
18
19
|
"chai": "^4.3.4",
|
|
19
|
-
"mocha": "^9.1.
|
|
20
|
+
"mocha": "^9.1.3",
|
|
21
|
+
"typescript": "^4.4.4",
|
|
22
|
+
"webpack-cli": "^4.9.1"
|
|
20
23
|
}
|
|
21
24
|
}
|
package/test/no-node.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
|
|
2
|
+
import { assert } from 'chai';
|
|
3
|
+
import Mime from '../index';
|
|
4
|
+
|
|
5
|
+
describe('[case 1. regex]', () => {
|
|
6
|
+
it('mime2type', () => {
|
|
7
|
+
debugger
|
|
8
|
+
assert.equal(Mime.extension('image/png;base64'), 'png', '1 png');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('type2mime', () => {
|
|
12
|
+
assert.equal(Mime.lookup('png'), 'image/png', '00.2 image/png');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('documents', () => {
|
|
16
|
+
assert.equal(Mime.extension('application/vnd.openxmlformats-officedocument.wordprocessingml.document'), 'docx', '2.1 docx');
|
|
17
|
+
assert.equal(Mime.extension('application/pdf'), 'pdf', '2.2 pdf');
|
|
18
|
+
assert.equal(Mime.extension('text/plain; charset=utf-8'), 'txt', '2.3 txt');
|
|
19
|
+
assert.equal(Mime.lookup('mp3'), 'audio/mpeg', '2.4 mp3');
|
|
20
|
+
assert.equal(Mime.lookup('mov'), 'video/quicktime', '2.5 mov');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('semantic-doc', () => {
|
|
24
|
+
assert.equal(Mime.extension('application/vnd.semantic-doc.html+zip'), 'sdoc', '3.1 html package');
|
|
25
|
+
assert.equal(Mime.extension('application/vnd.semantic-doc.story+json'), 'story', '3.2 story page');
|
|
26
|
+
assert.equal(Mime.lookup('sdoc'), 'application/vnd.semantic-doc.html+zip', '3.3 sdoc');
|
|
27
|
+
assert.equal(Mime.lookup('story'), 'application/vnd.semantic-doc.story+json', '3.4 story');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('unknown', () => {
|
|
31
|
+
assert.isUndefined(Mime.extension('application/x-not-listed'), '4.1 not in db.json');
|
|
32
|
+
assert.isUndefined(Mime.lookup('xyz'), '4.2 not in db.json');
|
|
33
|
+
});
|
|
34
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
// Generate d.ts files
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"experimentalDecorators": true,
|
|
7
|
+
"allowSyntheticDefaultImports": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"resolveJsonModule": true,
|
|
10
|
+
"outDir": "dist"
|
|
11
|
+
},
|
|
12
|
+
"exclude": [
|
|
13
|
+
"node_modules",
|
|
14
|
+
"**/*.config.js",
|
|
15
|
+
"test",
|
|
16
|
+
"./typings/**/*"
|
|
17
|
+
]
|
|
18
|
+
}
|
package/test/no-node.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import chai from 'chai';
|
|
3
|
-
import { expect, assert } from 'chai';
|
|
4
|
-
import Mime from '../index';
|
|
5
|
-
|
|
6
|
-
describe('[case 1. regex]', () => {
|
|
7
|
-
it('mime2type', () => {
|
|
8
|
-
assert.equal(Mime.extension('image/png;base64'), 'png', '1 png');
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('type2mime', () => {
|
|
12
|
-
assert.equal(Mime.lookup('png'), 'image/png', '00.2 --png');
|
|
13
|
-
});
|
|
14
|
-
});
|