file-type 10.1.0 → 10.5.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 +29 -0
- package/package.json +1 -1
- package/readme.md +16 -5
package/index.js
CHANGED
|
@@ -16,6 +16,10 @@ function readUInt64LE(buf, offset = 0) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
module.exports = input => {
|
|
19
|
+
if (!(input instanceof Uint8Array || Buffer.isBuffer(input))) {
|
|
20
|
+
throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\`, got \`${typeof input}\``);
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
const buf = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
20
24
|
|
|
21
25
|
if (!(buf && buf.length > 1)) {
|
|
@@ -867,5 +871,30 @@ module.exports = input => {
|
|
|
867
871
|
};
|
|
868
872
|
}
|
|
869
873
|
|
|
874
|
+
if (check([0x44, 0x49, 0x43, 0x4D], {offset: 128})) {
|
|
875
|
+
return {
|
|
876
|
+
ext: 'dcm',
|
|
877
|
+
mime: 'application/dicom'
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// Musepack, SV7
|
|
882
|
+
if (check([0x4D, 0x50, 0x2B])) {
|
|
883
|
+
return {
|
|
884
|
+
ext: 'mpc',
|
|
885
|
+
mime: 'audio/x-musepack'
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// Musepack, SV8
|
|
890
|
+
if (check([0x4D, 0x50, 0x43, 0x4B])) {
|
|
891
|
+
return {
|
|
892
|
+
ext: 'mpc',
|
|
893
|
+
mime: 'audio/x-musepack'
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
|
|
870
897
|
return null;
|
|
871
898
|
};
|
|
899
|
+
|
|
900
|
+
Object.defineProperty(module.exports, 'minimumBytes', {value: 4100});
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -23,7 +23,8 @@ $ npm install file-type
|
|
|
23
23
|
```js
|
|
24
24
|
const readChunk = require('read-chunk');
|
|
25
25
|
const fileType = require('file-type');
|
|
26
|
-
|
|
26
|
+
|
|
27
|
+
const buffer = readChunk.sync('unicorn.png', 0, fileType.minimumBytes);
|
|
27
28
|
|
|
28
29
|
fileType(buffer);
|
|
29
30
|
//=> {ext: 'png', mime: 'image/png'}
|
|
@@ -34,11 +35,13 @@ Or from a remote location:
|
|
|
34
35
|
```js
|
|
35
36
|
const http = require('http');
|
|
36
37
|
const fileType = require('file-type');
|
|
38
|
+
|
|
37
39
|
const url = 'http://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
|
|
38
40
|
|
|
39
|
-
http.get(url,
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
http.get(url, response => {
|
|
42
|
+
response.on('readable', () => {
|
|
43
|
+
const chunk = response.read(fileType.minimumBytes);
|
|
44
|
+
response.destroy();
|
|
42
45
|
console.log(fileType(chunk));
|
|
43
46
|
//=> {ext: 'gif', mime: 'image/gif'}
|
|
44
47
|
});
|
|
@@ -76,7 +79,13 @@ Or `null` when no match.
|
|
|
76
79
|
|
|
77
80
|
Type: `Buffer` `Uint8Array`
|
|
78
81
|
|
|
79
|
-
It only needs the first
|
|
82
|
+
It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file.
|
|
83
|
+
|
|
84
|
+
### fileType.minimumBytes
|
|
85
|
+
|
|
86
|
+
Type: `number`
|
|
87
|
+
|
|
88
|
+
The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.
|
|
80
89
|
|
|
81
90
|
|
|
82
91
|
## Supported file types
|
|
@@ -168,6 +177,8 @@ It only needs the first 4100 bytes. The exception is detection of `docx`, `pptx`
|
|
|
168
177
|
- [`asf`](https://en.wikipedia.org/wiki/Advanced_Systems_Format) - Advanced Systems Format
|
|
169
178
|
- [`wma`](https://en.wikipedia.org/wiki/Windows_Media_Audio) - Windows Media Audio
|
|
170
179
|
- [`wmv`](https://en.wikipedia.org/wiki/Windows_Media_Video) - Windows Media Video
|
|
180
|
+
- [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM Image File
|
|
181
|
+
- [`mpc`](https://en.wikipedia.org/wiki/Musepack) - Musepack (SV7 & SV8)
|
|
171
182
|
|
|
172
183
|
*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).*
|
|
173
184
|
|