@bufbuild/protobuf 0.0.10 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/cjs/create-registry-from-desc.js +2 -112
  2. package/dist/cjs/create-registry.js +1 -1
  3. package/dist/cjs/google/protobuf/descriptor_pb.js +2 -2
  4. package/dist/cjs/google/protobuf/empty_pb.js +0 -1
  5. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  6. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  7. package/dist/cjs/google/varint.js +85 -37
  8. package/dist/cjs/index.js +1 -7
  9. package/dist/cjs/private/json-format-common.js +6 -6
  10. package/dist/cjs/proto-int64.js +23 -40
  11. package/dist/esm/create-registry-from-desc.js +1 -110
  12. package/dist/esm/create-registry.js +1 -1
  13. package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
  14. package/dist/esm/google/protobuf/empty_pb.js +0 -1
  15. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  16. package/dist/esm/google/protobuf/type_pb.js +1 -1
  17. package/dist/esm/google/varint.js +81 -34
  18. package/dist/esm/index.js +0 -3
  19. package/dist/esm/private/json-format-common.js +6 -6
  20. package/dist/esm/proto-int64.js +24 -41
  21. package/dist/types/create-registry-from-desc.d.ts +0 -34
  22. package/dist/types/create-registry.d.ts +1 -1
  23. package/dist/types/google/protobuf/descriptor_pb.d.ts +6 -6
  24. package/dist/types/google/protobuf/empty_pb.d.ts +0 -1
  25. package/dist/types/google/varint.d.ts +21 -9
  26. package/dist/types/index.d.ts +0 -3
  27. package/dist/types/message.d.ts +3 -1
  28. package/dist/types/proto-int64.d.ts +14 -13
  29. package/package.json +5 -9
  30. package/dist/cjs/legacy-descriptor-registry.js +0 -468
  31. package/dist/cjs/legacy-descriptor-set.js +0 -453
  32. package/dist/cjs/legacy-type-registry.js +0 -104
  33. package/dist/esm/legacy-descriptor-registry.js +0 -464
  34. package/dist/esm/legacy-descriptor-set.js +0 -449
  35. package/dist/esm/legacy-type-registry.js +0 -100
  36. package/dist/types/legacy-descriptor-registry.d.ts +0 -45
  37. package/dist/types/legacy-descriptor-set.d.ts +0 -152
  38. package/dist/types/legacy-type-registry.d.ts +0 -44
