@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.2-alpha.2

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.
Files changed (138) hide show
  1. package/dist/cjs/binary-encoding.js +414 -409
  2. package/dist/cjs/binary-format.js +13 -0
  3. package/dist/cjs/descriptor-registry.js +420 -449
  4. package/dist/cjs/descriptor-set.js +429 -513
  5. package/dist/cjs/enum.js +13 -0
  6. package/dist/cjs/field-list.js +13 -0
  7. package/dist/cjs/field.js +43 -30
  8. package/dist/cjs/google/protobuf/any_pb.js +122 -140
  9. package/dist/cjs/google/protobuf/api_pb.js +177 -224
  10. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
  11. package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
  12. package/dist/cjs/google/protobuf/duration_pb.js +72 -88
  13. package/dist/cjs/google/protobuf/empty_pb.js +17 -17
  14. package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
  15. package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
  16. package/dist/cjs/google/protobuf/struct_pb.js +144 -181
  17. package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
  18. package/dist/cjs/google/protobuf/type_pb.js +406 -462
  19. package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
  20. package/dist/cjs/google/varint.js +166 -171
  21. package/dist/cjs/index.js +41 -122
  22. package/dist/cjs/json-format.js +13 -0
  23. package/dist/cjs/message-type.js +13 -0
  24. package/dist/cjs/message.js +89 -85
  25. package/dist/cjs/private/assert.js +33 -26
  26. package/dist/cjs/private/base64.js +93 -79
  27. package/dist/cjs/private/binary-format-common.js +208 -208
  28. package/dist/cjs/private/binary-format-proto2.js +88 -109
  29. package/dist/cjs/private/binary-format-proto3.js +70 -90
  30. package/dist/cjs/private/enum.js +77 -44
  31. package/dist/cjs/private/field-list.js +64 -50
  32. package/dist/cjs/private/field-wrapper.js +21 -10
  33. package/dist/cjs/private/field.js +35 -25
  34. package/dist/cjs/private/json-format-common.js +427 -440
  35. package/dist/cjs/private/json-format-proto2.js +91 -89
  36. package/dist/cjs/private/json-format-proto3.js +82 -93
  37. package/dist/cjs/private/message-type.js +39 -27
  38. package/dist/cjs/private/names.js +71 -62
  39. package/dist/cjs/private/options-map.js +13 -0
  40. package/dist/cjs/private/proto-runtime.js +25 -16
  41. package/dist/cjs/private/scalars.js +113 -103
  42. package/dist/cjs/private/util-common.js +193 -210
  43. package/dist/cjs/private/util.js +13 -0
  44. package/dist/cjs/proto-int64.js +123 -111
  45. package/dist/cjs/proto2.js +63 -63
  46. package/dist/cjs/proto3.js +74 -74
  47. package/dist/cjs/service-type.js +27 -17
  48. package/dist/cjs/type-registry.js +46 -31
  49. package/dist/esm/binary-encoding.js +414 -414
  50. package/dist/esm/binary-format.js +13 -0
  51. package/dist/esm/descriptor-registry.js +422 -458
  52. package/dist/esm/descriptor-set.js +430 -478
  53. package/dist/esm/enum.js +13 -0
  54. package/dist/esm/field-list.js +13 -0
  55. package/dist/esm/field.js +42 -29
  56. package/dist/esm/google/protobuf/any_pb.js +122 -140
  57. package/dist/esm/google/protobuf/api_pb.js +177 -197
  58. package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
  59. package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
  60. package/dist/esm/google/protobuf/duration_pb.js +72 -88
  61. package/dist/esm/google/protobuf/empty_pb.js +17 -17
  62. package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
  63. package/dist/esm/google/protobuf/source_context_pb.js +25 -25
  64. package/dist/esm/google/protobuf/struct_pb.js +143 -180
  65. package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
  66. package/dist/esm/google/protobuf/type_pb.js +401 -421
  67. package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
  68. package/dist/esm/google/varint.js +165 -164
  69. package/dist/esm/index.js +14 -1
  70. package/dist/esm/json-format.js +13 -0
  71. package/dist/esm/message-type.js +13 -0
  72. package/dist/esm/message.js +89 -85
  73. package/dist/esm/private/assert.js +32 -21
  74. package/dist/esm/private/base64.js +92 -78
  75. package/dist/esm/private/binary-format-common.js +209 -207
  76. package/dist/esm/private/binary-format-proto2.js +89 -81
  77. package/dist/esm/private/binary-format-proto3.js +71 -67
  78. package/dist/esm/private/enum.js +75 -43
  79. package/dist/esm/private/field-list.js +64 -50
  80. package/dist/esm/private/field-wrapper.js +21 -10
  81. package/dist/esm/private/field.js +35 -22
  82. package/dist/esm/private/json-format-common.js +427 -435
  83. package/dist/esm/private/json-format-proto2.js +89 -81
  84. package/dist/esm/private/json-format-proto3.js +79 -80
  85. package/dist/esm/private/message-type.js +40 -28
  86. package/dist/esm/private/names.js +70 -57
  87. package/dist/esm/private/options-map.js +13 -0
  88. package/dist/esm/private/proto-runtime.js +26 -12
  89. package/dist/esm/private/scalars.js +112 -99
  90. package/dist/esm/private/util-common.js +193 -192
  91. package/dist/esm/private/util.js +13 -0
  92. package/dist/esm/proto-int64.js +123 -111
  93. package/dist/esm/proto2.js +63 -57
  94. package/dist/esm/proto3.js +74 -68
  95. package/dist/esm/service-type.js +25 -12
  96. package/dist/esm/type-registry.js +46 -31
  97. package/dist/types/binary-encoding.d.ts +398 -398
  98. package/dist/types/binary-format.d.ts +69 -91
  99. package/dist/types/descriptor-registry.d.ts +23 -29
  100. package/dist/types/descriptor-set.d.ts +107 -128
  101. package/dist/types/enum.d.ts +21 -21
  102. package/dist/types/field-list.d.ts +21 -21
  103. package/dist/types/field.d.ts +191 -231
  104. package/dist/types/google/protobuf/any_pb.d.ts +56 -77
  105. package/dist/types/google/protobuf/api_pb.d.ts +144 -187
  106. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
  107. package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
  108. package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
  109. package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
  110. package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
  111. package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
  112. package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
  113. package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
  114. package/dist/types/google/protobuf/type_pb.d.ts +355 -422
  115. package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
  116. package/dist/types/google/varint.d.ts +6 -13
  117. package/dist/types/index.d.ts +4 -27
  118. package/dist/types/json-format.d.ts +70 -89
  119. package/dist/types/message-type.d.ts +37 -45
  120. package/dist/types/message.d.ts +62 -87
  121. package/dist/types/private/assert.d.ts +1 -4
  122. package/dist/types/private/binary-format-common.d.ts +6 -32
  123. package/dist/types/private/enum.d.ts +13 -11
  124. package/dist/types/private/field-list.d.ts +14 -21
  125. package/dist/types/private/field-wrapper.d.ts +4 -10
  126. package/dist/types/private/field.d.ts +12 -12
  127. package/dist/types/private/json-format-common.d.ts +5 -27
  128. package/dist/types/private/message-type.d.ts +12 -7
  129. package/dist/types/private/names.d.ts +1 -4
  130. package/dist/types/private/options-map.d.ts +1 -1
  131. package/dist/types/private/proto-runtime.d.ts +34 -42
  132. package/dist/types/private/scalars.d.ts +5 -12
  133. package/dist/types/private/util-common.d.ts +1 -4
  134. package/dist/types/private/util.d.ts +29 -41
  135. package/dist/types/proto-int64.d.ts +42 -42
  136. package/dist/types/service-type.d.ts +36 -56
  137. package/dist/types/type-registry.d.ts +13 -15
  138. package/package.json +2 -3
