@@ -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 .
3030fn 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.
4539fn 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.
7662fn 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.
10180pub 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 .
10887fn 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.
125104fn 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.
163139fn 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 .
180156fn ensureModuleWebUiScript (allocator : std.mem.Allocator , html : []const u8 ) ! []u8 {
181157 const script_tag = "<script src=\" /webui_bridge.js\" ></script>" ;
182158
0 commit comments