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