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/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# protons
|
|
1
|
+
# protons
|
|
2
2
|
|
|
3
3
|
[](https://ipfs.tech)
|
|
4
4
|
[](https://discuss.ipfs.tech)
|
|
@@ -54,7 +54,7 @@ $ tsc
|
|
|
54
54
|
In your code import the generated classes and use them to transform to/from bytes:
|
|
55
55
|
|
|
56
56
|
```js
|
|
57
|
-
import { Foo } from './foo.
|
|
57
|
+
import { Foo } from './foo.ts'
|
|
58
58
|
|
|
59
59
|
const foo = {
|
|
60
60
|
message: 'hello world'
|
|
@@ -69,7 +69,7 @@ console.info(decoded.message)
|
|
|
69
69
|
|
|
70
70
|
## Differences from protobuf.js
|
|
71
71
|
|
|
72
|
-
This module uses the internal reader/writer from `protobuf.js` as it is highly
|
|
72
|
+
This module uses the internal reader/writer from `protobuf.js` as it is highly optimized and there's no point reinventing the wheel.
|
|
73
73
|
|
|
74
74
|
It does have one or two differences:
|
|
75
75
|
|
|
@@ -227,8 +227,8 @@ $ npm i protons
|
|
|
227
227
|
|
|
228
228
|
Licensed under either of
|
|
229
229
|
|
|
230
|
-
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
231
|
-
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
230
|
+
- Apache 2.0, ([LICENSE-APACHE](https://github.com/ipfs/protons/blob/main/packages/protons/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
231
|
+
- MIT ([LICENSE-MIT](https://github.com/ipfs/protons/blob/main/packages/protons/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
232
232
|
|
|
233
233
|
# Contribute
|
|
234
234
|
|
package/dist/bin/protons.js
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Field } from './field.ts';
|
|
2
|
+
import type { FieldDef } from './field.ts';
|
|
3
|
+
import type { Parent } from '../types/index.ts';
|
|
4
|
+
export interface ArrayFieldDef extends FieldDef {
|
|
5
|
+
rule: 'repeated';
|
|
6
|
+
}
|
|
7
|
+
export declare function isArrayFieldDef(obj?: any): obj is ArrayFieldDef;
|
|
8
|
+
export declare class ArrayField extends Field {
|
|
9
|
+
private lengthLimit?;
|
|
10
|
+
constructor(name: string, def: ArrayFieldDef, parent: Parent);
|
|
11
|
+
getInterfaceField(parent: Parent, indent?: string): string;
|
|
12
|
+
getDefaultField(parent: Parent): string;
|
|
13
|
+
getEncoder(parent: Parent): string;
|
|
14
|
+
getDecoder(parent: Parent): string;
|
|
15
|
+
getStreamingDecoder(parent: Parent): string;
|
|
16
|
+
getLimitField(): string;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=array-field.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array-field.d.ts","sourceRoot":"","sources":["../../../src/fields/array-field.ts"],"names":[],"mappings":"AAEA,OAAO,EAAc,KAAK,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAQ,MAAM,mBAAmB,CAAA;AAErD,MAAM,WAAW,aAAc,SAAQ,QAAQ;IAC7C,IAAI,EAAE,UAAU,CAAA;CACjB;AAED,wBAAgB,eAAe,CAAE,GAAG,CAAC,EAAE,GAAG,GAAG,GAAG,IAAI,aAAa,CAEhE;AAED,qBAAa,UAAW,SAAQ,KAAK;IACnC,OAAO,CAAC,WAAW,CAAC,CAAQ;gBAEf,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM;IAM7D,iBAAiB,CAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAK,GAAG,MAAM;IAIvD,eAAe,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAIxC,UAAU,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAqBnC,UAAU,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAyBnC,mBAAmB,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IA4B5C,aAAa,IAAK,MAAM;CAGzB"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Enum } from "../types/enum.js";
|
|
2
|
+
import { Message } from "../types/message.js";
|
|
3
|
+
import { codecTypes, Field } from "./field.js";
|
|
4
|
+
export function isArrayFieldDef(obj) {
|
|
5
|
+
return obj?.rule === 'repeated';
|
|
6
|
+
}
|
|
7
|
+
export class ArrayField extends Field {
|
|
8
|
+
lengthLimit;
|
|
9
|
+
constructor(name, def, parent) {
|
|
10
|
+
super(name, def, parent);
|
|
11
|
+
this.lengthLimit = def.options?.['(protons.options).limit'];
|
|
12
|
+
}
|
|
13
|
+
getInterfaceField(parent, indent = '') {
|
|
14
|
+
return `${super.getInterfaceField(parent, indent)}[]`;
|
|
15
|
+
}
|
|
16
|
+
getDefaultField(parent) {
|
|
17
|
+
return `${this.name}: []`;
|
|
18
|
+
}
|
|
19
|
+
getEncoder(parent) {
|
|
20
|
+
const type = parent.findType(this.type);
|
|
21
|
+
let id = (this.id << 3) | codecTypes[this.type];
|
|
22
|
+
if (type instanceof Message) {
|
|
23
|
+
id = (this.id << 3) | codecTypes.message;
|
|
24
|
+
}
|
|
25
|
+
if (type instanceof Enum) {
|
|
26
|
+
id = (this.id << 3) | codecTypes.enum;
|
|
27
|
+
}
|
|
28
|
+
return `
|
|
29
|
+
if (obj.${this.name} != null) {
|
|
30
|
+
for (const value of obj.${this.name}) {
|
|
31
|
+
w.uint32(${id})
|
|
32
|
+
${type.getEncoder(this, 'value')}
|
|
33
|
+
}
|
|
34
|
+
}`;
|
|
35
|
+
}
|
|
36
|
+
getDecoder(parent) {
|
|
37
|
+
parent.addImport('protons-runtime', 'MaxLengthError');
|
|
38
|
+
let limit = `
|
|
39
|
+
if (opts.limits?.${this.name} != null && obj.${this.name}.length === opts.limits.${this.name}) {
|
|
40
|
+
throw new MaxLengthError('Decode error - repeated field "${this.name}" had too many elements')
|
|
41
|
+
}
|
|
42
|
+
`;
|
|
43
|
+
if (this.lengthLimit != null) {
|
|
44
|
+
limit += `
|
|
45
|
+
if (obj.${this.name}.length === ${this.lengthLimit}) {
|
|
46
|
+
throw new MaxLengthError('Decode error - repeated field "${this.name}" had too many elements')
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
const type = parent.findType(this.type);
|
|
51
|
+
return `case ${this.id}: {${limit}
|
|
52
|
+
obj.${this.name}.push(${type.getDecoder(this)})
|
|
53
|
+
break
|
|
54
|
+
}`;
|
|
55
|
+
}
|
|
56
|
+
getStreamingDecoder(parent) {
|
|
57
|
+
parent.addImport('protons-runtime', 'MaxLengthError');
|
|
58
|
+
let limit = `
|
|
59
|
+
if (opts.limits?.${this.name} != null && obj.${this.name} === opts.limits.${this.name}) {
|
|
60
|
+
throw new MaxLengthError('Streaming decode error - repeated field "${this.name}" had too many elements')
|
|
61
|
+
}
|
|
62
|
+
`;
|
|
63
|
+
if (this.lengthLimit != null) {
|
|
64
|
+
limit += `
|
|
65
|
+
if (obj.${this.name} === ${this.lengthLimit}) {
|
|
66
|
+
throw new MaxLengthError('Streaming decode error - repeated field "${this.name}" had too many elements')
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
}
|
|
70
|
+
const type = parent.findType(this.type);
|
|
71
|
+
return `case ${this.id}: {${limit}
|
|
72
|
+
${type.getStreamingDecoder(this, `\`\${prefix != null ? \`\${prefix}\` : '$'}.${this.name}[]\``, ' ')}
|
|
73
|
+
|
|
74
|
+
obj.${this.name}++
|
|
75
|
+
|
|
76
|
+
break
|
|
77
|
+
}`;
|
|
78
|
+
}
|
|
79
|
+
getLimitField() {
|
|
80
|
+
return `${this.name}: ${this.lengthLimit ?? 0}`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=array-field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array-field.js","sourceRoot":"","sources":["../../../src/fields/array-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAQ9C,MAAM,UAAU,eAAe,CAAE,GAAS;IACxC,OAAO,GAAG,EAAE,IAAI,KAAK,UAAU,CAAA;AACjC,CAAC;AAED,MAAM,OAAO,UAAW,SAAQ,KAAK;IAC3B,WAAW,CAAS;IAE5B,YAAa,IAAY,EAAE,GAAkB,EAAE,MAAc;QAC3D,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,CAAA;QAExB,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC,OAAO,EAAE,CAAC,yBAAyB,CAAC,CAAA;IAC7D,CAAC;IAED,iBAAiB,CAAE,MAAc,EAAE,MAAM,GAAG,EAAE;QAC5C,OAAO,GAAG,KAAK,CAAC,iBAAiB,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAA;IACvD,CAAC;IAED,eAAe,CAAE,MAAc;QAC7B,OAAO,GAAG,IAAI,CAAC,IAAI,MAAM,CAAA;IA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AI,CAAC,UAAU,CAAC,IAAI,CAAC;;cAE7C,CAAA;IACZ,CAAC;IAED,mBAAmB,CAAE,MAAc;QACjC,MAAM,CAAC,SAAS,CAAC,iBAAiB,EAAE,gBAAgB,CAAC,CAAA;QAErD,IAAI,KAAK,GAAG;iCACiB,IAAI,CAAC,IAAI,mBAAmB,IAAI,CAAC,IAAI,oBAAoB,IAAI,CAAC,IAAI;qFACd,IAAI,CAAC,IAAI;;CAE7F,CAAA;QAEG,IAAI,IAAI,CAAC,WAAW,IAAI,IAAI,EAAE,CAAC;YAC7B,KAAK,IAAI;wBACS,IAAI,CAAC,IAAI,QAAQ,IAAI,CAAC,WAAW;qFAC4B,IAAI,CAAC,IAAI;;CAE7F,CAAA;QACG,CAAC;QAED,MAAM,IAAI,GAAS,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAE7C,OAAO,QAAQ,IAAI,CAAC,EAAE,MAAM,KAAK;gBACrB,IAAI,CAAC,mBAAmB,CAAC,IAAI,EAAE,+CAA+C,IAAI,CAAC,IAAI,MAAM,EAAE,IAAI,CAAC;;oBAEhG,IAAI,CAAC,IAAI;;;cAGf,CAAA;IACZ,CAAC;IAED,aAAa;QACX,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,WAAW,IAAI,CAAC,EAAE,CAAA;IACjD,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Field } from './field.ts';
|
|
2
|
+
import type { FieldDef } from './field.ts';
|
|
3
|
+
import type { Parent } from '../types/index.ts';
|
|
4
|
+
export declare class EnumField extends Field {
|
|
5
|
+
private enum;
|
|
6
|
+
constructor(name: string, def: FieldDef, parent: Parent);
|
|
7
|
+
getDefaultField(parent: Parent): string;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=enum-field.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enum-field.d.ts","sourceRoot":"","sources":["../../../src/fields/enum-field.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE/C,qBAAa,SAAU,SAAQ,KAAK;IAClC,OAAO,CAAC,IAAI,CAAM;gBAEL,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM;IAYxD,eAAe,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;CASzC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Enum } from "../types/enum.js";
|
|
2
|
+
import { Field } from "./field.js";
|
|
3
|
+
export class EnumField extends Field {
|
|
4
|
+
enum;
|
|
5
|
+
constructor(name, def, parent) {
|
|
6
|
+
super(name, def, parent);
|
|
7
|
+
const type = parent.findType(def.type);
|
|
8
|
+
if (!(type instanceof Enum)) {
|
|
9
|
+
throw new Error(`Type "${def.type}" was not an Enum`);
|
|
10
|
+
}
|
|
11
|
+
this.enum = type;
|
|
12
|
+
}
|
|
13
|
+
getDefaultField(parent) {
|
|
14
|
+
if (this.optional) {
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
const type = parent.findType(this.type);
|
|
18
|
+
return `${this.name}: ${type.pbType}.${this.enum.lowestValueName}`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=enum-field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enum-field.js","sourceRoot":"","sources":["../../../src/fields/enum-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAIlC,MAAM,OAAO,SAAU,SAAQ,KAAK;IAC1B,IAAI,CAAM;IAElB,YAAa,IAAY,EAAE,GAAa,EAAE,MAAc;QACtD,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,CAAA;QAExB,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QAEtC,IAAI,CAAC,CAAC,IAAI,YAAY,IAAI,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,SAAS,GAAG,CAAC,IAAI,mBAAmB,CAAC,CAAA;QACvD,CAAC;QAED,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAED,eAAe,CAAE,MAAc;QAC7B,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,OAAO,EAAE,CAAA;QACX,CAAC;QAED,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAEvC,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAA;IACpE,CAAC;CACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Parent } from '../types/index.ts';
|
|
2
|
+
export declare const CODEC_TYPES: {
|
|
3
|
+
VARINT: number;
|
|
4
|
+
BIT64: number;
|
|
5
|
+
LENGTH_DELIMITED: number;
|
|
6
|
+
START_GROUP: number;
|
|
7
|
+
END_GROUP: number;
|
|
8
|
+
BIT32: number;
|
|
9
|
+
};
|
|
10
|
+
export declare const codecTypes: Record<string, number>;
|
|
11
|
+
export interface FieldOptions extends Record<string, any> {
|
|
12
|
+
proto3_optional?: boolean;
|
|
13
|
+
jstype?: 'string' | 'number';
|
|
14
|
+
}
|
|
15
|
+
export interface FieldDef {
|
|
16
|
+
id: number;
|
|
17
|
+
type: string;
|
|
18
|
+
repeated?: boolean;
|
|
19
|
+
options?: FieldOptions;
|
|
20
|
+
rule?: string;
|
|
21
|
+
oneof?: string[];
|
|
22
|
+
}
|
|
23
|
+
export interface MessageField {
|
|
24
|
+
/**
|
|
25
|
+
* Return a string that can be used in a typescript interface for this field
|
|
26
|
+
*/
|
|
27
|
+
getInterfaceField(parent: Parent, indent?: string): string;
|
|
28
|
+
}
|
|
29
|
+
export declare class Field implements MessageField {
|
|
30
|
+
id: number;
|
|
31
|
+
name: string;
|
|
32
|
+
optional: boolean;
|
|
33
|
+
type: string;
|
|
34
|
+
proto2Required: boolean;
|
|
35
|
+
jsTypeOverride?: 'string' | 'number';
|
|
36
|
+
oneof?: string[];
|
|
37
|
+
constructor(name: string, def: FieldDef, parent: Parent);
|
|
38
|
+
getInterfaceField(parent: Parent, indent?: string): string;
|
|
39
|
+
getDefaultField(parent: Parent): string;
|
|
40
|
+
getDecoder(parent: Parent): string;
|
|
41
|
+
getStreamingDecoder(parent: Parent): string;
|
|
42
|
+
getEncoder(parent: Parent): string;
|
|
43
|
+
getLimitField(): string;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=field.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"field.d.ts","sourceRoot":"","sources":["../../../src/fields/field.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAQ,MAAM,mBAAmB,CAAA;AAErD,eAAO,MAAM,WAAW;;;;;;;CAOvB,CAAA;AAED,eAAO,MAAM,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAkB7C,CAAA;AAkCD,MAAM,WAAW,YAAa,SAAQ,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IACvD,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,MAAM,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAA;CAC7B;AAED,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,OAAO,CAAC,EAAE,YAAY,CAAA;IACtB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,iBAAiB,CAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAC5D;AAED,qBAAa,KAAM,YAAW,YAAY;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,cAAc,EAAE,OAAO,CAAA;IACvB,cAAc,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAA;IACpC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;gBAEV,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM;IAiCxD,iBAAiB,CAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAK,GAAG,MAAM;IAIvD,eAAe,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAwBxC,UAAU,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IASnC,mBAAmB,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAY5C,UAAU,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAmBnC,aAAa,IAAK,MAAM;CAGzB"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { ParseError } from 'protons-runtime';
|
|
2
|
+
import { Enum } from "../types/enum.js";
|
|
3
|
+
import { Message } from "../types/message.js";
|
|
4
|
+
export const CODEC_TYPES = {
|
|
5
|
+
VARINT: 0,
|
|
6
|
+
BIT64: 1,
|
|
7
|
+
LENGTH_DELIMITED: 2,
|
|
8
|
+
START_GROUP: 3,
|
|
9
|
+
END_GROUP: 4,
|
|
10
|
+
BIT32: 5
|
|
11
|
+
};
|
|
12
|
+
export const codecTypes = {
|
|
13
|
+
bool: CODEC_TYPES.VARINT,
|
|
14
|
+
bytes: CODEC_TYPES.LENGTH_DELIMITED,
|
|
15
|
+
double: CODEC_TYPES.BIT64,
|
|
16
|
+
enum: CODEC_TYPES.VARINT,
|
|
17
|
+
fixed32: CODEC_TYPES.BIT32,
|
|
18
|
+
fixed64: CODEC_TYPES.BIT64,
|
|
19
|
+
float: CODEC_TYPES.BIT32,
|
|
20
|
+
int32: CODEC_TYPES.VARINT,
|
|
21
|
+
int64: CODEC_TYPES.VARINT,
|
|
22
|
+
message: CODEC_TYPES.LENGTH_DELIMITED,
|
|
23
|
+
sfixed32: CODEC_TYPES.BIT32,
|
|
24
|
+
sfixed64: CODEC_TYPES.BIT64,
|
|
25
|
+
sint32: CODEC_TYPES.VARINT,
|
|
26
|
+
sint64: CODEC_TYPES.VARINT,
|
|
27
|
+
string: CODEC_TYPES.LENGTH_DELIMITED,
|
|
28
|
+
uint32: CODEC_TYPES.VARINT,
|
|
29
|
+
uint64: CODEC_TYPES.VARINT
|
|
30
|
+
};
|
|
31
|
+
const jsTypeOverrides = {
|
|
32
|
+
JS_NUMBER: 'number',
|
|
33
|
+
JS_STRING: 'string'
|
|
34
|
+
};
|
|
35
|
+
const defaultValueGenerators = {
|
|
36
|
+
bool: () => 'false',
|
|
37
|
+
bytes: () => 'uint8ArrayAlloc(0)',
|
|
38
|
+
double: () => '0',
|
|
39
|
+
fixed32: () => '0',
|
|
40
|
+
fixed64: () => '0n',
|
|
41
|
+
float: () => '0',
|
|
42
|
+
int32: () => '0',
|
|
43
|
+
int64: () => '0n',
|
|
44
|
+
sfixed32: () => '0',
|
|
45
|
+
sfixed64: () => '0n',
|
|
46
|
+
sint32: () => '0',
|
|
47
|
+
sint64: () => '0n',
|
|
48
|
+
string: () => "''",
|
|
49
|
+
uint32: () => '0',
|
|
50
|
+
uint64: () => '0n'
|
|
51
|
+
};
|
|
52
|
+
const defaultValueGeneratorsJsTypeOverrides = {
|
|
53
|
+
number: () => '0',
|
|
54
|
+
string: () => "''"
|
|
55
|
+
};
|
|
56
|
+
export class Field {
|
|
57
|
+
id;
|
|
58
|
+
name;
|
|
59
|
+
optional;
|
|
60
|
+
type;
|
|
61
|
+
proto2Required;
|
|
62
|
+
jsTypeOverride;
|
|
63
|
+
oneof;
|
|
64
|
+
constructor(name, def, parent) {
|
|
65
|
+
this.id = def.id;
|
|
66
|
+
this.name = name;
|
|
67
|
+
// the default type for a message is unset so they are always optional
|
|
68
|
+
// https://developers.google.com/protocol-buffers/docs/proto3#default
|
|
69
|
+
this.optional = def.repeated !== true && def.options?.proto3_optional === true;
|
|
70
|
+
this.type = def.type ?? 'string';
|
|
71
|
+
this.oneof = def.oneof;
|
|
72
|
+
this.proto2Required = false;
|
|
73
|
+
if (def.options?.jstype != null) {
|
|
74
|
+
this.jsTypeOverride = jsTypeOverrides[def.options.jstype];
|
|
75
|
+
}
|
|
76
|
+
if (this.jsTypeOverride != null && !['int64', 'uint64', 'sint64', 'fixed64', 'sfixed64'].includes(this.type)) {
|
|
77
|
+
throw new Error(`jstype override option is only allowed on int64, uint64, sint64, fixed64 or sfixed64 fields - got "${this.type}"`);
|
|
78
|
+
}
|
|
79
|
+
if (def.rule === 'required') {
|
|
80
|
+
const message = `field "${name}" is required, this is not allowed in proto3. Please convert your proto2 definitions to proto3 - see https://github.com/ipfs/protons/wiki/Required-fields-and-protobuf-3`;
|
|
81
|
+
if (parent.flags?.strict === true) {
|
|
82
|
+
throw new ParseError(message);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.proto2Required = true;
|
|
86
|
+
// eslint-disable-next-line no-console
|
|
87
|
+
console.info(`[WARN] ${message}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
getInterfaceField(parent, indent = '') {
|
|
92
|
+
return `${indent}${this.name}${this.optional ? '?' : ''}: ${this.jsTypeOverride ?? parent.findType(this.type).jsType}`;
|
|
93
|
+
}
|
|
94
|
+
getDefaultField(parent) {
|
|
95
|
+
if (this.optional) {
|
|
96
|
+
return '';
|
|
97
|
+
}
|
|
98
|
+
const type = parent.findType(this.type);
|
|
99
|
+
if (type.pbType === 'bytes') {
|
|
100
|
+
parent.addImport('uint8arrays/alloc', 'alloc', 'uint8ArrayAlloc');
|
|
101
|
+
}
|
|
102
|
+
let defaultValueGenerator = defaultValueGenerators[type.pbType];
|
|
103
|
+
if (this.jsTypeOverride) {
|
|
104
|
+
defaultValueGenerator = defaultValueGeneratorsJsTypeOverrides[this.jsTypeOverride];
|
|
105
|
+
}
|
|
106
|
+
if (defaultValueGenerator == null) {
|
|
107
|
+
return '';
|
|
108
|
+
}
|
|
109
|
+
return `${this.name}: ${defaultValueGenerator()}`;
|
|
110
|
+
}
|
|
111
|
+
getDecoder(parent) {
|
|
112
|
+
const type = parent.findType(this.type);
|
|
113
|
+
return `case ${this.id}: {
|
|
114
|
+
obj.${this.name} = ${type.getDecoder(this)}
|
|
115
|
+
break
|
|
116
|
+
}`;
|
|
117
|
+
}
|
|
118
|
+
getStreamingDecoder(parent) {
|
|
119
|
+
const type = parent.findType(this.type);
|
|
120
|
+
return `case ${this.id}: {
|
|
121
|
+
yield {
|
|
122
|
+
field: \`\${prefix != null ? \`\${prefix}\` : '$'}.${this.name}\`,
|
|
123
|
+
value: ${type.getDecoder(this, ' ')}
|
|
124
|
+
}
|
|
125
|
+
break
|
|
126
|
+
}`;
|
|
127
|
+
}
|
|
128
|
+
getEncoder(parent) {
|
|
129
|
+
const type = parent.findType(this.type);
|
|
130
|
+
let id = (this.id << 3) | codecTypes[this.type];
|
|
131
|
+
if (type instanceof Message) {
|
|
132
|
+
id = (this.id << 3) | codecTypes.message;
|
|
133
|
+
}
|
|
134
|
+
if (type instanceof Enum) {
|
|
135
|
+
id = (this.id << 3) | codecTypes.enum;
|
|
136
|
+
}
|
|
137
|
+
return `
|
|
138
|
+
if (${type.getValueTest(this, `obj.${this.name}`)}) {
|
|
139
|
+
w.uint32(${id})
|
|
140
|
+
${type.getEncoder(this, `obj.${this.name}`)}
|
|
141
|
+
}`;
|
|
142
|
+
}
|
|
143
|
+
getLimitField() {
|
|
144
|
+
return '';
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"field.js","sourceRoot":"","sources":["../../../src/fields/field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAG7C,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,MAAM,EAAE,CAAC;IACT,KAAK,EAAE,CAAC;IACR,gBAAgB,EAAE,CAAC;IACnB,WAAW,EAAE,CAAC;IACd,SAAS,EAAE,CAAC;IACZ,KAAK,EAAE,CAAC;CACT,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAA2B;IAChD,IAAI,EAAE,WAAW,CAAC,MAAM;IACxB,KAAK,EAAE,WAAW,CAAC,gBAAgB;IACnC,MAAM,EAAE,WAAW,CAAC,KAAK;IACzB,IAAI,EAAE,WAAW,CAAC,MAAM;IACxB,OAAO,EAAE,WAAW,CAAC,KAAK;IAC1B,OAAO,EAAE,WAAW,CAAC,KAAK;IAC1B,KAAK,EAAE,WAAW,CAAC,KAAK;IACxB,KAAK,EAAE,WAAW,CAAC,MAAM;IACzB,KAAK,EAAE,WAAW,CAAC,MAAM;IACzB,OAAO,EAAE,WAAW,CAAC,gBAAgB;IACrC,QAAQ,EAAE,WAAW,CAAC,KAAK;IAC3B,QAAQ,EAAE,WAAW,CAAC,KAAK;IAC3B,MAAM,EAAE,WAAW,CAAC,MAAM;IAC1B,MAAM,EAAE,WAAW,CAAC,MAAM;IAC1B,MAAM,EAAE,WAAW,CAAC,gBAAgB;IACpC,MAAM,EAAE,WAAW,CAAC,MAAM;IAC1B,MAAM,EAAE,WAAW,CAAC,MAAM;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IAAI;yBACrD,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC;;;cAGtC,CAAA;IACZ,CAAC;IAED,UAAU,CAAE,MAAc;QACxB,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACvC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAE/C,IAAI,IAAI,YAAY,OAAO,EAAE,CAAC;YAC5B,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,UAAU,CAAC,OAAO,CAAA;QAC1C,CAAC;QAED,IAAI,IAAI,YAAY,IAAI,EAAE,CAAC;YACzB,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,CAAA;QACvC,CAAC;QAED,OAAO;cACG,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC;qBACpC,EAAE;YACX,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC;UAC3C,CAAA;IACR,CAAC;IAED,aAAa;QACX,OAAO,EAAE,CAAA;IACX,CAAC;CACF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Field } from './field.ts';
|
|
2
|
+
import type { FieldDef } from './field.ts';
|
|
3
|
+
import type { Parent } from '../types/index.ts';
|
|
4
|
+
export interface MapFieldDef extends FieldDef {
|
|
5
|
+
keyType?: string;
|
|
6
|
+
valueType: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function isMapFieldDef(obj?: any): obj is MapFieldDef;
|
|
9
|
+
export declare class MapField extends Field {
|
|
10
|
+
keyType: string;
|
|
11
|
+
valueType: string;
|
|
12
|
+
entryType: string;
|
|
13
|
+
private lengthLimit?;
|
|
14
|
+
constructor(name: string, def: MapFieldDef, parent: Parent);
|
|
15
|
+
getInterfaceField(parent: Parent): string;
|
|
16
|
+
getDefaultField(parent: Parent): string;
|
|
17
|
+
getEncoder(parent: Parent): string;
|
|
18
|
+
getDecoder(parent: Parent): string;
|
|
19
|
+
getStreamingDecoder(parent: Parent): string;
|
|
20
|
+
getLimitField(): string;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=map-field.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-field.d.ts","sourceRoot":"","sources":["../../../src/fields/map-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE/C,MAAM,WAAW,WAAY,SAAQ,QAAQ;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,wBAAgB,aAAa,CAAE,GAAG,CAAC,EAAE,GAAG,GAAG,GAAG,IAAI,WAAW,CAE5D;AAED,qBAAa,QAAS,SAAQ,KAAK;IAC1B,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACxB,OAAO,CAAC,WAAW,CAAC,CAAQ;gBAEf,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM;IAU3D,iBAAiB,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAI1C,eAAe,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAIxC,UAAU,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAanC,UAAU,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAyBnC,mBAAmB,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IA4B5C,aAAa,IAAK,MAAM;CAGzB"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { codecTypes, Field } from "./field.js";
|
|
2
|
+
export function isMapFieldDef(obj) {
|
|
3
|
+
return obj?.keyType != null;
|
|
4
|
+
}
|
|
5
|
+
export class MapField extends Field {
|
|
6
|
+
keyType;
|
|
7
|
+
valueType;
|
|
8
|
+
entryType;
|
|
9
|
+
lengthLimit;
|
|
10
|
+
constructor(name, def, parent) {
|
|
11
|
+
super(name, def, parent);
|
|
12
|
+
this.type = 'message';
|
|
13
|
+
this.keyType = def.keyType ?? 'string';
|
|
14
|
+
this.valueType = def.valueType;
|
|
15
|
+
this.entryType = def.type;
|
|
16
|
+
this.lengthLimit = def.options?.['(protons.options).limit'];
|
|
17
|
+
}
|
|
18
|
+
getInterfaceField(parent) {
|
|
19
|
+
return `${this.name}: Map<${parent.findType(this.keyType).jsType}, ${parent.findType(this.valueType).jsType}>`;
|
|
20
|
+
}
|
|
21
|
+
getDefaultField(parent) {
|
|
22
|
+
return `${this.name}: new Map<${parent.findType(this.keyType).jsType}, ${parent.findType(this.valueType).jsType}>()`;
|
|
23
|
+
}
|
|
24
|
+
getEncoder(parent) {
|
|
25
|
+
const id = (this.id << 3) | codecTypes.message;
|
|
26
|
+
const entryType = parent.findType(this.entryType);
|
|
27
|
+
return `
|
|
28
|
+
if (${entryType.getValueTest(this, 'value')}) {
|
|
29
|
+
for (const [key, value] of obj.${this.name}.entries()) {
|
|
30
|
+
w.uint32(${id})
|
|
31
|
+
${entryType.getEncoder(this, '{ key, value }')}
|
|
32
|
+
}
|
|
33
|
+
}`;
|
|
34
|
+
}
|
|
35
|
+
getDecoder(parent) {
|
|
36
|
+
parent.addImport('protons-runtime', 'MaxSizeError');
|
|
37
|
+
const entryType = parent.findType(this.entryType);
|
|
38
|
+
let limit = `
|
|
39
|
+
if (opts.limits?.${this.name} != null && obj.${this.name}.size === opts.limits.${this.name}) {
|
|
40
|
+
throw new MaxSizeError('Decode error - map field "${this.name}" had too many elements')
|
|
41
|
+
}
|
|
42
|
+
`;
|
|
43
|
+
if (this.lengthLimit != null) {
|
|
44
|
+
limit += `
|
|
45
|
+
if (obj.${this.name}.size === ${this.lengthLimit}) {
|
|
46
|
+
throw new MaxSizeError('Decode error - map field "${this.name}" had too many elements')
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
return `case ${this.id}: {${limit}
|
|
51
|
+
const entry = ${entryType.getDecoder(this)}
|
|
52
|
+
obj.${this.name}.set(entry.key, entry.value)
|
|
53
|
+
break
|
|
54
|
+
}`;
|
|
55
|
+
}
|
|
56
|
+
getStreamingDecoder(parent) {
|
|
57
|
+
parent.addImport('protons-runtime', 'MaxLengthError');
|
|
58
|
+
let limit = `
|
|
59
|
+
if (opts.limits?.${this.name} != null && obj.${this.name} === opts.limits.${this.name}) {
|
|
60
|
+
throw new MaxLengthError('Decode error - map field "${this.name}" had too many elements')
|
|
61
|
+
}
|
|
62
|
+
`;
|
|
63
|
+
if (this.lengthLimit != null) {
|
|
64
|
+
limit += `
|
|
65
|
+
if (obj.${this.name} === ${this.lengthLimit}) {
|
|
66
|
+
throw new MaxLengthError('Decode error - repeated field "${this.name}" had too many elements')
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
}
|
|
70
|
+
const type = parent.findType(this.entryType);
|
|
71
|
+
return `case ${this.id}: {${limit}
|
|
72
|
+
${type.getStreamingDecoder(this, `\`\${prefix != null ? \`\${prefix}\` : '$'}.${this.name}{}\``, ' ')}
|
|
73
|
+
|
|
74
|
+
obj.${this.name}++
|
|
75
|
+
|
|
76
|
+
break
|
|
77
|
+
}`;
|
|
78
|
+
}
|
|
79
|
+
getLimitField() {
|
|
80
|
+
return `${this.name}: ${this.lengthLimit ?? 0}`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=map-field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-field.js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CAAC;YAC7B,KAAK,IAAI;wBACS,IAAI,CAAC,IAAI,QAAQ,IAAI,CAAC,WAAW;2EACkB,IAAI,CAAC,IAAI;;CAEnF,CAAA;QACG,CAAC;QAED,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QAE5C,OAAO,QAAQ,IAAI,CAAC,EAAE,MAAM,KAAK;gBACrB,IAAI,CAAC,mBAAmB,CAAC,IAAI,EAAE,+CAA+C,IAAI,CAAC,IAAI,MAAM,EAAE,IAAI,CAAC;;oBAEhG,IAAI,CAAC,IAAI;;;cAGf,CAAA;IACZ,CAAC;IAED,aAAa;QACX,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,WAAW,IAAI,CAAC,EAAE,CAAA;IACjD,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Message } from '../types/message.ts';
|
|
2
|
+
import { Field } from './field.ts';
|
|
3
|
+
import type { Parent } from '../types/index.ts';
|
|
4
|
+
export declare class MessageField extends Field {
|
|
5
|
+
getInterfaceField(parent: Parent, indent?: string): string;
|
|
6
|
+
getMessage(parent: Parent): Message;
|
|
7
|
+
getStreamingDecoder(parent: Parent): string;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=message-field.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-field.d.ts","sourceRoot":"","sources":["../../../src/fields/message-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,KAAK,EAAE,MAAM,EAAQ,MAAM,mBAAmB,CAAA;AAErD,qBAAa,YAAa,SAAQ,KAAK;IACrC,iBAAiB,CAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAK,GAAG,MAAM;IAIvD,UAAU,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IAUpC,mBAAmB,CAAE,MAAM,EAAE,MAAM,GAAG,MAAM;CAS7C"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Message } from "../types/message.js";
|
|
2
|
+
import { Field } from "./field.js";
|
|
3
|
+
export class MessageField extends Field {
|
|
4
|
+
getInterfaceField(parent, indent = '') {
|
|
5
|
+
return `${indent}${this.name}?: ${parent.findType(this.type).jsType}`;
|
|
6
|
+
}
|
|
7
|
+
getMessage(parent) {
|
|
8
|
+
const type = parent.findType(this.type);
|
|
9
|
+
if (!(type instanceof Message)) {
|
|
10
|
+
throw new Error(`Field ${this.name} was not a message field`);
|
|
11
|
+
}
|
|
12
|
+
return type;
|
|
13
|
+
}
|
|
14
|
+
getStreamingDecoder(parent) {
|
|
15
|
+
const type = parent.findType(this.type);
|
|
16
|
+
return `case ${this.id}: {
|
|
17
|
+
${type.getStreamingDecoder(this, `\`\${prefix != null ? \`\${prefix}\` : '$'}.${this.name}\``, ' ')}
|
|
18
|
+
|
|
19
|
+
break
|
|
20
|
+
}`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=message-field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-field.js","sourceRoot":"","sources":["../../../src/fields/message-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAGlC,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,iBAAiB,CAAE,MAAc,EAAE,MAAM,GAAG,EAAE;QAC5C,OAAO,GAAG,MAAM,GAAG,IAAI,CAAC,IAAI,MAAM,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,CAAA;IACvE,CAAC;IAED,UAAU,CAAE,MAAc;QACxB,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAEvC,IAAI,CAAC,CAAC,IAAI,YAAY,OAAO,CAAC,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,IAAI,0BAA0B,CAAC,CAAA;QAC/D,CAAC;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAED,mBAAmB,CAAE,MAAc;QACjC,MAAM,IAAI,GAAS,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAE7C,OAAO,QAAQ,IAAI,CAAC,EAAE;gBACV,IAAI,CAAC,mBAAmB,CAAC,IAAI,EAAE,+CAA+C,IAAI,CAAC,IAAI,IAAI,EAAE,IAAI,CAAC;;;cAGpG,CAAA;IACZ,CAAC;CACF"}
|