@ddd-qc/cell-proxy 0.24.7 → 0.25.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/dist/hash.d.ts ADDED
@@ -0,0 +1,129 @@
1
+ import { HoloHash, HoloHashB64 } from "@holochain/client";
2
+ /**
3
+ * Checks if obj is a Hash or list of hashes and tries to convert it a B64 or list of B64
4
+ */
5
+ export declare function anyToB64(obj: any): any;
6
+ /** ------------------------------------------------------------------------------------------------------------------*/
7
+ export declare enum HoloHashType {
8
+ Action = "Action",
9
+ Agent = "Agent",
10
+ Dna = "Dna",
11
+ Entry = "Entry",
12
+ External = "External",
13
+ Network = "Network",
14
+ Wasm = "Wasm"
15
+ }
16
+ export declare const HASH_TYPE_PREFIX_B64: {
17
+ Action: string;
18
+ Agent: string;
19
+ Dna: string;
20
+ Entry: string;
21
+ External: string;
22
+ Network: string;
23
+ Wasm: string;
24
+ };
25
+ export declare function getHashType(hash: HoloHashB64): HoloHashType;
26
+ /** */
27
+ export declare function isHashTypeB64(hash: HoloHashB64, hashType: HoloHashType): boolean;
28
+ export declare function hasHoloHashType(hash: HoloHashB64): boolean;
29
+ /** */
30
+ export declare function validateHashB64(hash: HoloHashB64): void;
31
+ export declare function dec64(hash: HoloHashB64): HoloHash;
32
+ export declare function enc64(hash: HoloHash): HoloHashB64;
33
+ /** HoloHash starts with 'u' has a type and is 53 chars long */
34
+ export declare abstract class HolochainId {
35
+ readonly type: HoloHashType;
36
+ readonly b64: HoloHashB64;
37
+ /** Validate */
38
+ constructor(input: HoloHashB64 | HoloHash, type: HoloHashType);
39
+ get hash(): HoloHash;
40
+ /** First 8 chars of the Core */
41
+ get short(): string;
42
+ toString(): string;
43
+ print(): string;
44
+ }
45
+ /** Mixin */
46
+ export declare function createHolochainId(hashType: HoloHashType): {
47
+ new (input: HoloHashB64 | HoloHash): {
48
+ readonly b64: string;
49
+ readonly type: HoloHashType;
50
+ readonly hash: Uint8Array;
51
+ /** First 8 chars of the Core */
52
+ readonly short: string;
53
+ toString(): string;
54
+ print(): string;
55
+ };
56
+ };
57
+ declare const ActionId_base: {
58
+ new (input: string | Uint8Array): {
59
+ readonly b64: string;
60
+ readonly type: HoloHashType;
61
+ readonly hash: Uint8Array;
62
+ /** First 8 chars of the Core */
63
+ readonly short: string;
64
+ toString(): string;
65
+ print(): string;
66
+ };
67
+ };
68
+ export declare class ActionId extends ActionId_base {
69
+ }
70
+ declare const AgentId_base: {
71
+ new (input: string | Uint8Array): {
72
+ readonly b64: string;
73
+ readonly type: HoloHashType;
74
+ readonly hash: Uint8Array;
75
+ /** First 8 chars of the Core */
76
+ readonly short: string;
77
+ toString(): string;
78
+ print(): string;
79
+ };
80
+ };
81
+ export declare class AgentId extends AgentId_base {
82
+ }
83
+ declare const DnaId_base: {
84
+ new (input: string | Uint8Array): {
85
+ readonly b64: string;
86
+ readonly type: HoloHashType;
87
+ readonly hash: Uint8Array;
88
+ /** First 8 chars of the Core */
89
+ readonly short: string;
90
+ toString(): string;
91
+ print(): string;
92
+ };
93
+ };
94
+ export declare class DnaId extends DnaId_base {
95
+ }
96
+ declare const EntryId_base: {
97
+ new (input: string | Uint8Array): {
98
+ readonly b64: string;
99
+ readonly type: HoloHashType;
100
+ readonly hash: Uint8Array;
101
+ /** First 8 chars of the Core */
102
+ readonly short: string;
103
+ toString(): string;
104
+ print(): string;
105
+ };
106
+ };
107
+ export declare class EntryId extends EntryId_base {
108
+ }
109
+ declare const ExternalId_base: {
110
+ new (input: string | Uint8Array): {
111
+ readonly b64: string;
112
+ readonly type: HoloHashType;
113
+ readonly hash: Uint8Array;
114
+ /** First 8 chars of the Core */
115
+ readonly short: string;
116
+ toString(): string;
117
+ print(): string;
118
+ };
119
+ };
120
+ export declare class ExternalId extends ExternalId_base {
121
+ }
122
+ export type AnyDhtId = ActionId | EntryId;
123
+ export type AnyLinkableId = AnyDhtId | ExternalId;
124
+ /** */
125
+ export declare function intoDhtId(input: HoloHashB64 | HoloHash): AnyDhtId;
126
+ /** */
127
+ export declare function intoLinkableId(input: HoloHashB64 | HoloHash): AnyLinkableId;
128
+ export {};
129
+ /** ------- */
package/dist/hash.js ADDED
@@ -0,0 +1,246 @@
1
+ import { decodeHashFromBase64, encodeHashToBase64 } from "@holochain/client";
2
+ /**
3
+ * Checks if obj is a Hash or list of hashes and tries to convert it a B64 or list of B64
4
+ */
5
+ export function anyToB64(obj) {
6
+ /** Check if it's a hash */
7
+ if (obj instanceof Uint8Array) {
8
+ return encodeHashToBase64(obj);
9
+ }
10
+ else {
11
+ /** Check if it's an array of hashes */
12
+ if (Array.isArray(obj)) {
13
+ const isUint8Array = obj.length > 0 &&
14
+ obj.every((value) => {
15
+ return value instanceof Uint8Array;
16
+ });
17
+ if (isUint8Array) {
18
+ let result = [];
19
+ for (const cur of obj) {
20
+ result.push(encodeHashToBase64(cur));
21
+ }
22
+ return result;
23
+ }
24
+ }
25
+ }
26
+ return obj;
27
+ }
28
+ /** ------------------------------------------------------------------------------------------------------------------*/
29
+ export var HoloHashType;
30
+ (function (HoloHashType) {
31
+ HoloHashType["Action"] = "Action";
32
+ HoloHashType["Agent"] = "Agent";
33
+ //DhtOp = "DhtOp",
34
+ HoloHashType["Dna"] = "Dna";
35
+ HoloHashType["Entry"] = "Entry";
36
+ HoloHashType["External"] = "External";
37
+ HoloHashType["Network"] = "Network";
38
+ //Warrent = "Warrent",
39
+ HoloHashType["Wasm"] = "Wasm";
40
+ })(HoloHashType || (HoloHashType = {}));
41
+ export const HASH_TYPE_PREFIX_B64 = {
42
+ Action: "uhCkk",
43
+ Agent: "uhCAk",
44
+ Dna: "uhC0k",
45
+ //DhtOp: "hCQk",
46
+ Entry: "uhCEk",
47
+ External: "uhC8k",
48
+ Network: "uhCIk",
49
+ //Warrent: "Warrent",
50
+ Wasm: "uhCok",
51
+ };
52
+ export function getHashType(hash) {
53
+ const hashExt = hash.slice(0, 5);
54
+ const hashPrefixes = Object.values(HASH_TYPE_PREFIX_B64);
55
+ for (let i = 0; i < hashPrefixes.length; i += 1) {
56
+ if (hashPrefixes[i] == hashExt) {
57
+ return Object.keys(HASH_TYPE_PREFIX_B64)[i];
58
+ }
59
+ }
60
+ throw Error("Unknown hash type");
61
+ }
62
+ /** */
63
+ export function isHashTypeB64(hash, hashType) {
64
+ const slice = hash.slice(0, 5);
65
+ const prefix = HASH_TYPE_PREFIX_B64[hashType];
66
+ for (let i = 0; i < prefix.length; i++) {
67
+ if (slice[i] !== prefix[i]) {
68
+ return false;
69
+ }
70
+ }
71
+ return true;
72
+ }
73
+ export function hasHoloHashType(hash) {
74
+ return !!Object.values(HASH_TYPE_PREFIX_B64).find((prefix) => hash.startsWith(`${prefix}`));
75
+ }
76
+ /** */
77
+ export function validateHashB64(hash) {
78
+ if (!hash || typeof (hash) != 'string') {
79
+ throw new Error("The hash must be a valid string");
80
+ }
81
+ if (hash.length !== 53) {
82
+ throw new Error("The hash must be exactly 53 characters long.");
83
+ }
84
+ if (!hasHoloHashType(hash)) {
85
+ throw new Error("The hash must have a valid HoloHash type.");
86
+ }
87
+ }
88
+ export function dec64(hash) {
89
+ validateHashB64(hash);
90
+ return decodeHashFromBase64(hash);
91
+ }
92
+ export function enc64(hash) {
93
+ let b64 = encodeHashToBase64(hash);
94
+ validateHashB64(b64);
95
+ return b64;
96
+ }
97
+ /** HoloHash starts with 'u' has a type and is 53 chars long */
98
+ export class HolochainId {
99
+ //private readonly hash: HoloHash;
100
+ /** Validate */
101
+ constructor(input, type) {
102
+ this.type = type;
103
+ if (typeof (input) != 'string') {
104
+ input = encodeHashToBase64(input);
105
+ }
106
+ validateHashB64(input);
107
+ this.b64 = input;
108
+ }
109
+ get hash() { return dec64(this.b64); }
110
+ /** First 8 chars of the Core */
111
+ get short() { return this.b64.slice(5, 13); }
112
+ toString() { return this.b64; }
113
+ print() { return `${this.short} (${this.type})`; }
114
+ }
115
+ /** Mixin */
116
+ export function createHolochainId(hashType) {
117
+ class AHoloId extends HolochainId {
118
+ constructor(input) {
119
+ super(input, hashType);
120
+ const type = getHashType(this.b64);
121
+ if (hashType != type) {
122
+ throw new Error('The hash does not have the correct type. Expected ' + hashType + ', got' + type);
123
+ }
124
+ }
125
+ }
126
+ /** */
127
+ return AHoloId;
128
+ }
129
+ export class ActionId extends createHolochainId(HoloHashType.Action) {
130
+ }
131
+ export class AgentId extends createHolochainId(HoloHashType.Agent) {
132
+ }
133
+ export class DnaId extends createHolochainId(HoloHashType.Dna) {
134
+ }
135
+ export class EntryId extends createHolochainId(HoloHashType.Entry) {
136
+ }
137
+ export class ExternalId extends createHolochainId(HoloHashType.External) {
138
+ }
139
+ /** */
140
+ export function intoDhtId(input) {
141
+ try {
142
+ const actionId = new ActionId(input);
143
+ return actionId;
144
+ }
145
+ catch (e) {
146
+ const entryId = new EntryId(input);
147
+ return entryId;
148
+ }
149
+ }
150
+ /** */
151
+ export function intoLinkableId(input) {
152
+ try {
153
+ const dhtId = intoDhtId(input);
154
+ return dhtId;
155
+ }
156
+ catch (e) {
157
+ const externalId = new ExternalId(input);
158
+ return externalId;
159
+ }
160
+ }
161
+ //
162
+ // /** */
163
+ // export class DnaId extends HoloId {
164
+ // constructor(input: HoloHashB64 | HoloHash) {
165
+ // super(input, HoloHashType.Dna);
166
+ // const type = getHashType(this.value);
167
+ // if (HoloHashType.Dna != type) {
168
+ // throw new Error('The hash does not have the "Dna" type');
169
+ // }
170
+ // }
171
+ // }
172
+ //
173
+ //
174
+ // /** */
175
+ // export class AgentId extends HoloId {
176
+ // constructor(input: HoloHashB64 | HoloHash) {
177
+ // super(input, HoloHashType.Agent);
178
+ // const type = getHashType(this.value);
179
+ // if (HoloHashType.Agent != type) {
180
+ // throw new Error('The hash does not have the "Agent" type');
181
+ // }
182
+ // }
183
+ // }
184
+ /** ------- */
185
+ // export type HoloIdConstructor<T = {}> = new (input: HoloHashB64 | HoloHash) => T;
186
+ //
187
+ // export function HoloIdMixin<TBase extends GConstructor>(Base: TBase) {
188
+ // class AHoloId extends Base {
189
+ // public readonly b64: HoloHashB64;
190
+ //
191
+ // //constructor(...args: any[]) {super(args[0])}
192
+ //
193
+ // /** Validate */
194
+ // constructor(...args: any[]) {
195
+ // super(args);
196
+ // if (args.length != 1) {
197
+ // throw new Error("HoloId ctor must have exactly 1 argument.");
198
+ // }
199
+ // let input = args[0];
200
+ // if (typeof (input) != 'string') {
201
+ // input = enc64(input);
202
+ // }
203
+ // this.b64 = input;
204
+ // if (this.b64.length !== 53) {
205
+ // throw new Error("The hash must be exactly 53 characters long.");
206
+ // }
207
+ // }
208
+ //
209
+ // get hash(): HoloHash {
210
+ // return dec64(this.b64)
211
+ // }
212
+ //
213
+ // /** First 8 chars of the Core */
214
+ // get short(): string {
215
+ // return this.b64.slice(5, 13);
216
+ // }
217
+ // }
218
+ // return AHoloId;
219
+ // }
220
+ //
221
+ // export const HoloId = HoloIdMixin(Empty);
222
+ //
223
+ //
224
+ // /** */
225
+ // export class AgentId extends HoloId {
226
+ // constructor(input: HoloHashB64 | HoloHash) {
227
+ // super(input, HoloHashType.Agent);
228
+ // const type = getHashType(this.b64);
229
+ // if (HoloHashType.Agent != type) {
230
+ // throw new Error('The hash does not have the correct type: ' + HoloHashType.Agent);
231
+ // }
232
+ // }
233
+ // }
234
+ //
235
+ //
236
+ // /** */
237
+ // export class DnaId extends HoloIdMixin(Empty) {
238
+ // constructor(input: HoloHashB64 | HoloHash) {
239
+ // super(input, HoloHashType.Dna);
240
+ // const type = getHashType(this.b64);
241
+ // if (HoloHashType.Dna != type) {
242
+ // throw new Error('The hash does not have the correct type: ' + HoloHashType.Dna);
243
+ // }
244
+ // }
245
+ // }
246
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,oBAAoB,EAAE,kBAAkB,EAAwB,MAAM,mBAAmB,CAAC;AAKlG;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAQ;IAC/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AU,CAAC,KAAK,CAAC,CAAC;QACzC,OAAO,UAAU,CAAC;KACnB;AACH,CAAC;AAED,EAAE;AACF,SAAS;AACT,sCAAsC;AACtC,iDAAiD;AACjD,sCAAsC;AACtC,4CAA4C;AAC5C,sCAAsC;AACtC,kEAAkE;AAClE,QAAQ;AACR,MAAM;AACN,IAAI;AACJ,EAAE;AACF,EAAE;AACF,SAAS;AACT,wCAAwC;AACxC,iDAAiD;AACjD,wCAAwC;AACxC,4CAA4C;AAC5C,wCAAwC;AACxC,oEAAoE;AACpE,QAAQ;AACR,MAAM;AACN,IAAI;AAGJ,cAAc;AACd,oFAAoF;AACpF,EAAE;AACF,yEAAyE;AACzE,iCAAiC;AACjC,wCAAwC;AACxC,EAAE;AACF,qDAAqD;AACrD,EAAE;AACF,sBAAsB;AACtB,oCAAoC;AACpC,qBAAqB;AACrB,gCAAgC;AAChC,wEAAwE;AACxE,UAAU;AACV,6BAA6B;AAC7B,0CAA0C;AAC1C,gCAAgC;AAChC,UAAU;AACV,0BAA0B;AAC1B,sCAAsC;AACtC,2EAA2E;AAC3E,UAAU;AACV,QAAQ;AACR,EAAE;AACF,6BAA6B;AAC7B,+BAA+B;AAC/B,QAAQ;AACR,EAAE;AACF,uCAAuC;AACvC,4BAA4B;AAC5B,sCAAsC;AACtC,QAAQ;AACR,MAAM;AACN,oBAAoB;AACpB,IAAI;AACJ,EAAE;AACF,4CAA4C;AAC5C,EAAE;AACF,EAAE;AACF,SAAS;AACT,wCAAwC;AACxC,iDAAiD;AACjD,wCAAwC;AACxC,0CAA0C;AAC1C,wCAAwC;AACxC,2FAA2F;AAC3F,QAAQ;AACR,MAAM;AACN,IAAI;AACJ,EAAE;AACF,EAAE;AACF,SAAS;AACT,kDAAkD;AAClD,iDAAiD;AACjD,sCAAsC;AACtC,0CAA0C;AAC1C,sCAAsC;AACtC,yFAAyF;AACzF,QAAQ;AACR,MAAM;AACN,IAAI","sourcesContent":["import {decodeHashFromBase64, encodeHashToBase64, HoloHash, HoloHashB64} from \"@holochain/client\";\r\nimport {AbstractConstructor, CellMixin, Empty, GConstructor, ZomeMixin, ZomeSpecific} from \"./mixins\";\r\nimport {Cell} from \"./cell\";\r\n\r\n\r\n/**\r\n * Checks if obj is a Hash or list of hashes and tries to convert it a B64 or list of B64\r\n */\r\nexport function anyToB64(obj: any): any {\r\n /** Check if it's a hash */\r\n if (obj instanceof Uint8Array) {\r\n return encodeHashToBase64(obj);\r\n } else {\r\n /** Check if it's an array of hashes */\r\n if (Array.isArray(obj)) {\r\n const isUint8Array =\r\n obj.length > 0 &&\r\n obj.every((value) => {\r\n return value instanceof Uint8Array;\r\n });\r\n if (isUint8Array) {\r\n let result = [];\r\n for (const cur of obj) {\r\n result.push(encodeHashToBase64(cur));\r\n }\r\n return result;\r\n }\r\n }\r\n }\r\n return obj;\r\n}\r\n\r\n\r\n/** ------------------------------------------------------------------------------------------------------------------*/\r\n\r\nexport enum HoloHashType {\r\n Action = \"Action\",\r\n Agent = \"Agent\",\r\n //DhtOp = \"DhtOp\",\r\n Dna = \"Dna\",\r\n Entry = \"Entry\",\r\n External = \"External\",\r\n Network = \"Network\",\r\n //Warrent = \"Warrent\",\r\n Wasm = \"Wasm\",\r\n}\r\n\r\nexport const HASH_TYPE_PREFIX_B64 = {\r\n Action: \"uhCkk\",\r\n Agent: \"uhCAk\",\r\n Dna: \"uhC0k\",\r\n //DhtOp: \"hCQk\",\r\n Entry: \"uhCEk\",\r\n External: \"uhC8k\",\r\n Network: \"uhCIk\",\r\n //Warrent: \"Warrent\",\r\n Wasm: \"uhCok\",\r\n};\r\n\r\nexport function getHashType(hash: HoloHashB64): HoloHashType {\r\n const hashExt = hash.slice(0, 5);\r\n const hashPrefixes = Object.values(HASH_TYPE_PREFIX_B64)\r\n for (let i = 0; i < hashPrefixes.length; i+= 1) {\r\n if (hashPrefixes[i] == hashExt) {\r\n return Object.keys(HASH_TYPE_PREFIX_B64)[i] as HoloHashType;\r\n }\r\n }\r\n throw Error(\"Unknown hash type\");\r\n}\r\n\r\n\r\n/** */\r\nexport function isHashTypeB64(hash: HoloHashB64, hashType: HoloHashType) {\r\n const slice = hash.slice(0, 5);\r\n const prefix = HASH_TYPE_PREFIX_B64[hashType];\r\n for (let i = 0; i < prefix.length; i++) {\r\n if (slice[i] !== prefix[i]) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n}\r\n\r\n\r\nexport function hasHoloHashType(hash: HoloHashB64): boolean {\r\n return !!Object.values(HASH_TYPE_PREFIX_B64).find((prefix) => hash.startsWith(`${prefix}`));\r\n}\r\n\r\n\r\n/** */\r\nexport function validateHashB64(hash: HoloHashB64) {\r\n if (!hash || typeof(hash) != 'string') {\r\n throw new Error(\"The hash must be a valid string\");\r\n }\r\n if (hash.length !== 53) {\r\n throw new Error(\"The hash must be exactly 53 characters long.\");\r\n }\r\n if (!hasHoloHashType(hash)) {\r\n throw new Error(\"The hash must have a valid HoloHash type.\");\r\n\r\n }\r\n}\r\n\r\n\r\nexport function dec64(hash: HoloHashB64): HoloHash {\r\n validateHashB64(hash);\r\n return decodeHashFromBase64(hash);\r\n}\r\nexport function enc64(hash: HoloHash): HoloHashB64 {\r\n let b64 = encodeHashToBase64(hash);\r\n validateHashB64(b64);\r\n return b64;\r\n}\r\n\r\n\r\n/** HoloHash starts with 'u' has a type and is 53 chars long */\r\nexport abstract class HolochainId {\r\n public readonly b64: HoloHashB64;\r\n //private readonly hash: HoloHash;\r\n\r\n /** Validate */\r\n constructor(input: HoloHashB64 | HoloHash, public readonly type: HoloHashType) {\r\n if (typeof(input) != 'string') {\r\n input = encodeHashToBase64(input);\r\n }\r\n validateHashB64(input);\r\n this.b64 = input;\r\n }\r\n\r\n get hash(): HoloHash { return dec64(this.b64) }\r\n /** First 8 chars of the Core */\r\n get short(): string { return this.b64.slice(5, 13); }\r\n\r\n toString(): string {return this.b64;}\r\n\r\n print(): string { return `${this.short} (${this.type})`}\r\n}\r\n\r\n\r\n/** Mixin */\r\nexport function createHolochainId(hashType: HoloHashType) {\r\n class AHoloId extends HolochainId {\r\n constructor(input: HoloHashB64 | HoloHash) {\r\n super(input, hashType);\r\n const type = getHashType(this.b64);\r\n if (hashType != type) {\r\n throw new Error('The hash does not have the correct type. Expected ' + hashType + ', got' + type);\r\n }\r\n }\r\n }\r\n /** */\r\n return AHoloId;\r\n}\r\n\r\nexport class ActionId extends createHolochainId(HoloHashType.Action) {}\r\nexport class AgentId extends createHolochainId(HoloHashType.Agent) {}\r\nexport class DnaId extends createHolochainId(HoloHashType.Dna) {}\r\nexport class EntryId extends createHolochainId(HoloHashType.Entry) {}\r\nexport class ExternalId extends createHolochainId(HoloHashType.External) {}\r\n\r\nexport type AnyDhtId = ActionId | EntryId;\r\nexport type AnyLinkableId = AnyDhtId | ExternalId;\r\n\r\n\r\n/** */\r\nexport function intoDhtId(input: HoloHashB64 | HoloHash): AnyDhtId {\r\n try {\r\n const actionId = new ActionId(input);\r\n return actionId;\r\n } catch(e) {\r\n const entryId = new EntryId(input);\r\n return entryId;\r\n }\r\n}\r\n\r\n\r\n/** */\r\nexport function intoLinkableId(input: HoloHashB64 | HoloHash): AnyLinkableId {\r\n try {\r\n const dhtId = intoDhtId(input);\r\n return dhtId;\r\n } catch(e) {\r\n const externalId = new ExternalId(input);\r\n return externalId;\r\n }\r\n}\r\n\r\n//\r\n// /** */\r\n// export class DnaId extends HoloId {\r\n// constructor(input: HoloHashB64 | HoloHash) {\r\n// super(input, HoloHashType.Dna);\r\n// const type = getHashType(this.value);\r\n// if (HoloHashType.Dna != type) {\r\n// throw new Error('The hash does not have the \"Dna\" type');\r\n// }\r\n// }\r\n// }\r\n//\r\n//\r\n// /** */\r\n// export class AgentId extends HoloId {\r\n// constructor(input: HoloHashB64 | HoloHash) {\r\n// super(input, HoloHashType.Agent);\r\n// const type = getHashType(this.value);\r\n// if (HoloHashType.Agent != type) {\r\n// throw new Error('The hash does not have the \"Agent\" type');\r\n// }\r\n// }\r\n// }\r\n\r\n\r\n/** ------- */\r\n// export type HoloIdConstructor<T = {}> = new (input: HoloHashB64 | HoloHash) => T;\r\n//\r\n// export function HoloIdMixin<TBase extends GConstructor>(Base: TBase) {\r\n// class AHoloId extends Base {\r\n// public readonly b64: HoloHashB64;\r\n//\r\n// //constructor(...args: any[]) {super(args[0])}\r\n//\r\n// /** Validate */\r\n// constructor(...args: any[]) {\r\n// super(args);\r\n// if (args.length != 1) {\r\n// throw new Error(\"HoloId ctor must have exactly 1 argument.\");\r\n// }\r\n// let input = args[0];\r\n// if (typeof (input) != 'string') {\r\n// input = enc64(input);\r\n// }\r\n// this.b64 = input;\r\n// if (this.b64.length !== 53) {\r\n// throw new Error(\"The hash must be exactly 53 characters long.\");\r\n// }\r\n// }\r\n//\r\n// get hash(): HoloHash {\r\n// return dec64(this.b64)\r\n// }\r\n//\r\n// /** First 8 chars of the Core */\r\n// get short(): string {\r\n// return this.b64.slice(5, 13);\r\n// }\r\n// }\r\n// return AHoloId;\r\n// }\r\n//\r\n// export const HoloId = HoloIdMixin(Empty);\r\n//\r\n//\r\n// /** */\r\n// export class AgentId extends HoloId {\r\n// constructor(input: HoloHashB64 | HoloHash) {\r\n// super(input, HoloHashType.Agent);\r\n// const type = getHashType(this.b64);\r\n// if (HoloHashType.Agent != type) {\r\n// throw new Error('The hash does not have the correct type: ' + HoloHashType.Agent);\r\n// }\r\n// }\r\n// }\r\n//\r\n//\r\n// /** */\r\n// export class DnaId extends HoloIdMixin(Empty) {\r\n// constructor(input: HoloHashB64 | HoloHash) {\r\n// super(input, HoloHashType.Dna);\r\n// const type = getHashType(this.b64);\r\n// if (HoloHashType.Dna != type) {\r\n// throw new Error('The hash does not have the correct type: ' + HoloHashType.Dna);\r\n// }\r\n// }\r\n// }\r\n"]}
@@ -0,0 +1,32 @@
1
+ import { HoloHashB64 } from "@holochain/client";
2
+ import { ActionId, AgentId, DnaId, EntryId, HolochainId } from "./hash";
3
+ import { GConstructor } from "./mixins";
4
+ export declare class HolochainIdMap<K extends HolochainId, V> implements Map<K, V> {
5
+ private _ctor;
6
+ _map: Map<HoloHashB64, V>;
7
+ constructor(_ctor: GConstructor<K>, initialEntries?: Array<[K, V]>);
8
+ has(key: K): boolean;
9
+ get(key: K): V;
10
+ set(key: K, value: V): this;
11
+ delete(key: K): boolean;
12
+ values(): IterableIterator<V>;
13
+ keys(): IterableIterator<K>;
14
+ entries(): IterableIterator<[K, V]>;
15
+ clear(): void;
16
+ forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void;
17
+ get size(): number;
18
+ [Symbol.iterator](): IterableIterator<[K, V]>;
19
+ get [Symbol.toStringTag](): string;
20
+ }
21
+ export declare class ActionIdMap<T> extends HolochainIdMap<ActionId, T> {
22
+ constructor(initialEntries?: Array<[ActionId, T]>);
23
+ }
24
+ export declare class AgentIdMap<T> extends HolochainIdMap<AgentId, T> {
25
+ constructor(initialEntries?: Array<[AgentId, T]>);
26
+ }
27
+ export declare class DnaIdMap<T> extends HolochainIdMap<DnaId, T> {
28
+ constructor(initialEntries?: Array<[DnaId, T]>);
29
+ }
30
+ export declare class EntryIdMap<T> extends HolochainIdMap<EntryId, T> {
31
+ constructor(initialEntries?: Array<[EntryId, T]>);
32
+ }
@@ -0,0 +1,74 @@
1
+ import { ActionId, AgentId, DnaId, EntryId } from "./hash";
2
+ export class HolochainIdMap {
3
+ constructor(_ctor, initialEntries) {
4
+ this._ctor = _ctor;
5
+ this._map = new Map();
6
+ if (initialEntries) {
7
+ for (const [key, value] of initialEntries) {
8
+ this.set(key, value);
9
+ }
10
+ }
11
+ }
12
+ has(key) {
13
+ return this._map.has(key.b64);
14
+ }
15
+ get(key) {
16
+ return this._map.get(key.b64);
17
+ }
18
+ set(key, value) {
19
+ this._map.set(key.b64, value);
20
+ return this;
21
+ }
22
+ delete(key) {
23
+ return this._map.delete(key.b64);
24
+ }
25
+ values() {
26
+ return this._map.values();
27
+ }
28
+ keys() {
29
+ return Array.from(this._map.keys())
30
+ .map((h) => new this._ctor(h))[Symbol.iterator]();
31
+ }
32
+ entries() {
33
+ return Array.from(this._map.entries())
34
+ .map(([h, v]) => [new this._ctor(h), v])[Symbol.iterator]();
35
+ }
36
+ clear() {
37
+ return this._map.clear();
38
+ }
39
+ forEach(callbackfn, thisArg) {
40
+ return this._map.forEach((value, key) => {
41
+ callbackfn(value, new this._ctor(key), this);
42
+ }, thisArg);
43
+ }
44
+ get size() {
45
+ return this._map.size;
46
+ }
47
+ [Symbol.iterator]() {
48
+ return this.entries();
49
+ }
50
+ get [Symbol.toStringTag]() {
51
+ return this._map[Symbol.toStringTag];
52
+ }
53
+ }
54
+ export class ActionIdMap extends HolochainIdMap {
55
+ constructor(initialEntries) {
56
+ super(ActionId, initialEntries);
57
+ }
58
+ }
59
+ export class AgentIdMap extends HolochainIdMap {
60
+ constructor(initialEntries) {
61
+ super(AgentId, initialEntries);
62
+ }
63
+ }
64
+ export class DnaIdMap extends HolochainIdMap {
65
+ constructor(initialEntries) {
66
+ super(DnaId, initialEntries);
67
+ }
68
+ }
69
+ export class EntryIdMap extends HolochainIdMap {
70
+ constructor(initialEntries) {
71
+ super(EntryId, initialEntries);
72
+ }
73
+ }
74
+ //# sourceMappingURL=holochain-id-map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"holochain-id-map.js","sourceRoot":"","sources":["../src/holochain-id-map.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAc,MAAM,QAAQ,CAAC;AAItE,MAAM,OAAO,cAAc;IAGzB,YAAoB,KAAsB,EAAE,cAA8B;QAAtD,UAAK,GAAL,KAAK,CAAiB;QACxC,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,cAAc,EAAE;YAClB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,cAAc,EAAE;gBACzC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;aACtB;SACF;IACH,CAA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AAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YACtC,UAAU,CAAC,KAAK,EAAE,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;QAC/C,CAAC,EAAE,OAAO,CAAC,CAAC;IACd,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;IACxB,CAAC;IAED,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;QACtB,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACvC,CAAC;CACF;AAID,MAAM,OAAO,WAAe,SAAQ,cAA2B;IAC7D,YAAY,cAAqC;QAC/C,KAAK,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAA;IACjC,CAAC;CACF;AAED,MAAM,OAAO,UAAc,SAAQ,cAA0B;IAC3D,YAAY,cAAoC;QAC9C,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,CAAA;IAChC,CAAC;CACF;AAED,MAAM,OAAO,QAAY,SAAQ,cAAwB;IACvD,YAAY,cAAkC;QAC5C,KAAK,CAAC,KAAK,EAAE,cAAc,CAAC,CAAA;IAC9B,CAAC;CACF;AAED,MAAM,OAAO,UAAc,SAAQ,cAA0B;IAC3D,YAAY,cAAoC;QAC9C,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,CAAA;IAChC,CAAC;CACF","sourcesContent":["import {\n HoloHashB64,\n} from \"@holochain/client\";\nimport {ActionId, AgentId, DnaId, EntryId, HolochainId} from \"./hash\";\nimport {GConstructor} from \"./mixins\";\n\n\nexport class HolochainIdMap<K extends HolochainId, V> implements Map<K, V> {\n _map: Map<HoloHashB64, V>;\n\n constructor(private _ctor: GConstructor<K>, initialEntries?: Array<[K, V]>) {\n this._map = new Map();\n if (initialEntries) {\n for (const [key, value] of initialEntries) {\n this.set(key, value);\n }\n }\n }\n\n has(key: K) {\n return this._map.has(key.b64);\n }\n\n get(key: K): V {\n return this._map.get(key.b64);\n }\n\n set(key: K, value: V) {\n this._map.set(key.b64, value);\n return this;\n }\n\n delete(key: K) {\n return this._map.delete(key.b64);\n }\n\n values() {\n return this._map.values();\n }\n\n keys() {\n return Array.from(this._map.keys())\n .map((h) => new this._ctor(h))\n [Symbol.iterator]();\n }\n\n entries() {\n return Array.from(this._map.entries())\n .map(([h, v]) => [new this._ctor(h), v] as [K, V])\n [Symbol.iterator]();\n }\n\n clear() {\n return this._map.clear();\n }\n\n forEach(\n callbackfn: (value: V, key: K, map: Map<K, V>) => void,\n thisArg?: any\n ): void {\n return this._map.forEach((value, key) => {\n callbackfn(value, new this._ctor(key), this);\n }, thisArg);\n }\n\n get size() {\n return this._map.size;\n }\n\n [Symbol.iterator](): IterableIterator<[K, V]> {\n return this.entries();\n }\n\n get [Symbol.toStringTag](): string {\n return this._map[Symbol.toStringTag];\n }\n}\n\n\n\nexport class ActionIdMap<T> extends HolochainIdMap<ActionId, T> {\n constructor(initialEntries?: Array<[ActionId, T]>) {\n super(ActionId, initialEntries)\n }\n}\n\nexport class AgentIdMap<T> extends HolochainIdMap<AgentId, T> {\n constructor(initialEntries?: Array<[AgentId, T]>) {\n super(AgentId, initialEntries)\n }\n}\n\nexport class DnaIdMap<T> extends HolochainIdMap<DnaId, T> {\n constructor(initialEntries?: Array<[DnaId, T]>) {\n super(DnaId, initialEntries)\n }\n}\n\nexport class EntryIdMap<T> extends HolochainIdMap<EntryId, T> {\n constructor(initialEntries?: Array<[EntryId, T]>) {\n super(EntryId, initialEntries)\n }\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -8,3 +8,7 @@ export * from "./types";
8
8
  export * from "./hcl";
