@bufbuild/protobuf 1.7.2 → 1.8.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 (119) hide show
  1. package/dist/cjs/codegen-info.d.ts +6 -1
  2. package/dist/cjs/codegen-info.js +4 -3
  3. package/dist/cjs/create-descriptor-set.js +45 -33
  4. package/dist/cjs/create-registry-from-desc.js +60 -110
  5. package/dist/cjs/descriptor-set.d.ts +13 -9
  6. package/dist/cjs/field.d.ts +25 -51
  7. package/dist/cjs/field.js +0 -66
  8. package/dist/cjs/google/protobuf/compiler/plugin_pb.d.ts +18 -0
  9. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +2 -0
  10. package/dist/cjs/google/protobuf/descriptor_pb.d.ts +33 -33
  11. package/dist/cjs/google/protobuf/descriptor_pb.js +23 -10
  12. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  13. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  14. package/dist/cjs/google/protobuf/wrappers_pb.js +19 -19
  15. package/dist/cjs/index.d.ts +3 -1
  16. package/dist/cjs/index.js +6 -4
  17. package/dist/cjs/is-message.d.ts +22 -0
  18. package/dist/cjs/is-message.js +52 -0
  19. package/dist/cjs/json-format.d.ts +1 -2
  20. package/dist/cjs/private/binary-format.d.ts +7 -0
  21. package/dist/cjs/private/{binary-format-common.js → binary-format.js} +150 -53
  22. package/dist/cjs/private/extensions.js +1 -1
  23. package/dist/cjs/private/feature-set.js +1 -1
  24. package/dist/cjs/private/field-normalize.d.ts +9 -0
  25. package/dist/cjs/private/field-normalize.js +69 -0
  26. package/dist/cjs/private/field-wrapper.d.ts +1 -1
  27. package/dist/cjs/private/field-wrapper.js +12 -12
  28. package/dist/cjs/private/field.d.ts +1 -0
  29. package/dist/cjs/private/field.js +1 -0
  30. package/dist/cjs/private/json-format.d.ts +2 -0
  31. package/dist/cjs/private/{json-format-common.js → json-format.js} +261 -170
  32. package/dist/cjs/private/proto-runtime.d.ts +1 -1
  33. package/dist/cjs/private/proto-runtime.js +8 -4
  34. package/dist/cjs/private/reflect.d.ts +9 -0
  35. package/dist/cjs/private/reflect.js +79 -0
  36. package/dist/cjs/private/reify-wkt.js +11 -11
  37. package/dist/cjs/private/scalars.d.ts +9 -18
  38. package/dist/cjs/private/scalars.js +36 -80
  39. package/dist/cjs/private/text-format.d.ts +1 -1
  40. package/dist/cjs/private/text-format.js +17 -17
  41. package/dist/cjs/private/util-common.js +17 -14
  42. package/dist/cjs/proto-delimited.js +2 -3
  43. package/dist/cjs/proto2.js +29 -68
  44. package/dist/cjs/proto3.js +30 -77
  45. package/dist/cjs/scalar.d.ts +53 -0
  46. package/dist/cjs/scalar.js +81 -0
  47. package/dist/cjs/to-plain-message.js +3 -4
  48. package/dist/esm/codegen-info.d.ts +6 -1
  49. package/dist/esm/codegen-info.js +6 -5
  50. package/dist/esm/create-descriptor-set.js +22 -10
  51. package/dist/esm/create-registry-from-desc.js +62 -111
  52. package/dist/esm/descriptor-set.d.ts +13 -9
  53. package/dist/esm/descriptor-set.js +1 -1
  54. package/dist/esm/field.d.ts +25 -51
  55. package/dist/esm/field.js +1 -65
  56. package/dist/esm/google/protobuf/compiler/plugin_pb.d.ts +18 -0
  57. package/dist/esm/google/protobuf/compiler/plugin_pb.js +2 -0
  58. package/dist/esm/google/protobuf/descriptor_pb.d.ts +33 -33
  59. package/dist/esm/google/protobuf/descriptor_pb.js +23 -10
  60. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  61. package/dist/esm/google/protobuf/type_pb.js +1 -1
  62. package/dist/esm/google/protobuf/wrappers_pb.js +1 -1
  63. package/dist/esm/index.d.ts +3 -1
  64. package/dist/esm/index.js +2 -1
  65. package/dist/esm/is-message.d.ts +22 -0
  66. package/dist/esm/is-message.js +48 -0
  67. package/dist/esm/json-format.d.ts +1 -2
  68. package/dist/esm/json-format.js +1 -1
  69. package/dist/esm/private/binary-format.d.ts +7 -0
  70. package/dist/esm/private/{binary-format-common.js → binary-format.js} +126 -25
  71. package/dist/esm/private/extensions.js +2 -2
  72. package/dist/esm/private/feature-set.js +1 -1
  73. package/dist/esm/private/field-normalize.d.ts +9 -0
  74. package/dist/esm/private/field-normalize.js +65 -0
  75. package/dist/esm/private/field-wrapper.d.ts +1 -1
  76. package/dist/esm/private/field-wrapper.js +3 -2
  77. package/dist/esm/private/field.d.ts +1 -0
  78. package/dist/esm/private/field.js +1 -0
  79. package/dist/esm/private/json-format.d.ts +2 -0
  80. package/dist/esm/private/{json-format-common.js → json-format.js} +227 -135
  81. package/dist/esm/private/proto-runtime.d.ts +1 -1
  82. package/dist/esm/private/proto-runtime.js +8 -4
  83. package/dist/esm/private/reflect.d.ts +9 -0
  84. package/dist/esm/private/reflect.js +74 -0
  85. package/dist/esm/private/reify-wkt.js +1 -1
  86. package/dist/esm/private/scalars.d.ts +9 -18
  87. package/dist/esm/private/scalars.js +15 -59
  88. package/dist/esm/private/text-format.d.ts +1 -1
  89. package/dist/esm/private/text-format.js +1 -1
  90. package/dist/esm/private/util-common.js +10 -6
  91. package/dist/esm/proto-delimited.js +2 -3
  92. package/dist/esm/proto2.js +29 -68
  93. package/dist/esm/proto3.js +31 -78
  94. package/dist/esm/scalar.d.ts +53 -0
  95. package/dist/esm/scalar.js +78 -0
  96. package/dist/esm/to-plain-message.js +3 -2
  97. package/package.json +2 -8
  98. package/dist/cjs/private/binary-format-common.d.ts +0 -13
  99. package/dist/cjs/private/binary-format-proto2.d.ts +0 -2
  100. package/dist/cjs/private/binary-format-proto2.js +0 -91
  101. package/dist/cjs/private/binary-format-proto3.d.ts +0 -2
  102. package/dist/cjs/private/binary-format-proto3.js +0 -83
  103. package/dist/cjs/private/json-format-common.d.ts +0 -9
  104. package/dist/cjs/private/json-format-proto2.d.ts +0 -2
  105. package/dist/cjs/private/json-format-proto2.js +0 -103
  106. package/dist/cjs/private/json-format-proto3.d.ts +0 -2
  107. package/dist/cjs/private/json-format-proto3.js +0 -94
  108. package/dist/esm/private/binary-format-common.d.ts +0 -13
  109. package/dist/esm/private/binary-format-proto2.d.ts +0 -2
  110. package/dist/esm/private/binary-format-proto2.js +0 -87
  111. package/dist/esm/private/binary-format-proto3.d.ts +0 -2
  112. package/dist/esm/private/binary-format-proto3.js +0 -79
  113. package/dist/esm/private/json-format-common.d.ts +0 -9
  114. package/dist/esm/private/json-format-proto2.d.ts +0 -2
  115. package/dist/esm/private/json-format-proto2.js +0 -99
  116. package/dist/esm/private/json-format-proto3.d.ts +0 -2
  117. package/dist/esm/private/json-format-proto3.js +0 -90
  118. package/dist/proxy/index.d.ts +0 -1
  119. package/dist/proxy/index.js +0 -1