@@ -1,90 +1,98 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  import { wrapField } from "./field-wrapper.js";
2
15
  import { assert } from "./assert.js";
3
16
  import { makeJsonFormatCommon } from "./json-format-common.js";
4
17
  /* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
5
18
  export function makeJsonFormatProto2() {
6
- return makeJsonFormatCommon((writeEnum, writeScalar) => {
7
- return function writeField(field, value, options) {
8
- if (field.kind == "map") {
9
- const jsonObj = {};
10
- switch (field.V.kind) {
11
- case "scalar":
12
- for (const [entryKey, entryValue] of Object.entries(value)) {
13
- const val = writeScalar(field.V.T, entryValue, true);
14
- assert(val !== undefined);
15
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
19
+ return makeJsonFormatCommon((writeEnum, writeScalar) => {
20
+ return function writeField(field, value, options) {
21
+ if (field.kind == "map") {
22
+ const jsonObj = {};
23
+ switch (field.V.kind) {
24
+ case "scalar":
25
+ for (const [entryKey, entryValue] of Object.entries(value)) {
26
+ const val = writeScalar(field.V.T, entryValue, true);
27
+ assert(val !== undefined);
28
+ jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
29
+ }
30
+ break;
31
+ case "message":
32
+ for (const [entryKey, entryValue] of Object.entries(value)) {
33
+ // JSON standard allows only (double quoted) string as property key
34
+ jsonObj[entryKey.toString()] = entryValue.toJson(options);
35
+ }
36
+ break;
37
+ case "enum":
38
+ const enumType = field.V.T;
39
+ for (const [entryKey, entryValue] of Object.entries(value)) {
40
+ assert(entryValue === undefined || typeof entryValue == "number");
41
+ const val = writeEnum(enumType, entryValue, true, options.enumAsInteger);
42
+ assert(val !== undefined);
43
+ jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
44
+ }
45
+ break;
46
+ }
47
+ return options.emitDefaultValues || Object.keys(jsonObj).length > 0
48
+ ? jsonObj
49
+ : undefined;
16
50
  }
17
- break;
18
- case "message":
19
- for (const [entryKey, entryValue] of Object.entries(value)) {
20
- // JSON standard allows only (double quoted) string as property key
21
- jsonObj[entryKey.toString()] = entryValue.toJson(options);
51
+ else if (field.repeated) {
52
+ const jsonArr = [];
53
+ switch (field.kind) {
54
+ case "scalar":
55
+ for (let i = 0; i < value.length; i++) {
56
+ jsonArr.push(writeScalar(field.T, value[i], true));
57
+ }
58
+ break;
59
+ case "enum":
60
+ for (let i = 0; i < value.length; i++) {
61
+ jsonArr.push(writeEnum(field.T, value[i], true, options.enumAsInteger));
62
+ }
63
+ break;
64
+ case "message":
65
+ for (let i = 0; i < value.length; i++) {
66
+ jsonArr.push(value[i].toJson(options));
67
+ }
68
+ break;
69
+ }
70
+ return options.emitDefaultValues || jsonArr.length > 0
71
+ ? jsonArr
72
+ : undefined;
22
73
  }
23
- break;
24
- case "enum":
25
- const enumType = field.V.T;
26
- for (const [entryKey, entryValue] of Object.entries(value)) {
27
- assert(entryValue === undefined || typeof entryValue == "number");
28
- const val = writeEnum(
29
- enumType,
30
- entryValue,
31
- true,
32
- options.enumAsInteger
33
- );
34
- assert(val !== undefined);
35
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
74
+ else {
75
+ // In contrast to proto3, we raise an error if a non-optional (proto2 required)
76
+ // field is missing a value.
77
+ if (value === undefined) {
78
+ if (!field.oneof && !field.opt) {
79
+ throw `required field not set`;
80
+ }
81
+ return undefined;
82
+ }
83
+ switch (field.kind) {
84
+ case "scalar":
85
+ // In contrast to proto3, we do not skip intrinsic default values.
86
+ // Explicit default values are not special cased either.
87
+ return writeScalar(field.T, value, true);
88
+ case "enum":
89
+ // In contrast to proto3, we do not skip intrinsic default values.
90
+ // Explicit default values are not special cased either.
91
+ return writeEnum(field.T, value, true, options.enumAsInteger);
92
+ case "message":
93
+ return wrapField(field.T, value).toJson(options);
94
+ }
36
95
  }
37
- break;
38
- }
39
- return options.emitDefaultValues || Object.keys(jsonObj).length > 0
40
- ? jsonObj
41
- : undefined;
42
- } else if (field.repeated) {
43
- const jsonArr = [];
44
- switch (field.kind) {
45
- case "scalar":
46
- for (let i = 0; i < value.length; i++) {
47
- jsonArr.push(writeScalar(field.T, value[i], true));
48
- }
49
- break;
50
- case "enum":
51
- for (let i = 0; i < value.length; i++) {
52
- jsonArr.push(
53
- writeEnum(field.T, value[i], true, options.enumAsInteger)
54
- );
55
- }
56
- break;
57
- case "message":
58
- for (let i = 0; i < value.length; i++) {
59
- jsonArr.push(value[i].toJson(options));
60
- }
61
- break;
62
- }
63
- return options.emitDefaultValues || jsonArr.length > 0
64
- ? jsonArr
65
- : undefined;
66
- } else {
67
- // In contrast to proto3, we raise an error if a non-optional (proto2 required)
68
- // field is missing a value.
69
- if (value === undefined) {
70
- if (!field.oneof && !field.opt) {
71
- throw `required field not set`;
72
- }
73
- return undefined;
74
- }
75
- switch (field.kind) {
76
- case "scalar":
77
- // In contrast to proto3, we do not skip intrinsic default values.
78
- // Explicit default values are not special cased either.
79
- return writeScalar(field.T, value, true);
80
- case "enum":
81
- // In contrast to proto3, we do not skip intrinsic default values.
82
- // Explicit default values are not special cased either.
83
- return writeEnum(field.T, value, true, options.enumAsInteger);
84
- case "message":
85
- return wrapField(field.T, value).toJson(options);
86
- }
87
- }
88
- };
89
- });
96
+ };
97
+ });
90
98
  }
@@ -1,89 +1,88 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  import { wrapField } from "./field-wrapper.js";
2
15
  import { assert } from "./assert.js";
3
16
  import { makeJsonFormatCommon } from "./json-format-common.js";
4
17
  /* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
5
18
  export function makeJsonFormatProto3() {
6
- return makeJsonFormatCommon((writeEnum, writeScalar) => {
7
- return function writeField(field, value, options) {
8
- if (field.kind == "map") {
9
- const jsonObj = {};
10
- switch (field.V.kind) {
11
- case "scalar":
12
- for (const [entryKey, entryValue] of Object.entries(value)) {
13
- const val = writeScalar(field.V.T, entryValue, true);
14
- assert(val !== undefined);
15
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
19
+ return makeJsonFormatCommon((writeEnum, writeScalar) => {
20
+ return function writeField(field, value, options) {
21
+ if (field.kind == "map") {
22
+ const jsonObj = {};
23
+ switch (field.V.kind) {
24
+ case "scalar":
25
+ for (const [entryKey, entryValue] of Object.entries(value)) {
26
+ const val = writeScalar(field.V.T, entryValue, true);
27
+ assert(val !== undefined);
28
+ jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
29
+ }
30
+ break;
31
+ case "message":
32
+ for (const [entryKey, entryValue] of Object.entries(value)) {
33
+ // JSON standard allows only (double quoted) string as property key
34
+ jsonObj[entryKey.toString()] = entryValue.toJson(options);
35
+ }
36
+ break;
37
+ case "enum":
38
+ const enumType = field.V.T;
39
+ for (const [entryKey, entryValue] of Object.entries(value)) {
40
+ assert(entryValue === undefined || typeof entryValue == "number");
41
+ const val = writeEnum(enumType, entryValue, true, options.enumAsInteger);
42
+ assert(val !== undefined);
43
+ jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
44
+ }
45
+ break;
46
+ }
47
+ return options.emitDefaultValues || Object.keys(jsonObj).length > 0
48
+ ? jsonObj
49
+ : undefined;
16
50
  }
17
- break;
18
- case "message":
19
- for (const [entryKey, entryValue] of Object.entries(value)) {
20
- // JSON standard allows only (double quoted) string as property key
21
- jsonObj[entryKey.toString()] = entryValue.toJson(options);
51
+ else if (field.repeated) {
52
+ const jsonArr = [];
53
+ switch (field.kind) {
54
+ case "scalar":
55
+ for (let i = 0; i < value.length; i++) {
56
+ jsonArr.push(writeScalar(field.T, value[i], true));
57
+ }
58
+ break;
59
+ case "enum":
60
+ for (let i = 0; i < value.length; i++) {
61
+ jsonArr.push(writeEnum(field.T, value[i], true, options.enumAsInteger));
62
+ }
63
+ break;
64
+ case "message":
65
+ for (let i = 0; i < value.length; i++) {
66
+ jsonArr.push(wrapField(field.T, value[i]).toJson(options));
67
+ }
68
+ break;
69
+ }
70
+ return options.emitDefaultValues || jsonArr.length > 0
71
+ ? jsonArr
72
+ : undefined;
22
73
  }
23
- break;
24
- case "enum":
25
- const enumType = field.V.T;
26
- for (const [entryKey, entryValue] of Object.entries(value)) {
27
- assert(entryValue === undefined || typeof entryValue == "number");
28
- const val = writeEnum(
29
- enumType,
30
- entryValue,
31
- true,
32
- options.enumAsInteger
33
- );
34
- assert(val !== undefined);
35
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
74
+ else {
75
+ switch (field.kind) {
76
+ case "scalar":
77
+ return writeScalar(field.T, value, !!field.oneof || field.opt || options.emitDefaultValues);
78
+ case "enum":
79
+ return writeEnum(field.T, value, !!field.oneof || field.opt || options.emitDefaultValues, options.enumAsInteger);
80
+ case "message":
81
+ return value !== undefined
82
+ ? wrapField(field.T, value).toJson(options)
83
+ : undefined;
84
+ }
36
85
  }
37
- break;
38
- }
39
- return options.emitDefaultValues || Object.keys(jsonObj).length > 0
40
- ? jsonObj
41
- : undefined;
42
- } else if (field.repeated) {
43
- const jsonArr = [];
44
- switch (field.kind) {
45
- case "scalar":
46
- for (let i = 0; i < value.length; i++) {
47
- jsonArr.push(writeScalar(field.T, value[i], true));
48
- }
49
- break;
50
- case "enum":
51
- for (let i = 0; i < value.length; i++) {
52
- jsonArr.push(
53
- writeEnum(field.T, value[i], true, options.enumAsInteger)
54
- );
55
- }
56
- break;
57
- case "message":
58
- for (let i = 0; i < value.length; i++) {
59
- jsonArr.push(wrapField(field.T, value[i]).toJson(options));
60
- }
61
- break;
62
- }
63
- return options.emitDefaultValues || jsonArr.length > 0
64
- ? jsonArr
65
- : undefined;
66
- } else {
67
- switch (field.kind) {
68
- case "scalar":
69
- return writeScalar(
70
- field.T,
71
- value,
72
- !!field.oneof || field.opt || options.emitDefaultValues
73
- );
74
- case "enum":
75
- return writeEnum(
76
- field.T,
77
- value,
78
- !!field.oneof || field.opt || options.emitDefaultValues,
79
- options.enumAsInteger
80
- );
81
- case "message":
82
- return value !== undefined
83
- ? wrapField(field.T, value).toJson(options)
84
- : undefined;
85
- }
86
- }
87
- };
88
- });
86
+ };
87
+ });
89
88
  }
@@ -1,33 +1,45 @@
1
- import { Message } from "../message.js";
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { Message, } from "../message.js";
2
15
  /**
3
16
  * Create a new message type using the given runtime.
4
17
  */
