@h1r-ai/croche-react-native 0.5.4 → 0.6.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 (196) hide show
  1. package/README.md +69 -458
  2. package/THIRD-PARTY-NOTICES +7 -8
  3. package/android/build.gradle +6 -1
  4. package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +125 -0
  5. package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +81 -0
  6. package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +83 -0
  7. package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +45 -824
  8. package/android/src/main/java/dev/croche/reactnative/NativePal.kt +51 -0
  9. package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
  10. package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +42 -41
  11. package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +71 -0
  12. package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +29 -0
  13. package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
  14. package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
  15. package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
  16. package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
  17. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
  18. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +6 -6
  19. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
  20. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +51 -0
  21. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
  22. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
  23. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
  24. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
  25. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +8 -8
  26. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
  27. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
  28. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +121 -0
  29. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
  30. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
  31. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
  32. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +73 -0
  33. package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +9 -0
  34. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +5 -2
  35. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
  36. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +45 -0
  37. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +16 -1
  38. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +60 -105
  39. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +47 -107
  40. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +41 -40
  41. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +67 -0
  42. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +20 -0
  43. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +18 -20
  44. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +322 -0
  45. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +71 -34
  46. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
  47. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +9 -7
  48. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +47 -19
  49. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +5 -5
  50. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +1 -1
  51. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +9 -9
  52. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +108 -37
  53. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +44 -0
  54. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
  55. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +40 -0
  56. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
  57. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +8 -8
  58. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StoragePal.kt +18 -0
  59. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +50 -25
  60. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +22 -22
  61. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +8285 -2159
  62. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
  63. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +25 -0
  64. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
  65. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
  66. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
  67. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +31 -0
  68. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
  69. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
  70. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +128 -0
  71. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +72 -0
  72. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +34 -0
  73. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
  74. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +45 -0
  75. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
  76. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
  77. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +34 -0
  78. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
  79. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
  80. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +79 -0
  81. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
  82. package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
  83. package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
  84. package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
  85. package/dist/package.json +1 -0
  86. package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
  87. package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
  88. package/dist/react-native/src/core-native.d.ts +3 -0
  89. package/dist/react-native/src/core-native.js +193 -0
  90. package/dist/react-native/src/core-wire.d.ts +38 -0
  91. package/dist/react-native/src/core-wire.js +1 -0
  92. package/dist/react-native/src/native-module.d.ts +8 -0
  93. package/dist/react-native/src/native-module.js +53 -0
  94. package/dist/rn-shared-build.json +16 -0
  95. package/dist/runtime/src/agent.d.ts +43 -0
  96. package/dist/runtime/src/agent.js +75 -0
  97. package/dist/runtime/src/app-api.d.ts +284 -0
  98. package/dist/runtime/src/app-api.js +1 -0
  99. package/dist/runtime/src/cancel.d.ts +9 -0
  100. package/dist/runtime/src/cancel.js +24 -0
  101. package/dist/runtime/src/core/port.d.ts +234 -0
  102. package/dist/runtime/src/core/port.js +1 -0
  103. package/dist/runtime/src/core/wasm.d.ts +1 -0
  104. package/dist/runtime/src/core/wasm.js +2 -0
  105. package/dist/runtime/src/croche.d.ts +42 -0
  106. package/dist/runtime/src/croche.js +111 -0
  107. package/dist/runtime/src/errors.d.ts +26 -0
  108. package/dist/runtime/src/errors.js +49 -0
  109. package/dist/runtime/src/events.d.ts +5 -0
  110. package/dist/runtime/src/events.js +31 -0
  111. package/dist/runtime/src/generated/app-stream.d.ts +174 -0
  112. package/dist/runtime/src/generated/app-stream.js +124 -0
  113. package/dist/runtime/src/index.native.d.ts +21 -0
  114. package/dist/runtime/src/index.native.js +8 -0
  115. package/dist/runtime/src/json.d.ts +13 -0
  116. package/dist/runtime/src/json.js +120 -0
  117. package/dist/runtime/src/native-adapter.d.ts +3 -0
  118. package/dist/runtime/src/native-adapter.js +24 -0
  119. package/dist/runtime/src/pal/codec.d.ts +15 -0
  120. package/dist/runtime/src/pal/codec.js +52 -0
  121. package/dist/runtime/src/pal/host.d.ts +5 -0
  122. package/dist/runtime/src/pal/host.js +35 -0
  123. package/dist/runtime/src/pal/platform.d.ts +16 -0
  124. package/dist/runtime/src/pal/platform.js +7 -0
  125. package/dist/runtime/src/rn-entry.d.ts +1 -0
  126. package/dist/runtime/src/rn-entry.js +4 -0
  127. package/dist/runtime/src/run-tool.d.ts +33 -0
  128. package/dist/runtime/src/run-tool.js +7 -0
  129. package/dist/runtime/src/tools.d.ts +19 -0
  130. package/dist/runtime/src/tools.js +70 -0
  131. package/ios/CoreAgentTransport.swift +14 -0
  132. package/ios/CoreDeliveryTransport.swift +58 -0
  133. package/ios/CoreNativePal.swift +93 -0
  134. package/ios/CorePlatformTransport.swift +44 -0
  135. package/ios/CoreWireDispatch.swift +92 -0
  136. package/ios/CoreWireSupport.swift +111 -0
  137. package/ios/CrocheRuntimeBridge.swift +34 -994
  138. package/ios/ProjectPal.swift +46 -0
  139. package/ios/RNCrocheRuntime.mm +18 -201
  140. package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
  141. package/ios/vendor/CrocheRuntimeSDK/Background.swift +11 -11
  142. package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +64 -0
  143. package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
  144. package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
  145. package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +85 -43
  146. package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +443 -0
  147. package/ios/vendor/CrocheRuntimeSDK/Host.swift +122 -61
  148. package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
  149. package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
  150. package/ios/vendor/CrocheRuntimeSDK/JSON.swift +4 -4
  151. package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
  152. package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +37 -36
  153. package/ios/vendor/CrocheRuntimeSDK/Platform.swift +35 -35
  154. package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +285 -0
  155. package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +266 -0
  156. package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +195 -0
  157. package/ios/vendor/CrocheRuntimeSDK/Project.swift +209 -95
  158. package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +97 -123
  159. package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +195 -0
  160. package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
  161. package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +161 -0
  162. package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +234 -0
  163. package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +28 -0
  164. package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +177 -0
  165. package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +61 -0
  166. package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +95 -0
  167. package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +105 -0
  168. package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +209 -0
  169. package/ios/vendor/CrocheRuntimeSDK/Storage.swift +52 -52
  170. package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +180 -137
  171. package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +16 -10
  172. package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +131 -116
  173. package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
  174. package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +7209 -2803
  175. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +1342 -218
  176. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
  177. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +1342 -218
  178. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
  179. package/package.json +18 -8
  180. package/scripts/README.md +11 -0
  181. package/scripts/build-shared.mjs +46 -0
  182. package/scripts/build.mjs +11 -0
  183. package/scripts/vendor-native.sh +5 -4
  184. package/src/NativeCrocheRuntime.ts +12 -159
  185. package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
  186. package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
  187. package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
  188. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
  189. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
  190. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
  191. package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
  192. package/src/index.ts +0 -664
  193. package/src/project-types.ts +0 -35
  194. package/src/project.ts +0 -434
  195. package/src/protocol.ts +0 -28
  196. package/src/types.ts +0 -9
