@ibgib/ts-gib 0.4.9 → 0.5.0

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.
Files changed (66) hide show
  1. package/.vscode/settings.json +3 -1
  2. package/CHANGELOG.md +15 -0
  3. package/README.md +30 -5
  4. package/dist/V1/constants.d.mts +30 -1
  5. package/dist/V1/constants.d.mts.map +1 -1
  6. package/dist/V1/constants.mjs +30 -1
  7. package/dist/V1/constants.mjs.map +1 -1
  8. package/dist/V1/factory.d.mts +17 -2
  9. package/dist/V1/factory.d.mts.map +1 -1
  10. package/dist/V1/factory.mjs +15 -0
  11. package/dist/V1/factory.mjs.map +1 -1
  12. package/dist/V1/sha256v1.d.mts.map +1 -1
  13. package/dist/V1/sha256v1.mjs +1 -1
  14. package/dist/V1/sha256v1.mjs.map +1 -1
  15. package/dist/V1/transforms/fork.d.mts.map +1 -1
  16. package/dist/V1/transforms/fork.mjs +2 -1
  17. package/dist/V1/transforms/fork.mjs.map +1 -1
  18. package/dist/V1/transforms/mut8.d.mts.map +1 -1
  19. package/dist/V1/transforms/mut8.mjs +2 -1
  20. package/dist/V1/transforms/mut8.mjs.map +1 -1
  21. package/dist/V1/transforms/rel8.d.mts.map +1 -1
  22. package/dist/V1/transforms/rel8.mjs +2 -1
  23. package/dist/V1/transforms/rel8.mjs.map +1 -1
  24. package/dist/V1/transforms/transform-helper.d.mts.map +1 -1
  25. package/dist/V1/transforms/transform-helper.mjs +2 -1
  26. package/dist/V1/transforms/transform-helper.mjs.map +1 -1
  27. package/dist/V1/v1-helper.d.mts +38 -0
  28. package/dist/V1/v1-helper.d.mts.map +1 -0
  29. package/dist/V1/v1-helper.mjs +87 -0
  30. package/dist/V1/v1-helper.mjs.map +1 -0
  31. package/dist/V1/validate-helper.d.mts +83 -0
  32. package/dist/V1/validate-helper.d.mts.map +1 -0
  33. package/dist/V1/validate-helper.mjs +227 -0
  34. package/dist/V1/validate-helper.mjs.map +1 -0
  35. package/dist/helper.d.mts +0 -54
  36. package/dist/helper.d.mts.map +1 -1
  37. package/dist/helper.mjs +0 -114
  38. package/dist/helper.mjs.map +1 -1
  39. package/dist/types.d.mts +39 -8
  40. package/dist/types.d.mts.map +1 -1
  41. package/dist/types.mjs +31 -0
  42. package/dist/types.mjs.map +1 -1
  43. package/jasmine-browser.json +2 -2
  44. package/package.json +5 -1
  45. package/src/V1/constants.mts +31 -3
  46. package/src/V1/factory.mts +18 -2
  47. package/src/V1/factory.spec.mts +2 -1
  48. package/src/V1/sha256v1.mts +5 -5
  49. package/src/V1/transforms/fork.mts +2 -1
  50. package/src/V1/transforms/fork.spec.mts +2 -1
  51. package/src/V1/transforms/mut8.mts +2 -1
  52. package/src/V1/transforms/mut8.spec.mts +4 -3
  53. package/src/V1/transforms/rel8.mts +2 -1
  54. package/src/V1/transforms/rel8.spec.mts +2 -1
  55. package/src/V1/transforms/transform-helper.mts +3 -2
  56. package/src/V1/transforms/transform-helper.spec.mts +6 -5
  57. package/src/V1/v1-helper.mts +90 -0
  58. package/src/V1/validate-helper.mts +286 -0
  59. package/src/helper.mts +0 -116
  60. package/src/helper.spec.mts +47 -115
  61. package/src/types.mts +40 -9
  62. package/dist/index.cjs +0 -4
  63. package/dist/index.cjs.map +0 -1
  64. package/dist/index.d.cts +0 -4
  65. package/dist/index.d.cts.map +0 -1
  66. package/src/index.cts +0 -3
package/src/helper.mts CHANGED
@@ -1,19 +1,6 @@
1
1
  import { Ib, Gib, IbGib, IbGibAddr, IbAndGib } from './types.mjs';
2
- let crypto: any = globalThis.crypto;
3
- let { subtle } = crypto;
4
2
 
