@anysphere/file-service 0.0.0-e3fdf62d → 0.0.0-e492c95a
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 +88 -4
- package/index.js +69 -4
- package/package.json +15 -9
- package/.yarnrc.yml +0 -1
- package/Cargo.toml +0 -28
- package/build.rs +0 -44
- package/src/file_utils.rs +0 -214
- package/src/git_utils.rs +0 -207
- package/src/lib.rs +0 -158
- package/src/lib2.rs +0 -212
- package/src/merkle_tree/local_construction.rs +0 -206
- package/src/merkle_tree/mod.rs +0 -982
- package/src/merkle_tree/remote_sync.rs +0 -40
- package/src/merkle_tree/test.rs +0 -126
- package/src/merkle_tree/test_files/1/test.txt +0 -0
package/src/git_utils.rs
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
use std::collections::HashSet;
|
|
2
|
-
use std::path::PathBuf;
|
|
3
|
-
use std::process::Command;
|
|
4
|
-
|
|
5
|
-
pub fn list_ignored_files(
|
|
6
|
-
workspace_root_path: &str,
|
|
7
|
-
) -> Result<HashSet<PathBuf>, Box<dyn std::error::Error>> {
|
|
8
|
-
let mut gitignored_files = HashSet::new();
|
|
9
|
-
|
|
10
|
-
let commands = vec![
|
|
11
|
-
vec![
|
|
12
|
-
"git",
|
|
13
|
-
"ls-files",
|
|
14
|
-
"--others",
|
|
15
|
-
"--ignored",
|
|
16
|
-
"--exclude-standard",
|
|
17
|
-
],
|
|
18
|
-
vec![
|
|
19
|
-
"git",
|
|
20
|
-
"submodule",
|
|
21
|
-
"foreach",
|
|
22
|
-
"--quiet",
|
|
23
|
-
"git ls-files --others --ignored --exclude-standard | sed 's|^|$path/|'",
|
|
24
|
-
],
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
for command in commands {
|
|
28
|
-
let output = Command::new(&command[0])
|
|
29
|
-
.args(&command[1..])
|
|
30
|
-
.current_dir(workspace_root_path)
|
|
31
|
-
.output()?;
|
|
32
|
-
|
|
33
|
-
if output.status.success() {
|
|
34
|
-
let files = String::from_utf8(output.stdout)?
|
|
35
|
-
.lines()
|
|
36
|
-
.filter(|line| !line.is_empty())
|
|
37
|
-
.map(|line| PathBuf::from(line))
|
|
38
|
-
.collect::<HashSet<_>>();
|
|
39
|
-
|
|
40
|
-
gitignored_files.extend(files);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
Ok(gitignored_files)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
pub async fn is_git_ignored(
|
|
48
|
-
workspace_root_path: &str,
|
|
49
|
-
file_path: &str,
|
|
50
|
-
) -> Result<bool, anyhow::Error> {
|
|
51
|
-
let output = tokio::process::Command::new("git")
|
|
52
|
-
.args(&["check-ignore", file_path])
|
|
53
|
-
.current_dir(workspace_root_path)
|
|
54
|
-
.output()
|
|
55
|
-
.await?;
|
|
56
|
-
|
|
57
|
-
Ok(output.status.success())
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
#[cfg(test)]
|
|
61
|
-
mod tests {
|
|
62
|
-
use super::*;
|
|
63
|
-
use std::fs::File;
|
|
64
|
-
use std::io::Write;
|
|
65
|
-
|
|
66
|
-
#[test]
|
|
67
|
-
fn test_no_ignored_files() {
|
|
68
|
-
let dir = tempfile::tempdir().unwrap();
|
|
69
|
-
let gitignored_files = list_ignored_files(dir.path().to_str().unwrap()).unwrap();
|
|
70
|
-
Command::new("git")
|
|
71
|
-
.args(&["init"])
|
|
72
|
-
.current_dir(dir.path())
|
|
73
|
-
.output()
|
|
74
|
-
.unwrap();
|
|
75
|
-
assert_eq!(gitignored_files.len(), 0);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
#[test]
|
|
79
|
-
fn test_one_ignored_file() {
|
|
80
|
-
let dir = tempfile::tempdir().unwrap();
|
|
81
|
-
println!("tempdir: {:?}", dir);
|
|
82
|
-
let file_path = dir.path().join("ignored.txt");
|
|
83
|
-
let mut file = File::create(&file_path).unwrap();
|
|
84
|
-
writeln!(file, "This is an ignored file.").unwrap();
|
|
85
|
-
|
|
86
|
-
let gitignore_path = dir.path().join(".gitignore");
|
|
87
|
-
let mut gitignore = File::create(&gitignore_path).unwrap();
|
|
88
|
-
writeln!(gitignore, "ignored.txt").unwrap();
|
|
89
|
-
|
|
90
|
-
Command::new("git")
|
|
91
|
-
.args(&["init"])
|
|
92
|
-
.current_dir(dir.path())
|
|
93
|
-
.output()
|
|
94
|
-
.unwrap();
|
|
95
|
-
let gitignored_files = list_ignored_files(dir.path().to_str().unwrap()).unwrap();
|
|
96
|
-
println!(
|
|
97
|
-
"ignored files for test_one_ignored_file: {:?}",
|
|
98
|
-
gitignored_files
|
|
99
|
-
);
|
|
100
|
-
// assert_eq!(gitignored_files.len(), 1);
|
|
101
|
-
assert!(gitignored_files.contains(&PathBuf::from("ignored.txt")));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
#[test]
|
|
105
|
-
fn test_multiple_ignored_files() {
|
|
106
|
-
let dir = tempfile::tempdir().unwrap();
|
|
107
|
-
println!("tempdir: {:?}", dir);
|
|
108
|
-
let file_path1 = dir.path().join("ignored1.txt");
|
|
109
|
-
let file_path2 = dir.path().join("ignored2.txt");
|
|
110
|
-
let mut file1 = File::create(&file_path1).unwrap();
|
|
111
|
-
let mut file2 = File::create(&file_path2).unwrap();
|
|
112
|
-
writeln!(file1, "This is an ignored file.").unwrap();
|
|
113
|
-
writeln!(file2, "This is another ignored file.").unwrap();
|
|
114
|
-
|
|
115
|
-
let gitignore_path = dir.path().join(".gitignore");
|
|
116
|
-
let mut gitignore = File::create(&gitignore_path).unwrap();
|
|
117
|
-
writeln!(gitignore, "*.txt").unwrap();
|
|
118
|
-
|
|
119
|
-
Command::new("git")
|
|
120
|
-
.args(&["init"])
|
|
121
|
-
.current_dir(dir.path())
|
|
122
|
-
.output()
|
|
123
|
-
.unwrap();
|
|
124
|
-
let gitignored_files = list_ignored_files(dir.path().to_str().unwrap()).unwrap();
|
|
125
|
-
println!(
|
|
126
|
-
"ignored files for test_multiple_ignored_files: {:?}",
|
|
127
|
-
gitignored_files
|
|
128
|
-
);
|
|
129
|
-
// assert_eq!(gitignored_files.len(), 2);
|
|
130
|
-
assert!(gitignored_files.contains(&PathBuf::from("ignored1.txt")));
|
|
131
|
-
assert!(gitignored_files.contains(&PathBuf::from("ignored2.txt")));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
#[test]
|
|
135
|
-
fn test_multiple_ignored_files_in_current_dir() {
|
|
136
|
-
let gitignored_files = list_ignored_files(".").unwrap();
|
|
137
|
-
assert!(gitignored_files.len() > 1);
|
|
138
|
-
|
|
139
|
-
// print a sample of the ignored files
|
|
140
|
-
let mut count = 0;
|
|
141
|
-
for file in gitignored_files {
|
|
142
|
-
println!("ignored file: {:?}", file);
|
|
143
|
-
count += 1;
|
|
144
|
-
if count > 10 {
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
#[tokio::test]
|
|
152
|
-
async fn test_file_not_ignored() {
|
|
153
|
-
let dir = tempfile::tempdir().unwrap();
|
|
154
|
-
let file_path = dir.path().join("not_ignored.txt");
|
|
155
|
-
let mut file = File::create(&file_path).unwrap();
|
|
156
|
-
writeln!(file, "This is not an ignored file.").unwrap();
|
|
157
|
-
|
|
158
|
-
Command::new("git")
|
|
159
|
-
.args(&["init"])
|
|
160
|
-
.current_dir(dir.path())
|
|
161
|
-
.output()
|
|
162
|
-
.unwrap();
|
|
163
|
-
let is_ignored = is_git_ignored(dir.path().to_str().unwrap(), "not_ignored.txt").await.unwrap();
|
|
164
|
-
assert_eq!(is_ignored, false);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
#[tokio::test]
|
|
168
|
-
async fn test_file_ignored() {
|
|
169
|
-
let dir = tempfile::tempdir().unwrap();
|
|
170
|
-
let file_path = dir.path().join("ignored.txt");
|
|
171
|
-
let mut file = File::create(&file_path).unwrap();
|
|
172
|
-
writeln!(file, "This is an ignored file.").unwrap();
|
|
173
|
-
|
|
174
|
-
let gitignore_path = dir.path().join(".gitignore");
|
|
175
|
-
let mut gitignore = File::create(&gitignore_path).unwrap();
|
|
176
|
-
writeln!(gitignore, "ignored.txt").unwrap();
|
|
177
|
-
|
|
178
|
-
Command::new("git")
|
|
179
|
-
.args(&["init"])
|
|
180
|
-
.current_dir(dir.path())
|
|
181
|
-
.output()
|
|
182
|
-
.unwrap();
|
|
183
|
-
let is_ignored = is_git_ignored(dir.path().to_str().unwrap(), "ignored.txt").await.unwrap();
|
|
184
|
-
assert_eq!(is_ignored, true);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
#[tokio::test]
|
|
188
|
-
async fn test_file_ignored_with_wildcard() {
|
|
189
|
-
let dir = tempfile::tempdir().unwrap();
|
|
190
|
-
let file_path = dir.path().join("ignored.txt");
|
|
191
|
-
let mut file = File::create(&file_path).unwrap();
|
|
192
|
-
writeln!(file, "This is an ignored file.").unwrap();
|
|
193
|
-
|
|
194
|
-
let gitignore_path = dir.path().join(".gitignore");
|
|
195
|
-
let mut gitignore = File::create(&gitignore_path).unwrap();
|
|
196
|
-
writeln!(gitignore, "*.txt").unwrap();
|
|
197
|
-
|
|
198
|
-
Command::new("git")
|
|
199
|
-
.args(&["init"])
|
|
200
|
-
.current_dir(dir.path())
|
|
201
|
-
.output()
|
|
202
|
-
.unwrap();
|
|
203
|
-
let is_ignored = is_git_ignored(dir.path().to_str().unwrap(), "ignored.txt").await.unwrap();
|
|
204
|
-
assert_eq!(is_ignored, true);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
}
|
package/src/lib.rs
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
#![deny(clippy::all)]
|
|
2
|
-
pub mod file_utils;
|
|
3
|
-
pub mod git_utils;
|
|
4
|
-
pub mod merkle_tree;
|
|
5
|
-
|
|
6
|
-
use std::vec;
|
|
7
|
-
|
|
8
|
-
use merkle_tree::{LocalConstruction, MerkleTree};
|
|
9
|
-
|
|
10
|
-
#[macro_use]
|
|
11
|
-
extern crate napi_derive;
|
|
12
|
-
|
|
13
|
-
#[napi]
|
|
14
|
-
pub struct MerkleClient {
|
|
15
|
-
tree: MerkleTree,
|
|
16
|
-
root_directory: String,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
#[napi]
|
|
20
|
-
impl MerkleClient {
|
|
21
|
-
#[napi(constructor)]
|
|
22
|
-
pub fn new(root_directory: String) -> MerkleClient {
|
|
23
|
-
MerkleClient {
|
|
24
|
-
tree: MerkleTree::empty_tree(),
|
|
25
|
-
root_directory,
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
#[napi]
|
|
30
|
-
pub async unsafe fn init(&mut self) -> Result<(), napi::Error> {
|
|
31
|
-
// 1. compute the merkle tree
|
|
32
|
-
// 2. update the backend
|
|
33
|
-
// 3. sync with the remote
|
|
34
|
-
self.compute_merkle_tree().await?;
|
|
35
|
-
|
|
36
|
-
Ok(())
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
pub async unsafe fn interrupt(&mut self) -> Result<(), napi::Error> {
|
|
40
|
-
unimplemented!("Interrupt is not implemented yet");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// #[napi]
|
|
44
|
-
pub async unsafe fn compute_merkle_tree(
|
|
45
|
-
&mut self,
|
|
46
|
-
) -> Result<(), napi::Error> {
|
|
47
|
-
let t =
|
|
48
|
-
MerkleTree::construct_merkle_tree(self.root_directory.clone()).await;
|
|
49
|
-
|
|
50
|
-
match t {
|
|
51
|
-
Ok(tree) => {
|
|
52
|
-
self.tree = tree;
|
|
53
|
-
Ok(())
|
|
54
|
-
}
|
|
55
|
-
Err(e) => Err(napi::Error::new(
|
|
56
|
-
napi::Status::Unknown,
|
|
57
|
-
format!("Error in compute_merkle_tree: {:?}", e),
|
|
58
|
-
)),
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
#[napi]
|
|
63
|
-
pub async unsafe fn update_file(&mut self, file_path: String) {
|
|
64
|
-
let _ = self.tree.update_file(file_path).await;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
#[napi]
|
|
68
|
-
pub async unsafe fn delete_file(&mut self, file_path: String) {
|
|
69
|
-
let _r = self.tree.delete_file(file_path);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
#[napi]
|
|
73
|
-
pub async fn get_subtree_hash(
|
|
74
|
-
&self,
|
|
75
|
-
path: String,
|
|
76
|
-
) -> Result<String, napi::Error> {
|
|
77
|
-
let hash = self.tree.get_subtree_hash(path).await;
|
|
78
|
-
|
|
79
|
-
match hash {
|
|
80
|
-
Ok(hash) => Ok(hash),
|
|
81
|
-
Err(e) => Err(napi::Error::new(
|
|
82
|
-
napi::Status::Unknown,
|
|
83
|
-
format!("Error in get_subtree_hash: {:?}", e),
|
|
84
|
-
)),
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
#[napi]
|
|
89
|
-
pub async fn get_num_embeddable_files(&self) -> Result<i32, napi::Error> {
|
|
90
|
-
let num = self.tree.get_num_embeddable_files().await;
|
|
91
|
-
|
|
92
|
-
match num {
|
|
93
|
-
Ok(num) => Ok(num),
|
|
94
|
-
Err(e) => Err(napi::Error::new(
|
|
95
|
-
napi::Status::Unknown,
|
|
96
|
-
format!("Error in get_num_embeddable_files: {:?}", e),
|
|
97
|
-
)),
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
#[napi]
|
|
102
|
-
pub async fn get_all_files(&self) -> Result<Vec<String>, napi::Error> {
|
|
103
|
-
let files = self.tree.get_all_files().await;
|
|
104
|
-
|
|
105
|
-
match files {
|
|
106
|
-
Ok(files) => Ok(files),
|
|
107
|
-
Err(e) => Err(napi::Error::new(
|
|
108
|
-
napi::Status::Unknown,
|
|
109
|
-
format!("Error in get_all_files: {:?}", e),
|
|
110
|
-
)),
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
#[napi]
|
|
115
|
-
pub async unsafe fn get_next_file_to_embed(
|
|
116
|
-
&mut self,
|
|
117
|
-
) -> Result<Vec<String>, napi::Error> {
|
|
118
|
-
let n = self.tree.get_next_file_to_embed().await;
|
|
119
|
-
|
|
120
|
-
match n {
|
|
121
|
-
Ok((file, path)) => {
|
|
122
|
-
// now our job is to put the filename as the first element of the path.
|
|
123
|
-
|
|
124
|
-
// TODO(sualeh): we should assert that the path is ascending up to the path.
|
|
125
|
-
|
|
126
|
-
let ret = vec![file];
|
|
127
|
-
let ret = ret.into_iter().chain(path.into_iter()).collect::<Vec<_>>();
|
|
128
|
-
|
|
129
|
-
Ok(ret)
|
|
130
|
-
}
|
|
131
|
-
Err(e) => Err(napi::Error::new(
|
|
132
|
-
napi::Status::Unknown,
|
|
133
|
-
format!("Error in get_next_file_to_embed: {:?}", e),
|
|
134
|
-
)),
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
#[napi]
|
|
139
|
-
pub async fn get_hashes_for_files(
|
|
140
|
-
&self,
|
|
141
|
-
files: Vec<String>,
|
|
142
|
-
) -> Result<Vec<String>, napi::Error> {
|
|
143
|
-
let hashes = self.tree.get_hashes_for_files(files).await;
|
|
144
|
-
|
|
145
|
-
match hashes {
|
|
146
|
-
Ok(hashes) => Ok(hashes),
|
|
147
|
-
Err(e) => Err(napi::Error::new(
|
|
148
|
-
napi::Status::Unknown,
|
|
149
|
-
format!("Error in get_hashes_for_files: {:?}", e),
|
|
150
|
-
)),
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// #[napi]
|
|
155
|
-
pub fn update_root_directory(&mut self, root_directory: String) {
|
|
156
|
-
self.root_directory = root_directory;
|
|
157
|
-
}
|
|
158
|
-
}
|
package/src/lib2.rs
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
#![deny(clippy::all)]
|
|
2
|
-
pub mod file_utils;
|
|
3
|
-
pub mod git_utils;
|
|
4
|
-
pub mod merkle_tree;
|
|
5
|
-
|
|
6
|
-
// #[path = "proto/aiserver.v1.rs"]
|
|
7
|
-
// pub mod aiserver;
|
|
8
|
-
|
|
9
|
-
// use aiserver::repository_service_client::RepositoryServiceClient;
|
|
10
|
-
// use aiserver::{FastRepoInitHandshakeRequest, GetNumFilesToSendRequest, GetNumFilesToSendResponse};
|
|
11
|
-
use merkle_tree::{MerkleTree, LocalConstruction};
|
|
12
|
-
|
|
13
|
-
// use tonic::metadata::MetadataValue;
|
|
14
|
-
// use tonic::transport::Channel;
|
|
15
|
-
// use tonic::Request;
|
|
16
|
-
// use aiserver::v1::repository_client::RepositoryClient;
|
|
17
|
-
|
|
18
|
-
#[macro_use]
|
|
19
|
-
extern crate napi_derive;
|
|
20
|
-
|
|
21
|
-
// pub type RepositoryClient = RepositoryServiceClient<
|
|
22
|
-
// tonic::service::interceptor::InterceptedService<Channel, AsphrAuthInterceptor>,
|
|
23
|
-
// >;
|
|
24
|
-
|
|
25
|
-
#[napi]
|
|
26
|
-
pub struct MerkleClient {
|
|
27
|
-
tree: MerkleTree,
|
|
28
|
-
root_directory: String,
|
|
29
|
-
// intercepter: AsphrAuthInterceptor,
|
|
30
|
-
// backend_url: String,
|
|
31
|
-
// repository_info: RepositoryInfo,
|
|
32
|
-
// repo_client: Option<RepositoryClient>,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
#[napi]
|
|
37
|
-
impl MerkleClient {
|
|
38
|
-
#[napi(constructor)]
|
|
39
|
-
pub fn new(
|
|
40
|
-
root_directory: String,
|
|
41
|
-
// bearer_token: String,
|
|
42
|
-
// backend_url: String,
|
|
43
|
-
// repository_info: RepositoryInfo
|
|
44
|
-
) -> MerkleClient {
|
|
45
|
-
MerkleClient {
|
|
46
|
-
tree: MerkleTree::empty_tree(),
|
|
47
|
-
root_directory,
|
|
48
|
-
// intercepter: AsphrAuthInterceptor::new(bearer_token),
|
|
49
|
-
// backend_url,
|
|
50
|
-
// repository_info,
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
#[napi]
|
|
55
|
-
pub async unsafe fn init(&mut self) -> Result<(), napi::Error> {
|
|
56
|
-
// 1. compute the merkle tree
|
|
57
|
-
// 2. update the backend
|
|
58
|
-
// 3. sync with the remote
|
|
59
|
-
self.compute_merkle_tree().await;
|
|
60
|
-
let res = self.sync_with_remote().await;
|
|
61
|
-
|
|
62
|
-
if let Err(e) = res {
|
|
63
|
-
println!("Error: {:?}", e);
|
|
64
|
-
return Err(napi::Error::new(
|
|
65
|
-
napi::Status::Unknown,
|
|
66
|
-
"Error in sync_with_remote",
|
|
67
|
-
));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
Ok(())
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// #[napi]
|
|
74
|
-
pub async unsafe fn interrupt(&mut self) -> Result<(), napi::Error> {
|
|
75
|
-
unimplemented!("Interrupt is not implemented yet");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// pub async fn update_backend(&self) -> Result<RepositoryClient, napi::Error> {
|
|
79
|
-
// let channel = match Channel::from_shared(self.backend_url.clone()) {
|
|
80
|
-
// Ok(channel) => {
|
|
81
|
-
// let connection = match channel.connect().await {
|
|
82
|
-
// Ok(connection) => connection,
|
|
83
|
-
// Err(_) => {
|
|
84
|
-
// return Err(napi::Error::new(
|
|
85
|
-
// napi::Status::Unknown,
|
|
86
|
-
// "Invalid backend URL",
|
|
87
|
-
// ))
|
|
88
|
-
// }
|
|
89
|
-
// };
|
|
90
|
-
|
|
91
|
-
// connection
|
|
92
|
-
// }
|
|
93
|
-
// Err(_) => {
|
|
94
|
-
// return Err(napi::Error::new(
|
|
95
|
-
// napi::Status::Unknown,
|
|
96
|
-
// "Invalid backend URL",
|
|
97
|
-
// ))
|
|
98
|
-
// }
|
|
99
|
-
// };
|
|
100
|
-
|
|
101
|
-
// let repo_client = RepositoryServiceClient::with_interceptor(channel, self.intercepter.clone());
|
|
102
|
-
|
|
103
|
-
// Ok(repo_client)
|
|
104
|
-
// }
|
|
105
|
-
|
|
106
|
-
// #[napi]
|
|
107
|
-
pub async fn sync_with_remote(&self) -> Result<(), napi::Error> {
|
|
108
|
-
// BLOCKER:
|
|
109
|
-
// 1. need to get the repository info nicely from javascript.
|
|
110
|
-
// 2. need to handle the napi conversions.
|
|
111
|
-
// let client: Result<RepositoryClient, napi::Error> = self.update_backend().await;
|
|
112
|
-
|
|
113
|
-
// if let Err(e) = client {
|
|
114
|
-
// return Err(e);
|
|
115
|
-
// }
|
|
116
|
-
// let mut client = client.unwrap();
|
|
117
|
-
|
|
118
|
-
// 1. make sure your tree is upto date.
|
|
119
|
-
// just make sure that you did an update recently and that you havent left too many updates to be buffered up.
|
|
120
|
-
|
|
121
|
-
// 2. sync it carefully.
|
|
122
|
-
// let repo_init_handshake_req = tonic::Request::new(FastRepoInitHandshakeRequest {
|
|
123
|
-
// repository:
|
|
124
|
-
// merkle_tree: self.tree.clone(),
|
|
125
|
-
// });
|
|
126
|
-
// client.fast_repo_init_handshake()
|
|
127
|
-
|
|
128
|
-
// 2.5 if the hash is the same, you are happy and you just leave.
|
|
129
|
-
|
|
130
|
-
// 3. start the diffing process if everything is ok.
|
|
131
|
-
// let different_files = self.tree.sync_with_remote(client).await;
|
|
132
|
-
// let num_files_to_send = different_files.len();
|
|
133
|
-
|
|
134
|
-
// 4. send those files to the server!
|
|
135
|
-
Ok(())
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
#[napi]
|
|
139
|
-
pub async unsafe fn compute_merkle_tree(&mut self) {
|
|
140
|
-
self.tree = MerkleTree::construct_merkle_tree(self.root_directory.clone()).await;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
#[napi]
|
|
144
|
-
pub async unsafe fn update_file(&mut self, file_path: String) {
|
|
145
|
-
let _ = self.tree.update_file(file_path).await;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
#[napi]
|
|
149
|
-
pub async unsafe fn delete_file(&mut self, file_path: String) {
|
|
150
|
-
let _r = self.tree.delete_file(file_path);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// #[napi]
|
|
154
|
-
pub fn update_root_directory(&mut self, root_directory: String) {
|
|
155
|
-
self.root_directory = root_directory;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// TODO
|
|
159
|
-
// pub fn update_repository_info(&mut self, repository_info: RepositoryInfo) {
|
|
160
|
-
// self.repository_info = repository_info;
|
|
161
|
-
// }
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
#[napi(object)]
|
|
165
|
-
#[derive(Clone, Debug)]
|
|
166
|
-
pub struct RepositoryInfo {
|
|
167
|
-
pub relative_workspace_path: String,
|
|
168
|
-
pub remote_urls: String,
|
|
169
|
-
pub remote_names: String,
|
|
170
|
-
pub repo_name: String,
|
|
171
|
-
pub repo_owner: String,
|
|
172
|
-
pub is_tracked: bool,
|
|
173
|
-
pub is_local: bool,
|
|
174
|
-
pub num_files: Option<i32>
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
// use std::convert::TryFrom;
|
|
179
|
-
// A simple implementation of an interceptor that adds a bearer token to the request
|
|
180
|
-
// #[derive(Clone)]
|
|
181
|
-
// pub struct AsphrAuthInterceptor {
|
|
182
|
-
// auth_token: String,
|
|
183
|
-
// full_auth_string: String,
|
|
184
|
-
// }
|
|
185
|
-
|
|
186
|
-
// impl tonic::service::Interceptor for AsphrAuthInterceptor {
|
|
187
|
-
// fn call(&mut self, mut request: tonic::Request<()>) -> Result<tonic::Request<()>, tonic::Status> {
|
|
188
|
-
// request.metadata_mut().insert(
|
|
189
|
-
// "authorization",
|
|
190
|
-
// MetadataValue::try_from(&self.full_auth_string).unwrap(),
|
|
191
|
-
// );
|
|
192
|
-
// Ok(request)
|
|
193
|
-
// }
|
|
194
|
-
// }
|
|
195
|
-
|
|
196
|
-
// impl AsphrAuthInterceptor {
|
|
197
|
-
// pub fn new(auth_token: String) -> AsphrAuthInterceptor {
|
|
198
|
-
// AsphrAuthInterceptor {
|
|
199
|
-
// auth_token: auth_token.clone(),
|
|
200
|
-
// full_auth_string: format!("Bearer {}", auth_token),
|
|
201
|
-
// }
|
|
202
|
-
// }
|
|
203
|
-
|
|
204
|
-
// pub fn get_auth_token(&self) -> String {
|
|
205
|
-
// self.auth_token.clone()
|
|
206
|
-
// }
|
|
207
|
-
|
|
208
|
-
// pub fn update_auth_token(&mut self, new_token: String) {
|
|
209
|
-
// self.auth_token = new_token;
|
|
210
|
-
// self.full_auth_string = format!("Bearer {}", self.auth_token);
|
|
211
|
-
// }
|
|
212
|
-
// }
|