-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathOptimizer.ts
More file actions
196 lines (184 loc) · 6.96 KB
/
Optimizer.ts
File metadata and controls
196 lines (184 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {
NewReport,
Report,
ReportElement,
Options,
OptimizableComponentGroup,
Reporter,
} from './types'
import { Parser } from '@asyncapi/parser'
import {
removeComponents,
reuseComponents,
moveAllToComponents,
moveDuplicatesToComponents,
} from './Reporters'
import YAML from 'js-yaml'
import merge from 'merge-deep'
import * as _ from 'lodash'
import { getOptimizableComponents } from './ComponentProvider'
import { filterReportElements, hasParent, sortReportElements, toJS, lodashPathToJsonPointer } from './Utils'
import Debug from 'debug'
export enum Action {
Move = 'move',
Remove = 'remove',
Reuse = 'reuse',
}
export enum Output {
JSON = 'JSON',
YAML = 'YAML',
}
/**
* this class is the starting point of the library.
* user will only interact with this class. here we generate different kind of reports using optimizers, apply changes and return the results to the user.
*
* @public
*/
export class Optimizer {
private components!: OptimizableComponentGroup[]
private reporters: Reporter[]
private reports: NewReport[] | undefined
private outputObject = {}
/**
* @param {any} YAMLorJSON - YAML or JSON document that you want to optimize. You can pass Object, YAML or JSON version of your AsyncAPI document here.
*/
constructor(private YAMLorJSON: any) {
this.outputObject = toJS(this.YAMLorJSON)
this.reporters = [
removeComponents,
reuseComponents,
moveAllToComponents,
moveDuplicatesToComponents,
]
}
/**
* @returns {Report} an object containing all of the optimizations that the library can do.
*
*/
async getReport(): Promise<Report> {
const parser = new Parser()
const parsedDocument = await parser.parse(this.YAMLorJSON, { applyTraits: false })
if (!parsedDocument.document) {
// eslint-disable-next-line no-undef, no-console
console.error(parsedDocument.diagnostics)
throw new Error('Parsing failed.')
}
this.components = getOptimizableComponents(parsedDocument.document)
const rawReports = this.reporters.map((reporter) => reporter(this.components))
const reportsWithParents = rawReports.map((report) => ({
type: report.type,
elements: report.elements.filter((reportElement) =>
hasParent(reportElement, this.outputObject)
),
}))
const filteredReports = filterReportElements(reportsWithParents)
const sortedReports = filteredReports.map((report) => sortReportElements(report))
this.reports = sortedReports
// since changing the report format is considered a breaking change, we are going to return the report in the old format.
return convertToReportFormat(this.reports)
}
/**
* @typedef {Object} Rules
* @property {Boolean=} reuseComponents - whether to reuse components from `components` section or not. Defaults to `true`.
* @property {Boolean=} removeComponents - whether to remove un-used components from `components` section or not. Defaults to `true`.
* @property {Boolean=} moveAllToComponents - whether to move all AsyncAPI Specification-valid components to the `components` section or not. Defaults to `true`.
* @property {Boolean=} moveDuplicatesToComponents - whether to move duplicated components to the `components` section or not. Defaults to `false`.
*/
/**
* @typedef {Object} DisableOptimizationFor
* @property {Boolean=} schema - whether object `schema` should be excluded from the process of optimization (`true` instructs **not** to add calculated `schemas` to the optimized AsyncAPI Document.)
*/
/**
* @typedef {Object} Options
* @property {Rules=} rules - the list of rules that specifies which type of optimizations should be applied.
* @property {String=} output - specifies which type of output user wants, `'JSON'` or `'YAML'`. Defaults to `'YAML'`;
* @property {DisableOptimizationFor=} disableOptimizationFor - the list of objects that should be excluded from the process of optimization.
*/
/**
* This function is used to get the optimized document after seeing the report.
*
* @param {Options=} Options - the options are a way to customize the final output.
* @returns {string } returns an stringified version of the YAML output.
*
*/
getOptimizedDocument(options?: Options): string {
const defaultOptions = {
rules: {
reuseComponents: true,
removeComponents: true,
moveAllToComponents: true,
moveDuplicatesToComponents: false, // there is no need to move duplicates if `moveAllToComponents` is `true`
},
output: Output.YAML,
disableOptimizationFor: {
schema: false,
},
}
options = merge(defaultOptions, options)
if (!this.reports) {
throw new Error(
'No report has been generated. please first generate a report by calling getReport method.'
)
}
for (const report of this.reports) {
if (options.disableOptimizationFor?.schema === true) {
report.elements = report.elements.filter(
(element) => !element.target?.includes('.schemas.')
)
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (options.rules[report.type] === true) {
this.applyReport(report.elements)
}
}
if (options.output === Output.JSON) {
return JSON.stringify(this.outputObject)
}
return YAML.dump(this.outputObject)
}
private removeEmptyParent(childPath: string): void {
const parentPath = childPath.substring(0, childPath.lastIndexOf('.'))
const parent = _.get(this.outputObject, parentPath)
if (_.isEmpty(parent)) {
_.unset(this.outputObject, parentPath)
}
}
private applyReport(changes: ReportElement[]): void {
const debug = Debug('optimizer:applyReport')
for (const change of changes) {
switch (change.action) {
case Action.Move:
_.set(this.outputObject, change.target as string, _.get(this.outputObject, change.path))
_.set(this.outputObject, change.path, {
$ref: `#/${lodashPathToJsonPointer(change.target as string)}`,
})
debug('moved %s to %s', change.path, change.target)
break
case Action.Reuse:
if (_.get(this.outputObject, change.target as string)) {
_.set(this.outputObject, change.path, {
$ref: `#/${lodashPathToJsonPointer(change.target as string)}`,
})
}
debug('%s reused %s', change.path, change.target)
break
case Action.Remove:
_.unset(this.outputObject, change.path)
//if parent becomes empty after removing, parent should be removed as well.
this.removeEmptyParent(change.path)
debug('removed %s', change.path)
break
}
}
}
}
function convertToReportFormat(reports: NewReport[]): Report {
const result = {}
for (const report of reports) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
result[report.type] = report.elements
}
return result as Report
}