9
9
  export * from "./mixins";
10
10
  export * from "./cell";
11
+ export * from "./pretty";
12
+ export * from "./hash";
13
+ export * from "./holochain-id-map";
14
+ export * from "./zomeSignals.types";
package/dist/index.js CHANGED
@@ -8,4 +8,8 @@ export * from "./types";
8
8
  export * from "./hcl";
9
9
  export * from "./mixins";
10
10
  export * from "./cell";
11
+ export * from "./pretty";
12
+ export * from "./hash";
13
+ export * from "./holochain-id-map";
14
+ export * from "./zomeSignals.types";
11
15
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC","sourcesContent":["export * from \"./AppProxy\";\nexport * from \"./ConductorAppProxy\";\nexport * from \"./ExternalAppProxy\";\nexport * from \"./CellProxy\";\nexport * from \"./ZomeProxy\";\nexport * from \"./utils\";\nexport * from \"./types\";\nexport * from \"./hcl\";\nexport * from \"./mixins\";\nexport * from \"./cell\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC","sourcesContent":["export * from \"./AppProxy\";\nexport * from \"./ConductorAppProxy\";\nexport * from \"./ExternalAppProxy\";\nexport * from \"./CellProxy\";\nexport * from \"./ZomeProxy\";\nexport * from \"./utils\";\nexport * from \"./types\";\nexport * from \"./hcl\";\nexport * from \"./mixins\";\nexport * from \"./cell\";\nexport * from \"./pretty\";\nexport * from \"./hash\";\nexport * from \"./holochain-id-map\";\nexport * from \"./zomeSignals.types\";\n"]}
package/dist/mixins.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import { FunctionName, ZomeName } from "@holochain/client";
2
2
  import { Cell } from "./cell";