@@ -7,7 +7,7 @@ import Darwin
7
7
 
8
8
  /// acquire must provide exclusive ownership until release. save must atomically replace
9
9
  /// the whole JSON snapshot or throw; it must never be interrupted by cancellation.
10
- public protocol RuntimeStorage: Sendable {
10
+ protocol RuntimeStorage: Sendable {
11
11
  func acquire() throws
12
12
  func load() throws -> JSON
13
13
  func save(_ state: JSON) throws
@@ -21,7 +21,7 @@ public protocol RuntimeStorage: Sendable {
21
21
  /// snapshot, under the same ownership. `load(entry:)` returns `.null` for an entry never saved.
22
22
  /// A storage without entries still works; the Runtime then cannot keep sends that arrive while a
23
23
  /// turn runs, and refuses them (`storage_error`) rather than overwrite the snapshot.
24
- public protocol KeyedRuntimeStorage: RuntimeStorage {
24
+ protocol KeyedRuntimeStorage: RuntimeStorage {
25
25
  func load(entry: String) throws -> JSON
26
26
  func save(entry: String, _ value: JSON) throws
27
27
  /// Deletes an entry for real; absence succeeds.
@@ -40,7 +40,7 @@ public protocol KeyedRuntimeStorage: RuntimeStorage {
40
40
  /// never a mix and never none; `[]` deletes the entry;
41
41
  /// - `remove` deletes for real; an absent entry is not an error;
42
42
  /// - `entries` lists the entries whose names start with `prefix`, with their sizes in bytes.
43
- public protocol ArchiveRuntimeStorage: RuntimeStorage {
43
+ protocol ArchiveRuntimeStorage: RuntimeStorage {
44
44
  func appendLine(entry: String, _ line: String) throws
45
45
  func readLines(entry: String) throws -> [String]
46
46
  func replaceLines(entry: String, _ lines: [String]) throws
@@ -54,77 +54,77 @@ public protocol ArchiveRuntimeStorage: RuntimeStorage {
54
54
  /// is sidecars too (`<file>.archive0001`, JSON lines), under the same lock; a segment is rewritten
55
55
  /// to `<file>.archive0001.tmp-<nonce>` and renamed over it, and stale temporaries are deleted when
56
56
  /// the storage is acquired.
57
- public final class FileRuntimeStorage: KeyedRuntimeStorage, ArchiveRuntimeStorage, @unchecked Sendable {
57
+ final class FileRuntimeStorage: KeyedRuntimeStorage, ArchiveRuntimeStorage, @unchecked Sendable {
58
58
  private let url: URL
59
59
  private let mutex = NSLock()
60
60
  private var descriptor: Int32 = -1
61
61
  /// Runs after a segment's temporary is complete and before the rename (tests inject a crash).
62
62
  var beforeCommit: ((URL) throws -> Void)?
63
- public init(url: URL) { self.url = url }
64
- public func acquire() throws {
63
+ init(url: URL) { self.url = url }
64
+ func acquire() throws {
65
65
  mutex.lock(); defer { mutex.unlock() }
66
- guard descriptor == -1 else { throw CrocheError("store_busy", "Storage is already owned") }
66
+ guard descriptor == -1 else { throw LegacyCrocheError("store_busy", "Storage is already owned") }
67
67
  try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
68
68
  let fd = open(url.path + ".lock", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)
69
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not open storage lock") }
69
+ guard fd >= 0 else { throw LegacyCrocheError("storage_error", "Could not open storage lock") }
70
70
  guard flock(fd, LOCK_EX | LOCK_NB) == 0 else {
71
- _ = close(fd); throw CrocheError("store_busy", "Storage is already owned")
71
+ _ = close(fd); throw LegacyCrocheError("store_busy", "Storage is already owned")
72
72
  }
73
73
  descriptor = fd
74
74
  removeStaleTemporaries()
75
75
  }
76
- public func load() throws -> JSON {
76
+ func load() throws -> JSON {
77
77
  mutex.lock(); defer { mutex.unlock() }
78
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
78
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
79
79
  if !FileManager.default.fileExists(atPath: url.path) { return .null }
80
80
  let saved = try JSON(data: Data(contentsOf: url))
81
- guard saved.object != nil else { throw CrocheError("storage_error", "Existing Runtime snapshot must be an object") }
81
+ guard saved.object != nil else { throw LegacyCrocheError("storage_error", "Existing Runtime snapshot must be an object") }
82
82
  return saved
83
83
  }
84
- public func save(_ state: JSON) throws {
84
+ func save(_ state: JSON) throws {
85
85
  mutex.lock(); defer { mutex.unlock() }
86
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
86
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
87
87
  try state.encoded().write(to: url, options: .atomic)
88
88
  _ = chmod(url.path, S_IRUSR | S_IWUSR)
89
89
  }
90
- public func load(entry: String) throws -> JSON {
90
+ func load(entry: String) throws -> JSON {
91
91
  mutex.lock(); defer { mutex.unlock() }
92
92
  let file = try sidecar(entry)
93
93
  if !FileManager.default.fileExists(atPath: file.path) { return .null }
94
94
  return try JSON(data: Data(contentsOf: file))
95
95
  }
96
- public func save(entry: String, _ value: JSON) throws {
96
+ func save(entry: String, _ value: JSON) throws {
97
97
  mutex.lock(); defer { mutex.unlock() }
98
98
  let file = try sidecar(entry)
99
99
  try value.encoded().write(to: file, options: .atomic)
100
100
  _ = chmod(file.path, S_IRUSR | S_IWUSR)
101
101
  }
102
102
  private func sidecar(_ entry: String) throws -> URL {
103
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
104
- guard !entry.isEmpty, entry.allSatisfy({ $0.isASCII && ($0.isLowercase || $0.isNumber) }) else { throw CrocheError("storage_error", "Invalid storage entry") }
103
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
104
+ guard !entry.isEmpty, entry.allSatisfy({ $0.isASCII && ($0.isLowercase || $0.isNumber) }) else { throw LegacyCrocheError("storage_error", "Invalid storage entry") }
105
105
  return URL(fileURLWithPath: url.path + "." + entry)
106
106
  }
107
107
  // MARK: The turn archive (F20.6)
108
- public func appendLine(entry: String, _ line: String) throws {
108
+ func appendLine(entry: String, _ line: String) throws {
109
109
  mutex.lock(); defer { mutex.unlock() }
110
110
  let file = try segment(entry)
111
- guard !line.contains("\n") else { throw CrocheError("storage_error", "An archive line holds no newline") }
111
+ guard !line.contains("\n") else { throw LegacyCrocheError("storage_error", "An archive line holds no newline") }
112
112
  let fd = open(file.path, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)
113
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not open the archive") }
113
+ guard fd >= 0 else { throw LegacyCrocheError("storage_error", "Could not open the archive") }
114
114
  defer { _ = close(fd) }
115
115
  var info = stat()
116
- guard fstat(fd, &info) == 0 else { throw CrocheError("storage_error", "Could not read the archive") }
116
+ guard fstat(fd, &info) == 0 else { throw LegacyCrocheError("storage_error", "Could not read the archive") }
117
117
  var bytes = [UInt8]()
118
118
  if info.st_size > 0 {
119
119
  var last: UInt8 = 0
120
- guard pread(fd, &last, 1, off_t(info.st_size - 1)) == 1 else { throw CrocheError("storage_error", "Could not read the archive") }
120
+ guard pread(fd, &last, 1, off_t(info.st_size - 1)) == 1 else { throw LegacyCrocheError("storage_error", "Could not read the archive") }
121
121
  if last != 0x0a { bytes.append(0x0a) } // a torn line never swallows this record
122
122
  }
123
123
  bytes += Array(line.utf8); bytes.append(0x0a)
124
124
  try Self.writeAll(fd, bytes)
125
- guard fsync(fd) == 0 else { throw CrocheError("storage_error", "Could not sync the archive") }
125
+ guard fsync(fd) == 0 else { throw LegacyCrocheError("storage_error", "Could not sync the archive") }
126
126
  }
127
- public func readLines(entry: String) throws -> [String] {
127
+ func readLines(entry: String) throws -> [String] {
128
128
  mutex.lock(); defer { mutex.unlock() }
129
129
  let file = try segment(entry)
130
130
  guard FileManager.default.fileExists(atPath: file.path) else { return [] }
@@ -133,33 +133,33 @@ public final class FileRuntimeStorage: KeyedRuntimeStorage, ArchiveRuntimeStorag
133
133
  lines.removeLast() // the piece after the last newline: a torn line, or "" when whole
134
134
  return lines.filter { !$0.isEmpty }
135
135
  }
136
- public func replaceLines(entry: String, _ lines: [String]) throws {
136
+ func replaceLines(entry: String, _ lines: [String]) throws {
137
137
  mutex.lock(); defer { mutex.unlock() }
138
138
  let file = try segment(entry)
139
139
  guard !lines.isEmpty else { return try unlinkIfPresent(file) }
140
- guard !lines.contains(where: { $0.contains("\n") }) else { throw CrocheError("storage_error", "An archive line holds no newline") }
140
+ guard !lines.contains(where: { $0.contains("\n") }) else { throw LegacyCrocheError("storage_error", "An archive line holds no newline") }
141
141
  // '.' and '-' are outside the entry rule: a temporary is never an entry, `held` or `lock`.
142
142
  let nonce = UUID().uuidString.lowercased()
143
143
  let temporary = URL(fileURLWithPath: file.path + ".tmp-" + nonce)
144
144
  let fd = open(temporary.path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)
145
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not write the archive") }
145
+ guard fd >= 0 else { throw LegacyCrocheError("storage_error", "Could not write the archive") }
146
146
  do {
147
147
  defer { _ = close(fd) }
148
148
  try Self.writeAll(fd, Array(lines.map { $0 + "\n" }.joined().utf8))
149
- guard fsync(fd) == 0 else { throw CrocheError("storage_error", "Could not sync the archive") }
149
+ guard fsync(fd) == 0 else { throw LegacyCrocheError("storage_error", "Could not sync the archive") }
150
150
  } catch { _ = unlink(temporary.path); throw error }
151
151
  do {
152
152
  try beforeCommit?(temporary)
153
- guard rename(temporary.path, file.path) == 0 else { throw CrocheError("storage_error", "Could not replace the archive") }
153
+ guard rename(temporary.path, file.path) == 0 else { throw LegacyCrocheError("storage_error", "Could not replace the archive") }
154
154
  } catch { _ = unlink(temporary.path); throw error }
155
155
  }
156
- public func remove(entry: String) throws {
156
+ func remove(entry: String) throws {
157
157
  mutex.lock(); defer { mutex.unlock() }
158
158
  try unlinkIfPresent(try sidecar(entry))
159
159
  }
160
- public func entries(prefix: String) throws -> [String: Int] {
160
+ func entries(prefix: String) throws -> [String: Int] {
161
161
  mutex.lock(); defer { mutex.unlock() }
162
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
162
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
163
163
  var found: [String: Int] = [:]
164
164
  for (name, path) in try siblings() {
165
165
  guard Self.isEntry(name), name.hasPrefix(prefix) else { continue }
@@ -171,7 +171,7 @@ public final class FileRuntimeStorage: KeyedRuntimeStorage, ArchiveRuntimeStorag
171
171
  /// log's (`log` and six digits, S1). Never the snapshot's own sidecars (`held`) or its lock
172
172
  /// file (`lock`).
173
173
  private func segment(_ entry: String) throws -> URL {
174
- guard Self.isSegment(entry) else { throw CrocheError("storage_error", "Invalid archive entry") }
174
+ guard Self.isSegment(entry) else { throw LegacyCrocheError("storage_error", "Invalid archive entry") }
175
175
  return try sidecar(entry)
176
176
  }
177
177
  private static func isSegment(_ entry: Substring) -> Bool {
@@ -199,25 +199,25 @@ public final class FileRuntimeStorage: KeyedRuntimeStorage, ArchiveRuntimeStorag
199
199
  }
200
200
  }
201
201
  private func unlinkIfPresent(_ file: URL) throws {
202
- if unlink(file.path) != 0 && errno != ENOENT { throw CrocheError("storage_error", "Could not delete the archive") }
202
+ if unlink(file.path) != 0 && errno != ENOENT { throw LegacyCrocheError("storage_error", "Could not delete the archive") }
203
203
  }
204
204
  private static func writeAll(_ fd: Int32, _ bytes: [UInt8]) throws {
205
205
  var offset = 0
206
206
  while offset < bytes.count {
207
207
  let written = bytes[offset...].withUnsafeBytes { write(fd, $0.baseAddress, $0.count) }
208
- guard written > 0 else { throw CrocheError("storage_error", "Could not write the archive") }
208
+ guard written > 0 else { throw LegacyCrocheError("storage_error", "Could not write the archive") }
209
209
  offset += written
210
210
  }
211
211
  }
212
212
 
213
- public func remove() throws {
213
+ func remove() throws {
214
214
  try mutex.withLock {
215
- guard descriptor >= 0 else { throw CrocheError("storage_error", "Storage is not owned") }
215
+ guard descriptor >= 0 else { throw LegacyCrocheError("storage_error", "Storage is not owned") }
216
216
  try unlinkIfPresent(url)
217
217
  }
218
218
  }
219
219
 
220
- public func release() {
220
+ func release() {
221
221
  mutex.lock(); defer { mutex.unlock() }
222
222
  if descriptor >= 0 { _ = flock(descriptor, LOCK_UN); _ = close(descriptor); descriptor = -1 }
223
223
  }
@@ -246,12 +246,12 @@ final class ProjectRecordFiles: @unchecked Sendable {
246
246
  static func forOpen(projectDirectory: URL?, hasPrivateStorage: Bool = ProjectRecordFiles.hasPrivateStorage) throws -> ProjectRecordFiles {
247
247
  if hasPrivateStorage {
248
248
  guard projectDirectory == nil else {
249
- throw CrocheError("invalid_configuration", "this platform keeps the project record in the app's private storage")
249
+ throw LegacyCrocheError("invalid_configuration", "this platform keeps the project record in the app's private storage")
250
250
  }
251
251
  return ProjectRecordFiles()
252
252
  }
253
253
  guard let projectDirectory else {
254
- throw CrocheError("storage_error", "This platform requires projectDirectory for the project record")
254
+ throw LegacyCrocheError("storage_error", "This platform requires projectDirectory for the project record")
255
255
  }
256
256
  return ProjectRecordFiles(directory: { projectDirectory })
257
257
  }
@@ -267,34 +267,34 @@ final class ProjectRecordFiles: @unchecked Sendable {
267
267
  let prefix = "croche.project.v1."
268
268
  let hash = key.dropFirst(prefix.count)
269
269
  guard key.hasPrefix(prefix), hash.count == 64, hash.allSatisfy({ $0.isASCII && ($0.isNumber || ("a"..."f").contains(String($0))) }) else {
270
- throw CrocheError("storage_error", "Unsupported project record key")
270
+ throw LegacyCrocheError("storage_error", "Unsupported project record key")
271
271
  }
272
272
  guard let directory = directory() else {
273
- throw CrocheError("storage_error", "Project record needs app-private storage or an explicit projectDirectory")
273
+ throw LegacyCrocheError("storage_error", "Project record needs app-private storage or an explicit projectDirectory")
274
274
  }
275
275
  return directory.appendingPathComponent("croche.project.v1.\(hash).json")
276
276
  }
277
277
  private func io<T>(_ body: () throws -> T) throws -> T {
278
278
  do { return try body() }
279
- catch let error as CrocheError { throw error }
280
- catch { throw CrocheError("storage_error", "Could not access the project record directory") }
279
+ catch let error as LegacyCrocheError { throw error }
280
+ catch { throw LegacyCrocheError("storage_error", "Could not access the project record directory") }
281
281
  }
282
282
  func acquire(_ key: String) throws {
283
283
  beforeLock?(key)
284
284
  try io { try mutex.withLock {
285
- guard descriptors[key] == nil else { throw CrocheError("store_busy", "Project record is already owned") }
285
+ guard descriptors[key] == nil else { throw LegacyCrocheError("store_busy", "Project record is already owned") }
286
286
  let url = try file(key)
287
287
  try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
288
288
  let fd = open(url.path + ".lock", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)
289
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not open project record lock") }
289
+ guard fd >= 0 else { throw LegacyCrocheError("storage_error", "Could not open project record lock") }
290
290
  guard flock(fd, LOCK_EX | LOCK_NB) == 0 else {
291
- _ = close(fd); throw CrocheError("store_busy", "Project record is already owned")
291
+ _ = close(fd); throw LegacyCrocheError("store_busy", "Project record is already owned")
292
292
  }
293
293
  descriptors[key] = fd
294
294
  } }
295
295
  }
296
296
  private func ownedFile(_ key: String) throws -> URL {
297
- guard descriptors[key] != nil else { throw CrocheError("storage_error", "Project record is not owned") }
297
+ guard descriptors[key] != nil else { throw LegacyCrocheError("storage_error", "Project record is not owned") }
298
298
  return try file(key)
299
299
  }
300
300
  func read(_ key: String) throws -> String? {
@@ -311,7 +311,7 @@ final class ProjectRecordFiles: @unchecked Sendable {
311
311
  let url = try ownedFile(key)
312
312
  let temporary = URL(fileURLWithPath: url.path + ".tmp-" + UUID().uuidString)
313
313
  let fd = open(temporary.path, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR)
314
- guard fd >= 0 else { throw CrocheError("storage_error", "Could not write project record") }
314
+ guard fd >= 0 else { throw LegacyCrocheError("storage_error", "Could not write project record") }
315
315
  defer { _ = close(fd); _ = unlink(temporary.path) }
316
316
  let bytes = Array(value.utf8)
317
317
  var offset = 0
@@ -324,11 +324,11 @@ final class ProjectRecordFiles: @unchecked Sendable {
324
324
  #endif
325
325
  }
326
326
  if count < 0 && errno == EINTR { continue }
327
- guard count > 0 else { throw CrocheError("storage_error", "Could not write project record") }
327
+ guard count > 0 else { throw LegacyCrocheError("storage_error", "Could not write project record") }
328
328
  offset += count
329
329
  }
330
330
  guard fsync(fd) == 0, rename(temporary.path, url.path) == 0 else {
331
- throw CrocheError("storage_error", "Could not commit project record")
331
+ throw LegacyCrocheError("storage_error", "Could not commit project record")
332
332
  }
333
333
  } }
334
334
  afterWrite?(key)