elysia
Version:
Ergonomic Framework for Human
160 lines (159 loc) • 5 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var file_exports = {};
__export(file_exports, {
ElysiaFile: () => ElysiaFile,
file: () => file,
getFileExtension: () => getFileExtension,
mime: () => mime
});
module.exports = __toCommonJS(file_exports);
var import_utils = require('./utils.js');
const mime = {
aac: "audio/aac",
abw: "application/x-abiword",
ai: "application/postscript",
arc: "application/octet-stream",
avi: "video/x-msvideo",
azw: "application/vnd.amazon.ebook",
bin: "application/octet-stream",
bz: "application/x-bzip",
bz2: "application/x-bzip2",
csh: "application/x-csh",
css: "text/css",
csv: "text/csv",
doc: "application/msword",
dll: "application/octet-stream",
eot: "application/vnd.ms-fontobject",
epub: "application/epub+zip",
gif: "image/gif",
htm: "text/html",
html: "text/html",
ico: "image/x-icon",
ics: "text/calendar",
jar: "application/java-archive",
jpeg: "image/jpeg",
jpg: "image/jpeg",
js: "application/javascript",
json: "application/json",
mid: "audio/midi",
midi: "audio/midi",
mp2: "audio/mpeg",
mp3: "audio/mpeg",
mp4: "video/mp4",
mpa: "video/mpeg",
mpe: "video/mpeg",
mpeg: "video/mpeg",
mpkg: "application/vnd.apple.installer+xml",
odp: "application/vnd.oasis.opendocument.presentation",
ods: "application/vnd.oasis.opendocument.spreadsheet",
odt: "application/vnd.oasis.opendocument.text",
oga: "audio/ogg",
ogv: "video/ogg",
ogx: "application/ogg",
otf: "font/otf",
png: "image/png",
pdf: "application/pdf",
ppt: "application/vnd.ms-powerpoint",
rar: "application/x-rar-compressed",
rtf: "application/rtf",
sh: "application/x-sh",
svg: "image/svg+xml",
swf: "application/x-shockwave-flash",
tar: "application/x-tar",
tif: "image/tiff",
tiff: "image/tiff",
ts: "application/typescript",
ttf: "font/ttf",
txt: "text/plain",
vsd: "application/vnd.visio",
wav: "audio/x-wav",
weba: "audio/webm",
webm: "video/webm",
webp: "image/webp",
woff: "font/woff",
woff2: "font/woff2",
xhtml: "application/xhtml+xml",
xls: "application/vnd.ms-excel",
xlsx: "application/vnd.ms-excel",
xlsx_OLD: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
xml: "application/xml",
xul: "application/vnd.mozilla.xul+xml",
zip: "application/zip",
"3gp": "video/3gpp",
"3gp_DOES_NOT_CONTAIN_VIDEO": "audio/3gpp",
"3gp2": "video/3gpp2",
"3gp2_DOES_NOT_CONTAIN_VIDEO": "audio/3gpp2",
"7z": "application/x-7z-compressed"
}, getFileExtension = (path) => {
const index = path.lastIndexOf(".");
return index === -1 ? "" : path.slice(index + 1);
}, file = (path) => new ElysiaFile(path);
let createReadStream, stat;
class ElysiaFile {
constructor(path) {
this.path = path;
if (import_utils.isBun) this.value = Bun.file(path);
else {
if (!createReadStream || !stat) {
if (typeof window < "u") {
console.warn("Browser environment does not support file");
return;
}
const warnMissing = (name) => console.warn(
new Error(
`[elysia] \`file\` require \`fs${name ? "." + name : ""}\` ${name?.includes(".") ? "module " : ""}which is not available in this environment`
)
);
if (typeof process > "u" || typeof process.getBuiltinModule != "function") {
warnMissing();
return;
}
const fs = process.getBuiltinModule("fs");
if (!fs) {
warnMissing();
return;
}
if (typeof fs.createReadStream != "function") {
warnMissing();
return;
}
if (typeof fs.promises?.stat != "function") {
warnMissing();
return;
}
createReadStream = fs.createReadStream, stat = fs.promises.stat;
}
this.value = createReadStream(path), this.stats = stat(path);
}
}
get type() {
return (
// @ts-ignore
mime[getFileExtension(this.path)] || "application/octet-stream"
);
}
get length() {
return import_utils.isBun ? this.value.size : this.stats?.then((x) => x.size) ?? 0;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ElysiaFile,
file,
getFileExtension,
mime
});