Skip to content

Commit 52b65c7

Browse files
authored
fix(raw): use mime to chck binary types and exclude .json (#2239)
1 parent a445fae commit 52b65c7

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/rollup/plugins/raw.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { promises as fsp } from "node:fs";
22
import { extname } from "pathe";
3+
import mime from "mime";
34
import type { Plugin } from "rollup";
45

56
export interface RawOptions {
@@ -18,15 +19,9 @@ export function raw(opts: RawOptions = {}): Plugin {
1819
".css",
1920
".htm",
2021
".html",
21-
".json",
22-
".json5",
23-
".csv",
2422
...(opts.extensions || []),
2523
]);
2624

27-
// TODO: use ext=>mime
28-
const isBinary = (id) => !extensions.has(extname(id));
29-
3025
return {
3126
name: "raw",
3227
resolveId(id) {
@@ -79,6 +74,17 @@ export function raw(opts: RawOptions = {}): Plugin {
7974
};
8075
}
8176

77+
function isBinary(id: string) {
78+
const idMime = mime.getType(id) || "";
79+
if (idMime.startsWith("text/")) {
80+
return false;
81+
}
82+
if (/application\/(json|xml|yaml)/.test(idMime)) {
83+
return false;
84+
}
85+
return true;
86+
}
87+
8288
function getHelpers() {
8389
const js = String.raw;
8490
return js`

test/fixture/assets/test.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}

test/fixture/routes/assets/all.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default eventHandler(async (event) => {
2+
const serverAssets = useStorage("assets/server");
3+
4+
const keys = await serverAssets.getKeys();
5+
const items = await Promise.all(
6+
keys.map(async (key) => {
7+
return {
8+
key,
9+
meta: await serverAssets.getMeta(key),
10+
data: await serverAssets.getItem(key).then((r) =>
11+
// prettier-ignore
12+
typeof r === "string" ? r.slice(0, 32) : (isPureObject(r) ? r : `<data>`)
13+
),
14+
};
15+
})
16+
);
17+
18+
return items;
19+
});
20+
21+
function isPureObject(value) {
22+
return (
23+
value !== null && typeof value === "object" && value.constructor === Object
24+
);
25+
}

0 commit comments

Comments
 (0)