-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
205 lines (173 loc) · 5.48 KB
/
index.js
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
197
198
199
200
201
202
203
204
205
const yaml = require('js-yaml');
const _ = require('lodash');
const {
serialize,
dotsToSlashes,
eventToChannel,
streamToChannel,
convertMessage,
} = require('./helpers');
const lib = module.exports;
/**
* Value for key (version) represents the function which converts specification from previous version to the given as key.
*/
const conversions = {
'1.0.0': undefined,
'1.1.0': from__1_0_0__to__1_1_0,
'1.2.0': from__1_1_0__to__1_2_0,
'2.0.0-rc1': from__1_2_0__to__2_0_0_rc1,
'2.0.0-rc2': from__2_0_0_rc1__to__2_0_0_rc2,
'2.0.0': from__2_0_0_rc2__to__2_0_0,
'2.1.0': from__2_0_0__to__2_1_0,
'2.2.0': from__2_1_0__to__2_2_0,
}
const conversionVersions = Object.keys(conversions);
lib.convert = (asyncapi, version, options = {}) => {
const { isYAML, parsed } = serialize(asyncapi);
if (parsed === undefined) return '';
let fromVersion = conversionVersions.indexOf(parsed.asyncapi);
const toVersion = conversionVersions.indexOf(version);
if (fromVersion === -1 || toVersion === -1) {
console.error(`Cannot convert from ${parsed.asyncapi} to ${version}.`);
return;
}
if (fromVersion > toVersion) {
console.error(`Cannot downgrade from ${parsed.asyncapi} to ${version}.`);
return;
}
if (fromVersion === toVersion) {
console.error(`Cannot convert to the same version.`);
return;
}
// add 1 to `fromVersion` because we convert from previous to next
fromVersion++;
let converted = parsed;
for (let i = fromVersion; i <= toVersion; i++) {
const fn = conversions[conversionVersions[i]];
converted = fn(converted, options);
}
if (isYAML) {
converted = yaml.safeDump(converted, { skipInvalid: true });
}
return converted;
};
function from__1_0_0__to__1_1_0(asyncapi) {
return asyncapi;
}
function from__1_1_0__to__1_2_0(asyncapi) {
return asyncapi;
}
function from__1_2_0__to__2_0_0_rc1(asyncapi1, options) { // NOSONAR
const result = asyncapi1;
result.asyncapi = '2.0.0-rc1';
result.id = options.id || `urn:${asyncapi1.info.title.toLowerCase().split(' ').join('.')}`;
if (asyncapi1.servers) {
const security = asyncapi1.security;
result.servers = asyncapi1.servers.map(server => {
const { scheme, schemeVersion, ...rest } = server;
const out = {
...rest,
...{
protocol: scheme,
}
};
if (schemeVersion) out.protocolVersion = schemeVersion;
if (security) {
out.security = security;
}
return out;
});
}
if (asyncapi1.topics) {
const baseTopic = asyncapi1.baseTopic ? `${asyncapi1.baseTopic}.` : "";
result.channels = _.mapKeys(result.topics, (__, topicName) => dotsToSlashes(`${baseTopic}${topicName}`));
_.map(result.channels, ch => {
if (ch.publish) {
ch.publish = { message: ch.publish };
}
if (ch.subscribe) {
ch.subscribe = { message: ch.subscribe };
}
});
} else if (asyncapi1.stream) {
result.channels = {
'/': streamToChannel(asyncapi1.stream),
};
} else if (asyncapi1.events) {
result.channels = {
'/': eventToChannel(asyncapi1.events),
};
}
delete result.topics;
delete result.stream;
delete result.events;
delete result.baseTopic;
delete result.security;
return result;
}
function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi2rc1, options) { // NOSONAR
const result = asyncapi2rc1;
result.asyncapi = '2.0.0-rc2';
result.id = result.id || options.id;
if (asyncapi2rc1.servers) {
const serverMap = {};
asyncapi2rc1.servers.forEach((server, index) => {
if (server.baseChannel) delete server.baseChannel;
const name = index === 0 ? 'default' : `server${index}`;
serverMap[name] = server;
});
result.servers = serverMap;
}
if (result.channels) {
_.each(result.channels, (channel, channelName) => {
if (channel.parameters) {
const parametersMap = {};
const paramNames = channelName.match(/\{([^\}]{1,100})\}/g).map(p => p.substr(1, p.length - 2));
channel.parameters.forEach((parameter, index) => {
const name = parameter.name || paramNames[index];
if (parameter.name) delete parameter.name;
parametersMap[name] = parameter;
});
channel.parameters = parametersMap;
}
if (channel.publish && channel.publish.message) {
const message = channel.publish.message;
convertMessage(message);
}
if (channel.subscribe && channel.subscribe.message) {
const message = channel.subscribe.message;
convertMessage(message);
}
if (channel.protocolInfo) {
channel.bindings = channel.protocolInfo;
delete channel.protocolInfo;
}
if (channel.publish && channel.publish.protocolInfo) {
channel.publish.bindings = channel.publish.protocolInfo;
delete channel.publish.protocolInfo;
}
if (channel.subscribe && channel.subscribe.protocolInfo) {
channel.subscribe.bindings = channel.subscribe.protocolInfo;
delete channel.subscribe.protocolInfo;
}
});
}
if (!options.id) delete result.id;
return result;
}
function from__2_0_0_rc2__to__2_0_0(asyncapi2rc2, options) {
const result = asyncapi2rc2;
if (!options.id) delete result.id;
result.asyncapi = '2.0.0';
return result;
}
function from__2_0_0__to__2_1_0(asyncapi2) {
const result = asyncapi2;
result.asyncapi = '2.1.0';
return result;
}
function from__2_1_0__to__2_2_0(asyncapi2) {
const result = asyncapi2;
result.asyncapi = '2.2.0';
return result;
}