From fea9267798a1306fde92762b1487fa1ce157f695 Mon Sep 17 00:00:00 2001 From: Colin Kennedy Date: Sun, 14 Apr 2024 20:35:59 -0400 Subject: [PATCH] Fix StdioOverrideFunction Type export This was a case of bad management of the types in play and their exposure outside the package. The fix that works for both package maintenance, and external users is to ensure that the type is available and copied into the final npm package. Finally this would have been caught with a test, which has now been added. - Fix #40 - Add dedicated type file for StdioOverrideFunction - Change type file paths to be relative - Move all type files to the `src` folder to make things easier - Add Makefile rules to copy over `.d.ts` files so they are in the package --- Makefile | 12 +++++-- dist/.npmignore | 2 ++ {types => src}/LibmagicModule.d.ts | 7 ++-- src/StdioOverrideFunction.d.ts | 6 ++++ src/index.ts | 5 ++- {types => src}/libmagic-wrapper.d.ts | 2 ++ src/test/integration/index.ts | 51 ++++++++++++++++++++++++++++ 7 files changed, 77 insertions(+), 8 deletions(-) rename {types => src}/LibmagicModule.d.ts (62%) create mode 100644 src/StdioOverrideFunction.d.ts rename {types => src}/libmagic-wrapper.d.ts (64%) diff --git a/Makefile b/Makefile index 4b8c1f5..e64d1d7 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ dist/test/integration/foobar_magic dist/test/integration/png_magic dist/test/int package: dist/index.js dist/libmagic.LICENSE -dist/index.js: $(ts_files) dist/libmagic-wrapper.js +dist/index.js: $(ts_files) dist/libmagic-wrapper.js dist/LibmagicModule.d.ts dist/StdioOverrideFunction.d.ts npx tsc -d dist/libmagic-wrapper.js: src/libmagic-wrapper.c dist/magic.mgc dist/libmagic.so dist/libmagic-wrapper.d.ts @@ -31,8 +31,14 @@ dist/libmagic-wrapper.js: src/libmagic-wrapper.c dist/magic.mgc dist/libmagic.so -o dist/libmagic-wrapper.js \ src/libmagic-wrapper.c -dist/libmagic-wrapper.d.ts: types/libmagic-wrapper.d.ts - cp types/libmagic-wrapper.d.ts dist/libmagic-wrapper.d.ts +dist/libmagic-wrapper.d.ts: src/libmagic-wrapper.d.ts + cp src/libmagic-wrapper.d.ts dist/libmagic-wrapper.d.ts + +dist/LibmagicModule.d.ts: src/LibmagicModule.d.ts + cp src/LibmagicModule.d.ts dist/LibmagicModule.d.ts + +dist/StdioOverrideFunction.d.ts: src/StdioOverrideFunction.d.ts + cp src/StdioOverrideFunction.d.ts dist/StdioOverrideFunction.d.ts dist/libmagic.LICENSE: vendor/file/COPYING cp vendor/file/COPYING dist/libmagic.LICENSE diff --git a/dist/.npmignore b/dist/.npmignore index 160d5c2..cbddbe0 100644 --- a/dist/.npmignore +++ b/dist/.npmignore @@ -2,3 +2,5 @@ !index.* !*LICENSE !libmagic-wrapper.* +!LibmagicModule.d.ts +!StdioOverrideFunction.d.ts diff --git a/types/LibmagicModule.d.ts b/src/LibmagicModule.d.ts similarity index 62% rename from types/LibmagicModule.d.ts rename to src/LibmagicModule.d.ts index 9902a68..5bbbe51 100644 --- a/types/LibmagicModule.d.ts +++ b/src/LibmagicModule.d.ts @@ -1,7 +1,4 @@ -type StdioOverrideFunction = ( - stdioName: "stdout" | "stderr", - text: string, -) => void; +import StdioOverrideFunction from "./StdioOverrideFunction"; interface LibmagicModule extends EmscriptenModule { FS: typeof FS; @@ -9,3 +6,5 @@ interface LibmagicModule extends EmscriptenModule { cwrap: typeof cwrap; printOverride: StdioOverrideFunction; } + +export default LibmagicModule; diff --git a/src/StdioOverrideFunction.d.ts b/src/StdioOverrideFunction.d.ts new file mode 100644 index 0000000..d759a9f --- /dev/null +++ b/src/StdioOverrideFunction.d.ts @@ -0,0 +1,6 @@ +type StdioOverrideFunction = ( + stdioName: "stdout" | "stderr", + text: string, +) => void; + +export default StdioOverrideFunction; diff --git a/src/index.ts b/src/index.ts index 83740dc..0ee32f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,7 @@ -import libmagicFactory from "../dist/libmagic-wrapper"; +import libmagicFactory from "./libmagic-wrapper"; +import LibmagicModule from "./LibmagicModule"; +import StdioOverrideFunction from "./StdioOverrideFunction"; +export type * as StdioOverrideFunction from "./StdioOverrideFunction"; declare function wrappedDetect(bufPointer: number, bufLength: number): string; diff --git a/types/libmagic-wrapper.d.ts b/src/libmagic-wrapper.d.ts similarity index 64% rename from types/libmagic-wrapper.d.ts rename to src/libmagic-wrapper.d.ts index ac06de7..d087dde 100644 --- a/types/libmagic-wrapper.d.ts +++ b/src/libmagic-wrapper.d.ts @@ -1,2 +1,4 @@ +import LibmagicModule from "./LibmagicModule"; + declare const factory: EmscriptenModuleFactory; export default factory; diff --git a/src/test/integration/index.ts b/src/test/integration/index.ts index 8ff4965..cb93c71 100644 --- a/src/test/integration/index.ts +++ b/src/test/integration/index.ts @@ -3,6 +3,7 @@ import * as path from "path"; import { glob } from "glob"; import { cases } from "./data"; import { WASMagic, WASMagicFlags } from "../../index"; +import StdioOverrideFunction from "../../StdioOverrideFunction"; describe("WASMagic", () => { describe("Default magic file", () => { @@ -49,6 +50,24 @@ describe("WASMagic", () => { expect(magicText.detect(fileBuf)).toBe(expectedText); }, ); + + test("WASMagic throws when it fails to load", async () => { + let err: Error | undefined; + try { + await WASMagic.create({ + loadDefaultMagicfile: false, + magicFiles: [ + // To force a loading failure + Buffer.from("FOOOOOOOBAR"), + ], + }); + } catch (_err) { + err = _err as Error; + } + + expect(err).toBeInstanceOf(Error); + expect(err?.message.startsWith("WASMagic Load Error: ")).toBe(true); + }); }); describe("Specified foobar custom magic file, with default magic file", () => { @@ -112,4 +131,36 @@ Some made up stuff }, ); }); + + describe("StdioOverrideFunction", () => { + describe("StdioOverrideFunction fires when WASMagic fails to load", () => { + let stdioType: string | undefined; + let text: string | undefined; + let func: StdioOverrideFunction = (_stdioType, _text) => { + stdioType = _stdioType; + text = _text; + }; + + beforeAll(async () => { + try { + await WASMagic.create({ + loadDefaultMagicfile: false, + magicFiles: [ + // To force a loading failure + Buffer.from("FOOOOOOOBAR"), + ], + stdio: func, + }); + } catch (_err) { + // Ignoring _err + } + }); + + test("stdio is a String", () => expect(typeof stdioType).toBe("string")); + test("stdio is stderr", () => expect(stdioType).toBe("stderr")); + test("text is a String", () => expect(typeof text).toBe("string")); + test("text is a known error", () => + expect(text).toBe("0, 1: Warning: offset `FOOOOOOOBAR' invalid")); + }); + }); });