@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,3 +1,16 @@
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 { unwrapField, wrapField } from "./field-wrapper.js";
2
15
  import { ScalarType } from "../field.js";
3
16
  import { assert, assertFloat32, assertInt32, assertUInt32 } from "./assert.js";
@@ -6,468 +19,447 @@ import { base64decode, base64encode } from "./base64.js";
6
19
  /* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
7
20
  // Default options for parsing JSON.
8
21
  const jsonReadDefaults = {
9
- ignoreUnknownFields: false,
22
+ ignoreUnknownFields: false,
10
23
  };
11
24
  // Default options for serializing to JSON.
12
25
  const jsonWriteDefaults = {
13
- emitDefaultValues: false,
14
- enumAsInteger: false,
15
- useProtoFieldName: false,
16
- prettySpaces: 0,
26
+ emitDefaultValues: false,
27
+ enumAsInteger: false,
28
+ useProtoFieldName: false,
29
+ prettySpaces: 0,
17
30
  };
18
31
  function makeReadOptions(options) {
19
- return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
32
+ return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
20
33
  }
21
34
  function makeWriteOptions(options) {
22
- return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
35
+ return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
23
36
  }
24
37
  export function makeJsonFormatCommon(makeWriteField) {
25
- const writeField = makeWriteField(writeEnum, writeScalar);
26
- return {
27
- makeReadOptions,
28
- makeWriteOptions,
29
- readMessage(type, json, options, message) {
30
- if (json == null || Array.isArray(json) || typeof json != "object") {
31
- throw new Error(
32
- `cannot decode message ${type.typeName} from JSON: ${this.debug(
33
- json
34
- )}`
35
- );
36
- }
37
- message = message ?? new type();
38
- const oneofSeen = {};
39
- for (const [jsonKey, jsonValue] of Object.entries(json)) {
40
- const field = type.fields.findJsonName(jsonKey);
41
- if (!field) {
42
- if (!options.ignoreUnknownFields) {
43
- throw new Error(
44
- `cannot decode message ${type.typeName} from JSON: key "${jsonKey}" is unknown`
45
- );
46
- }
47
- continue;
48
- }
49
- let localName = field.localName;
50
- let target = message;
51
- if (field.oneof) {
52
- if (jsonValue === null && field.kind == "scalar") {
53
- // see conformance test Required.Proto3.JsonInput.OneofFieldNull{First,Second}
54
- continue;
55
- }
56
- const seen = oneofSeen[field.oneof.localName];
57
- if (seen) {
58
- throw new Error(
59
- `cannot decode message ${type.typeName} from JSON: multiple keys for oneof "${field.oneof.name}" present: "${seen}", "${jsonKey}"`
60
- );
61
- }
62
- oneofSeen[field.oneof.localName] = jsonKey;
63
- target = target[field.oneof.localName] = { case: localName };
64
- localName = "value";
65
- }
66
- if (field.repeated) {
67
- if (jsonValue === null) {
68
- continue;
69
- }
70
- if (!Array.isArray(jsonValue)) {
71
- throw new Error(
72
- `cannot decode field ${type.typeName}.${
73
- field.name
74
- } from JSON: "${this.debug(jsonValue)}"`
75
- );
76
- }
77
- const targetArray = target[localName];
78
- for (const jsonItem of jsonValue) {
79
- if (jsonItem === null) {
80
- throw new Error(
81
- `cannot decode field ${type.typeName}.${
82
- field.name
83
- } from JSON: "${this.debug(jsonItem)}"`
84
- );
38
+ const writeField = makeWriteField(writeEnum, writeScalar);
39
+ return {
40
+ makeReadOptions,
41
+ makeWriteOptions,
42
+ readMessage(type, json, options, message) {
43
+ if (json == null || Array.isArray(json) || typeof json != "object") {
44
+ throw new Error(`cannot decode message ${type.typeName} from JSON: ${this.debug(json)}`);
85
45
  }
86
- let val;
87
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- "map" is invalid for repeated fields
88
- switch (field.kind) {
89
- case "message":
90
- val = field.T.fromJson(jsonItem, options);
91
- break;
92
- case "enum":
93
- val = readEnum(field.T, jsonItem, options.ignoreUnknownFields);
94
- if (val === undefined) continue;
95
- break;
96
- case "scalar":
97
- try {
98
- val = readScalar(field.T, jsonItem);
99
- } catch (e) {
100
- let m = `cannot decode field ${type.typeName}.${
101
- field.name
102
- } from JSON: "${this.debug(jsonItem)}"`;
103
- if (e instanceof Error && e.message.length > 0) {
104
- m += `: ${e.message}`;
105
- }
106
- throw new Error(m);
46
+ message = message ?? new type();
47
+ const oneofSeen = {};
48
+ for (const [jsonKey, jsonValue] of Object.entries(json)) {
49
+ const field = type.fields.findJsonName(jsonKey);
50
+ if (!field) {
51
+ if (!options.ignoreUnknownFields) {
52
+ throw new Error(`cannot decode message ${type.typeName} from JSON: key "${jsonKey}" is unknown`);
53
+ }
54
+ continue;
107
55
  }
108
- break;
109
- }
110
- targetArray.push(val);
111
- }
112
- } else if (field.kind == "map") {
113
- if (jsonValue === null) {
114
- continue;
115
- }
116
- if (Array.isArray(jsonValue) || typeof jsonValue != "object") {
117
- throw new Error(
118
- `cannot decode field ${type.typeName}.${
119
- field.name
120
- } from JSON: ${this.debug(jsonValue)}`
121
- );
122
- }
123
- const targetMap = target[localName];
124
- for (const [jsonMapKey, jsonMapValue] of Object.entries(jsonValue)) {
125
- if (jsonMapValue === null) {
126
- throw new Error(
127
- `cannot decode field ${type.typeName}.${field.name} from JSON: map value null`
128
- );
129
- }
130
- let val;
131
- switch (field.V.kind) {
132
- case "message":
133
- val = field.V.T.fromJson(jsonMapValue, options);
134
- break;
135
- case "enum":
136
- val = readEnum(
137
- field.V.T,
138
- jsonMapValue,
139
- options.ignoreUnknownFields
140
- );
141
- if (val === undefined) continue;
142
- break;
143
- case "scalar":
144
- try {
145
- val = readScalar(field.V.T, jsonMapValue);
146
- } catch (e) {
147
- let m = `cannot decode map value for field ${type.typeName}.${
148
- field.name
149
- } from JSON: "${this.debug(jsonValue)}"`;
150
- if (e instanceof Error && e.message.length > 0) {
151
- m += `: ${e.message}`;
152
- }
153
- throw new Error(m);
56
+ let localName = field.localName;
57
+ let target = message;
58
+ if (field.oneof) {
59
+ if (jsonValue === null && field.kind == "scalar") {
60
+ // see conformance test Required.Proto3.JsonInput.OneofFieldNull{First,Second}
61
+ continue;
62
+ }
63
+ const seen = oneofSeen[field.oneof.localName];
64
+ if (seen) {
65
+ throw new Error(`cannot decode message ${type.typeName} from JSON: multiple keys for oneof "${field.oneof.name}" present: "${seen}", "${jsonKey}"`);
66
+ }
67
+ oneofSeen[field.oneof.localName] = jsonKey;
68
+ target = target[field.oneof.localName] = { case: localName };
69
+ localName = "value";
70
+ }
71
+ if (field.repeated) {
72
+ if (jsonValue === null) {
73
+ continue;
74
+ }
75
+ if (!Array.isArray(jsonValue)) {
76
+ throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`);
77
+ }
78
+ const targetArray = target[localName];
79
+ for (const jsonItem of jsonValue) {
80
+ if (jsonItem === null) {
81
+ throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonItem)}"`);
82
+ }
83
+ let val;
84
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- "map" is invalid for repeated fields
85
+ switch (field.kind) {
86
+ case "message":
87
+ val = field.T.fromJson(jsonItem, options);
88
+ break;
89
+ case "enum":
90
+ val = readEnum(field.T, jsonItem, options.ignoreUnknownFields);
91
+ if (val === undefined)
92
+ continue;
93
+ break;
94
+ case "scalar":
95
+ try {
96
+ val = readScalar(field.T, jsonItem);
97
+ }
98
+ catch (e) {
99
+ let m = `cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonItem)}"`;
100
+ if (e instanceof Error && e.message.length > 0) {
101
+ m += `: ${e.message}`;
102
+ }
103
+ throw new Error(m);
104
+ }
105
+ break;
106
+ }
107
+ targetArray.push(val);
108
+ }
109
+ }
110
+ else if (field.kind == "map") {
111
+ if (jsonValue === null) {
112
+ continue;
113
+ }
114
+ if (Array.isArray(jsonValue) || typeof jsonValue != "object") {
115
+ throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`);
116
+ }
117
+ const targetMap = target[localName];
118
+ for (const [jsonMapKey, jsonMapValue] of Object.entries(jsonValue)) {
119
+ if (jsonMapValue === null) {
120
+ throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: map value null`);
121
+ }
122
+ let val;
123
+ switch (field.V.kind) {
124
+ case "message":
125
+ val = field.V.T.fromJson(jsonMapValue, options);
126
+ break;
127
+ case "enum":
128
+ val = readEnum(field.V.T, jsonMapValue, options.ignoreUnknownFields);
129
+ if (val === undefined)
130
+ continue;
131
+ break;
132
+ case "scalar":
133
+ try {
134
+ val = readScalar(field.V.T, jsonMapValue);
135
+ }
136
+ catch (e) {
137
+ let m = `cannot decode map value for field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
138
+ if (e instanceof Error && e.message.length > 0) {
139
+ m += `: ${e.message}`;
140
+ }
141
+ throw new Error(m);
142
+ }
143
+ break;
144
+ }
145
+ try {
146
+ targetMap[readScalar(field.K, field.K == ScalarType.BOOL
147
+ ? jsonMapKey == "true"
148
+ ? true
149
+ : jsonMapKey == "false"
150
+ ? false
151
+ : jsonMapKey
152
+ : jsonMapKey).toString()] = val;
153
+ }
154
+ catch (e) {
155
+ let m = `cannot decode map key for field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
156
+ if (e instanceof Error && e.message.length > 0) {
157
+ m += `: ${e.message}`;
158
+ }
159
+ throw new Error(m);
160
+ }
161
+ }
162
+ }
163
+ else {
164
+ switch (field.kind) {
165
+ case "message":
166
+ const messageType = field.T;
167
+ if (jsonValue === null &&
168
+ messageType.typeName != "google.protobuf.Value") {
169
+ if (field.oneof) {
170
+ throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: null is invalid for oneof field "${jsonKey}"`);
171
+ }
172
+ continue;
173
+ }
174
+ const targetMessage = target[localName] === undefined
175
+ ? new messageType()
176
+ : wrapField(messageType, target[localName]);
177
+ target[localName] = unwrapField(messageType, targetMessage.fromJson(jsonValue, options));
178
+ break;
179
+ case "enum":
180
+ const enumValue = readEnum(field.T, jsonValue, options.ignoreUnknownFields);
181
+ if (enumValue !== undefined) {
182
+ target[localName] = enumValue;
183
+ }
184
+ break;
185
+ case "scalar":
186
+ try {
187
+ target[localName] = readScalar(field.T, jsonValue);
188
+ }
189
+ catch (e) {
190
+ let m = `cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
191
+ if (e instanceof Error && e.message.length > 0) {
192
+ m += `: ${e.message}`;
193
+ }
194
+ throw new Error(m);
195
+ }
196
+ break;
197
+ }
154
198
  }
155
- break;
156
199
  }
200
+ return message;
201
+ },
202
+ writeMessage(message, options) {
203
+ const type = message.getType();
204
+ const json = {};
205
+ let field;
157
206
  try {
158
- targetMap[
159
- readScalar(
160
- field.K,
161
- field.K == ScalarType.BOOL
162
- ? jsonMapKey == "true"
163
- ? true
164
- : jsonMapKey == "false"
165
- ? false
166
- : jsonMapKey
167
- : jsonMapKey
168
- ).toString()
169
- ] = val;
170
- } catch (e) {
171
- let m = `cannot decode map key for field ${type.typeName}.${
172
- field.name
173
- } from JSON: "${this.debug(jsonValue)}"`;
174
- if (e instanceof Error && e.message.length > 0) {
175
- m += `: ${e.message}`;
176
- }
177
- throw new Error(m);
178
- }
179
- }
180
- } else {
181
- switch (field.kind) {
182
- case "message":
183
- const messageType = field.T;
184
- if (
185
- jsonValue === null &&
186
- messageType.typeName != "google.protobuf.Value"
187
- ) {
188
- if (field.oneof) {
189
- throw new Error(
190
- `cannot decode field ${type.typeName}.${field.name} from JSON: null is invalid for oneof field "${jsonKey}"`
191
- );
192
- }
193
- continue;
194
- }
195
- const targetMessage =
196
- target[localName] === undefined
197
- ? new messageType()
198
- : wrapField(messageType, target[localName]);
199
- target[localName] = unwrapField(
200
- messageType,
201
- targetMessage.fromJson(jsonValue, options)
202
- );
203
- break;
204
- case "enum":
205
- const enumValue = readEnum(
206
- field.T,
207
- jsonValue,
208
- options.ignoreUnknownFields
209
- );
210
- if (enumValue !== undefined) {
211
- target[localName] = enumValue;
212
- }
213
- break;
214
- case "scalar":
215
- try {
216
- target[localName] = readScalar(field.T, jsonValue);
217
- } catch (e) {
218
- let m = `cannot decode field ${type.typeName}.${
219
- field.name
220
- } from JSON: "${this.debug(jsonValue)}"`;
221
- if (e instanceof Error && e.message.length > 0) {
222
- m += `: ${e.message}`;
207
+ for (const member of type.fields.byMember()) {
208
+ let jsonValue;
209
+ if (member.kind == "oneof") {
210
+ const oneof = message[member.localName];
211
+ if (oneof.value === undefined) {
212
+ continue;
213
+ }
214
+ field = member.findField(oneof.case);
215
+ if (!field) {
216
+ throw "oneof case not found: " + oneof.case;
217
+ }
218
+ jsonValue = writeField(field, oneof.value, options);
219
+ }
220
+ else {
221
+ field = member;
222
+ jsonValue = writeField(field, message[field.localName], options);
223
+ }
224
+ if (jsonValue !== undefined) {
225
+ json[options.useProtoFieldName ? field.name : field.jsonName] =
226
+ jsonValue;
227
+ }
223
228
  }
224
- throw new Error(m);
225
- }
226
- break;
227
- }
228
- }
229
- }
230
- return message;
231
- },
232
- writeMessage(message, options) {
233
- const type = message.getType();
234
- const json = {};
235
- let field;
236
- try {
237
- for (const member of type.fields.byMember()) {
238
- let jsonValue;
239
- if (member.kind == "oneof") {
240
- const oneof = message[member.localName];
241
- if (oneof.value === undefined) {
242
- continue;
243
229
  }
244
- field = member.findField(oneof.case);
245
- if (!field) {
246
- throw "oneof case not found: " + oneof.case;
230
+ catch (e) {
231
+ const m = field
232
+ ? `cannot encode field ${type.typeName}.${field.name} to JSON`
233
+ : `cannot encode message ${type.typeName} to JSON`;
234
+ const r = e instanceof Error ? e.message : String(e);
235
+ throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
247
236
  }
248
- jsonValue = writeField(field, oneof.value, options);
249
- } else {
250
- field = member;
251
- jsonValue = writeField(field, message[field.localName], options);
252
- }
253
- if (jsonValue !== undefined) {
254
- json[options.useProtoFieldName ? field.name : field.jsonName] =
255
- jsonValue;
256
- }
257
- }
258
- } catch (e) {
259
- const m = field
260
- ? `cannot encode field ${type.typeName}.${field.name} to JSON`
261
- : `cannot encode message ${type.typeName} to JSON`;
262
- const r = e instanceof Error ? e.message : String(e);
263
- throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
264
- }
265
- return json;
266
- },
267
- readScalar,
268
- writeScalar,
269
- debug: debugJsonValue,
270
- };
237
+ return json;
238
+ },
239
+ readScalar,
240
+ writeScalar,
241
+ debug: debugJsonValue,
242
+ };
271
243
  }
272
244
  function debugJsonValue(json) {
273
- if (json === null) {
274
- return "null";
275
- }
276
- switch (typeof json) {
277
- case "object":
278
- return Array.isArray(json) ? "array" : "object";
279
- case "string":
280
- return json.length > 100 ? "string" : `"${json.split('"').join('\\"')}"`;
281
- default:
282
- return json.toString();
283
- }
245
+ if (json === null) {
246
+ return "null";
247
+ }
248
+ switch (typeof json) {
249
+ case "object":
250
+ return Array.isArray(json) ? "array" : "object";
251
+ case "string":
252
+ return json.length > 100 ? "string" : `"${json.split('"').join('\\"')}"`;
253
+ default:
254
+ return json.toString();
255
+ }
284
256
  }
285
257
  // May throw an error. If the error message is non-blank, it should be shown.
286
258
  // It is up to the caller to provide context.
287
259
  function readScalar(type, json) {
288
- // every valid case in the switch below returns, and every fall
289
- // through is regarded as a failure.
290
- switch (type) {
291
- // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
292
- // Either numbers or strings are accepted. Exponent notation is also accepted.
293
- case ScalarType.DOUBLE:
294
- case ScalarType.FLOAT:
295
- if (json === null) return 0.0;
296
- if (json === "NaN") return Number.NaN;
297
- if (json === "Infinity") return Number.POSITIVE_INFINITY;
298
- if (json === "-Infinity") return Number.NEGATIVE_INFINITY;
299
- if (json === "") {
300
- // empty string is not a number
301
- break;
302
- }
303
- if (typeof json == "string" && json.trim().length !== json.length) {
304
- // extra whitespace
305
- break;
306
- }
307
- if (typeof json != "string" && typeof json != "number") {
308
- break;
309
- }
310
- const float = Number(json);
311
- if (Number.isNaN(float)) {
312
- // not a number
313
- break;
314
- }
315
- if (!Number.isFinite(float)) {
316
- // infinity and -infinity are handled by string representation above, so this is an error
317
- break;
318
- }
319
- if (type == ScalarType.FLOAT) assertFloat32(float);
320
- return float;
321
- // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
322
- case ScalarType.INT32:
323
- case ScalarType.FIXED32:
324
- case ScalarType.SFIXED32:
325
- case ScalarType.SINT32:
326
- case ScalarType.UINT32:
327
- if (json === null) return 0;
328
- let int32;
329
- if (typeof json == "number") int32 = json;
330
- else if (typeof json == "string" && json.length > 0) {
331
- if (json.trim().length === json.length) int32 = Number(json);
332
- }
333
- if (int32 === undefined) break;
334
- if (type == ScalarType.UINT32) assertUInt32(int32);
335
- else assertInt32(int32);
336
- return int32;
337
- // int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
338
- case ScalarType.INT64:
339
- case ScalarType.SFIXED64:
340
- case ScalarType.SINT64:
341
- if (json === null) return protoInt64.zero;
342
- if (typeof json != "number" && typeof json != "string") break;
343
- return protoInt64.parse(json);
344
- case ScalarType.FIXED64:
345
- case ScalarType.UINT64:
346
- if (json === null) return protoInt64.zero;
347
- if (typeof json != "number" && typeof json != "string") break;
348
- return protoInt64.uParse(json);
349
- // bool:
350
- case ScalarType.BOOL:
351
- if (json === null) return false;
352
- if (typeof json !== "boolean") break;
353
- return json;
354
- // string:
355
- case ScalarType.STRING:
356
- if (json === null) return "";
357
- if (typeof json !== "string") {
358
- break;
359
- }
360
- // A string must always contain UTF-8 encoded or 7-bit ASCII.
361
- // We validate with encodeURIComponent, which appears to be the fastest widely available option.
362
- try {
363
- encodeURIComponent(json);
364
- } catch (e) {
365
- throw new Error("invalid UTF8");
366
- }
367
- return json;
368
- // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
369
- // Either standard or URL-safe base64 encoding with/without paddings are accepted.
370
- case ScalarType.BYTES:
371
- if (json === null || json === "") return new Uint8Array(0);
372
- if (typeof json !== "string") break;
373
- return base64decode(json);
374
- }
375
- throw new Error();
260
+ // every valid case in the switch below returns, and every fall
261
+ // through is regarded as a failure.
262
+ switch (type) {
263
+ // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
264
+ // Either numbers or strings are accepted. Exponent notation is also accepted.
265
+ case ScalarType.DOUBLE:
266
+ case ScalarType.FLOAT:
267
+ if (json === null)
268
+ return 0.0;
269
+ if (json === "NaN")
270
+ return Number.NaN;
271
+ if (json === "Infinity")
272
+ return Number.POSITIVE_INFINITY;
273
+ if (json === "-Infinity")
274
+ return Number.NEGATIVE_INFINITY;
275
+ if (json === "") {
276
+ // empty string is not a number
277
+ break;
278
+ }
279
+ if (typeof json == "string" && json.trim().length !== json.length) {
280
+ // extra whitespace
281
+ break;
282
+ }
283
+ if (typeof json != "string" && typeof json != "number") {
284
+ break;
285
+ }
286
+ const float = Number(json);
287
+ if (Number.isNaN(float)) {
288
+ // not a number
289
+ break;
290
+ }
291
+ if (!Number.isFinite(float)) {
292
+ // infinity and -infinity are handled by string representation above, so this is an error
293
+ break;
294
+ }
295
+ if (type == ScalarType.FLOAT)
296
+ assertFloat32(float);
297
+ return float;
298
+ // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
299
+ case ScalarType.INT32:
300
+ case ScalarType.FIXED32:
301
+ case ScalarType.SFIXED32:
302
+ case ScalarType.SINT32:
303
+ case ScalarType.UINT32:
304
+ if (json === null)
305
+ return 0;
306
+ let int32;
307
+ if (typeof json == "number")
308
+ int32 = json;
309
+ else if (typeof json == "string" && json.length > 0) {
310
+ if (json.trim().length === json.length)
311
+ int32 = Number(json);
312
+ }
313
+ if (int32 === undefined)
314
+ break;
315
+ if (type == ScalarType.UINT32)
316
+ assertUInt32(int32);
317
+ else
318
+ assertInt32(int32);
319
+ return int32;
320
+ // int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
321
+ case ScalarType.INT64:
322
+ case ScalarType.SFIXED64:
323
+ case ScalarType.SINT64:
324
+ if (json === null)
325
+ return protoInt64.zero;
326
+ if (typeof json != "number" && typeof json != "string")
327
+ break;
328
+ return protoInt64.parse(json);
329
+ case ScalarType.FIXED64:
330
+ case ScalarType.UINT64:
331
+ if (json === null)
332
+ return protoInt64.zero;
333
+ if (typeof json != "number" && typeof json != "string")
334
+ break;
335
+ return protoInt64.uParse(json);
336
+ // bool:
337
+ case ScalarType.BOOL:
338
+ if (json === null)
339
+ return false;
340
+ if (typeof json !== "boolean")
341
+ break;
342
+ return json;
343
+ // string:
344
+ case ScalarType.STRING:
345
+ if (json === null)
346
+ return "";
347
+ if (typeof json !== "string") {
348
+ break;
349
+ }
350
+ // A string must always contain UTF-8 encoded or 7-bit ASCII.
351
+ // We validate with encodeURIComponent, which appears to be the fastest widely available option.
352
+ try {
353
+ encodeURIComponent(json);
354
+ }
355
+ catch (e) {
356
+ throw new Error("invalid UTF8");
357
+ }
358
+ return json;
359
+ // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
360
+ // Either standard or URL-safe base64 encoding with/without paddings are accepted.
361
+ case ScalarType.BYTES:
362
+ if (json === null || json === "")
363
+ return new Uint8Array(0);
364
+ if (typeof json !== "string")
365
+ break;
366
+ return base64decode(json);
367
+ }
368
+ throw new Error();
376
369
  }
377
370
  function readEnum(type, json, ignoreUnknownFields) {
378
- if (json === null) {
379
- // proto3 requires 0 to be default value for all enums
380
- return 0;
381
- }
382
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
383
- switch (typeof json) {
384
- case "number":
385
- if (Number.isInteger(json)) {
386
- return json;
387
- }
388
- break;
389
- case "string":
390
- const value = type.findName(json);
391
- if (value || ignoreUnknownFields) {
392
- return value?.no;
393
- }
394
- break;
395
- }
396
- throw new Error(
397
- `cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`
398
- );
371
+ if (json === null) {
372
+ // proto3 requires 0 to be default value for all enums
373
+ return 0;
374
+ }
375
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
376
+ switch (typeof json) {
377
+ case "number":
378
+ if (Number.isInteger(json)) {
379
+ return json;
380
+ }
381
+ break;
382
+ case "string":
383
+ const value = type.findName(json);
384
+ if (value || ignoreUnknownFields) {
385
+ return value?.no;
386
+ }
387
+ break;
388
+ }
389
+ throw new Error(`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`);
399
390
  }
400
391
  function writeEnum(type, value, emitIntrinsicDefault, enumAsInteger) {
401
- if (value === undefined) {
402
- return value;
403
- }
404
- if (value === 0 && !emitIntrinsicDefault) {
405
- // proto3 requires 0 to be default value for all enums
406
- return undefined;
407
- }
408
- if (enumAsInteger) {
409
- return value;
410
- }
411
- if (type.typeName == "google.protobuf.NullValue") {
412
- return null;
413
- }
414
- const val = type.findNumber(value);
415
- return val?.name ?? value; // if we don't know the enum value, just return the number
392
+ if (value === undefined) {
393
+ return value;
394
+ }
395
+ if (value === 0 && !emitIntrinsicDefault) {
396
+ // proto3 requires 0 to be default value for all enums
397
+ return undefined;
398
+ }
399
+ if (enumAsInteger) {
400
+ return value;
401
+ }
402
+ if (type.typeName == "google.protobuf.NullValue") {
403
+ return null;
404
+ }
405
+ const val = type.findNumber(value);
406
+ return val?.name ?? value; // if we don't know the enum value, just return the number
416
407
  }
417
408
  function writeScalar(type, value, emitIntrinsicDefault) {
418
- if (value === undefined) {
419
- return undefined;
420
- }
421
- switch (type) {
422
- // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
423
- case ScalarType.INT32:
424
- case ScalarType.SFIXED32:
425
- case ScalarType.SINT32:
426
- case ScalarType.FIXED32:
427
- case ScalarType.UINT32:
428
- assert(typeof value == "number");
429
- return value != 0 || emitIntrinsicDefault ? value : undefined;
430
- // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
431
- // Either numbers or strings are accepted. Exponent notation is also accepted.
432
- case ScalarType.FLOAT:
433
- // assertFloat32(value);
434
- case ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
435
- assert(typeof value == "number");
436
- if (Number.isNaN(value)) return "NaN";
437
- if (value === Number.POSITIVE_INFINITY) return "Infinity";
438
- if (value === Number.NEGATIVE_INFINITY) return "-Infinity";
439
- return value !== 0 || emitIntrinsicDefault ? value : undefined;
440
- // string:
441
- case ScalarType.STRING:
442
- assert(typeof value == "string");
443
- return value.length > 0 || emitIntrinsicDefault ? value : undefined;
444
- // bool:
445
- case ScalarType.BOOL:
446
- assert(typeof value == "boolean");
447
- return value || emitIntrinsicDefault ? value : undefined;
448
- // JSON value will be a decimal string. Either numbers or strings are accepted.
449
- case ScalarType.UINT64:
450
- case ScalarType.FIXED64:
451
- case ScalarType.INT64:
452
- case ScalarType.SFIXED64:
453
- case ScalarType.SINT64:
454
- assert(
455
- typeof value == "bigint" ||
456
- typeof value == "string" ||
457
- typeof value == "number"
458
- );
459
- // We use implicit conversion with `value != 0` to catch both 0n and "0"
460
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
461
- // @ts-ignore
462
- return emitIntrinsicDefault || value != 0
463
- ? value.toString(10)
464
- : undefined;
465
- // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
466
- // Either standard or URL-safe base64 encoding with/without paddings are accepted.
467
- case ScalarType.BYTES:
468
- assert(value instanceof Uint8Array);
469
- return emitIntrinsicDefault || value.byteLength > 0
470
- ? base64encode(value)
471
- : undefined;
472
- }
409
+ if (value === undefined) {
410
+ return undefined;
411
+ }
412
+ switch (type) {
413
+ // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
414
+ case ScalarType.INT32:
415
+ case ScalarType.SFIXED32:
416
+ case ScalarType.SINT32:
417
+ case ScalarType.FIXED32:
418
+ case ScalarType.UINT32:
419
+ assert(typeof value == "number");
420
+ return value != 0 || emitIntrinsicDefault ? value : undefined;
421
+ // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
422
+ // Either numbers or strings are accepted. Exponent notation is also accepted.
423
+ case ScalarType.FLOAT:
424
+ // assertFloat32(value);
425
+ case ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
426
+ assert(typeof value == "number");
427
+ if (Number.isNaN(value))
428
+ return "NaN";
429
+ if (value === Number.POSITIVE_INFINITY)
430
+ return "Infinity";
431
+ if (value === Number.NEGATIVE_INFINITY)
432
+ return "-Infinity";
433
+ return value !== 0 || emitIntrinsicDefault ? value : undefined;
434
+ // string:
435
+ case ScalarType.STRING:
436
+ assert(typeof value == "string");
437
+ return value.length > 0 || emitIntrinsicDefault ? value : undefined;
438
+ // bool:
439
+ case ScalarType.BOOL:
440
+ assert(typeof value == "boolean");
441
+ return value || emitIntrinsicDefault ? value : undefined;
442
+ // JSON value will be a decimal string. Either numbers or strings are accepted.
443
+ case ScalarType.UINT64:
444
+ case ScalarType.FIXED64:
445
+ case ScalarType.INT64:
446
+ case ScalarType.SFIXED64:
447
+ case ScalarType.SINT64:
448
+ assert(typeof value == "bigint" ||
449
+ typeof value == "string" ||
450
+ typeof value == "number");
451
+ // We use implicit conversion with `value != 0` to catch both 0n and "0"
452
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
453
+ // @ts-ignore
454
+ return emitIntrinsicDefault || value != 0
455
+ ? value.toString(10)
456
+ : undefined;
457
+ // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
458
+ // Either standard or URL-safe base64 encoding with/without paddings are accepted.
459
+ case ScalarType.BYTES:
460
+ assert(value instanceof Uint8Array);
461
+ return emitIntrinsicDefault || value.byteLength > 0
462
+ ? base64encode(value)
463
+ : undefined;
464
+ }
473
465
  }