5
18
  export function makeMessageType(runtime, typeName, fields, opt) {
6
- const localName =
7
- opt?.localName ?? typeName.substring(typeName.lastIndexOf(".") + 1);
8
- const type = {
9
- [localName]: function (data) {
10
- runtime.util.initFields(this);
11
- runtime.util.initPartial(data, this);
12
- },
13
- }[localName];
14
- Object.setPrototypeOf(type.prototype, new Message());
15
- Object.assign(type, {
16
- runtime,
17
- typeName,
18
- fields: runtime.util.newFieldList(fields),
19
- fromBinary(bytes, options) {
20
- return new type().fromBinary(bytes, options);
21
- },
22
- fromJson(jsonValue, options) {
23
- return new type().fromJson(jsonValue, options);
24
- },
25
- fromJsonString(jsonString, options) {
26
- return new type().fromJsonString(jsonString, options);
27
- },
28
- equals(a, b) {
29
- return runtime.util.equals(type, a, b);
30
- },
31
- });
32
- return type;
19
+ const localName = opt?.localName ?? typeName.substring(typeName.lastIndexOf(".") + 1);
20
+ const type = {
21
+ [localName]: function (data) {
22
+ runtime.util.initFields(this);
23
+ runtime.util.initPartial(data, this);
24
+ },
25
+ }[localName];
26
+ Object.setPrototypeOf(type.prototype, new Message());
27
+ Object.assign(type, {
28
+ runtime,
29
+ typeName,
30
+ fields: runtime.util.newFieldList(fields),
31
+ fromBinary(bytes, options) {
32
+ return new type().fromBinary(bytes, options);
33
+ },
34
+ fromJson(jsonValue, options) {
35
+ return new type().fromJson(jsonValue, options);
36
+ },
37
+ fromJsonString(jsonString, options) {
38
+ return new type().fromJsonString(jsonString, options);
39
+ },
40
+ equals(a, b) {
41
+ return runtime.util.equals(type, a, b);
42
+ },
43
+ });
44
+ return type;
33
45
  }
