@anysphere/file-service 0.0.0-a397c899 → 0.0.0-a4fe55c1

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.
package/index.d.ts CHANGED
@@ -6,11 +6,15 @@
6
6
  export class MerkleClient {
7
7
  constructor(rootDirectory: string)
8
8
  init(): Promise<void>
9
+ computeMerkleTree(): Promise<void>
9
10
  updateFile(filePath: string): Promise<void>
10
11
  deleteFile(filePath: string): Promise<void>
11
- getSubtreeHash(path: string): Promise<string>
12
+ getSubtreeHash(relativePath: string): Promise<string>
12
13
  getNumEmbeddableFiles(): Promise<number>
13
14
  getAllFiles(): Promise<Array<string>>
15
+ getAllDirFilesToEmbed(absoluteFilePath: string): Promise<Array<string>>
14
16
  getNextFileToEmbed(): Promise<Array<string>>
17
+ getSpline(absoluteFilePath: string): Promise<Array<string>>
15
18
  getHashesForFiles(files: Array<string>): Promise<Array<string>>
19
+ updateRootDirectory(rootDirectory: string): void
16
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anysphere/file-service",
3
- "version": "0.0.0-a397c899",
3
+ "version": "0.0.0-a4fe55c1",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "napi": {
@@ -35,11 +35,11 @@
35
35
  "version": "napi version"
36
36
  },
37
37
  "optionalDependencies": {
38
- "@anysphere/file-service-win32-x64-msvc": "0.0.0-a397c899",
39
- "@anysphere/file-service-darwin-x64": "0.0.0-a397c899",
40
- "@anysphere/file-service-linux-x64-gnu": "0.0.0-a397c899",
41
- "@anysphere/file-service-darwin-arm64": "0.0.0-a397c899",
42
- "@anysphere/file-service-win32-arm64-msvc": "0.0.0-a397c899",
43
- "@anysphere/file-service-darwin-universal": "0.0.0-a397c899"
38
+ "@anysphere/file-service-win32-x64-msvc": "0.0.0-a4fe55c1",
39
+ "@anysphere/file-service-darwin-x64": "0.0.0-a4fe55c1",
40
+ "@anysphere/file-service-linux-x64-gnu": "0.0.0-a4fe55c1",
41
+ "@anysphere/file-service-darwin-arm64": "0.0.0-a4fe55c1",
42
+ "@anysphere/file-service-win32-arm64-msvc": "0.0.0-a4fe55c1",
43
+ "@anysphere/file-service-darwin-universal": "0.0.0-a4fe55c1"
44
44
  }
45
45
  }
package/src/lib.rs CHANGED
@@ -40,7 +40,7 @@ impl MerkleClient {
40
40
  unimplemented!("Interrupt is not implemented yet");
41
41
  }
42
42
 
