mnemonica 1.3.7 → 1.3.9

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 (57) hide show
  1. package/FOR_HUMANS.md +79 -7
  2. package/build/api/errors/exceptionConstructor.js +3 -3
  3. package/build/api/errors/index.d.ts +3 -7
  4. package/build/api/errors/index.js +3 -5
  5. package/build/api/errors/throwModificationError.js +2 -4
  6. package/build/api/hooks/HookInvocation.js +1 -1
  7. package/build/api/hooks/flowCheckers.d.ts +3 -3
  8. package/build/api/hooks/flowCheckers.js +1 -1
  9. package/build/api/hooks/invokeHook.js +1 -1
  10. package/build/api/index.d.ts +3 -3
  11. package/build/api/types/InstanceCreator.js +6 -7
  12. package/build/api/types/InstanceModificator.js +6 -3
  13. package/build/api/types/TypeProxy.js +2 -2
  14. package/build/api/types/compileNewModificatorFunctionBody.js +1 -1
  15. package/build/api/types/index.d.ts +3 -3
  16. package/build/api/types/index.js +32 -19
  17. package/build/api/utils/index.d.ts +2 -2
  18. package/build/api/utils/index.js +31 -18
  19. package/build/constants/index.js +4 -1
  20. package/build/descriptors/errors/index.js +1 -1
  21. package/build/descriptors/types/index.js +21 -8
  22. package/build/index.d.ts +11 -13
  23. package/build/index.js +17 -18
  24. package/build/types/index.d.ts +116 -23
  25. package/build/types/index.js +1 -1
  26. package/build/utils/collectConstructors.js +1 -1
  27. package/build/utils/fork.d.ts +2 -1
  28. package/build/utils/fork.js +1 -1
  29. package/build/utils/index.js +3 -1
  30. package/build/utils/lineage.d.ts +2 -0
  31. package/build/utils/lineage.js +156 -0
  32. package/build/utils/parse.js +1 -1
  33. package/docs/UTILS.md +41 -2
  34. package/docs/async-constructors.md +6 -0
  35. package/package.json +4 -2
  36. package/src/api/errors/exceptionConstructor.ts +7 -5
  37. package/src/api/errors/index.ts +17 -16
  38. package/src/api/errors/throwModificationError.ts +4 -5
  39. package/src/api/hooks/HookInvocation.ts +2 -1
  40. package/src/api/hooks/flowCheckers.ts +8 -3
  41. package/src/api/hooks/invokeHook.ts +1 -1
  42. package/src/api/types/InstanceCreator.ts +20 -10
  43. package/src/api/types/InstanceModificator.ts +19 -4
  44. package/src/api/types/TypeProxy.ts +11 -2
  45. package/src/api/types/compileNewModificatorFunctionBody.ts +6 -0
  46. package/src/api/types/index.ts +84 -28
  47. package/src/api/utils/index.ts +60 -24
  48. package/src/constants/index.ts +6 -0
  49. package/src/descriptors/errors/index.ts +4 -2
  50. package/src/descriptors/types/index.ts +44 -17
  51. package/src/index.ts +86 -52
  52. package/src/types/index.ts +262 -36
  53. package/src/utils/collectConstructors.ts +0 -9
  54. package/src/utils/fork.ts +2 -1
  55. package/src/utils/index.ts +6 -1
  56. package/src/utils/lineage.ts +220 -0
  57. package/src/utils/parse.ts +2 -1
