godprotocol 1.2.22 → 1.2.24

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.
@@ -482,7 +482,10 @@ class Manager {
482
482
 
483
483
  address = `${acc}/${address}`;
484
484
 
485
- let folder = await this.ds.folder(address, { account: acc });
485
+ let folder = await this.ds.folder(address, {
486
+ ...options,
487
+ account: acc,
488
+ });
486
489
 
487
490
  return folder;
488
491
  };
@@ -15,6 +15,7 @@ class Repository {
15
15
  obj.username = this.owner;
16
16
  obj.repo = this.repo;
17
17
  obj._id = `${this.owner}/${this.repo}`;
18
+ obj.branch = this.branch;
18
19
  }
19
20
 
20
21
  return obj;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.2.22",
3
+ "version": "1.2.24",
4
4
  "description": "A distributed computing environment",
5
5
  "main": "Index.js",
6
6
  "scripts": {
package/repos/github.js CHANGED
@@ -1,97 +1,185 @@
1
- import Repository from '../Objects/Repository.js'
1
+ import Repository from "../Objects/Repository.js";
2
2
 
3
- class Github extends Repository{
4
- constructor({ key, username, repo }) {
5
- super('github')
6
-
7
- this.base_url = 'https://api.github.com';
3
+ class Github extends Repository {
4
+ constructor({ key, username, repo, branch }) {
5
+ super("github");
6
+
7
+ this.base_url = "https://api.github.com";
8
8
  this.auth_token = key;
9
9
  this.owner = username;
10
10
  this.repo = repo;
11
+ this.branch = branch || "master";
11
12
  }
12
13
 
13
14
  // Headers for API calls
14
15
  get_headers = () => ({
15
16
  Authorization: `Bearer ${this.auth_token}`,
16
- Accept: 'application/vnd.github.v3+json',
17
+ Accept: "application/vnd.github.v3+json",
17
18
  });
18
19
 
19
20
  // Write (create or update) a file
20
- write_file = async (file_path, content, options={}) => {
21
- const url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
22
- let sha = null, exists;
21
+ write_file = async (file_path, content, options = {}) => {
22
+ let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
23
+ let sha = null,
24
+ exists;
23
25
 
24
26
  // Check if file exists, get its SHA
25
- try{
27
+ try {
26
28
  exists = await this.file_exists(file_path);
27
-
29
+
28
30
  if (exists) {
29
- const res = await fetch(url, { headers: this.get_headers() });
31
+ let res = await fetch(url, { headers: this.get_headers() });
30
32
  if (res.ok) {
31
- const data = await res.json();
33
+ let data = await res.json();
32
34
  sha = data.sha;
33
35
  }
34
36
  }
35
- if (options.just_exists) return exists
36
- }catch(e){
37
- console.log(e.message)
38
- return
37
+ if (options.just_exists) return exists;
38
+ } catch (e) {
39
+ console.log(e.message);
40
+ return;
39
41
  }
40
42
 
41
43
  // Build payload
42
44
  let body;
43
- try{
45
+ try {
44
46
  body = {
45
- message: exists ? 'Update file' : 'Create file',
46
- content: Buffer.from(content).toString('base64'),
47
- ...(sha && { sha }),
48
- };
49
- }catch(e){
47
+ message: exists ? "Update file" : "Create file",
48
+ content: Buffer.from(content).toString("base64"),
49
+ ...(sha && { sha }),
50
+ };
51
+ } catch (e) {}
50
52
 
51
- }
52
-
53
- try{
54
- const response = await fetch(url, {
55
- method: 'PUT',
53
+ try {
54
+ let response = await fetch(url, {
55
+ method: "PUT",
56
56
  headers: this.get_headers(),
57
57
  body: JSON.stringify(body),
58
58
  });
59
59
 
60
60
  return response.json();
61
- }catch(e){
62
- console.log(e.message)
61
+ } catch (e) {
62
+ console.log(e.message);
63
63
  }
64
-
65
64
  };
66
65
 
67
66
  // Read a file
68
67
  read_file = async (file_path) => {
69
- const url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
68
+ let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
70
69
 
71
70
  let response;
72
- try{
73
- response = await fetch(url, { headers: this.get_headers() })
71
+ try {
72
+ response = await fetch(url, { headers: this.get_headers() });
74
73
 
75
74
  if (!response.ok) return null;
76
75
 
77
- const fileData = await response.json();
78
- return Buffer.from(fileData.content, 'base64').toString('utf-8');
79
- }catch(e){
80
- console.log(e.message)
76
+ let fileData = await response.json();
77
+ return Buffer.from(fileData.content, "base64").toString("utf-8");
78
+ } catch (e) {
79
+ console.log(e.message);
81
80
  return;
82
81
  }
83
82
  };
84
83
 
85
84
  // Check if file exists
86
85
  file_exists = async (file_path) => {
87
- const url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
86
+ let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
88
87
  try {
89
- const response = await fetch(url, { headers: this.get_headers() });
88
+ let response = await fetch(url, { headers: this.get_headers() });
90
89
  return response.ok;
91
90
  } catch {
92
91
  return false;
93
92
  }
94
93
  };
94
+
95
+ // --- Delete File ---
96
+ delete_file = async (file_path, message = "Delete file") => {
97
+ let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
98
+
99
+ try {
100
+ let res = await fetch(url, { headers: this.get_headers() });
101
+ if (!res.ok) throw new Error("File not found");
102
+ let data = await res.json();
103
+ let sha = data.sha;
104
+
105
+ let body = { message, sha };
106
+
107
+ let del = await fetch(url, {
108
+ method: "DELETE",
109
+ headers: this.get_headers(),
110
+ body: JSON.stringify(body),
111
+ });
112
+
113
+ if (!del.ok) {
114
+ let err = await del.text();
115
+ throw new Error(`Failed to delete file: ${err}`);
116
+ }
117
+
118
+ return await del.json();
119
+ } catch (e) {
120
+ console.log(e.message);
121
+ return null;
122
+ }
123
+ };
124
+
125
+ delete_folder = async (folder_path, message = "Delete folder") => {
126
+ let headers = this.get_headers();
127
+
128
+ // 1️⃣ Get the latest commit SHA
129
+ let refUrl = `${this.base_url}/repos/${this.owner}/${this.repo}/git/ref/heads/${this.branch}`;
130
+ let refRes = await fetch(refUrl, { headers });
131
+ let refData = await refRes.json();
132
+ let commitSha = refData.object.sha;
133
+
134
+ // 2️⃣ Get the tree of that commit
135
+ let commitUrl = `${this.base_url}/repos/${this.owner}/${this.repo}/git/commits/${commitSha}`;
136
+ let commitRes = await fetch(commitUrl, { headers });
137
+ let commitData = await commitRes.json();
138
+ let treeSha = commitData.tree.sha;
139
+
140
+ // 3️⃣ Get the full tree (recursive)
141
+ let treeUrl = `${this.base_url}/repos/${this.owner}/${this.repo}/git/trees/${treeSha}?recursive=1`;
142
+ let treeRes = await fetch(treeUrl, { headers });
143
+ let treeData = await treeRes.json();
144
+
145
+ // 4️⃣ Filter out the folder you want to delete
146
+ let filtered = treeData.tree.filter((t) => !t.path.startsWith(folder_path));
147
+
148
+ // 5️⃣ Create a new tree without that folder
149
+ let newTreeRes = await fetch(
150
+ `${this.base_url}/repos/${this.owner}/${this.repo}/git/trees`,
151
+ {
152
+ method: "POST",
153
+ headers,
154
+ body: JSON.stringify({ tree: filtered }),
155
+ }
156
+ );
157
+ let newTreeData = await newTreeRes.json();
158
+
159
+ // 6️⃣ Create a new commit pointing to the new tree
160
+ let newCommitRes = await fetch(
161
+ `${this.base_url}/repos/${this.owner}/${this.repo}/git/commits`,
162
+ {
163
+ method: "POST",
164
+ headers,
165
+ body: JSON.stringify({
166
+ message,
167
+ tree: newTreeData.sha,
168
+ parents: [commitSha],
169
+ }),
170
+ }
171
+ );
172
+ let newCommitData = await newCommitRes.json();
173
+
174
+ // 7️⃣ Update the branch ref
175
+ await fetch(refUrl, {
176
+ method: "PATCH",
177
+ headers,
178
+ body: JSON.stringify({ sha: newCommitData.sha }),
179
+ });
180
+
181
+ return { success: true, message: "Folder deleted via new commit" };
182
+ };
95
183
  }
96
184
 
97
185
  export default Github;