🤖 Merge PR #46409 [@types/mmmagic] fix detection callback result type for MAGIC_CONTINUE by @antoinep92

* fix mmmagic detection callback result type for MAGIC_CONTINUE

* update mmmagic callback to string|string[] as discussed in PR 46409
This commit is contained in:
Antoine Poliakov 2020-09-09 10:02:30 +02:00 committed by GitHub
parent 55a3f10c61
commit 3816f1d084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -5,13 +5,19 @@
/// <reference types="node" />
/**
* callback for detect() and detectFile()
* Result is a string, except when MAGIC_CONTINUE is set,
* then it is an array of string
*/
type DetectionCallback = ((err: Error, result: string | string[]) => void)
export type bitmask = number;
export declare class Magic {
constructor(magicPath?: string, mask?: bitmask);
constructor(mask?: bitmask);
detectFile(path: string, callback: (err: Error, result: string) => void): void;
detect(data: Buffer, callback: (err: Error, result: string) => void): void;
detectFile(path: string, callback: DetectionCallback): void;
detect(data: Buffer, callback: DetectionCallback): void;
}
export declare var MAGIC_NONE: bitmask; // no flags set
export declare var MAGIC_DEBUG: bitmask; // turn on debugging

View File

@ -5,16 +5,18 @@ import Magic = require("mmmagic");
var magic: Magic.Magic;
magic = new Magic.Magic();
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err: Error, result: string) {
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err: Error, result: string | string[]) {
if (err) throw err;
if (typeof result !== 'string') throw new Error('expected a string');
console.log(result);
// output on Windows with 32-bit node:
});
// get mime type for a file
magic = new Magic.Magic(Magic.MAGIC_MIME_TYPE);
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err: Error, result: string) {
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err: Error, result) {
if (err) throw err;
if (typeof result !== 'string') throw new Error('expected a string');
console.log(result);
});
@ -22,8 +24,22 @@ magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err:
magic = new Magic.Magic();
var buf = new Buffer('import Options\nfrom os import unlink, symlink');
magic.detect(buf, function(err: Error, result: string) {
magic.detect(buf, function(err: Error, result: string | string[]) {
if (err) throw err;
if (typeof result !== 'string') throw new Error('expected a string');
console.log(result);
// output: Python script, ASCII text executable
});
// get all mime type canidates
const magics = new Magic.Magic(Magic.MAGIC_MIME_TYPE | Magic.MAGIC_CONTINUE);
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
if (!Array.isArray(result)) throw new Error('expected an array')
console.log(result);
});
magic.detect(buf, function(err: Error, result: string | string[]) {
if (err) throw err;
if (!Array.isArray(result)) throw new Error('expected an array')
console.log(result);
})