Skip to content

Commit

Permalink
feat: support binary server assets (#2107)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 22, 2024
1 parent 7d3ca94 commit 60eb9f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/rollup/plugins/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface RawOptions {
extensions?: string[];
}

const HELPER_ID = "\0raw-helpers";

export function raw(opts: RawOptions = {}): Plugin {
const extensions = new Set([
".md",
Expand All @@ -18,9 +20,16 @@ export function raw(opts: RawOptions = {}): Plugin {
...(opts.extensions || []),
]);

// TODO: use ext=>mime
const isBinary = (id) => !extensions.has(extname(id));

return {
name: "raw",
resolveId(id) {
if (id === HELPER_ID) {
return id;
}

if (id[0] === "\0") {
return;
}
Expand All @@ -38,20 +47,45 @@ export function raw(opts: RawOptions = {}): Plugin {
}
},
load(id) {
if (id === HELPER_ID) {
return getHelpers();
}
if (id.startsWith("\0raw:")) {
// this.addWatchFile(id.substring(5))
return fsp.readFile(id.slice(5), "utf8");
return fsp.readFile(id.slice(5), isBinary(id) ? "binary" : "utf8");
}
},
transform(code, id) {
if (id.startsWith("\0raw:")) {
if (!id.startsWith("\0raw:")) {
return;
}
if (isBinary(id)) {
const serialized = Buffer.from(code, "binary").toString("base64");
return {
code: `// ROLLUP_NO_REPLACE \n import {base64ToUint8Array } from "${HELPER_ID}" \n export default base64ToUint8Array("${serialized}")`,
map: null,
};
} else {
return {
code: `// ROLLUP_NO_REPLACE \n export default ${JSON.stringify(
code
)}`,
code: `// ROLLUP_NO_REPLACE \n export default ${JSON.stringify(code)}`,
map: null,
};
}
},
};
}

function getHelpers() {
const js = String.raw;
return js`
export function base64ToUint8Array(str) {
const data = atob(str);
const size = data.length;
const bytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
bytes[i] = data.charCodeAt(i);
}
return bytes;
}
`;
}
Binary file added test/fixture/assets/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions test/fixture/routes/assets/[id].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default eventHandler(async (event) => {
const serverAssets = useStorage("assets/server");
const id = event.context.params.id;

if (!(await serverAssets.hasItem(id))) {
throw createError({ message: `Asset ${id} not found`, statusCode: 404 });
}

const meta = (await serverAssets.getMeta(
event.context.params.id
)) as unknown as { type: string; etag: string; mtime: string };
meta.type && setResponseHeader(event, "content-type", meta.type);
meta.etag && setResponseHeader(event, "etag", meta.etag);
meta.mtime && setResponseHeader(event, "last-modified", meta.mtime);

return serverAssets.getItemRaw(event.context.params.id);
});

0 comments on commit 60eb9f6

Please sign in to comment.