file-type 16.5.4 → 21.3.4
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/core.d.ts +221 -354
- package/core.js +2599 -1165
- package/index.d.ts +89 -18
- package/index.js +154 -23
- package/package.json +96 -21
- package/readme.md +467 -270
- package/supported.js +359 -278
- package/util.js +36 -16
- package/browser.d.ts +0 -50
- package/browser.js +0 -49
package/util.js
CHANGED
|
@@ -1,40 +1,60 @@
|
|
|
1
|
-
|
|
1
|
+
import {StringType} from 'token-types';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export function stringToBytes(string, encoding) {
|
|
4
|
+
if (encoding === 'utf-16le') {
|
|
5
|
+
const bytes = [];
|
|
6
|
+
for (let index = 0; index < string.length; index++) {
|
|
7
|
+
const code = string.charCodeAt(index); // eslint-disable-line unicorn/prefer-code-point
|
|
8
|
+
bytes.push(code & 0xFF, (code >> 8) & 0xFF); // High byte
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return bytes;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (encoding === 'utf-16be') {
|
|
15
|
+
const bytes = [];
|
|
16
|
+
for (let index = 0; index < string.length; index++) {
|
|
17
|
+
const code = string.charCodeAt(index); // eslint-disable-line unicorn/prefer-code-point
|
|
18
|
+
bytes.push((code >> 8) & 0xFF, code & 0xFF); // Low byte
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return bytes;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return [...string].map(character => character.charCodeAt(0)); // eslint-disable-line unicorn/prefer-code-point
|
|
25
|
+
}
|
|
4
26
|
|
|
5
27
|
/**
|
|
6
28
|
Checks whether the TAR checksum is valid.
|
|
7
29
|
|
|
8
|
-
@param {
|
|
30
|
+
@param {Uint8Array} arrayBuffer - The TAR header `[offset ... offset + 512]`.
|
|
9
31
|
@param {number} offset - TAR header offset.
|
|
10
32
|
@returns {boolean} `true` if the TAR checksum is valid, otherwise `false`.
|
|
11
33
|
*/
|
|
12
|
-
|
|
13
|
-
const readSum = parseInt(
|
|
14
|
-
if (isNaN(readSum)) {
|
|
34
|
+
export function tarHeaderChecksumMatches(arrayBuffer, offset = 0) {
|
|
35
|
+
const readSum = Number.parseInt(new StringType(6).get(arrayBuffer, 148).replace(/\0.*$/, '').trim(), 8); // Read sum in header
|
|
36
|
+
if (Number.isNaN(readSum)) {
|
|
15
37
|
return false;
|
|
16
38
|
}
|
|
17
39
|
|
|
18
40
|
let sum = 8 * 0x20; // Initialize signed bit sum
|
|
19
41
|
|
|
20
|
-
for (let
|
|
21
|
-
sum +=
|
|
42
|
+
for (let index = offset; index < offset + 148; index++) {
|
|
43
|
+
sum += arrayBuffer[index];
|
|
22
44
|
}
|
|
23
45
|
|
|
24
|
-
for (let
|
|
25
|
-
sum +=
|
|
46
|
+
for (let index = offset + 156; index < offset + 512; index++) {
|
|
47
|
+
sum += arrayBuffer[index];
|
|
26
48
|
}
|
|
27
49
|
|
|
28
50
|
return readSum === sum;
|
|
29
|
-
}
|
|
51
|
+
}
|
|
30
52
|
|
|
31
53
|
/**
|
|
32
54
|
ID3 UINT32 sync-safe tokenizer token.
|
|
33
55
|
28 bits (representing up to 256MB) integer, the msb is 0 to avoid "false syncsignals".
|
|
34
56
|
*/
|
|
35
|
-
|
|
36
|
-
get: (buffer, offset) =>
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
len: 4
|
|
57
|
+
export const uint32SyncSafeToken = {
|
|
58
|
+
get: (buffer, offset) => (buffer[offset + 3] & 0x7F) | ((buffer[offset + 2]) << 7) | ((buffer[offset + 1]) << 14) | ((buffer[offset]) << 21),
|
|
59
|
+
len: 4,
|
|
40
60
|
};
|
package/browser.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/// <reference lib="dom"/>
|
|
2
|
-
import * as core from './core';
|
|
3
|
-
|
|
4
|
-
export type FileTypeResult = core.FileTypeResult;
|
|
5
|
-
export type FileExtension = core.FileExtension;
|
|
6
|
-
export type MimeType = core.MimeType;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
Determine file type from a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
import FileType = require('file-type/browser');
|
|
13
|
-
|
|
14
|
-
const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
|
|
15
|
-
|
|
16
|
-
(async () => {
|
|
17
|
-
const response = await fetch(url);
|
|
18
|
-
const fileType = await FileType.fromStream(response.body);
|
|
19
|
-
|
|
20
|
-
console.log(fileType);
|
|
21
|
-
//=> {ext: 'jpg', mime: 'image/jpeg'}
|
|
22
|
-
})();
|
|
23
|
-
```
|
|
24
|
-
*/
|
|
25
|
-
export declare function fromStream(stream: ReadableStream): Promise<core.FileTypeResult | undefined>;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
Determine file type from a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
29
|
-
|
|
30
|
-
```
|
|
31
|
-
import FileType = require('file-type/browser');
|
|
32
|
-
|
|
33
|
-
(async () => {
|
|
34
|
-
const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
|
|
35
|
-
type: 'plain/text',
|
|
36
|
-
endings: 'native'
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
console.log(await FileType.fromBlob(blob));
|
|
40
|
-
//=> {ext: 'txt', mime: 'plain/text'}
|
|
41
|
-
})();
|
|
42
|
-
```
|
|
43
|
-
*/
|
|
44
|
-
export declare function fromBlob(blob: Blob): Promise<core.FileTypeResult | undefined>;
|
|
45
|
-
|
|
46
|
-
export {
|
|
47
|
-
fromBuffer,
|
|
48
|
-
extensions,
|
|
49
|
-
mimeTypes
|
|
50
|
-
} from './core';
|
package/browser.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const {ReadableWebToNodeStream} = require('readable-web-to-node-stream');
|
|
3
|
-
const core = require('./core');
|
|
4
|
-
|
|
5
|
-
async function fromStream(stream) {
|
|
6
|
-
const readableWebToNodeStream = new ReadableWebToNodeStream(stream);
|
|
7
|
-
const fileType = await core.fromStream(readableWebToNodeStream);
|
|
8
|
-
await readableWebToNodeStream.close();
|
|
9
|
-
return fileType;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async function fromBlob(blob) {
|
|
13
|
-
const buffer = await blobToArrayBuffer(blob);
|
|
14
|
-
return core.fromBuffer(Buffer.from(buffer));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
Convert Blobs to ArrayBuffer.
|
|
19
|
-
@param {Blob} blob - Web API Blob.
|
|
20
|
-
@returns {Promise<ArrayBuffer>}
|
|
21
|
-
*/
|
|
22
|
-
function blobToArrayBuffer(blob) {
|
|
23
|
-
if (blob.arrayBuffer) {
|
|
24
|
-
return blob.arrayBuffer();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// TODO: Remove when stop supporting older environments
|
|
28
|
-
return new Promise((resolve, reject) => {
|
|
29
|
-
const fileReader = new FileReader();
|
|
30
|
-
fileReader.addEventListener('loadend', event => {
|
|
31
|
-
resolve(event.target.result);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
fileReader.addEventListener('error', event => {
|
|
35
|
-
reject(new Error(event.message));
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
fileReader.addEventListener('abort', event => {
|
|
39
|
-
reject(new Error(event.type));
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
fileReader.readAsArrayBuffer(blob);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
Object.assign(module.exports, core, {
|
|
47
|
-
fromStream,
|
|
48
|
-
fromBlob
|
|
49
|
-
});
|