@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,4 +1,17 @@
1
1
  "use strict";
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
2
15
  Object.defineProperty(exports, "__esModule", { value: true });
3
16
  exports.makeUtilCommon = void 0;
4
17
  const enum_js_1 = require("./enum.js");
@@ -6,224 +19,194 @@ const field_js_1 = require("../field.js");
6
19
  const scalars_js_1 = require("./scalars.js");
7
20
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument,no-case-declarations */
8
21
  function makeUtilCommon() {
9
- return {
10
- setEnumType: enum_js_1.setEnumType,
11
- initPartial(source, target) {
12
- if (source === undefined) {
13
- return;
14
- }
15
- const type = target.getType();
16
- for (const member of type.fields.byMember()) {
17
- const localName = member.localName,
18
- t = target,
19
- s = source;
20
- if (s[localName] === undefined) {
21
- continue;
22
- }
23
- switch (member.kind) {
24
- case "oneof":
25
- const sk = s[localName].case;
26
- if (sk === undefined) {
27
- continue;
22
+ return {
23
+ setEnumType: enum_js_1.setEnumType,
24
+ initPartial(source, target) {
25
+ if (source === undefined) {
26
+ return;
28
27
  }
29
- const sourceField = member.findField(sk);
30
- let val = s[localName].value;
31
- if (
32
- sourceField &&
33
- sourceField.kind == "message" &&
34
- !(val instanceof sourceField.T)
35
- ) {
36
- val = new sourceField.T(val);
37
- }
38
- t[localName] = { case: sk, value: val };
39
- break;
40
- case "scalar":
41
- case "enum":
42
- t[localName] = s[localName];
43
- break;
44
- case "map":
45
- switch (member.V.kind) {
46
- case "scalar":
47
- case "enum":
48
- Object.assign(t[localName], s[localName]);
49
- break;
50
- case "message":
51
- const messageType = member.V.T;
52
- for (const k of Object.keys(s[localName])) {
53
- let val = s[localName][k];
54
- if (!messageType.fieldWrapper) {
55
- // We only take partial input for messages that are not a wrapper type.
56
- // For those messages, we recursively normalize the partial input.
57
- val = new messageType(val);
58
- }
59
- t[localName][k] = val;
28
+ const type = target.getType();
29
+ for (const member of type.fields.byMember()) {
30
+ const localName = member.localName, t = target, s = source;
31
+ if (s[localName] === undefined) {
32
+ continue;
33
+ }
34
+ switch (member.kind) {
35
+ case "oneof":
36
+ const sk = s[localName].case;
37
+ if (sk === undefined) {
38
+ continue;
39
+ }
40
+ const sourceField = member.findField(sk);
41
+ let val = s[localName].value;
42
+ if (sourceField &&
43
+ sourceField.kind == "message" &&
44
+ !(val instanceof sourceField.T)) {
45
+ val = new sourceField.T(val);
46
+ }
47
+ t[localName] = { case: sk, value: val };
48
+ break;
49
+ case "scalar":
50
+ case "enum":
51
+ t[localName] = s[localName];
52
+ break;
53
+ case "map":
54
+ switch (member.V.kind) {
55
+ case "scalar":
56
+ case "enum":
57
+ Object.assign(t[localName], s[localName]);
58
+ break;
59
+ case "message":
60
+ const messageType = member.V.T;
61
+ for (const k of Object.keys(s[localName])) {
62
+ let val = s[localName][k];
63
+ if (!messageType.fieldWrapper) {
64
+ // We only take partial input for messages that are not a wrapper type.
65
+ // For those messages, we recursively normalize the partial input.
66
+ val = new messageType(val);
67
+ }
68
+ t[localName][k] = val;
69
+ }
70
+ break;
71
+ }
72
+ break;
73
+ case "message":
74
+ const mt = member.T;
75
+ if (member.repeated) {
76
+ t[localName] = s[localName].map((val) => val instanceof mt ? val : new mt(val));
77
+ }
78
+ else if (s[localName] !== undefined) {
79
+ const val = s[localName];
80
+ if (mt.fieldWrapper) {
81
+ t[localName] = val;
82
+ }
83
+ else {
84
+ t[localName] = val instanceof mt ? mt : new mt(val);
85
+ }
86
+ }
87
+ break;
60
88
  }
61
- break;
62
- }
63
- break;
64
- case "message":
65
- const mt = member.T;
66
- if (member.repeated) {
67
- t[localName] = s[localName].map((val) =>
68
- val instanceof mt ? val : new mt(val)
69
- );
70
- } else if (s[localName] !== undefined) {
71
- const val = s[localName];
72
- if (mt.fieldWrapper) {
73
- t[localName] = val;
74
- } else {
75
- t[localName] = val instanceof mt ? mt : new mt(val);
76
- }
77
- }
78
- break;
79
- }
80
- }
81
- },
82
- equals(type, a, b) {
83
- if (a === b) {
84
- return true;
85
- }
86
- if (!a || !b) {
87
- return false;
88
- }
89
- return type.fields.byMember().every((m) => {
90
- const va = a[m.localName];
91
- const vb = b[m.localName];
92
- if (m.repeated) {
93
- if (va.length !== vb.length) {
94
- return false;
95
- }
96
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- repeated fields are never "map"
97
- switch (m.kind) {
98
- case "message":
99
- return va.every((a, i) => m.T.equals(a, vb[i]));
100
- case "scalar":
101
- return va.every((a, i) =>
102
- (0, scalars_js_1.scalarEquals)(m.T, a, vb[i])
103
- );
104
- case "enum":
105
- return va.every((a, i) =>
106
- (0, scalars_js_1.scalarEquals)(
107
- field_js_1.ScalarType.INT32,
108
- a,
109
- vb[i]
110
- )
111
- );
112
- }
113
- throw new Error(`repeated cannot contain ${m.kind}`);
114
- }
115
- switch (m.kind) {
116
- case "message":
117
- return m.T.equals(va, vb);
118
- case "enum":
119
- return (0, scalars_js_1.scalarEquals)(
120
- field_js_1.ScalarType.INT32,
121
- va,
122
- vb
123
- );
124
- case "scalar":
125
- return (0, scalars_js_1.scalarEquals)(m.T, va, vb);
126
- case "oneof":
127
- if (va.case !== vb.case) {
128
- return false;
129
- }
130
- const k = va.case,
131
- s = m.findField(k);
132
- if (s === undefined) {
133
- return true;
134
89
  }
135
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- oneof fields are never "map"
136
- switch (s.kind) {
137
- case "message":
138
- return s.T.equals(va[k], vb[k]);
139
- case "enum":
140
- return (0, scalars_js_1.scalarEquals)(
141
- field_js_1.ScalarType.INT32,
142
- va,
143
- vb
144
- );
145
- case "scalar":
146
- return (0, scalars_js_1.scalarEquals)(s.T, va, vb);
90
+ },
91
+ equals(type, a, b) {
92
+ if (a === b) {
93
+ return true;
147
94
  }
148
- throw new Error(`oneof cannot contain ${s.kind}`);
149
- case "map":
150
- const keys = Object.keys(va);
151
- if (keys.some((k) => vb[k] === undefined)) {
152
- return false;
95
+ if (!a || !b) {
96
+ return false;
153
97
  }
154
- switch (m.V.kind) {
155
- case "message":
156
- const messageType = m.V.T;
157
- return keys.every((k) => messageType.equals(va[k], vb[k]));
158
- case "enum":
159
- return keys.every((k) =>
160
- (0, scalars_js_1.scalarEquals)(
161
- field_js_1.ScalarType.INT32,
162
- va[k],
163
- vb[k]
164
- )
165
- );
166
- case "scalar":
167
- const scalarType = m.V.T;
168
- return keys.every((k) =>
169
- (0, scalars_js_1.scalarEquals)(scalarType, va[k], vb[k])
170
- );
98
+ return type.fields.byMember().every((m) => {
99
+ const va = a[m.localName];
100
+ const vb = b[m.localName];
101
+ if (m.repeated) {
102
+ if (va.length !== vb.length) {
103
+ return false;
104
+ }
105
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- repeated fields are never "map"
106
+ switch (m.kind) {
107
+ case "message":
108
+ return va.every((a, i) => m.T.equals(a, vb[i]));
109
+ case "scalar":
110
+ return va.every((a, i) => (0, scalars_js_1.scalarEquals)(m.T, a, vb[i]));
111
+ case "enum":
112
+ return va.every((a, i) => (0, scalars_js_1.scalarEquals)(field_js_1.ScalarType.INT32, a, vb[i]));
113
+ }
114
+ throw new Error(`repeated cannot contain ${m.kind}`);
115
+ }
116
+ switch (m.kind) {
117
+ case "message":
118
+ return m.T.equals(va, vb);
119
+ case "enum":
120
+ return (0, scalars_js_1.scalarEquals)(field_js_1.ScalarType.INT32, va, vb);
121
+ case "scalar":
122
+ return (0, scalars_js_1.scalarEquals)(m.T, va, vb);
123
+ case "oneof":
124
+ if (va.case !== vb.case) {
125
+ return false;
126
+ }
127
+ const k = va.case, s = m.findField(k);
128
+ if (s === undefined) {
129
+ return true;
130
+ }
131
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- oneof fields are never "map"
132
+ switch (s.kind) {
133
+ case "message":
134
+ return s.T.equals(va[k], vb[k]);
135
+ case "enum":
136
+ return (0, scalars_js_1.scalarEquals)(field_js_1.ScalarType.INT32, va, vb);
137
+ case "scalar":
138
+ return (0, scalars_js_1.scalarEquals)(s.T, va, vb);
139
+ }
140
+ throw new Error(`oneof cannot contain ${s.kind}`);
141
+ case "map":
142
+ const keys = Object.keys(va);
143
+ if (keys.some((k) => vb[k] === undefined)) {
144
+ return false;
145
+ }
146
+ switch (m.V.kind) {
147
+ case "message":
148
+ const messageType = m.V.T;
149
+ return keys.every((k) => messageType.equals(va[k], vb[k]));
150
+ case "enum":
151
+ return keys.every((k) => (0, scalars_js_1.scalarEquals)(field_js_1.ScalarType.INT32, va[k], vb[k]));
152
+ case "scalar":
153
+ const scalarType = m.V.T;
154
+ return keys.every((k) => (0, scalars_js_1.scalarEquals)(scalarType, va[k], vb[k]));
155
+ }
156
+ break;
157
+ }
158
+ });
159
+ },
160
+ clone(message) {
161
+ const type = message.getType(), target = new type(), any = target;
162
+ for (const member of type.fields.byMember()) {
163
+ const source = message[member.localName];
164
+ let copy;
165
+ if (member.repeated) {
166
+ copy = source.map((e) => cloneSingularField(member, e));
167
+ }
168
+ else if (member.kind == "map") {
169
+ copy = any[member.localName];
170
+ for (const [key, v] of Object.entries(source)) {
171
+ copy[key] = cloneSingularField(member.V, v);
172
+ }
173
+ }
174
+ else if (member.kind == "oneof") {
175
+ const f = member.findField(source.case);
176
+ copy = f
177
+ ? { case: source.case, value: cloneSingularField(f, source.value) }
178
+ : { case: undefined };
179
+ }
180
+ else {
181
+ copy = cloneSingularField(member, source);
182
+ }
183
+ any[member.localName] = copy;
171
184
  }
172
- break;
173
- }
174
- });
175
- },
176
- clone(message) {
177
- const type = message.getType(),
178
- target = new type(),
179
- any = target;
180
- for (const member of type.fields.byMember()) {
181
- const source = message[member.localName];
182
- let copy;
183
- if (member.repeated) {
184
- copy = source.map((e) => cloneSingularField(member, e));
185
- } else if (member.kind == "map") {
186
- copy = any[member.localName];
187
- for (const [key, v] of Object.entries(source)) {
188
- copy[key] = cloneSingularField(member.V, v);
189
- }
190
- } else if (member.kind == "oneof") {
191
- const f = member.findField(source.case);
192
- copy = f
193
- ? { case: source.case, value: cloneSingularField(f, source.value) }
194
- : { case: undefined };
195
- } else {
196
- copy = cloneSingularField(member, source);
197
- }
198
- any[member.localName] = copy;
199
- }
200
- return target;
201
- },
202
- };
185
+ return target;
186
+ },
187
+ };
203
188
  }
204
189
  exports.makeUtilCommon = makeUtilCommon;
205
190
  // clone a single field value - i.e. the element type of repeated fields, the value type of maps
206
191
  function cloneSingularField(field, value) {
207
- if (value === undefined) {
208
- return value;
209
- }
210
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- unmatched "map" is unsupported
211
- switch (field.kind) {
212
- case "enum":
213
- return value;
214
- case "scalar":
215
- if (field.T === field_js_1.ScalarType.BYTES) {
216
- const c = new Uint8Array(value.byteLength);
217
- c.set(value);
218
- return c;
219
- }
220
- return value;
221
- case "message":
222
- if (field.T.fieldWrapper) {
223
- return field.T.fieldWrapper.unwrapField(
224
- field.T.fieldWrapper.wrapField(value).clone()
225
- );
226
- }
227
- return value.clone();
228
- }
192
+ if (value === undefined) {
193
+ return value;
194
+ }
195
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- unmatched "map" is unsupported
196
+ switch (field.kind) {
197
+ case "enum":
198
+ return value;
199
+ case "scalar":
200
+ if (field.T === field_js_1.ScalarType.BYTES) {
201
+ const c = new Uint8Array(value.byteLength);
202
+ c.set(value);
203
+ return c;
204
+ }
205
+ return value;
206
+ case "message":
207
+ if (field.T.fieldWrapper) {
208
+ return field.T.fieldWrapper.unwrapField(field.T.fieldWrapper.wrapField(value).clone());
209
+ }
210
+ return value.clone();
211
+ }
229
212
  }
@@ -1,2 +1,15 @@
1
1
  "use strict";
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
2
15
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,121 +1,133 @@
1
1
  "use strict";
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
2
15
  Object.defineProperty(exports, "__esModule", { value: true });
3
16
  exports.protoInt64 = void 0;
4
17
  const varint_js_1 = require("./google/varint.js");
5
18
  function makeInt64Support() {
6
- const dv = new DataView(new ArrayBuffer(8));
7
- // note that Safari 14 implements BigInt, but not the DataView methods
8
- const ok =
9
- globalThis.BigInt !== undefined && // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- conditional for BigInt is very much necessary
10
- typeof dv.getBigInt64 === "function" &&
11
- typeof dv.getBigUint64 === "function" &&
12
- typeof dv.setBigInt64 === "function" &&
13
- typeof dv.setBigUint64 === "function";
14
- if (ok) {
15
- const MIN = BigInt("-9223372036854775808"),
16
- MAX = BigInt("9223372036854775807"),
17
- UMIN = BigInt("0"),
18
- UMAX = BigInt("18446744073709551615");
19
- return {
20
- zero: BigInt(0),
21
- supported: true,
22
- parse(value) {
23
- const bi = typeof value == "bigint" ? value : BigInt(value);
24
- if (bi > MAX || bi < MIN) {
25
- throw new Error(`int64 invalid: ${value}`);
26
- }
27
- return bi;
28
- },
29
- uParse(value) {
30
- const bi = typeof value == "bigint" ? value : BigInt(value);
31
- if (bi > UMAX || bi < UMIN) {
32
- throw new Error(`uint64 invalid: ${value}`);
33
- }
34
- return bi;
35
- },
36
- enc(value) {
37
- dv.setBigInt64(0, this.parse(value), true);
38
- return {
39
- lo: dv.getInt32(0, true),
40
- hi: dv.getInt32(4, true),
41
- };
42
- },
43
- uEnc(value) {
44
- dv.setBigInt64(0, this.uParse(value), true);
19
+ const dv = new DataView(new ArrayBuffer(8));
20
+ // note that Safari 14 implements BigInt, but not the DataView methods
21
+ const ok = globalThis.BigInt !== undefined && // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- conditional for BigInt is very much necessary
22
+ typeof dv.getBigInt64 === "function" &&
23
+ typeof dv.getBigUint64 === "function" &&
24
+ typeof dv.setBigInt64 === "function" &&
25
+ typeof dv.setBigUint64 === "function";
26
+ if (ok) {
27
+ const MIN = BigInt("-9223372036854775808"), MAX = BigInt("9223372036854775807"), UMIN = BigInt("0"), UMAX = BigInt("18446744073709551615");
45
28
  return {
46
- lo: dv.getInt32(0, true),
47
- hi: dv.getInt32(4, true),
29
+ zero: BigInt(0),
30
+ supported: true,
31
+ parse(value) {
32
+ const bi = typeof value == "bigint" ? value : BigInt(value);
33
+ if (bi > MAX || bi < MIN) {
34
+ throw new Error(`int64 invalid: ${value}`);
35
+ }
36
+ return bi;
37
+ },
38
+ uParse(value) {
39
+ const bi = typeof value == "bigint" ? value : BigInt(value);
40
+ if (bi > UMAX || bi < UMIN) {
41
+ throw new Error(`uint64 invalid: ${value}`);
42
+ }
43
+ return bi;
44
+ },
45
+ enc(value) {
46
+ dv.setBigInt64(0, this.parse(value), true);
47
+ return {
48
+ lo: dv.getInt32(0, true),
49
+ hi: dv.getInt32(4, true),
50
+ };
51
+ },
52
+ uEnc(value) {
53
+ dv.setBigInt64(0, this.uParse(value), true);
54
+ return {
55
+ lo: dv.getInt32(0, true),
56
+ hi: dv.getInt32(4, true),
57
+ };
58
+ },
59
+ dec(lo, hi) {
60
+ dv.setInt32(0, lo, true);
61
+ dv.setInt32(4, hi, true);
62
+ return dv.getBigInt64(0, true);
63
+ },
64
+ uDec(lo, hi) {
65
+ dv.setInt32(0, lo, true);
66
+ dv.setInt32(4, hi, true);
67
+ return dv.getBigUint64(0, true);
68
+ },
48
69
  };
49
- },
50
- dec(lo, hi) {
51
- dv.setInt32(0, lo, true);
52
- dv.setInt32(4, hi, true);
53
- return dv.getBigInt64(0, true);
54
- },
55
- uDec(lo, hi) {
56
- dv.setInt32(0, lo, true);
57
- dv.setInt32(4, hi, true);
58
- return dv.getBigUint64(0, true);
59
- },
70
+ }
71
+ return {
72
+ zero: "0",
73
+ supported: false,
74
+ parse(value) {
75
+ if (!/^-?[0-9]+$/.test(value)) {
76
+ throw new Error(`int64 invalid: ${value}`);
77
+ }
78
+ return value;
79
+ },
80
+ uParse(value) {
81
+ if (!/^-?[0-9]+$/.test(value)) {
82
+ throw new Error(`uint64 invalid: ${value}`);
83
+ }
84
+ return value;
85
+ },
86
+ enc(value) {
87
+ if (typeof value == "string") {
88
+ if (!/^-?[0-9]+$/.test(value)) {
89
+ throw new Error(`int64 invalid: ${value}`);
90
+ }
91
+ }
92
+ else {
93
+ value = value.toString(10);
94
+ }
95
+ const [, lo, hi] = (0, varint_js_1.int64fromString)(value);
96
+ return { lo, hi };
97
+ },
98
+ uEnc(value) {
99
+ if (typeof value == "string") {
100
+ if (!/^-?[0-9]+$/.test(value)) {
101
+ throw new Error(`uint64 invalid: ${value}`);
102
+ }
103
+ }
104
+ else {
105
+ value = value.toString(10);
106
+ }
107
+ const [minus, lo, hi] = (0, varint_js_1.int64fromString)(value);
108
+ if (minus) {
109
+ throw new Error(`uint64 invalid: ${value}`);
110
+ }
111
+ return { lo, hi };
112
+ },
113
+ dec(lo, hi) {
114
+ const minus = (hi & 0x80000000) !== 0;
115
+ if (minus) {
116
+ // negate
117
+ hi = ~hi;
118
+ if (lo) {
119
+ lo = ~lo + 1;
120
+ }
121
+ else {
122
+ hi += 1;
123
+ }
124
+ return ("-" + (0, varint_js_1.int64toString)(lo, hi));
125
+ }
126
+ return (0, varint_js_1.int64toString)(lo, hi);
127
+ },
128
+ uDec(lo, hi) {
129
+ return (0, varint_js_1.int64toString)(lo, hi);
130
+ },
60
131
  };
61
- }
62
- return {
63
- zero: "0",
64
- supported: false,
65
- parse(value) {
66
- if (!/^-?[0-9]+$/.test(value)) {
67
- throw new Error(`int64 invalid: ${value}`);
68
- }
69
- return value;
70
- },
71
- uParse(value) {
72
- if (!/^-?[0-9]+$/.test(value)) {
73
- throw new Error(`uint64 invalid: ${value}`);
74
- }
75
- return value;
76
- },
77
- enc(value) {
78
- if (typeof value == "string") {
79
- if (!/^-?[0-9]+$/.test(value)) {
80
- throw new Error(`int64 invalid: ${value}`);
81
- }
82
- } else {
83
- value = value.toString(10);
84
- }
85
- const [, lo, hi] = (0, varint_js_1.int64fromString)(value);
86
- return { lo, hi };
87
- },
88
- uEnc(value) {
89
- if (typeof value == "string") {
90
- if (!/^-?[0-9]+$/.test(value)) {
91
- throw new Error(`uint64 invalid: ${value}`);
92
- }
93
- } else {
94
- value = value.toString(10);
95
- }
96
- const [minus, lo, hi] = (0, varint_js_1.int64fromString)(value);
97
- if (minus) {
98
- throw new Error(`uint64 invalid: ${value}`);
99
- }
100
- return { lo, hi };
101
- },
102
- dec(lo, hi) {
103
- const minus = (hi & 0x80000000) !== 0;
104
- if (minus) {
105
- // negate
106
- hi = ~hi;
107
- if (lo) {
108
- lo = ~lo + 1;
109
- } else {
110
- hi += 1;
111
- }
112
- return "-" + (0, varint_js_1.int64toString)(lo, hi);
113
- }
114
- return (0, varint_js_1.int64toString)(lo, hi);
115
- },
116
- uDec(lo, hi) {
117
- return (0, varint_js_1.int64toString)(lo, hi);
118
- },
119
- };
120
132
  }
121
133
  exports.protoInt64 = makeInt64Support();