godprotocol 1.1.2 → 1.1.31

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/Objects/Oracle.js CHANGED
@@ -1,94 +1,268 @@
1
1
  import fs from "fs";
2
- import GDS from "generalised-datastore";
2
+ import { hash } from "../utils/hash";
3
+
3
4
 
4
5
  class Fs {
5
6
  constructor(oracle) {
6
7
  this.oracle = oracle;
8
+
7
9
  this.fs = fs;
8
10
  this.base = __dirname;
9
11
  }
10
12
 
11
- exists = async (path) => {
12
- this.fs.existsSync(`${this.base}/${path}`);
13
+ is_directory=async path=>{
14
+ if(!await this.exists(path, {pure:true}))return false;
15
+
16
+ try {
17
+ const stats = this.fs.statSync(`${this.base}/${path}`);
18
+ return stats.isDirectory();
19
+ } catch (err) {
20
+ // console.error(`Error checking path: ${err.message}`);
21
+ return false; // Handle error, e.g., path doesn't exist
22
+ }
23
+ }
24
+
25
+ exists = async (path, options={}) => {
26
+ return this.fs.existsSync(`${this.base}/${path}`);
13
27
  };
14
28
 
15
29
  mkdir = async (path) => {
16
30
  path = `${this.base}/${path}`;
17
31
 
18
- !(await this.exists(path)) && this.fs.mkdirSync(path, { recursive: true });
32
+ !(await this.exists(path, {pure:true})) && this.fs.mkdirSync(path, { recursive: true });
19
33
  };
20
34
 
21
- read_file = async (path) => {
22
- path = `${this.base}/${path}`;
23
- try {
24
- if (await this.exists(path)) return this.fs.readFileSync(path);
25
- else return null;
26
- } catch (e) {}
27
- };
35
+ read = async(path)=>{
36
+ let content;
37
+ try{
38
+ content = this.fs.readFileSync(`${this.base}/${path}`, {encoding:'utf-8'})
39
+ content = JSON.parse(content)
40
+ }catch(e){}
28
41
 
29
- write_file = async (path, content) => {
30
- if (typeof content !== "string") content = JSON.stringify(content);
42
+ return content;
43
+ }
31
44
 
32
- this.fs.writeFileSync(`${this.base}/${path}`, content, { recursive: true });
33
- };
45
+ retrieve = async(path, options)=>{
46
+ let config, result;
47
+ if (options.current){
48
+ config = {current: path}
49
+ }else{
50
+ config = await this.read(`${path}/.config`) || {};
51
+
52
+ if (!config.current) {
53
+ if (options.config){
54
+ return
55
+ }
56
+ }
57
+ }
58
+
59
+ if (options.sibling){
60
+ if (config.sibling){
61
+ let res = await this.read(`${config.sibling}`)
62
+ result = res.content
63
+ }
64
+ }else{
65
+ if (config.current){
66
+ let res = await this.read(`${config.current}/.config`)
67
+
68
+ let value = await this.read(`${config.current}/${res.index-1}`)
69
+ if(!value && res.current && !res.current.includes('/')){
70
+ value = await this.read(`${config.current}/${res.current}`)
71
+ }
72
+ if(options.config){
73
+ result = typeof value.content === 'string'? JSON.parse(value.content): value.content;
74
+ }
75
+ }
76
+ }
77
+
78
+ return result
79
+ }
80
+
81
+ write = async(path, content, options={})=>{
82
+ let addr;
83
+ if (options.config){
84
+ await this.mkdir(path);
85
+
86
+ let conf_path = `${path}/.config`
87
+ let hash = await this.oracle.hash(content)
88
+ let conf = await this.read(conf_path) || {index: 0}
89
+ let obj = {content, sibling: conf.current}
90
+ let current = `${path}/${hash}`
91
+ conf.current = current;
92
+ await this.write(conf_path, JSON.stringify(conf))
93
+
94
+ await this.mkdir(current)
95
+ let current_conff = `${current}/.config`
96
+ let conff = await this.read(current_conff) || {index: 0}
97
+ await this.write(`${current}/${conff.index}`, JSON.stringify(obj))
98
+ conff.index++
99
+
100
+ await this.write(current_conff, JSON.stringify(conff))
101
+ addr = current
102
+ }else {
103
+ this.fs.writeFileSync(`${this.base}/${path}`, content)
104
+ addr = path;
105
+ }
106
+
107
+ return addr;
108
+ }
109
+
110
+ store = async(content, {path, type, hash: hash_, is_instance})=>{
111
+ if (!type) type = this.oracle.get_type(content)
112
+
113
+ let result, hash = hash_;
114
+ switch(type){
115
+ case 'array':
116
+ hash = hash || await this.oracle.hash(content)
117
+ let arr_path = `${path}/${hash}`
118
+ await this.mkdir(arr_path)
119
+
120
+ let arr = []
121
+ for (let i=0; i< content.length; i++){
122
+ let item = content[i]
123
+
124
+ let item_path = `${arr_path}/${i}`
125
+ await this.mkdir(item_path)
126
+ item = await this.store(item, {path: item_path})
127
+
128
+ let conf = await this.read(`${item_path}/.config`)
129
+ if(!conf) conf = {index:0}
130
+ else conf.index++
131
+
132
+ await this.write(`${item_path}/${conf.index}`, item)
133
+ await this.write(`${item_path}/.config`, JSON.stringify(conf));
134
+
135
+ arr.push(conf.index)
136
+ }
137
+ let aconf = await this.read(`${path}/.config`) || {}
138
+ let arr_hash = await this.oracle.hash(arr)
139
+ let arr_conf = await this.read(`${arr_path}/.config`) || {index: 0}
140
+ await this.write(`${arr_path}/${arr_hash}`, JSON.stringify({content:arr, type: is_instance? 'instance': type, previous: arr_conf.current, indice: aconf.indice}))
141
+ arr_conf.current = arr_hash
142
+ arr_conf.sibling = aconf.current;
143
+
144
+ await this.write(`${arr_path}/.config`, JSON.stringify(arr_conf));
145
+
146
+ aconf.current = arr_path;
147
+ await this.write(`${path}/.config`, JSON.stringify(aconf))
148
+
149
+ result = arr_path
150
+
151
+ break;
152
+ case 'twain':
153
+ hash = hash || await this.oracle.hash(content)
154
+ let twa_path = `${path}/${hash}`
155
+ await this.mkdir(twa_path)
156
+
157
+ let twa = {}
158
+ for (let prop in content){
159
+ let value = content[prop];
160
+ let p_hash = await this.oracle.hash(prop)
161
+ let pair_path = `${twa_path}/${p_hash}`
162
+ await this.mkdir(pair_path)
163
+ let p_addr = await this.store(prop, {path: pair_path})
164
+ let val_addr = await this.store(value, {path: pair_path})
165
+
166
+ let pair = [p_addr, val_addr]
167
+
168
+ let conf = await this.read(`${pair_path}/.config`)
169
+ if (!conf){
170
+ conf = {index: 0}
171
+ }else if (!Object.keys(conf).includes('index'))conf.index = 0
172
+ else {
173
+ conf.index++
174
+ }
175
+
176
+ await this.write(`${pair_path}/${conf.index}`, JSON.stringify(pair))
177
+
178
+ twa[p_hash] = conf.index;
179
+ await this.write(`${pair_path}/.config`, JSON.stringify(conf));
180
+ }
181
+
182
+ let pconf = await this.read(`${path}/.config`) || {}
183
+
184
+ let twa_hash = await this.oracle.hash(twa)
185
+ let twa_conf = await this.read(`${twa_path}/.config`) || {index: 0}
186
+ await this.write(`${twa_path}/${twa_hash}`, JSON.stringify({content:twa, type: is_instance? 'instance': type, previous: twa_conf.current, indice: pconf.indice}))
187
+ twa_conf.current = twa_hash
188
+ twa_conf.sibling = pconf.current;
189
+
190
+ await this.write(`${twa_path}/.config`, JSON.stringify(twa_conf));
191
+
192
+ pconf.current = twa_path;
193
+ await this.write(`${path}/.config`, JSON.stringify(pconf))
194
+
195
+ result = twa_path
196
+ break;
197
+ case 'string':
198
+ hash = await this.oracle.hash(content)
199
+ result = await this.write(`${path}/${hash}`, content)
200
+ break
201
+ case 'number':
202
+ hash = await this.oracle.hash(`0${content}`)
203
+ result = await this.write(`${path}/${hash}`, JSON.stringify(content))
204
+ break
205
+ case 'boolean':
206
+ hash = await this.oracle.hash(`0${content}`)
207
+ result = await this.write(`${path}/${hash}`, JSON.stringify(content))
208
+ break
209
+ }
210
+
211
+ return result;
212
+ }
34
213
  }
35
214
 
36
215
  class Oracle {
37
216
  constructor(manager) {
217
+
38
218
  this.manager = manager;
39
- this.repository = manager.repository;
40
219
 
41
220
  this.fs = new Fs(this);
42
- this.gds = new GDS(manager.name, { manager });
43
221
  }
44
222
 
45
- folder = async (physical_address, options) => {
46
- return await this.gds.folder(physical_address, options);
47
- };
223
+ read = async(path, options)=>{
224
+ let result = await this.fs.retrieve(path, options)
48
225
 
49
- sync = async () => {
50
- await this.gds.sync();
51
- };
226
+ return result;
227
+ }
52
228
 
53
- read = async (address, account) => {
54
- let content = await this.repository.read_address(address);
229
+ write = async(path, content, options={})=>{
230
+ let type = this.get_type(content), addr;
55
231
 
56
- return content;
57
- };
232
+ if(options.config){
233
+ return await this.fs.write(path, JSON.stringify(content), {config: true})
234
+ }
58
235
 
59
- write = async (payload) => {
60
- await payload.hash();
236
+ if (options.hash) path = `${path}/${options.hash}`
61
237
 
62
- let address = await this.repository.write_file(
63
- payload.path,
64
- payload.content
65
- );
238
+ addr = await this.fs.store(content, {path, type, has_hash: options.hash, is_instance: options.is_instance})
66
239
 
67
- return address;
68
- };
240
+ return addr;
241
+ }
69
242
 
70
- add_server = async ({ path, ds, server }) => {
71
- console.log(path, ds, "here", this.manager.server, server);
72
- if (ds) {
73
- if (this.gds.path === path) {
74
- await this.gds.add_server(server);
243
+ hash = async(value, alg)=>{
244
+ if (typeof value !== 'string')value = JSON.stringify(value)
245
+
246
+ return hash(value, alg)
247
+ }
75
248
 
76
- return { listening: true };
77
- } else return { not_found: true };
78
- }
79
- let folder = await this.gds.get_folder(path);
80
- if (folder) {
81
- await folder.add_server(server);
82
- return { listening: true };
83
- }
84
- if (!folder) {
85
- if (await this.fs.exists(path)) {
86
- return { sleeping: true };
87
- } else {
88
- return { not_found: true };
89
- }
90
- }
249
+ sync = async () => {
250
+ this.manager.path = `${this.manager.trinity.game}/${this.manager.name}`;
251
+ await this.fs.mkdir(this.manager.path)
91
252
  };
253
+
254
+ get_type = (content)=>{
255
+ let type = typeof content;
256
+
257
+ if (content == null) return 'void'
258
+
259
+ if (type === 'object'){
260
+ if (Array.isArray(content)) type = 'array'
261
+ else if (!content) type = 'void'
262
+ else type = 'twain'
263
+ }
264
+ return type;
265
+ }
92
266
  }
93
267
 
94
268
  export default Oracle;
@@ -8,7 +8,7 @@ class Repository {
8
8
  }
9
9
 
10
10
  read_address = async (address) => {
11
- let content = await get_request(`${address}`);
11
+ let content = await this.manager.oracle.fs.read_file(`${address}`);
12
12
 
13
13
  try {
14
14
  content = JSON.parse(content);
@@ -18,49 +18,20 @@ class Repository {
18
18
  };
19
19
 
20
20
  sync = async () => {
21
- this.ds = await this.gds.sync();
22
21
  };
23
22
 
24
23
  create_or_update_file = async (path, content, message, sha) => {
25
- post_request({
26
- data: {
27
- content: Buffer.from(content).toString("base64"),
28
- message: message || `Updating file content - ${Date.now()}`,
29
- sha,
30
- },
31
- options: this.repo_domain,
32
- path: `/repos/${this.manager.username}/${this.manager.repo_name}/contents/${path}`,
33
- });
34
-
35
- console.log("File created/updated successfully:", response.data);
24
+
36
25
  };
37
26
 
38
27
  get_file_sha = async (path) => {
39
- try {
40
- let sha = await post_request({});
41
- return sha;
42
- } catch (error) {
43
- if (error.response && error.response.status === 404) {
44
- console.log("File does not exist; a new file will be created.");
45
- return null; // File doesn't exist, so SHA is not needed
46
- }
47
- throw new Error(`Failed to get file SHA: ${error.message}`);
48
- }
28
+
49
29
  };
50
30
 
51
31
  write_file = async (path, content, message = "") => {
52
- try {
53
- let sha = await this.get_file_sha(path);
54
-
55
- console.log(`File ${path} created/updated successfully.`);
56
- } catch (error) {
57
- console.error("Error creating/updating file:", error);
58
- }
59
-
60
- // Usage
61
- // createOrUpdateFile("username", "my-new-repo", "README.md", "Hello, world!", "Add README");
62
-
63
- return `${this.repo_domain}/${owner}/${path}`;
32
+ await this.manager.oracle.fs.write_file(path, content)
33
+
34
+ return path
64
35
  };
65
36
  }
66
37
 
@@ -1,13 +1,13 @@
1
1
  import Manager from "./Manager";
2
2
 
3
3
  class Trinity {
4
- constructor() {
4
+ constructor(game) {
5
+ this.game = game;
5
6
  this.managers = new Object();
6
7
  }
7
8
 
8
9
  add_manager = async (name, options) => {
9
- console.log(name, options);
10
- let manager = new Manager(name, options);
10
+ let manager = new Manager(name, {...options, trinity: this});
11
11
  let mgrs = this.managers[name];
12
12
  if (!mgrs) {
13
13
  mgrs = new Array();
@@ -0,0 +1,16 @@
1
+ class Utils{
2
+ constructor(){}
3
+
4
+ resolve_addr = (addr)=>{
5
+ if (!addr)return addr;
6
+
7
+ if (!addr.startsWith(this.physical_address)){
8
+ addr = `${this.physical_address}/${addr}`
9
+ }
10
+
11
+ return addr;
12
+ }
13
+ }
14
+
15
+
16
+ export default Utils