Skip to content

Commit

Permalink
Fix StdioOverrideFunction Type export
Browse files Browse the repository at this point in the history
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
  • Loading branch information
moshen committed Apr 15, 2024
1 parent f80e5c3 commit 91e0178
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 9 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions dist/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
!index.*
!*LICENSE
!libmagic-wrapper.*
!LibmagicModule.d.ts
!StdioOverrideFunction.d.ts
7 changes: 3 additions & 4 deletions types/LibmagicModule.d.ts → src/LibmagicModule.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
type StdioOverrideFunction = (
stdioName: "stdout" | "stderr",
text: string,
) => void;
import StdioOverrideFunction from "./StdioOverrideFunction";

interface LibmagicModule extends EmscriptenModule {
FS: typeof FS;
ccall: typeof ccall;
cwrap: typeof cwrap;
printOverride: StdioOverrideFunction;
}

export default LibmagicModule;
6 changes: 6 additions & 0 deletions src/StdioOverrideFunction.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type StdioOverrideFunction = (
stdioName: "stdout" | "stderr",
text: string,
) => void;

export default StdioOverrideFunction;
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import libmagicFactory from "../dist/libmagic-wrapper";
import libmagicFactory from "./libmagic-wrapper";
import LibmagicModule from "./LibmagicModule";
import StdioOverrideFunction from "./StdioOverrideFunction";
export { default as StdioOverrideFunction } from "./StdioOverrideFunction";

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

Expand Down
2 changes: 2 additions & 0 deletions types/libmagic-wrapper.d.ts → src/libmagic-wrapper.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import LibmagicModule from "./LibmagicModule";

declare const factory: EmscriptenModuleFactory<LibmagicModule>;
export default factory;
53 changes: 52 additions & 1 deletion src/test/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import { glob } from "glob";
import { cases } from "./data";
import { WASMagic, WASMagicFlags } from "../../index";
import { WASMagic, WASMagicFlags, StdioOverrideFunction } from "../../index";

describe("WASMagic", () => {
describe("Default magic file", () => {
Expand Down Expand Up @@ -49,6 +49,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", () => {
Expand Down Expand Up @@ -112,4 +130,37 @@ Some made up stuff
},
);
});

describe("StdioOverrideFunction", () => {
describe("StdioOverrideFunction fires when WASMagic fails to load", () => {
let stdioType: string | undefined;
let text: string | undefined;
const stdio: 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,
});
} catch (_err) {
// Ignoring _err
}
});

test("stdioType is a String", () =>
expect(typeof stdioType).toBe("string"));
test("stdioType 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"));
});
});
});

0 comments on commit 91e0178

Please sign in to comment.