This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
thrift.js
563 lines (495 loc) · 18.6 KB
/
thrift.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/* eslint max-statements:[1, 45] */
'use strict';
var assert = require('assert');
var util = require('util');
var fs = require('fs');
var path = require('path');
var idl = require('./thrift-idl');
var Result = require('bufrw/result');
var lcp = require('./lib/lcp');
var asyncEach = require('./lib/async-each.js');
var ThriftService = require('./service').ThriftService;
var ThriftStruct = require('./struct').ThriftStruct;
var ThriftUnion = require('./union').ThriftUnion;
var ThriftEnum = require('./enum').ThriftEnum;
var ThriftVoid = require('./void').ThriftVoid;
var ThriftBoolean = require('./boolean').ThriftBoolean;
var ThriftString = require('./string').ThriftString;
var ThriftBinary = require('./binary').ThriftBinary;
var ThriftI8 = require('./i8').ThriftI8;
var ThriftI16 = require('./i16').ThriftI16;
var ThriftI32 = require('./i32').ThriftI32;
var ThriftI64 = require('./i64').ThriftI64;
var ThriftDouble = require('./double').ThriftDouble;
var ThriftList = require('./list').ThriftList;
var ThriftSet = require('./set').ThriftSet;
var ThriftMap = require('./map').ThriftMap;
var ThriftConst = require('./const').ThriftConst;
var ThriftTypedef = require('./typedef').ThriftTypedef;
var Literal = require('./ast').Literal;
var Message = require('./message').Message;
var messageExceptionDef = require('./message').exceptionDef;
var messageExceptionTypesDef = require('./message').exceptionTypesDef;
var validThriftIdentifierRE = /^[a-zA-Z_][a-zA-Z0-9_\.]+$/;
function Thrift(options) {
assert(options, 'options required');
assert(typeof options === 'object', 'options must be object');
assert(options.source || options.entryPoint, 'opts.entryPoint required');
// Coerce weakly-deprecated single include usage
if (options.source) {
assert(typeof options.source === 'string', 'source must be string');
options.entryPoint = 'service.thrift';
options.idls = {'service.thrift': options.source};
}
// filename to parse status
this.parsed = options.parsed || Object.create(null);
// filename to source
this.idls = options.idls || Object.create(null);
// filename to ast
this.asts = options.asts || Object.create(null);
// filename to Thrift instance
this.memo = options.memo || Object.create(null);
// Grant file system access for resolving includes, as opposed to lifting
// includes from provided options.idls alone.
this.fs = options.fs;
if (options.allowFilesystemAccess) {
this.fs = fs;
}
this.strict = options.strict !== undefined ? options.strict : true;
this.defaultValueDefinition = new Literal(options.defaultAsUndefined ? undefined : null);
this.defaultAsUndefined = options.defaultAsUndefined;
// [name] :Thrift* implementing {compile, link, &c}
// Heterogenous Thrift model objects by name in a consolidated name-space
// to prevent duplicate references with the same and different types, like
// a service and a struct with the same name in the scope of a Thrift IDL
// module:
this.models = Object.create(null);
// [serviceName][functionName] :{rw, Arguments, Result}
this.services = Object.create(null);
// [constName] :Value
this.consts = Object.create(null);
// [enumName][name] :String
this.enums = Object.create(null);
// [structName] :Constructor
this.structs = Object.create(null);
// [exceptionName] :Constructor
this.exceptions = Object.create(null);
// [unionName] :Constructor
this.unions = Object.create(null);
// [typedefName] :Constructor (might be Array, Object, or Number)
this.typedefs = Object.create(null);
// [moduleName] :Thrift
// Child modules indexed by their local alias.
this.modules = Object.create(null);
this.surface = this;
this.linked = false;
this.noLink = options.noLink;
this.allowIncludeAlias = options.allowIncludeAlias || false;
this.allowOptionalArguments = options.allowOptionalArguments || false;
this.filename = options.entryPoint;
this.dirname = path.dirname(this.filename);
this.memo[this.filename] = this;
this.exception = null;
var cb = options.callback;
if (cb) {
var thrift = this;
thrift.load(thrift.filename, function (err) {
if (err) {
return cb(err, undefined);
}
try {
thrift.compileAndLink();
} catch (err) {
return cb(err, undefined);
}
cb(undefined, thrift);
});
} else {
this.loadSync(this.filename);
this.compileAndLink();
}
}
// Alternative constructor allowing for asynchronous source loading.
Thrift.load = function load(options, cb) {
assert(options != null, 'options required');
assert(typeof options === 'object', 'options must be object');
assert(options.fs != null && typeof options.fs.readFile === 'function',
'options.fs.readFile is required for async loading');
options.callback = cb;
new Thrift(options);
}
Thrift.prototype.models = 'module';
Thrift.prototype.Message = Message;
Thrift.prototype.load = function load(filename, cb) {
var model = this;
if (model.parsed[filename]) {
return cb(undefined);
}
model.parsed[filename] = true;
if (model.idls[filename] || model.asts[filename]) {
return model.loadIncludedModules(filename, cb);
}
model.fs.readFile(filename, 'utf-8', function (err, source) {
if (err) {
return cb(err);
}
model.idls[filename] = source;
model.loadIncludedModules(filename, cb);
});
}
Thrift.prototype.loadIncludedModules = function loadIncludedModules(filename, cb) {
var model = this;
var dirname = path.dirname(filename);
var defs;
try {
if (!model.asts[filename]) {
model.asts[filename] = idl.parse(model.idls[filename]);
}
defs = model.asts[filename].headers.concat(model.asts[filename].definitions);
model.validateIncludedModules(dirname, defs);
} catch (err) {
return cb(err);
}
asyncEach(defs, function (def, handleCb) {
if (def.type !== 'Include') {
return handleCb(undefined);
}
var includeFilename = path.join(dirname, def.id);
model.load(includeFilename, function (err) {
handleCb(err);
});
}, cb);
}
Thrift.prototype.loadSync = function _parse(filename) {
if (this.parsed[filename]) {
return;
}
this.parsed[filename] = true;
if (!this.idls[filename] && !this.asts[filename]) {
/* eslint-disable max-len */
assert.ok(this.fs, filename + ': Thrift must be constructed with either a complete set of options.idls, options.asts, or options.fs access');
/* eslint-enable max-len */
this.idls[filename] = this.fs.readFileSync(path.resolve(filename), 'utf-8');
}
if (!this.asts[filename]) {
this.asts[filename] = idl.parse(this.idls[filename]);
}
var dirname = path.dirname(filename);
var defs = this.asts[filename].headers.concat(this.asts[filename].definitions);
this.validateIncludedModules(dirname, defs);
for (var index = 0; index < defs.length; index++) {
var def = defs[index];
if (def.type !== 'Include') {
continue;
}
var includeFilename = path.join(dirname, def.id);
this.loadSync(includeFilename, true);
}
}
Thrift.prototype.validateIncludedModules = function validateIncludedModules(dirname, defs) {
for (var index = 0; index < defs.length; index++) {
var def = defs[index];
if (def.type !== 'Include') {
continue;
}
if (def.id.lastIndexOf('/', 0) === 0) {
throw Error('Include path string must not be an absolute path');
}
this.getNamespace(def);
}
}
Thrift.prototype.getNamespace = function getNamespace(def) {
var ns = def.namespace && def.namespace.name;
// If include isn't name, get filename sans *.thrift file extension.
if (!this.allowIncludeAlias || !ns) {
var basename = path.basename(def.id);
ns = basename.slice(0, basename.length - 7);
if (!validThriftIdentifierRE.test(ns)) {
throw Error('Thrift include filename is not valid thrift identifier');
}
}
return ns;
}
Thrift.prototype.compileAndLink = function compileAndLink() {
// Separate compile/link passes permits forward references and cyclic
// references.
this.compile();
// We only link from the root Thrift object.
if (!this.noLink) {
this.link();
}
}
Thrift.prototype.getType = function getType(name) {
return this.getTypeResult(name).toValue();
};
Thrift.prototype.getTypeResult = function getTypeResult(name) {
var model = this.models[name];
if (!model || model.models !== 'type') {
/* eslint-disable max-len */
return new Result(new Error(util.format('type %s not found. Make sure that the service name matches a service in the thrift file and that the method name is nested under that service.', name)));
/* eslint-enable max-len */
}
return new Result(null, model.link(this));
};
Thrift.prototype.getSources = function getSources() {
var filenames = Object.keys(this.idls);
var common = lcp.longestCommonPath(filenames);
var idls = {};
for (var index = 0; index < filenames.length; index++) {
var filename = filenames[index];
idls[filename.slice(common.length)] = this.idls[filename];
}
var entryPoint = this.filename.slice(common.length);
return {entryPoint: entryPoint, idls: idls};
};
Thrift.prototype.toJSON = function toJSON() {
var filenames = Object.keys(this.idls);
var common = lcp.longestCommonPath(filenames);
var asts = {};
for (var index = 0; index < filenames.length; index++) {
var filename = filenames[index];
asts[filename.slice(common.length)] = this.asts[filename];
}
var entryPoint = this.filename.slice(common.length);
return {entryPoint: entryPoint, asts: asts};
};
Thrift.prototype.getServiceEndpoints = function getServiceEndpoints(target) {
target = target || null;
var services = Object.keys(this.services);
var endpoints = [];
for (var i = 0; i < services.length; i++) {
var service = this.models[services[i]];
if (!target || target === service.name) {
for (var j = 0; j < service.functions.length; j++) {
endpoints.push(service.name + '::' + service.functions[j].name);
}
}
}
return endpoints;
};
Thrift.prototype.baseTypes = {
void: ThriftVoid,
bool: ThriftBoolean,
byte: ThriftI8,
i8: ThriftI8,
i16: ThriftI16,
i32: ThriftI32,
i64: ThriftI64,
double: ThriftDouble,
string: ThriftString,
binary: ThriftBinary
};
Thrift.prototype.compile = function compile() {
var syntax = this.asts[this.filename];
assert.equal(syntax.type, 'Program', 'expected a program');
this._compile(syntax.headers);
this._compile(syntax.definitions);
this.compileEnum(messageExceptionTypesDef);
this.exception = this.compileStruct(messageExceptionDef);
};
Thrift.prototype.define = function define(name, def, model) {
assert(!this.models[name], 'duplicate reference to ' + name + ' at ' + def.line + ':' + def.column);
this.models[name] = model;
};
Thrift.prototype.compilers = {
// sorted
Const: 'compileConst',
Enum: 'compileEnum',
Exception: 'compileException',
Include: 'compileInclude',
Service: 'compileService',
Struct: 'compileStruct',
Typedef: 'compileTypedef',
Union: 'compileUnion'
};
Thrift.prototype._compile = function _compile(defs) {
for (var index = 0; index < defs.length; index++) {
var def = defs[index];
var compilerName = this.compilers[def.type];
// istanbul ignore else
if (compilerName) {
this[compilerName](def);
}
}
};
Thrift.prototype.compileInclude = function compileInclude(def) {
var filename = path.join(this.dirname, def.id);
var ns = this.getNamespace(def);
var model;
if (this.memo[filename]) {
model = this.memo[filename];
} else {
model = new Thrift({
entryPoint: filename,
parsed: this.parsed,
idls: this.idls,
asts: this.asts,
memo: this.memo,
strict: this.strict,
allowIncludeAlias: this.allowIncludeAlias,
allowOptionalArguments: this.allowOptionalArguments,
noLink: true,
defaultAsUndefined: this.defaultAsUndefined
});
}
this.define(ns, def, model);
// Alias if first character is not lower-case
this.modules[ns] = model;
if (!/^[a-z]/.test(ns)) {
this[ns] = model;
}
};
Thrift.prototype.compileStruct = function compileStruct(def) {
var model = new ThriftStruct({strict: this.strict, defaultValueDefinition: this.defaultValueDefinition});
model.compile(def, this);
this.define(model.fullName, def, model);
return model;
};
Thrift.prototype.compileException = function compileException(def) {
var model = new ThriftStruct({strict: this.strict, isException: true});
model.compile(def, this);
this.define(model.fullName, def, model);
return model;
};
Thrift.prototype.compileUnion = function compileUnion(def) {
var model = new ThriftUnion({strict: this.strict});
model.compile(def, this);
this.define(model.fullName, def, model);
return model;
};
Thrift.prototype.compileTypedef = function compileTypedef(def) {
var model = new ThriftTypedef({strict: this.strict});
model.compile(def, this);
this.define(model.name, def, model);
return model;
};
Thrift.prototype.compileService = function compileService(def) {
var service = new ThriftService({strict: this.strict});
service.compile(def, this);
this.define(service.name, def.id, service);
};
Thrift.prototype.compileConst = function compileConst(def, model) {
var thriftConst = new ThriftConst(def);
this.define(def.id.name, def.id, thriftConst);
};
Thrift.prototype.compileEnum = function compileEnum(def) {
var model = new ThriftEnum();
model.compile(def, this);
this.define(model.name, def.id, model);
};
Thrift.prototype.link = function link() {
if (this.linked) {
return this;
}
this.linked = true;
var names = Object.keys(this.models);
for (var index = 0; index < names.length; index++) {
this.models[names[index]].link(this);
}
this.exception.link(this);
return this;
};
Thrift.prototype.resolve = function resolve(def) {
// istanbul ignore else
if (def.type === 'BaseType') {
return new this.baseTypes[def.baseType](def.annotations);
} else if (def.type === 'Identifier') {
return this.resolveIdentifier(def, def.name, 'type');
} else if (def.type === 'List') {
return new ThriftList(this.resolve(def.valueType), def.annotations);
} else if (def.type === 'Set') {
return new ThriftSet(this.resolve(def.valueType), def.annotations);
} else if (def.type === 'Map') {
return new ThriftMap(this.resolve(def.keyType), this.resolve(def.valueType), def.annotations);
} else {
assert.fail(util.format(
'Can\'t get reader/writer for definition with unknown type %s at %s:%s',
def.type, def.line, def.column
));
}
};
// TODO thread type model and validate / coerce
Thrift.prototype.resolveValue = function resolveValue(def) {
// istanbul ignore else
if (!def) {
return null;
} else if (def.type === 'Literal') {
return def.value;
} else if (def.type === 'ConstList') {
return this.resolveListConst(def);
} else if (def.type === 'ConstMap') {
return this.resolveMapConst(def);
} else if (def.type === 'Identifier') {
if (def.name === 'true') {
return true;
} else if (def.name === 'false') {
return false;
}
return this.resolveIdentifier(def, def.name, 'value').value;
} else {
assert.fail('unrecognized const type ' + def.type);
}
};
Thrift.prototype.resolveListConst = function resolveListConst(def) {
var list = [];
for (var index = 0; index < def.values.length; index++) {
list.push(this.resolveValue(def.values[index]));
}
return list;
};
Thrift.prototype.resolveMapConst = function resolveMapConst(def) {
var map = {};
for (var index = 0; index < def.entries.length; index++) {
map[this.resolveValue(def.entries[index].key)] =
this.resolveValue(def.entries[index].value);
}
return map;
};
Thrift.prototype.resolveIdentifier = function resolveIdentifier(def, name, models) {
var model;
// short circuit if in global namespace of this thrift.
if (this.models[name]) {
model = this.models[name].link(this);
if (model.models !== models) {
err = new Error(
'type mismatch for ' + def.name + ' at ' + def.line + ':' + def.column +
', expects ' + models + ', got ' + model.models
);
err.line = def.line;
err.column = def.column;
throw err;
}
return model;
}
var parts = name.split('.');
var err;
var module = this.modules[parts.shift()];
if (module) {
return module.resolveIdentifier(def, parts.join('.'), models);
} else {
err = new Error('cannot resolve reference to ' + def.name + ' at ' + def.line + ':' + def.column);
err.line = def.line;
err.column = def.column;
throw err;
}
};
module.exports.Thrift = Thrift;