@@ -1,68 +1,81 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  /**
2
15
  * Returns the JSON name for a protobuf field, exactly like protoc does.
3
16
  */
4
17
  export function makeJsonName(protoName) {
5
- return protoCamelCase(protoName);
18
+ return protoCamelCase(protoName);
6
19
  }
7
20
  /**
8
21
  * Returns the local name of a field, exactly like protoc-gen-es does.
9
22
  */
10
23
  export function makeFieldName(protoName, inOneof) {
11
- const n = protoCamelCase(protoName);
12
- if (inOneof) {
13
- return n;
14
- }
15
- return rProp[n] ? n + escapeChar : n;
24
+ const n = protoCamelCase(protoName);
25
+ if (inOneof) {
26
+ return n;
27
+ }
28
+ return rProp[n] ? n + escapeChar : n;
16
29
  }
17
30
  /**
18
31
  * Returns the local name of a oneof group, exactly like protoc-gen-es does.
19
32
  */
20
33
  export function makeOneofName(protoName) {
21
- return makeFieldName(protoName, false);
34
+ return makeFieldName(protoName, false);
22
35
  }
23
36
  /**
24
37
  * Returns the local name of a rpc, exactly like protoc-gen-es does.
25
38
  */
26
39
  export function makeMethodName(protoName) {
27
- if (protoName.length == 0) {
28
- return protoName;
29
- }
30
- return protoName[0].toLowerCase() + protoName.substring(1);
40
+ if (protoName.length == 0) {
41
+ return protoName;
42
+ }
43
+ return protoName[0].toLowerCase() + protoName.substring(1);
31
44
  }
