@lix-js/sdk 0.6.0-preview.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 (223) 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 +39 -201
  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 +20 -50
  16. package/SKILL.md +0 -506
  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 -821
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -26
  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 -303
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/kv.rs +0 -358
  31. package/dist-engine-src/src/backend/mod.rs +0 -12
  32. package/dist-engine-src/src/backend/testing.rs +0 -658
  33. package/dist-engine-src/src/backend/types.rs +0 -96
  34. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  35. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  36. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  37. package/dist-engine-src/src/binary_cas/kv.rs +0 -1063
  38. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  39. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  40. package/dist-engine-src/src/catalog/context.rs +0 -412
  41. package/dist-engine-src/src/catalog/mod.rs +0 -10
  42. package/dist-engine-src/src/catalog/schema.rs +0 -4
  43. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  44. package/dist-engine-src/src/cel/context.rs +0 -86
  45. package/dist-engine-src/src/cel/error.rs +0 -19
  46. package/dist-engine-src/src/cel/mod.rs +0 -8
  47. package/dist-engine-src/src/cel/provider.rs +0 -9
  48. package/dist-engine-src/src/cel/runtime.rs +0 -167
  49. package/dist-engine-src/src/cel/value.rs +0 -50
  50. package/dist-engine-src/src/commit_graph/context.rs +0 -901
  51. package/dist-engine-src/src/commit_graph/mod.rs +0 -11
  52. package/dist-engine-src/src/commit_graph/types.rs +0 -109
  53. package/dist-engine-src/src/commit_graph/walker.rs +0 -756
  54. package/dist-engine-src/src/commit_store/codec.rs +0 -887
  55. package/dist-engine-src/src/commit_store/context.rs +0 -944
  56. package/dist-engine-src/src/commit_store/materialization.rs +0 -84
  57. package/dist-engine-src/src/commit_store/mod.rs +0 -16
  58. package/dist-engine-src/src/commit_store/storage.rs +0 -600
  59. package/dist-engine-src/src/commit_store/types.rs +0 -215
  60. package/dist-engine-src/src/common/error.rs +0 -313
  61. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  62. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  63. package/dist-engine-src/src/common/identity.rs +0 -145
  64. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  65. package/dist-engine-src/src/common/metadata.rs +0 -40
  66. package/dist-engine-src/src/common/mod.rs +0 -23
  67. package/dist-engine-src/src/common/types.rs +0 -105
  68. package/dist-engine-src/src/common/wire.rs +0 -222
  69. package/dist-engine-src/src/domain.rs +0 -324
  70. package/dist-engine-src/src/engine.rs +0 -225
  71. package/dist-engine-src/src/entity_identity.rs +0 -405
  72. package/dist-engine-src/src/functions/context.rs +0 -292
  73. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  74. package/dist-engine-src/src/functions/mod.rs +0 -18
  75. package/dist-engine-src/src/functions/provider.rs +0 -130
  76. package/dist-engine-src/src/functions/state.rs +0 -336
  77. package/dist-engine-src/src/functions/types.rs +0 -37
  78. package/dist-engine-src/src/init.rs +0 -558
  79. package/dist-engine-src/src/json_store/compression.rs +0 -77
  80. package/dist-engine-src/src/json_store/context.rs +0 -423
  81. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  82. package/dist-engine-src/src/json_store/mod.rs +0 -12
  83. package/dist-engine-src/src/json_store/store.rs +0 -1109
  84. package/dist-engine-src/src/json_store/types.rs +0 -217
  85. package/dist-engine-src/src/lib.rs +0 -62
  86. package/dist-engine-src/src/live_state/context.rs +0 -2019
  87. package/dist-engine-src/src/live_state/mod.rs +0 -15
  88. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  89. package/dist-engine-src/src/live_state/reader.rs +0 -23
  90. package/dist-engine-src/src/live_state/types.rs +0 -222
  91. package/dist-engine-src/src/live_state/visibility.rs +0 -223
  92. package/dist-engine-src/src/plugin/archive.rs +0 -438
  93. package/dist-engine-src/src/plugin/component.rs +0 -183
  94. package/dist-engine-src/src/plugin/install.rs +0 -619
  95. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  96. package/dist-engine-src/src/plugin/materializer.rs +0 -477
  97. package/dist-engine-src/src/plugin/mod.rs +0 -33
  98. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -118
  99. package/dist-engine-src/src/plugin/storage.rs +0 -74
  100. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  101. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  102. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  103. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  104. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  105. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  106. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  107. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  108. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  109. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  110. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  111. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  112. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  113. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  114. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  115. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +0 -34
  116. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +0 -48
  117. package/dist-engine-src/src/schema/builtin/mod.rs +0 -222
  118. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  119. package/dist-engine-src/src/schema/definition.json +0 -187
  120. package/dist-engine-src/src/schema/definition.rs +0 -742
  121. package/dist-engine-src/src/schema/key.rs +0 -138
  122. package/dist-engine-src/src/schema/mod.rs +0 -20
  123. package/dist-engine-src/src/schema/seed.rs +0 -14
  124. package/dist-engine-src/src/schema/tests.rs +0 -780
  125. package/dist-engine-src/src/session/context.rs +0 -404
  126. package/dist-engine-src/src/session/create_version.rs +0 -88
  127. package/dist-engine-src/src/session/execute.rs +0 -541
  128. package/dist-engine-src/src/session/merge/analysis.rs +0 -102
  129. package/dist-engine-src/src/session/merge/apply.rs +0 -23
  130. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  131. package/dist-engine-src/src/session/merge/mod.rs +0 -11
  132. package/dist-engine-src/src/session/merge/stats.rs +0 -65
  133. package/dist-engine-src/src/session/merge/version.rs +0 -427
  134. package/dist-engine-src/src/session/mod.rs +0 -27
  135. package/dist-engine-src/src/session/optimization9_sql2_bench.rs +0 -100
  136. package/dist-engine-src/src/session/switch_version.rs +0 -110
  137. package/dist-engine-src/src/session/transaction.rs +0 -76
  138. package/dist-engine-src/src/sql2/change_provider.rs +0 -331
  139. package/dist-engine-src/src/sql2/classify.rs +0 -174
  140. package/dist-engine-src/src/sql2/context.rs +0 -311
  141. package/dist-engine-src/src/sql2/directory_history_provider.rs +0 -631
  142. package/dist-engine-src/src/sql2/directory_provider.rs +0 -2453
  143. package/dist-engine-src/src/sql2/dml.rs +0 -148
  144. package/dist-engine-src/src/sql2/entity_history_provider.rs +0 -440
  145. package/dist-engine-src/src/sql2/entity_provider.rs +0 -3211
  146. package/dist-engine-src/src/sql2/error.rs +0 -215
  147. package/dist-engine-src/src/sql2/execute.rs +0 -3533
  148. package/dist-engine-src/src/sql2/file_history_provider.rs +0 -910
  149. package/dist-engine-src/src/sql2/file_provider.rs +0 -3679
  150. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1490
  151. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  152. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  153. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  154. package/dist-engine-src/src/sql2/history_provider.rs +0 -412
  155. package/dist-engine-src/src/sql2/history_route.rs +0 -657
  156. package/dist-engine-src/src/sql2/lix_state_provider.rs +0 -2512
  157. package/dist-engine-src/src/sql2/mod.rs +0 -47
  158. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -246
  159. package/dist-engine-src/src/sql2/public_bind/assignment.rs +0 -46
  160. package/dist-engine-src/src/sql2/public_bind/capability.rs +0 -41
  161. package/dist-engine-src/src/sql2/public_bind/dml.rs +0 -172
  162. package/dist-engine-src/src/sql2/public_bind/mod.rs +0 -26
  163. package/dist-engine-src/src/sql2/public_bind/table.rs +0 -168
  164. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  165. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  166. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  167. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  168. package/dist-engine-src/src/sql2/session.rs +0 -132
  169. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  170. package/dist-engine-src/src/sql2/udfs/lix_active_version_commit_id.rs +0 -53
  171. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  172. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  173. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  174. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  175. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  176. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  177. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  178. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  179. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -89
  180. package/dist-engine-src/src/sql2/udfs/public_call.rs +0 -238
  181. package/dist-engine-src/src/sql2/version_provider.rs +0 -1202
  182. package/dist-engine-src/src/sql2/version_scope.rs +0 -394
  183. package/dist-engine-src/src/sql2/write_normalization.rs +0 -345
  184. package/dist-engine-src/src/storage/context.rs +0 -356
  185. package/dist-engine-src/src/storage/mod.rs +0 -14
  186. package/dist-engine-src/src/storage/read_scope.rs +0 -88
  187. package/dist-engine-src/src/storage/types.rs +0 -501
  188. package/dist-engine-src/src/storage_bench.rs +0 -4863
  189. package/dist-engine-src/src/test_support.rs +0 -228
  190. package/dist-engine-src/src/tracked_state/by_file_index.rs +0 -98
  191. package/dist-engine-src/src/tracked_state/codec.rs +0 -2085
  192. package/dist-engine-src/src/tracked_state/context.rs +0 -1867
  193. package/dist-engine-src/src/tracked_state/diff.rs +0 -686
  194. package/dist-engine-src/src/tracked_state/materialization.rs +0 -403
  195. package/dist-engine-src/src/tracked_state/materializer.rs +0 -488
  196. package/dist-engine-src/src/tracked_state/merge.rs +0 -492
  197. package/dist-engine-src/src/tracked_state/mod.rs +0 -32
  198. package/dist-engine-src/src/tracked_state/storage.rs +0 -375
  199. package/dist-engine-src/src/tracked_state/tree.rs +0 -3187
  200. package/dist-engine-src/src/tracked_state/types.rs +0 -231
  201. package/dist-engine-src/src/transaction/commit.rs +0 -1484
  202. package/dist-engine-src/src/transaction/context.rs +0 -1548
  203. package/dist-engine-src/src/transaction/live_state_overlay.rs +0 -35
  204. package/dist-engine-src/src/transaction/mod.rs +0 -13
  205. package/dist-engine-src/src/transaction/normalization.rs +0 -890
  206. package/dist-engine-src/src/transaction/prep.rs +0 -37
  207. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -149
  208. package/dist-engine-src/src/transaction/staging.rs +0 -1731
  209. package/dist-engine-src/src/transaction/types.rs +0 -460
  210. package/dist-engine-src/src/transaction/validation.rs +0 -5830
  211. package/dist-engine-src/src/untracked_state/codec.rs +0 -307
  212. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  213. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  214. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  215. package/dist-engine-src/src/untracked_state/storage.rs +0 -396
  216. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  217. package/dist-engine-src/src/version/context.rs +0 -40
  218. package/dist-engine-src/src/version/lifecycle.rs +0 -221
  219. package/dist-engine-src/src/version/mod.rs +0 -13
  220. package/dist-engine-src/src/version/refs.rs +0 -330
  221. package/dist-engine-src/src/version/stage_rows.rs +0 -67
  222. package/dist-engine-src/src/version/types.rs +0 -21
  223. 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
- }