file-type 5.1.0 → 6.2.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/index.js +98 -51
- package/license +4 -16
- package/package.json +108 -106
- package/readme.md +12 -2
package/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
const toBytes = s => Array.from(s).map(c => c.charCodeAt(0));
|
|
3
|
+
const xpiZipFilename = toBytes('META-INF/mozilla.rsa');
|
|
4
|
+
const oxmlContentTypes = toBytes('[Content_Types].xml');
|
|
5
|
+
const oxmlRels = toBytes('_rels/.rels');
|
|
2
6
|
|
|
3
7
|
module.exports = input => {
|
|
4
8
|
const buf = new Uint8Array(input);
|
|
@@ -13,7 +17,13 @@ module.exports = input => {
|
|
|
13
17
|
}, opts);
|
|
14
18
|
|
|
15
19
|
for (let i = 0; i < header.length; i++) {
|
|
16
|
-
|
|
20
|
+
// If a bitmask is set
|
|
21
|
+
if (opts.mask) {
|
|
22
|
+
// If header doesn't equal `buf` with bits masked off
|
|
23
|
+
if (header[i] !== (opts.mask[i] & buf[i + opts.offset])) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
} else if (header[i] !== buf[i + opts.offset]) {
|
|
17
27
|
return false;
|
|
18
28
|
}
|
|
19
29
|
}
|
|
@@ -98,27 +108,62 @@ module.exports = input => {
|
|
|
98
108
|
};
|
|
99
109
|
}
|
|
100
110
|
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
+
// Zip-based file formats
|
|
112
|
+
// Need to be before the `zip` check
|
|
113
|
+
if (check([0x50, 0x4B, 0x3, 0x4])) {
|
|
114
|
+
if (
|
|
115
|
+
check([0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70], {offset: 30})
|
|
116
|
+
) {
|
|
117
|
+
return {
|
|
118
|
+
ext: 'epub',
|
|
119
|
+
mime: 'application/epub+zip'
|
|
120
|
+
};
|
|
121
|
+
}
|
|
111
122
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
123
|
+
// Assumes signed `.xpi` from addons.mozilla.org
|
|
124
|
+
if (check(xpiZipFilename, {offset: 30})) {
|
|
125
|
+
return {
|
|
126
|
+
ext: 'xpi',
|
|
127
|
+
mime: 'application/x-xpinstall'
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// https://github.com/file/file/blob/master/magic/Magdir/msooxml
|
|
132
|
+
if (check(oxmlContentTypes, {offset: 30}) || check(oxmlRels, {offset: 30})) {
|
|
133
|
+
const sliced = buf.subarray(4, 4 + 2000);
|
|
134
|
+
const nextZipHeaderIndex = arr => arr.findIndex((el, i, arr) => arr[i] === 0x50 && arr[i + 1] === 0x4B && arr[i + 2] === 0x3 && arr[i + 3] === 0x4);
|
|
135
|
+
const header2Pos = nextZipHeaderIndex(sliced);
|
|
136
|
+
|
|
137
|
+
if (header2Pos !== -1) {
|
|
138
|
+
const slicedAgain = buf.subarray(header2Pos + 8, header2Pos + 8 + 1000);
|
|
139
|
+
const header3Pos = nextZipHeaderIndex(slicedAgain);
|
|
140
|
+
|
|
141
|
+
if (header3Pos !== -1) {
|
|
142
|
+
const offset = 8 + header2Pos + header3Pos + 30;
|
|
143
|
+
|
|
144
|
+
if (check(toBytes('word/'), {offset})) {
|
|
145
|
+
return {
|
|
146
|
+
ext: 'docx',
|
|
147
|
+
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (check(toBytes('ppt/'), {offset})) {
|
|
152
|
+
return {
|
|
153
|
+
ext: 'pptx',
|
|
154
|
+
mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (check(toBytes('xl/'), {offset})) {
|
|
159
|
+
return {
|
|
160
|
+
ext: 'xlsx',
|
|
161
|
+
mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
122
167
|
}
|
|
123
168
|
|
|
124
169
|
if (
|
|
@@ -177,33 +222,25 @@ module.exports = input => {
|
|
|
177
222
|
};
|
|
178
223
|
}
|
|
179
224
|
|
|
180
|
-
if (
|
|
225
|
+
if (check([0x33, 0x67, 0x70, 0x35]) || // 3gp5
|
|
181
226
|
(
|
|
182
|
-
check([0x0, 0x0, 0x0]) &&
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
) {
|
|
227
|
+
check([0x0, 0x0, 0x0]) && check([0x66, 0x74, 0x79, 0x70], {offset: 4}) &&
|
|
228
|
+
(
|
|
229
|
+
check([0x6D, 0x70, 0x34, 0x31], {offset: 8}) || // MP41
|
|
230
|
+
check([0x6D, 0x70, 0x34, 0x32], {offset: 8}) || // MP42
|
|
231
|
+
check([0x69, 0x73, 0x6F, 0x6D], {offset: 8}) || // ISOM
|
|
232
|
+
check([0x69, 0x73, 0x6F, 0x32], {offset: 8}) || // ISO2
|
|
233
|
+
check([0x6D, 0x6D, 0x70, 0x34], {offset: 8}) || // MMP4
|
|
234
|
+
check([0x4D, 0x34, 0x56], {offset: 8}) || // M4V
|
|
235
|
+
check([0x64, 0x61, 0x73, 0x68], {offset: 8}) // DASH
|
|
236
|
+
)
|
|
237
|
+
)) {
|
|
194
238
|
return {
|
|
195
239
|
ext: 'mp4',
|
|
196
240
|
mime: 'video/mp4'
|
|
197
241
|
};
|
|
198
242
|
}
|
|
199
243
|
|
|
200
|
-
if (check([0x0, 0x0, 0x0, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x56])) {
|
|
201
|
-
return {
|
|
202
|
-
ext: 'm4v',
|
|
203
|
-
mime: 'video/x-m4v'
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
244
|
if (check([0x4D, 0x54, 0x68, 0x64])) {
|
|
208
245
|
return {
|
|
209
246
|
ext: 'mid',
|
|
@@ -216,7 +253,7 @@ module.exports = input => {
|
|
|
216
253
|
const sliced = buf.subarray(4, 4 + 4096);
|
|
217
254
|
const idPos = sliced.findIndex((el, i, arr) => arr[i] === 0x42 && arr[i + 1] === 0x82);
|
|
218
255
|
|
|
219
|
-
if (idPos
|
|
256
|
+
if (idPos !== -1) {
|
|
220
257
|
const docTypePos = idPos + 3;
|
|
221
258
|
const findDocType = type => Array.from(type).every((c, i) => sliced[docTypePos + i] === c.charCodeAt(0));
|
|
222
259
|
|
|
@@ -271,14 +308,17 @@ module.exports = input => {
|
|
|
271
308
|
};
|
|
272
309
|
}
|
|
273
310
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
311
|
+
// Check for MP3 header at different starting offsets
|
|
312
|
+
for (let start = 0; start < 2 && start < (buf.length - 16); start++) {
|
|
313
|
+
if (
|
|
314
|
+
check([0x49, 0x44, 0x33], {offset: start}) || // ID3 header
|
|
315
|
+
check([0xFF, 0xE2], {offset: start, mask: [0xFF, 0xE2]}) // MPEG 1 or 2 Layer 3 header
|
|
316
|
+
) {
|
|
317
|
+
return {
|
|
318
|
+
ext: 'mp3',
|
|
319
|
+
mime: 'audio/mpeg'
|
|
320
|
+
};
|
|
321
|
+
}
|
|
282
322
|
}
|
|
283
323
|
|
|
284
324
|
if (
|
|
@@ -534,7 +574,7 @@ module.exports = input => {
|
|
|
534
574
|
};
|
|
535
575
|
}
|
|
536
576
|
|
|
537
|
-
if (check([0x47], {offset: 4})) {
|
|
577
|
+
if (check([0x47], {offset: 4}) && (check([0x47], {offset: 192}) || check([0x47], {offset: 196}))) {
|
|
538
578
|
return {
|
|
539
579
|
ext: 'mts',
|
|
540
580
|
mime: 'video/mp2t'
|
|
@@ -548,5 +588,12 @@ module.exports = input => {
|
|
|
548
588
|
};
|
|
549
589
|
}
|
|
550
590
|
|
|
591
|
+
if (check([0x42, 0x50, 0x47, 0xFB])) {
|
|
592
|
+
return {
|
|
593
|
+
ext: 'bpg',
|
|
594
|
+
mime: 'image/bpg'
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
|
|
551
598
|
return null;
|
|
552
599
|
};
|
package/license
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
3
|
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
4
4
|
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
11
6
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
14
8
|
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,108 +1,110 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
2
|
+
"name": "file-type",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"description": "Detect the file type of a Buffer/Uint8Array",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "sindresorhus/file-type",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Sindre Sorhus",
|
|
9
|
+
"email": "sindresorhus@gmail.com",
|
|
10
|
+
"url": "sindresorhus.com"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=4"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "xo && ava"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"index.js"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"mime",
|
|
23
|
+
"file",
|
|
24
|
+
"type",
|
|
25
|
+
"archive",
|
|
26
|
+
"image",
|
|
27
|
+
"img",
|
|
28
|
+
"pic",
|
|
29
|
+
"picture",
|
|
30
|
+
"flash",
|
|
31
|
+
"photo",
|
|
32
|
+
"video",
|
|
33
|
+
"detect",
|
|
34
|
+
"check",
|
|
35
|
+
"is",
|
|
36
|
+
"exif",
|
|
37
|
+
"exe",
|
|
38
|
+
"binary",
|
|
39
|
+
"buffer",
|
|
40
|
+
"uint8array",
|
|
41
|
+
"jpg",
|
|
42
|
+
"png",
|
|
43
|
+
"gif",
|
|
44
|
+
"webp",
|
|
45
|
+
"flif",
|
|
46
|
+
"cr2",
|
|
47
|
+
"tif",
|
|
48
|
+
"bmp",
|
|
49
|
+
"jxr",
|
|
50
|
+
"psd",
|
|
51
|
+
"zip",
|
|
52
|
+
"tar",
|
|
53
|
+
"rar",
|
|
54
|
+
"gz",
|
|
55
|
+
"bz2",
|
|
56
|
+
"7z",
|
|
57
|
+
"dmg",
|
|
58
|
+
"mp4",
|
|
59
|
+
"m4v",
|
|
60
|
+
"mid",
|
|
61
|
+
"mkv",
|
|
62
|
+
"webm",
|
|
63
|
+
"mov",
|
|
64
|
+
"avi",
|
|
65
|
+
"mpg",
|
|
66
|
+
"mp3",
|
|
67
|
+
"m4a",
|
|
68
|
+
"ogg",
|
|
69
|
+
"opus",
|
|
70
|
+
"flac",
|
|
71
|
+
"wav",
|
|
72
|
+
"amr",
|
|
73
|
+
"pdf",
|
|
74
|
+
"epub",
|
|
75
|
+
"swf",
|
|
76
|
+
"rtf",
|
|
77
|
+
"woff",
|
|
78
|
+
"woff2",
|
|
79
|
+
"eot",
|
|
80
|
+
"ttf",
|
|
81
|
+
"otf",
|
|
82
|
+
"ico",
|
|
83
|
+
"flv",
|
|
84
|
+
"ps",
|
|
85
|
+
"xz",
|
|
86
|
+
"sqlite",
|
|
87
|
+
"xpi",
|
|
88
|
+
"cab",
|
|
89
|
+
"deb",
|
|
90
|
+
"ar",
|
|
91
|
+
"rpm",
|
|
92
|
+
"Z",
|
|
93
|
+
"lz",
|
|
94
|
+
"msi",
|
|
95
|
+
"mxf",
|
|
96
|
+
"mts",
|
|
97
|
+
"wasm",
|
|
98
|
+
"webassembly",
|
|
99
|
+
"blend",
|
|
100
|
+
"bpg",
|
|
101
|
+
"docx",
|
|
102
|
+
"pptx",
|
|
103
|
+
"xlsx"
|
|
104
|
+
],
|
|
105
|
+
"devDependencies": {
|
|
106
|
+
"ava": "*",
|
|
107
|
+
"read-chunk": "^2.0.0",
|
|
108
|
+
"xo": "*"
|
|
109
|
+
}
|
|
108
110
|
}
|
package/readme.md
CHANGED
|
@@ -8,7 +8,7 @@ The file type is detected by checking the [magic number](http://en.wikipedia.org
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
10
10
|
```
|
|
11
|
-
$ npm install
|
|
11
|
+
$ npm install file-type
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
|
|
@@ -139,6 +139,10 @@ It only needs the first 4100 bytes.
|
|
|
139
139
|
- [`mts`](https://en.wikipedia.org/wiki/.m2ts)
|
|
140
140
|
- [`wasm`](https://en.wikipedia.org/wiki/WebAssembly)
|
|
141
141
|
- [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format)
|
|
142
|
+
- [`bpg`](https://bellard.org/bpg/)
|
|
143
|
+
- [`docx`](https://en.wikipedia.org/wiki/Office_Open_XML)
|
|
144
|
+
- [`pptx`](https://en.wikipedia.org/wiki/Office_Open_XML)
|
|
145
|
+
- [`xlsx`](https://en.wikipedia.org/wiki/Office_Open_XML)
|
|
142
146
|
|
|
143
147
|
*SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*
|
|
144
148
|
|
|
@@ -150,6 +154,12 @@ It only needs the first 4100 bytes.
|
|
|
150
154
|
- [file-type-cli](https://github.com/sindresorhus/file-type-cli) - CLI for this module
|
|
151
155
|
|
|
152
156
|
|
|
157
|
+
## Created by
|
|
158
|
+
|
|
159
|
+
- [Sindre Sorhus](https://github.com/sindresorhus)
|
|
160
|
+
- [Mikael Finstad](https://github.com/mifi)
|
|
161
|
+
|
|
162
|
+
|
|
153
163
|
## License
|
|
154
164
|
|
|
155
|
-
MIT
|
|
165
|
+
MIT
|