3
+ export type Constructor<T> = {
4
+ new (): T;
5
+ };
6
+ export type GConstructor<T = {}> = new (...args: any[]) => T;
3
7
  export type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T;
4
8
  export declare class Empty {
5
9
  constructor(...args: any[]);
@@ -1 +1 @@
1
- {"version":3,"file":"mixins.js","sourceRoot":"","sources":["../src/mixins.ts"],"names":[],"mappings":"AAWA,MAAM,OAAO,KAAK;IAChB,YAAY,GAAG,IAAW,IAAG,CAAC;CAC/B;AAGD,mGAAmG;AAEnG;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAoC,IAAW;IACtE,MAAe,KAAM,SAAQ,IAAI;QAM/B,IAAI,IAAI,KAAW,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,MAAM,KAAK,CAAC,+BAA+B,CAAC,CAAE,CAAC,OAAO,IAAI,CAAC,KAAM,CAAA,CAAC,CAAC;KAExG;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,+CAA+C;AAI/C,mGAAmG;AAEnG;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAoC,IAAW;IACtE,MAAe,aAAc,SAAQ,IAAI;QAOvC,YAAY,GAAG,IAAW;YACxB,KAAK,CAAC,IAAI,CAAC,CAAC;YACZ,IAAI,CAAC,QAAQ,GAAI,IAAI,CAAC,WAAoC,CAAC,iBAAiB,CAAA;QAC9E,CAAC;QAED,IAAI,eAAe;YACjB,OAAQ,IAAI,CAAC,WAAoC,CAAC,iBAAiB,CAAC;QACtE,CAAC;QAED,iCAAiC;QACjC,IAAI,OAAO;YACT,MAAM,OAAO,GAAI,IAAI,CAAC,WAAoC,CAAC,QAAQ,CAAC;YACpE,IAAI,CAAC,OAAO,EAAE;gBACZ,MAAM,KAAK,CAAC,6CAA6C,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;aACpF;YACD,OAAQ,OAA0B,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;gBAChD,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;YAChC,CAAC,CAAC,CAAA;QACJ,CAAC;KACF;IAED,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC","sourcesContent":["import {\n FunctionName,\n ZomeName\n} from \"@holochain/client\";\nimport {Cell} from \"./cell\";\n\ntype Constructor<T> = {new (): T};\ntype GConstructor<T = {}> = new (...args: any[]) => T;\nexport type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T\n\n\nexport class Empty {\n constructor(...args: any[]) {}\n}\n\n\n/** ------------------------------------------------------------------------------------------- **/\n\n/**\n * Mixin for Cell bound classes.\n * A Cell bound class must have a \"Provisioned\" or \"Cloned\" cell from @holochain/client\n */\nexport function CellMixin<TBase extends AbstractConstructor>(Base: TBase) {\n abstract class ACell extends Base {\n // constructor(...args: any[]){\n // super(args);\n // }\n _cell?: Cell;\n\n get cell(): Cell { if (!this._cell) throw Error(\"Cell field not set for object\") ; return this._cell! }\n\n }\n\n return ACell;\n}\n\n//export const CellSpecific = CellMixin(Empty);\n\n\n\n/** ------------------------------------------------------------------------------------------- **/\n\n/**\n * Mixin for Zome bound classes.\n * A Zome bound class must have a zome name and a list of zome functions\n */\nexport function ZomeMixin<TBase extends AbstractConstructor>(Base: TBase) {\n abstract class AZomeSpecific extends Base {\n\n static readonly DEFAULT_ZOME_NAME: ZomeName;\n zomeName: ZomeName;\n\n static readonly FN_NAMES: FunctionName[];\n\n constructor(...args: any[]){\n super(args);\n this.zomeName = (this.constructor as typeof AZomeSpecific).DEFAULT_ZOME_NAME\n }\n\n get defaultZomeName(): ZomeName {\n return (this.constructor as typeof AZomeSpecific).DEFAULT_ZOME_NAME;\n }\n\n /** Tuple array with zome name */\n get fnNames(): [ZomeName, FunctionName][] {\n const fnNames = (this.constructor as typeof AZomeSpecific).FN_NAMES;\n if (!fnNames) {\n throw Error(\"FN_NAMES not defined in ZomeProxy subclass \" + this.constructor.name);\n }\n return (fnNames as FunctionName[]).map((fnName) => {\n return [this.zomeName, fnName]\n })\n }\n }\n\n return AZomeSpecific;\n}\n\nexport const ZomeSpecific = ZomeMixin(Empty);\n\n"]}
1
+ {"version":3,"file":"mixins.js","sourceRoot":"","sources":["../src/mixins.ts"],"names":[],"mappings":"AAWA,MAAM,OAAO,KAAK;IAChB,YAAY,GAAG,IAAW,IAAG,CAAC;CAC/B;AAGD,mGAAmG;AAEnG;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAoC,IAAW;IACtE,MAAe,KAAM,SAAQ,IAAI;QAM/B,IAAI,IAAI,KAAW,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,MAAM,KAAK,CAAC,+BAA+B,CAAC,CAAE,CAAC,OAAO,IAAI,CAAC,KAAM,CAAA,CAAC,CAAC;KAExG;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,+CAA+C;AAI/C,mGAAmG;AAEnG;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAoC,IAAW;IACtE,MAAe,aAAc,SAAQ,IAAI;QAOvC,YAAY,GAAG,IAAW;YACxB,KAAK,CAAC,IAAI,CAAC,CAAC;YACZ,IAAI,CAAC,QAAQ,GAAI,IAAI,CAAC,WAAoC,CAAC,iBAAiB,CAAA;QAC9E,CAAC;QAED,IAAI,eAAe;YACjB,OAAQ,IAAI,CAAC,WAAoC,CAAC,iBAAiB,CAAC;QACtE,CAAC;QAED,iCAAiC;QACjC,IAAI,OAAO;YACT,MAAM,OAAO,GAAI,IAAI,CAAC,WAAoC,CAAC,QAAQ,CAAC;YACpE,IAAI,CAAC,OAAO,EAAE;gBACZ,MAAM,KAAK,CAAC,6CAA6C,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;aACpF;YACD,OAAQ,OAA0B,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;gBAChD,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;YAChC,CAAC,CAAC,CAAA;QACJ,CAAC;KACF;IAED,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC","sourcesContent":["import {\n FunctionName,\n ZomeName\n} from \"@holochain/client\";\nimport {Cell} from \"./cell\";\n\nexport type Constructor<T> = {new (): T};\nexport type GConstructor<T = {}> = new (...args: any[]) => T;\nexport type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T\n\n\nexport class Empty {\n constructor(...args: any[]) {}\n}\n\n\n/** ------------------------------------------------------------------------------------------- **/\n\n/**\n * Mixin for Cell bound classes.\n * A Cell bound class must have a \"Provisioned\" or \"Cloned\" cell from @holochain/client\n */\nexport function CellMixin<TBase extends AbstractConstructor>(Base: TBase) {\n abstract class ACell extends Base {\n // constructor(...args: any[]){\n // super(args);\n // }\n _cell?: Cell;\n\n get cell(): Cell { if (!this._cell) throw Error(\"Cell field not set for object\") ; return this._cell! }\n\n }\n\n return ACell;\n}\n\n//export const CellSpecific = CellMixin(Empty);\n\n\n\n/** ------------------------------------------------------------------------------------------- **/\n\n/**\n * Mixin for Zome bound classes.\n * A Zome bound class must have a zome name and a list of zome functions\n */\nexport function ZomeMixin<TBase extends AbstractConstructor>(Base: TBase) {\n abstract class AZomeSpecific extends Base {\n\n static readonly DEFAULT_ZOME_NAME: ZomeName;\n zomeName: ZomeName;\n\n static readonly FN_NAMES: FunctionName[];\n\n constructor(...args: any[]){\n super(args);\n this.zomeName = (this.constructor as typeof AZomeSpecific).DEFAULT_ZOME_NAME\n }\n\n get defaultZomeName(): ZomeName {\n return (this.constructor as typeof AZomeSpecific).DEFAULT_ZOME_NAME;\n }\n\n /** Tuple array with zome name */\n get fnNames(): [ZomeName, FunctionName][] {\n const fnNames = (this.constructor as typeof AZomeSpecific).FN_NAMES;\n if (!fnNames) {\n throw Error(\"FN_NAMES not defined in ZomeProxy subclass \" + this.constructor.name);\n }\n return (fnNames as FunctionName[]).map((fnName) => {\n return [this.zomeName, fnName]\n })\n }\n }\n\n return AZomeSpecific;\n}\n\nexport const ZomeSpecific = ZomeMixin(Empty);\n"]}
@@ -0,0 +1,17 @@
1
+ import { SignalLog } from "./AppProxy";
2
+ import { AppInfo } from "@holochain/client";
3
+ import { BaseRoleName, CellsForRole } from "./types";
4
+ export declare function prettyDuration(date: Date): string;
5
+ /** */
6
+ export declare function prettyDate(date: Date): string;
7
+ export declare function prettySignalLogs(signalLogs: SignalLog[]): {
8
+ timestamp: string;
9
+ dnaHash: string;
10
+ zome: string;
11
+ type: string;
12
+ payload: import("./zomeSignals.types").ZomeSignal;
13
+ }[];
14
+ /** */
15
+ export declare function printAppInfo(appInfo: AppInfo): string;
16
+ /** */
17
+ export declare function printCellsForRole(baseRoleName: BaseRoleName, cells: CellsForRole): string;
package/dist/pretty.js ADDED
@@ -0,0 +1,57 @@
1
+ import { CellType } from "@holochain/client";
2
+ import { str2CellId } from "./types";
3
+ import { intoStem } from "./cell";
4
+ import { enc64 } from "./hash";
5
+ const zeroPad = (num, places) => String(num).padStart(places, '0');
6
+ export function prettyDuration(date) {
7
+ return date.getSeconds() + "." + zeroPad(date.getMilliseconds(), 3);
8
+ }
9
+ /** */
10
+ export function prettyDate(date) {
11
+ return ""
12
+ + zeroPad(date.getHours(), 2)
13
+ + ":" + zeroPad(date.getMinutes(), 2)
14
+ + ":" + zeroPad(date.getSeconds(), 2)
15
+ + "." + zeroPad(date.getMilliseconds(), 3);
16
+ }
17
+ export function prettySignalLogs(signalLogs) {
18
+ return signalLogs.map((log) => {
19
+ const dnaHash = enc64(str2CellId(log.cellId)[0]).slice(-8);
20
+ return { timestamp: prettyDate(new Date(log.ts)), dnaHash, zome: log.zomeName, type: log.type, payload: log.zomeSignal };
21
+ });
22
+ }
23
+ /** */
24
+ export function printAppInfo(appInfo) {
25
+ let print = `Happ "${appInfo.installed_app_id}" info: (status: ${JSON.stringify(appInfo.status)})`;
26
+ for (const [roleName, cellInfos] of Object.entries(appInfo.cell_info)) {
27
+ for (const cellInfo of Object.values(cellInfos)) {
28
+ if (CellType.Stem in cellInfo) {
29
+ const stem = intoStem(cellInfo);
30
+ print += `\n - ${roleName}.${stem.name ? stem.name : "unnamed"}: ${enc64(stem.dna)} (stem)`;
31
+ continue;
32
+ }
33
+ if (CellType.Provisioned in cellInfo) {
34
+ const cell = cellInfo.provisioned;
35
+ print += `\n - ${roleName}: ${cell.name} | ${enc64(cell.cell_id[0])}`;
36
+ continue;
37
+ }
38
+ if (CellType.Cloned in cellInfo) {
39
+ const cell = cellInfo.cloned;
40
+ print += `\n - ${roleName}.${cell.clone_id}: ${cell.name} | ${enc64(cell.cell_id[0])}`;
41
+ continue;
42
+ }
43
+ }
44
+ }
45
+ return print;
46
+ }
47
+ /** */
48
+ export function printCellsForRole(baseRoleName, cells) {
49
+ let print = `CellsForRole "${baseRoleName}": (${enc64(cells.provisioned.cell_id[1])})\n`;
50
+ print += ` - Provisioned: ${cells.provisioned.name} | ${enc64(cells.provisioned.cell_id[0])}\n`;
51
+ print += ` - Clones : ${Object.values(cells.clones).length}\n`;
52
+ for (const [cloneId, clone] of Object.entries(cells.clones)) {
53
+ print += ` - (${clone.enabled ? "enabled" : "disabled"})${cloneId}: ${clone.name} | ${enc64(clone.cell_id[0])}\n`;
54
+ }
55
+ return print;
56
+ }
57
+ //# sourceMappingURL=pretty.js.map