client-creator.js
7.53 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var fs = require('fs');
var path = require('path');
// Generate service clients
function ClientCreator() {
this._metadata = require('../apis/metadata');
this._apisFolderPath = path.join(__dirname, '..', 'apis');
this._clientFolderPath = path.join(__dirname, '..', 'clients');
this._serviceCustomizationsFolderPath = path.join(__dirname, '..', 'lib', 'services');
this._packageJsonPath = path.join(__dirname, '..', 'package.json');
this._apiFileNames = null;
}
ClientCreator.prototype.getAllApiFilenames = function getAllApiFilenames() {
if (this._apiFileNames) {
return this._apiFileNames;
}
var apiFileNames = fs.readdirSync(this._apisFolderPath);
// filter out metadata
this._apiFileNames = apiFileNames.filter(function(name) {
return name !== 'metadata.json';
});
return this._apiFileNames;
};
ClientCreator.prototype.getAllApiFilenamesForService = function getAllApiFilenamesForService(modelName) {
var serviceRegex = new RegExp('(^' + modelName + '-([\\d]{4}-[\\d]{2}-[\\d]{2})\\.([\\w]+))\\.json$');
var modelRegex = /(([\d]{4}-[\d]{2}-[\d]{2})\.([\w]+))\.json$/;
var models = {};
var versions = {};
this.getAllApiFilenames().filter(function(name) {
return name.search(serviceRegex) === 0;
}).forEach(function(name) {
var matches = name.match(serviceRegex);
if (!matches) {
return;
}
var model = matches[1];
var version = matches[2];
var modelType = matches[3];
if (!versions.hasOwnProperty(version)) {
versions[version] = {};
}
var versionInfo = versions[version];
switch (modelType) {
case 'min':
versionInfo.api = model;
break;
case 'paginators':
versionInfo.paginators = model;
break;
case 'waiters2':
versionInfo.waiters = model;
break;
default:
return;
}
});
models.versions = versions;
return models;
};
ClientCreator.prototype.customizationsExist = function customizationsExist(serviceName) {
var customizationsFolder = this._serviceCustomizationsFolderPath;
return fs.existsSync(path.join(customizationsFolder, serviceName + '.js'));
};
ClientCreator.prototype.generateClientFileSource = function generateClientFileSource(serviceMetadata, specifiedVersion) {
var clientFolderPath = this._clientFolderPath;
var className = serviceMetadata.name;
var serviceName = className.toLowerCase();
var modelName = serviceMetadata.prefix || serviceName;
specifiedVersion = specifiedVersion || '*';
// get models for the service
var models = this.getAllApiFilenamesForService(modelName);
var modelVersions = models && models.versions;
if (!modelVersions) {
throw new Error('Unable to get models for ' + modelName);
}
var obsoleteVersions = serviceMetadata.versions || [];
var versionNumbers = Object.keys(modelVersions);
var tab = ' ';
var code = '';
code += 'require(\'../lib/node_loader\');\n';
code += 'var AWS = require(\'../lib/core\');\n';
code += 'var Service = AWS.Service;\n';
code += 'var apiLoader = AWS.apiLoader;\n\n';
code += 'apiLoader.services[\'' + serviceName +'\'] = {};\n';
code += 'AWS.' + className + ' = Service.defineService(\'' + serviceName + '\', [\'' + [].concat(obsoleteVersions, versionNumbers).sort().join('\', \'') + '\']);\n';
// pull in service customizations
if (this.customizationsExist(serviceName)) {
code += 'require(\'../lib/services/' + serviceName + '\');\n';
}
versionNumbers.forEach(function(version) {
// check version
if (specifiedVersion !== '*' && specifiedVersion !== version) {
return;
}
var versionInfo = modelVersions[version];
if (!versionInfo.hasOwnProperty('api')) {
throw new Error('No API model for ' + serviceName + '-' + version);
}
code += 'Object.defineProperty(apiLoader.services[\'' + serviceName +'\'], \'' + version + '\', {\n';
code += tab + 'get: function get() {\n';
code += tab + tab + 'var model = require(\'../apis/' + versionInfo.api + '.json\');\n';
if (versionInfo.hasOwnProperty('paginators')) {
code += tab + tab + 'model.paginators = require(\'../apis/' + versionInfo.paginators + '.json\').pagination;\n';
}
if (versionInfo.hasOwnProperty('waiters')) {
code += tab + tab + 'model.waiters = require(\'../apis/' + versionInfo.waiters + '.json\').waiters;\n';
}
code += tab + tab + 'return model;\n';
code += tab + '},\n';
code += tab + 'enumerable: true,\n';
code += tab + 'configurable: true\n';
code += '});\n';
});
code += '\n';
code += 'module.exports = AWS.' + className + ';\n';
return {
code: code,
path: path.join(clientFolderPath, serviceName + '.js'),
service: serviceName,
};
};
ClientCreator.prototype.tabs = function tabs(count) {
var tab = '';
for (var i = 0; i < count; i++) {
tab += ' ';
}
return tab;
};
ClientCreator.prototype.generateDefinePropertySource = function generateDefinePropertySource(objName, serviceName, className) {
var tabs = this.tabs;
var code = '';
code += 'var ' + serviceName + ' = null;\n';
code += 'Object.defineProperty(' + objName + ', \'' + className + '\', {\n';
code += tabs(1) + 'get: function get() {\n';
code += tabs(2) + 'return ' + serviceName + ' || require(\'./' + serviceName + '\');\n';
code += tabs(1) + '},\n';
code += tabs(1) + 'set: function set(svc) {\n';
code += tabs(2) + serviceName + ' = svc;\n';
code += tabs(1) + '},\n';
code += tabs(1) + 'enumerable: true,\n';
code += tabs(1) + 'configurable: true\n';
code += '});\n';
return code;
};
ClientCreator.prototype.generateAllServicesSource = function generateAllServicesSource(services, fileName) {
var metadata = this._metadata;
var self = this;
var code = '';
code += 'require(\'../lib/node_loader\');\n';
code += 'module.exports = {\n';
services.forEach(function(service, idx) {
var className = metadata[service].name;
var tab = ' ';
var isLast = idx === services.length - 1;
code += self.tabs(1) + className + ': require(\'./' + service + '\')' + (isLast ? '' : ',') + '\n';
});
code += '};';
return {
code: code,
path: path.join(this._clientFolderPath, fileName + '.js'),
service: fileName
};
};
ClientCreator.prototype.getDefaultServices = function getDefaultServices() {
var metadata = this._metadata;
var services = [];
for (var key in metadata) {
if (!metadata.hasOwnProperty(key)) {
continue;
}
var className = metadata[key].name;
var serviceName = className.toLowerCase();
services.push(serviceName);
}
return services;
};
ClientCreator.prototype.writeClientServices = function writeClientServices() {
var metadata = this._metadata;
var services = [];
var corsServices = [];
for (var key in metadata) {
if (!metadata.hasOwnProperty(key)) {
continue;
}
var clientInfo = this.generateClientFileSource(metadata[key]);
fs.writeFileSync(clientInfo.path, clientInfo.code);
services.push(clientInfo.service);
// check if service supports CORS
if (metadata[key].cors === true) {
corsServices.push(clientInfo.service);
}
}
var allClientInfo = this.generateAllServicesSource(services, 'all');
fs.writeFileSync(allClientInfo.path, allClientInfo.code);
var browserClientInfo = this.generateAllServicesSource(corsServices, 'browser_default');
fs.writeFileSync(browserClientInfo.path, browserClientInfo.code);
};
module.exports = ClientCreator;