@degenerate/sharedata 0.0.1

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @Degenerate/<new-package-name> Package
2
+
3
+ This package contains the functions for <new-package-name>. The package <what-the-purpose-of-the-package-is> in the SAFE environment.
@@ -0,0 +1,2 @@
1
+ export * from './share-data/share-data';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./share-data/share-data"), exports);
@@ -0,0 +1,76 @@
1
+ import { IShareData, IShareDataEvent, IShareFunction, IShareRuleApplied, IShareRuleEvalutionResult, IShareTransactionHistory } from '@degenerate/generic';
2
+ /**
3
+ * This contains the interface for the share data object
4
+ */
5
+ export declare class ShareData {
6
+ private _id;
7
+ private _version;
8
+ private _rules;
9
+ private _functions;
10
+ private _transactionHistory;
11
+ private _events;
12
+ private isLocked;
13
+ private _data;
14
+ /**
15
+ * This is the constructor for the share data object
16
+ * @param id the id of the share data
17
+ */
18
+ constructor(id: string);
19
+ /**
20
+ * This adds a rule to the share block.
21
+ * @param rule the rule to add to the share block
22
+ */
23
+ addRule(rule: IShareRuleApplied): void;
24
+ /**
25
+ * This gets the rules for the share block
26
+ */
27
+ get rules(): IShareRuleApplied[];
28
+ /**
29
+ * This gets the transaction history for the share block
30
+ */
31
+ addFunction(func: IShareFunction<any, (...args: any[]) => any, any>): void;
32
+ /**
33
+ * This gets the functions for the share block
34
+ */
35
+ get functions(): IShareFunction<any, (...args: any[]) => any, any>[];
36
+ /**
37
+ * This gets the events for the sharedata
38
+ */
39
+ get events(): IShareDataEvent[];
40
+ /**
41
+ * This adds an event to the share data
42
+ * @param event the event to add
43
+ */
44
+ addEvent(event: IShareDataEvent): void;
45
+ /**
46
+ * This adds a transaction to the share block
47
+ * @param rulesEvaluation the evaluation of the rules (result)
48
+ * @param transactionFunction the function that was run
49
+ */
50
+ addTransactionAlreadyValidated(proposedData: IShareData, rulesEvaluation: IShareRuleEvalutionResult, transactionFunction: IShareFunction<any, (...args: any[]) => any, any>): void;
51
+ /**
52
+ * This adds a transaction to the share block but the transaction has not yet been validated
53
+ *
54
+ * @param proposedData the proposed data
55
+ * @param transactionFunction the function that was attempted
56
+ */
57
+ addTransactionNotValidated(proposedData: IShareData, transactionFunction: IShareFunction<any, (...args: any[]) => any, any>): void;
58
+ /**
59
+ * This gets the transaction history for the share block
60
+ */
61
+ get transactionHistory(): IShareTransactionHistory[];
62
+ /**
63
+ * This returns the current state of data for the share block
64
+ */
65
+ get data(): IShareData;
66
+ /**
67
+ * This gets the version of the share data
68
+ */
69
+ get version(): number;
70
+ /**
71
+ * This copies a share data object to a new transaction number
72
+ * @param transactionNumber the number of the transaction to run until.
73
+ */
74
+ copyTo(transactionNumber: number): ShareData;
75
+ }
76
+ //# sourceMappingURL=share-data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"share-data.d.ts","sourceRoot":"","sources":["../../src/share-data/share-data.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AU,EACxB,mBAAmB,EAAE,cAAc,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAAE,GAAG,CAAC,GACvE,IAAI;IA0BP;;OAEG;IACH,IAAI,kBAAkB,IAAI,wBAAwB,EAAE,CAEnD;IAED;;OAEG;IACH,IAAI,IAAI,IAAI,UAAU,CAErB;IAED;;OAEG;IACH,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED;;;OAGG;IACH,MAAM,CAAC,iBAAiB,EAAE,MAAM,GAAG,SAAS;CAa/C"}
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ShareData = void 0;
4
+ const rules_1 = require("@degenerate/rules");
5
+ const generic_1 = require("@degenerate/generic");
6
+ /**
7
+ * This contains the interface for the share data object
8
+ */
9
+ class ShareData {
10
+ /**
11
+ * This is the constructor for the share data object
12
+ * @param id the id of the share data
13
+ */
14
+ constructor(id) {
15
+ this.isLocked = false;
16
+ this._version = 1,
17
+ this._id = id;
18
+ this._rules = [];
19
+ this._functions = [];
20
+ this._transactionHistory = [];
21
+ this._events = [];
22
+ this._data = {};
23
+ }
24
+ //#region Getters and Setters
25
+ /**
26
+ * This adds a rule to the share block.
27
+ * @param rule the rule to add to the share block
28
+ */
29
+ addRule(rule) {
30
+ if (this.isLocked) {
31
+ throw new generic_1.ShareDataIsLockedError(this._id, generic_1.ShareDataTypes.RULE);
32
+ }
33
+ this._rules.push(rule);
34
+ }
35
+ /**
36
+ * This gets the rules for the share block
37
+ */
38
+ get rules() {
39
+ return this._rules;
40
+ }
41
+ /**
42
+ * This gets the transaction history for the share block
43
+ */
44
+ addFunction(func) {
45
+ if (this.isLocked) {
46
+ throw new generic_1.ShareDataIsLockedError(this._id, generic_1.ShareDataTypes.FUNCTION);
47
+ }
48
+ this._functions.push(func);
49
+ }
50
+ /**
51
+ * This gets the functions for the share block
52
+ */
53
+ get functions() {
54
+ return this._functions;
55
+ }
56
+ /**
57
+ * This gets the events for the sharedata
58
+ */
59
+ get events() {
60
+ return this._events;
61
+ }
62
+ /**
63
+ * This adds an event to the share data
64
+ * @param event the event to add
65
+ */
66
+ addEvent(event) {
67
+ this._events.push(event);
68
+ }
69
+ /**
70
+ * This adds a transaction to the share block
71
+ * @param rulesEvaluation the evaluation of the rules (result)
72
+ * @param transactionFunction the function that was run
73
+ */
74
+ addTransactionAlreadyValidated(proposedData, rulesEvaluation, transactionFunction) {
75
+ // make sure the function is known.
76
+ if (!this._functions.includes(transactionFunction)) {
77
+ throw new Error('The function is not known');
78
+ }
79
+ const transactionTime = Date.now(); // get the current time
80
+ const transactionId = this._transactionHistory.length + 1; // get the transaction id
81
+ const ruleOutcome = (rulesEvaluation.proposedResult && rulesEvaluation.currentResult);
82
+ const transaction = {
83
+ ruleOutcome,
84
+ ruleFailures: [],
85
+ transactionFunction,
86
+ transactionId: transactionId.toString(),
87
+ transactionTime,
88
+ };
89
+ // the outcome of the rules is valid write the data to be the new current data.
90
+ if (ruleOutcome) {
91
+ this._data = proposedData;
92
+ }
93
+ this._transactionHistory.push(transaction);
94
+ }
95
+ /**
96
+ * This adds a transaction to the share block but the transaction has not yet been validated
97
+ *
98
+ * @param proposedData the proposed data
99
+ * @param transactionFunction the function that was attempted
100
+ */
101
+ addTransactionNotValidated(proposedData, transactionFunction) {
102
+ // make sure the function is known.
103
+ if (!this._functions.includes(transactionFunction)) {
104
+ // throw ShareDataFunctionBeingAppliedDoesNotExistOnShareDataError
105
+ throw new Error('The function is not known');
106
+ }
107
+ // // evaluate the rules.
108
+ // const rulesEvalOutcome = evaluateRules('transaction', this._data, proposedData, this._rules);
109
+ const rulesOutcome = (0, rules_1.startRuleEvaluation)(this._id, transactionFunction, this.functions, this._rules, this._data, proposedData);
110
+ const transactionTime = Date.now(); // get the current time
111
+ const transactionId = this._transactionHistory.length + 1; // get the transaction id
112
+ const transaction = {
113
+ ruleOutcome: rulesOutcome.isValidProposal,
114
+ ruleFailures: [],
115
+ transactionFunction,
116
+ transactionId: transactionId.toString(),
117
+ transactionTime,
118
+ };
119
+ // update the data if the rules are valid
120
+ if (rulesOutcome.isValidProposal) {
121
+ this._data = proposedData;
122
+ }
123
+ // add the transaction to the history
124
+ this._transactionHistory.push(transaction);
125
+ }
126
+ /**
127
+ * This gets the transaction history for the share block
128
+ */
129
+ get transactionHistory() {
130
+ return this._transactionHistory;
131
+ }
132
+ /**
133
+ * This returns the current state of data for the share block
134
+ */
135
+ get data() {
136
+ return this._data;
137
+ }
138
+ /**
139
+ * This gets the version of the share data
140
+ */
141
+ get version() {
142
+ return this._version;
143
+ }
144
+ /**
145
+ * This copies a share data object to a new transaction number
146
+ * @param transactionNumber the number of the transaction to run until.
147
+ */
148
+ copyTo(transactionNumber) {
149
+ // return share data with a new ID
150
+ const newShareData = new ShareData(`${this._id}-${transactionNumber}`);
151
+ newShareData._rules = [...this._rules];
152
+ newShareData._functions = [...this._functions];
153
+ newShareData._transactionHistory = [...this._transactionHistory];
154
+ newShareData._events = [...this._events];
155
+ newShareData._data = { ...this._data };
156
+ newShareData._version = this._version;
157
+ return newShareData;
158
+ }
159
+ }
160
+ exports.ShareData = ShareData;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@degenerate/sharedata",
3
+ "version": "0.0.1",
4
+ "description": "This contains the ShareData Adaptive Framework Ecosystem (SAFE) network core code",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "rm -rf dist/* && tsc -p tsconfig.pkg.json",
9
+ "compile": "rm -rf dist/* && tsc -p tsconfig.pkg.json",
10
+ "test": "jest --forceExit --detectOpenHandles --verbose --silent=false --env node --config tsconfig.pkg.json",
11
+ "test:coverage": "jest --forceExit --coverage --detectOpenHandles --verbose --silent=false --env node --config tsconfig.pkg.json",
12
+ "test:clearCache": "jest --clearCache"
13
+ },
14
+ "devDependencies": {
15
+ "@types/jest": "^29.5.14",
16
+ "@types/node": "^22.8.0",
17
+ "jest": "^29.7.0",
18
+ "ts-jest": "^29.2.5",
19
+ "typescript": "^5.6.3"
20
+ },
21
+ "keywords": [
22
+ "ShareData Adaptive Framework Ecosystem",
23
+ "SAFE",
24
+ "SAFE ShareData",
25
+ "ShareData",
26
+ "ShareBlock"
27
+ ],
28
+ "author": "Degenerate Inc.",
29
+ "license": "ISC",
30
+ "dependencies": {
31
+ "@degenerate/generic": "^0.0.17",
32
+ "@degenerate/rules": "^0.0.9"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/syncrude/safe/issues"
36
+ },
37
+ "homepage": "https://github.com/syncrude/safe#readme",
38
+ "gitHead": "0e8f76c14364f6d23ab66cdb9547d4e486fad9a4",
39
+ "publishConfig": {
40
+ "access": "public"
41
+ }
42
+ }