Skip to content

Commit

Permalink
Feature add magic flags (#32)
Browse files Browse the repository at this point in the history
Closes #30

In order to allow more extensive usage of LibMagic we now expose the
Flags (https://github.com/file/file/blob/46da4cf/src/magic.h.in#L32).
Where the default is to use `MAGIC_MIME_TYPE`.

- Force UTC Timezone for tests. This avoids errors when GitHub runner
  are running in another timezone

Co-authored-by: Warnar Boekkooi <[email protected]>
  • Loading branch information
moshen and boekkooi-lengoo authored Feb 15, 2024
1 parent be67c28 commit ca502e2
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ num_processors := $(shell nproc || printf "1")
export EMCC_CFLAGS = -msimd128 -O2

test: dist/index.js dist/test/integration/foobar_magic dist/test/integration/png_magic dist/test/integration/jpeg_magic
npm run test
TZ='UTC' npm run test

dist/test/integration/foobar_magic dist/test/integration/png_magic dist/test/integration/jpeg_magic &:
mkdir -p dist/test/integration \
Expand Down
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ provides accurate filetype detection with zero prod dependencies.
npm install wasmagic
```

Detect the mime of something:
Detect the mime and encoding of something:

```javascript
const { WASMagic } = require("wasmagic");
const { WASMagic, WASMagicFlags } = require("wasmagic");

async function main() {
const magic = await WASMagic.create();
const magic = await WASMagic.create({
flags: WASMagicFlags.MIME_TYPE | WASMagicFlags.MIME_ENCODING,
});
const pngFile = Buffer.from("89504E470D0A1A0A0000000D49484452", "hex");
console.log(magic.getMime(pngFile));
}

main().catch((err) => console.error(err));
// outputs: image/png
// outputs: image/png; charset=binary
```

### Options
Expand All @@ -45,6 +47,7 @@ tests](src/test/integration/index.ts).

```typescript
type WASMagicOptions = {
flags?: WASMagicFlags;
loadDefaultMagicfile?: boolean;
magicFiles?: Uint8Array[];
stdio?: (stdioName: "stdout" | "stderr", text: string) => void;
Expand All @@ -55,12 +58,22 @@ Default options:

```typescript
{
flags: WASMagicFlags.MIME_TYPE,
loadDefaultMagicfile: true,
magicFiles: [],
stdio: (_stdioName: "stdout" | "stderr", _text: string) => {},
}
```

##### `flags`

`flags` control `libmagic` behavior. To use the flags, import the `enum` from
the module, [per the example above](#usage).

[Please refer to the code for the flag definitions](src/index.ts#L5)

**Default**: `WASMagicFlags.MIME_TYPE`

##### `loadDefaultMagicfile`

`loadDefaultMagicFile` is a `boolean` dictates whether or not to load the
Expand Down
66 changes: 64 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,81 @@ import libmagicFactory from "../dist/libmagic-wrapper";

declare function wrappedGetMime(bufPointer: number, bufLength: number): string;

export enum WASMagicFlags {
/** No flags */
NONE = 0x0000000,
/** Turn on debugging */
DEBUG = 0x0000001,
/** Follow symlinks */
SYMLINK = 0x0000002,
/** Check inside compressed files */
COMPRESS = 0x0000004,
/** Look at the contents of devices */
DEVICES = 0x0000008,
/** Return the MIME type */
MIME_TYPE = 0x0000010,
/** Return all matches */
CONTINUE = 0x0000020,
/** Print warnings to stderr */
CHECK = 0x0000040,
/** Restore access time on exit */
PRESERVE_ATIME = 0x0000080,
/** Don't convert unprintable chars */
RAW = 0x0000100,
/** Handle ENOENT etc as real errors */
ERROR = 0x0000200,
/** Return the MIME encoding */
MIME_ENCODING = 0x0000400,
/** Return the Apple creator/type */
APPLE = 0x0000800,
/** Return a /-separated list of extensions */
EXTENSION = 0x1000000,
/** Check inside compressed files but not report compression */
COMPRESS_TRANSP = 0x2000000,
/** Don't allow decompression that needs to fork */
NO_COMPRESS_FORK = 0x4000000,
/** Don't check for compressed files */
NO_CHECK_COMPRESS = 0x0001000,
/** Don't check for tar files */
NO_CHECK_TAR = 0x0002000,
/** Don't check magic entries */
NO_CHECK_SOFT = 0x0004000,
/** Don't check application type */
NO_CHECK_APPTYPE = 0x0008000,
/** Don't check for elf details */
NO_CHECK_ELF = 0x0010000,
/** Don't check for text files */
NO_CHECK_TEXT = 0x0020000,
/** Don't check for cdf files */
NO_CHECK_CDF = 0x0040000,
/** Don't check for CSV files */
NO_CHECK_CSV = 0x0080000,
/** Don't check tokens */
NO_CHECK_TOKENS = 0x0100000,
/** Don't check text encodings */
NO_CHECK_ENCODING = 0x0200000,
/** Don't check for JSON files */
NO_CHECK_JSON = 0x0400000,
/** Don't check for SIMH tape files */
NO_CHECK_SIMH = 0x0800000,
}

export type WASMagicOptions = {
flags?: WASMagicFlags;
loadDefaultMagicfile?: boolean;
magicFiles?: Uint8Array[];
stdio?: StdioOverrideFunction;
};

type WASMagicOptionsComplete = {
flags: WASMagicFlags;
loadDefaultMagicfile: boolean;
magicFiles: Uint8Array[];
stdio: StdioOverrideFunction;
};

const defaultWASMagicOptions: WASMagicOptionsComplete = Object.freeze({
flags: WASMagicFlags.MIME_TYPE,
loadDefaultMagicfile: true,
magicFiles: [],
stdio: (_stdioName: "stdout" | "stderr", _text: string) => {},
Expand Down Expand Up @@ -52,8 +114,8 @@ export class WASMagic {
const loadErr = Module.ccall(
"magic_wrapper_load",
"string",
["string"],
[magicPaths.join(":")],
["number", "string"],
[options.flags, magicPaths.join(":")],
);

if (loadErr !== "") {
Expand Down
3 changes: 2 additions & 1 deletion src/libmagic-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
struct magic_set *ms = NULL;

const char* magic_wrapper_load(
int flags,
char* magic_paths
) {
if (ms != NULL) {
return "Load called multiple times";
}

ms = magic_open(0 | MAGIC_MIME_TYPE);
ms = magic_open(flags);
if (magic_load(ms, magic_paths) == -1) {
return magic_error(ms);
}
Expand Down
Loading

0 comments on commit ca502e2

Please sign in to comment.