@lix-js/sdk 0.6.0-preview.5 → 0.6.1

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 (274) hide show
  1. package/README.md +76 -4
  2. package/dist/errors.d.ts +7 -0
  3. package/dist/errors.js +19 -0
  4. package/dist/index.d.ts +4 -5
  5. package/dist/index.js +3 -3
  6. package/dist/native.d.ts +1 -0
  7. package/dist/native.js +47 -0
  8. package/dist/open-lix.d.ts +38 -207
  9. package/dist/open-lix.js +59 -284
  10. package/dist/result.d.ts +18 -0
  11. package/dist/result.js +48 -0
  12. package/dist/types.d.ts +114 -1
  13. package/dist/value.d.ts +28 -0
  14. package/dist/value.js +245 -0
  15. package/package.json +38 -71
  16. package/SKILL.md +0 -507
  17. package/dist/builtin-schemas.d.ts +0 -1
  18. package/dist/builtin-schemas.js +0 -1
  19. package/dist/engine-wasm/index.d.ts +0 -87
  20. package/dist/engine-wasm/index.js +0 -339
  21. package/dist/engine-wasm/wasm/lix_engine.d.ts +0 -79
  22. package/dist/engine-wasm/wasm/lix_engine.js +0 -833
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -27
  25. package/dist/generated/builtin-schemas.d.ts +0 -427
  26. package/dist/generated/builtin-schemas.js +0 -643
  27. package/dist/sqlite/index.d.ts +0 -12
  28. package/dist/sqlite/index.js +0 -359
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/capabilities.rs +0 -67
  31. package/dist-engine-src/src/backend/conformance/baseline.rs +0 -1127
  32. package/dist-engine-src/src/backend/conformance/factory.rs +0 -93
  33. package/dist-engine-src/src/backend/conformance/failure_tests.rs +0 -608
  34. package/dist-engine-src/src/backend/conformance/fixtures.rs +0 -26
  35. package/dist-engine-src/src/backend/conformance/mod.rs +0 -75
  36. package/dist-engine-src/src/backend/conformance/model.rs +0 -28
  37. package/dist-engine-src/src/backend/conformance/model_based.rs +0 -257
  38. package/dist-engine-src/src/backend/conformance/persistence.rs +0 -204
  39. package/dist-engine-src/src/backend/conformance/projection.rs +0 -21
  40. package/dist-engine-src/src/backend/conformance/pushdown.rs +0 -24
  41. package/dist-engine-src/src/backend/conformance/runner.rs +0 -90
  42. package/dist-engine-src/src/backend/conformance/scan.rs +0 -24
  43. package/dist-engine-src/src/backend/conformance/write.rs +0 -16
  44. package/dist-engine-src/src/backend/error.rs +0 -94
  45. package/dist-engine-src/src/backend/in_memory.rs +0 -670
  46. package/dist-engine-src/src/backend/mod.rs +0 -39
  47. package/dist-engine-src/src/backend/predicate.rs +0 -80
  48. package/dist-engine-src/src/backend/traits.rs +0 -260
  49. package/dist-engine-src/src/backend/types.rs +0 -239
  50. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  51. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  52. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  53. package/dist-engine-src/src/binary_cas/kv.rs +0 -1038
  54. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  55. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  56. package/dist-engine-src/src/branch/context.rs +0 -40
  57. package/dist-engine-src/src/branch/lifecycle.rs +0 -221
  58. package/dist-engine-src/src/branch/mod.rs +0 -13
  59. package/dist-engine-src/src/branch/refs.rs +0 -321
  60. package/dist-engine-src/src/branch/stage_rows.rs +0 -67
  61. package/dist-engine-src/src/branch/types.rs +0 -21
  62. package/dist-engine-src/src/catalog/context.rs +0 -412
  63. package/dist-engine-src/src/catalog/mod.rs +0 -10
  64. package/dist-engine-src/src/catalog/schema.rs +0 -4
  65. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  66. package/dist-engine-src/src/cel/context.rs +0 -86
  67. package/dist-engine-src/src/cel/error.rs +0 -19
  68. package/dist-engine-src/src/cel/mod.rs +0 -8
  69. package/dist-engine-src/src/cel/provider.rs +0 -9
  70. package/dist-engine-src/src/cel/runtime.rs +0 -167
  71. package/dist-engine-src/src/cel/value.rs +0 -50
  72. package/dist-engine-src/src/changelog/bench_support.rs +0 -785
  73. package/dist-engine-src/src/changelog/change.rs +0 -1
  74. package/dist-engine-src/src/changelog/codec.rs +0 -497
  75. package/dist-engine-src/src/changelog/commit.rs +0 -1
  76. package/dist-engine-src/src/changelog/context.rs +0 -1614
  77. package/dist-engine-src/src/changelog/mod.rs +0 -29
  78. package/dist-engine-src/src/changelog/store.rs +0 -163
  79. package/dist-engine-src/src/changelog/test_support.rs +0 -54
  80. package/dist-engine-src/src/changelog/types.rs +0 -213
  81. package/dist-engine-src/src/commit_graph/context.rs +0 -944
  82. package/dist-engine-src/src/commit_graph/mod.rs +0 -9
  83. package/dist-engine-src/src/commit_graph/types.rs +0 -89
  84. package/dist-engine-src/src/commit_graph/walker.rs +0 -786
  85. package/dist-engine-src/src/common/error.rs +0 -347
  86. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  87. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  88. package/dist-engine-src/src/common/identity.rs +0 -145
  89. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  90. package/dist-engine-src/src/common/metadata.rs +0 -40
  91. package/dist-engine-src/src/common/mod.rs +0 -23
  92. package/dist-engine-src/src/common/types.rs +0 -105
  93. package/dist-engine-src/src/common/wire.rs +0 -222
  94. package/dist-engine-src/src/domain.rs +0 -320
  95. package/dist-engine-src/src/engine.rs +0 -203
  96. package/dist-engine-src/src/entity_pk.rs +0 -402
  97. package/dist-engine-src/src/functions/context.rs +0 -296
  98. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  99. package/dist-engine-src/src/functions/mod.rs +0 -18
  100. package/dist-engine-src/src/functions/provider.rs +0 -130
  101. package/dist-engine-src/src/functions/state.rs +0 -335
  102. package/dist-engine-src/src/functions/types.rs +0 -37
  103. package/dist-engine-src/src/init.rs +0 -692
  104. package/dist-engine-src/src/json_store/compression.rs +0 -77
  105. package/dist-engine-src/src/json_store/context.rs +0 -172
  106. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  107. package/dist-engine-src/src/json_store/mod.rs +0 -38
  108. package/dist-engine-src/src/json_store/store.rs +0 -494
  109. package/dist-engine-src/src/json_store/types.rs +0 -212
  110. package/dist-engine-src/src/lib.rs +0 -92
  111. package/dist-engine-src/src/live_state/context.rs +0 -1883
  112. package/dist-engine-src/src/live_state/mod.rs +0 -21
  113. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  114. package/dist-engine-src/src/live_state/reader.rs +0 -23
  115. package/dist-engine-src/src/live_state/types.rs +0 -231
  116. package/dist-engine-src/src/live_state/visibility.rs +0 -666
  117. package/dist-engine-src/src/plugin/archive.rs +0 -438
  118. package/dist-engine-src/src/plugin/component.rs +0 -183
  119. package/dist-engine-src/src/plugin/install.rs +0 -619
  120. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  121. package/dist-engine-src/src/plugin/materializer.rs +0 -202
  122. package/dist-engine-src/src/plugin/mod.rs +0 -33
  123. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -119
  124. package/dist-engine-src/src/plugin/storage.rs +0 -74
  125. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  126. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  127. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  128. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  129. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  130. package/dist-engine-src/src/schema/builtin/lix_branch_descriptor.json +0 -34
  131. package/dist-engine-src/src/schema/builtin/lix_branch_ref.json +0 -48
  132. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  133. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  134. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  135. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  136. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  137. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  138. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  139. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  140. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  141. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  142. package/dist-engine-src/src/schema/builtin/mod.rs +0 -220
  143. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  144. package/dist-engine-src/src/schema/definition.json +0 -187
  145. package/dist-engine-src/src/schema/definition.rs +0 -742
  146. package/dist-engine-src/src/schema/key.rs +0 -138
  147. package/dist-engine-src/src/schema/mod.rs +0 -20
  148. package/dist-engine-src/src/schema/seed.rs +0 -14
  149. package/dist-engine-src/src/schema/tests.rs +0 -780
  150. package/dist-engine-src/src/session/context.rs +0 -1059
  151. package/dist-engine-src/src/session/create_branch.rs +0 -94
  152. package/dist-engine-src/src/session/execute.rs +0 -681
  153. package/dist-engine-src/src/session/merge/analysis.rs +0 -108
  154. package/dist-engine-src/src/session/merge/branch.rs +0 -417
  155. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  156. package/dist-engine-src/src/session/merge/mod.rs +0 -10
  157. package/dist-engine-src/src/session/merge/stats.rs +0 -61
  158. package/dist-engine-src/src/session/mod.rs +0 -30
  159. package/dist-engine-src/src/session/switch_branch.rs +0 -113
  160. package/dist-engine-src/src/session/transaction.rs +0 -557
  161. package/dist-engine-src/src/sql2/bind/classify.rs +0 -102
  162. package/dist-engine-src/src/sql2/bind/error.rs +0 -5
  163. package/dist-engine-src/src/sql2/bind/expr.rs +0 -29
  164. package/dist-engine-src/src/sql2/bind/mod.rs +0 -12
  165. package/dist-engine-src/src/sql2/bind/public_udf.rs +0 -306
  166. package/dist-engine-src/src/sql2/bind/read.rs +0 -65
  167. package/dist-engine-src/src/sql2/bind/statement.rs +0 -2236
  168. package/dist-engine-src/src/sql2/bind/table.rs +0 -273
  169. package/dist-engine-src/src/sql2/bind/write.rs +0 -86
  170. package/dist-engine-src/src/sql2/branch_scope.rs +0 -436
  171. package/dist-engine-src/src/sql2/catalog/capability.rs +0 -20
  172. package/dist-engine-src/src/sql2/catalog/entity_surface.rs +0 -296
  173. package/dist-engine-src/src/sql2/catalog/mod.rs +0 -15
  174. package/dist-engine-src/src/sql2/catalog/registry.rs +0 -556
  175. package/dist-engine-src/src/sql2/catalog/schema.rs +0 -88
  176. package/dist-engine-src/src/sql2/catalog/surface.rs +0 -41
  177. package/dist-engine-src/src/sql2/change_materialization.rs +0 -122
  178. package/dist-engine-src/src/sql2/context.rs +0 -317
  179. package/dist-engine-src/src/sql2/dml.rs +0 -148
  180. package/dist-engine-src/src/sql2/error.rs +0 -215
  181. package/dist-engine-src/src/sql2/exec/bound_public_write.rs +0 -1593
  182. package/dist-engine-src/src/sql2/exec/datafusion.rs +0 -5266
  183. package/dist-engine-src/src/sql2/exec/fast_write.rs +0 -82
  184. package/dist-engine-src/src/sql2/exec/mod.rs +0 -24
  185. package/dist-engine-src/src/sql2/exec/write.rs +0 -661
  186. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1485
  187. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  188. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  189. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  190. package/dist-engine-src/src/sql2/history_route.rs +0 -661
  191. package/dist-engine-src/src/sql2/mod.rs +0 -52
  192. package/dist-engine-src/src/sql2/optimize/datafusion.rs +0 -1
  193. package/dist-engine-src/src/sql2/optimize/mod.rs +0 -2
  194. package/dist-engine-src/src/sql2/optimize/simple_write.rs +0 -116
  195. package/dist-engine-src/src/sql2/parse/mod.rs +0 -69
  196. package/dist-engine-src/src/sql2/parse/normalize.rs +0 -1
  197. package/dist-engine-src/src/sql2/plan/branch_scope.rs +0 -24
  198. package/dist-engine-src/src/sql2/plan/mod.rs +0 -5
  199. package/dist-engine-src/src/sql2/plan/predicate.rs +0 -22
  200. package/dist-engine-src/src/sql2/plan/write.rs +0 -147
  201. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -504
  202. package/dist-engine-src/src/sql2/providers/branch.rs +0 -1206
  203. package/dist-engine-src/src/sql2/providers/change.rs +0 -445
  204. package/dist-engine-src/src/sql2/providers/directory.rs +0 -2422
  205. package/dist-engine-src/src/sql2/providers/directory_history.rs +0 -645
  206. package/dist-engine-src/src/sql2/providers/entity.rs +0 -1484
  207. package/dist-engine-src/src/sql2/providers/entity_history.rs +0 -452
  208. package/dist-engine-src/src/sql2/providers/file.rs +0 -3686
  209. package/dist-engine-src/src/sql2/providers/file_history.rs +0 -924
  210. package/dist-engine-src/src/sql2/providers/history.rs +0 -426
  211. package/dist-engine-src/src/sql2/providers/lix_state.rs +0 -2542
  212. package/dist-engine-src/src/sql2/providers/mod.rs +0 -508
  213. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  214. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  215. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  216. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  217. package/dist-engine-src/src/sql2/session.rs +0 -83
  218. package/dist-engine-src/src/sql2/storage/constraints.rs +0 -1
  219. package/dist-engine-src/src/sql2/storage/mod.rs +0 -1
  220. package/dist-engine-src/src/sql2/test_support/differential.rs +0 -712
  221. package/dist-engine-src/src/sql2/test_support/generators.rs +0 -354
  222. package/dist-engine-src/src/sql2/test_support/mod.rs +0 -2
  223. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  224. package/dist-engine-src/src/sql2/udfs/lix_active_branch_commit_id.rs +0 -53
  225. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  226. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  227. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  228. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  229. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  230. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  231. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  232. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  233. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -86
  234. package/dist-engine-src/src/sql2/write_normalization.rs +0 -368
  235. package/dist-engine-src/src/storage/conformance.rs +0 -399
  236. package/dist-engine-src/src/storage/context.rs +0 -620
  237. package/dist-engine-src/src/storage/mod.rs +0 -52
  238. package/dist-engine-src/src/storage/point.rs +0 -440
  239. package/dist-engine-src/src/storage/read_scope.rs +0 -67
  240. package/dist-engine-src/src/storage/reader.rs +0 -867
  241. package/dist-engine-src/src/storage/scan.rs +0 -784
  242. package/dist-engine-src/src/storage/spaces.rs +0 -236
  243. package/dist-engine-src/src/storage/stats.rs +0 -80
  244. package/dist-engine-src/src/storage/write_set.rs +0 -962
  245. package/dist-engine-src/src/storage_bench.rs +0 -171
  246. package/dist-engine-src/src/test_support.rs +0 -450
  247. package/dist-engine-src/src/tracked_state/bench_support.rs +0 -394
  248. package/dist-engine-src/src/tracked_state/codec.rs +0 -1183
  249. package/dist-engine-src/src/tracked_state/commit_root_rebuild.rs +0 -358
  250. package/dist-engine-src/src/tracked_state/context.rs +0 -2801
  251. package/dist-engine-src/src/tracked_state/diff.rs +0 -2140
  252. package/dist-engine-src/src/tracked_state/merge.rs +0 -478
  253. package/dist-engine-src/src/tracked_state/mod.rs +0 -35
  254. package/dist-engine-src/src/tracked_state/row_materialization.rs +0 -275
  255. package/dist-engine-src/src/tracked_state/storage.rs +0 -427
  256. package/dist-engine-src/src/tracked_state/tree.rs +0 -3063
  257. package/dist-engine-src/src/tracked_state/types.rs +0 -238
  258. package/dist-engine-src/src/transaction/bench_support.rs +0 -407
  259. package/dist-engine-src/src/transaction/commit.rs +0 -1592
  260. package/dist-engine-src/src/transaction/context.rs +0 -1653
  261. package/dist-engine-src/src/transaction/mod.rs +0 -24
  262. package/dist-engine-src/src/transaction/normalization.rs +0 -877
  263. package/dist-engine-src/src/transaction/prep.rs +0 -37
  264. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -163
  265. package/dist-engine-src/src/transaction/staging.rs +0 -1525
  266. package/dist-engine-src/src/transaction/types.rs +0 -403
  267. package/dist-engine-src/src/transaction/validation.rs +0 -5766
  268. package/dist-engine-src/src/untracked_state/codec.rs +0 -615
  269. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  270. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  271. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  272. package/dist-engine-src/src/untracked_state/storage.rs +0 -898
  273. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  274. package/dist-engine-src/src/wasm/mod.rs +0 -60