@@ -0,0 +1,220 @@
1
+ 'use strict';
2
+
3
+ // utils.lineage — the lineage walker (lineage-export plan, layer 3).
4
+ // Exports the lineage graph of the given instances in the lethe format —
5
+ // the cross-language contract (@mnemonica/lethe): what survives when live
6
+ // instances are forgotten. Built on the parse() primitive (the one-level
7
+ // snapshot deepParse walks): the recursion follows parse().parent, the
8
+ // REAL parent-instance links, so the per-type prototype hops never appear.
9
+
10
+ import { parse } from './parse';
11
+ import {
12
+ _getProps as getPropsRef, Props
13
+ } from '../api/types/Props';
14
+ import { constants } from '../constants';
15
+
16
+ const { SymbolConfig } = constants;
17
+
18
+ import type {
19
+ LineageGraph,
20
+ LineageNode,
21
+ LineageOptions,
22
+ LineageValue
23
+ } from '../types';
24
+
25
+ // Instance ids are implementation-specific by contract: a per-realm random
26
+ // prefix plus a process-local counter, held in a WeakMap — stable within
27
+ // one realm, never reused, nothing retained, and never compared across
28
+ // processes or languages (the fixture comparison maps them 1:1 in
29
+ // first-encounter order — see @mnemonica/lethe testdata README)
30
+ const realmPrefix = Math.random().toString( 16 )
31
+ .slice( 2, 10 );
32
+ let idCounter = 0;
33
+ const instanceIds = new WeakMap<object, string>();
34
+ const idOf = ( instance: object ): string => {
35
+ let id = instanceIds.get( instance );
36
+ if ( id === undefined ) {
37
+ idCounter += 1;
38
+ id = `${realmPrefix}:${idCounter}`;
39
+ instanceIds.set( instance, id );
40
+ }
41
+ return id;
42
+ };
43
+
44
+ const unsupported = ( kind: string ): LineageValue => {
45
+ const result: LineageValue = {
46
+ '$mnemonica' : 'unsupported',
47
+ kind
48
+ };
49
+ return result;
50
+ };
51
+
52
+ const isInstance = ( value: object ): boolean => {
53
+ const instanceCheck = getPropsRef( value ) !== undefined;
54
+ return instanceCheck;
55
+ };
56
+
57
+ export const lineage = ( instances: object[], options?: LineageOptions ): LineageGraph => {
58
+
59
+ const nodes: Record<string, LineageNode> = {};
60
+ const nodePaths: Record<string, string> = {};
61
+ const visiting = new Set<string>();
62
+
63
+ const exportValue = ( value: unknown, seen: Set<unknown> ): LineageValue => {
64
+
65
+ if ( value === null || value === undefined ) {
66
+ return null;
67
+ }
68
+
69
+ if ( typeof value === 'boolean' || typeof value === 'string' ) {
70
+ return value;
71
+ }
72
+
73
+ if ( typeof value === 'number' ) {
74
+ if ( Number.isNaN( value ) ) {
75
+ const nanResult = unsupported( 'nan' );
76
+ return nanResult;
77
+ }
78
+ if ( value === Infinity ) {
79
+ const infResult = unsupported( '+inf' );
80
+ return infResult;
81
+ }
82
+ if ( value === -Infinity ) {
83
+ const ninfResult = unsupported( '-inf' );
84
+ return ninfResult;
85
+ }
86
+ return value;
87
+ }
88
+
89
+ // the schema's kind enum is the cross-language set: JS-only shapes
90
+ // (symbols, bigints) report as 'invalid' — never an error
91
+ if ( typeof value === 'bigint' || typeof value === 'symbol' ) {
92
+ const invalidResult = unsupported( 'invalid' );
93
+ return invalidResult;
94
+ }
95
+ if ( typeof value === 'function' ) {
96
+ const funcResult = unsupported( 'func' );
97
+ return funcResult;
98
+ }
99
+
100
+ // a field holding another mnemonica instance exports as $ref; the
101
+ // referenced instance joins the graph at first encounter
102
+ if ( isInstance( value as object ) ) {
103
+ const refResult: LineageValue = {
104
+ $ref : visit( value as object )
105
+ };
106
+ return refResult;
107
+ }
108
+
109
+ if ( seen.has( value ) ) {
110
+ const cycleResult = unsupported( 'cycle' );
111
+ return cycleResult;
112
+ }
113
+ seen.add( value );
114
+
115
+ let objectResult: LineageValue;
116
+ if ( Array.isArray( value ) ) {
117
+ const arrayResult: LineageValue[] = [];
118
+ value.forEach( ( item ) => {
119
+ arrayResult.push( exportValue( item, seen ) );
120
+ } );
121
+ objectResult = arrayResult;
122
+ } else {
123
+ const recordResult: { [ key: string ]: LineageValue } = {};
124
+ Object.keys( value as object ).forEach( ( key ) => {
125
+ recordResult[ key ] = exportValue(
126
+ ( value as Record<string, unknown> )[ key ],
127
+ seen
128
+ );
129
+ } );
130
+ objectResult = recordResult;
131
+ }
132
+ seen.delete( value );
133
+ return objectResult;
134
+ };
135
+
136
+ const visit = ( instance: object ): string => {
137
+
138
+ const id = idOf( instance );
139
+ // dedup at any depth; the visiting set also breaks instance-valued
140
+ // field cycles (a field pointing at its own instance)
141
+ if ( nodes[ id ] !== undefined || visiting.has( id ) ) {
142
+ return id;
143
+ }
144
+ visiting.add( id );
145
+
146
+ const parsed = parse( instance );
147
+
148
+ // own fields in declaration order FIRST — a $ref target is exported
149
+ // at first encounter, before the parent link (the encounter order
150
+ // the cross-language comparison maps ids by)
151
+ const own: { [ key: string ]: LineageValue } = {};
152
+ const ownProps = parsed.props as Record<string, unknown>;
153
+ Object.keys( ownProps ).forEach( ( key ) => {
154
+ own[ key ] = exportValue( ownProps[ key ], new Set() );
155
+ } );
156
+
157
+ const baseProps = getPropsRef( instance ) as Props;
158
+
159
+ let nodeArgs: LineageValue | undefined;
160
+ if ( options && options.args ) {
161
+ nodeArgs = exportValue( baseProps.__args__, new Set() );
162
+ }
163
+
164
+ let nodeProps: { [ key: string ]: LineageValue } | undefined;
165
+ if ( options && Array.isArray( options.props ) ) {
166
+ nodeProps = {};
167
+ options.props.forEach( ( key ) => {
168
+ nodeProps![ key ] = exportValue( baseProps[ key ], new Set() );
169
+ } );
170
+ }
171
+
172
+ // then the parent — the type path is the parent's path plus this
173
+ // level's name (names alone collide across levels and collections)
174
+ let parentId: string | null = null;
175
+ let path = parsed.name;
176
+ if ( parsed.parent !== null ) {
177
+ parentId = visit( parsed.parent );
178
+ path = `${nodePaths[ parentId ]}.${parsed.name}`;
179
+ }
180
+
181
+ const { __collection__: collection } = baseProps;
182
+ // every collection carries a name by construction (the default
183
+ // collection is 'defaultTypes', unnamed customs are auto-named), so
184
+ // no fallback is needed here
185
+ const collectionConfig = ( collection as unknown as { [ SymbolConfig ]?: { name?: string } } )[
186
+ SymbolConfig
187
+ ]!;
188
+ const collectionName: string = collectionConfig.name!;
189
+
190
+ const node: LineageNode = {
191
+ type : {
192
+ collection : collectionName,
193
+ path
194
+ },
195
+ own,
196
+ parent : parentId
197
+ };
198
+ if ( nodeArgs !== undefined ) {
199
+ node.args = nodeArgs;
200
+ }
201
+ if ( nodeProps !== undefined ) {
202
+ node.props = nodeProps;
203
+ }
204
+
205
+ nodes[ id ] = node;
206
+ nodePaths[ id ] = path;
207
+ visiting.delete( id );
208
+ return id;
209
+ };
210
+
211
+ const heads = instances.map( ( instance ) => visit( instance ) );
212
+
213
+ const result: LineageGraph = {
214
+ version : '1',
215
+ heads,
216
+ nodes
217
+ };
218
+ return result;
219
+
220
+ };
@@ -15,6 +15,7 @@ const {
15
15
 
16
16
  import { extract } from './extract';
17
17
  import type {
18
+ InstanceConstructor,
18
19
  EmptyParsed, Parsed
19
20
  } from '../types';
20
21
 
@@ -41,7 +42,7 @@ export function parse<T extends object> ( self: T | null ): Parsed<T> | EmptyPar
41
42
  return nullResult;
42
43
  }
43
44
 
44
- if ( !self || !( self as { constructor?: CallableFunction } ).constructor ) {
45
+ if ( !self || !( self as { constructor?: InstanceConstructor } ).constructor ) {
45
46
  throw new WRONG_MODIFICATION_PATTERN;
46
47
  }
47
48