32
45
  // Converts snake_case to protoCamelCase according to the convention
33
46
  // used by protoc to convert a field name to a JSON name.
34
47
  function protoCamelCase(snakeCase) {
35
- let capNext = false;
36
- const b = [];
37
- for (let i = 0; i < snakeCase.length; i++) {
38
- let c = snakeCase.charAt(i);
39
- switch (c) {
40
- case "_":
41
- capNext = true;
42
- break;
43
- case "0":
44
- case "1":
45
- case "2":
46
- case "3":
47
- case "4":
48
- case "5":
49
- case "6":
50
- case "7":
51
- case "8":
52
- case "9":
53
- b.push(c);
54
- capNext = false;
55
- break;
56
- default:
57
- if (capNext) {
58
- capNext = false;
59
- c = c.toUpperCase();
48
+ let capNext = false;
49
+ const b = [];
50
+ for (let i = 0; i < snakeCase.length; i++) {
51
+ let c = snakeCase.charAt(i);
52
+ switch (c) {
53
+ case "_":
54
+ capNext = true;
55
+ break;
56
+ case "0":
57
+ case "1":
58
+ case "2":
59
+ case "3":
60
+ case "4":
61
+ case "5":
62
+ case "6":
63
+ case "7":
64
+ case "8":
65
+ case "9":
66
+ b.push(c);
67
+ capNext = false;
68
+ break;
69
+ default:
70
+ if (capNext) {
71
+ capNext = false;
72
+ c = c.toUpperCase();
73
+ }
74
+ b.push(c);
75
+ break;
60
76
  }
61
- b.push(c);
62
- break;
63
77
  }
64
- }
65
- return b.join("");
78
+ return b.join("");
66
79
  }
67
80
  // escapeChar must be appended to a reserved name.
68
81
  // We choose '$' because it is invalid in proto identifiers.
@@ -70,21 +83,21 @@ const escapeChar = "$";
70
83
  // Names that cannot be used for object properties.
71
84
  // See buf_es/protoc-gen-es/internal/protoplugin/names.go
72
85
  const rProp = {
73
- // names reserved by JavaScript
74
- constructor: true,
75
- toString: true,
76
- toJSON: true,
77
- valueOf: true,
78
- // names reserved by the runtime
79
- getType: true,
80
- clone: true,
81
- equals: true,
82
- fromBinary: true,
83
- fromJson: true,
84
- fromJsonString: true,
85
- toBinary: true,
86
- toJson: true,
87
- toJsonString: true,
88
- // names reserved by the runtime for the future
89
- toObject: true,
86
+ // names reserved by JavaScript
87
+ constructor: true,
88
+ toString: true,
89
+ toJSON: true,
90
+ valueOf: true,
91
+ // names reserved by the runtime
92
+ getType: true,
93
+ clone: true,
94
+ equals: true,
95
+ fromBinary: true,
96
+ fromJson: true,
97
+ fromJsonString: true,
98
+ toBinary: true,
99
+ toJson: true,
100
+ toJsonString: true,
101
+ // names reserved by the runtime for the future
102
+ toObject: true,
90
103
  };
@@ -1 +1,14 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
1
14
  export {};
@@ -1,15 +1,29 @@
1
- import { getEnumType, makeEnumType } from "./enum.js";
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { getEnumType, makeEnum, makeEnumType } from "./enum.js";
2
15
  import { makeMessageType } from "./message-type.js";
3
16
  export function makeProtoRuntime(syntax, json, bin, util) {
4
- return {
5
- syntax,
6
- json,
7
- bin,
8
- util,
9
- makeMessageType(typeName, fields, opt) {
10
- return makeMessageType(this, typeName, fields, opt);
11
- },
12
- makeEnumType,
13
- getEnumType,
14
- };
17
+ return {
18
+ syntax,
19
+ json,
20
+ bin,
21
+ util,
22
+ makeMessageType(typeName, fields, opt) {
23
+ return makeMessageType(this, typeName, fields, opt);
24
+ },
25
+ makeEnum,
26
+ makeEnumType,
27
+ getEnumType,
28
+ };
15
29
  }