@depup/apollo__protobufjs 1.2.7-depup.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/LICENSE +39 -0
- package/README.md +32 -0
- package/bin/pbjs +6 -0
- package/bin/pbts +6 -0
- package/changes.json +14 -0
- package/cli/LICENSE +33 -0
- package/cli/README.md +11 -0
- package/cli/bin/pbjs +6 -0
- package/cli/bin/pbts +6 -0
- package/cli/index.d.ts +3 -0
- package/cli/index.js +3 -0
- package/cli/lib/tsd-jsdoc/LICENSE +21 -0
- package/cli/lib/tsd-jsdoc/README.md +23 -0
- package/cli/lib/tsd-jsdoc/plugin.js +21 -0
- package/cli/lib/tsd-jsdoc/publish.js +693 -0
- package/cli/lib/tsd-jsdoc.json +18 -0
- package/cli/package.json +7 -0
- package/cli/package.standalone.json +32 -0
- package/cli/pbjs.d.ts +9 -0
- package/cli/pbjs.js +331 -0
- package/cli/pbts.d.ts +9 -0
- package/cli/pbts.js +198 -0
- package/cli/targets/json-module.js +38 -0
- package/cli/targets/json.js +8 -0
- package/cli/targets/proto.js +326 -0
- package/cli/targets/proto2.js +10 -0
- package/cli/targets/proto3.js +10 -0
- package/cli/targets/static-module.js +29 -0
- package/cli/targets/static.js +709 -0
- package/cli/util.js +183 -0
- package/cli/wrappers/amd.js +7 -0
- package/cli/wrappers/closure.js +7 -0
- package/cli/wrappers/commonjs.js +7 -0
- package/cli/wrappers/default.js +15 -0
- package/cli/wrappers/es6.js +5 -0
- package/dist/README.md +31 -0
- package/dist/light/README.md +31 -0
- package/dist/light/protobuf.js +7198 -0
- package/dist/light/protobuf.js.map +1 -0
- package/dist/light/protobuf.min.js +7 -0
- package/dist/light/protobuf.min.js.map +1 -0
- package/dist/minimal/README.md +31 -0
- package/dist/minimal/protobuf.js +2675 -0
- package/dist/minimal/protobuf.js.map +1 -0
- package/dist/minimal/protobuf.min.js +7 -0
- package/dist/minimal/protobuf.min.js.map +1 -0
- package/dist/protobuf.js +8775 -0
- package/dist/protobuf.js.map +1 -0
- package/dist/protobuf.min.js +7 -0
- package/dist/protobuf.min.js.map +1 -0
- package/ext/debug/README.md +4 -0
- package/ext/debug/index.js +71 -0
- package/ext/descriptor/README.md +72 -0
- package/ext/descriptor/index.d.ts +191 -0
- package/ext/descriptor/index.js +1052 -0
- package/ext/descriptor/test.js +54 -0
- package/google/LICENSE +27 -0
- package/google/README.md +1 -0
- package/google/api/annotations.json +83 -0
- package/google/api/annotations.proto +11 -0
- package/google/api/http.json +86 -0
- package/google/api/http.proto +31 -0
- package/google/protobuf/api.json +118 -0
- package/google/protobuf/api.proto +34 -0
- package/google/protobuf/descriptor.json +739 -0
- package/google/protobuf/descriptor.proto +286 -0
- package/google/protobuf/source_context.json +20 -0
- package/google/protobuf/source_context.proto +7 -0
- package/google/protobuf/type.json +202 -0
- package/google/protobuf/type.proto +89 -0
- package/index.d.ts +2628 -0
- package/index.js +4 -0
- package/light.d.ts +2 -0
- package/light.js +4 -0
- package/minimal.d.ts +2 -0
- package/minimal.js +4 -0
- package/package.json +147 -0
- package/scripts/postinstall.js +35 -0
- package/src/common.js +399 -0
- package/src/converter.js +304 -0
- package/src/decoder.js +106 -0
- package/src/encoder.js +119 -0
- package/src/enum.js +181 -0
- package/src/field.js +379 -0
- package/src/index-light.js +104 -0
- package/src/index-minimal.js +36 -0
- package/src/index.js +12 -0
- package/src/mapfield.js +126 -0
- package/src/message.js +139 -0
- package/src/method.js +151 -0
- package/src/namespace.js +433 -0
- package/src/object.js +200 -0
- package/src/oneof.js +203 -0
- package/src/parse.js +761 -0
- package/src/reader.js +405 -0
- package/src/reader_buffer.js +44 -0
- package/src/root.js +353 -0
- package/src/roots.js +18 -0
- package/src/rpc/service.js +142 -0
- package/src/rpc.js +36 -0
- package/src/service.js +167 -0
- package/src/tokenize.js +397 -0
- package/src/type.js +589 -0
- package/src/types.js +196 -0
- package/src/typescript.jsdoc +22 -0
- package/src/util/longbits.js +200 -0
- package/src/util/minimal.js +406 -0
- package/src/util.js +178 -0
- package/src/verifier.js +191 -0
- package/src/wrappers.js +83 -0
- package/src/writer.js +459 -0
- package/src/writer_buffer.js +81 -0
- package/tsconfig.json +7 -0
package/src/namespace.js
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = Namespace;
|
|
3
|
+
|
|
4
|
+
// extends ReflectionObject
|
|
5
|
+
var ReflectionObject = require("./object");
|
|
6
|
+
((Namespace.prototype = Object.create(ReflectionObject.prototype)).constructor = Namespace).className = "Namespace";
|
|
7
|
+
|
|
8
|
+
var Field = require("./field"),
|
|
9
|
+
util = require("./util");
|
|
10
|
+
|
|
11
|
+
var Type, // cyclic
|
|
12
|
+
Service,
|
|
13
|
+
Enum;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Constructs a new namespace instance.
|
|
17
|
+
* @name Namespace
|
|
18
|
+
* @classdesc Reflected namespace.
|
|
19
|
+
* @extends NamespaceBase
|
|
20
|
+
* @constructor
|
|
21
|
+
* @param {string} name Namespace name
|
|
22
|
+
* @param {Object.<string,*>} [options] Declared options
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Constructs a namespace from JSON.
|
|
27
|
+
* @memberof Namespace
|
|
28
|
+
* @function
|
|
29
|
+
* @param {string} name Namespace name
|
|
30
|
+
* @param {Object.<string,*>} json JSON object
|
|
31
|
+
* @returns {Namespace} Created namespace
|
|
32
|
+
* @throws {TypeError} If arguments are invalid
|
|
33
|
+
*/
|
|
34
|
+
Namespace.fromJSON = function fromJSON(name, json) {
|
|
35
|
+
return new Namespace(name, json.options).addJSON(json.nested);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Converts an array of reflection objects to JSON.
|
|
40
|
+
* @memberof Namespace
|
|
41
|
+
* @param {ReflectionObject[]} array Object array
|
|
42
|
+
* @param {IToJSONOptions} [toJSONOptions] JSON conversion options
|
|
43
|
+
* @returns {Object.<string,*>|undefined} JSON object or `undefined` when array is empty
|
|
44
|
+
*/
|
|
45
|
+
function arrayToJSON(array, toJSONOptions) {
|
|
46
|
+
if (!(array && array.length))
|
|
47
|
+
return undefined;
|
|
48
|
+
var obj = {};
|
|
49
|
+
for (var i = 0; i < array.length; ++i)
|
|
50
|
+
obj[array[i].name] = array[i].toJSON(toJSONOptions);
|
|
51
|
+
return obj;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
Namespace.arrayToJSON = arrayToJSON;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Tests if the specified id is reserved.
|
|
58
|
+
* @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
|
|
59
|
+
* @param {number} id Id to test
|
|
60
|
+
* @returns {boolean} `true` if reserved, otherwise `false`
|
|
61
|
+
*/
|
|
62
|
+
Namespace.isReservedId = function isReservedId(reserved, id) {
|
|
63
|
+
if (reserved)
|
|
64
|
+
for (var i = 0; i < reserved.length; ++i)
|
|
65
|
+
if (typeof reserved[i] !== "string" && reserved[i][0] <= id && reserved[i][1] > id)
|
|
66
|
+
return true;
|
|
67
|
+
return false;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Tests if the specified name is reserved.
|
|
72
|
+
* @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
|
|
73
|
+
* @param {string} name Name to test
|
|
74
|
+
* @returns {boolean} `true` if reserved, otherwise `false`
|
|
75
|
+
*/
|
|
76
|
+
Namespace.isReservedName = function isReservedName(reserved, name) {
|
|
77
|
+
if (reserved)
|
|
78
|
+
for (var i = 0; i < reserved.length; ++i)
|
|
79
|
+
if (reserved[i] === name)
|
|
80
|
+
return true;
|
|
81
|
+
return false;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Not an actual constructor. Use {@link Namespace} instead.
|
|
86
|
+
* @classdesc Base class of all reflection objects containing nested objects. This is not an actual class but here for the sake of having consistent type definitions.
|
|
87
|
+
* @exports NamespaceBase
|
|
88
|
+
* @extends ReflectionObject
|
|
89
|
+
* @abstract
|
|
90
|
+
* @constructor
|
|
91
|
+
* @param {string} name Namespace name
|
|
92
|
+
* @param {Object.<string,*>} [options] Declared options
|
|
93
|
+
* @see {@link Namespace}
|
|
94
|
+
*/
|
|
95
|
+
function Namespace(name, options) {
|
|
96
|
+
ReflectionObject.call(this, name, options);
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Nested objects by name.
|
|
100
|
+
* @type {Object.<string,ReflectionObject>|undefined}
|
|
101
|
+
*/
|
|
102
|
+
this.nested = undefined; // toJSON
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Cached nested objects as an array.
|
|
106
|
+
* @type {ReflectionObject[]|null}
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
109
|
+
this._nestedArray = null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function clearCache(namespace) {
|
|
113
|
+
namespace._nestedArray = null;
|
|
114
|
+
return namespace;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Nested objects of this namespace as an array for iteration.
|
|
119
|
+
* @name NamespaceBase#nestedArray
|
|
120
|
+
* @type {ReflectionObject[]}
|
|
121
|
+
* @readonly
|
|
122
|
+
*/
|
|
123
|
+
Object.defineProperty(Namespace.prototype, "nestedArray", {
|
|
124
|
+
get: function() {
|
|
125
|
+
return this._nestedArray || (this._nestedArray = util.toArray(this.nested));
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Namespace descriptor.
|
|
131
|
+
* @interface INamespace
|
|
132
|
+
* @property {Object.<string,*>} [options] Namespace options
|
|
133
|
+
* @property {Object.<string,AnyNestedObject>} [nested] Nested object descriptors
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Any extension field descriptor.
|
|
138
|
+
* @typedef AnyExtensionField
|
|
139
|
+
* @type {IExtensionField|IExtensionMapField}
|
|
140
|
+
*/
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Any nested object descriptor.
|
|
144
|
+
* @typedef AnyNestedObject
|
|
145
|
+
* @type {IEnum|IType|IService|AnyExtensionField|INamespace}
|
|
146
|
+
*/
|
|
147
|
+
// ^ BEWARE: VSCode hangs forever when using more than 5 types (that's why AnyExtensionField exists in the first place)
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Converts this namespace to a namespace descriptor.
|
|
151
|
+
* @param {IToJSONOptions} [toJSONOptions] JSON conversion options
|
|
152
|
+
* @returns {INamespace} Namespace descriptor
|
|
153
|
+
*/
|
|
154
|
+
Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
|
|
155
|
+
return util.toObject([
|
|
156
|
+
"options" , this.options,
|
|
157
|
+
"nested" , arrayToJSON(this.nestedArray, toJSONOptions)
|
|
158
|
+
]);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Adds nested objects to this namespace from nested object descriptors.
|
|
163
|
+
* @param {Object.<string,AnyNestedObject>} nestedJson Any nested object descriptors
|
|
164
|
+
* @returns {Namespace} `this`
|
|
165
|
+
*/
|
|
166
|
+
Namespace.prototype.addJSON = function addJSON(nestedJson) {
|
|
167
|
+
var ns = this;
|
|
168
|
+
/* istanbul ignore else */
|
|
169
|
+
if (nestedJson) {
|
|
170
|
+
for (var names = Object.keys(nestedJson), i = 0, nested; i < names.length; ++i) {
|
|
171
|
+
nested = nestedJson[names[i]];
|
|
172
|
+
ns.add( // most to least likely
|
|
173
|
+
( nested.fields !== undefined
|
|
174
|
+
? Type.fromJSON
|
|
175
|
+
: nested.values !== undefined
|
|
176
|
+
? Enum.fromJSON
|
|
177
|
+
: nested.methods !== undefined
|
|
178
|
+
? Service.fromJSON
|
|
179
|
+
: nested.id !== undefined
|
|
180
|
+
? Field.fromJSON
|
|
181
|
+
: Namespace.fromJSON )(names[i], nested)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return this;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Gets the nested object of the specified name.
|
|
190
|
+
* @param {string} name Nested object name
|
|
191
|
+
* @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist
|
|
192
|
+
*/
|
|
193
|
+
Namespace.prototype.get = function get(name) {
|
|
194
|
+
return this.nested && this.nested[name]
|
|
195
|
+
|| null;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Gets the values of the nested {@link Enum|enum} of the specified name.
|
|
200
|
+
* This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
|
|
201
|
+
* @param {string} name Nested enum name
|
|
202
|
+
* @returns {Object.<string,number>} Enum values
|
|
203
|
+
* @throws {Error} If there is no such enum
|
|
204
|
+
*/
|
|
205
|
+
Namespace.prototype.getEnum = function getEnum(name) {
|
|
206
|
+
if (this.nested && this.nested[name] instanceof Enum)
|
|
207
|
+
return this.nested[name].values;
|
|
208
|
+
throw Error("no such enum: " + name);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Adds a nested object to this namespace.
|
|
213
|
+
* @param {ReflectionObject} object Nested object to add
|
|
214
|
+
* @returns {Namespace} `this`
|
|
215
|
+
* @throws {TypeError} If arguments are invalid
|
|
216
|
+
* @throws {Error} If there is already a nested object with this name
|
|
217
|
+
*/
|
|
218
|
+
Namespace.prototype.add = function add(object) {
|
|
219
|
+
|
|
220
|
+
if (!(object instanceof Field && object.extend !== undefined || object instanceof Type || object instanceof Enum || object instanceof Service || object instanceof Namespace))
|
|
221
|
+
throw TypeError("object must be a valid nested object");
|
|
222
|
+
|
|
223
|
+
if (!this.nested)
|
|
224
|
+
this.nested = {};
|
|
225
|
+
else {
|
|
226
|
+
var prev = this.get(object.name);
|
|
227
|
+
if (prev) {
|
|
228
|
+
if (prev instanceof Namespace && object instanceof Namespace && !(prev instanceof Type || prev instanceof Service)) {
|
|
229
|
+
// replace plain namespace but keep existing nested elements and options
|
|
230
|
+
var nested = prev.nestedArray;
|
|
231
|
+
for (var i = 0; i < nested.length; ++i)
|
|
232
|
+
object.add(nested[i]);
|
|
233
|
+
this.remove(prev);
|
|
234
|
+
if (!this.nested)
|
|
235
|
+
this.nested = {};
|
|
236
|
+
object.setOptions(prev.options, true);
|
|
237
|
+
|
|
238
|
+
} else
|
|
239
|
+
throw Error("duplicate name '" + object.name + "' in " + this);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
this.nested[object.name] = object;
|
|
243
|
+
object.onAdd(this);
|
|
244
|
+
return clearCache(this);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Removes a nested object from this namespace.
|
|
249
|
+
* @param {ReflectionObject} object Nested object to remove
|
|
250
|
+
* @returns {Namespace} `this`
|
|
251
|
+
* @throws {TypeError} If arguments are invalid
|
|
252
|
+
* @throws {Error} If `object` is not a member of this namespace
|
|
253
|
+
*/
|
|
254
|
+
Namespace.prototype.remove = function remove(object) {
|
|
255
|
+
|
|
256
|
+
if (!(object instanceof ReflectionObject))
|
|
257
|
+
throw TypeError("object must be a ReflectionObject");
|
|
258
|
+
if (object.parent !== this)
|
|
259
|
+
throw Error(object + " is not a member of " + this);
|
|
260
|
+
|
|
261
|
+
delete this.nested[object.name];
|
|
262
|
+
if (!Object.keys(this.nested).length)
|
|
263
|
+
this.nested = undefined;
|
|
264
|
+
|
|
265
|
+
object.onRemove(this);
|
|
266
|
+
return clearCache(this);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Defines additial namespaces within this one if not yet existing.
|
|
271
|
+
* @param {string|string[]} path Path to create
|
|
272
|
+
* @param {*} [json] Nested types to create from JSON
|
|
273
|
+
* @returns {Namespace} Pointer to the last namespace created or `this` if path is empty
|
|
274
|
+
*/
|
|
275
|
+
Namespace.prototype.define = function define(path, json) {
|
|
276
|
+
|
|
277
|
+
if (util.isString(path))
|
|
278
|
+
path = path.split(".");
|
|
279
|
+
else if (!Array.isArray(path))
|
|
280
|
+
throw TypeError("illegal path");
|
|
281
|
+
if (path && path.length && path[0] === "")
|
|
282
|
+
throw Error("path must be relative");
|
|
283
|
+
|
|
284
|
+
var ptr = this;
|
|
285
|
+
while (path.length > 0) {
|
|
286
|
+
var part = path.shift();
|
|
287
|
+
if (ptr.nested && ptr.nested[part]) {
|
|
288
|
+
ptr = ptr.nested[part];
|
|
289
|
+
if (!(ptr instanceof Namespace))
|
|
290
|
+
throw Error("path conflicts with non-namespace objects");
|
|
291
|
+
} else
|
|
292
|
+
ptr.add(ptr = new Namespace(part));
|
|
293
|
+
}
|
|
294
|
+
if (json)
|
|
295
|
+
ptr.addJSON(json);
|
|
296
|
+
return ptr;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
|
|
301
|
+
* @returns {Namespace} `this`
|
|
302
|
+
*/
|
|
303
|
+
Namespace.prototype.resolveAll = function resolveAll() {
|
|
304
|
+
var nested = this.nestedArray, i = 0;
|
|
305
|
+
while (i < nested.length)
|
|
306
|
+
if (nested[i] instanceof Namespace)
|
|
307
|
+
nested[i++].resolveAll();
|
|
308
|
+
else
|
|
309
|
+
nested[i++].resolve();
|
|
310
|
+
return this.resolve();
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Recursively looks up the reflection object matching the specified path in the scope of this namespace.
|
|
315
|
+
* @param {string|string[]} path Path to look up
|
|
316
|
+
* @param {*|Array.<*>} filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
|
|
317
|
+
* @param {boolean} [parentAlreadyChecked=false] If known, whether the parent has already been checked
|
|
318
|
+
* @returns {ReflectionObject|null} Looked up object or `null` if none could be found
|
|
319
|
+
*/
|
|
320
|
+
Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
|
|
321
|
+
|
|
322
|
+
/* istanbul ignore next */
|
|
323
|
+
if (typeof filterTypes === "boolean") {
|
|
324
|
+
parentAlreadyChecked = filterTypes;
|
|
325
|
+
filterTypes = undefined;
|
|
326
|
+
} else if (filterTypes && !Array.isArray(filterTypes))
|
|
327
|
+
filterTypes = [ filterTypes ];
|
|
328
|
+
|
|
329
|
+
if (util.isString(path) && path.length) {
|
|
330
|
+
if (path === ".")
|
|
331
|
+
return this.root;
|
|
332
|
+
path = path.split(".");
|
|
333
|
+
} else if (!path.length)
|
|
334
|
+
return this;
|
|
335
|
+
|
|
336
|
+
// Start at root if path is absolute
|
|
337
|
+
if (path[0] === "")
|
|
338
|
+
return this.root.lookup(path.slice(1), filterTypes);
|
|
339
|
+
|
|
340
|
+
// Test if the first part matches any nested object, and if so, traverse if path contains more
|
|
341
|
+
var found = this.get(path[0]);
|
|
342
|
+
if (found) {
|
|
343
|
+
if (path.length === 1) {
|
|
344
|
+
if (!filterTypes || filterTypes.indexOf(found.constructor) > -1)
|
|
345
|
+
return found;
|
|
346
|
+
} else if (found instanceof Namespace && (found = found.lookup(path.slice(1), filterTypes, true)))
|
|
347
|
+
return found;
|
|
348
|
+
|
|
349
|
+
// Otherwise try each nested namespace
|
|
350
|
+
} else
|
|
351
|
+
for (var i = 0; i < this.nestedArray.length; ++i)
|
|
352
|
+
if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i].lookup(path, filterTypes, true)))
|
|
353
|
+
return found;
|
|
354
|
+
|
|
355
|
+
// If there hasn't been a match, try again at the parent
|
|
356
|
+
if (this.parent === null || parentAlreadyChecked)
|
|
357
|
+
return null;
|
|
358
|
+
return this.parent.lookup(path, filterTypes);
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Looks up the reflection object at the specified path, relative to this namespace.
|
|
363
|
+
* @name NamespaceBase#lookup
|
|
364
|
+
* @function
|
|
365
|
+
* @param {string|string[]} path Path to look up
|
|
366
|
+
* @param {boolean} [parentAlreadyChecked=false] Whether the parent has already been checked
|
|
367
|
+
* @returns {ReflectionObject|null} Looked up object or `null` if none could be found
|
|
368
|
+
* @variation 2
|
|
369
|
+
*/
|
|
370
|
+
// lookup(path: string, [parentAlreadyChecked: boolean])
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Looks up the {@link Type|type} at the specified path, relative to this namespace.
|
|
374
|
+
* Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
|
|
375
|
+
* @param {string|string[]} path Path to look up
|
|
376
|
+
* @returns {Type} Looked up type
|
|
377
|
+
* @throws {Error} If `path` does not point to a type
|
|
378
|
+
*/
|
|
379
|
+
Namespace.prototype.lookupType = function lookupType(path) {
|
|
380
|
+
var found = this.lookup(path, [ Type ]);
|
|
381
|
+
if (!found)
|
|
382
|
+
throw Error("no such type: " + path);
|
|
383
|
+
return found;
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
|
|
388
|
+
* Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
|
|
389
|
+
* @param {string|string[]} path Path to look up
|
|
390
|
+
* @returns {Enum} Looked up enum
|
|
391
|
+
* @throws {Error} If `path` does not point to an enum
|
|
392
|
+
*/
|
|
393
|
+
Namespace.prototype.lookupEnum = function lookupEnum(path) {
|
|
394
|
+
var found = this.lookup(path, [ Enum ]);
|
|
395
|
+
if (!found)
|
|
396
|
+
throw Error("no such Enum '" + path + "' in " + this);
|
|
397
|
+
return found;
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
|
|
402
|
+
* Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
|
|
403
|
+
* @param {string|string[]} path Path to look up
|
|
404
|
+
* @returns {Type} Looked up type or enum
|
|
405
|
+
* @throws {Error} If `path` does not point to a type or enum
|
|
406
|
+
*/
|
|
407
|
+
Namespace.prototype.lookupTypeOrEnum = function lookupTypeOrEnum(path) {
|
|
408
|
+
var found = this.lookup(path, [ Type, Enum ]);
|
|
409
|
+
if (!found)
|
|
410
|
+
throw Error("no such Type or Enum '" + path + "' in " + this);
|
|
411
|
+
return found;
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Looks up the {@link Service|service} at the specified path, relative to this namespace.
|
|
416
|
+
* Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
|
|
417
|
+
* @param {string|string[]} path Path to look up
|
|
418
|
+
* @returns {Service} Looked up service
|
|
419
|
+
* @throws {Error} If `path` does not point to a service
|
|
420
|
+
*/
|
|
421
|
+
Namespace.prototype.lookupService = function lookupService(path) {
|
|
422
|
+
var found = this.lookup(path, [ Service ]);
|
|
423
|
+
if (!found)
|
|
424
|
+
throw Error("no such Service '" + path + "' in " + this);
|
|
425
|
+
return found;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// Sets up cyclic dependencies (called in index-light)
|
|
429
|
+
Namespace._configure = function(Type_, Service_, Enum_) {
|
|
430
|
+
Type = Type_;
|
|
431
|
+
Service = Service_;
|
|
432
|
+
Enum = Enum_;
|
|
433
|
+
};
|
package/src/object.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = ReflectionObject;
|
|
3
|
+
|
|
4
|
+
ReflectionObject.className = "ReflectionObject";
|
|
5
|
+
|
|
6
|
+
var util = require("./util");
|
|
7
|
+
|
|
8
|
+
var Root; // cyclic
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Constructs a new reflection object instance.
|
|
12
|
+
* @classdesc Base class of all reflection objects.
|
|
13
|
+
* @constructor
|
|
14
|
+
* @param {string} name Object name
|
|
15
|
+
* @param {Object.<string,*>} [options] Declared options
|
|
16
|
+
* @abstract
|
|
17
|
+
*/
|
|
18
|
+
function ReflectionObject(name, options) {
|
|
19
|
+
|
|
20
|
+
if (!util.isString(name))
|
|
21
|
+
throw TypeError("name must be a string");
|
|
22
|
+
|
|
23
|
+
if (options && !util.isObject(options))
|
|
24
|
+
throw TypeError("options must be an object");
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Options.
|
|
28
|
+
* @type {Object.<string,*>|undefined}
|
|
29
|
+
*/
|
|
30
|
+
this.options = options; // toJSON
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Unique name within its namespace.
|
|
34
|
+
* @type {string}
|
|
35
|
+
*/
|
|
36
|
+
this.name = name;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parent namespace.
|
|
40
|
+
* @type {Namespace|null}
|
|
41
|
+
*/
|
|
42
|
+
this.parent = null;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Whether already resolved or not.
|
|
46
|
+
* @type {boolean}
|
|
47
|
+
*/
|
|
48
|
+
this.resolved = false;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Comment text, if any.
|
|
52
|
+
* @type {string|null}
|
|
53
|
+
*/
|
|
54
|
+
this.comment = null;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Defining file name.
|
|
58
|
+
* @type {string|null}
|
|
59
|
+
*/
|
|
60
|
+
this.filename = null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
Object.defineProperties(ReflectionObject.prototype, {
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Reference to the root namespace.
|
|
67
|
+
* @name ReflectionObject#root
|
|
68
|
+
* @type {Root}
|
|
69
|
+
* @readonly
|
|
70
|
+
*/
|
|
71
|
+
root: {
|
|
72
|
+
get: function() {
|
|
73
|
+
var ptr = this;
|
|
74
|
+
while (ptr.parent !== null)
|
|
75
|
+
ptr = ptr.parent;
|
|
76
|
+
return ptr;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Full name including leading dot.
|
|
82
|
+
* @name ReflectionObject#fullName
|
|
83
|
+
* @type {string}
|
|
84
|
+
* @readonly
|
|
85
|
+
*/
|
|
86
|
+
fullName: {
|
|
87
|
+
get: function() {
|
|
88
|
+
var path = [ this.name ],
|
|
89
|
+
ptr = this.parent;
|
|
90
|
+
while (ptr) {
|
|
91
|
+
path.unshift(ptr.name);
|
|
92
|
+
ptr = ptr.parent;
|
|
93
|
+
}
|
|
94
|
+
return path.join(".");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Converts this reflection object to its descriptor representation.
|
|
101
|
+
* @returns {Object.<string,*>} Descriptor
|
|
102
|
+
* @abstract
|
|
103
|
+
*/
|
|
104
|
+
ReflectionObject.prototype.toJSON = /* istanbul ignore next */ function toJSON() {
|
|
105
|
+
throw Error(); // not implemented, shouldn't happen
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Called when this object is added to a parent.
|
|
110
|
+
* @param {ReflectionObject} parent Parent added to
|
|
111
|
+
* @returns {undefined}
|
|
112
|
+
*/
|
|
113
|
+
ReflectionObject.prototype.onAdd = function onAdd(parent) {
|
|
114
|
+
if (this.parent && this.parent !== parent)
|
|
115
|
+
this.parent.remove(this);
|
|
116
|
+
this.parent = parent;
|
|
117
|
+
this.resolved = false;
|
|
118
|
+
var root = parent.root;
|
|
119
|
+
if (root instanceof Root)
|
|
120
|
+
root._handleAdd(this);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Called when this object is removed from a parent.
|
|
125
|
+
* @param {ReflectionObject} parent Parent removed from
|
|
126
|
+
* @returns {undefined}
|
|
127
|
+
*/
|
|
128
|
+
ReflectionObject.prototype.onRemove = function onRemove(parent) {
|
|
129
|
+
var root = parent.root;
|
|
130
|
+
if (root instanceof Root)
|
|
131
|
+
root._handleRemove(this);
|
|
132
|
+
this.parent = null;
|
|
133
|
+
this.resolved = false;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Resolves this objects type references.
|
|
138
|
+
* @returns {ReflectionObject} `this`
|
|
139
|
+
*/
|
|
140
|
+
ReflectionObject.prototype.resolve = function resolve() {
|
|
141
|
+
if (this.resolved)
|
|
142
|
+
return this;
|
|
143
|
+
if (this.root instanceof Root)
|
|
144
|
+
this.resolved = true; // only if part of a root
|
|
145
|
+
return this;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Gets an option value.
|
|
150
|
+
* @param {string} name Option name
|
|
151
|
+
* @returns {*} Option value or `undefined` if not set
|
|
152
|
+
*/
|
|
153
|
+
ReflectionObject.prototype.getOption = function getOption(name) {
|
|
154
|
+
if (this.options)
|
|
155
|
+
return this.options[name];
|
|
156
|
+
return undefined;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Sets an option.
|
|
161
|
+
* @param {string} name Option name
|
|
162
|
+
* @param {*} value Option value
|
|
163
|
+
* @param {boolean} [ifNotSet] Sets the option only if it isn't currently set
|
|
164
|
+
* @returns {ReflectionObject} `this`
|
|
165
|
+
*/
|
|
166
|
+
ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) {
|
|
167
|
+
if (!ifNotSet || !this.options || this.options[name] === undefined)
|
|
168
|
+
(this.options || (this.options = {}))[name] = value;
|
|
169
|
+
return this;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Sets multiple options.
|
|
174
|
+
* @param {Object.<string,*>} options Options to set
|
|
175
|
+
* @param {boolean} [ifNotSet] Sets an option only if it isn't currently set
|
|
176
|
+
* @returns {ReflectionObject} `this`
|
|
177
|
+
*/
|
|
178
|
+
ReflectionObject.prototype.setOptions = function setOptions(options, ifNotSet) {
|
|
179
|
+
if (options)
|
|
180
|
+
for (var keys = Object.keys(options), i = 0; i < keys.length; ++i)
|
|
181
|
+
this.setOption(keys[i], options[keys[i]], ifNotSet);
|
|
182
|
+
return this;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Converts this instance to its string representation.
|
|
187
|
+
* @returns {string} Class name[, space, full name]
|
|
188
|
+
*/
|
|
189
|
+
ReflectionObject.prototype.toString = function toString() {
|
|
190
|
+
var className = this.constructor.className,
|
|
191
|
+
fullName = this.fullName;
|
|
192
|
+
if (fullName.length)
|
|
193
|
+
return className + " " + fullName;
|
|
194
|
+
return className;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Sets up cyclic dependencies (called in index-light)
|
|
198
|
+
ReflectionObject._configure = function(Root_) {
|
|
199
|
+
Root = Root_;
|
|
200
|
+
};
|