@songtonyli/dsh-cli 0.1.32 → 0.1.33

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 (190) hide show
  1. package/README.md +1 -1
  2. package/bin/dsh.mjs +1 -1
  3. package/node_modules/@bufbuild/protobuf/README.md +41 -0
  4. package/node_modules/@bufbuild/protobuf/dist/commonjs/clone.js +66 -0
  5. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/boot.js +18 -0
  6. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/enum.js +31 -0
  7. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/extension.js +28 -0
  8. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/file.js +18 -0
  9. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/index.js +39 -0
  10. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/message.js +24 -0
  11. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/service.js +27 -0
  12. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/symbols.js +43 -0
  13. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv1/types.js +15 -0
  14. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/boot.js +105 -0
  15. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/embed.js +244 -0
  16. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/enum.js +58 -0
  17. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/extension.js +28 -0
  18. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/file.js +35 -0
  19. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/index.js +39 -0
  20. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/message.js +24 -0
  21. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/restore-json-names.js +29 -0
  22. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/scalar.js +67 -0
  23. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/service.js +27 -0
  24. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/symbols.js +74 -0
  25. package/node_modules/@bufbuild/protobuf/dist/commonjs/codegenv2/types.js +15 -0
  26. package/node_modules/@bufbuild/protobuf/dist/commonjs/create.js +296 -0
  27. package/node_modules/@bufbuild/protobuf/dist/commonjs/descriptors.js +53 -0
  28. package/node_modules/@bufbuild/protobuf/dist/commonjs/equals.js +209 -0
  29. package/node_modules/@bufbuild/protobuf/dist/commonjs/extensions.js +168 -0
  30. package/node_modules/@bufbuild/protobuf/dist/commonjs/fields.js +45 -0
  31. package/node_modules/@bufbuild/protobuf/dist/commonjs/from-binary.js +415 -0
  32. package/node_modules/@bufbuild/protobuf/dist/commonjs/from-json.js +1037 -0
  33. package/node_modules/@bufbuild/protobuf/dist/commonjs/index.js +55 -0
  34. package/node_modules/@bufbuild/protobuf/dist/commonjs/is-message.js +33 -0
  35. package/node_modules/@bufbuild/protobuf/dist/commonjs/json-value.js +15 -0
  36. package/node_modules/@bufbuild/protobuf/dist/commonjs/merge.js +70 -0
  37. package/node_modules/@bufbuild/protobuf/dist/commonjs/package.json +4 -0
  38. package/node_modules/@bufbuild/protobuf/dist/commonjs/proto-int64.js +131 -0
  39. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/error.js +36 -0
  40. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/guard.js +78 -0
  41. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/index.js +41 -0
  42. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/message.js +157 -0
  43. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/names.js +115 -0
  44. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/nested-types.js +110 -0
  45. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/path.js +376 -0
  46. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/reflect-check.js +292 -0
  47. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/reflect-types.js +16 -0
  48. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/reflect.js +469 -0
  49. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/scalar.js +129 -0
  50. package/node_modules/@bufbuild/protobuf/dist/commonjs/reflect/unsafe.js +149 -0
  51. package/node_modules/@bufbuild/protobuf/dist/commonjs/registry.js +954 -0
  52. package/node_modules/@bufbuild/protobuf/dist/commonjs/to-binary.js +437 -0
  53. package/node_modules/@bufbuild/protobuf/dist/commonjs/to-json.js +625 -0
  54. package/node_modules/@bufbuild/protobuf/dist/commonjs/txtpb/from-text.js +684 -0
  55. package/node_modules/@bufbuild/protobuf/dist/commonjs/txtpb/index.js +21 -0
  56. package/node_modules/@bufbuild/protobuf/dist/commonjs/txtpb/is-group-like.js +47 -0
  57. package/node_modules/@bufbuild/protobuf/dist/commonjs/txtpb/reader.js +481 -0
  58. package/node_modules/@bufbuild/protobuf/dist/commonjs/txtpb/to-text.js +358 -0
  59. package/node_modules/@bufbuild/protobuf/dist/commonjs/txtpb/writer.js +220 -0
  60. package/node_modules/@bufbuild/protobuf/dist/commonjs/types.js +15 -0
  61. package/node_modules/@bufbuild/protobuf/dist/commonjs/unknown-enum.js +26 -0
  62. package/node_modules/@bufbuild/protobuf/dist/commonjs/wire/base64-encoding.js +188 -0
  63. package/node_modules/@bufbuild/protobuf/dist/commonjs/wire/binary-encoding.js +726 -0
  64. package/node_modules/@bufbuild/protobuf/dist/commonjs/wire/index.js +37 -0
  65. package/node_modules/@bufbuild/protobuf/dist/commonjs/wire/size-delimited.js +191 -0
  66. package/node_modules/@bufbuild/protobuf/dist/commonjs/wire/text-encoding.js +106 -0
  67. package/node_modules/@bufbuild/protobuf/dist/commonjs/wire/text-format.js +199 -0
  68. package/node_modules/@bufbuild/protobuf/dist/commonjs/wire/varint.js +334 -0
  69. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/any.js +75 -0
  70. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/duration.js +39 -0
  71. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/any_pb.js +27 -0
  72. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/api_pb.js +39 -0
  73. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/compiler/plugin_pb.js +68 -0
  74. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/cpp_features_pb.js +60 -0
  75. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/descriptor_pb.js +892 -0
  76. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/duration_pb.js +27 -0
  77. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/empty_pb.js +27 -0
  78. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/field_mask_pb.js +27 -0
  79. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/go_features_pb.js +126 -0
  80. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/java_features_pb.js +106 -0
  81. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/source_context_pb.js +27 -0
  82. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/struct_pb.js +59 -0
  83. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/timestamp_pb.js +27 -0
  84. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/type_pb.js +242 -0
  85. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/gen/google/protobuf/wrappers_pb.js +67 -0
  86. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/index.js +48 -0
  87. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/json.js +44 -0
  88. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/timestamp.js +57 -0
  89. package/node_modules/@bufbuild/protobuf/dist/commonjs/wkt/wrappers.js +66 -0
  90. package/node_modules/@bufbuild/protobuf/dist/esm/clone.js +63 -0
  91. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/boot.js +14 -0
  92. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/enum.js +26 -0
  93. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/extension.js +25 -0
  94. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/file.js +14 -0
  95. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/index.js +23 -0
  96. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/message.js +21 -0
  97. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/service.js +24 -0
  98. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/symbols.js +40 -0
  99. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv1/types.js +14 -0
  100. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/boot.js +101 -0
  101. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/embed.js +239 -0
  102. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/enum.js +53 -0
  103. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/extension.js +25 -0
  104. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/file.js +32 -0
  105. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/index.js +23 -0
  106. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/message.js +21 -0
  107. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/restore-json-names.js +26 -0
  108. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/scalar.js +63 -0
  109. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/service.js +24 -0
  110. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/symbols.js +71 -0
  111. package/node_modules/@bufbuild/protobuf/dist/esm/codegenv2/types.js +14 -0
  112. package/node_modules/@bufbuild/protobuf/dist/esm/create.js +293 -0
  113. package/node_modules/@bufbuild/protobuf/dist/esm/descriptors.js +50 -0
  114. package/node_modules/@bufbuild/protobuf/dist/esm/equals.js +206 -0
  115. package/node_modules/@bufbuild/protobuf/dist/esm/extensions.js +159 -0
  116. package/node_modules/@bufbuild/protobuf/dist/esm/fields.js +41 -0
  117. package/node_modules/@bufbuild/protobuf/dist/esm/from-binary.js +409 -0
  118. package/node_modules/@bufbuild/protobuf/dist/esm/from-json.js +1029 -0
  119. package/node_modules/@bufbuild/protobuf/dist/esm/index.js +29 -0
  120. package/node_modules/@bufbuild/protobuf/dist/esm/is-message.js +30 -0
  121. package/node_modules/@bufbuild/protobuf/dist/esm/json-value.js +14 -0
  122. package/node_modules/@bufbuild/protobuf/dist/esm/merge.js +67 -0
  123. package/node_modules/@bufbuild/protobuf/dist/esm/package.json +4 -0
  124. package/node_modules/@bufbuild/protobuf/dist/esm/proto-int64.js +128 -0
  125. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/error.js +31 -0
  126. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/guard.js +71 -0
  127. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/index.js +21 -0
  128. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/message.js +152 -0
  129. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/names.js +109 -0
  130. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/nested-types.js +105 -0
  131. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/path.js +369 -0
  132. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/reflect-check.js +284 -0
  133. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/reflect-types.js +14 -0
  134. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/reflect.js +464 -0
  135. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/scalar.js +124 -0
  136. package/node_modules/@bufbuild/protobuf/dist/esm/reflect/unsafe.js +140 -0
  137. package/node_modules/@bufbuild/protobuf/dist/esm/registry.js +948 -0
  138. package/node_modules/@bufbuild/protobuf/dist/esm/to-binary.js +433 -0
  139. package/node_modules/@bufbuild/protobuf/dist/esm/to-json.js +620 -0
  140. package/node_modules/@bufbuild/protobuf/dist/esm/txtpb/from-text.js +680 -0
  141. package/node_modules/@bufbuild/protobuf/dist/esm/txtpb/index.js +15 -0
  142. package/node_modules/@bufbuild/protobuf/dist/esm/txtpb/is-group-like.js +44 -0
  143. package/node_modules/@bufbuild/protobuf/dist/esm/txtpb/reader.js +477 -0
  144. package/node_modules/@bufbuild/protobuf/dist/esm/txtpb/to-text.js +355 -0
  145. package/node_modules/@bufbuild/protobuf/dist/esm/txtpb/writer.js +214 -0
  146. package/node_modules/@bufbuild/protobuf/dist/esm/types.js +14 -0
  147. package/node_modules/@bufbuild/protobuf/dist/esm/unknown-enum.js +23 -0
  148. package/node_modules/@bufbuild/protobuf/dist/esm/wire/base64-encoding.js +184 -0
  149. package/node_modules/@bufbuild/protobuf/dist/esm/wire/binary-encoding.js +721 -0
  150. package/node_modules/@bufbuild/protobuf/dist/esm/wire/index.js +18 -0
  151. package/node_modules/@bufbuild/protobuf/dist/esm/wire/size-delimited.js +186 -0
  152. package/node_modules/@bufbuild/protobuf/dist/esm/wire/text-encoding.js +101 -0
  153. package/node_modules/@bufbuild/protobuf/dist/esm/wire/text-format.js +195 -0
  154. package/node_modules/@bufbuild/protobuf/dist/esm/wire/varint.js +325 -0
  155. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/any.js +69 -0
  156. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/duration.js +35 -0
  157. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/any_pb.js +24 -0
  158. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/api_pb.js +36 -0
  159. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.js +65 -0
  160. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/cpp_features_pb.js +57 -0
  161. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +888 -0
  162. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/duration_pb.js +24 -0
  163. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/empty_pb.js +24 -0
  164. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/field_mask_pb.js +24 -0
  165. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/go_features_pb.js +123 -0
  166. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/java_features_pb.js +103 -0
  167. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/source_context_pb.js +24 -0
  168. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/struct_pb.js +56 -0
  169. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/timestamp_pb.js +24 -0
  170. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/type_pb.js +239 -0
  171. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/gen/google/protobuf/wrappers_pb.js +64 -0
  172. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/index.js +32 -0
  173. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/json.js +41 -0
  174. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/timestamp.js +50 -0
  175. package/node_modules/@bufbuild/protobuf/dist/esm/wkt/wrappers.js +61 -0
  176. package/node_modules/@bufbuild/protobuf/package.json +136 -0
  177. package/node_modules/@deepseek-ai/dsh-base/package.json +1 -0
  178. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/package.json +2 -2
  179. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
  180. package/node_modules/@deepseek-ai/dsh-client-ui-slots/package.json +2 -2
  181. package/node_modules/@deepseek-ai/dsh-llm-cursor/README.i18n.yaml +6 -0
  182. package/node_modules/@deepseek-ai/dsh-llm-cursor/README.md +142 -0
  183. package/node_modules/@deepseek-ai/dsh-llm-cursor/README.zh.md +142 -0
  184. package/node_modules/@deepseek-ai/dsh-llm-cursor/lib/index.js +3013 -0
  185. package/node_modules/@deepseek-ai/dsh-llm-cursor/package.json +61 -0
  186. package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +2 -2
  187. package/node_modules/@deepseek-ai/dsh-tui-app/README.md +5 -5
  188. package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +5 -5
  189. package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +44 -18
  190. package/package.json +5 -1