@@ -0,0 +1,78 @@
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ /**
15
+ * Scalar value types. This is a subset of field types declared by protobuf
16
+ * enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
17
+ * are omitted, but the numerical values are identical.
18
+ */
19
+ export var ScalarType;
20
+ (function (ScalarType) {
21
+ // 0 is reserved for errors.
22
+ // Order is weird for historical reasons.
23
+ ScalarType[ScalarType["DOUBLE"] = 1] = "DOUBLE";
24
+ ScalarType[ScalarType["FLOAT"] = 2] = "FLOAT";
25
+ // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
26
+ // negative values are likely.
27
+ ScalarType[ScalarType["INT64"] = 3] = "INT64";
28
+ ScalarType[ScalarType["UINT64"] = 4] = "UINT64";
29
+ // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
30
+ // negative values are likely.
31
+ ScalarType[ScalarType["INT32"] = 5] = "INT32";
32
+ ScalarType[ScalarType["FIXED64"] = 6] = "FIXED64";
33
+ ScalarType[ScalarType["FIXED32"] = 7] = "FIXED32";
34
+ ScalarType[ScalarType["BOOL"] = 8] = "BOOL";
35
+ ScalarType[ScalarType["STRING"] = 9] = "STRING";
36
+ // Tag-delimited aggregate.
37
+ // Group type is deprecated and not supported in proto3. However, Proto3
38
+ // implementations should still be able to parse the group wire format and
39
+ // treat group fields as unknown fields.
40
+ // TYPE_GROUP = 10,
41
+ // TYPE_MESSAGE = 11, // Length-delimited aggregate.
42
+ // New in version 2.
43
+ ScalarType[ScalarType["BYTES"] = 12] = "BYTES";
44
+ ScalarType[ScalarType["UINT32"] = 13] = "UINT32";
45
+ // TYPE_ENUM = 14,
46
+ ScalarType[ScalarType["SFIXED32"] = 15] = "SFIXED32";
47
+ ScalarType[ScalarType["SFIXED64"] = 16] = "SFIXED64";
48
+ ScalarType[ScalarType["SINT32"] = 17] = "SINT32";
49
+ ScalarType[ScalarType["SINT64"] = 18] = "SINT64";
50
+ })(ScalarType || (ScalarType = {}));
51
+ /**
52
+ * JavaScript representation of fields with 64 bit integral types (int64, uint64,
53
+ * sint64, fixed64, sfixed64).
54
+ *
55
+ * This is a subset of google.protobuf.FieldOptions.JSType, which defines JS_NORMAL,
56
+ * JS_STRING, and JS_NUMBER. Protobuf-ES uses BigInt by default, but will use
57
+ * String if `[jstype = JS_STRING]` is specified.
58
+ *
59
+ * ```protobuf
60
+ * uint64 field_a = 1; // BigInt
61
+ * uint64 field_b = 2 [jstype = JS_NORMAL]; // BigInt
62
+ * uint64 field_b = 2 [jstype = JS_NUMBER]; // BigInt
63
+ * uint64 field_b = 2 [jstype = JS_STRING]; // String
64
+ * ```
65
+ */
66
+ export var LongType;
67
+ (function (LongType) {
68
+ /**
69
+ * Use JavaScript BigInt.
70
+ */
71
+ LongType[LongType["BIGINT"] = 0] = "BIGINT";
72
+ /**
73
+ * Use JavaScript String.
74
+ *
75
+ * Field option `[jstype = JS_STRING]`.
76
+ */
77
+ LongType[LongType["STRING"] = 1] = "STRING";
78
+ })(LongType || (LongType = {}));
@@ -13,6 +13,7 @@
13
13
  // limitations under the License.