@@ -1,438 +0,0 @@
1
- use std::collections::{BTreeMap, BTreeSet};
2
- use std::io::{Cursor, Read};
3
- use std::path::{Component, Path};
4
-
5
- use serde_json::Value as JsonValue;
6
- use zip::read::ZipArchive;
7
-
8
- use crate::schema::{schema_key_from_definition, validate_lix_schema_definition};
9
- use crate::LixError;
10
-
11
- use super::{parse_plugin_manifest_json, InstalledPlugin, PluginManifest};
12
-
13
- #[derive(Debug, Clone)]
14
- pub(crate) struct ParsedPluginArchive {
15
- pub manifest: PluginManifest,
16
- pub schemas: Vec<JsonValue>,
17
- }
18
-
19
- pub(crate) fn parse_plugin_archive_for_install(
20
- archive_bytes: &[u8],
21
- ) -> Result<ParsedPluginArchive, LixError> {
22
- let files = read_archive_files_for_install(archive_bytes)?;
23
-
24
- let manifest_bytes = files.get("manifest.json").ok_or_else(|| LixError {
25
- code: "LIX_ERROR_UNKNOWN".to_string(),
26
- message: "Plugin archive must contain manifest.json".to_string(),
27
- hint: None,
28
- details: None,
29
- })?;
30
- let manifest_raw = std::str::from_utf8(manifest_bytes).map_err(|error| LixError {
31
- code: "LIX_ERROR_UNKNOWN".to_string(),
32
- message: format!("Plugin archive manifest.json must be UTF-8: {error}"),
33
- hint: None,
34
- details: None,
35
- })?;
36
- let validated_manifest = parse_plugin_manifest_json(manifest_raw)?;
37
-
38
- let entry_path = normalize_archive_path_for_install(&validated_manifest.manifest.entry)?;
39
- let wasm_bytes = files
40
- .get(&entry_path)
41
- .ok_or_else(|| LixError {
42
- code: "LIX_ERROR_UNKNOWN".to_string(),
43
- message: format!(
44
- "Plugin archive is missing manifest entry file '{}'",
45
- validated_manifest.manifest.entry
46
- ),
47
- hint: None,
48
- details: None,
49
- })?
50
- .clone();
51
- ensure_valid_plugin_wasm_for_install(&wasm_bytes)?;
52
-
53
- let mut schemas = Vec::with_capacity(validated_manifest.manifest.schemas.len());
54
- let mut seen_schema_keys = BTreeSet::<(String, String)>::new();
55
- for schema_path in &validated_manifest.manifest.schemas {
56
- let normalized_schema_path = normalize_archive_path_for_install(schema_path)?;
57
- let schema_bytes = files.get(&normalized_schema_path).ok_or_else(|| LixError {
58
- code: "LIX_ERROR_UNKNOWN".to_string(),
59
- message: format!("Plugin archive is missing schema file '{schema_path}'"),
60
- hint: None,
61
- details: None,
62
- })?;
63
- let schema_json: JsonValue =
64
- serde_json::from_slice(schema_bytes).map_err(|error| LixError {
65
- code: "LIX_ERROR_UNKNOWN".to_string(),
66
- message: format!(
67
- "Plugin archive schema '{schema_path}' is invalid JSON: {error}"
68
- ),
69
- hint: None,
70
- details: None,
71
- })?;
72
- validate_lix_schema_definition(&schema_json)?;
73
- let schema_key = schema_key_from_definition(&schema_json)?;
74
- if !seen_schema_keys.insert(schema_key.schema_key.clone()) {
75
- return Err(LixError {
76
- code: "LIX_ERROR_UNKNOWN".to_string(),
77
- message: format!(
78
- "Plugin archive declares duplicate schema '{}'",
79
- schema_key.schema_key
80
- ),
81
- hint: None,
82
- details: None,
83
- });
84
- }
85
- schemas.push(schema_json);
86
- }
87
-
88
- Ok(ParsedPluginArchive {
89
- manifest: validated_manifest.manifest,
90
- schemas,
91
- })
92
- }
93
-
94
- pub(crate) fn load_installed_plugin_from_archive_bytes(
95
- plugin_key: &str,
96
- archive_path: &str,
97
- archive_bytes: &[u8],
98
- ) -> Result<InstalledPlugin, LixError> {
99
- let files = read_plugin_archive_files(archive_path, archive_bytes)?;
100
- let manifest_bytes = files.get("manifest.json").ok_or_else(|| LixError {
101
- code: "LIX_ERROR_UNKNOWN".to_string(),
102
- message: format!(
103
- "plugin materialization: archive '{}' is missing manifest.json",
104
- archive_path
105
- ),
106
- hint: None,
107
- details: None,
108
- })?;
109
- let manifest_raw = std::str::from_utf8(manifest_bytes).map_err(|error| LixError {
110
- code: "LIX_ERROR_UNKNOWN".to_string(),
111
- message: format!(
112
- "plugin materialization: archive '{}' manifest.json must be UTF-8: {error}",
113
- archive_path
114
- ),
115
- hint: None,
116
- details: None,
117
- })?;
118
- let validated_manifest = parse_plugin_manifest_json(manifest_raw)?;
119
- if validated_manifest.manifest.key != plugin_key {
120
- return Err(LixError {
121
- code: "LIX_ERROR_UNKNOWN".to_string(),
122
- message: format!(
123
- "plugin materialization: archive '{}' key mismatch: path key '{}' vs manifest key '{}'",
124
- archive_path, plugin_key, validated_manifest.manifest.key
125
- ),
126
- hint: None,
127
- details: None,
128
- });
129
- }
130
-
131
- let entry_path =
132
- normalize_plugin_archive_path_for_materialization(&validated_manifest.manifest.entry)?;
133
- let wasm = files.get(&entry_path).ok_or_else(|| LixError {
134
- code: "LIX_ERROR_UNKNOWN".to_string(),
135
- message: format!(
136
- "plugin materialization: archive '{}' is missing entry file '{}'",
137
- archive_path, validated_manifest.manifest.entry
138
- ),
139
- hint: None,
140
- details: None,
141
- })?;
142
- ensure_valid_plugin_wasm_for_materialization(wasm)?;
143
-
144
- let manifest = validated_manifest.manifest;
145
- let content_type = manifest.file_match.content_type;
146
-
147
- Ok(InstalledPlugin {
148
- key: manifest.key,
149
- runtime: manifest.runtime,
150
- api_version: manifest.api_version,
151
- path_glob: manifest.file_match.path_glob,
152
- content_type,
153
- entry: manifest.entry,
154
- manifest_json: validated_manifest.normalized_json,
155
- wasm: wasm.clone(),
156
- })
157
- }
158
-
159
- fn read_archive_files_for_install(
160
- archive_bytes: &[u8],
161
- ) -> Result<BTreeMap<String, Vec<u8>>, LixError> {
162
- if archive_bytes.is_empty() {
163
- return Err(LixError {
164
- code: "LIX_ERROR_UNKNOWN".to_string(),
165
- message: "Plugin archive bytes must not be empty".to_string(),
166
- hint: None,
167
- details: None,
168
- });
169
- }
170
-
171
- let mut archive = ZipArchive::new(Cursor::new(archive_bytes)).map_err(|error| LixError {
172
- code: "LIX_ERROR_UNKNOWN".to_string(),
173
- message: format!("Plugin archive is not a valid zip file: {error}"),
174
- hint: None,
175
- details: None,
176
- })?;
177
- let mut files = BTreeMap::<String, Vec<u8>>::new();
178
-
179
- for index in 0..archive.len() {
180
- let mut entry = archive.by_index(index).map_err(|error| LixError {
181
- code: "LIX_ERROR_UNKNOWN".to_string(),
182
- message: format!("Failed to read plugin archive entry at index {index}: {error}"),
183
- hint: None,
184
- details: None,
185
- })?;
186
- let raw_name = entry.name().to_string();
187
-
188
- if entry.is_dir() {
189
- continue;
190
- }
191
- if is_symlink_mode(entry.unix_mode()) {
192
- return Err(LixError {
193
- code: "LIX_ERROR_UNKNOWN".to_string(),
194
- message: format!("Plugin archive entry '{raw_name}' must not be a symlink"),
195
- hint: None,
196
- details: None,
197
- });
198
- }
199
-
200
- let normalized_path = normalize_archive_path_for_install(&raw_name)?;
201
- let mut bytes = Vec::new();
202
- entry.read_to_end(&mut bytes).map_err(|error| LixError {
203
- code: "LIX_ERROR_UNKNOWN".to_string(),
204
- message: format!("Failed to read plugin archive entry '{raw_name}': {error}"),
205
- hint: None,
206
- details: None,
207
- })?;
208
- if files.insert(normalized_path.clone(), bytes).is_some() {
209
- return Err(LixError {
210
- code: "LIX_ERROR_UNKNOWN".to_string(),
211
- message: format!("Plugin archive contains duplicate entry '{normalized_path}'"),
212
- hint: None,
213
- details: None,
214
- });
215
- }
216
- }
217
-
218
- Ok(files)
219
- }
220
-
221
- fn read_plugin_archive_files(
222
- archive_path: &str,
223
- archive_bytes: &[u8],
224
- ) -> Result<BTreeMap<String, Vec<u8>>, LixError> {
225
- if archive_bytes.is_empty() {
226
- return Err(LixError {
227
- code: "LIX_ERROR_UNKNOWN".to_string(),
228
- message: format!(
229
- "plugin materialization: archive '{}' is empty",
230
- archive_path
231
- ),
232
- hint: None,
233
- details: None,
234
- });
235
- }
236
-
237
- let mut archive = ZipArchive::new(Cursor::new(archive_bytes)).map_err(|error| LixError {
238
- code: "LIX_ERROR_UNKNOWN".to_string(),
239
- message: format!(
240
- "plugin materialization: archive '{}' is not a valid zip file: {error}",
241
- archive_path
242
- ),
243
- hint: None,
244
- details: None,
245
- })?;
246
- let mut files = BTreeMap::<String, Vec<u8>>::new();
247
-
248
- for index in 0..archive.len() {
249
- let mut entry = archive.by_index(index).map_err(|error| LixError {
250
- code: "LIX_ERROR_UNKNOWN".to_string(),
251
- message: format!(
252
- "plugin materialization: failed to read archive '{}' entry index {}: {error}",
253
- archive_path, index
254
- ),
255
- hint: None,
256
- details: None,
257
- })?;
258
-
259
- let entry_name = entry.name().to_string();
260
- let normalized_path = normalize_plugin_archive_path_for_materialization(&entry_name)?;
261
- if normalized_path.ends_with('/') {
262
- continue;
263
- }
264
-
265
- let mut bytes = Vec::new();
266
- entry.read_to_end(&mut bytes).map_err(|error| LixError {
267
- code: "LIX_ERROR_UNKNOWN".to_string(),
268
- message: format!(
269
- "plugin materialization: failed to read archive '{}' entry '{}': {error}",
270
- archive_path, entry_name
271
- ),
272
- hint: None,
273
- details: None,
274
- })?;
275
- files.insert(normalized_path, bytes);
276
- }
277
-
278
- Ok(files)
279
- }
280
-
281
- fn normalize_archive_path_for_install(path: &str) -> Result<String, LixError> {
282
- if path.is_empty() {
283
- return Err(LixError {
284
- code: "LIX_ERROR_UNKNOWN".to_string(),
285
- message: "Plugin archive path must not be empty".to_string(),
286
- hint: None,
287
- details: None,
288
- });
289
- }
290
- if path.starts_with('/') || path.starts_with('\\') {
291
- return Err(LixError {
292
- code: "LIX_ERROR_UNKNOWN".to_string(),
293
- message: format!("Plugin archive path '{path}' must be relative"),
294
- hint: None,
295
- details: None,
296
- });
297
- }
298
- if path.contains('\\') {
299
- return Err(LixError {
300
- code: "LIX_ERROR_UNKNOWN".to_string(),
301
- message: format!("Plugin archive path '{path}' must use forward slash separators"),
302
- hint: None,
303
- details: None,
304
- });
305
- }
306
-
307
- let mut segments = Vec::<String>::new();
308
- for component in Path::new(path).components() {
309
- match component {
310
- Component::Normal(value) => {
311
- let segment = value.to_str().ok_or_else(|| LixError {
312
- code: "LIX_ERROR_UNKNOWN".to_string(),
313
- message: format!(
314
- "Plugin archive path '{path}' contains non-UTF-8 components"
315
- ),
316
- hint: None,
317
- details: None,
318
- })?;
319
- if segment.is_empty() {
320
- return Err(LixError {
321
- code: "LIX_ERROR_UNKNOWN".to_string(),
322
- message: format!("Plugin archive path '{path}' is invalid"),
323
- hint: None,
324
- details: None,
325
- });
326
- }
327
- segments.push(segment.to_string());
328
- }
329
- Component::CurDir | Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
330
- return Err(LixError {
331
- code: "LIX_ERROR_UNKNOWN".to_string(),
332
- message: format!(
333
- "Plugin archive path '{path}' must not contain traversal or absolute components"
334
- ),
335
- hint: None,
336
- details: None,
337
- })
338
- }
339
- }
340
- }
341
-
342
- if segments.is_empty() {
343
- return Err(LixError {
344
- code: "LIX_ERROR_UNKNOWN".to_string(),
345
- message: format!("Plugin archive path '{path}' is invalid"),
346
- hint: None,
347
- details: None,
348
- });
349
- }
350
-
351
- Ok(segments.join("/"))
352
- }
353
-
354
- fn normalize_plugin_archive_path_for_materialization(path: &str) -> Result<String, LixError> {
355
- let raw_path = Path::new(path);
356
- if raw_path.is_absolute() {
357
- return Err(LixError {
358
- code: "LIX_ERROR_UNKNOWN".to_string(),
359
- message: format!(
360
- "plugin materialization: archive path '{}' must be relative",
361
- path
362
- ),
363
- hint: None,
364
- details: None,
365
- });
366
- }
367
-
368
- let mut normalized = Vec::new();
369
- for component in raw_path.components() {
370
- match component {
371
- Component::Normal(part) => normalized.push(part.to_string_lossy().to_string()),
372
- Component::CurDir => {}
373
- Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
374
- return Err(LixError {
375
- code: "LIX_ERROR_UNKNOWN".to_string(),
376
- message: format!(
377
- "plugin materialization: archive path '{}' must not escape the archive root",
378
- path
379
- ),
380
- hint: None,
381
- details: None,
382
- });
383
- }
384
- }
385
- }
386
-
387
- if normalized.is_empty() {
388
- return Err(LixError {
389
- code: "LIX_ERROR_UNKNOWN".to_string(),
390
- message: "plugin materialization: archive path must not be empty".to_string(),
391
- hint: None,
392
- details: None,
393
- });
394
- }
395
-
396
- Ok(normalized.join("/"))
397
- }
398
-
399
- fn ensure_valid_plugin_wasm_for_install(wasm_bytes: &[u8]) -> Result<(), LixError> {
400
- if wasm_bytes.is_empty() {
401
- return Err(LixError {
402
- code: "LIX_ERROR_UNKNOWN".to_string(),
403
- message: "Plugin wasm bytes must not be empty".to_string(),
404
- hint: None,
405
- details: None,
406
- });
407
- }
408
- if wasm_bytes.len() < 8 || !wasm_bytes.starts_with(&[0x00, 0x61, 0x73, 0x6d]) {
409
- return Err(LixError {
410
- code: "LIX_ERROR_UNKNOWN".to_string(),
411
- message: "Plugin wasm bytes must start with a valid wasm header".to_string(),
412
- hint: None,
413
- details: None,
414
- });
415
- }
416
- Ok(())
417
- }
418
-
419
- fn ensure_valid_plugin_wasm_for_materialization(bytes: &[u8]) -> Result<(), LixError> {
420
- const WASM_MAGIC: &[u8; 4] = b"\0asm";
421
- if bytes.len() < WASM_MAGIC.len() || &bytes[..WASM_MAGIC.len()] != WASM_MAGIC {
422
- return Err(LixError {
423
- code: "LIX_ERROR_UNKNOWN".to_string(),
424
- message: "plugin materialization: entry file must be a valid WebAssembly module"
425
- .to_string(),
426
- hint: None,
427
- details: None,
428
- });
429
- }
430
-
431
- Ok(())
432
- }
433
-
434
- fn is_symlink_mode(mode: Option<u32>) -> bool {
435
- const MODE_FILE_TYPE_MASK: u32 = 0o170000;
436
- const MODE_SYMLINK: u32 = 0o120000;
437
- mode.is_some_and(|value| (value & MODE_FILE_TYPE_MASK) == MODE_SYMLINK)
438
- }
@@ -1,183 +0,0 @@
1
- use std::sync::Arc;
2
-
3
- use crate::common::LixError;
4
- use crate::wasm::{WasmComponentInstance, WasmLimits, WasmRuntime};
5
-
6
- use super::InstalledPlugin;
7
-
8
- #[derive(Clone)]
9
- pub(crate) struct CachedPluginComponent {
10
- pub(crate) wasm: Vec<u8>,
11
- pub(crate) instance: Arc<dyn WasmComponentInstance>,
12
- }
13
-
14
- const APPLY_CHANGES_EXPORTS: &[&str] = &["apply-changes", "api#apply-changes"];
15
-
16
- pub(crate) trait PluginComponentHost {
17
- fn plugin_component_cache(
18
- &self,
19
- ) -> &std::sync::Mutex<std::collections::BTreeMap<String, CachedPluginComponent>>;
20
-
21
- fn wasm_runtime(&self) -> &Arc<dyn WasmRuntime>;
22
- }
23
-
24
- pub(crate) async fn load_or_init_plugin_component(
25
- host: &impl PluginComponentHost,
26
- plugin: &InstalledPlugin,
27
- ) -> Result<Arc<dyn WasmComponentInstance>, LixError> {
28
- {
29
- let guard = host.plugin_component_cache().lock().map_err(|_| LixError {
30
- code: "LIX_ERROR_UNKNOWN".to_string(),
31
- message: "plugin component cache lock poisoned".to_string(),
32
- hint: None,
33
- details: None,
34
- })?;
35
- if let Some(cached) = guard.get(&plugin.key) {
36
- if cached.wasm == plugin.wasm {
37
- return Ok(cached.instance.clone());
38
- }
39
- }
40
- }
41
-
42
- let initialized = host
43
- .wasm_runtime()
44
- .init_component(plugin.wasm.clone(), WasmLimits::default())
45
- .await?;
46
- let mut guard = host.plugin_component_cache().lock().map_err(|_| LixError {
47
- code: "LIX_ERROR_UNKNOWN".to_string(),
48
- message: "plugin component cache lock poisoned".to_string(),
49
- hint: None,
50
- details: None,
51
- })?;
52
- if let Some(cached) = guard.get(&plugin.key) {
53
- if cached.wasm == plugin.wasm {
54
- return Ok(cached.instance.clone());
55
- }
56
- }
57
- guard.insert(
58
- plugin.key.clone(),
59
- CachedPluginComponent {
60
- wasm: plugin.wasm.clone(),
61
- instance: initialized.clone(),
62
- },
63
- );
64
- Ok(initialized)
65
- }
66
-
67
- pub(crate) async fn apply_changes_with_plugin(
68
- host: &impl PluginComponentHost,
69
- plugin: &InstalledPlugin,
70
- payload: &[u8],
71
- ) -> Result<Vec<u8>, LixError> {
72
- let instance = load_or_init_plugin_component(host, plugin).await?;
73
- invoke_apply_changes_export(instance.as_ref(), payload).await
74
- }
75
-
76
- async fn invoke_apply_changes_export(
77
- instance: &dyn WasmComponentInstance,
78
- payload: &[u8],
79
- ) -> Result<Vec<u8>, LixError> {
80
- let mut errors = Vec::new();
81
- for export in APPLY_CHANGES_EXPORTS {
82
- match instance.call(export, payload).await {
83
- Ok(output) => return Ok(output),
84
- Err(error) => errors.push(format!("{export}: {}", error.message)),
85
- }
86
- }
87
-
88
- Err(LixError {
89
- code: "LIX_ERROR_UNKNOWN".to_string(),
90
- message: format!(
91
- "plugin materialization: failed to call apply-changes export ({})",
92
- errors.join("; ")
93
- ),
94
- hint: None,
95
- details: None,
96
- })
97
- }
98
-
99
- #[cfg(test)]
100
- mod tests {
101
- use super::*;
102
- use crate::plugin::{InstalledPlugin, PluginRuntime};
103
- use crate::wasm::WasmRuntime;
104
- use async_trait::async_trait;
105
- use std::sync::atomic::{AtomicUsize, Ordering};
106
-
107
- struct TestHost {
108
- wasm_runtime: Arc<dyn WasmRuntime>,
109
- plugin_component_cache:
110
- std::sync::Mutex<std::collections::BTreeMap<String, CachedPluginComponent>>,
111
- }
112
-
113
- impl PluginComponentHost for TestHost {
114
- fn plugin_component_cache(
115
- &self,
116
- ) -> &std::sync::Mutex<std::collections::BTreeMap<String, CachedPluginComponent>> {
117
- &self.plugin_component_cache
118
- }
119
-
120
- fn wasm_runtime(&self) -> &Arc<dyn WasmRuntime> {
121
- &self.wasm_runtime
122
- }
123
- }
124
-
125
- #[derive(Default)]
126
- struct CountingRuntime {
127
- init_calls: Arc<AtomicUsize>,
128
- }
129
-
130
- struct NoopComponent;
131
-
132
- #[async_trait(?Send)]
133
- impl WasmRuntime for CountingRuntime {
134
- async fn init_component(
135
- &self,
136
- _bytes: Vec<u8>,
137
- _limits: WasmLimits,
138
- ) -> Result<Arc<dyn WasmComponentInstance>, LixError> {
139
- self.init_calls.fetch_add(1, Ordering::SeqCst);
140
- Ok(Arc::new(NoopComponent))
141
- }
142
- }
143
-
144
- #[async_trait(?Send)]
145
- impl WasmComponentInstance for NoopComponent {
146
- async fn call(&self, _export: &str, _input: &[u8]) -> Result<Vec<u8>, LixError> {
147
- Ok(Vec::new())
148
- }
149
- }
150
-
151
- #[tokio::test]
152
- async fn component_cache_reinitializes_when_same_key_wasm_changes() {
153
- let runtime = Arc::new(CountingRuntime::default());
154
- let host = TestHost {
155
- wasm_runtime: runtime.clone(),
156
- plugin_component_cache: std::sync::Mutex::new(Default::default()),
157
- };
158
- let mut plugin = InstalledPlugin {
159
- key: "k".to_string(),
160
- runtime: PluginRuntime::WasmComponentV1,
161
- api_version: "0.1.0".to_string(),
162
- path_glob: "*.json".to_string(),
163
- content_type: None,
164
- entry: "plugin.wasm".to_string(),
165
- manifest_json: "{}".to_string(),
166
- wasm: vec![1],
167
- };
168
-
169
- load_or_init_plugin_component(&host, &plugin)
170
- .await
171
- .expect("first init should succeed");
172
- load_or_init_plugin_component(&host, &plugin)
173
- .await
174
- .expect("second lookup should reuse cache");
175
- assert_eq!(runtime.init_calls.load(Ordering::SeqCst), 1);
176
-
177
- plugin.wasm = vec![2];
178
- load_or_init_plugin_component(&host, &plugin)
179
- .await
180
- .expect("changed wasm should reinitialize instance");
181
- assert_eq!(runtime.init_calls.load(Ordering::SeqCst), 2);
182
- }
183
- }