Update Bot
This commit is contained in:
21
node_modules/magic-bytes.js/LICENSE
generated
vendored
Normal file
21
node_modules/magic-bytes.js/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Lars Kölpin
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
110
node_modules/magic-bytes.js/README.md
generated
vendored
Normal file
110
node_modules/magic-bytes.js/README.md
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
# Magic bytes
|
||||
|
||||
[](https://github.com/LarsKoelpin/magic-bytes/actions/workflows/build-and-test.yml)
|
||||
|
||||
Magic Bytes is a javascript library analyzing the first bytes of a file to tell you its type.
|
||||
Use it inside your browser or serversided using nodejs.
|
||||
|
||||
The procedure is based on https://en.wikipedia.org/wiki/List_of_file_signatures.
|
||||
|
||||
|
||||
> [!NOTE]
|
||||
> A small note on versioning.
|
||||
> Strictly speaking, each new filetype which is supported by this library can break someones' API.
|
||||
> Please note that this library adds new filetypes with minor release.
|
||||
> This means files, which validate to "null" in some versions, may find a result in a new version.
|
||||
>
|
||||
> Or in some cases the library will find more results, than before. So don't depend on the found-array size in
|
||||
> any shape or form.
|
||||
> Filetypes will not be removed though.
|
||||
|
||||
# Installation
|
||||
Run `npm install magic-bytes.js`
|
||||
|
||||
|
||||
# Interactive example
|
||||
There is an interactive example present at https://larskoelpin.github.io/magic-bytes/.
|
||||
|
||||
# Usage
|
||||
|
||||
The following functions are available:
|
||||
* `filetypeinfo(bytes: number[])` Contains typeinformation like name, extension and mime type: `[{typename: "zip"}, {typename: "jar"}]`
|
||||
* `filetypename(bytes: number[])` : Contains type names only: `["zip", "jar"]`
|
||||
* `filetypemime(bytes: number[])` : Contains type mime types only: `["application/zip", "application/jar"]`
|
||||
* `filetypeextension(bytes: number[])` : Contains type extensions only: `["zip", "jar"]`
|
||||
* `register(fileType: string, string[])`: registers a custom signature
|
||||
|
||||
Both function return an empty array `[]` otherwise, which means it could not detect the file signature. Keep in mind that
|
||||
txt files for example fall in this category.
|
||||
|
||||
You don't have to load the whole file in memory. For validating a file uploaded to S3 using Lambda for example, it may be
|
||||
enough to load the files first 100 bytes and validate against them. This is especially useful for big files.
|
||||
|
||||
see examples for practical usage.
|
||||
|
||||
On server:
|
||||
```javascript
|
||||
import filetype from 'magic-bytes.js'
|
||||
|
||||
filetype(fs.readFileSync("myimage.png")) // ["png"]
|
||||
```
|
||||
|
||||
|
||||
To run an HTML-Example checkout the project and run
|
||||
|
||||
```
|
||||
npm install; npm run example
|
||||
```
|
||||
|
||||
This opens an HTML example using magic bytes as a window variable. It kinda looks like that.
|
||||
|
||||
```html
|
||||
<input type="file" id="file" />
|
||||
|
||||
<script src="node_modules/magic-bytes.js/dist/browser.js" type="application/javascript"></script>
|
||||
<script>
|
||||
document.getElementById("file").addEventListener('change', (event, x) => {
|
||||
const fileReader = new FileReader();
|
||||
fileReader.onloadend = (f) => {
|
||||
const bytes = new Uint8Array(f.target.result);
|
||||
console.log("Possible filetypes: " + filetypeinfo(bytes))
|
||||
}
|
||||
fileReader.readAsArrayBuffer(event.target.files[0])
|
||||
})
|
||||
</script>
|
||||
```
|
||||
|
||||
|
||||
# Tests
|
||||
Run `npm test`
|
||||
|
||||
# Example
|
||||
See examples/
|
||||
|
||||
# How does it work
|
||||
The `create-snapshot.js` creates a new tree. The tree has a similar shape to the following
|
||||
```json
|
||||
{
|
||||
"0x47": {
|
||||
"0x49": {
|
||||
"0x46": {
|
||||
"0x38": {
|
||||
"0x37": {
|
||||
"0x61": {
|
||||
"matches": [
|
||||
{
|
||||
"typename": "gif",
|
||||
"mime": "image/gif",
|
||||
"extension": "gif"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
It acts as a giant lookup map for the given byte signatures.
|
||||
8
node_modules/magic-bytes.js/dist/index.d.ts
generated
vendored
Normal file
8
node_modules/magic-bytes.js/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import { GuessedFile, Info } from "./model/tree";
|
||||
export declare const filetypeinfo: (bytes: number[] | Uint8Array | Uint8ClampedArray) => GuessedFile[];
|
||||
export default filetypeinfo;
|
||||
export declare const filetypename: (bytes: number[] | Uint8Array | Uint8ClampedArray) => string[];
|
||||
export declare const filetypemime: (bytes: number[] | Uint8Array | Uint8ClampedArray) => string[];
|
||||
export declare const filetypeextension: (bytes: number[] | Uint8Array | Uint8ClampedArray) => string[];
|
||||
export declare const register: (typename: string, signature: string[], additionalInfo?: Info | undefined, offset?: number | undefined) => void;
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
1
node_modules/magic-bytes.js/dist/index.d.ts.map
generated
vendored
Normal file
1
node_modules/magic-bytes.js/dist/index.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAc,IAAI,EAAE,MAAM,cAAc,CAAC;AAI7D,eAAO,MAAM,YAAY,UAChB,MAAM,EAAE,GAAG,UAAU,GAAG,iBAAiB,KAC/C,WAAW,EAkBb,CAAC;AA0BF,eAAe,YAAY,CAAC;AAE5B,eAAO,MAAM,YAAY,UAChB,MAAM,EAAE,GAAG,UAAU,GAAG,iBAAiB,KAC/C,MAAM,EAAgD,CAAC;AAE1D,eAAO,MAAM,YAAY,UAChB,MAAM,EAAE,GAAG,UAAU,GAAG,iBAAiB,KAC/C,MAAM,EAGiC,CAAC;AAE3C,eAAO,MAAM,iBAAiB,UACrB,MAAM,EAAE,GAAG,UAAU,GAAG,iBAAiB,KAC/C,MAAM,EAGiC,CAAC;AAE3C,eAAO,MAAM,QAAQ,aACT,MAAM,aACL,MAAM,EAAE,mBACF,IAAI,GAAG,SAAS,sCAIlC,CAAA"}
|
||||
61
node_modules/magic-bytes.js/dist/index.js
generated
vendored
Normal file
61
node_modules/magic-bytes.js/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.register = exports.filetypeextension = exports.filetypemime = exports.filetypename = exports.filetypeinfo = void 0;
|
||||
const pattern_tree_1 = require("./model/pattern-tree");
|
||||
const toHex_1 = require("./model/toHex");
|
||||
const patternTree = pattern_tree_1.createTree();
|
||||
const filetypeinfo = (bytes) => {
|
||||
let tree = patternTree;
|
||||
for (const k of Object.keys(tree.offset)) {
|
||||
const offset = toHex_1.fromHex(k);
|
||||
const offsetExceedsFile = offset >= bytes.length;
|
||||
if (offsetExceedsFile) {
|
||||
continue;
|
||||
}
|
||||
const node = patternTree.offset[k];
|
||||
const guessed = walkTree(offset, bytes, node);
|
||||
if (guessed.length > 0) {
|
||||
return guessed;
|
||||
}
|
||||
}
|
||||
if (tree.noOffset === null) {
|
||||
return [];
|
||||
}
|
||||
return walkTree(0, bytes, tree.noOffset);
|
||||
};
|
||||
exports.filetypeinfo = filetypeinfo;
|
||||
const walkTree = (index, bytes, node) => {
|
||||
let step = node;
|
||||
let guessFile = [];
|
||||
while (true) {
|
||||
const currentByte = toHex_1.toHex(bytes[index]);
|
||||
if (step.bytes["?"] && !step.bytes[currentByte]) {
|
||||
step = step.bytes["?"];
|
||||
}
|
||||
else {
|
||||
step = step.bytes[currentByte];
|
||||
}
|
||||
if (!step) {
|
||||
return guessFile;
|
||||
}
|
||||
if (step && step.matches) {
|
||||
guessFile = step.matches.slice(0);
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
};
|
||||
exports.default = exports.filetypeinfo;
|
||||
const filetypename = (bytes) => exports.filetypeinfo(bytes).map((e) => e.typename);
|
||||
exports.filetypename = filetypename;
|
||||
const filetypemime = (bytes) => exports.filetypeinfo(bytes)
|
||||
.map((e) => (e.mime ? e.mime : null))
|
||||
.filter((x) => x !== null);
|
||||
exports.filetypemime = filetypemime;
|
||||
const filetypeextension = (bytes) => exports.filetypeinfo(bytes)
|
||||
.map((e) => (e.extension ? e.extension : null))
|
||||
.filter((x) => x !== null);
|
||||
exports.filetypeextension = filetypeextension;
|
||||
const register = (typename, signature, additionalInfo, offset) => {
|
||||
pattern_tree_1.add(typename, signature, additionalInfo, offset);
|
||||
};
|
||||
exports.register = register;
|
||||
2
node_modules/magic-bytes.js/dist/index.spec.d.ts
generated
vendored
Normal file
2
node_modules/magic-bytes.js/dist/index.spec.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=index.spec.d.ts.map
|
||||
1
node_modules/magic-bytes.js/dist/index.spec.d.ts.map
generated
vendored
Normal file
1
node_modules/magic-bytes.js/dist/index.spec.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.spec.d.ts","sourceRoot":"","sources":["../src/index.spec.ts"],"names":[],"mappings":""}
|
||||
304
node_modules/magic-bytes.js/dist/index.spec.js
generated
vendored
Normal file
304
node_modules/magic-bytes.js/dist/index.spec.js
generated
vendored
Normal file
@@ -0,0 +1,304 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs = __importStar(require("fs"));
|
||||
const index_1 = require("./index");
|
||||
const getBytes = (filename) => {
|
||||
const file = require.resolve(`./testfiles/${filename}`);
|
||||
const buffer = fs.readFileSync(file);
|
||||
return Array.prototype.slice.call(buffer, 0);
|
||||
};
|
||||
describe("Tests the public API", () => {
|
||||
it("detects woff", () => {
|
||||
const bytes = getBytes("font.woff");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result).toStrictEqual({
|
||||
typename: "woff",
|
||||
mime: "font/woff",
|
||||
extension: "woff",
|
||||
});
|
||||
});
|
||||
it("detects woff2", () => {
|
||||
const bytes = getBytes("inter.woff2");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result).toStrictEqual({
|
||||
typename: "woff2",
|
||||
mime: "font/woff2",
|
||||
extension: "woff2",
|
||||
});
|
||||
});
|
||||
it("detects tar with offset", () => {
|
||||
const bytes = getBytes("a.tar");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("tar");
|
||||
});
|
||||
it("detects apng", () => {
|
||||
const bytes = getBytes("a.apng");
|
||||
const result = index_1.filetypeinfo(bytes);
|
||||
expect(result).toHaveLength(2);
|
||||
const [png, apng] = result;
|
||||
expect(png.typename).toBe("png");
|
||||
expect(png.mime).toBe("image/png");
|
||||
expect(apng.typename).toBe("apng");
|
||||
expect(apng.mime).toBe("image/apng");
|
||||
});
|
||||
it("detects mp4", () => {
|
||||
const bytes = getBytes("a.mp4");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("mp4");
|
||||
expect(result.mime).toBe("video/mp4");
|
||||
});
|
||||
describe("detects ogg containers", () => {
|
||||
it("detects ogv", () => {
|
||||
const bytes = getBytes("a.ogv");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("ogv");
|
||||
expect(result.mime).toBe("video/ogg");
|
||||
});
|
||||
it("detects ogm", () => {
|
||||
const bytes = getBytes("a.ogm");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("ogm");
|
||||
expect(result.mime).toBe("video/ogg");
|
||||
});
|
||||
it("detects oga", () => {
|
||||
const bytes = getBytes("a.oga");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("oga");
|
||||
expect(result.mime).toBe("audio/ogg");
|
||||
});
|
||||
it("detects spx", () => {
|
||||
const bytes = getBytes("a.spx");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("spx");
|
||||
expect(result.mime).toBe("audio/ogg");
|
||||
});
|
||||
it("detects ogg", () => {
|
||||
const bytes = getBytes("a.ogg");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("ogg");
|
||||
expect(result.mime).toBe("audio/ogg");
|
||||
});
|
||||
it("detects ogx", () => {
|
||||
const bytes = getBytes("a.ogx");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("ogx");
|
||||
expect(result.mime).toBe("application/ogg");
|
||||
});
|
||||
});
|
||||
describe("detects mov", () => {
|
||||
it("detects mov (moov)", () => {
|
||||
const bytes = getBytes("a.moov.mov");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("mov");
|
||||
expect(result.extension).toBe("mov");
|
||||
expect(result.mime).toBe("video/quicktime");
|
||||
});
|
||||
it("detects mov (mdat)", () => {
|
||||
const bytes = getBytes("a.mdat.mov");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("mov");
|
||||
expect(result.extension).toBe("mov");
|
||||
expect(result.mime).toBe("video/quicktime");
|
||||
});
|
||||
it("detects mov (ftypqt)", () => {
|
||||
const bytes = getBytes("a.ftypqt.mov");
|
||||
const [result] = index_1.filetypeinfo(bytes);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.typename).toBe("mov");
|
||||
expect(result.extension).toBe("mov");
|
||||
expect(result.mime).toBe("video/quicktime");
|
||||
});
|
||||
});
|
||||
it("filetypeinfo", () => {
|
||||
const bytes = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
||||
const result = index_1.filetypeinfo(bytes);
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0]).toHaveProperty("typename");
|
||||
});
|
||||
it("filetypename", () => {
|
||||
const bytes = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
||||
const result = index_1.filetypename(bytes);
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result).toEqual(["png", "apng"]);
|
||||
});
|
||||
it("filetypename failure", () => {
|
||||
const bytes = [0x89, 0x00, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
||||
const result = index_1.filetypename(bytes);
|
||||
expect(result).toHaveLength(0);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
it("filetypemime", () => {
|
||||
const bytes = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
||||
const result = index_1.filetypemime(bytes);
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result).toEqual(["image/png", "image/apng"]);
|
||||
});
|
||||
it("filetypemime not found", () => {
|
||||
const bytes = [0x89, 0x50, 0x00, 0x47, 0x00, 0x0a, 0x1a, 0x0a];
|
||||
const result = index_1.filetypemime(bytes);
|
||||
expect(result).toHaveLength(0);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
it("filetypeextension", () => {
|
||||
const bytes = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
||||
const result = index_1.filetypeextension(bytes);
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result).toEqual(["png", "apng"]);
|
||||
});
|
||||
it("filetypeextension not found", () => {
|
||||
const bytes = [0x89, 0x50, 0x4e, 0x47, 0x00, 0x0a, 0x1a, 0x0a];
|
||||
const result = index_1.filetypeextension(bytes);
|
||||
expect(result).toHaveLength(0);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
it("detects utf8", () => {
|
||||
const file = getBytes("a.utf8");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("text/plain; charset=UTF-8");
|
||||
});
|
||||
it("detects utf16le", () => {
|
||||
const file = getBytes("a.utf16le");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("text/plain; charset=UTF-16LE");
|
||||
});
|
||||
it("detects utf16be", () => {
|
||||
const file = getBytes("a.utf16be");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("text/plain; charset=UTF-16BE");
|
||||
});
|
||||
it("detects json object", () => {
|
||||
const fileObj = getBytes("a.json");
|
||||
const fileArray = getBytes("a_array.json");
|
||||
const result = index_1.filetypemime(fileObj);
|
||||
const result2 = index_1.filetypemime(fileArray);
|
||||
expect(result).toContain("application/json");
|
||||
expect(result2).toContain("application/json");
|
||||
});
|
||||
it("detects srt", () => {
|
||||
const file = getBytes("a.srt");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("application/x-subrip");
|
||||
});
|
||||
it("detects vtt", () => {
|
||||
const file = getBytes("a.vtt");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("text/vtt");
|
||||
});
|
||||
it("detects jpeg (photoshop)", () => {
|
||||
// File created with Adobe Photoshop 2024 via "Save As" menu
|
||||
const file = getBytes("photoshop.jpg");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("image/jpeg");
|
||||
});
|
||||
it("detects jpeg (photoshop export)", () => {
|
||||
// File created with Adobe Photoshop 2024 via "Export As" menu
|
||||
const file = getBytes("photoshop-export.jpg");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("image/jpeg");
|
||||
});
|
||||
it("detects jpeg (png2jpg)", () => {
|
||||
// File created using https://png2jpg.com
|
||||
const file = getBytes("png2jpg.jpg");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("image/jpeg");
|
||||
});
|
||||
describe("add new custom types", () => {
|
||||
beforeAll(() => {
|
||||
index_1.register('customNoInfo', ["0xde", "0xad", "0xbe", "0xef"]);
|
||||
index_1.register('customMime', ["0x12", "0x34", "0x56", "0x78"], {
|
||||
mime: 'application/vnd-custom',
|
||||
extension: '.cust'
|
||||
});
|
||||
index_1.register('customOffset', ["0xab", "0xcb"], {
|
||||
mime: 'application/vnd-custom-offset',
|
||||
extension: '.custoff'
|
||||
}, 2);
|
||||
});
|
||||
it("detects customNoInfo file", () => {
|
||||
const bytes = [0xde, 0xad, 0xbe, 0xef, 0x00];
|
||||
const result = index_1.filetypeinfo(bytes);
|
||||
expect(result).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
"typename": "customNoInfo",
|
||||
})
|
||||
]));
|
||||
});
|
||||
it("detects customMime file", () => {
|
||||
const bytes = [0x12, 0x34, 0x56, 0x78];
|
||||
const result = index_1.filetypeinfo(bytes);
|
||||
expect(result).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
"typename": "customMime",
|
||||
"mime": "application/vnd-custom",
|
||||
"extension": ".cust"
|
||||
})
|
||||
]));
|
||||
});
|
||||
it("detects customOffset file", () => {
|
||||
const bytes = [0x12, 0x34, 0xab, 0xcb];
|
||||
const result = index_1.filetypeinfo(bytes);
|
||||
expect(result).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
"typename": "customOffset",
|
||||
"mime": "application/vnd-custom-offset",
|
||||
"extension": ".custoff"
|
||||
})
|
||||
]));
|
||||
});
|
||||
});
|
||||
it("detects pdf (Libreoffice export)", () => {
|
||||
// File created using libreoffice writter export to pdf
|
||||
const file = getBytes("a.pdf");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("application/pdf");
|
||||
});
|
||||
it("detects poscript (pdf2ps)", () => {
|
||||
// File created using pdf2ps from https://www.ghostscript.com
|
||||
const file = getBytes("a.ps");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("application/postscript");
|
||||
});
|
||||
it("detects svg", () => {
|
||||
// File created using https://png2jpg.com
|
||||
const file = getBytes("a.svg");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("image/svg+xml");
|
||||
});
|
||||
it("detects avif", () => {
|
||||
// File created using avifenc on a.apng
|
||||
const file = getBytes("a.avif");
|
||||
const result = index_1.filetypemime(file);
|
||||
expect(result).toContain("image/avif");
|
||||
});
|
||||
});
|
||||
8
node_modules/magic-bytes.js/dist/model/pattern-tree.d.ts
generated
vendored
Normal file
8
node_modules/magic-bytes.js/dist/model/pattern-tree.d.ts
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Info, Tree } from "./tree";
|
||||
declare type TypeName = string;
|
||||
declare type Signature = string[];
|
||||
export declare const add: (typename: TypeName, signature: Signature, additionalInfo?: Info | undefined, offset?: number | undefined) => void;
|
||||
export declare const createTree: () => Tree;
|
||||
declare const _default: () => Tree;
|
||||
export default _default;
|
||||
//# sourceMappingURL=pattern-tree.d.ts.map
|
||||
1
node_modules/magic-bytes.js/dist/model/pattern-tree.d.ts.map
generated
vendored
Normal file
1
node_modules/magic-bytes.js/dist/model/pattern-tree.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"pattern-tree.d.ts","sourceRoot":"","sources":["../../src/model/pattern-tree.ts"],"names":[],"mappings":"AACA,OAAO,EAAiC,IAAI,EAAS,IAAI,EAAE,MAAM,QAAQ,CAAC;AAQ1E,aAAK,QAAQ,GAAG,MAAM,CAAC;AACvB,aAAK,SAAS,GAAG,MAAM,EAAE,CAAC;AAE1B,eAAO,MAAM,GAAG,aACJ,QAAQ,yCAED,IAAI,GAAG,SAAS,sCAwClC,CAAC;AA2xCF,eAAO,MAAM,UAAU,QAAO,IAAoB,CAAC;8BAChC,IAAI;AAAvB,wBAAwC"}
|
||||
1163
node_modules/magic-bytes.js/dist/model/pattern-tree.js
generated
vendored
Normal file
1163
node_modules/magic-bytes.js/dist/model/pattern-tree.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
3
node_modules/magic-bytes.js/dist/model/toHex.d.ts
generated
vendored
Normal file
3
node_modules/magic-bytes.js/dist/model/toHex.d.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export declare const toHex: (num: number) => string;
|
||||
export declare const fromHex: (hex: string) => number;
|
||||
//# sourceMappingURL=toHex.d.ts.map
|
||||
1
node_modules/magic-bytes.js/dist/model/toHex.d.ts.map
generated
vendored
Normal file
1
node_modules/magic-bytes.js/dist/model/toHex.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"toHex.d.ts","sourceRoot":"","sources":["../../src/model/toHex.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,KAAK,QAAS,MAAM,KAAG,MACsB,CAAC;AAC3D,eAAO,MAAM,OAAO,QAAS,MAAM,WAA8B,CAAC"}
|
||||
8
node_modules/magic-bytes.js/dist/model/toHex.js
generated
vendored
Normal file
8
node_modules/magic-bytes.js/dist/model/toHex.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.fromHex = exports.toHex = void 0;
|
||||
const hex = (num) => new Number(num).toString(16).toLowerCase();
|
||||
const toHex = (num) => `0x${hex(num).length === 1 ? "0" + hex(num) : hex(num)}`;
|
||||
exports.toHex = toHex;
|
||||
const fromHex = (hex) => new Number(hex);
|
||||
exports.fromHex = fromHex;
|
||||
30
node_modules/magic-bytes.js/dist/model/tree.d.ts
generated
vendored
Normal file
30
node_modules/magic-bytes.js/dist/model/tree.d.ts
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
export declare type PathlessNewNode = {
|
||||
info: Info;
|
||||
typename: string;
|
||||
};
|
||||
export declare type NewNode = PathlessNewNode & {
|
||||
bytes: string[];
|
||||
};
|
||||
export declare type GuessedFile = Info & {
|
||||
typename: string;
|
||||
};
|
||||
export declare type Info = {
|
||||
mime?: string;
|
||||
extension?: string;
|
||||
};
|
||||
export declare type Node = {
|
||||
matches?: GuessedFile[];
|
||||
bytes: {
|
||||
[nextbyte: string]: Node;
|
||||
};
|
||||
};
|
||||
export declare type Tree = {
|
||||
noOffset: Node | null;
|
||||
offset: {
|
||||
[offsetByte: string]: Node;
|
||||
};
|
||||
};
|
||||
export declare const merge: (node: NewNode, tree: Node) => Node;
|
||||
export declare const createNode: (typename: string, bytes: string[], info?: Info | undefined) => NewNode;
|
||||
export declare const createComplexNode: (typename: string, bytes: string[], info?: Info | undefined) => Node;
|
||||
//# sourceMappingURL=tree.d.ts.map
|
||||
1
node_modules/magic-bytes.js/dist/model/tree.d.ts.map
generated
vendored
Normal file
1
node_modules/magic-bytes.js/dist/model/tree.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../src/model/tree.ts"],"names":[],"mappings":"AAAA,oBAAY,eAAe,GAAG;IAC5B,IAAI,EAAE,IAAI,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,oBAAY,OAAO,GAAG,eAAe,GAAG;IACtC,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF,oBAAY,WAAW,GAAG,IAAI,GAAG;IAC/B,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,oBAAY,IAAI,GAAG;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,oBAAY,IAAI,GAAG;IACjB,OAAO,CAAC,EAAE,WAAW,EAAE,CAAC;IACxB,KAAK,EAAE;QACL,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;KAC1B,CAAC;CACH,CAAC;AAEF,oBAAY,IAAI,GAAG;IACjB,QAAQ,EAAE,IAAI,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;CACxC,CAAC;AAYF,eAAO,MAAM,KAAK,SAAU,OAAO,QAAQ,IAAI,KAAG,IA4BjD,CAAC;AAEF,eAAO,MAAM,UAAU,aACX,MAAM,SACT,MAAM,EAAE,8BAEd,OAEF,CAAC;AAEF,eAAO,MAAM,iBAAiB,aAClB,MAAM,SACT,MAAM,EAAE,8BAEd,IAmBF,CAAC"}
|
||||
61
node_modules/magic-bytes.js/dist/model/tree.js
generated
vendored
Normal file
61
node_modules/magic-bytes.js/dist/model/tree.js
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createComplexNode = exports.createNode = exports.merge = void 0;
|
||||
const createMatch = (leaf) => ({
|
||||
typename: leaf.typename,
|
||||
mime: leaf.info.mime,
|
||||
extension: leaf.info.extension,
|
||||
});
|
||||
const isLeafNode = (tree, path) => tree && path.length === 0;
|
||||
const merge = (node, tree) => {
|
||||
if (node.bytes.length === 0)
|
||||
return tree;
|
||||
const [currentByte, ...path] = node.bytes;
|
||||
const currentTree = tree.bytes[currentByte];
|
||||
// traversed to end. Just add key to leaf.
|
||||
if (isLeafNode(currentTree, path)) {
|
||||
const matchingNode = tree.bytes[currentByte];
|
||||
tree.bytes[currentByte] = {
|
||||
...matchingNode,
|
||||
matches: [
|
||||
...(matchingNode.matches ?? []),
|
||||
createMatch(node),
|
||||
],
|
||||
};
|
||||
return tree;
|
||||
}
|
||||
// Path exists already, Merge subtree
|
||||
if (tree.bytes[currentByte]) {
|
||||
tree.bytes[currentByte] = exports.merge(exports.createNode(node.typename, path, node.info), tree.bytes[currentByte]);
|
||||
}
|
||||
else { // Tree did not exist before
|
||||
tree.bytes[currentByte] = exports.createComplexNode(node.typename, path, node.info);
|
||||
}
|
||||
return tree;
|
||||
};
|
||||
exports.merge = merge;
|
||||
const createNode = (typename, bytes, info) => {
|
||||
return { typename, bytes, info: info ? info : {} };
|
||||
};
|
||||
exports.createNode = createNode;
|
||||
const createComplexNode = (typename, bytes, info) => {
|
||||
let obj = {
|
||||
bytes: {},
|
||||
matches: undefined,
|
||||
};
|
||||
const [currentKey, ...path] = bytes;
|
||||
if (bytes.length === 0) {
|
||||
return {
|
||||
matches: [
|
||||
createMatch({
|
||||
typename: typename,
|
||||
info: info ? { extension: info.extension, mime: info.mime } : {},
|
||||
}),
|
||||
],
|
||||
bytes: {},
|
||||
};
|
||||
}
|
||||
obj.bytes[currentKey] = exports.createComplexNode(typename, path, info);
|
||||
return obj;
|
||||
};
|
||||
exports.createComplexNode = createComplexNode;
|
||||
2
node_modules/magic-bytes.js/dist/model/tree.spec.d.ts
generated
vendored
Normal file
2
node_modules/magic-bytes.js/dist/model/tree.spec.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=tree.spec.d.ts.map
|
||||
1
node_modules/magic-bytes.js/dist/model/tree.spec.d.ts.map
generated
vendored
Normal file
1
node_modules/magic-bytes.js/dist/model/tree.spec.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"tree.spec.d.ts","sourceRoot":"","sources":["../../src/model/tree.spec.ts"],"names":[],"mappings":""}
|
||||
34
node_modules/magic-bytes.js/dist/model/tree.spec.js
generated
vendored
Normal file
34
node_modules/magic-bytes.js/dist/model/tree.spec.js
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const tree_1 = require("./tree");
|
||||
describe("tree", () => {
|
||||
it("Creates complex node", () => {
|
||||
const tree = tree_1.createComplexNode("mpe", ["0x00", "0x01"]);
|
||||
expect(tree.bytes["0x00"].bytes["0x01"]).toHaveProperty("matches");
|
||||
expect(tree.bytes["0x00"].bytes["0x01"]["matches"][0].typename).toBe("mpe");
|
||||
});
|
||||
it("Merges trees", () => {
|
||||
const tree = tree_1.createComplexNode("pic", ["0x00"]);
|
||||
const dba = tree_1.createNode("dba", ["0x00", "0x01", "0x02", "0x03"]);
|
||||
const merged = tree_1.merge(dba, tree);
|
||||
expect(merged.bytes["0x00"].matches[0].typename).toBe("pic");
|
||||
expect(merged.bytes["0x00"].bytes["0x01"].bytes["0x02"].bytes["0x03"].matches[0]
|
||||
.typename).toBe("dba");
|
||||
});
|
||||
it("Merges overlapping", () => {
|
||||
const tree = tree_1.createComplexNode("pic", ["0x00"]);
|
||||
const dba = tree_1.createNode("pif", ["0x00"]);
|
||||
const merged = tree_1.merge(dba, tree);
|
||||
expect(merged.bytes["0x00"].matches).toHaveLength(2);
|
||||
});
|
||||
it("Merges deep overlapping", () => {
|
||||
const gifA = tree_1.createComplexNode("gif", ["0x47", "0x49", "0x46", "0x38", "0x37", "0x61"], { mime: "image/gif", extension: "gif" });
|
||||
const gifB = tree_1.createNode("gif", ["0x47", "0x49", "0x46", "0x38", "0x38", "0x61"], { mime: "image/gif", extension: "gif" });
|
||||
const gifC = tree_1.createNode("gif", ["0x47", "0x49", "0x46", "0x38", "0x39", "0x61"], { mime: "image/gif", extension: "gif" });
|
||||
const mergeA = tree_1.merge(gifB, gifA);
|
||||
const mergeB = tree_1.merge(gifC, mergeA);
|
||||
expect(mergeB.bytes["0x47"].bytes["0x49"].bytes["0x46"].bytes["0x38"].bytes["0x37"].bytes["0x61"].matches[0]).toEqual({ typename: "gif", extension: "gif", mime: "image/gif" });
|
||||
expect(mergeB.bytes["0x47"].bytes["0x49"].bytes["0x46"].bytes["0x38"].bytes["0x39"].bytes["0x61"].matches[0]).toEqual({ typename: "gif", extension: "gif", mime: "image/gif" });
|
||||
expect(mergeB.bytes["0x47"].bytes["0x49"].bytes["0x46"].bytes["0x38"].bytes["0x38"].bytes["0x61"].matches[0]).toEqual({ typename: "gif", extension: "gif", mime: "image/gif" });
|
||||
});
|
||||
});
|
||||
77
node_modules/magic-bytes.js/package.json
generated
vendored
Normal file
77
node_modules/magic-bytes.js/package.json
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
{
|
||||
"name": "magic-bytes.js",
|
||||
"version": "1.12.1",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/"
|
||||
],
|
||||
"release": {
|
||||
"branches": [
|
||||
"master"
|
||||
],
|
||||
"plugins": [
|
||||
"@semantic-release/commit-analyzer",
|
||||
"@semantic-release/release-notes-generator",
|
||||
"@semantic-release/npm",
|
||||
"@semantic-release/github",
|
||||
"@semantic-release/git"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"prettier": "prettier --write \"{src,__{tests,mocks}__}/**/*.{tsx,ts}\"",
|
||||
"pre-test": "jest --clear-cache",
|
||||
"prebuild": "rimraf dist",
|
||||
"test": "jest",
|
||||
"build": "tsc",
|
||||
"example:html": "webpack --config example/html/webpack.config.js; open example/html/index.html",
|
||||
"example:webapp": "vite --config example/webapp/vite.config.js example/webapp",
|
||||
"build:example:webapp": "vite --config example/webapp/vite.config.js build example/webapp",
|
||||
"semantic-release": "semantic-release",
|
||||
"prepare": "npm run build"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://github.com/LarsKoelpin/magic-bytes",
|
||||
"type": "git"
|
||||
},
|
||||
"author": "Lars Kölpin",
|
||||
"license": "MIT",
|
||||
"description": "Detect Filetype by bytes",
|
||||
"keywords": [
|
||||
"magic-bytes",
|
||||
"mime",
|
||||
"filetype",
|
||||
"file",
|
||||
"extension",
|
||||
"magic byte",
|
||||
"magic number",
|
||||
"mime",
|
||||
"mimetype",
|
||||
"validation",
|
||||
"javascript",
|
||||
"upload"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@babel/preset-typescript": "^7.15.0",
|
||||
"@commitlint/cli": "^17.7.1",
|
||||
"@commitlint/config-conventional": "^17.7.0",
|
||||
"@semantic-release/git": "^10.0.1",
|
||||
"@types/jest": "^27.5.2",
|
||||
"@types/node": "^16.6.2",
|
||||
"husky": "^8.0.3",
|
||||
"jest": "^27.0.6",
|
||||
"open": "^9.1.0",
|
||||
"prettier": "^2.6.1",
|
||||
"prettier-plugin-organize-imports": "^2.3.4",
|
||||
"regenerator-runtime": "^0.11.1",
|
||||
"semantic-release": "^22.0.5",
|
||||
"ts-jest": "^27.0.5",
|
||||
"ts-loader": "^9.4.4",
|
||||
"ts-node": "^10.2.1",
|
||||
"typescript": "^4.3.5",
|
||||
"vite": "^4.4.9",
|
||||
"webpack-cli": "^5.1.4",
|
||||
"rimraf": "^6.0.1"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user