14
14
  /* 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 */
15
15
  import { Message } from "./message.js";
16
+ import { isMessage } from "./is-message.js";
16
17
  /**
17
18
  * toPlainMessage returns a new object by stripping
18
19
  * all methods from a message, leaving only fields and
@@ -23,7 +24,7 @@ import { Message } from "./message.js";
23
24
  * returned as-is.
24
25
  */
25
26
  export function toPlainMessage(message) {
26
- if (!(message instanceof Message)) {
27
+ if (!isMessage(message)) {
27
28
  return message;
28
29
  }
29
30
  const type = message.getType();
@@ -57,7 +58,7 @@ function toPlainValue(value) {
57
58
  if (value === undefined) {
58
59
  return value;
59
60
  }
60
- if (value instanceof Message) {
61
+ if (isMessage(value)) {
61
62
  return toPlainMessage(value);
62
63
  }
63
64
  if (value instanceof Uint8Array) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protobuf",
3
- "version": "1.7.2",
3
+ "version": "1.8.0",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "A complete implementation of Protocol Buffers in TypeScript, suitable for web browsers and Node.js.",
6
6
  "repository": {
@@ -10,10 +10,9 @@
10
10
  },
11
11
  "scripts": {
12
12
  "clean": "rm -rf ./dist/*",
13
- "build": "npm run build:cjs && npm run build:esm && npm run build:proxy",
13
+ "build": "npm run build:cjs && npm run build:esm",
14
14
  "build:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
15
15
  "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/esm",
16
- "build:proxy": "node ../../scripts/gen-esm-proxy.mjs .",
17
16
  "bootstrap:featureset-defaults": "upstream-inject-feature-defaults src/private/feature-set.ts",
18
17
  "prebootstrap:wkt": "rm -rf .tmp && mkdir -p .tmp/google/protobuf && cp -rp src/google/protobuf/* .tmp/google/protobuf",
19
18
  "bootstrap:wkt": "protoc --es_out=src --es_opt=bootstrap_wkt=true,ts_nocheck=false,target=ts --proto_path $(upstream-include wkt) $(upstream-files wkt) && license-header src/google/protobuf",
@@ -25,11 +24,6 @@
25
24
  "main": "./dist/cjs/index.js",
26
25
  "exports": {
27
26
  ".": {
28
- "node": {
29
- "import": "./dist/proxy/index.js",
30
- "require": "./dist/cjs/index.js"
31
- },
32
- "module": "./dist/esm/index.js",
33
27
  "import": "./dist/esm/index.js",
34
28
  "require": "./dist/cjs/index.js"
35
29
  }
@@ -1,13 +0,0 @@
1
- import type { IBinaryWriter } from "../binary-encoding.js";
2
- import type { BinaryFormat, BinaryWriteOptions } from "../binary-format.js";
3
- import type { FieldInfo } from "../field.js";
4
- import { ScalarType } from "../field.js";
5
- export declare function makeBinaryFormatCommon(): Omit<BinaryFormat, "writeMessage" | "writeField">;
6
- export declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
7
- kind: "map";
8
- }, key: any, value: any): void;
9
- export declare function writeMessageField(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
10
- kind: "message";
11
- }, value: any): void;
12
- export declare function writeScalar(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any, emitIntrinsicDefault: boolean): void;
13
- export declare function writePacked(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any[]): void;
@@ -1,2 +0,0 @@
1
- import type { BinaryFormat } from "../binary-format.js";
2
- export declare function makeBinaryFormatProto2(): BinaryFormat;
@@ -1,91 +0,0 @@
1
- "use strict";
2
- // Copyright 2021-2024 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.makeBinaryFormatProto2 = void 0;
17
- const field_js_1 = require("../field.js");
18
- const binary_format_common_js_1 = require("./binary-format-common.js");
19
- /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, no-case-declarations, prefer-const */
20
- function makeBinaryFormatProto2() {
21
- return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeField,
22
- writeMessage(message, writer, options) {
23
- const type = message.getType();
24
- let field;
25
- for (field of type.fields.byNumber()) {
26
- let value, localName = field.localName;
27
- if (field.oneof) {
28
- const oneof = message[field.oneof.localName];
29
- if (oneof.case !== localName) {
30
- continue; // field is not selected, skip
31
- }
32
- value = oneof.value;
33
- }
34
- else {
35
- value = message[localName];
36
- // In contrast to proto3, we raise an error if a non-optional (proto2 required)
37
- // field is missing a value.
38
- if (value === undefined && !field.oneof && !field.opt) {
39
- throw new Error(`cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary: required field not set`);
40
- }
41
- }
42
- writeField(field, value, writer, options);
43
- }
44
- if (options.writeUnknownFields) {
45
- this.writeUnknownFields(message, writer);
46
- }
47
- return writer;
48
- } });
49
- }
50
- exports.makeBinaryFormatProto2 = makeBinaryFormatProto2;
51
- // TODO field presence: merge this function with proto3
52
- function writeField(field, value, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
53
- writer, options) {
54
- const repeated = field.repeated;
55
- switch (field.kind) {
56
- case "scalar":
57
- case "enum":
58
- let scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
59
- if (repeated) {
60
- if (field.packed) {
61
- (0, binary_format_common_js_1.writePacked)(writer, scalarType, field.no, value);
62
- }
63
- else {
64
- for (const item of value) {
65
- (0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, item, true);
66
- }
67
- }
68
- }
69
- else if (value !== undefined) {
70
- // In contrast to proto3, we do not skip intrinsic default values.
71
- // Explicit default values are not special cased either.
72
- (0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, value, true);
73
- }
74
- break;
75
- case "message":
76
- if (repeated) {
77
- for (const item of value) {
78
- (0, binary_format_common_js_1.writeMessageField)(writer, options, field, item);
79
- }
80
- }
81
- else if (value !== undefined) {
82
- (0, binary_format_common_js_1.writeMessageField)(writer, options, field, value);
83
- }
84
- break;
85
- case "map":
86
- for (const [key, val] of Object.entries(value)) {
87
- (0, binary_format_common_js_1.writeMapEntry)(writer, options, field, key, val);
88
- }
89
- break;
90
- }
91
- }
@@ -1,2 +0,0 @@
1
- import type { BinaryFormat } from "../binary-format.js";
2
- export declare function makeBinaryFormatProto3(): BinaryFormat;
@@ -1,83 +0,0 @@
1
- "use strict";
2
- // Copyright 2021-2024 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.makeBinaryFormatProto3 = void 0;
17
- const field_js_1 = require("../field.js");
18
- const binary_format_common_js_1 = require("./binary-format-common.js");
19
- /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, prefer-const, no-case-declarations */
20
- function makeBinaryFormatProto3() {
21
- return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeField,
22
- writeMessage(message, writer, options) {
23
- const type = message.getType();
24
- for (const field of type.fields.byNumber()) {
25
- let value, localName = field.localName;
26
- if (field.oneof) {
27
- const oneof = message[field.oneof.localName];
28
- if (oneof.case !== localName) {
29
- continue; // field is not selected, skip
30
- }
31
- value = oneof.value;
32
- }
33
- else {
34
- value = message[localName];
35
- }
36
- writeField(field, value, writer, options);
37
- }
38
- if (options.writeUnknownFields) {
39
- this.writeUnknownFields(message, writer);
40
- }
41
- return writer;
42
- } });
43
- }
44
- exports.makeBinaryFormatProto3 = makeBinaryFormatProto3;
45
- // TODO field presence: merge this function with proto2
46
- function writeField(field, value, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
47
- writer, options) {
48
- const repeated = field.repeated;
49
- switch (field.kind) {
50
- case "scalar":
51
- case "enum":
52
- let scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
53
- if (repeated) {
54
- if (field.packed) {
55
- (0, binary_format_common_js_1.writePacked)(writer, scalarType, field.no, value);
56
- }
57
- else {
58
- for (const item of value) {
59
- (0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, item, true);
60
- }
61
- }
62
- }
63
- else if (value !== undefined) {
64
- (0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, value, !!field.oneof || field.opt);
65
- }
66
- break;
67
- case "message":
68
- if (repeated) {
69
- for (const item of value) {
70
- (0, binary_format_common_js_1.writeMessageField)(writer, options, field, item);
71
- }
72
- }
73
- else if (value !== undefined) {
74
- (0, binary_format_common_js_1.writeMessageField)(writer, options, field, value);
75
- }
76
- break;
77
- case "map":
78
- for (const [key, val] of Object.entries(value)) {
79
- (0, binary_format_common_js_1.writeMapEntry)(writer, options, field, key, val);
80
- }
81
- break;
82
- }
83
- }
@@ -1,9 +0,0 @@
1
- import type { JsonFormat, JsonValue, JsonWriteOptions } from "../json-format.js";
2
- import type { FieldInfo } from "../field.js";
3
- import { ScalarType } from "../field.js";
4
- import type { EnumType } from "../enum.js";
5
- type JsonFormatWriteFieldFn = (field: FieldInfo, value: any, options: JsonWriteOptions) => JsonValue | undefined;
6
- export declare function makeJsonFormatCommon(nullAsZeroValue: boolean, makeWriteField: (writeEnumFn: typeof writeEnum, writeScalarFn: typeof writeScalar) => JsonFormatWriteFieldFn): JsonFormat;
7
- declare function writeEnum(type: EnumType, value: number | undefined, emitZeroValue: boolean, enumAsInteger: boolean): JsonValue | undefined;
8
- declare function writeScalar(type: ScalarType, value: any, emitZeroValue: boolean): JsonValue | undefined;
9
- export {};
@@ -1,2 +0,0 @@
1
- import type { JsonFormat } from "../json-format.js";
2
- export declare function makeJsonFormatProto2(): JsonFormat;
@@ -1,103 +0,0 @@
1
- "use strict";
2
- // Copyright 2021-2024 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.makeJsonFormatProto2 = void 0;
17
- const field_wrapper_js_1 = require("./field-wrapper.js");
18
- const assert_js_1 = require("./assert.js");
19
- const json_format_common_js_1 = require("./json-format-common.js");
20
- /* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
21
- function makeJsonFormatProto2() {
22
- // TODO field presence: merge this function with proto3. the reading side already switches behavior on the nullAsZeroValue argument.
23
- return (0, json_format_common_js_1.makeJsonFormatCommon)(false, (writeEnum, writeScalar) => {
24
- return function writeField(field, value, options) {
25
- if (field.kind == "map") {
26
- const jsonObj = {};
27
- switch (field.V.kind) {
28
- case "scalar":
29
- for (const [entryKey, entryValue] of Object.entries(value)) {
30
- const val = writeScalar(field.V.T, entryValue, true);
31
- (0, assert_js_1.assert)(val !== undefined);
32
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
33
- }
34
- break;
35
- case "message":
36
- for (const [entryKey, entryValue] of Object.entries(value)) {
37
- // JSON standard allows only (double quoted) string as property key
38
- jsonObj[entryKey.toString()] = entryValue.toJson(options);
39
- }
40
- break;
41
- case "enum":
42
- const enumType = field.V.T;
43
- for (const [entryKey, entryValue] of Object.entries(value)) {
44
- (0, assert_js_1.assert)(entryValue === undefined || typeof entryValue == "number");
45
- const val = writeEnum(enumType, entryValue, true, options.enumAsInteger);
46
- (0, assert_js_1.assert)(val !== undefined);
47
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
48
- }
49
- break;
50
- }
51
- return options.emitDefaultValues || Object.keys(jsonObj).length > 0
52
- ? jsonObj
53
- : undefined;
54
- }
55
- else if (field.repeated) {
56
- const jsonArr = [];
57
- switch (field.kind) {
58
- case "scalar":
59
- for (let i = 0; i < value.length; i++) {
60
- jsonArr.push(writeScalar(field.T, value[i], true));
61
- }
62
- break;
63
- case "enum":
64
- for (let i = 0; i < value.length; i++) {
65
- jsonArr.push(writeEnum(field.T, value[i], true, options.enumAsInteger));
66
- }
67
- break;
68
- case "message":
69
- for (let i = 0; i < value.length; i++) {
70
- jsonArr.push(value[i].toJson(options));
71
- }
72
- break;
73
- }
74
- return options.emitDefaultValues || jsonArr.length > 0
75
- ? jsonArr
76
- : undefined;
77
- }
78
- else {
79
- // In contrast to proto3, we raise an error if a non-optional (proto2 required)
80
- // field is missing a value.
81
- if (value === undefined) {
82
- if (!field.oneof && !field.opt) {
83
- throw `required field not set`;
84
- }
85
- return undefined;
86
- }
87
- switch (field.kind) {
88
- case "scalar":
89
- // In contrast to proto3, we do not skip intrinsic default values.
90
- // Explicit default values are not special cased either.
91
- return writeScalar(field.T, value, true);
92
- case "enum":
93
- // In contrast to proto3, we do not skip intrinsic default values.
94
- // Explicit default values are not special cased either.
95
- return writeEnum(field.T, value, true, options.enumAsInteger);
96
- case "message":
97
- return (0, field_wrapper_js_1.wrapField)(field.T, value).toJson(options);
98
- }
99
- }
100
- };
101
- });
102
- }
103
- exports.makeJsonFormatProto2 = makeJsonFormatProto2;
@@ -1,2 +0,0 @@
1
- import type { JsonFormat } from "../json-format.js";
2
- export declare function makeJsonFormatProto3(): JsonFormat;
@@ -1,94 +0,0 @@
1
- "use strict";
2
- // Copyright 2021-2024 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.makeJsonFormatProto3 = void 0;
17
- const field_wrapper_js_1 = require("./field-wrapper.js");
18
- const assert_js_1 = require("./assert.js");
19
- const json_format_common_js_1 = require("./json-format-common.js");
20
- /* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
21
- function makeJsonFormatProto3() {
22
- // TODO field presence: merge this function with proto2. the reading side already switches behavior on the nullAsZeroValue argument.
23
- return (0, json_format_common_js_1.makeJsonFormatCommon)(true, (writeEnum, writeScalar) => {
24
- return function writeField(field, value, options) {
25
- if (field.kind == "map") {
26
- const jsonObj = {};
27
- switch (field.V.kind) {
28
- case "scalar":
29
- for (const [entryKey, entryValue] of Object.entries(value)) {
30
- const val = writeScalar(field.V.T, entryValue, true);
31
- (0, assert_js_1.assert)(val !== undefined);
32
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
33
- }
34
- break;
35
- case "message":
36
- for (const [entryKey, entryValue] of Object.entries(value)) {
37
- // JSON standard allows only (double quoted) string as property key
38
- jsonObj[entryKey.toString()] = entryValue.toJson(options);
39
- }
40
- break;
41
- case "enum":
42
- const enumType = field.V.T;
43
- for (const [entryKey, entryValue] of Object.entries(value)) {
44
- (0, assert_js_1.assert)(entryValue === undefined || typeof entryValue == "number");
45
- const val = writeEnum(enumType, entryValue, true, options.enumAsInteger);
46
- (0, assert_js_1.assert)(val !== undefined);
47
- jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
48
- }
49
- break;
50
- }
51
- return options.emitDefaultValues || Object.keys(jsonObj).length > 0
52
- ? jsonObj
53
- : undefined;
54
- }
55
- else if (field.repeated) {
56
- const jsonArr = [];
57
- switch (field.kind) {
58
- case "scalar":
59
- for (let i = 0; i < value.length; i++) {
60
- jsonArr.push(writeScalar(field.T, value[i], true));
61
- }
62
- break;
63
- case "enum":
64
- for (let i = 0; i < value.length; i++) {
65
- jsonArr.push(writeEnum(field.T, value[i], true, options.enumAsInteger));
66
- }
67
- break;
68
- case "message":
69
- for (let i = 0; i < value.length; i++) {
70
- jsonArr.push(value[i].toJson(options));
71
- }
72
- break;
73
- }
74
- return options.emitDefaultValues || jsonArr.length > 0
75
- ? jsonArr
76
- : undefined;
77
- }
78
- else {
79
- if (value === undefined) {
80
- return undefined;
81
- }
82
- switch (field.kind) {
83
- case "scalar":
84
- return writeScalar(field.T, value, !!field.oneof || field.opt || options.emitDefaultValues);
85
- case "enum":
86
- return writeEnum(field.T, value, !!field.oneof || field.opt || options.emitDefaultValues, options.enumAsInteger);
87
- case "message":
88
- return (0, field_wrapper_js_1.wrapField)(field.T, value).toJson(options);
89
- }
90
- }
91
- };
92
- });
93
- }
94
- exports.makeJsonFormatProto3 = makeJsonFormatProto3;
@@ -1,13 +0,0 @@
1
- import type { IBinaryWriter } from "../binary-encoding.js";
2
- import type { BinaryFormat, BinaryWriteOptions } from "../binary-format.js";
3
- import type { FieldInfo } from "../field.js";
4
- import { ScalarType } from "../field.js";
5
- export declare function makeBinaryFormatCommon(): Omit<BinaryFormat, "writeMessage" | "writeField">;
6
- export declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
7
- kind: "map";
8
- }, key: any, value: any): void;
9
- export declare function writeMessageField(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
10
- kind: "message";
11
- }, value: any): void;
12
- export declare function writeScalar(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any, emitIntrinsicDefault: boolean): void;
13
- export declare function writePacked(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: any[]): void;
@@ -1,2 +0,0 @@
1
- import type { BinaryFormat } from "../binary-format.js";
2
- export declare function makeBinaryFormatProto2(): BinaryFormat;
@@ -1,87 +0,0 @@
1
- // Copyright 2021-2024 Buf Technologies, Inc.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
- import { ScalarType } from "../field.js";
15
- import { makeBinaryFormatCommon, writeMapEntry, writeMessageField, writePacked, writeScalar, } from "./binary-format-common.js";
16
- /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, no-case-declarations, prefer-const */
17
- export function makeBinaryFormatProto2() {
18
- return Object.assign(Object.assign({}, makeBinaryFormatCommon()), { writeField,
19
- writeMessage(message, writer, options) {
20
- const type = message.getType();
21
- let field;
22
- for (field of type.fields.byNumber()) {
23
- let value, localName = field.localName;
24
- if (field.oneof) {
25
- const oneof = message[field.oneof.localName];
26
- if (oneof.case !== localName) {
27
- continue; // field is not selected, skip
28
- }
29
- value = oneof.value;
30
- }
31
- else {
32
- value = message[localName];
33
- // In contrast to proto3, we raise an error if a non-optional (proto2 required)
34
- // field is missing a value.
35
- if (value === undefined && !field.oneof && !field.opt) {
36
- throw new Error(`cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary: required field not set`);
37
- }
38
- }
39
- writeField(field, value, writer, options);
40
- }
41
- if (options.writeUnknownFields) {
42
- this.writeUnknownFields(message, writer);
43
- }
44
- return writer;
45
- } });
46
- }
47
- // TODO field presence: merge this function with proto3
48
- function writeField(field, value, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
49
- writer, options) {
50
- const repeated = field.repeated;
51
- switch (field.kind) {
52
- case "scalar":
53
- case "enum":
54
- let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
55
- if (repeated) {
56
- if (field.packed) {
57
- writePacked(writer, scalarType, field.no, value);
58
- }
59
- else {
60
- for (const item of value) {
61
- writeScalar(writer, scalarType, field.no, item, true);
62
- }
63
- }
64
- }
65
- else if (value !== undefined) {
66
- // In contrast to proto3, we do not skip intrinsic default values.
67
- // Explicit default values are not special cased either.
68
- writeScalar(writer, scalarType, field.no, value, true);
69
- }
70
- break;
71
- case "message":
72
- if (repeated) {
73
- for (const item of value) {
74
- writeMessageField(writer, options, field, item);
75
- }
76
- }
77
- else if (value !== undefined) {
78
- writeMessageField(writer, options, field, value);
79
- }
80
- break;
81
- case "map":
82
- for (const [key, val] of Object.entries(value)) {
83
- writeMapEntry(writer, options, field, key, val);
84
- }
85
- break;
86
- }
87
- }