mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 19:07:08 +00:00
🤖 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:
parent
55a3f10c61
commit
3816f1d084
10
types/mmmagic/index.d.ts
vendored
10
types/mmmagic/index.d.ts
vendored
@ -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
|
||||
|
||||
@ -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);
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user