@@ -1,468 +0,0 @@
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.
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.LegacyDescriptorRegistry = void 0;
17
- const descriptor_pb_js_1 = require("./google/protobuf/descriptor_pb.js");
18
- const assert_js_1 = require("./private/assert.js");
19
- const proto3_js_1 = require("./proto3.js");
20
- const proto2_js_1 = require("./proto2.js");
21
- const field_js_1 = require("./field.js");
22
- const legacy_descriptor_set_js_1 = require("./legacy-descriptor-set.js");
23
- const proto_int64_js_1 = require("./proto-int64.js");
24
- const timestamp_pb_js_1 = require("./google/protobuf/timestamp_pb.js");
25
- const duration_pb_js_1 = require("./google/protobuf/duration_pb.js");
26
- const any_pb_js_1 = require("./google/protobuf/any_pb.js");
27
- const empty_pb_js_1 = require("./google/protobuf/empty_pb.js");
28
- const field_mask_pb_js_1 = require("./google/protobuf/field_mask_pb.js");
29
- const struct_pb_js_1 = require("./google/protobuf/struct_pb.js");
30
- const enum_js_1 = require("./private/enum.js");
31
- const wrappers_pb_js_1 = require("./google/protobuf/wrappers_pb.js");
32
- // well-known message types with specialized JSON representation
33
- const wkMessages = [
34
- any_pb_js_1.Any,
35
- duration_pb_js_1.Duration,
36
- empty_pb_js_1.Empty,
37
- field_mask_pb_js_1.FieldMask,
38
- struct_pb_js_1.Struct,
39
- struct_pb_js_1.Value,
40
- struct_pb_js_1.ListValue,
41
- timestamp_pb_js_1.Timestamp,
42
- duration_pb_js_1.Duration,
43
- wrappers_pb_js_1.DoubleValue,
44
- wrappers_pb_js_1.FloatValue,
45
- wrappers_pb_js_1.Int64Value,
46
- wrappers_pb_js_1.Int32Value,
47
- wrappers_pb_js_1.UInt32Value,
48
- wrappers_pb_js_1.UInt64Value,
49
- wrappers_pb_js_1.BoolValue,
50
- wrappers_pb_js_1.StringValue,
51
- wrappers_pb_js_1.BytesValue,
52
- ];
53
- // well-known enum types with specialized JSON representation
54
- const wkEnums = [(0, enum_js_1.getEnumType)(struct_pb_js_1.NullValue)];
55
- /**
56
- * LegacyDescriptorRegistry is a type registry that dynamically creates types
57
- * from a set of google.protobuf.FileDescriptorProto.
58
- *
59
- * By default, all well-known types with a specialized JSON representation
60
- * are replaced with their generated counterpart in this package.
61
- *
62
- * @deprecated
63
- */
64
- class LegacyDescriptorRegistry {
65
- constructor(descriptorSet, replaceWkt = true) {
66
- this.enums = {};
67
- this.messages = {};
68
- this.services = {};
69
- this.ds = descriptorSet !== null && descriptorSet !== void 0 ? descriptorSet : new legacy_descriptor_set_js_1.LegacyDescriptorSet();
70
- if (replaceWkt) {
71
- for (const mt of wkMessages) {
72
- this.messages["." + mt.typeName] = mt;
73
- }
74
- for (const et of wkEnums) {
75
- this.enums["." + et.typeName] = et;
76
- }
77
- }
78
- }
79
- /**
80
- * Conveniently create a DescriptorRegistry from a FileDescriptorSet
81
- * instance or a FileDescriptorSet in binary format.
82
- */
83
- static fromFileDescriptorSet(bytesOrSet) {
84
- const set = bytesOrSet instanceof Uint8Array
85
- ? descriptor_pb_js_1.FileDescriptorSet.fromBinary(bytesOrSet)
86
- : new descriptor_pb_js_1.FileDescriptorSet(bytesOrSet);
87
- const dr = new LegacyDescriptorRegistry();
88
- dr.add(...set.file);
89
- return dr;
90
- }
91
- /**
92
- * May raise an error on invalid descriptors.
93
- */
94
- add(...files) {
95
- this.ds.add(...files);
96
- }
97
- /**
98
- * May raise an error on invalid descriptors.
99
- */
100
- findEnum(typeName) {
101
- const protoTypeName = "." + typeName;
102
- const existing = this.enums[protoTypeName];
103
- if (existing) {
104
- return existing;
105
- }
106
- const raw = this.ds.enums[protoTypeName];
107
- if (!raw) {
108
- return undefined;
109
- }
110
- const runtime = raw.file.syntax == "proto3" ? proto3_js_1.proto3 : proto2_js_1.proto2;
111
- const type = runtime.makeEnumType(typeName, raw.values.map((u) => ({
112
- no: u.number,
113
- name: u.name,
114
- localName: u.name,
115
- })), {});
116
- this.enums[protoTypeName] = type;
117
- return type;
118
- }
119
- /**
120
- * May raise an error on invalid descriptors.
121
- */
122
- findMessage(typeName) {
123
- const protoTypeName = "." + typeName;
124
- const existing = this.messages[protoTypeName];
125
- if (existing) {
126
- return existing;
127
- }
128
- const raw = this.ds.messages[protoTypeName];
129
- if (!raw) {
130
- return undefined;
131
- }
132
- const runtime = raw.file.syntax == "proto3" ? proto3_js_1.proto3 : proto2_js_1.proto2;
133
- const fields = [];
134
- const type = runtime.makeMessageType(typeName, () => fields, {
135
- localName: makeTypeLocalName(raw),
136
- });
137
- this.messages[protoTypeName] = type;
138
- for (const field of raw.fields.map((f) => f.resolve(this.ds))) {
139
- const fieldInfo = makeFieldInfo(field, this);
140
- fields.push(fieldInfo);
141
- }
142
- return type;
143
- }
144
- /**
145
- * May raise an error on invalid descriptors.
146
- */
147
- findService(typeName) {
148
- const protoTypeName = "." + typeName;
149
- const existing = this.services[protoTypeName];
150
- if (existing) {
151
- return existing;
152
- }
153
- const raw = this.ds.services[protoTypeName];
154
- if (!raw) {
155
- return undefined;
156
- }
157
- const methods = {};
158
- for (const u of raw.methods) {
159
- const it = this.findMessage(u.inputTypeName);
160
- const ot = this.findMessage(u.outputTypeName);
161
- (0, assert_js_1.assert)(it, `message "${u.inputTypeName}" for ${u.toString()} not found`);
162
- (0, assert_js_1.assert)(ot, `output message "${u.outputTypeName}" for ${u.toString()} not found`);
163
- const m = {
164
- name: u.name,
165
- localName: localMethodName(u.name),
166
- I: it,
167
- O: ot,
168
- kind: u.kind,
169
- idempotency: u.idempotency,
170
- options: {},
171
- };
172
- methods[m.localName] = m;
173
- }
174
- return (this.services[protoTypeName] = {
175
- typeName: raw.typeName,
176
- methods,
177
- });
178
- }
179
- }
180
- exports.LegacyDescriptorRegistry = LegacyDescriptorRegistry;
181
- function localMethodName(protoName) {
182
- if (protoName.length == 0) {
183
- return protoName;
184
- }
185
- return protoName[0].toLowerCase() + protoName.substring(1);
186
- }
187
- function makeTypeLocalName(type) {
188
- var _a;
189
- const typeName = type.typeName;
190
- const packagePrefix = ((_a = type.file.proto.package) !== null && _a !== void 0 ? _a : "") + ".";
191
- if (!typeName.startsWith(packagePrefix)) {
192
- return type.name;
193
- }
194
- return typeName.substring(packagePrefix.length).split(".").join("_");
195
- }
196
- function makeFieldInfo(field, resolver) {
197
- if (field.map !== undefined) {
198
- return makeMapFieldInfo(field, resolver);
199
- }
200
- if (field.message) {
201
- return makeMessageFieldInfo(field, resolver);
202
- }
203
- const fi = field.enum
204
- ? makeEnumFieldInfo(field, resolver)
205
- : makeScalarFieldInfo(field);
206
- fi.default = parseDefaultValue(field);
207
- return fi;
208
- }
209
- function makeMapFieldInfo(field, resolver) {
210
- const base = {
211
- kind: "map",
212
- name: field.name,
213
- no: field.number,
214
- K: field.map.key,
215
- };
216
- if (field.map.value.message) {
217
- const messageType = resolver.findMessage(field.map.value.message.typeName);
218
- (0, assert_js_1.assert)(messageType, `message "${field.map.value.message.typeName}" for ${field.toString()} not found`);
219
- return Object.assign(Object.assign({}, base), { V: {
220
- kind: "message",
221
- T: messageType,
222
- } });
223
- }
224
- if (field.map.value.enum) {
225
- const enumType = resolver.findEnum(field.map.value.enum.typeName);
226
- (0, assert_js_1.assert)(enumType, `enum "${field.map.value.enum.typeName}" for ${field.toString()} not found`);
227
- return Object.assign(Object.assign({}, base), { V: {
228
- kind: "enum",
229
- T: enumType,
230
- } });
231
- }
232
- return Object.assign(Object.assign({}, base), { V: {
233
- kind: "scalar",
234
- T: field.map.value.scalarType,
235
- } });
236
- }
237
- function makeScalarFieldInfo(field) {
238
- const base = {
239
- no: field.number,
240
- name: field.name,
241
- kind: "scalar",
242
- T: field.scalarType,
243
- };
244
- if (field.repeated) {
245
- return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined, T: field.scalarType });
246
- }
247
- if (field.oneof) {
248
- return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
249
- }
250
- if (field.optional) {
251
- return Object.assign(Object.assign({}, base), { opt: true });
252
- }
253
- return base;
254
- }
255
- function makeMessageFieldInfo(field, resolver) {
256
- const messageType = resolver.findMessage(field.message.typeName);
257
- (0, assert_js_1.assert)(messageType, `message "${field.message.typeName}" for ${field.toString()} not found`);
258
- const base = {
259
- no: field.number,
260
- name: field.name,
261
- kind: "message",
262
- T: messageType,
263
- };
264
- if (field.repeated) {
265
- return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
266
- }
267
- if (field.oneof) {
268
- return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
269
- }
270
- if (field.optional) {
271
- return Object.assign(Object.assign({}, base), { opt: true });
272
- }
273
- return base;
274
- }
275
- function makeEnumFieldInfo(field, resolver) {
276
- const enumType = resolver.findEnum(field.enum.typeName);
277
- (0, assert_js_1.assert)(enumType, `message "${field.enum.typeName}" for ${field.toString()} not found`);
278
- const base = {
279
- no: field.number,
280
- name: field.name,
281
- kind: "enum",
282
- T: enumType,
283
- };
284
- if (field.repeated) {
285
- return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
286
- }
287
- if (field.oneof) {
288
- return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
289
- }
290
- if (field.optional) {
291
- return Object.assign(Object.assign({}, base), { opt: true });
292
- }
293
- return base;
294
- }
295
- function parseDefaultValue(field) {
296
- const d = field.proto.defaultValue;
297
- if (d === undefined) {
298
- return undefined;
299
- }
300
- if (field.enum) {
301
- const enumValue = field.enum.values.find((v) => v.name === d);
302
- (0, assert_js_1.assert)(enumValue, `cannot parse ${field.toString()} default value: ${d}`);
303
- return enumValue.number;
304
- }
305
- if (field.scalarType) {
306
- switch (field.scalarType) {
307
- case field_js_1.ScalarType.STRING:
308
- return d;
309
- case field_js_1.ScalarType.BYTES: {
310
- const u = unescapeBytesDefaultValue(d);
311
- if (u === false) {
312
- throw new Error(`cannot parse ${field.toString()} default value: ${d}`);
313
- }
314
- return u;
315
- }
316
- case field_js_1.ScalarType.INT64:
317
- case field_js_1.ScalarType.SFIXED64:
318
- case field_js_1.ScalarType.SINT64:
319
- return proto_int64_js_1.protoInt64.parse(d);
320
- case field_js_1.ScalarType.UINT64:
321
- case field_js_1.ScalarType.FIXED64:
322
- return proto_int64_js_1.protoInt64.uParse(d);
323
- case field_js_1.ScalarType.DOUBLE:
324
- case field_js_1.ScalarType.FLOAT:
325
- switch (d) {
326
- case "inf":
327
- return Number.POSITIVE_INFINITY;
328
- case "-inf":
329
- return Number.NEGATIVE_INFINITY;
330
- case "nan":
331
- return Number.NaN;
332
- default:
333
- return parseFloat(d);
334
- }
335
- case field_js_1.ScalarType.BOOL:
336
- return Boolean(d);
337
- case field_js_1.ScalarType.INT32:
338
- case field_js_1.ScalarType.UINT32:
339
- case field_js_1.ScalarType.SINT32:
340
- case field_js_1.ScalarType.FIXED32:
341
- case field_js_1.ScalarType.SFIXED32:
342
- return parseInt(d, 10);
343
- }
344
- }
345
- return undefined;
346
- }
347
- // unescapeBytesDefaultValue parses a text-encoded default value (proto2) of a
348
- // BYTES field.
349
- function unescapeBytesDefaultValue(str) {
350
- const b = [];
351
- const input = {
352
- tail: str,
353
- c: "",
354
- next() {
355
- if (this.tail.length == 0) {
356
- return false;
357
- }
358
- this.c = this.tail[0];
359
- this.tail = this.tail.substring(1);
360
- return true;
361
- },
362
- take(n) {
363
- if (this.tail.length >= n) {
364
- const r = this.tail.substring(0, n);
365
- this.tail = this.tail.substring(n);
366
- return r;
367
- }
368
- return false;
369
- },
370
- };
371
- while (input.next()) {
372
- switch (input.c) {
373
- case "\\":
374
- if (input.next()) {
375
- switch (input.c) {
376
- case "\\":
377
- b.push(input.c.charCodeAt(0));
378
- break;
379
- case "b":
380
- b.push(0x08);
381
- break;
382
- case "f":
383
- b.push(0x0c);
384
- break;
385
- case "n":
386
- b.push(0x0a);
387
- break;
388
- case "r":
389
- b.push(0x0d);
390
- break;
391
- case "t":
392
- b.push(0x09);
393
- break;
394
- case "v":
395
- b.push(0x0b);
396
- break;
397
- case "0":
398
- case "1":
399
- case "2":
400
- case "3":
401
- case "4":
402
- case "5":
403
- case "6":
404
- case "7": {
405
- const s = input.c;
406
- const t = input.take(2);
407
- if (t === false) {
408
- return false;
409
- }
410
- const n = parseInt(s + t, 8);
411
- if (isNaN(n)) {
412
- return false;
413
- }
414
- b.push(n);
415
- break;
416
- }
417
- case "x": {
418
- const s = input.c;
419
- const t = input.take(2);
420
- if (t === false) {
421
- return false;
422
- }
423
- const n = parseInt(s + t, 16);
424
- if (isNaN(n)) {
425
- return false;
426
- }
427
- b.push(n);
428
- break;
429
- }
430
- case "u": {
431
- const s = input.c;
432
- const t = input.take(4);
433
- if (t === false) {
434
- return false;
435
- }
436
- const n = parseInt(s + t, 16);
437
- if (isNaN(n)) {
438
- return false;
439
- }
440
- const chunk = new Uint8Array(4);
441
- const view = new DataView(chunk.buffer);
442
- view.setInt32(0, n, true);
443
- b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
444
- break;
445
- }
446
- case "U": {
447
- const s = input.c;
448
- const t = input.take(8);
449
- if (t === false) {
450
- return false;
451
- }
452
- const tc = proto_int64_js_1.protoInt64.uEnc(s + t);
453
- const chunk = new Uint8Array(8);
454
- const view = new DataView(chunk.buffer);
455
- view.setInt32(0, tc.lo, true);
456
- view.setInt32(4, tc.hi, true);
457
- b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
458
- break;
459
- }
460
- }
461
- }
462
- break;
463
- default:
464
- b.push(input.c.charCodeAt(0));
465
- }
466
- }
467
- return new Uint8Array(b);
468
- }