43
- // #[napi]
43
+ #[napi]
44
44
  pub async unsafe fn compute_merkle_tree(
45
45
  &mut self,
46
46
  ) -> Result<(), napi::Error> {
@@ -72,9 +72,11 @@ impl MerkleClient {
72
72
  #[napi]
73
73
  pub async fn get_subtree_hash(
74
74
  &self,
75
- path: String,
75
+ relative_path: String,
76
76
  ) -> Result<String, napi::Error> {
77
- let hash = self.tree.get_subtree_hash(path).await;
77
+ let absolute_path =
78
+ std::path::Path::new(&self.root_directory).join(relative_path);
79
+ let hash = self.tree.get_subtree_hash(absolute_path).await;
78
80
 
79
81
  match hash {
80
82
  Ok(hash) => Ok(hash),
@@ -98,6 +100,26 @@ impl MerkleClient {
98
100
  }
99
101
  }
100
102
 
103
+ pub async fn get_num_embeddable_files_in_subtree(
104
+ &self,
105
+ relative_path: String,
106
+ ) -> Result<i32, napi::Error> {
107
+ let absolute_path =
108
+ std::path::Path::new(&self.root_directory).join(relative_path);
109
+ let num = self
110
+ .tree
111
+ .get_num_embeddable_files_in_subtree(absolute_path)
112
+ .await;
113
+
114
+ match num {
115
+ Ok(num) => Ok(num),
116
+ Err(e) => Err(napi::Error::new(
117
+ napi::Status::Unknown,
118
+ format!("Error in get_num_embeddable_files_in_subtree: {:?}", e),
119
+ )),
120
+ }
121
+ }
122
+
101
123
  #[napi]
102
124
  pub async fn get_all_files(&self) -> Result<Vec<String>, napi::Error> {
103
125
  let files = self.tree.get_all_files().await;
@@ -111,6 +133,26 @@ impl MerkleClient {
111
133
  }
112
134
  }
113
135
 
136
+ #[napi]
137
+ pub async fn get_all_dir_files_to_embed(
138
+ &self,
139
+ absolute_file_path: String,
140
+ ) -> Result<Vec<String>, napi::Error> {
141
+ let absolute_path_str = absolute_file_path.as_str();
142
+ let files = self
143
+ .tree
144
+ .get_all_dir_files_to_embed(absolute_path_str)
145
+ .await;
146
+
147
+ match files {
148
+ Ok(files) => Ok(files),
149
+ Err(e) => Err(napi::Error::new(
150
+ napi::Status::Unknown,
151
+ format!("Error in get_all_dir_files_to_embed: {:?}", e),
152
+ )),
153
+ }
154
+ }
155
+
114
156
  #[napi]
115
157
  pub async unsafe fn get_next_file_to_embed(
116
158
  &mut self,
@@ -135,6 +177,25 @@ impl MerkleClient {
135
177
  }
136
178
  }
137
179
 
180
+ // FIXME(sualeh): get_spline
181
+ #[napi]
182
+ pub async fn get_spline(
183
+ &self,
184
+ absolute_file_path: String,
185
+ ) -> Result<Vec<String>, napi::Error> {
186
+ // let spline = self.tree.get_spline(absolute_file_path).await;
187
+
188
+ return Ok(vec![]);
189
+
190
+ // match spline {
191
+ // Ok(spline) => Ok(spline),
192
+ // Err(e) => Err(napi::Error::new(
193
+ // napi::Status::Unknown,
194
+ // format!("Error in get_spline: {:?}", e),
195
+ // )),
196
+ // }
197
+ }
198
+
138
199
  #[napi]
139
200
  pub async fn get_hashes_for_files(
140
201
  &self,
@@ -151,7 +212,7 @@ impl MerkleClient {
151
212
  }
152
213
  }
153
214
 
154
- // #[napi]
215
+ #[napi]
155
216
  pub fn update_root_directory(&mut self, root_directory: String) {
156
217
  self.root_directory = root_directory;
157
218
  }
@@ -3,6 +3,7 @@ use crate::merkle_tree::{
3
3
  };
4
4
 
5
5
  use super::{LocalConstruction, MerkleTree};
6
+ use std::collections::BTreeMap;
6
7
  use std::path::PathBuf;
7
8
  use std::{collections::HashMap, path::Path, sync::Arc};
8
9
  use tonic::async_trait;
@@ -39,7 +40,7 @@ impl LocalConstruction for MerkleTree {
39
40
  let root_node = MerkleNode::new(path, None).await;
40
41
  let mut mt = MerkleTree {
41
42
  root: root_node,
42
- files: HashMap::new(),
43
+ files: BTreeMap::new(),
43
44
  root_path: root_directory,
44
45
  cursor: None,
45
46
  };
@@ -48,7 +49,7 @@ impl LocalConstruction for MerkleTree {
48
49
  // TODO(later): i can make this parallel.
49
50
  fn add_nodes_to_hashmap<'a>(
50
51
  node: &'a MerkleNodePtr,
51
- files: &'a mut HashMap<String, File>,
52
+ files: &'a mut BTreeMap<String, File>,
52
53
  ) -> PinnedFuture<'a, ()> {
53
54
  Box::pin(async move {
54
55
  let node_reader = node.read().await;
@@ -1,7 +1,8 @@
1
1
  use super::file_utils;
2
2
  use sha2::Digest;
3
+ use std::collections::BTreeMap;
3
4
  use std::path::PathBuf;
4
- use std::{collections::HashMap, fs, path::Path, sync::Arc};
5
+ use std::{fs, path::Path, sync::Arc};
5
6
  use tokio::sync::RwLock;
6
7
  use tonic::async_trait;
7
8
  pub mod local_construction;
@@ -12,7 +13,7 @@ pub type MerkleNodePtr = Arc<RwLock<MerkleNode>>;
12
13
  pub struct MerkleTree {
13
14
  root_path: String,
14
15
  root: MerkleNodePtr,
15
- files: HashMap<String, File>,
16
+ files: BTreeMap<String, File>,
16
17
  cursor: Option<MerkleNodePtr>,
17
18
  }
18
19
 
@@ -87,7 +88,7 @@ impl MerkleTree {
87
88
  pub fn empty_tree() -> MerkleTree {
88
89
  MerkleTree {
89
90
  root: Arc::new(RwLock::new(MerkleNode::empty_node(None, None))),
90
- files: HashMap::new(),
91
+ files: BTreeMap::new(),
91
92
  root_path: "".to_string(),
92
93
  cursor: None,
93
94
  }
@@ -95,10 +96,18 @@ impl MerkleTree {
95
96
 
96
97
  pub async fn get_subtree_hash(
97
98
  &self,
98
- path: String,
99
+ absolute_path: PathBuf,
99
100
  ) -> Result<String, anyhow::Error> {
100
- let path = PathBuf::from(path);
101
- let node = match self.files.get(path.to_str().unwrap()) {
101
+ let abs_string = match absolute_path.to_str() {
102
+ Some(s) => s.to_string(),
103
+ None => {
104
+ return Err(anyhow::anyhow!(
105
+ "get_subtree_hash: Failed to convert path to string"
106
+ ))
107
+ }
108
+ };
109
+
110
+ let node = match self.files.get(&abs_string) {
102
111
  Some(file) => file.node.clone(),
103
112
  None => {
104
113
  return Err(anyhow::anyhow!("Could not find file in tree!"));
@@ -132,6 +141,43 @@ impl MerkleTree {
132
141
  Ok(count)
133
142
  }
134
143
 
144
+ pub async fn get_num_embeddable_files_in_subtree(
145
+ &self,
146
+ absolute_path: PathBuf,
147
+ ) -> Result<i32, anyhow::Error> {
148
+ let mut count = 0;
149
+
150
+ let absolute_path = match absolute_path.to_str() {
151
+ Some(s) => s.to_string(),
152
+ None => {
153
+ return Err(anyhow::anyhow!(
154
+ "get_num_embeddable_files_in_subtree: Failed to convert path to string"
155
+ ))
156
+ }
157
+ };
158
+
159
+ // TODO(sualeh): worth keeping this list sorted.
160
+
161
+ for (_, file) in &self.files {
162
+ let file_reader = file.node.read().await;
163
+ match &file_reader.node_type {
164
+ NodeType::File(file_name) => {
165
+ if file_name.contains(&absolute_path) {
166
+ count += 1;
167
+ }
168
+ }
169
+ NodeType::Branch(_) => {
170
+ continue;
171
+ }
172
+ NodeType::ErrorNode(_) => {
173
+ continue;
174
+ }
175
+ }
176
+ }
177
+
178
+ Ok(count)
179
+ }
180
+
135
181
  pub async fn get_all_files(&self) -> Result<Vec<String>, anyhow::Error> {
136
182
  let mut files = Vec::new();
137
183
 
@@ -267,6 +313,33 @@ impl MerkleTree {
267
313
  Err(anyhow::anyhow!("Could not find file to embed!"))
268
314
  }
269
315
 
316
+ pub async fn get_all_dir_files_to_embed(
317
+ &self,
318
+ absolute_path: &str,
319
+ ) -> Result<Vec<String>, anyhow::Error> {
320
+ let mut files = Vec::new();
321
+
322
+ for (file_path, f) in &self.files {
323
+ if !file_path.contains(absolute_path) {
324
+ continue;
325
+ }
326
+
327
+ match f.node.read().await.node_type {
328
+ NodeType::File(_) => {
329
+ files.push(file_path.clone());
330
+ }
331
+ NodeType::Branch(_) => {
332
+ continue;
333
+ }
334
+ NodeType::ErrorNode(_) => {
335
+ continue;
336
+ }
337
+ }
338
+ }
339
+
340
+ Ok(files)
341
+ }
342
+
270
343
  /// creates a new node and attaches it to the current tree.
271
344
  /// SPEC:
272
345
  /// - you are allowed to create a file with a node such that the