@@ -0,0 +1,620 @@
1
+ // Copyright 2021-2026 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 "./descriptors.js";
15
+ import { protoCamelCase, protoSnakeCase } from "./reflect/names.js";
16
+ import { anyUnpack } from "./wkt/index.js";
17
+ import { hasCustomJsonRepresentation, isWrapperDesc } from "./wkt/wrappers.js";
18
+ import { durationSecondsMax, durationSecondsMin, timestampMsMax, timestampMsMin, } from "./wkt/json.js";
19
+ import { base64Encode } from "./wire/index.js";
20
+ import { createExtensionContainer, getExtension } from "./extensions.js";
21
+ import { checkField, formatVal } from "./reflect/reflect-check.js";
22
+ import { FieldError } from "./reflect/error.js";
23
+ import { unsafeLocal } from "./reflect/unsafe.js";
24
+ import { scalarZeroValue } from "./reflect/scalar.js";
25
+ import { localMessageMapper } from "./reflect/message.js";
26
+ // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name = $number;
27
+ const LEGACY_REQUIRED = 3;
28
+ // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name = $number;
29
+ const IMPLICIT = 2;
30
+ // Default options for serializing to JSON.
31
+ const jsonWriteDefaults = {
32
+ alwaysEmitImplicit: false,
33
+ enumAsInteger: false,
34
+ useProtoFieldName: false,
35
+ };
36
+ function makeWriteOptions(options) {
37
+ return options ? Object.assign(Object.assign({}, jsonWriteDefaults), options) : jsonWriteDefaults;
38
+ }
39
+ /**
40
+ * Serialize the message to a JSON value, a JavaScript value that can be
41
+ * passed to JSON.stringify().
42
+ */
43
+ export function toJson(schema, message, options) {
44
+ return compiledWriter(schema)(makeWriteOptions(options), message);
45
+ }
46
+ /**
47
+ * Serialize the message to a JSON string.
48
+ */
49
+ export function toJsonString(schema, message, options) {
50
+ var _a;
51
+ const jsonValue = toJson(schema, message, options);
52
+ return JSON.stringify(jsonValue, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
53
+ }
54
+ /**
55
+ * Serialize a single enum value to JSON.
56
+ */
57
+ export function enumToJson(descEnum, value) {
58
+ var _a;
59
+ if (descEnum.typeName == "google.protobuf.NullValue") {
60
+ return null;
61
+ }
62
+ const name = (_a = descEnum.value[value]) === null || _a === void 0 ? void 0 : _a.name;
63
+ if (name === undefined) {
64
+ throw new Error(`${value} is not a value in ${descEnum}`);
65
+ }
66
+ return name;
67
+ }
68
+ const compiledWriters = new WeakMap();
69
+ /**
70
+ * Return the compiled encoder for a message, compiling it on first use.
71
+ */
72
+ function compiledWriter(desc) {
73
+ let compiled = compiledWriters.get(desc);
74
+ if (compiled === undefined) {
75
+ compiled = compileMessage(desc);
76
+ }
77
+ return compiled;
78
+ }
79
+ function compileMessage(desc) {
80
+ const typeName = desc.typeName;
81
+ const writeWkt = compileWkt(desc);
82
+ if (writeWkt !== undefined) {
83
+ // The field reported in ForeignFieldError. All well-known types with a
84
+ // custom JSON representation have at least one field.
85
+ const foreignField = desc.fields[0];
86
+ const compiledWriter = (opts, message) => {
87
+ if (message.$typeName !== typeName && foreignField !== undefined) {
88
+ throw new FieldError(foreignField, `cannot use ${foreignField} with message ${message.$typeName}`, "ForeignFieldError");
89
+ }
90
+ return writeWkt(opts, message);
91
+ };
92
+ compiledWriters.set(desc, compiledWriter);
93
+ return compiledWriter;
94
+ }
95
+ const sortedFields = desc.fields.concat().sort((a, b) => a.number - b.number);
96
+ // The field reported in ForeignFieldError.
97
+ const foreignField = sortedFields[0];
98
+ const fieldWriters = [];
99
+ const compiledWriter = (opts, message) => {
100
+ if (message.$typeName !== typeName && foreignField !== undefined) {
101
+ throw new FieldError(foreignField, `cannot use ${foreignField} with message ${message.$typeName}`, "ForeignFieldError");
102
+ }
103
+ const json = {};
104
+ for (let i = 0; i < fieldWriters.length; i++) {
105
+ fieldWriters[i](opts, message, json);
106
+ }
107
+ if (opts.registry) {
108
+ writeExtensions(json, opts, opts.registry, message, desc);
109
+ }
110
+ return json;
111
+ };
112
+ // Register before compiling fields, so that recursive message types
113
+ // resolve to this instance instead of compiling endlessly.
114
+ compiledWriters.set(desc, compiledWriter);
115
+ for (const field of sortedFields) {
116
+ fieldWriters.push(compileField(field));
117
+ }
118
+ return compiledWriter;
119
+ }
120
+ /**
121
+ * Compile an encoder for a well-known type with a custom JSON representation,
122
+ * or return undefined for other messages.
123
+ */
124
+ function compileWkt(desc) {
125
+ if (!desc.typeName.startsWith("google.protobuf.")) {
126
+ return undefined;
127
+ }
128
+ switch (desc.typeName) {
129
+ case "google.protobuf.Any":
130
+ return (opts, message) => anyToJson(message, opts);
131
+ case "google.protobuf.Timestamp":
132
+ return (opts, message) => timestampToJson(message);
133
+ case "google.protobuf.Duration":
134
+ return (opts, message) => durationToJson(message);
135
+ case "google.protobuf.FieldMask":
136
+ return (opts, message) => fieldMaskToJson(message);
137
+ case "google.protobuf.Struct":
138
+ return (opts, message) => structToJson(message);
139
+ case "google.protobuf.Value":
140
+ return (opts, message) => valueToJson(message);
141
+ case "google.protobuf.ListValue":
142
+ return (opts, message) => listValueToJson(message);
143
+ default:
144
+ if (isWrapperDesc(desc)) {
145
+ const valueField = desc.fields[0];
146
+ const localName = valueField.localName;
147
+ const zero = scalarZeroValue(valueField.scalar, false);
148
+ const writeScalar = compileScalarValue(valueField);
149
+ return (opts, message) => {
150
+ const value = message[localName];
151
+ return writeScalar(opts, value === undefined ? zero : value);
152
+ };
153
+ }
154
+ return undefined;
155
+ }
156
+ }
157
+ function compileField(field) {
158
+ switch (field.fieldKind) {
159
+ case "scalar":
160
+ case "enum":
161
+ case "message":
162
+ return compileSingularField(field);
163
+ case "list":
164
+ case "map": {
165
+ const writeValue = field.fieldKind == "list"
166
+ ? compileListValue(field)
167
+ : compileMapValue(field);
168
+ const protoName = field.name;
169
+ const jsonKey = field.jsonName;
170
+ const localName = field.localName;
171
+ return (opts, message, json) => {
172
+ const value = writeValue(opts, message[localName]);
173
+ if (value !== undefined) {
174
+ json[opts.useProtoFieldName ? protoName : jsonKey] = value;
175
+ }
176
+ };
177
+ }
178
+ }
179
+ }
180
+ /**
181
+ * Compile an encoder for a singular field: the presence check, and the
182
+ * value encoder.
183
+ */
184
+ function compileSingularField(field) {
185
+ const writeValue = compileSingularValue(field);
186
+ const protoName = field.name;
187
+ const jsonKey = field.jsonName;
188
+ const localName = field.localName;
189
+ if (field.oneof) {
190
+ const oneofLocalName = field.oneof.localName;
191
+ return (opts, message, json) => {
192
+ const oneof = message[oneofLocalName];
193
+ if (oneof.case === localName) {
194
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, oneof.value);
195
+ }
196
+ };
197
+ }
198
+ if (field.presence != IMPLICIT) {
199
+ const requiredError = field.presence == LEGACY_REQUIRED
200
+ ? `cannot encode ${field} to JSON: required field not set`
201
+ : undefined;
202
+ return (opts, message, json) => {
203
+ const value = message[localName];
204
+ // Fields with explicit presence have properties on the prototype
205
+ // chain for default / zero values (except for proto3).
206
+ if (value !== undefined &&
207
+ Object.prototype.hasOwnProperty.call(message, localName)) {
208
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, value);
209
+ }
210
+ else if (requiredError !== undefined) {
211
+ throw new Error(requiredError);
212
+ }
213
+ };
214
+ }
215
+ // Implicit presence: the field is emitted when the value is not the zero
216
+ // value, or when alwaysEmitImplicit is enabled. The zero check is inlined
217
+ // per type, see isScalarZeroValue.
218
+ if (field.fieldKind == "enum") {
219
+ const zero = field.enum.values[0].number;
220
+ return (opts, message, json) => {
221
+ const value = message[localName];
222
+ if (value !== zero || opts.alwaysEmitImplicit) {
223
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, value);
224
+ }
225
+ };
226
+ }
227
+ switch (field.scalar) {
228
+ case ScalarType.BOOL:
229
+ return (opts, message, json) => {
230
+ const value = message[localName];
231
+ if (value !== false || opts.alwaysEmitImplicit) {
232
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, value);
233
+ }
234
+ };
235
+ case ScalarType.STRING:
236
+ return (opts, message, json) => {
237
+ const value = message[localName];
238
+ if (value !== "" || opts.alwaysEmitImplicit) {
239
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, value);
240
+ }
241
+ };
242
+ case ScalarType.BYTES:
243
+ return (opts, message, json) => {
244
+ const value = message[localName];
245
+ if (!(value instanceof Uint8Array) ||
246
+ value.byteLength > 0 ||
247
+ opts.alwaysEmitImplicit) {
248
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, value);
249
+ }
250
+ };
251
+ case ScalarType.DOUBLE:
252
+ case ScalarType.FLOAT:
253
+ return (opts, message, json) => {
254
+ const value = message[localName];
255
+ // Object.is distinguishes -0 from 0.
256
+ if (!Object.is(value, 0) || opts.alwaysEmitImplicit) {
257
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, value);
258
+ }
259
+ };
260
+ default:
261
+ return (opts, message, json) => {
262
+ const value = message[localName];
263
+ // Loose comparison matches 0n, 0 and "0".
264
+ if (value != 0 || opts.alwaysEmitImplicit) {
265
+ json[opts.useProtoFieldName ? protoName : jsonKey] = writeValue(opts, value);
266
+ }
267
+ };
268
+ }
269
+ }
270
+ /**
271
+ * Compile an encoder for the value of a field of any kind. Used for
272
+ * extension values.
273
+ */
274
+ function compileFieldValue(field) {
275
+ switch (field.fieldKind) {
276
+ case "scalar":
277
+ case "enum":
278
+ case "message":
279
+ return compileSingularValue(field);
280
+ case "list":
281
+ return compileListValue(field);
282
+ case "map":
283
+ return compileMapValue(field);
284
+ }
285
+ }
286
+ /**
287
+ * Compile an encoder for the value of a singular field.
288
+ */
289
+ function compileSingularValue(field) {
290
+ switch (field.fieldKind) {
291
+ case "scalar":
292
+ return compileScalarValue(field);
293
+ case "enum":
294
+ return compileEnumValue(field);
295
+ case "message":
296
+ return compileMessageValue(field);
297
+ }
298
+ }
299
+ /**
300
+ * Compile an encoder for the value of a message field.
301
+ */
302
+ function compileMessageValue(field) {
303
+ const { toMessage } = localMessageMapper(field);
304
+ const writeMessage = compiledWriter(field.message);
305
+ return (opts, value) => writeMessage(opts, toMessage(value));
306
+ }
307
+ /**
308
+ * Compile an encoder for a list field value. Returns undefined for an empty
309
+ * list, unless alwaysEmitImplicit is enabled.
310
+ */
311
+ function compileListValue(field) {
312
+ const writeItem = compileListItemValue(field);
313
+ return (opts, value) => {
314
+ const items = value;
315
+ if (items.length == 0 && !opts.alwaysEmitImplicit) {
316
+ return undefined;
317
+ }
318
+ const jsonArray = [];
319
+ for (let i = 0; i < items.length; i++) {
320
+ jsonArray.push(writeItem(opts, items[i]));
321
+ }
322
+ return jsonArray;
323
+ };
324
+ }
325
+ function compileListItemValue(field) {
326
+ switch (field.listKind) {
327
+ case "scalar":
328
+ return compileScalarValue(field);
329
+ case "enum":
330
+ return compileEnumValue(field);
331
+ case "message":
332
+ return compileMessageValue(field);
333
+ }
334
+ }
335
+ /**
336
+ * Compile an encoder for a map field value. Returns undefined for an empty
337
+ * map, unless alwaysEmitImplicit is enabled. Map keys are stored as object
338
+ * keys and are used as JSON keys as-is.
339
+ */
340
+ function compileMapValue(field) {
341
+ const writeMapValue = compileMapEntryValue(field);
342
+ return (opts, value) => {
343
+ const record = value;
344
+ const keys = Object.keys(record);
345
+ if (keys.length == 0 && !opts.alwaysEmitImplicit) {
346
+ return undefined;
347
+ }
348
+ const jsonObject = {};
349
+ for (let i = 0; i < keys.length; i++) {
350
+ const key = keys[i];
351
+ jsonObject[key] = writeMapValue(opts, record[key]);
352
+ }
353
+ return jsonObject;
354
+ };
355
+ }
356
+ function compileMapEntryValue(field) {
357
+ switch (field.mapKind) {
358
+ case "scalar":
359
+ return compileScalarValue(field);
360
+ case "enum":
361
+ return compileEnumValue(field);
362
+ case "message":
363
+ return compileMessageValue(field);
364
+ }
365
+ }
366
+ /**
367
+ * Compile an encoder for an enum value.
368
+ */
369
+ function compileEnumValue(field) {
370
+ const desc = field.enum;
371
+ if (desc.typeName == "google.protobuf.NullValue") {
372
+ return (opts, value) => {
373
+ if (typeof value != "number") {
374
+ throw errorEnumValue(desc, value);
375
+ }
376
+ return null;
377
+ };
378
+ }
379
+ return (opts, value) => {
380
+ var _a, _b;
381
+ if (typeof value != "number") {
382
+ throw errorEnumValue(desc, value);
383
+ }
384
+ if (opts.enumAsInteger) {
385
+ return value;
386
+ }
387
+ // If we don't know the enum value, just return the number.
388
+ return (_b = (_a = desc.value[value]) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : value;
389
+ };
390
+ }
391
+ function errorEnumValue(desc, value) {
392
+ return new Error(`cannot encode ${desc} to JSON: expected number, got ${formatVal(value)}`);
393
+ }
394
+ /**
395
+ * Compile an encoder for a scalar value. Errors report the original field
396
+ * descriptor, which may be a list or map field for items of those fields.
397
+ */
398
+ function compileScalarValue(field) {
399
+ switch (field.scalar) {
400
+ // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
401
+ case ScalarType.INT32:
402
+ case ScalarType.SFIXED32:
403
+ case ScalarType.SINT32:
404
+ case ScalarType.FIXED32:
405
+ case ScalarType.UINT32:
406
+ return (opts, value) => {
407
+ if (typeof value != "number") {
408
+ throw errorScalarValue(field, value);
409
+ }
410
+ return value;
411
+ };
412
+ // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
413
+ // Either numbers or strings are accepted. Exponent notation is also accepted.
414
+ case ScalarType.FLOAT:
415
+ case ScalarType.DOUBLE:
416
+ return (opts, value) => {
417
+ if (typeof value != "number") {
418
+ throw errorScalarValue(field, value);
419
+ }
420
+ if (Number.isNaN(value))
421
+ return "NaN";
422
+ if (value === Number.POSITIVE_INFINITY)
423
+ return "Infinity";
424
+ if (value === Number.NEGATIVE_INFINITY)
425
+ return "-Infinity";
426
+ return value;
427
+ };
428
+ // string:
429
+ case ScalarType.STRING:
430
+ return (opts, value) => {
431
+ if (typeof value != "string") {
432
+ throw errorScalarValue(field, value);
433
+ }
434
+ return value;
435
+ };
436
+ // bool:
437
+ case ScalarType.BOOL:
438
+ return (opts, value) => {
439
+ if (typeof value != "boolean") {
440
+ throw errorScalarValue(field, value);
441
+ }
442
+ return value;
443
+ };
444
+ // JSON value will be a decimal string. Either numbers or strings are accepted.
445
+ case ScalarType.UINT64:
446
+ case ScalarType.FIXED64:
447
+ case ScalarType.INT64:
448
+ case ScalarType.SFIXED64:
449
+ case ScalarType.SINT64:
450
+ return (opts, value) => {
451
+ if (typeof value == "bigint" ||
452
+ typeof value == "string" ||
453
+ (typeof value == "number" && Number.isInteger(value))) {
454
+ return value.toString();
455
+ }
456
+ throw errorScalarValue(field, value);
457
+ };
458
+ // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
459
+ // Either standard or URL-safe base64 encoding with/without paddings are accepted.
460
+ case ScalarType.BYTES:
461
+ return (opts, value) => {
462
+ if (value instanceof Uint8Array) {
463
+ return base64Encode(value);
464
+ }
465
+ throw errorScalarValue(field, value);
466
+ };
467
+ }
468
+ }
469
+ function errorScalarValue(field, value) {
470
+ var _a;
471
+ return new Error(`cannot encode ${field} to JSON: ${(_a = checkField(field, value)) === null || _a === void 0 ? void 0 : _a.message}`);
472
+ }
473
+ /**
474
+ * Write extensions for unknown fields that are found in the registry.
475
+ */
476
+ function writeExtensions(json, opts, registry, message, desc) {
477
+ const unknown = message.$unknown;
478
+ if (unknown === undefined) {
479
+ return;
480
+ }
481
+ const tagSeen = new Set();
482
+ for (let i = 0; i < unknown.length; i++) {
483
+ const { no } = unknown[i];
484
+ // Same tag can appear multiple times, so we
485
+ // keep track and skip identical ones.
486
+ if (!tagSeen.has(no)) {
487
+ tagSeen.add(no);
488
+ const extension = registry.getExtensionFor(desc, no);
489
+ if (!extension) {
490
+ continue;
491
+ }
492
+ const value = getExtension(message, extension);
493
+ const [container, field] = createExtensionContainer(extension, value);
494
+ const local = container[unsafeLocal];
495
+ const jsonValue = compileFieldValue(field)(opts, local[field.localName]);
496
+ if (jsonValue !== undefined) {
497
+ json[extension.jsonName] = jsonValue;
498
+ }
499
+ }
500
+ }
501
+ }
502
+ function anyToJson(val, opts) {
503
+ if (val.typeUrl === "") {
504
+ return {};
505
+ }
506
+ const { registry } = opts;
507
+ let message;
508
+ let desc;
509
+ if (registry) {
510
+ message = anyUnpack(val, registry);
511
+ if (message) {
512
+ desc = registry.getMessage(message.$typeName);
513
+ }
514
+ }
515
+ if (!desc || !message) {
516
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: "${val.typeUrl}" is not in the type registry`);
517
+ }
518
+ const json = hasCustomJsonRepresentation(desc)
519
+ ? {
520
+ value: compiledWriter(desc)(opts, message),
521
+ }
522
+ : compiledWriter(desc)(opts, message);
523
+ json["@type"] = val.typeUrl;
524
+ return json;
525
+ }
526
+ function durationToJson(val) {
527
+ const seconds = Number(val.seconds);
528
+ const nanos = val.nanos;
529
+ if (seconds > durationSecondsMax || seconds < durationSecondsMin) {
530
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: value out of range`);
531
+ }
532
+ if ((seconds > 0 && nanos < 0) || (seconds < 0 && nanos > 0)) {
533
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos sign must match seconds sign`);
534
+ }
535
+ let text = val.seconds.toString();
536
+ if (nanos !== 0) {
537
+ let nanosStr = Math.abs(nanos).toString();
538
+ nanosStr = "0".repeat(9 - nanosStr.length) + nanosStr;
539
+ if (nanosStr.substring(3) === "000000") {
540
+ nanosStr = nanosStr.substring(0, 3);
541
+ }
542
+ else if (nanosStr.substring(6) === "000") {
543
+ nanosStr = nanosStr.substring(0, 6);
544
+ }
545
+ text += "." + nanosStr;
546
+ if (nanos < 0 && seconds == 0) {
547
+ text = "-" + text;
548
+ }
549
+ }
550
+ return text + "s";
551
+ }
552
+ function fieldMaskToJson(val) {
553
+ return val.paths
554
+ .map((p) => {
555
+ if (protoSnakeCase(protoCamelCase(p)) !== p) {
556
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: lowerCamelCase of path name "${p}" is irreversible`);
557
+ }
558
+ return protoCamelCase(p);
559
+ })
560
+ .join(",");
561
+ }
562
+ function structToJson(val) {
563
+ const json = {};
564
+ const keys = Object.keys(val.fields);
565
+ for (let i = 0; i < keys.length; i++) {
566
+ const key = keys[i];
567
+ json[key] = valueToJson(val.fields[key]);
568
+ }
569
+ return json;
570
+ }
571
+ function valueToJson(val) {
572
+ switch (val.kind.case) {
573
+ case "nullValue":
574
+ return null;
575
+ case "numberValue":
576
+ if (!Number.isFinite(val.kind.value)) {
577
+ throw new Error(`${val.$typeName} cannot be NaN or Infinity`);
578
+ }
579
+ return val.kind.value;
580
+ case "boolValue":
581
+ return val.kind.value;
582
+ case "stringValue":
583
+ return val.kind.value;
584
+ case "structValue":
585
+ return structToJson(val.kind.value);
586
+ case "listValue":
587
+ return listValueToJson(val.kind.value);
588
+ default:
589
+ throw new Error(`${val.$typeName} must have a value`);
590
+ }
591
+ }
592
+ function listValueToJson(val) {
593
+ return val.values.map(valueToJson);
594
+ }
595
+ function timestampToJson(val) {
596
+ const ms = Number(val.seconds) * 1000;
597
+ if (ms < timestampMsMin || ms > timestampMsMax) {
598
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);
599
+ }
600
+ if (val.nanos < 0) {
601
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be negative`);
602
+ }
603
+ if (val.nanos > 999999999) {
604
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be greater than 99999999`);
605
+ }
606
+ let z = "Z";
607
+ if (val.nanos > 0) {
608
+ const nanosStr = (val.nanos + 1000000000).toString().substring(1);
609
+ if (nanosStr.substring(3) === "000000") {
610
+ z = "." + nanosStr.substring(0, 3) + "Z";
611
+ }
612
+ else if (nanosStr.substring(6) === "000") {
613
+ z = "." + nanosStr.substring(0, 6) + "Z";
614
+ }
615
+ else {
616
+ z = "." + nanosStr + "Z";
617
+ }
618
+ }
619
+ return new Date(ms).toISOString().replace(".000Z", z);
620
+ }