@infrascan/shared-types 0.3.0 → 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.
- package/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/src/api.ts +3 -14
- package/src/graph.ts +66 -82
- package/src/index.ts +1 -0
- package/src/plugins.ts +42 -0
- package/tsconfig.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @infrascan/shared-types
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#82](https://github.com/infrascan/infrascan/pull/82) [`e28a6d9`](https://github.com/infrascan/infrascan/commit/e28a6d91eb36fa83e9a40a667eb39a15b2a45ccb) Thanks [@lfarrel6](https://github.com/lfarrel6)! - Introduce new readable/writable interface for @infrascan/core graph interface to make types nicer to work with. Previously, the graph returned union types to bridge the Graph's internal model of the state with its exposed API for adding children/parents/edges. This resulted in a lot of properties with signatures like `parent?: Node | string`.
|
|
8
|
+
|
|
9
|
+
This has been updated to wrap the Node and Edge types in `Readable` and `Writable` types based on the context they're being used in. This is inspired by the Kysely `Selectable`/`Insertable` pattern. When a Node is `Readable` its edges, parent and children will resolve to other Node objects. When `Writable`, all other Nodes are expected to be referenced using a string type containing their ID.
|
|
10
|
+
|
|
11
|
+
Introduce @infrascan/node-reducer-plugin which reduces graph complexity for dynamicially provisioned infrastructure.
|
|
12
|
+
|
|
13
|
+
## 0.4.0
|
|
14
|
+
|
|
15
|
+
### Minor Changes
|
|
16
|
+
|
|
17
|
+
- [#77](https://github.com/infrascan/infrascan/pull/77) [`21d4ecf`](https://github.com/infrascan/infrascan/commit/21d4ecf4b7fec31f4ac7b2cc5857aa5d2b725075) Thanks [@lfarrel6](https://github.com/lfarrel6)! - Add in-memory graph model to decouple the infrascan packages from a single graphing library, and to make graph manipulation easier
|
|
18
|
+
|
|
3
19
|
## 0.3.0
|
|
4
20
|
|
|
5
21
|
### Minor Changes
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AwsCredentialIdentityProvider, RetryStrategy, RetryStrategyV2 } from "@aws-sdk/types";
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { SelectedEdge, SelectedNode } from "./graph";
|
|
4
4
|
import type { BaseEdgeResolver } from "./config";
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -85,9 +85,9 @@ type GetterFn<T, P extends Provider> = (
|
|
|
85
85
|
type GetNodeFn<T extends Provider> = (
|
|
86
86
|
stateConnector: Connector,
|
|
87
87
|
context: ProviderContextMap[T],
|
|
88
|
-
) => Promise<
|
|
88
|
+
) => Promise<SelectedNode[]>;
|
|
89
89
|
|
|
90
|
-
type GetEdgeFn = (stateConnector: Connector) => Promise<
|
|
90
|
+
type GetEdgeFn = (stateConnector: Connector) => Promise<SelectedEdge[]>;
|
|
91
91
|
|
|
92
92
|
export type Provider = "aws";
|
|
93
93
|
|
|
@@ -106,15 +106,6 @@ export type ProviderContext<T extends Provider> = {
|
|
|
106
106
|
context: ProviderContextMap[T];
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
-
type FormatNodeFn<P extends Provider> = (
|
|
110
|
-
node: GraphNode,
|
|
111
|
-
context: ProviderContextMap[P],
|
|
112
|
-
) => GraphNode;
|
|
113
|
-
type FormatEdgeFn<P extends Provider> = (
|
|
114
|
-
node: GraphEdge,
|
|
115
|
-
context: ProviderContextMap[P],
|
|
116
|
-
) => GraphEdge;
|
|
117
|
-
|
|
118
109
|
export type EntityRoleData = {
|
|
119
110
|
roleArn: string;
|
|
120
111
|
executor: string;
|
|
@@ -131,9 +122,7 @@ export interface ServiceModule<T, P extends Provider> {
|
|
|
131
122
|
getters: GetterFn<T, P>[];
|
|
132
123
|
nodes?: string[];
|
|
133
124
|
getNodes?: GetNodeFn<P>;
|
|
134
|
-
formatNode?: FormatNodeFn<P>;
|
|
135
125
|
edges?: BaseEdgeResolver[];
|
|
136
126
|
getEdges?: GetEdgeFn;
|
|
137
|
-
formatEdge?: FormatEdgeFn<P>;
|
|
138
127
|
getIamRoles?: GetIamRoleFn;
|
|
139
128
|
}
|
package/src/graph.ts
CHANGED
|
@@ -1,105 +1,89 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A node returned from a state selector before its been formatted for a graphing library
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
4
|
+
export interface SelectedNode {
|
|
5
5
|
id: string;
|
|
6
6
|
name?: string;
|
|
7
7
|
parent?: string;
|
|
8
8
|
type?: string;
|
|
9
9
|
rawState?: any;
|
|
10
|
-
}
|
|
10
|
+
}
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
group: "nodes";
|
|
17
|
-
id: string;
|
|
18
|
-
data: {
|
|
19
|
-
id: string;
|
|
20
|
-
type: string;
|
|
21
|
-
/**
|
|
22
|
-
* Parent node (account, region etc)
|
|
23
|
-
*/
|
|
24
|
-
parent?: string;
|
|
25
|
-
name?: string;
|
|
26
|
-
service?: string;
|
|
27
|
-
};
|
|
28
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
29
|
-
metadata?: any;
|
|
30
|
-
};
|
|
12
|
+
export interface SelectedEdgeTarget {
|
|
13
|
+
name: string;
|
|
14
|
+
target: string;
|
|
15
|
+
}
|
|
31
16
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
id: string;
|
|
43
|
-
name: string;
|
|
44
|
-
/**
|
|
45
|
-
* Source Node
|
|
46
|
-
*/
|
|
47
|
-
source: string;
|
|
48
|
-
/**
|
|
49
|
-
* Target Node
|
|
50
|
-
*/
|
|
51
|
-
target: string;
|
|
52
|
-
type: string;
|
|
53
|
-
};
|
|
54
|
-
metadata?: {
|
|
55
|
-
label: string;
|
|
56
|
-
roleArn?: string;
|
|
57
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
58
|
-
statement?: any;
|
|
59
|
-
};
|
|
17
|
+
export interface SelectedEdge {
|
|
18
|
+
source: string;
|
|
19
|
+
target: string;
|
|
20
|
+
metadata?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Create Read/Write types inspired by kysely
|
|
24
|
+
export type ElementType<ReadType, WriteType> = {
|
|
25
|
+
readonly _read: ReadType;
|
|
26
|
+
readonly _write: WriteType;
|
|
60
27
|
};
|
|
61
28
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
29
|
+
export type ElementRecord<K extends keyof any, R, W> = ElementType<
|
|
30
|
+
Record<K, Readable<R>>,
|
|
31
|
+
Record<K, Writable<W>>
|
|
32
|
+
>;
|
|
66
33
|
|
|
67
|
-
export type
|
|
68
|
-
|
|
69
|
-
|
|
34
|
+
export type ReadType<T> = T extends ElementType<infer R, any> ? Readable<R> : T;
|
|
35
|
+
export type Readable<T> = {
|
|
36
|
+
[K in keyof T]: ReadType<T[K]>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type WriteType<T> = T extends ElementType<any, infer W>
|
|
40
|
+
? Writable<W>
|
|
41
|
+
: T;
|
|
42
|
+
export type Writable<T> = {
|
|
43
|
+
[K in keyof T]: WriteType<T[K]>;
|
|
70
44
|
};
|
|
71
45
|
|
|
72
46
|
export interface Node {
|
|
73
|
-
id: string
|
|
74
|
-
name: string
|
|
47
|
+
id: string;
|
|
48
|
+
name: string;
|
|
75
49
|
metadata: Record<string, unknown>;
|
|
76
|
-
service?: string
|
|
77
|
-
type?: string
|
|
78
|
-
parent?:
|
|
79
|
-
children?:
|
|
80
|
-
incomingEdges:
|
|
81
|
-
outgoingEdges:
|
|
50
|
+
service?: string;
|
|
51
|
+
type?: string;
|
|
52
|
+
parent?: ElementType<Node, string>;
|
|
53
|
+
children?: ElementRecord<string, Node, string>;
|
|
54
|
+
incomingEdges: ElementRecord<string, Edge, string>;
|
|
55
|
+
outgoingEdges: ElementRecord<string, Edge, string>;
|
|
82
56
|
}
|
|
83
57
|
|
|
84
|
-
export interface Edge
|
|
85
|
-
id: string
|
|
86
|
-
name?: string
|
|
87
|
-
metadata: Record<string, unknown
|
|
88
|
-
source:
|
|
89
|
-
target:
|
|
58
|
+
export interface Edge {
|
|
59
|
+
id: string;
|
|
60
|
+
name?: string;
|
|
61
|
+
metadata: Record<string, unknown>;
|
|
62
|
+
source: ElementType<Node, string>;
|
|
63
|
+
target: ElementType<Node, string>;
|
|
90
64
|
}
|
|
91
65
|
|
|
92
66
|
export interface Graph {
|
|
93
|
-
nodes: Node[]
|
|
94
|
-
edges: Edge
|
|
95
|
-
addNode: (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
67
|
+
nodes: Readable<Node>[];
|
|
68
|
+
edges: Readable<Edge>[];
|
|
69
|
+
addNode: (
|
|
70
|
+
node: Pick<
|
|
71
|
+
Writable<Node>,
|
|
72
|
+
"id" | "name" | "metadata" | "parent" | "service" | "type"
|
|
73
|
+
>,
|
|
74
|
+
) => void;
|
|
75
|
+
addEdge: (
|
|
76
|
+
edge: Pick<Writable<Edge>, "name" | "source" | "target" | "metadata">,
|
|
77
|
+
) => void;
|
|
78
|
+
addChild: (parent: string, child: string) => void;
|
|
79
|
+
getNode: (id: string) => Readable<Node> | undefined;
|
|
80
|
+
mapNodesById: (
|
|
81
|
+
id: string,
|
|
82
|
+
mapperFn: (node: Readable<Node>) => Writable<Node>[],
|
|
83
|
+
) => void;
|
|
84
|
+
removeEdge: (id: string) => Readable<Edge>;
|
|
85
|
+
removeNode: (id: string) => Readable<Node>;
|
|
86
|
+
serialize: () => string;
|
|
87
|
+
}
|
|
104
88
|
|
|
105
|
-
export type GraphSerializer<T> = (graph: Graph) => T;
|
|
89
|
+
export type GraphSerializer<T> = (graph: Graph) => T;
|
package/src/index.ts
CHANGED
package/src/plugins.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Graph } from "./graph";
|
|
2
|
+
import { AwsContext } from "./api";
|
|
3
|
+
|
|
4
|
+
// All supported events to trigger a plugin
|
|
5
|
+
export type GraphPluginEvents = "onServiceComplete" | "onRegionComplete" | "onAccountComplete" | "onGraphComplete";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A plugin which is called everytime a service's nodes have been added to the graph.
|
|
9
|
+
* Receives the current graph state, the service ID, and the current AWS Context.
|
|
10
|
+
*/
|
|
11
|
+
export type OnServiceCompletePlugin = (graph: Graph, service: string, context: AwsContext) => void | Promise<void>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A plugin which is called everytime a region's nodes have been added to the graph.
|
|
15
|
+
* Receives the current graph state, and the current AWS Context.
|
|
16
|
+
*/
|
|
17
|
+
export type OnRegionCompletePlugin = (graph: Graph, context: AwsContext) => void | Promise<void>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A plugin which is called everytime an account's nodes have been added to the graph.
|
|
21
|
+
* Receives the current graph state, and the current AWS Context.
|
|
22
|
+
*/
|
|
23
|
+
export type OnAccountCompletePlugin = (graph: Graph, context: AwsContext) => void | Promise<void>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A plugin which is called when the entire graph has been generated.
|
|
27
|
+
* Receives the entire graph state.
|
|
28
|
+
*/
|
|
29
|
+
export type OnGraphCompletePlugin = (graph: Graph) => void | Promise<void>;
|
|
30
|
+
|
|
31
|
+
export type PluginEventHandlerTypes = {
|
|
32
|
+
"onServiceComplete": OnServiceCompletePlugin,
|
|
33
|
+
"onRegionComplete": OnRegionCompletePlugin,
|
|
34
|
+
"onAccountComplete": OnAccountCompletePlugin,
|
|
35
|
+
"onGraphComplete": OnGraphCompletePlugin,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export interface GraphPlugin<E extends GraphPluginEvents> {
|
|
39
|
+
id: string,
|
|
40
|
+
event: E,
|
|
41
|
+
handler: PluginEventHandlerTypes[E]
|
|
42
|
+
}
|
package/tsconfig.json
CHANGED