@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.1

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