@native-sdk/cli 0.8.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1849 -18
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +84 -18
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -40,6 +40,8 @@ const extensions = @import("../extensions/root.zig");
40
40
  const app_manifest = @import("app_manifest");
41
41
  const platform = @import("../platform/root.zig");
42
42
  const runtime_effects = @import("effects.zig");
43
+ const runtime_record_store = @import("record_store.zig");
44
+ const runtime_relational_store = @import("relational_store.zig");
43
45
  const security = @import("../security/root.zig");
44
46
 
45
47
  const max_async_bridge_responses = runtime_async_bridge.max_async_bridge_responses;
@@ -674,6 +676,7 @@ pub const Runtime = struct {
674
676
  pub const focusWindow = WindowViewMethods.focusWindow;
675
677
  pub const closeWindow = WindowViewMethods.closeWindow;
676
678
  pub const minimizeWindow = WindowViewMethods.minimizeWindow;
679
+ pub const hideWindow = WindowViewMethods.hideWindow;
677
680
  pub const showWindow = WindowViewMethods.showWindow;
678
681
  pub const quitApp = WindowViewMethods.quitApp;
679
682
  pub const createShellWindow = WindowViewMethods.createShellWindow;
@@ -772,9 +775,11 @@ pub const Runtime = struct {
772
775
  pub const setCredential = SystemServiceMethods.setCredential;
773
776
  pub const getCredential = SystemServiceMethods.getCredential;
774
777
  pub const deleteCredential = SystemServiceMethods.deleteCredential;
778
+ pub const formatLocalTime = SystemServiceMethods.formatLocalTime;
775
779
  pub const createTray = SystemServiceMethods.createTray;
776
780
  pub const updateTrayMenu = SystemServiceMethods.updateTrayMenu;
777
781
  pub const updateTrayTitle = SystemServiceMethods.updateTrayTitle;
782
+ pub const updateTrayPresentation = SystemServiceMethods.updateTrayPresentation;
778
783
  pub const removeTray = SystemServiceMethods.removeTray;
779
784
  const trayCommandNameForItem = SystemServiceMethods.trayCommandNameForItem;
780
785
  const supportsFeatureFromJson = SystemServiceMethods.supportsFeatureFromJson;
@@ -1038,7 +1043,6 @@ pub fn TestHarness() type {
1038
1043
  trace_records: [64]trace.Record = undefined,
1039
1044
  trace_sink: trace.BufferSink = undefined,
1040
1045
  runtime: Runtime = undefined,
1041
-
1042
1046
  /// The harness embeds the multi-megabyte Runtime, so stack
1043
1047
  /// instances overflow test threads; create on the heap.
1044
1048
  pub fn create(gpa: std.mem.Allocator, surface: platform.Surface) !*Self {
@@ -1047,6 +1051,30 @@ pub fn TestHarness() type {
1047
1051
  return self;
1048
1052
  }
1049
1053
 
1054
+ /// Store-capability counterpart of `create`. The SQLite database is
1055
+ /// private to this harness and is automatically bound to every UiApp
1056
+ /// effect channel installed into its runtime.
1057
+ pub fn createWithRecordStore(gpa: std.mem.Allocator, surface: platform.Surface) !*RecordStoreTestHarness() {
1058
+ return RecordStoreTestHarness().create(gpa, surface);
1059
+ }
1060
+
1061
+ /// Relational-capability counterpart of `create`. `native test`
1062
+ /// receives a private in-memory SQLite database and never touches an
1063
+ /// app-data path.
1064
+ pub fn createWithRelationalStore(gpa: std.mem.Allocator, surface: platform.Surface) !*RelationalStoreTestHarness() {
1065
+ return RelationalStoreTestHarness().create(gpa, surface);
1066
+ }
1067
+
1068
+ /// Credential-capability counterpart of `create`. It enables both
1069
+ /// gates against the NullPlatform's process-local byte store, so
1070
+ /// `native test` never opens a real desktop keychain/keyring.
1071
+ pub fn createWithCredentials(gpa: std.mem.Allocator, surface: platform.Surface) !*Self {
1072
+ const self = try create(gpa, surface);
1073
+ self.runtime.options.credentials_enabled = true;
1074
+ self.runtime.options.security.permissions = &.{security.permission_credentials};
1075
+ return self;
1076
+ }
1077
+
1050
1078
  pub fn destroy(self: *Self, gpa: std.mem.Allocator) void {
1051
1079
  // The harness embeds the runtime's platform: deinit both
1052
1080
  // returns the runtime's heap-owned storage (registered font
@@ -1056,15 +1084,21 @@ pub fn TestHarness() type {
1056
1084
  // tests) can never wake the freed host — the run loop's exit
1057
1085
  // defer for real apps, this line for harness-driven ones.
1058
1086
  self.runtime.deinit();
1087
+ self.null_platform.deinit();
1059
1088
  gpa.destroy(self);
1060
1089
  }
1061
1090
 
1062
1091
  pub fn init(self: *Self, surface: platform.Surface) void {
1092
+ self.initRuntime(surface, null);
1093
+ }
1094
+
1095
+ fn initRuntime(self: *Self, surface: platform.Surface, record_store_binding: ?runtime_effects.RecordStoreBinding) void {
1063
1096
  self.null_platform = platform.NullPlatform.init(surface);
1064
1097
  self.trace_sink = trace.BufferSink.init(&self.trace_records);
1065
1098
  Runtime.initAt(&self.runtime, .{
1066
1099
  .platform = self.null_platform.platform(),
1067
1100
  .trace_sink = self.trace_sink.sink(),
1101
+ .record_store = record_store_binding,
1068
1102
  // On-demand runtime storage (registered font bytes,
1069
1103
  // registered image slot buffers, adopted media-surface
1070
1104
  // texture buffers) routes through
@@ -1095,6 +1129,104 @@ pub fn TestHarness() type {
1095
1129
  };
1096
1130
  }
1097
1131
 
1132
+ /// Capability-opted harness returned by `TestHarness().createWithRecordStore`.
1133
+ /// It is a separate concrete type so ordinary app tests that use
1134
+ /// `TestHarness().create` never analyze or link SQLite merely because Zig's
1135
+ /// test reflection visits the public SDK surface.
1136
+ pub fn RecordStoreTestHarness() type {
1137
+ return struct {
1138
+ const Self = @This();
1139
+
1140
+ null_platform: platform.NullPlatform = platform.NullPlatform.init(.{}),
1141
+ trace_records: [64]trace.Record = undefined,
1142
+ trace_sink: trace.BufferSink = undefined,
1143
+ runtime: Runtime = undefined,
1144
+ record_store: ?runtime_record_store.Store = null,
1145
+
1146
+ pub fn create(gpa: std.mem.Allocator, surface: platform.Surface) !*Self {
1147
+ const self = try gpa.create(Self);
1148
+ errdefer gpa.destroy(self);
1149
+ self.record_store = try runtime_record_store.Store.openMemory(gpa);
1150
+ errdefer if (self.record_store) |*store| store.deinit();
1151
+ self.null_platform = platform.NullPlatform.init(surface);
1152
+ self.trace_sink = trace.BufferSink.init(&self.trace_records);
1153
+ Runtime.initAt(&self.runtime, .{
1154
+ .platform = self.null_platform.platform(),
1155
+ .trace_sink = self.trace_sink.sink(),
1156
+ .record_store = self.record_store.?.binding(),
1157
+ .allocator = if (builtin.is_test) std.testing.allocator else std.heap.page_allocator,
1158
+ .environ = if (builtin.is_test) std.testing.environ else null,
1159
+ });
1160
+ self.runtime.dispatch_error_policy = .propagate;
1161
+ return self;
1162
+ }
1163
+
1164
+ pub fn destroy(self: *Self, gpa: std.mem.Allocator) void {
1165
+ self.runtime.deinit();
1166
+ if (self.record_store) |*store| store.deinit();
1167
+ gpa.destroy(self);
1168
+ }
1169
+
1170
+ pub fn start(self: *Self, app: App) anyerror!void {
1171
+ try self.runtime.dispatchPlatformEvent(app, .app_start);
1172
+ try self.runtime.dispatchPlatformEvent(app, .{ .surface_resized = self.null_platform.surface_value });
1173
+ try self.runtime.dispatchPlatformEvent(app, .frame_requested);
1174
+ }
1175
+
1176
+ pub fn stop(self: *Self, app: App) anyerror!void {
1177
+ try self.runtime.dispatchPlatformEvent(app, .app_shutdown);
1178
+ }
1179
+ };
1180
+ }
1181
+
1182
+ /// Capability-opted harness returned by
1183
+ /// `TestHarness().createWithRelationalStore`.
1184
+ pub fn RelationalStoreTestHarness() type {
1185
+ return struct {
1186
+ const Self = @This();
1187
+
1188
+ null_platform: platform.NullPlatform = platform.NullPlatform.init(.{}),
1189
+ trace_records: [64]trace.Record = undefined,
1190
+ trace_sink: trace.BufferSink = undefined,
1191
+ runtime: Runtime = undefined,
1192
+ relational_store: ?runtime_relational_store.Database = null,
1193
+
1194
+ pub fn create(gpa: std.mem.Allocator, surface: platform.Surface) !*Self {
1195
+ const self = try gpa.create(Self);
1196
+ errdefer gpa.destroy(self);
1197
+ self.relational_store = try runtime_relational_store.Database.openMemory(gpa);
1198
+ errdefer if (self.relational_store) |*database| database.deinit();
1199
+ self.null_platform = platform.NullPlatform.init(surface);
1200
+ self.trace_sink = trace.BufferSink.init(&self.trace_records);
1201
+ Runtime.initAt(&self.runtime, .{
1202
+ .platform = self.null_platform.platform(),
1203
+ .trace_sink = self.trace_sink.sink(),
1204
+ .relational_store = self.relational_store.?.binding(),
1205
+ .allocator = if (builtin.is_test) std.testing.allocator else std.heap.page_allocator,
1206
+ .environ = if (builtin.is_test) std.testing.environ else null,
1207
+ });
1208
+ self.runtime.dispatch_error_policy = .propagate;
1209
+ return self;
1210
+ }
1211
+
1212
+ pub fn destroy(self: *Self, gpa: std.mem.Allocator) void {
1213
+ self.runtime.deinit();
1214
+ if (self.relational_store) |*database| database.deinit();
1215
+ gpa.destroy(self);
1216
+ }
1217
+
1218
+ pub fn start(self: *Self, app: App) anyerror!void {
1219
+ try self.runtime.dispatchPlatformEvent(app, .app_start);
1220
+ try self.runtime.dispatchPlatformEvent(app, .{ .surface_resized = self.null_platform.surface_value });
1221
+ try self.runtime.dispatchPlatformEvent(app, .frame_requested);
1222
+ }
1223
+
1224
+ pub fn stop(self: *Self, app: App) anyerror!void {
1225
+ try self.runtime.dispatchPlatformEvent(app, .app_shutdown);
1226
+ }
1227
+ };
1228
+ }
1229
+
1098
1230
  const testingWriteViewJson = writeViewJson;
1099
1231
  const testingCopyInto = copyInto;
1100
1232
  const testingCanvasWidgetSemanticsById = canvasWidgetSemanticsById;
@@ -0,0 +1,115 @@
1
+ //! Engine-owned credential-store adapter shared by core effects and the
2
+ //! builtin WebView bridge. PlatformServices remains the one OS vtable; this
3
+ //! module fixes the app-facing bounds, app namespace, and closed outcomes so
4
+ //! neither caller grows its own interpretation of keychain errors.
5
+
6
+ const std = @import("std");
7
+ const platform = @import("../platform/root.zig");
8
+
9
+ pub const max_key_bytes: usize = 256;
10
+ pub const max_secret_bytes: usize = platform.max_credential_secret_bytes;
11
+
12
+ pub const Operation = enum(u8) { set, get, delete };
13
+
14
+ pub const Outcome = enum(u8) {
15
+ ok,
16
+ miss,
17
+ denied,
18
+ locked,
19
+ io_failed,
20
+ over_bound,
21
+ rejected,
22
+ };
23
+
24
+ pub const Execution = struct {
25
+ outcome: Outcome,
26
+ len: usize = 0,
27
+ };
28
+
29
+ /// One app-scoped view over the platform credential vtable. `service` is the
30
+ /// stable app identity (app.zon id); `key` becomes the platform account.
31
+ pub const Binding = struct {
32
+ services: *const platform.PlatformServices,
33
+ service: []const u8,
34
+ permitted: bool,
35
+ };
36
+
37
+ pub fn validKey(key: []const u8) bool {
38
+ return key.len > 0 and key.len <= max_key_bytes and
39
+ std.mem.indexOfScalar(u8, key, 0) == null and
40
+ std.unicode.utf8ValidateSlice(key);
41
+ }
42
+
43
+ pub fn outcomeName(outcome: Outcome) []const u8 {
44
+ return @tagName(outcome);
45
+ }
46
+
47
+ pub fn outcomeFromName(name: []const u8) ?Outcome {
48
+ inline for (std.meta.tags(Outcome)) |outcome| {
49
+ if (std.mem.eql(u8, name, @tagName(outcome))) return outcome;
50
+ }
51
+ return null;
52
+ }
53
+
54
+ pub fn execute(binding: Binding, operation: Operation, key: []const u8, secret: []const u8, output: []u8) Execution {
55
+ if (!binding.permitted) return .{ .outcome = .denied };
56
+ if (!validKey(key) or binding.service.len == 0 or binding.service.len > platform.max_credential_service_bytes or
57
+ std.mem.indexOfScalar(u8, binding.service, 0) != null or
58
+ !std.unicode.utf8ValidateSlice(binding.service))
59
+ {
60
+ return .{ .outcome = .over_bound };
61
+ }
62
+ if (operation == .set and secret.len > max_secret_bytes) return .{ .outcome = .over_bound };
63
+ const credential_key: platform.CredentialKey = .{ .service = binding.service, .account = key };
64
+ return switch (operation) {
65
+ .set => blk: {
66
+ binding.services.setCredential(.{
67
+ .service = binding.service,
68
+ .account = key,
69
+ .secret = secret,
70
+ }) catch |err| break :blk .{ .outcome = classifyError(err) };
71
+ break :blk .{ .outcome = .ok };
72
+ },
73
+ .get => blk: {
74
+ const bytes = binding.services.getCredential(credential_key, output) catch |err| {
75
+ if (err == error.CredentialNotFound) break :blk .{ .outcome = .miss };
76
+ break :blk .{ .outcome = classifyError(err) };
77
+ };
78
+ if (bytes.len > max_secret_bytes or bytes.len > output.len) break :blk .{ .outcome = .over_bound };
79
+ break :blk .{ .outcome = .ok, .len = bytes.len };
80
+ },
81
+ .delete => blk: {
82
+ binding.services.deleteCredential(credential_key) catch |err| {
83
+ if (err == error.CredentialNotFound) break :blk .{ .outcome = .miss };
84
+ break :blk .{ .outcome = classifyError(err) };
85
+ };
86
+ break :blk .{ .outcome = .ok };
87
+ },
88
+ };
89
+ }
90
+
91
+ fn classifyError(err: anyerror) Outcome {
92
+ return switch (err) {
93
+ error.CredentialNotFound => .miss,
94
+ error.UnsupportedService => .locked,
95
+ error.InvalidCredentialOptions,
96
+ error.CredentialFieldTooLarge,
97
+ error.NoSpaceLeft,
98
+ => .over_bound,
99
+ error.CredentialStoreLocked => .locked,
100
+ error.CredentialStoreFailed => .io_failed,
101
+ error.AccessDenied,
102
+ error.PermissionDenied,
103
+ => .denied,
104
+ else => .io_failed,
105
+ };
106
+ }
107
+
108
+ test "credential adapter validates keys and names closed outcomes" {
109
+ try std.testing.expect(validKey("token"));
110
+ try std.testing.expect(!validKey(""));
111
+ try std.testing.expect(!validKey("token\x00suffix"));
112
+ try std.testing.expect(!validKey(&.{ 0xff, 0xfe }));
113
+ try std.testing.expectEqualStrings("locked", outcomeName(.locked));
114
+ try std.testing.expectEqual(Outcome.denied, outcomeFromName("denied").?);
115
+ }