Skip to content

Commit 4aec3ca

Browse files
committed
cleanup
1 parent b439e37 commit 4aec3ca

4 files changed

Lines changed: 128 additions & 261 deletions

File tree

src/embedded_index.zig

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn setEnvironMap(environ_map: *std.process.Environ.Map) void {
2626
process_environ_map = environ_map;
2727
}
2828

29-
// Split index template.
29+
/// Splits the built HTML shell around the bootstrap placeholder inserted at build time.
3030
fn splitIndexTemplate(html: []const u8) ?IndexTemplate {
3131
const marker_index = std.mem.indexOf(u8, html, BOOTSTRAP_PLACEHOLDER) orelse return null;
3232
return .{
@@ -35,13 +35,7 @@ fn splitIndexTemplate(html: []const u8) ?IndexTemplate {
3535
};
3636
}
3737

38-
// Path exists.
39-
fn pathExists(path: []const u8) bool {
40-
std.Io.Dir.accessAbsolute(process_io, path, .{}) catch return false;
41-
return true;
42-
}
43-
44-
// Returns get env var owned compat.
38+
/// Reads an environment variable into owned memory across libc and no-libc targets.
4539
fn getEnvVarOwnedCompat(allocator: std.mem.Allocator, key: []const u8) ![]u8 {
4640
if (process_environ_map) |environ_map| {
4741
if (environ_map.get(key)) |value| {
@@ -64,23 +58,15 @@ fn getEnvVarOwnedCompat(allocator: std.mem.Allocator, key: []const u8) ![]u8 {
6458
return error.EnvironmentVariableNotFound;
6559
}
6660

67-
// Returns get home dir.
68-
fn getHomeDir(allocator: std.mem.Allocator) ![]u8 {
69-
if (builtin.os.tag == .windows) {
70-
return getEnvVarOwnedCompat(allocator, "USERPROFILE");
71-
}
72-
return getEnvVarOwnedCompat(allocator, "HOME");
73-
}
74-
75-
// Returns get app local data dir.
61+
/// Resolves the per-user application data directory used for bootstrap-state and live HTML.
7662
fn getAppLocalDataDir(allocator: std.mem.Allocator) ![]u8 {
7763
if (builtin.os.tag == .windows) {
7864
const appdata = try getEnvVarOwnedCompat(allocator, "APPDATA");
7965
defer allocator.free(appdata);
8066
return std.fs.path.join(allocator, &.{ appdata, APP_ID });
8167
}
8268

83-
const home = try getHomeDir(allocator);
69+
const home = try getEnvVarOwnedCompat(allocator, "HOME");
8470
defer allocator.free(home);
8571

8672
if (builtin.os.tag == .macos) {
@@ -90,21 +76,14 @@ fn getAppLocalDataDir(allocator: std.mem.Allocator) ![]u8 {
9076
return std.fs.path.join(allocator, &.{ home, ".local", "share", APP_ID });
9177
}
9278

93-
// Builds the bootstrap-state file path in the per-user application data directory.
94-
fn bootstrapStatePath(allocator: std.mem.Allocator) ![]u8 {
95-
const app_dir = try getAppLocalDataDir(allocator);
96-
defer allocator.free(app_dir);
97-
return std.fs.path.join(allocator, &.{ app_dir, BOOTSTRAP_STATE_FILE });
98-
}
99-
10079
/// Returns the path to the generated live `index.html` that webui serves at runtime.
10180
pub fn liveIndexPath(allocator: std.mem.Allocator) ![]u8 {
10281
const app_dir = try getAppLocalDataDir(allocator);
10382
defer allocator.free(app_dir);
10483
return std.fs.path.join(allocator, &.{ app_dir, LIVE_INDEX_FILE });
10584
}
10685

107-
// Writes write text file atomic.
86+
/// Writes a file by replacing it atomically so readers never observe a partial HTML snapshot.
10887
fn writeTextFileAtomic(allocator: std.mem.Allocator, path: []const u8, contents: []const u8) !void {
10988
if (std.fs.path.dirname(path)) |parent| {
11089
try std.Io.Dir.cwd().createDirPath(process_io, parent);
@@ -121,16 +100,18 @@ fn writeTextFileAtomic(allocator: std.mem.Allocator, path: []const u8, contents:
121100
try std.Io.Dir.renameAbsolute(temp_path, path, process_io);
122101
}
123102

124-
// Loads the saved bootstrap-state JSON or falls back to the built-in default payload.
103+
/// Loads the persisted bootstrap-state JSON, normalizing empty/missing files to the default payload.
125104
fn readBootstrapStateJsonOrDefault(allocator: std.mem.Allocator) ![]u8 {
126-
const state_path = bootstrapStatePath(allocator) catch {
127-
return allocator.dupe(u8, DEFAULT_BOOTSTRAP_STATE_JSON);
128-
};
105+
const state_path = blk: {
106+
const app_dir = getAppLocalDataDir(allocator) catch break :blk null;
107+
defer allocator.free(app_dir);
108+
break :blk try std.fs.path.join(allocator, &.{ app_dir, BOOTSTRAP_STATE_FILE });
109+
} orelse return allocator.dupe(u8, DEFAULT_BOOTSTRAP_STATE_JSON);
129110
defer allocator.free(state_path);
130111

131-
if (!pathExists(state_path)) {
112+
std.Io.Dir.accessAbsolute(process_io, state_path, .{}) catch {
132113
return allocator.dupe(u8, DEFAULT_BOOTSTRAP_STATE_JSON);
133-
}
114+
};
134115

135116
const raw = std.Io.Dir.cwd().readFileAlloc(process_io, state_path, allocator, .limited(8 * 1024 * 1024)) catch {
136117
return allocator.dupe(u8, DEFAULT_BOOTSTRAP_STATE_JSON);
@@ -154,29 +135,24 @@ fn readBootstrapStateJsonOrDefault(allocator: std.mem.Allocator) ![]u8 {
154135
return normalized;
155136
}
156137

157-
// Serializes the in-memory default bootstrap-state payload.
158-
fn fallbackBootstrapStateJson(allocator: std.mem.Allocator) ![]u8 {
159-
return allocator.dupe(u8, DEFAULT_BOOTSTRAP_STATE_JSON);
160-
}
161-
162-
// Normalizes bootstrap JSON before it is embedded into the live HTML shell.
138+
/// Canonicalizes bootstrap JSON so the generated live HTML always embeds valid compact JSON.
163139
fn canonicalizeBootstrapJson(allocator: std.mem.Allocator, bootstrap_json: []const u8) ![]u8 {
164140
var parsed = std.json.parseFromSlice(std.json.Value, allocator, bootstrap_json, .{}) catch {
165-
return fallbackBootstrapStateJson(allocator);
141+
return allocator.dupe(u8, DEFAULT_BOOTSTRAP_STATE_JSON);
166142
};
167143
defer parsed.deinit();
168144

169145
switch (parsed.value) {
170146
.object => {},
171-
else => return fallbackBootstrapStateJson(allocator),
147+
else => return allocator.dupe(u8, DEFAULT_BOOTSTRAP_STATE_JSON),
172148
}
173149

174150
return std.fmt.allocPrint(allocator, "{f}", .{
175151
std.json.fmt(parsed.value, .{}),
176152
});
177153
}
178154

179-
// Makes sure the generated HTML includes the webui bridge bootstrap script.
155+
/// Injects the webui bridge script tag when the built frontend omitted it.
180156
fn ensureModuleWebUiScript(allocator: std.mem.Allocator, html: []const u8) ![]u8 {
181157
const script_tag = "<script src=\"/webui_bridge.js\"></script>";
182158

src/main.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ const LaunchRequest = struct {
1111
surfaces: [3]?webui.LaunchSurface = .{ null, null, null },
1212
len: usize = 0,
1313

14-
// Append.
14+
/// Appends a launch surface once while preserving the user-specified order.
1515
fn append(self: *LaunchRequest, surface: webui.LaunchSurface) void {
1616
if (self.contains(surface)) return;
1717
if (self.len >= self.surfaces.len) return;
1818
self.surfaces[self.len] = surface;
1919
self.len += 1;
2020
}
2121

22-
// Contains.
22+
/// Returns whether the request already includes the given launch surface.
2323
fn contains(self: *const LaunchRequest, surface: webui.LaunchSurface) bool {
2424
for (self.surfaces[0..self.len]) |entry| {
2525
if (entry == surface) return true;
@@ -49,7 +49,7 @@ pub fn main(init: std.process.Init) !void {
4949
try runModeService(allocator, init.io, live_index_path, window_style, launch_request);
5050
}
5151

52-
// Parses parse launch request.
52+
/// Parses CLI launch flags into an ordered surface preference list for webui.
5353
fn parseLaunchRequest(args: std.process.Args, allocator: std.mem.Allocator) !LaunchRequest {
5454
var request: LaunchRequest = .{};
5555
var arg_it = try std.process.Args.Iterator.initAllocator(args, allocator);
@@ -88,7 +88,7 @@ fn parseLaunchRequest(args: std.process.Args, allocator: std.mem.Allocator) !Lau
8888
return request;
8989
}
9090

91-
// Runs run mode service.
91+
/// Creates the webui service with the requested launch policy and keeps it alive until shutdown.
9292
fn runModeService(
9393
allocator: std.mem.Allocator,
9494
io: std.Io,
@@ -165,7 +165,7 @@ fn runModeService(
165165
service.shutdown();
166166
}
167167

168-
// Ensure native webview available.
168+
/// Rejects explicit native-only launches when the runtime can only fall back to another surface.
169169
fn ensureNativeWebviewAvailable(service: *webui.Service, allocator: std.mem.Allocator) !void {
170170
const capabilities = service.probeCapabilities();
171171
if (capabilities.surface_if_shown != .native_webview or capabilities.fallback_expected) {
@@ -183,7 +183,7 @@ fn ensureNativeWebviewAvailable(service: *webui.Service, allocator: std.mem.Allo
183183
try logMissingNativeRequirements(service, allocator);
184184
}
185185

186-
// Log missing native requirements.
186+
/// Logs missing native runtime requirements and fails if any required dependency is unavailable.
187187
fn logMissingNativeRequirements(service: *webui.Service, allocator: std.mem.Allocator) !void {
188188
const requirements = try service.listRuntimeRequirements(allocator);
189189
defer allocator.free(requirements);

0 commit comments

Comments
 (0)