holosphere 1.0.5 → 1.0.7
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/LICENSE +56 -0
- package/README.md +206 -0
- package/config/default.js +1 -0
- package/holosphere.js +117 -76
- package/package.json +3 -2
- package/test/holosphere.test.js +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
|
9
|
+
|
|
10
|
+
0. Additional Definitions.
|
|
11
|
+
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
|
12
|
+
|
|
13
|
+
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
|
14
|
+
|
|
15
|
+
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
|
16
|
+
|
|
17
|
+
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
|
18
|
+
|
|
19
|
+
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
|
20
|
+
|
|
21
|
+
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
|
22
|
+
|
|
23
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
24
|
+
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
|
25
|
+
|
|
26
|
+
2. Conveying Modified Versions.
|
|
27
|
+
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
|
28
|
+
|
|
29
|
+
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
|
30
|
+
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
|
31
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
32
|
+
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
|
33
|
+
|
|
34
|
+
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
|
35
|
+
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
|
36
|
+
4. Combined Works.
|
|
37
|
+
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
|
38
|
+
|
|
39
|
+
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
|
40
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
|
41
|
+
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
|
42
|
+
d) Do one of the following:
|
|
43
|
+
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
|
44
|
+
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
|
45
|
+
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
|
46
|
+
5. Combined Libraries.
|
|
47
|
+
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
|
48
|
+
|
|
49
|
+
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
|
50
|
+
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
|
51
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
52
|
+
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
|
53
|
+
|
|
54
|
+
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
|
55
|
+
|
|
56
|
+
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# HoloSphere
|
|
2
|
+
|
|
3
|
+
HoloSphere is a decentralized geospatial data management system that combines hierarchical hexagonal tiling (H3) with distributed data storage (GunDB) and AI-powered processing of information.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Hierarchical Spatial Data**: Uses Uber's H3 spatial indexing system for efficient geospatial data organization
|
|
8
|
+
- **Decentralized Storage**: Built on GunDB for peer-to-peer data storage and synchronization
|
|
9
|
+
- **Schema Validation**: JSON Schema validation for data integrity
|
|
10
|
+
- **AI Processing**: Extensible AI-powered content analysis and summarization across hexagonal regions
|
|
11
|
+
- **Voting System**: Built-in delegation and voting mechanisms for collaborative decision-making
|
|
12
|
+
- **Multi-scale Operations**: Automatic content propagation across different spatial resolutions
|
|
13
|
+
|
|
14
|
+
## License
|
|
15
|
+
|
|
16
|
+
GNU Lesser General Public License v3.0 (LGPL-3.0)
|
|
17
|
+
|
|
18
|
+
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
|
|
19
|
+
|
|
20
|
+
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
|
21
|
+
|
|
22
|
+
You should have received a copy of the GNU Lesser General Public License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
|
23
|
+
|
|
24
|
+
### Key Points of LGPL-3.0
|
|
25
|
+
|
|
26
|
+
- You can use this library in proprietary applications
|
|
27
|
+
- Any modifications to the library itself must be distributed under LGPL
|
|
28
|
+
- Applications that use the library don't have to be LGPL
|
|
29
|
+
- If you modify the library, you must make the source code available
|
|
30
|
+
- You must provide a way for users to relink with a modified version of the library
|
|
31
|
+
|
|
32
|
+
## Dependencies
|
|
33
|
+
|
|
34
|
+
### Core Dependencies
|
|
35
|
+
- **h3-js**: Uber's H3 geospatial indexing system
|
|
36
|
+
- **gun**: Decentralized graph database
|
|
37
|
+
- **ajv**: JSON Schema validation
|
|
38
|
+
- **lodash**: Utility functions
|
|
39
|
+
|
|
40
|
+
### Optional Dependencies
|
|
41
|
+
- **langchain**: AI/LLM integration framework
|
|
42
|
+
- **openai**: OpenAI API client (optional)
|
|
43
|
+
|
|
44
|
+
## Getting Started
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install holosphere
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Basic Usage
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
import { HoloSphere } from 'holosphere';
|
|
54
|
+
|
|
55
|
+
// Initialize HoloSphere with your configuration
|
|
56
|
+
const holo = new HoloSphere({
|
|
57
|
+
peers: ['http://localhost:8765/gun'],
|
|
58
|
+
aiProvider: 'local' // or 'openai', 'azure', etc.
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Create or access a hexagonal region
|
|
62
|
+
const hex = await holo.getHex('8928308280fffff');
|
|
63
|
+
|
|
64
|
+
// Store data in the region
|
|
65
|
+
await hex.put({
|
|
66
|
+
type: 'observation',
|
|
67
|
+
data: {
|
|
68
|
+
temperature: 22.5,
|
|
69
|
+
timestamp: Date.now()
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Subscribe to changes
|
|
74
|
+
hex.subscribe(data => {
|
|
75
|
+
console.log('New data:', data);
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Advanced Features
|
|
80
|
+
|
|
81
|
+
### Spatial Queries
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
// Find all hexagons within a radius
|
|
85
|
+
const hexagons = await holo.findWithin({
|
|
86
|
+
lat: 37.7749,
|
|
87
|
+
lng: -122.4194,
|
|
88
|
+
radius: 5000 // meters
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Query data across multiple resolutions
|
|
92
|
+
const aggregatedData = await holo.aggregate({
|
|
93
|
+
hexIds: hexagons,
|
|
94
|
+
resolution: 7,
|
|
95
|
+
method: 'average'
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### AI Processing
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
// Process content with AI
|
|
103
|
+
const summary = await hex.processContent({
|
|
104
|
+
processor: 'summarize',
|
|
105
|
+
options: {
|
|
106
|
+
maxLength: 100
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Analyze trends across regions
|
|
111
|
+
const analysis = await holo.analyzeRegion({
|
|
112
|
+
hexIds: hexagons,
|
|
113
|
+
timeRange: '7d',
|
|
114
|
+
metrics: ['sentiment', 'topics']
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Voting and Governance
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
// Create a proposal
|
|
122
|
+
const proposal = await hex.createProposal({
|
|
123
|
+
title: 'Update Data Schema',
|
|
124
|
+
description: 'Add new fields for environmental metrics',
|
|
125
|
+
changes: {
|
|
126
|
+
schema: newSchemaDefinition
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Cast a vote
|
|
131
|
+
await proposal.vote({
|
|
132
|
+
support: true,
|
|
133
|
+
weight: 1.0
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Configuration
|
|
138
|
+
|
|
139
|
+
HoloSphere can be configured with various options:
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
const config = {
|
|
143
|
+
// Network configuration
|
|
144
|
+
peers: ['https://peer1.example.com/gun', 'https://peer2.example.com/gun'],
|
|
145
|
+
|
|
146
|
+
// Storage options
|
|
147
|
+
storage: {
|
|
148
|
+
type: 'indexeddb',
|
|
149
|
+
namespace: 'my-app'
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
// AI processing configuration
|
|
153
|
+
ai: {
|
|
154
|
+
provider: 'local',
|
|
155
|
+
model: 'gpt-3.5-turbo',
|
|
156
|
+
apiKey: process.env.AI_API_KEY
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
// Schema validation
|
|
160
|
+
schema: {
|
|
161
|
+
strict: true,
|
|
162
|
+
customValidators: {}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const holo = new HoloSphere(config);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Documentation
|
|
170
|
+
|
|
171
|
+
For detailed documentation, please visit our [documentation site](https://docs.holons.io).
|
|
172
|
+
|
|
173
|
+
## Contributing
|
|
174
|
+
|
|
175
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details on:
|
|
176
|
+
- Code of Conduct
|
|
177
|
+
- Development setup
|
|
178
|
+
- Testing guidelines
|
|
179
|
+
- Pull request process
|
|
180
|
+
|
|
181
|
+
## Support
|
|
182
|
+
|
|
183
|
+
- [GitHub Issues](https://github.com/liminalvillage/holosphere/issues)
|
|
184
|
+
- [Discord Community](https://discord.gg/liminalvillage)
|
|
185
|
+
|
|
186
|
+
## Roadmap
|
|
187
|
+
|
|
188
|
+
- [ ] Enhanced spatial indexing algorithms
|
|
189
|
+
- [ ] Additional AI model integrations
|
|
190
|
+
- [ ] Improved data replication strategies
|
|
191
|
+
- [ ] Extended governance mechanisms
|
|
192
|
+
- [ ] Mobile SDK development
|
|
193
|
+
|
|
194
|
+
## Citation
|
|
195
|
+
|
|
196
|
+
If you use HoloSphere in your research, please cite:
|
|
197
|
+
|
|
198
|
+
```bibtex
|
|
199
|
+
@software{holosphere2024,
|
|
200
|
+
author = {Roberto Valenti},
|
|
201
|
+
title = {HoloSphere: Decentralized Geospatial Data Management},
|
|
202
|
+
year = {2024},
|
|
203
|
+
url = {https://github.com/holosphere/holosphere}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/holosphere.js
CHANGED
|
@@ -31,18 +31,18 @@ class HoloSphere {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* Sets the JSON schema for a specific
|
|
35
|
-
* @param {string}
|
|
34
|
+
* Sets the JSON schema for a specific lens.
|
|
35
|
+
* @param {string} lens - The lens identifier.
|
|
36
36
|
* @param {object} schema - The JSON schema to set.
|
|
37
37
|
* @returns {Promise} - Resolves when the schema is set.
|
|
38
38
|
*/
|
|
39
|
-
async setSchema(
|
|
39
|
+
async setSchema(lens, schema) {
|
|
40
40
|
return new Promise((resolve, reject) => {
|
|
41
|
-
this.gun.get(
|
|
41
|
+
this.gun.get(lens).get('schema').put(JSON.stringify(schema), ack => {
|
|
42
42
|
if (ack.err) {
|
|
43
43
|
resolve(new Error('Failed to add schema: ' + ack.err));
|
|
44
44
|
} else {
|
|
45
|
-
console.log('Schema added successfully under
|
|
45
|
+
console.log('Schema added successfully under lens:', lens);
|
|
46
46
|
resolve(ack);
|
|
47
47
|
}
|
|
48
48
|
})
|
|
@@ -50,13 +50,13 @@ class HoloSphere {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
|
-
* Retrieves the JSON schema for a specific
|
|
54
|
-
* @param {string}
|
|
53
|
+
* Retrieves the JSON schema for a specific lens.
|
|
54
|
+
* @param {string} lens - The lens identifier.
|
|
55
55
|
* @returns {Promise<object|null>} - The retrieved schema or null if not found.
|
|
56
56
|
*/
|
|
57
|
-
async getSchema(
|
|
57
|
+
async getSchema(lens) {
|
|
58
58
|
return new Promise((resolve) => {
|
|
59
|
-
this.gun.get(
|
|
59
|
+
this.gun.get(lens).get('schema').once(data => {
|
|
60
60
|
if (data) {
|
|
61
61
|
let parsed;
|
|
62
62
|
try {
|
|
@@ -82,14 +82,16 @@ class HoloSphere {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
|
-
* Stores content in the specified hex and
|
|
85
|
+
* Stores content in the specified hex and lens.
|
|
86
86
|
* @param {string} hex - The hex identifier.
|
|
87
|
-
* @param {string}
|
|
87
|
+
* @param {string} lens - The lens under which to store the content.
|
|
88
88
|
* @param {object} content - The content to store.
|
|
89
89
|
*/
|
|
90
|
-
async put(hex,
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
async put(hex, lens, content) {
|
|
91
|
+
if (!hex || !lens || !content) return;
|
|
92
|
+
console.error('Error in put:', hex, lens, content);
|
|
93
|
+
// Retrieve the schema for the lens
|
|
94
|
+
let schema = await this.getSchema(lens)
|
|
93
95
|
if (schema) {
|
|
94
96
|
// Validate the content against the schema
|
|
95
97
|
const valid = this.validator.validate(schema, content);
|
|
@@ -105,71 +107,102 @@ class HoloSphere {
|
|
|
105
107
|
let noderef;
|
|
106
108
|
|
|
107
109
|
if (content.id) { //use the user-defined id. Important to be able to send updates using put
|
|
108
|
-
noderef = this.gun.get(
|
|
109
|
-
this.gun.get(hex.toString()).get(
|
|
110
|
+
noderef = this.gun.get(lens).get(content.id).put(payload)
|
|
111
|
+
this.gun.get(hex.toString()).get(lens).get(content.id).put(payload)
|
|
110
112
|
} else { // create a content-addressable reference like IPFS. Note: no updates possible using put
|
|
111
113
|
const hashBuffer = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(payload));
|
|
112
114
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
113
115
|
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, "0")).join("");
|
|
114
|
-
noderef = this.gun.get(
|
|
115
|
-
this.gun.get(hex.toString()).get(
|
|
116
|
+
noderef = this.gun.get(lens).get(hashHex).put(payload)
|
|
117
|
+
this.gun.get(hex.toString()).get(lens).get(hashHex).put(payload)
|
|
116
118
|
}
|
|
117
119
|
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
async putNode(hex,
|
|
121
|
-
this.gun.get(hex).get(
|
|
122
|
+
async putNode(hex, lens, node) {
|
|
123
|
+
this.gun.get(hex).get(lens).set(node)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async parse(data) {
|
|
127
|
+
let parsed = {};
|
|
128
|
+
|
|
129
|
+
if (typeof data === 'object' && data !== null) {
|
|
130
|
+
if (data._ && data._["#"]) {
|
|
131
|
+
// If the data is a reference, fetch the actual content
|
|
132
|
+
let query = data._['#'].split('/');
|
|
133
|
+
let hex = query[1];
|
|
134
|
+
let lens = query[2];
|
|
135
|
+
let key = query[3];
|
|
136
|
+
parsed = await this.getKey(hex, lens, key);
|
|
137
|
+
} else if (data._ && data._['>']) {
|
|
138
|
+
// This might be a GunDB node, try to get its value
|
|
139
|
+
const nodeValue = Object.values(data).find(v => typeof v !== 'object' && v !== '_');
|
|
140
|
+
if (nodeValue) {
|
|
141
|
+
try {
|
|
142
|
+
parsed = JSON.parse(nodeValue);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
console.log('Invalid JSON in node value:', nodeValue);
|
|
145
|
+
parsed = nodeValue; // return the raw data
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
console.log('Unable to parse GunDB node:', data);
|
|
149
|
+
parsed = data; // return the original data
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
// Treat it as regular data
|
|
153
|
+
parsed = data;
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
// If it's not an object, try parsing it as JSON
|
|
157
|
+
try {
|
|
158
|
+
parsed = JSON.parse(data);
|
|
159
|
+
} catch (e) {
|
|
160
|
+
console.log('Invalid JSON:', data);
|
|
161
|
+
parsed = data; // return the raw data
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return parsed;
|
|
122
166
|
}
|
|
123
167
|
|
|
124
168
|
/**
|
|
125
|
-
* Retrieves content from the specified hex and
|
|
169
|
+
* Retrieves content from the specified hex and lens.
|
|
126
170
|
* @param {string} hex - The hex identifier.
|
|
127
|
-
* @param {string}
|
|
171
|
+
* @param {string} lens - The lens from which to retrieve content.
|
|
128
172
|
* @returns {Promise<Array<object>>} - The retrieved content.
|
|
129
173
|
*/
|
|
130
|
-
async get(hex,
|
|
174
|
+
async get(hex, lens) {
|
|
175
|
+
if (!hex || !lens) {
|
|
176
|
+
console.log('Wrong get:', hex, lens)
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
131
179
|
// Wrap the GunDB operation in a promise
|
|
132
|
-
//retrieve
|
|
133
|
-
const schema = await this.getSchema(
|
|
180
|
+
//retrieve lens schema
|
|
181
|
+
const schema = await this.getSchema(lens);
|
|
134
182
|
|
|
135
183
|
if (!schema) {
|
|
136
|
-
console.log('The schema for "' +
|
|
184
|
+
console.log('The schema for "' + lens + '" is not defined');
|
|
137
185
|
// return null; // No schema found, return null if strict about it
|
|
138
186
|
}
|
|
139
187
|
|
|
140
188
|
return new Promise(async (resolve, reject) => {
|
|
141
189
|
let output = []
|
|
142
190
|
let counter = 0
|
|
143
|
-
this.gun.get(hex.toString()).get(
|
|
191
|
+
this.gun.get(hex.toString()).get(lens).once((data, key) => {
|
|
144
192
|
if (data) {
|
|
145
193
|
const maplenght = Object.keys(data).length - 1
|
|
146
194
|
console.log('Map length:', maplenght)
|
|
147
|
-
this.gun.get(hex.toString()).get(
|
|
195
|
+
this.gun.get(hex.toString()).get(lens).map().once(async (itemdata, key) => {
|
|
148
196
|
counter += 1
|
|
149
197
|
if (itemdata) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
// If the data is a reference, fetch the actual content
|
|
153
|
-
let query = itemdata._['#'].split('/')
|
|
154
|
-
let hex = query[1]
|
|
155
|
-
let lense = query[2]
|
|
156
|
-
let key = query[3]
|
|
157
|
-
itemdata = await this.getNode(hex, lense, key)
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
var parsed = {}
|
|
161
|
-
try {
|
|
162
|
-
parsed = JSON.parse(itemdata);
|
|
163
|
-
} catch (e) {
|
|
164
|
-
console.log('Invalid JSON:', itemdata);
|
|
165
|
-
parsed = itemdata //return the raw data
|
|
166
|
-
}
|
|
198
|
+
let parsed = await this.parse (itemdata)
|
|
199
|
+
|
|
167
200
|
|
|
168
201
|
if (schema) {
|
|
169
202
|
let valid = this.validator.validate(schema, parsed);
|
|
170
203
|
if (!valid || parsed == null || parsed == undefined) {
|
|
171
204
|
console.log('Removing Invalid content:', this.validator.errors);
|
|
172
|
-
this.gun.get(hex).get(
|
|
205
|
+
this.gun.get(hex).get(lens).get(key).put(null);
|
|
173
206
|
|
|
174
207
|
} else {
|
|
175
208
|
output.push(parsed);
|
|
@@ -192,18 +225,26 @@ class HoloSphere {
|
|
|
192
225
|
}
|
|
193
226
|
|
|
194
227
|
/**
|
|
195
|
-
* Retrieves a specific key from the specified hex and
|
|
228
|
+
* Retrieves a specific key from the specified hex and lens.
|
|
196
229
|
* @param {string} hex - The hex identifier.
|
|
197
|
-
* @param {string}
|
|
230
|
+
* @param {string} lens - The lens from which to retrieve the key.
|
|
198
231
|
* @param {string} key - The specific key to retrieve.
|
|
199
232
|
* @returns {Promise<object|null>} - The retrieved content or null if not found.
|
|
200
233
|
*/
|
|
201
|
-
async getKey(hex,
|
|
234
|
+
async getKey(hex, lens, key) {
|
|
202
235
|
return new Promise((resolve) => {
|
|
203
236
|
// Use Gun to get the data
|
|
204
|
-
this.gun.get(hex).get(
|
|
237
|
+
this.gun.get(hex).get(lens).get(key).once((data, key) => {
|
|
205
238
|
if (data) {
|
|
206
|
-
|
|
239
|
+
console.log('Data getting parsed:', data)
|
|
240
|
+
try {
|
|
241
|
+
let parsed = JSON.parse(data); // Resolve the promise with the data if data is found
|
|
242
|
+
resolve(parsed);
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
resolve(data)
|
|
246
|
+
}
|
|
247
|
+
|
|
207
248
|
} else {
|
|
208
249
|
resolve(null); // Reject the promise if no data is found
|
|
209
250
|
}
|
|
@@ -213,26 +254,26 @@ class HoloSphere {
|
|
|
213
254
|
}
|
|
214
255
|
|
|
215
256
|
/**
|
|
216
|
-
* Retrieves a specific gundb node from the specified hex and
|
|
257
|
+
* Retrieves a specific gundb node from the specified hex and lens.
|
|
217
258
|
* @param {string} hex - The hex identifier.
|
|
218
|
-
* @param {string}
|
|
259
|
+
* @param {string} lens - The lens from which to retrieve the key.
|
|
219
260
|
* @param {string} key - The specific key to retrieve.
|
|
220
261
|
* @returns {Promise<object|null>} - The retrieved content or null if not found.
|
|
221
262
|
*/
|
|
222
|
-
getNode(hex,
|
|
263
|
+
getNode(hex, lens, key) {
|
|
223
264
|
// Use Gun to get the data
|
|
224
|
-
return this.gun.get(hex).get(
|
|
265
|
+
return this.gun.get(hex).get(lens).get(key)
|
|
225
266
|
}
|
|
226
267
|
|
|
227
268
|
|
|
228
269
|
|
|
229
270
|
/**
|
|
230
|
-
* Computes summaries based on the content within a hex and
|
|
271
|
+
* Computes summaries based on the content within a hex and lens.
|
|
231
272
|
* @param {string} hex - The hex identifier.
|
|
232
|
-
* @param {string}
|
|
273
|
+
* @param {string} lens - The lens to compute.
|
|
233
274
|
* @param {string} operation - The operation to perform.
|
|
234
275
|
*/
|
|
235
|
-
async compute(hex,
|
|
276
|
+
async compute(hex, lens, operation) {
|
|
236
277
|
|
|
237
278
|
let res = h3.getResolution(hex);
|
|
238
279
|
if (res < 1 || res > 15) return;
|
|
@@ -251,7 +292,7 @@ class HoloSphere {
|
|
|
251
292
|
resolve(); // Resolve the promise to prevent it from hanging
|
|
252
293
|
}, 1000); // Timeout of 5 seconds
|
|
253
294
|
|
|
254
|
-
this.gun.get(siblings[i]).get(
|
|
295
|
+
this.gun.get(siblings[i]).get(lens).map().once((data, key) => {
|
|
255
296
|
clearTimeout(timeout); // Clear the timeout if data is received
|
|
256
297
|
if (data) {
|
|
257
298
|
content.push(data.content);
|
|
@@ -267,23 +308,23 @@ class HoloSphere {
|
|
|
267
308
|
console.log('Computed:', computed)
|
|
268
309
|
let node = await this.gun.get(parent + '_summary').put({ id: parent + '_summary', content: computed })
|
|
269
310
|
|
|
270
|
-
this.put(parent,
|
|
271
|
-
this.compute(parent,
|
|
311
|
+
this.put(parent, lens, node);
|
|
312
|
+
this.compute(parent, lens, operation)
|
|
272
313
|
}
|
|
273
314
|
|
|
274
315
|
/**
|
|
275
|
-
* Clears all entities under a specific hex and
|
|
316
|
+
* Clears all entities under a specific hex and lens.
|
|
276
317
|
* @param {string} hex - The hex identifier.
|
|
277
|
-
* @param {string}
|
|
318
|
+
* @param {string} lens - The lens to clear.
|
|
278
319
|
*/
|
|
279
|
-
async
|
|
320
|
+
async clearlens(hex, lens) {
|
|
280
321
|
let entities = {};
|
|
281
322
|
|
|
282
323
|
// Get list out of Gun
|
|
283
|
-
this.gun.get(hex).get(
|
|
324
|
+
this.gun.get(hex).get(lens).map().once((data, key) => {
|
|
284
325
|
//entities = data;
|
|
285
326
|
//const id = Object.keys(entities)[0] // since this would be in object form, you can manipulate it as you would like.
|
|
286
|
-
this.gun.get(hex).get(
|
|
327
|
+
this.gun.get(hex).get(lens).put({ [key]: null })
|
|
287
328
|
})
|
|
288
329
|
}
|
|
289
330
|
|
|
@@ -328,21 +369,21 @@ class HoloSphere {
|
|
|
328
369
|
/**
|
|
329
370
|
* Upcasts content to parent hexagons recursively.
|
|
330
371
|
* @param {string} hex - The current hex identifier.
|
|
331
|
-
* @param {string}
|
|
372
|
+
* @param {string} lens - The lens under which to upcast.
|
|
332
373
|
* @param {object} content - The content to upcast.
|
|
333
374
|
* @returns {Promise<object>} - The upcasted content.
|
|
334
375
|
*/
|
|
335
|
-
async upcast(hex,
|
|
376
|
+
async upcast(hex, lens, content) {
|
|
336
377
|
let res = h3.getResolution(hex)
|
|
337
378
|
if (res == 0) {
|
|
338
|
-
await this.putNode(hex,
|
|
379
|
+
await this.putNode(hex, lens, content)
|
|
339
380
|
return content
|
|
340
381
|
}
|
|
341
382
|
else {
|
|
342
|
-
console.log('Upcasting ', hex,
|
|
343
|
-
await this.putNode(hex,
|
|
383
|
+
console.log('Upcasting ', hex, lens, content, res)
|
|
384
|
+
await this.putNode(hex, lens, content)
|
|
344
385
|
let parent = h3.cellToParent(hex, res - 1)
|
|
345
|
-
return this.upcast(parent,
|
|
386
|
+
return this.upcast(parent, lens, content)
|
|
346
387
|
}
|
|
347
388
|
}
|
|
348
389
|
|
|
@@ -410,13 +451,13 @@ class HoloSphere {
|
|
|
410
451
|
}
|
|
411
452
|
|
|
412
453
|
/**
|
|
413
|
-
* Subscribes to changes in a specific hex and
|
|
454
|
+
* Subscribes to changes in a specific hex and lens.
|
|
414
455
|
* @param {string} hex - The hex identifier.
|
|
415
|
-
* @param {string}
|
|
456
|
+
* @param {string} lens - The lens to subscribe to.
|
|
416
457
|
* @param {function} callback - The callback to execute on changes.
|
|
417
458
|
*/
|
|
418
|
-
subscribe(hex,
|
|
419
|
-
this.gun.get(hex).get(
|
|
459
|
+
subscribe(hex, lens, callback) {
|
|
460
|
+
this.gun.get(hex).get(lens).map().on((data, key) => {
|
|
420
461
|
callback(data, key)
|
|
421
462
|
})
|
|
422
463
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "holosphere",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Holonic Geospatial Communication Infrastructure
|
|
3
|
+
"version": "1.0.7",
|
|
4
|
+
"description": "Holonic Geospatial Communication Infrastructure",
|
|
5
5
|
"main": "holosphere.js",
|
|
6
|
+
"types": "holosphere.d.ts",
|
|
6
7
|
"type": "module",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import HoloSphere from '../holosphere.js';
|
|
2
|
+
|
|
3
|
+
describe('HoloSphere', () => {
|
|
4
|
+
let holoSphere;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
holoSphere = new HoloSphere('test-app', {
|
|
8
|
+
gunPeers: ['http://localhost:8765/gun'],
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('initialization', () => {
|
|
13
|
+
expect(holoSphere).toBeDefined();
|
|
14
|
+
expect(holoSphere.users).toEqual({});
|
|
15
|
+
expect(holoSphere.hexagonVotes).toEqual({});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('voting system', async () => {
|
|
19
|
+
const userId = 'test-user';
|
|
20
|
+
const hexId = '8928308280fffff';
|
|
21
|
+
const topic = 'test-topic';
|
|
22
|
+
const vote = 'yes';
|
|
23
|
+
|
|
24
|
+
await holoSphere.initializeUser(userId);
|
|
25
|
+
await holoSphere.vote(userId, hexId, topic, vote);
|
|
26
|
+
|
|
27
|
+
const votes = holoSphere.aggregateVotes(hexId, topic);
|
|
28
|
+
expect(votes).toHaveProperty('yes', 1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Add more tests...
|
|
32
|
+
});
|