Skip to content

Commit 457eb2d

Browse files
fix: dynamically imported the file-type
1 parent 03f8e9d commit 457eb2d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

packages/core/src/helper.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { fileTypeFromBuffer } from "file-type";
21
import type {
32
AudioContent,
43
BlobResourceContents,
@@ -45,14 +44,14 @@ export function resourceLink(
4544

4645
export type ContentOptions =
4746
| {
48-
url: string;
49-
}
47+
url: string;
48+
}
5049
| {
51-
buffer: Buffer;
52-
}
50+
buffer: Buffer;
51+
}
5352
| {
54-
path: string;
55-
};
53+
path: string;
54+
};
5655

5756
export async function imageContent(
5857
content: ContentOptions,
@@ -70,7 +69,7 @@ export async function imageContent(
7069
throw new Error("Invalid content options");
7170
}
7271

73-
const mimeType = await fileTypeFromBuffer(rawData);
72+
const mimeType = await getMimeType(rawData);
7473

7574
const base64Data = rawData.toString("base64");
7675

@@ -98,7 +97,7 @@ export async function audioContent(
9897
throw new Error("Invalid content options");
9998
}
10099

101-
const mimeType = await fileTypeFromBuffer(rawData);
100+
const mimeType = await getMimeType(rawData);
102101

103102
const base64Data = rawData.toString("base64");
104103

@@ -134,3 +133,13 @@ async function loadFromPath(path: string) {
134133
throw new Error("muppet: Unable to import 'fs' module.");
135134
}
136135
}
136+
137+
async function getMimeType(buffer: Buffer) {
138+
try {
139+
const { fileTypeFromBuffer } = await import("file-type");
140+
141+
return fileTypeFromBuffer(buffer);
142+
} catch (error) {
143+
throw new Error("muppet: Unable to import 'file-type' module.");
144+
}
145+
}

0 commit comments

Comments
 (0)