ReviewOS

also looking at this

craft-native/craft

fix(bridge): stop discarding the fields the page actually sends

#82
Merged glennmichael123 wants to merge fix/bridge-field-names into main
7 files +375 -34
packages/zig/src/bridge_fs.zigmodified+25-6
Changes to packages/zig/src/bridge_fs.zig
@@ -152,7 +152,13 @@ pub const FSBridge = struct {
152152 /// JSON: {"path": "/path/to/file", "content": "data", "callbackId": "cb1"}
153153 fn writeFile(self: *Self, data: []const u8) !void {
154154 const path = json_utils.getString(data, "path") orelse "";
155 const content = json_utils.getString(data, "content") orelse "";
155 // `craft.fs.writeFile(path, data)` sends the bytes under `data`; this
156 // read `content`, which the page has never sent, so `orelse ""` fired
157 // on every call and the file was created empty. `content` stays
158 // accepted for anything posting raw bridge messages against the old
159 // spelling.
160 const content = json_utils.getString(data, "data") orelse
161 json_utils.getString(data, "content") orelse "";
156162 const callback_id = json_utils.getString(data, "callbackId") orelse "";
157163
158164 if (path.len == 0) return BridgeError.MissingData;
@@ -179,7 +185,10 @@ pub const FSBridge = struct {
179185 /// JSON: {"path": "/path/to/file", "content": "data", "callbackId": "cb1"}
180186 fn appendFile(self: *Self, data: []const u8) !void {
181187 const path = json_utils.getString(data, "path") orelse "";
182 const content = json_utils.getString(data, "content") orelse "";
188 // Same mismatch as `writeFile`: the page sends `data`, this read
189 // `content`, and the append wrote nothing while reporting success.
190 const content = json_utils.getString(data, "data") orelse
191 json_utils.getString(data, "content") orelse "";
183192 const callback_id = json_utils.getString(data, "callbackId") orelse "";
184193
185194 if (path.len == 0) return BridgeError.MissingData;
@@ -451,8 +460,13 @@ pub const FSBridge = struct {
451460 /// Copy file
452461 /// JSON: {"src": "/path/from", "dest": "/path/to", "callbackId": "cb1"}
453462 fn copy(self: *Self, data: []const u8) !void {
454 const src = json_utils.getString(data, "src") orelse "";
455 const dest = json_utils.getString(data, "dest") orelse "";
463 // `craft.fs.copy(from, to)` sends `from`/`to`; this read `src`/`dest`,
464 // so both were empty and the call failed its own path check while the
465 // caller's promise resolved.
466 const src = json_utils.getString(data, "from") orelse
467 json_utils.getString(data, "src") orelse "";
468 const dest = json_utils.getString(data, "to") orelse
469 json_utils.getString(data, "dest") orelse "";
456470 const callback_id = json_utils.getString(data, "callbackId") orelse "";
457471
458472 if (src.len == 0 or dest.len == 0) return BridgeError.MissingData;
@@ -473,8 +487,13 @@ pub const FSBridge = struct {
473487 /// Move/rename file
474488 /// JSON: {"src": "/path/from", "dest": "/path/to", "callbackId": "cb1"}
475489 fn move(self: *Self, data: []const u8) !void {
476 const src = json_utils.getString(data, "src") orelse "";
477 const dest = json_utils.getString(data, "dest") orelse "";
490 // `craft.fs.move(from, to)` sends `from`/`to`; this read `src`/`dest`,
491 // so both were empty and the call failed its own path check while the
492 // caller's promise resolved.
493 const src = json_utils.getString(data, "from") orelse
494 json_utils.getString(data, "src") orelse "";
495 const dest = json_utils.getString(data, "to") orelse
496 json_utils.getString(data, "dest") orelse "";
478497 const callback_id = json_utils.getString(data, "callbackId") orelse "";
479498
480499 if (src.len == 0 or dest.len == 0) return BridgeError.MissingData;