5
- export type HashAlgorithm = 'SHA-256' | 'SHA-512';
6
- export const HashAlgorithm: { [key: string]: HashAlgorithm } = {
7
- 'sha_256': 'SHA-256' as HashAlgorithm,
8
- 'sha_512': 'SHA-512' as HashAlgorithm,
9
- }
10
3
 
11
- export function clone(obj: any) {
12
- return JSON.parse(JSON.stringify(obj));
13
- }
14
- export function getTimestamp() {
15
- return (new Date()).toUTCString();
16
- }
17
4
  /**
18
5
  * Gets the ib^gib address from the given ib and gib or
19
6
  * from the ibGib object.
@@ -87,106 +74,3 @@ export function getIbAndGib({
87
74
  }
88
75
  }
89
76
  }
90
-
91
- /**
92
- * Simple hash function.
93
- *
94
- * NOTE:
95
- * This is not used for ibGib.gib values (ATOW)
96
- * but rather as a helper function for generating random UUIDs.
97
- *
98
- * @param s string to hash
99
- * @param algorithm to use, currently only 'SHA-256'
100
- */
101
- export async function hash({
102
- s,
103
- algorithm = 'SHA-256',
104
- }: {
105
- s: string,
106
- algorithm?: HashAlgorithm,
107
- }): Promise<string> {
108
- if (!s) { return ''; }
109
-
110
- const validAlgorithms = Object.values(HashAlgorithm);
111
- if (!validAlgorithms.includes(algorithm)) {
112
- console.error(`Only ${validAlgorithms} implemented`); return '';
113
- }
114
- try {
115
- const msgUint8 = new TextEncoder().encode(s);
116
- const buffer = await subtle.digest(algorithm, msgUint8);
117
- const asArray = Array.from(new Uint8Array(buffer));
118
- return asArray.map(b => b.toString(16).padStart(2, '0')).join('');
119
- } catch (e) {
120
- console.error(extractErrorMsg(e.message));
121
- return '';
122
- }
123
- }
124
-
125
- /**
126
- * Simple func to generate UUID (sha-256 hash basically).
127
- *
128
- * @param seedSize size of seed for UUID generation
129
- */
130
- export async function getUUID(seedSize = 64): Promise<string> {
131
- let uuid: string = '';
132
- if (seedSize < 32) { throw new Error(`Seed size must be at least 32`); }
133
- if (!globalThis.crypto) { throw new Error(`Cannot create UUID, as unknown crypto library version. If using node.js, v19+ is required. (E: c02cee3fd8a94f678d3f4ebe9dc49797)`); }
134
-
135
- const values = crypto.getRandomValues(new Uint8Array(16));
136
- uuid = await hash({ s: values.join('') });
137
-
138
- if (!uuid) { throw new Error(`Did not create UUID...hmm...`); }
139
-
140
- return uuid;
141
- }
142
-
143
- /**
144
- * Syntactic sugar for JSON.stringify(obj, null, 2);
145
- *
146
- * @param obj to pretty stringify
147
- */
148
- export function pretty(obj: any): string {
149
- return JSON.stringify(obj, null, 2);
150
- }
151
-
152
- /**
153
- * Just delays given number of ms.
154
- *
155
- * @param ms milliseconds to delay
156
- */
157
- export async function delay(ms: number): Promise<void> {
158
- return new Promise<void>(resolve => {
159
- setTimeout(() => {
160
- resolve();
161
- }, ms);
162
- });
163
- }
164
-
165
- /**
166
- * extracts the error message from an error object/string/falsy arg.
167
- *
168
- * ## notes
169
- *
170
- * * some libs throw errors, some throw just strings.
171
- * * who knows what else it could be.
172
- *
173
- * ## todo
174
- *
175
- * * extract inner errors/causes if we ever use this function extensively.
176
- *
177
- * @param error the error object in the catch area of the try..catch block.
178
- * @returns error.message if it's a string, error itself if it's a string, or canned error messages if it's falsy or none of the above.
179
- */
180
- export function extractErrorMsg(error: any): string {
181
- if (!error && error !== 0) {
182
- return '[error is falsy]';
183
- } else if (typeof error === 'string') {
184
- return error;
185
- } else if (typeof error.message === 'string') {
186
- return error.message;
187
- } else if (typeof error === 'number') {
188
- return JSON.stringify(error);
189
- } else {
190
- return `[error is not a string and error.message is not a string. typeof error: ${typeof error}]`;
191
- }
192
- }
@@ -1,127 +1,59 @@
1
- /**
2
- * Test helper functions.
3
- */
1
+ import { getIbAndGib, getIbGibAddr } from './helper.mjs';
2
+ import { Ib, IbGib } from './types.mjs';
3
+ import { ROOT, ROOT_ADDR } from './V1/constants.mjs';
4
4
 
