Skip to content

Commit

Permalink
refactor: TS
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Aug 29, 2024
1 parent c54663d commit 9aa5c7c
Show file tree
Hide file tree
Showing 18 changed files with 635 additions and 794 deletions.
52 changes: 52 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Stats } from "fs";
import { ReadStream } from "fs";
import mrmime from "mrmime";

/**
* Create a simple ETag.
*/
export async function getEtag(stat: Stats): Promise<string> {
const mtime = stat.mtime.getTime().toString(16);
const size = stat.size.toString(16);

return `W/"${size}-${mtime}"`;
}

export function createReadStreamOrReadFileSync(
filename: string,
outputFileSystem: {
createReadStream: (
path: string,
options: { start: number; end: number },
) => Buffer | ReadStream;
},
start: number,
end: number,
): { bufferOrStream: Buffer | ReadStream; byteLength: number } {
const bufferOrStream = outputFileSystem.createReadStream(filename, {
start,
end,
});
// Handle files with zero bytes
const byteLength = end === 0 ? 0 : end - start + 1;

return { bufferOrStream, byteLength };
}

/**
* Create a full Content-Type header given a MIME type or extension.
*/
export function getContentType(str: string): false | string {
let mime = mrmime.lookup(str);
if (!mime) {
return false;
}
if (
mime.startsWith("text/") ||
mime === "application/json" ||
mime === "application/manifest+json"
) {
mime += `; charset=utf-8`;
}
return mime;
}
266 changes: 0 additions & 266 deletions src/index.js

This file was deleted.

Loading

0 comments on commit 9aa5c7c

Please sign in to comment.