protons 7.7.0 → 8.0.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 +5 -5
- package/dist/bin/protons.js +1 -1
- package/dist/src/fields/array-field.d.ts +18 -0
- package/dist/src/fields/array-field.d.ts.map +1 -0
- package/dist/src/fields/array-field.js +83 -0
- package/dist/src/fields/array-field.js.map +1 -0
- package/dist/src/fields/enum-field.d.ts +9 -0
- package/dist/src/fields/enum-field.d.ts.map +1 -0
- package/dist/src/fields/enum-field.js +21 -0
- package/dist/src/fields/enum-field.js.map +1 -0
- package/dist/src/fields/field.d.ts +45 -0
- package/dist/src/fields/field.d.ts.map +1 -0
- package/dist/src/fields/field.js +147 -0
- package/dist/src/fields/field.js.map +1 -0
- package/dist/src/fields/map-field.d.ts +22 -0
- package/dist/src/fields/map-field.d.ts.map +1 -0
- package/dist/src/fields/map-field.js +83 -0
- package/dist/src/fields/map-field.js.map +1 -0
- package/dist/src/fields/message-field.d.ts +9 -0
- package/dist/src/fields/message-field.d.ts.map +1 -0
- package/dist/src/fields/message-field.js +23 -0
- package/dist/src/fields/message-field.js.map +1 -0
- package/dist/src/index.d.ts +190 -16
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +5 -963
- package/dist/src/index.js.map +1 -1
- package/dist/src/types/enum.d.ts +21 -0
- package/dist/src/types/enum.d.ts.map +1 -0
- package/dist/src/types/enum.js +87 -0
- package/dist/src/types/enum.js.map +1 -0
- package/dist/src/types/index.d.ts +20 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +2 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/message.d.ts +49 -0
- package/dist/src/types/message.d.ts.map +1 -0
- package/dist/src/types/message.js +478 -0
- package/dist/src/types/message.js.map +1 -0
- package/dist/src/types/module.d.ts +30 -0
- package/dist/src/types/module.d.ts.map +1 -0
- package/dist/src/types/module.js +184 -0
- package/dist/src/types/module.js.map +1 -0
- package/dist/src/types/primitive.d.ts +13 -0
- package/dist/src/types/primitive.d.ts.map +1 -0
- package/dist/src/types/primitive.js +174 -0
- package/dist/src/types/primitive.js.map +1 -0
- package/dist/typedoc-urls.json +2 -4
- package/package.json +102 -15
- package/src/fields/array-field.ts +109 -0
- package/src/fields/enum-field.ts +30 -0
- package/src/fields/field.ts +201 -0
- package/src/fields/map-field.ts +107 -0
- package/src/fields/message-field.ts +29 -0
- package/src/index.ts +6 -1202
- package/src/types/enum.ts +112 -0
- package/src/types/index.ts +21 -0
- package/src/types/message.ts +558 -0
- package/src/types/module.ts +234 -0
- package/src/types/primitive.ts +215 -0
- package/LICENSE +0 -4
package/dist/src/index.d.ts
CHANGED
|
@@ -1,21 +1,196 @@
|
|
|
1
|
-
export declare enum CODEC_TYPES {
|
|
2
|
-
VARINT = 0,
|
|
3
|
-
BIT64 = 1,
|
|
4
|
-
LENGTH_DELIMITED = 2,
|
|
5
|
-
START_GROUP = 3,
|
|
6
|
-
END_GROUP = 4,
|
|
7
|
-
BIT32 = 5
|
|
8
|
-
}
|
|
9
1
|
/**
|
|
10
|
-
*
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `protons` is a high performance implementation of [Protocol Buffers v3](https://protobuf.dev/programming-guides/proto3/).
|
|
5
|
+
*
|
|
6
|
+
* It transpiles code to TypeScript and supports BigInts for 64 bit types.
|
|
7
|
+
*
|
|
8
|
+
* The `protons` module contains the code to compile `.proto` files to `.ts` files and `protons-runtime` contains the code to do serialization/deserialization to `Uint8Array`s during application execution.
|
|
9
|
+
*
|
|
10
|
+
* Please ensure you declare them as the correct type of dependencies:
|
|
11
|
+
*
|
|
12
|
+
* ```console
|
|
13
|
+
* $ npm install --save-dev protons
|
|
14
|
+
* $ npm install --save protons-runtime
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ## Usage
|
|
18
|
+
*
|
|
19
|
+
* First generate your `.ts` files:
|
|
20
|
+
*
|
|
21
|
+
* ```console
|
|
22
|
+
* $ protons ./path/to/foo.proto ./path/to/output.ts
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Then run tsc over them as normal:
|
|
26
|
+
*
|
|
27
|
+
* ```console
|
|
28
|
+
* $ tsc
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* In your code import the generated classes and use them to transform to/from bytes:
|
|
32
|
+
*
|
|
33
|
+
* ```js
|
|
34
|
+
* import { Foo } from './foo.ts'
|
|
35
|
+
*
|
|
36
|
+
* const foo = {
|
|
37
|
+
* message: 'hello world'
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* const encoded = Foo.encode(foo)
|
|
41
|
+
* const decoded = Foo.decode(encoded)
|
|
42
|
+
*
|
|
43
|
+
* console.info(decoded.message)
|
|
44
|
+
* // 'hello world'
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* ## Differences from protobuf.js
|
|
48
|
+
*
|
|
49
|
+
* This module uses the internal reader/writer from `protobuf.js` as it is highly optimized and there's no point reinventing the wheel.
|
|
50
|
+
*
|
|
51
|
+
* It does have one or two differences:
|
|
52
|
+
*
|
|
53
|
+
* 1. Supports `proto3` semantics only
|
|
54
|
+
* 2. All 64 bit values are represented as `BigInt`s and not `Long`s (e.g. `int64`, `uint64`, `sint64` etc)
|
|
55
|
+
* 3. Unset `optional` fields are set on the deserialized object forms as `undefined` instead of the default values
|
|
56
|
+
* 4. `singular` fields set to default values are not serialized and are set to default values when deserialized if not set - protobuf.js [diverges from the language guide](https://github.com/protobufjs/protobuf.js/issues/1468#issuecomment-745177012) around this feature
|
|
57
|
+
* 5. `map` fields can have keys of any type - protobufs.js [only supports strings](https://github.com/protobufjs/protobuf.js/issues/1203#issuecomment-488637338)
|
|
58
|
+
* 6. `map` fields are deserialized as ES6 `Map`s - protobuf.js uses `Object`s
|
|
59
|
+
*
|
|
60
|
+
* ## Extra features
|
|
61
|
+
*
|
|
62
|
+
* ### Limiting the size of repeated/map elements
|
|
63
|
+
*
|
|
64
|
+
* To protect decoders from malicious payloads, it's possible to limit the maximum size of repeated/map elements.
|
|
65
|
+
*
|
|
66
|
+
* You can either do this at compile time by using the [protons.options](https://github.com/protocolbuffers/protobuf/blob/6f1d88107f268b8ebdad6690d116e74c403e366e/docs/options.md?plain=1#L490-L493) extension:
|
|
67
|
+
*
|
|
68
|
+
* ```
|
|
69
|
+
* message MyMessage {
|
|
70
|
+
* // repeatedField cannot have more than 10 entries
|
|
71
|
+
* repeated uint32 repeatedField = 1 [(protons.options).limit = 10];
|
|
72
|
+
*
|
|
73
|
+
* // stringMap cannot have more than 10 keys
|
|
74
|
+
* map<string, string> stringMap = 2 [(protons.options).limit = 10];
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* Or at runtime by passing objects to the `.decode` function of your message:
|
|
11
79
|
*
|
|
12
|
-
*
|
|
80
|
+
* ```TypeScript
|
|
81
|
+
* const message = MyMessage.decode(buf, {
|
|
82
|
+
* limits: {
|
|
83
|
+
* repeatedField: 10,
|
|
84
|
+
* stringMap: 10
|
|
85
|
+
* }
|
|
86
|
+
* })
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* #### Limiting repeating fields of nested messages at runtime
|
|
90
|
+
*
|
|
91
|
+
* Sub messages with repeating elements can be limited in a similar way:
|
|
92
|
+
*
|
|
93
|
+
* ```
|
|
94
|
+
* message SubMessage {
|
|
95
|
+
* repeated uint32 repeatedField = 1;
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* message MyMessage {
|
|
99
|
+
* SubMessage message = 1;
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* ```TypeScript
|
|
104
|
+
* const message = MyMessage.decode(buf, {
|
|
105
|
+
* limits: {
|
|
106
|
+
* messages: {
|
|
107
|
+
* repeatedField: 5 // the SubMessage can not have more than 5 repeatedField entries
|
|
108
|
+
* }
|
|
109
|
+
* }
|
|
110
|
+
* })
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* #### Limiting repeating fields of repeating messages at runtime
|
|
114
|
+
*
|
|
115
|
+
* Sub messages defined in repeating elements can be limited by appending `$` to the field name in the runtime limit options:
|
|
116
|
+
*
|
|
117
|
+
* ```
|
|
118
|
+
* message SubMessage {
|
|
119
|
+
* repeated uint32 repeatedField = 1;
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* message MyMessage {
|
|
123
|
+
* repeated SubMessage messages = 1;
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* ```TypeScript
|
|
128
|
+
* const message = MyMessage.decode(buf, {
|
|
129
|
+
* limits: {
|
|
130
|
+
* messages: 5 // max 5x SubMessages
|
|
131
|
+
* messages$: {
|
|
132
|
+
* repeatedField: 5 // no SubMessage can have more than 5 repeatedField entries
|
|
133
|
+
* }
|
|
134
|
+
* }
|
|
135
|
+
* })
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* #### Limiting repeating fields of map entries at runtime
|
|
139
|
+
*
|
|
140
|
+
* Repeating fields in map entries can be limited by appending `$value` to the field name in the runtime limit options:
|
|
141
|
+
*
|
|
142
|
+
* ```
|
|
143
|
+
* message SubMessage {
|
|
144
|
+
* repeated uint32 repeatedField = 1;
|
|
145
|
+
* }
|
|
146
|
+
*
|
|
147
|
+
* message MyMessage {
|
|
148
|
+
* map<string, SubMessage> messages = 1;
|
|
149
|
+
* }
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* ```TypeScript
|
|
153
|
+
* const message = MyMessage.decode(buf, {
|
|
154
|
+
* limits: {
|
|
155
|
+
* messages: 5 // max 5x SubMessages in the map
|
|
156
|
+
* messages$value: {
|
|
157
|
+
* repeatedField: 5 // no SubMessage in the map can have more than 5 repeatedField entries
|
|
158
|
+
* }
|
|
159
|
+
* }
|
|
160
|
+
* })
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* ### Overriding 64 bit types
|
|
164
|
+
*
|
|
165
|
+
* By default 64 bit types are implemented as [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)s.
|
|
166
|
+
*
|
|
167
|
+
* Sometimes this is undesirable due to [performance issues](https://betterprogramming.pub/the-downsides-of-bigints-in-javascript-6350fd807d) or code legibility.
|
|
168
|
+
*
|
|
169
|
+
* It's possible to override the JavaScript type 64 bit fields will deserialize to:
|
|
170
|
+
*
|
|
171
|
+
* ```
|
|
172
|
+
* message MyMessage {
|
|
173
|
+
* repeated int64 bigintField = 1;
|
|
174
|
+
* repeated int64 numberField = 2 [jstype = JS_NUMBER];
|
|
175
|
+
* repeated int64 stringField = 3 [jstype = JS_STRING];
|
|
176
|
+
* }
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* ```TypeScript
|
|
180
|
+
* const message = MyMessage.decode(buf)
|
|
181
|
+
*
|
|
182
|
+
* console.info(typeof message.bigintField) // bigint
|
|
183
|
+
* console.info(typeof message.numberField) // number
|
|
184
|
+
* console.info(typeof message.stringField) // string
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* ## Missing features
|
|
188
|
+
*
|
|
189
|
+
* Some features may be missing due to them not being needed in ipfs/libp2p so far.
|
|
190
|
+
*
|
|
191
|
+
* If these features are important to you, please open PRs implementing them along with tests comparing the generated bytes to `protobuf.js` and `pbjs`.
|
|
13
192
|
*/
|
|
14
|
-
export
|
|
15
|
-
code: string;
|
|
16
|
-
constructor(message: string, code: string, options?: ErrorOptions);
|
|
17
|
-
}
|
|
18
|
-
interface Flags {
|
|
193
|
+
export interface Flags {
|
|
19
194
|
/**
|
|
20
195
|
* Specifies an output directory
|
|
21
196
|
*/
|
|
@@ -30,5 +205,4 @@ interface Flags {
|
|
|
30
205
|
path?: string[];
|
|
31
206
|
}
|
|
32
207
|
export declare function generate(source: string, flags: Flags): Promise<void>;
|
|
33
|
-
export {};
|
|
34
208
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+LG;AAaH,MAAM,WAAW,KAAK;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAEhB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;CAChB;AAED,wBAAsB,QAAQ,CAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAkB3E"}
|