protobufjs 8.2.1 → 8.3.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/README.md +60 -23
- package/dist/light/protobuf.js +21 -13
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +2 -2
- package/dist/minimal/protobuf.min.js +2 -2
- package/dist/protobuf.js +21 -13
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/descriptor.d.ts +11 -234
- package/ext/descriptor.generated.d.ts +409 -0
- package/ext/descriptor.js +89 -64
- package/ext/textformat.d.ts +2 -7
- package/ext/textformat.generated.d.ts +11 -0
- package/ext/textformat.js +11 -3
- package/index.d.ts +301 -267
- package/package.json +4 -4
- package/src/enum.js +8 -3
- package/src/field.js +2 -0
- package/src/message.js +3 -6
- package/src/method.js +1 -1
- package/src/object.js +0 -1
- package/src/oneof.js +1 -0
- package/src/service.js +2 -0
- package/src/type.js +2 -0
- package/src/typescript.js +19 -0
- package/tsconfig.json +2 -4
- package/src/typescript.jsdoc +0 -15
package/README.md
CHANGED
|
@@ -70,7 +70,7 @@ protobuf.js converts `.proto` field names to camelCase by default, so `awesome_f
|
|
|
70
70
|
|
|
71
71
|
### Load a schema
|
|
72
72
|
|
|
73
|
-
```
|
|
73
|
+
```ts
|
|
74
74
|
const protobuf = require("protobufjs");
|
|
75
75
|
|
|
76
76
|
const root = protobuf.loadSync("awesome.proto");
|
|
@@ -81,7 +81,7 @@ Use `protobuf.load()` for the asynchronous variant.
|
|
|
81
81
|
|
|
82
82
|
### Encode and decode
|
|
83
83
|
|
|
84
|
-
```
|
|
84
|
+
```ts
|
|
85
85
|
const payload = { awesomeField: "hello" };
|
|
86
86
|
|
|
87
87
|
// Optionally verify if the payload is of uncertain shape
|
|
@@ -103,7 +103,7 @@ Install [`long`](https://github.com/dcodeIO/long.js) with protobuf.js when exact
|
|
|
103
103
|
|
|
104
104
|
### Convert plain objects
|
|
105
105
|
|
|
106
|
-
```
|
|
106
|
+
```ts
|
|
107
107
|
const message = AwesomeMessage.fromObject({ awesomeField: 42 });
|
|
108
108
|
const object = AwesomeMessage.toObject(message, {
|
|
109
109
|
longs: String,
|
|
@@ -197,7 +197,7 @@ Bundling schemas avoids reparsing `.proto` files at runtime and can reduce brows
|
|
|
197
197
|
npx pbjs -t json -o awesome.json awesome1.proto awesome2.proto ...
|
|
198
198
|
```
|
|
199
199
|
|
|
200
|
-
```
|
|
200
|
+
```ts
|
|
201
201
|
const bundle = require("./awesome.json");
|
|
202
202
|
|
|
203
203
|
const root = protobuf.Root.fromJSON(bundle);
|
|
@@ -208,7 +208,7 @@ const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
|
|
|
208
208
|
npx pbjs -t json-module -w esm -o awesome.js --dts awesome.proto
|
|
209
209
|
```
|
|
210
210
|
|
|
211
|
-
```
|
|
211
|
+
```ts
|
|
212
212
|
import { awesomepackage } from "./awesome.js";
|
|
213
213
|
|
|
214
214
|
const AwesomeMessage = awesomepackage.AwesomeMessage;
|
|
@@ -218,7 +218,45 @@ JSON modules export the reflection root and, with `-w esm`, also provide top-lev
|
|
|
218
218
|
|
|
219
219
|
### TypeScript integration
|
|
220
220
|
|
|
221
|
-
|
|
221
|
+
protobuf.js works with TypeScript out of the box: the runtime API is typed, and generated declarations expose idiomatic TypeScript types with type-checked oneofs and JavaScript-friendly plain-object input. No separate `protoc` invocation or TypeScript transpile step is necessary.
|
|
222
|
+
|
|
223
|
+
For example, given the oneof:
|
|
224
|
+
|
|
225
|
+
```proto
|
|
226
|
+
message Profile {
|
|
227
|
+
oneof contact {
|
|
228
|
+
string email = 1;
|
|
229
|
+
string phone = 2;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Generated declarations narrow both the `contact` oneof and the concrete values:
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
const profile = Profile.create({
|
|
238
|
+
contact: "email",
|
|
239
|
+
email: "hello@example.com"
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
if (profile.contact === "email") {
|
|
243
|
+
profile.email; // string
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const decoded = Profile.decode(bytes);
|
|
247
|
+
if (decoded.phone != null) {
|
|
248
|
+
decoded.phone; // string
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Plain objects can use the same narrowed shape through a collision-free scoped type:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
const object: Profile.$Shape = {
|
|
256
|
+
contact: "email",
|
|
257
|
+
email: "hello@example.com"
|
|
258
|
+
};
|
|
259
|
+
```
|
|
222
260
|
|
|
223
261
|
## Advanced usage
|
|
224
262
|
|
|
@@ -226,7 +264,7 @@ TypeScript is a first-class target in protobuf.js. The runtime API is typed, and
|
|
|
226
264
|
|
|
227
265
|
The full and light builds can construct schemas directly through reflection:
|
|
228
266
|
|
|
229
|
-
```
|
|
267
|
+
```ts
|
|
230
268
|
const AwesomeMessage = new protobuf.Type("AwesomeMessage")
|
|
231
269
|
.add(new protobuf.Field("awesomeField", 1, "string"));
|
|
232
270
|
|
|
@@ -237,30 +275,29 @@ const root = new protobuf.Root()
|
|
|
237
275
|
|
|
238
276
|
### Custom message classes
|
|
239
277
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
```js
|
|
243
|
-
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;
|
|
278
|
+
A reflected type can use a custom class as its runtime constructor:
|
|
244
279
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
AwesomeMessage.prototype.customInstanceMethod = function() {
|
|
249
|
-
// ...
|
|
250
|
-
};
|
|
251
|
-
```
|
|
280
|
+
```ts
|
|
281
|
+
class AwesomeMessage extends protobuf.Message<AwesomeMessage> {
|
|
282
|
+
awesomeField = "";
|
|
252
283
|
|
|
253
|
-
|
|
284
|
+
constructor(properties?: protobuf.Properties<AwesomeMessage>) {
|
|
285
|
+
super(properties);
|
|
286
|
+
// ...
|
|
287
|
+
}
|
|
254
288
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
289
|
+
customInstanceMethod() {
|
|
290
|
+
return this.awesomeField.toLowerCase();
|
|
291
|
+
}
|
|
258
292
|
}
|
|
259
293
|
|
|
260
294
|
root.lookupType("awesomepackage.AwesomeMessage").ctor = AwesomeMessage;
|
|
295
|
+
|
|
296
|
+
const decoded = AwesomeMessage.decode(bytes);
|
|
297
|
+
decoded.customInstanceMethod(); // string
|
|
261
298
|
```
|
|
262
299
|
|
|
263
|
-
|
|
300
|
+
protobuf.js will populate the constructor with the usual static runtime methods and use it for decoded messages. In TypeScript, custom members are visible when using the custom class type in consuming code.
|
|
264
301
|
|
|
265
302
|
### Services
|
|
266
303
|
|
package/dist/light/protobuf.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* protobuf.js v8.
|
|
3
|
-
* compiled wed, 13 may 2026
|
|
2
|
+
* protobuf.js v8.3.0 (c) 2016, daniel wirtz
|
|
3
|
+
* compiled wed, 13 may 2026 21:15:34 utc
|
|
4
4
|
* licensed under the bsd-3-clause license
|
|
5
5
|
* see: https://github.com/dcodeio/protobuf.js for details
|
|
6
6
|
*/
|
|
@@ -714,7 +714,7 @@ var Namespace = require(12),
|
|
|
714
714
|
* @param {Object.<string,number>} [values] Enum values as an object, by name
|
|
715
715
|
* @param {Object.<string,*>} [options] Declared options
|
|
716
716
|
* @param {string} [comment] The comment for this enum
|
|
717
|
-
* @param {Object.<string,string>} [comments] The value comments for this enum
|
|
717
|
+
* @param {Object.<string,string|null>} [comments] The value comments for this enum
|
|
718
718
|
* @param {Object.<string,Object<string,*>>|undefined} [valuesOptions] The value options for this enum
|
|
719
719
|
*/
|
|
720
720
|
function Enum(name, values, options, comment, comments, valuesOptions) {
|
|
@@ -743,7 +743,7 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
|
|
|
743
743
|
|
|
744
744
|
/**
|
|
745
745
|
* Value comment texts, if any.
|
|
746
|
-
* @type {Object.<string,string>}
|
|
746
|
+
* @type {Object.<string,string|null>}
|
|
747
747
|
*/
|
|
748
748
|
this.comments = comments || {};
|
|
749
749
|
|
|
@@ -793,8 +793,13 @@ Enum.prototype._resolveFeatures = function _resolveFeatures(edition) {
|
|
|
793
793
|
/**
|
|
794
794
|
* Enum descriptor.
|
|
795
795
|
* @interface IEnum
|
|
796
|
+
* @property {string} [edition] Edition
|
|
796
797
|
* @property {Object.<string,number>} values Enum values
|
|
797
798
|
* @property {Object.<string,*>} [options] Enum options
|
|
799
|
+
* @property {Object.<string,Object.<string,*>>} [valuesOptions] Enum value options
|
|
800
|
+
* @property {Array.<number[]|string>} [reserved] Reserved ranges
|
|
801
|
+
* @property {string|null} [comment] Enum comment
|
|
802
|
+
* @property {Object.<string,string|null>} [comments] Value comments
|
|
798
803
|
*/
|
|
799
804
|
|
|
800
805
|
/**
|
|
@@ -805,7 +810,7 @@ Enum.prototype._resolveFeatures = function _resolveFeatures(edition) {
|
|
|
805
810
|
* @throws {TypeError} If arguments are invalid
|
|
806
811
|
*/
|
|
807
812
|
Enum.fromJSON = function fromJSON(name, json) {
|
|
808
|
-
var enm = new Enum(name, json.values, json.options, json.comment, json.comments);
|
|
813
|
+
var enm = new Enum(name, json.values, json.options, json.comment, json.comments, json.valuesOptions);
|
|
809
814
|
enm.reserved = json.reserved;
|
|
810
815
|
if (json.edition)
|
|
811
816
|
enm._edition = json.edition;
|
|
@@ -1184,10 +1189,12 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
|
|
|
1184
1189
|
/**
|
|
1185
1190
|
* Field descriptor.
|
|
1186
1191
|
* @interface IField
|
|
1192
|
+
* @property {string} [edition] Edition
|
|
1187
1193
|
* @property {string} [rule="optional"] Field rule
|
|
1188
1194
|
* @property {string} type Field type
|
|
1189
1195
|
* @property {number} id Field id
|
|
1190
1196
|
* @property {Object.<string,*>} [options] Field options
|
|
1197
|
+
* @property {string|null} [comment] Field comment
|
|
1191
1198
|
*/
|
|
1192
1199
|
|
|
1193
1200
|
/**
|
|
@@ -1670,12 +1677,9 @@ var util = require(34);
|
|
|
1670
1677
|
function Message(properties) {
|
|
1671
1678
|
// not used internally
|
|
1672
1679
|
if (properties)
|
|
1673
|
-
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
continue;
|
|
1677
|
-
this[key] = properties[key];
|
|
1678
|
-
}
|
|
1680
|
+
for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
|
|
1681
|
+
if (properties[keys[i]] != null && keys[i] !== "__proto__")
|
|
1682
|
+
this[keys[i]] = properties[keys[i]];
|
|
1679
1683
|
}
|
|
1680
1684
|
|
|
1681
1685
|
/**
|
|
@@ -1909,7 +1913,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
|
|
|
1909
1913
|
* @property {boolean} [requestStream=false] Whether requests are streamed
|
|
1910
1914
|
* @property {boolean} [responseStream=false] Whether responses are streamed
|
|
1911
1915
|
* @property {Object.<string,*>} [options] Method options
|
|
1912
|
-
* @property {string} comment Method
|
|
1916
|
+
* @property {string|null} [comment] Method comment
|
|
1913
1917
|
* @property {Array.<Object.<string,*>>} [parsedOptions] Method options properly parsed into objects
|
|
1914
1918
|
*/
|
|
1915
1919
|
|
|
@@ -2670,7 +2674,6 @@ Object.defineProperties(ReflectionObject.prototype, {
|
|
|
2670
2674
|
/**
|
|
2671
2675
|
* Converts this reflection object to its descriptor representation.
|
|
2672
2676
|
* @returns {Object.<string,*>} Descriptor
|
|
2673
|
-
* @abstract
|
|
2674
2677
|
*/
|
|
2675
2678
|
ReflectionObject.prototype.toJSON = /* istanbul ignore next */ function toJSON() {
|
|
2676
2679
|
throw Error(); // not implemented, shouldn't happen
|
|
@@ -2967,6 +2970,7 @@ function OneOf(name, fieldNames, options, comment) {
|
|
|
2967
2970
|
* @interface IOneOf
|
|
2968
2971
|
* @property {Array.<string>} oneof Oneof field names
|
|
2969
2972
|
* @property {Object.<string,*>} [options] Oneof options
|
|
2973
|
+
* @property {string|null} [comment] Oneof comment
|
|
2970
2974
|
*/
|
|
2971
2975
|
|
|
2972
2976
|
/**
|
|
@@ -4401,7 +4405,9 @@ function Service(name, options) {
|
|
|
4401
4405
|
* Service descriptor.
|
|
4402
4406
|
* @interface IService
|
|
4403
4407
|
* @extends INamespace
|
|
4408
|
+
* @property {string} [edition] Edition
|
|
4404
4409
|
* @property {Object.<string,IMethod>} methods Method descriptors
|
|
4410
|
+
* @property {string|null} [comment] Service comment
|
|
4405
4411
|
*/
|
|
4406
4412
|
|
|
4407
4413
|
/**
|
|
@@ -4786,11 +4792,13 @@ function clearCache(type) {
|
|
|
4786
4792
|
* Message type descriptor.
|
|
4787
4793
|
* @interface IType
|
|
4788
4794
|
* @extends INamespace
|
|
4795
|
+
* @property {string} [edition] Edition
|
|
4789
4796
|
* @property {Object.<string,IOneOf>} [oneofs] Oneof descriptors
|
|
4790
4797
|
* @property {Object.<string,IField>} fields Field descriptors
|
|
4791
4798
|
* @property {number[][]} [extensions] Extension ranges
|
|
4792
4799
|
* @property {Array.<number[]|string>} [reserved] Reserved ranges
|
|
4793
4800
|
* @property {boolean} [group=false] Whether a legacy group or not
|
|
4801
|
+
* @property {string|null} [comment] Message type comment
|
|
4794
4802
|
*/
|
|
4795
4803
|
|
|
4796
4804
|
/**
|