Compare commits

...

4 Commits

Author SHA1 Message Date
Jarred Sumner
fce2a0d44e Merge branch 'main' into don/build/mime-type-comptime 2025-03-31 11:25:50 -07:00
Don Isaac
c7be93ccf1 Update mime_type.zig
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
2025-03-28 09:01:04 -07:00
DonIsaac
514e555aad bun run zig-format 2025-03-27 20:43:58 +00:00
Don Isaac
8917eae0df build: reduce comptime usages in MimeType 2025-03-27 13:41:26 -07:00
4 changed files with 30 additions and 63 deletions

View File

@@ -6843,7 +6843,7 @@ pub fn putOrOverwriteAsset(
) !void {
dev.graph_safety_lock.lock();
defer dev.graph_safety_lock.unlock();
_ = try dev.assets.replacePath(path.text, contents, &.byExtension(path.name.extWithoutLeadingDot()), content_hash);
_ = try dev.assets.replacePath(path.text, contents, &MimeType.byExtension(path.name.extWithoutLeadingDot()), content_hash);
}
/// Storage for hashed assets on `/_bun/asset/{hash}.ext`

View File

@@ -14,7 +14,6 @@ const SystemError = JSC.SystemError;
const Output = bun.Output;
const MutableString = bun.MutableString;
const strings = bun.strings;
const string = bun.string;
const default_allocator = bun.default_allocator;
const FeatureFlags = bun.FeatureFlags;
const ArrayBuffer = @import("../base.zig").ArrayBuffer;
@@ -84,7 +83,7 @@ pub const Blob = struct {
/// If the blob is contained in Response or Request, this must be null
allocator: ?std.mem.Allocator = null,
store: ?*Store = null,
content_type: string = "",
content_type: []const u8 = "",
content_type_allocated: bool = false,
content_type_was_set: bool = false,
@@ -569,7 +568,7 @@ pub const Blob = struct {
};
search_params.toString(URLSearchParamsConverter, &converter, URLSearchParamsConverter.convert);
var store = Blob.Store.init(converter.buf, allocator);
store.mime_type = MimeType.all.@"application/x-www-form-urlencoded";
store.mime_type = MimeType.form_urlencoded;
var blob = Blob.initWithStore(store, globalThis);
blob.content_type = store.mime_type.value;
@@ -619,7 +618,7 @@ pub const Blob = struct {
return blob;
}
pub fn contentType(this: *const Blob) string {
pub fn contentType(this: *const Blob) []const u8 {
return this.content_type;
}
@@ -4711,7 +4710,7 @@ pub const Blob = struct {
}
}
var content_type: string = "";
var content_type: []const u8 = "";
var content_type_was_allocated = false;
if (args_iter.nextEat()) |content_type_| {
inner: {

View File

@@ -1,21 +1,13 @@
const std = @import("std");
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const Loader = @import("../options.zig").Loader;
const ComptimeStringMap = bun.ComptimeStringMap;
const MimeType = @This();
value: string,
value: []const u8,
category: Category,
pub const Map = bun.StringHashMap(MimeType);
@@ -89,28 +81,19 @@ pub const Category = enum {
}
};
pub const none = MimeType.initComptime("", .none);
pub const other = MimeType.initComptime("application/octet-stream", .other);
pub const css = MimeType.initComptime("text/css;charset=utf-8", .css);
pub const javascript = MimeType.initComptime("text/javascript;charset=utf-8", .javascript);
pub const ico = MimeType.initComptime("image/vnd.microsoft.icon", .image);
pub const html = MimeType.initComptime("text/html;charset=utf-8", .html);
// we transpile json to javascript so that it is importable without import assertions.
pub const json = MimeType.initComptime("application/json;charset=utf-8", .json);
pub const none = MimeType{ .value = "", .category = .none };
pub const other = MimeType{ .value = "application/octet-stream", .category = .other };
pub const css = MimeType{ .value = "text/css;charset=utf-8", .category = .css };
pub const javascript = MimeType{ .value = "text/javascript;charset=utf-8", .category = .javascript };
pub const ico = MimeType{ .value = "image/vnd.microsoft.icon", .category = .image };
pub const html = MimeType{ .value = "text/html;charset=utf-8", .category = .html };
pub const json = MimeType{ .value = "application/json;charset=utf-8", .category = .json };
pub const transpiled_json = javascript;
pub const text = MimeType.initComptime("text/plain;charset=utf-8", .html);
pub const wasm = MimeType.initComptime(
"application/wasm",
.wasm,
);
fn initComptime(comptime str: string, t: Category) MimeType {
return MimeType{
.value = str,
.category = t,
};
}
pub const text = MimeType{ .value = "text/plain;charset=utf-8", .category = .html }; // why??
pub const wasm = MimeType{ .value = "application/wasm", .category = .wasm };
pub const form_urlencoded = MimeType{ .value = "application/x-www-form-urlencoded;charset=utf-8", .category = .application };
pub fn init(str_: string, allocator: ?std.mem.Allocator, allocated: ?*bool) MimeType {
pub fn init(str_: []const u8, allocator: ?std.mem.Allocator, allocated: ?*bool) MimeType {
var str = str_;
if (std.mem.indexOfScalar(u8, str, '/')) |slash| {
const category_ = str[0..slash];
@@ -216,31 +199,16 @@ pub fn init(str_: string, allocator: ?std.mem.Allocator, allocated: ?*bool) Mime
};
}
// TODO: improve this
pub fn byLoader(loader: Loader, ext: string) MimeType {
switch (loader) {
.tsx, .ts, .js, .jsx, .json => {
return javascript;
},
.css => {
return css;
},
else => {
return byExtension(ext);
},
}
}
pub fn byExtension(ext_without_leading_dot: string) MimeType {
pub fn byExtension(ext_without_leading_dot: []const u8) MimeType {
return byExtensionNoDefault(ext_without_leading_dot) orelse MimeType.other;
}
pub fn byExtensionNoDefault(ext_without_leading_dot: string) ?MimeType {
pub fn byExtensionNoDefault(ext_without_leading_dot: []const u8) ?MimeType {
return extensions.get(ext_without_leading_dot);
}
// this is partially auto-generated
pub const all = struct {
const all = struct {
pub const @"application/webassembly" = wasm;
pub const @"application/1d-interleaved-parityfec": MimeType = MimeType{ .category = .application, .value = "application/1d-interleaved-parityfec" };
pub const @"application/3gpdash-qoe-report+xml": MimeType = MimeType{ .category = .application, .value = "application/3gpdash-qoe-report+xml" };
@@ -1879,7 +1847,7 @@ pub const all = struct {
pub const @"application/x-virtualbox-vmdk": MimeType = MimeType{ .category = .application, .value = "application/x-virtualbox-vmdk" };
pub const @"application/x-wais-source": MimeType = MimeType{ .category = .application, .value = "application/x-wais-source" };
pub const @"application/x-web-app-manifest+json": MimeType = MimeType{ .category = .application, .value = "application/x-web-app-manifest+json" };
pub const @"application/x-www-form-urlencoded": MimeType = MimeType{ .category = .application, .value = "application/x-www-form-urlencoded;charset=UTF-8" };
pub const @"application/x-www-form-urlencoded": MimeType = form_urlencoded;
pub const @"application/x-x509-ca-cert": MimeType = MimeType{ .category = .application, .value = "application/x-x509-ca-cert" };
pub const @"application/x-x509-ca-ra-cert": MimeType = MimeType{ .category = .application, .value = "application/x-x509-ca-ra-cert" };
pub const @"application/x-x509-next-ca-cert": MimeType = MimeType{ .category = .application, .value = "application/x-x509-next-ca-cert" };
@@ -2537,7 +2505,7 @@ pub fn byName(name: []const u8) MimeType {
pub fn deinit(mimeType: MimeType, allocator: std.mem.Allocator) void {
allocator.free(mimeType.value);
}
pub const extensions = ComptimeStringMap(MimeType, .{
const extensions = ComptimeStringMap(MimeType, .{
.{ "123", all.@"application/vnd.lotus-1-2-3" },
.{ "1km", all.@"application/vnd.1000minds.decision-model+xml" },
.{ "3dml", all.@"text/vnd.in3d.3dml" },
@@ -3734,13 +3702,13 @@ pub const extensions = ComptimeStringMap(MimeType, .{
.{ "zmm", all.@"application/vnd.handheld-entertainment+xml" },
});
const IMAGES_HEADERS = .{
.{ [_]u8{ 0x42, 0x4d }, all.@"image/bmp" },
.{ [_]u8{ 0xff, 0xd8, 0xff }, all.@"image/jpeg" },
.{ [_]u8{ 0x49, 0x49, 0x2a, 0x00 }, all.@"image/tiff" },
.{ [_]u8{ 0x4d, 0x4d, 0x00, 0x2a }, all.@"image/tiff" },
.{ [_]u8{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, all.@"image/gif" },
.{ [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }, all.@"image/png" },
const IMAGES_HEADERS = &[_]struct { []const u8, MimeType }{
.{ &[_]u8{ 0x42, 0x4d }, all.@"image/bmp" },
.{ &[_]u8{ 0xff, 0xd8, 0xff }, all.@"image/jpeg" },
.{ &[_]u8{ 0x49, 0x49, 0x2a, 0x00 }, all.@"image/tiff" },
.{ &[_]u8{ 0x4d, 0x4d, 0x00, 0x2a }, all.@"image/tiff" },
.{ &[_]u8{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, all.@"image/gif" },
.{ &[_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }, all.@"image/png" },
};
pub fn sniff(bytes: []const u8) ?MimeType {
if (bytes.len < 2) return null;

View File

@@ -870,7 +870,7 @@ pub const color_map = ComptimeStringMap(string, .{
const RESET: string = "\x1b[0m";
pub fn prettyFmt(comptime fmt: string, comptime is_enabled: bool) [:0]const u8 {
if (comptime bun.fast_debug_build_mode)
return fmt;
return fmt ++ "\x00";
comptime var new_fmt: [fmt.len * 4]u8 = undefined;
comptime var new_fmt_i: usize = 0;