5
- import * as h from './helper.mjs';
6
- import { HashAlgorithm } from './helper.mjs';
7
5
 
8
- const SOME_STRING = "This is some stringy stuff...";
9
- const SOME_STRING_HASH = "5DC14EA1027B956AD6BA51F11372DF823FCF3429B5F2063F1DDA358E0F4F2992";
10
- const SOME_STRING_HASH_512 = "C48C780DA7C43EE7047CDAB0D7A2FDF5DC050DF3291D3CAE973FB0F51BE8535668592EB25A730C95A23D3B4D57924893E7E87C86DC0F52C6D5D2E34EE41203E5";
11
- const SOME_OTHER_STRING = "This is quite a different string of stuff.";
12
- const TEST_HASHES: { [key: string]: string } = {
13
- [SOME_STRING + h.HashAlgorithm.sha_256]: SOME_STRING_HASH,
14
- [SOME_STRING + h.HashAlgorithm.sha_512]: SOME_STRING_HASH_512,
15
- [SOME_OTHER_STRING + h.HashAlgorithm.sha_256]: 'AE4C18B37B2329770E05EA3F946C6EB6DE56D2DC568E1F5CBB395E2A1556F58A',
16
- [SOME_OTHER_STRING + h.HashAlgorithm.sha_512]: '2F1A72B21C914ED459319DF4B5D70E81CEE67C4B48BC74CD6BA8F41DCEC20DCD100C914913F370B2782985268D05C590B46F3FE9005A7477BA952935D2454E53',
17
- }
6
+ describe(`getIbGibAddr`, () => {
7
+ // unsure if these would change when not in V1...(these tests are outside V1 atow)
18
8
 
19
- describe(`when cloning`, () => {
20
-
21
- it(`should copy deep objects`, async () => {
22
- const objSimple = { a: SOME_STRING };
23
- const objADeep = {
24
- levelOne: {
25
- levelTwo: {
26
- buckle: "your shoe",
27
- three: "four",
28
- objSimple: objSimple,
29
- }
30
- }
31
- };
32
- const cloneADeep = h.clone(objADeep);
33
- expect(cloneADeep?.levelOne?.levelTwo?.buckle).toBe("your shoe");
34
- expect(cloneADeep?.levelOne?.levelTwo?.three).toBe("four");
35
- expect(cloneADeep?.levelOne?.levelTwo?.objSimple).toEqual(objSimple);
36
-
37
- cloneADeep.levelOne.levelTwo.objSimple.a = SOME_OTHER_STRING;
38
-
39
- // original should **still** be the first value
40
- expect(objSimple.a).toBe(SOME_STRING);
41
- // clone should be changed.
42
- expect(cloneADeep.levelOne.levelTwo.objSimple.a).toBe(SOME_OTHER_STRING);
43
- });
44
- });
45
-
46
- describe(`when getting timestamp`, () => {
47
- it(`should get the current date as UTCString`, async () => {
48
- // implementation detail hmm....
49
- const timestamp = h.getTimestamp();
50
- const date = new Date(timestamp);
51
- const dateAsUTCString = date.toUTCString();
52
- expect(timestamp).toBe(dateAsUTCString);
9
+ it(`should get the right addr for ROOT`, async () => {
10
+ let ibGib = ROOT;
11
+ let gotten = getIbGibAddr({ ibGib });
12
+ expect(gotten).toBeTruthy();
13
+ expect(gotten).toEqual(ROOT_ADDR);
53
14
  });
54
- });
55
-
56
- describe(`when hashing (helper)`, () => {
57
- const TEST_ALGORITHMS: h.HashAlgorithm[] = Object.values(h.HashAlgorithm);
58
15
 
59
- // implicit is just SHA-256
60
- it(`should hash consistently with implicit SHA-256`, async () => {
61
- const hash = await h.hash({ s: SOME_STRING }) || "";
62
- expect(hash.toUpperCase()).toBe(TEST_HASHES[SOME_STRING + 'SHA-256']);
16
+ it(`should get the right addr for primitives`, async () => {
17
+ let ibs: Ib[] = ['7', 'foo', 'ibgib', 'wakka doodle'];
18
+ for (let i = 0; i < ibs.length; i++) {
19
+ const ib = ibs[i];
20
+ let ibGib: IbGib = { ib, gib: 'gib' };
21
+ let gotten = getIbGibAddr({ ibGib });
22
+ expect(gotten).toBeTruthy();
23
+ expect(gotten).toEqual(`${ib}^gib`);
24
+ }
63
25
  });
64
26
 
65
- for (let algorithm of TEST_ALGORITHMS) {
66
- it(`should hash consistently with explicit ${algorithm}`, async () => {
67
- // const hash = await h.hash({s: SOME_STRING, algorithm: "SHA-256"}) || "";
68
- let hash = await h.hash({ s: SOME_STRING, algorithm }) || "";
69
- expect(hash.toUpperCase()).toBe(TEST_HASHES[SOME_STRING + algorithm]);
70
- hash = await h.hash({ s: SOME_OTHER_STRING, algorithm }) || "";
71
- expect(hash.toUpperCase()).toBe(TEST_HASHES[SOME_OTHER_STRING + algorithm]);
72
- });
73
- it(`should hash without collisions, 1000 times, ${algorithm}`, async () => {
74
- const hashes: string[] = [];
75
- const salt = await h.getUUID(1024);
76
- // console.log(`salt: ${salt}`);
77
- for (let i = 0; i < 1000; i++) {
78
- const hash = await h.hash({ s: salt + i.toString(), algorithm }) || "";
79
- // console.log(hash);
80
- expect(hashes).not.toContain(hash);
81
- hashes.push(hash);
82
- }
83
- });
84
- };
85
27
  });
86
28
 
87
- describe(`when generating UUIDs`, () => {
88
- it(`shouldn't duplicate UUIDs`, async () => {
89
- const ids: string[] = [];
90
- for (let i = 0; i < 100; i++) {
91
- const id = await h.getUUID();
92
- expect(ids).not.toContain(id);
93
- ids.push(id);
29
+ describe(`getIbAndGib`, () => {
30
+ // unsure if these would change when not in V1...(these tests are outside V1 atow)
31
+
32
+ it(`should get the right ib & gib for ROOT, with ibGib param`, async () => {
33
+ let ibGib = ROOT;
34
+ let gotten = getIbAndGib({ ibGib });
35
+ expect(gotten).toBeTruthy();
36
+ expect(gotten.ib).toEqual('ib');
37
+ expect(gotten.gib).toEqual('gib');
38
+ });
39
+
40
+ it(`should get the right ib & gib for ROOT_ADDR, with ibGibAddr param`, async () => {
41
+ let gotten = getIbAndGib({ ibGibAddr: ROOT_ADDR });
42
+ expect(gotten).toBeTruthy();
43
+ expect(gotten.ib).toEqual('ib');
44
+ expect(gotten.gib).toEqual('gib');
45
+ });
46
+
47
+ it(`should get the right ib & gib for primitives`, async () => {
48
+ let ibs: Ib[] = ['7', 'foo', 'ibgib', 'wakka doodle'];
49
+ for (let i = 0; i < ibs.length; i++) {
50
+ const ib = ibs[i];
51
+ let ibGib: IbGib = { ib, gib: 'gib' };
52
+ let gotten = getIbAndGib({ ibGib });
53
+ expect(gotten).toBeTruthy();
54
+ expect(gotten.ib).toEqual(ib);
55
+ expect(gotten.gib).toEqual('gib');
94
56
  }
95
57
  });
96
- });
97
58
 
98
- describe(`when extractErrorMsg`, () => {
99
- it(`should return canned msg when error is falsy`, () => {
100
- const defaultMsg = '[error is falsy]'; // duplicate of code in helper.mjs
101
- [null, undefined, ''].forEach(error => {
102
- expect(h.extractErrorMsg(error)).withContext(JSON.stringify(error)).toEqual(defaultMsg);
103
- });
104
- });
105
- it(`should return incoming error if it is a string`, () => {
106
- ['string here', 'undefined', '42', 'ibgib'].forEach(stringError => {
107
- expect(h.extractErrorMsg(stringError)).withContext(JSON.stringify(stringError)).toEqual(stringError);
108
- });
109
- });
110
- it(`should return error.message if it's a thrown error`, () => {
111
- ['string here', 'undefined', 'something went wrong', 'danger. out of memory (E: ce86ffd7a0174c1d8ce5e56c807dd4b1)']
112
- .map(x => new Error(x))
113
- .forEach(error => {
114
- expect(h.extractErrorMsg(error)).withContext(error.message).toEqual(error.message);
115
- });
116
- });
117
- it(`should return incoming error stringified if it is a number`, () => {
118
- [1234, 0, 1_000, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER, -5, 1 / 23].forEach(numberError => {
119
- expect(h.extractErrorMsg(numberError)).withContext(JSON.stringify(numberError)).toEqual(JSON.stringify(numberError));
120
- });
121
- });
122
- it(`should return canned response with type if incoming error is none of the above`, () => {
123
- let error = { x: 1, y: 2 };
124
- let msg = h.extractErrorMsg(error);
125
- expect(msg).withContext(JSON.stringify(error)).toBeTruthy();
126
- });
127
- });
59
+ });
package/src/types.mts CHANGED
@@ -1,17 +1,44 @@
1
1
  /**
2
- * The core of the simplicity of the ibGib protocol is that
3
- * fundamentally you are taking two ibGibs and producing a third.
2
+ * @module types
4
3
  *
5
- * There are three primary functions: mut8, rel8, fork
4
+ * These are the core types at the lowest level of the ibGib protocol.
6
5
  *
7
- * These are actually different aspects of the single function of
8
- * relationship and time, either extending or creating a timeline.
6
+ * The core of the simplicity of the ibGib protocol is that fundamentally you
7
+ * are encompassing the idea of an ibgib (similar to a base class "object" or
8
+ * "thing") that has intrinsic data and relationships to extrinsic ibgib. Each
9
+ * ibgib is content addressed with a unique addressing style called its ib^gib
10
+ * address. This allows us semantics for abstracting the "content addressing"
11
+ * per use case, with various schemas allowed, but in general the `ib` is
12
+ * per-use-case metadata and the `gib` is the more conventional hash of the
13
+ * ibgib record to allow for the cryptographic guarantees one would expect. Each
14
+ * discrete frame in time of the ibgib is its own ibgib, and a timeline stream
15
+ * of that ibgib is created via the "past" relationship (`rel8n`).
9
16
  *
10
- * Mut8 is intrinsic, rel8 is extrinsic, fork is a new timeline.
11
- * Mut8 changes a timeline, rel8 changes a timeline's link(s),
12
- * fork creates a new timeline.
17
+ * New ibgib records are created via "transforms", of which atow there are three
18
+ * kinds: `mut8`, `rel8`, `fork`
19
+ *
20
+ * These are actually different aspects of the single function of relationship
21
+ * and time, either extending or creating a timeline.
22
+ *
23
+ * Mut8 is intrinsic, rel8 is extrinsic, fork makes a new timeline. Mut8
24
+ * changes a timeline, rel8 changes a timeline's link(s), fork creates a new
25
+ * timeline.
26
+ *
27
+ * ## notes
28
+ *
29
+ * I have attempted to make these version independent, though we will not know
30
+ * how successful that is until we do a V2.
31
+ */
32
+
33
+ /**
34
+ * Type alias of string (or empty string) that is meant to convey that it
35
+ * is used as the ib in an ib^gib address or an `IbGib` class.
13
36
  */
14
37
  export declare type Ib = '' | string;
38
+ /**
39
+ * Type alias of string (or empty string) that is meant to convey that it
40
+ * is used as the gib in an ib^gib address or an `IbGib` class.
41
+ */
15
42
  export declare type Gib = '' | 'gib' | string;
16
43
  /**
17
44
  * Convenient for when destructuring an IbGibAddr
@@ -27,6 +54,11 @@ export interface IbAndGib {
27
54
  * that's to decide for future.
28
55
  */
29
56
  export declare type IbGibAddr = string;
57
+ /**
58
+ * Type alias that indicates that this is intended to be a tjp (temporal
59
+ * junction point) address.
60
+ */
61
+ export declare type TjpIbGibAddr = IbGibAddr;
30
62
  export declare type IbGibRel8ns = {
31
63
  /**
32
64
  * The key is the rel8nName.
@@ -224,7 +256,6 @@ export interface TransformOpts_Rel8<TSrc extends IbGib = IbGib> extends Transfor
224
256
  */
225
257
  rel8nsToRemoveByAddr?: IbGibRel8ns;
226
258
  }
227
-
228
259
  export interface TransformResult<TOut extends IbGib> {
229
260
  /**
230
261
  * The final result of the transformation
package/dist/index.cjs DELETED
@@ -1,4 +0,0 @@
1
- export * as V1 from './V1/index.mjs';
2
- export * from './types.mjs';
3
- export * from './helper.mjs';
4
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.cts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
package/dist/index.d.cts DELETED
@@ -1,4 +0,0 @@
1
- export * as V1 from './V1/index.mjs';
2
- export * from './types.mjs';
3
- export * from './helper.mjs';
4
- //# sourceMappingURL=index.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.cts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
package/src/index.cts DELETED
@@ -1,3 +0,0 @@
1
- export * as V1 from './V1/index.mjs';
2
- export * from './types.mjs';
3
- export * from './helper.mjs';