tardis-machine 18.0.0 → 18.0.1

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 (208) hide show
  1. package/benchmark.js +35 -14
  2. package/bin/tardis-machine.js +0 -1
  3. package/dist/tardismachine.d.ts.map +1 -1
  4. package/dist/tardismachine.js +20 -55
  5. package/dist/tardismachine.js.map +1 -1
  6. package/dist/ws/replay.d.ts +2 -2
  7. package/dist/ws/replay.d.ts.map +1 -1
  8. package/dist/ws/replay.js +11 -5
  9. package/dist/ws/replay.js.map +1 -1
  10. package/dist/ws/replaynormalized.d.ts +2 -2
  11. package/dist/ws/replaynormalized.d.ts.map +1 -1
  12. package/dist/ws/replaynormalized.js +7 -5
  13. package/dist/ws/replaynormalized.js.map +1 -1
  14. package/dist/ws/server.d.ts +20 -0
  15. package/dist/ws/server.d.ts.map +1 -0
  16. package/dist/ws/server.js +154 -0
  17. package/dist/ws/server.js.map +1 -0
  18. package/dist/ws/streamnormalized.d.ts +2 -2
  19. package/dist/ws/streamnormalized.d.ts.map +1 -1
  20. package/dist/ws/streamnormalized.js +7 -5
  21. package/dist/ws/streamnormalized.js.map +1 -1
  22. package/dist/ws/subscriptionsmappers.d.ts +1 -1
  23. package/dist/ws/subscriptionsmappers.d.ts.map +1 -1
  24. package/node_modules/tardis-dev/README.md +1 -1
  25. package/node_modules/tardis-dev/dist/debug.d.ts +12 -2
  26. package/node_modules/tardis-dev/dist/debug.d.ts.map +1 -1
  27. package/node_modules/tardis-dev/dist/debug.js +2 -1
  28. package/node_modules/tardis-dev/dist/debug.js.map +1 -1
  29. package/node_modules/tardis-dev/dist/handy.d.ts +3 -1
  30. package/node_modules/tardis-dev/dist/handy.d.ts.map +1 -1
  31. package/node_modules/tardis-dev/dist/handy.js +72 -86
  32. package/node_modules/tardis-dev/dist/handy.js.map +1 -1
  33. package/node_modules/tardis-dev/dist/realtimefeeds/mexc.d.ts +0 -7
  34. package/node_modules/tardis-dev/dist/realtimefeeds/mexc.d.ts.map +1 -1
  35. package/node_modules/tardis-dev/dist/realtimefeeds/mexc.js +2 -70
  36. package/node_modules/tardis-dev/dist/realtimefeeds/mexc.js.map +1 -1
  37. package/node_modules/tardis-dev/dist/realtimefeeds/mexcprotobuf.d.ts +43 -0
  38. package/node_modules/tardis-dev/dist/realtimefeeds/mexcprotobuf.d.ts.map +1 -0
  39. package/node_modules/tardis-dev/dist/realtimefeeds/mexcprotobuf.js +294 -0
  40. package/node_modules/tardis-dev/dist/realtimefeeds/mexcprotobuf.js.map +1 -0
  41. package/node_modules/tardis-dev/dist/realtimefeeds/realtimefeed.d.ts +3 -3
  42. package/node_modules/tardis-dev/dist/realtimefeeds/realtimefeed.d.ts.map +1 -1
  43. package/node_modules/tardis-dev/dist/realtimefeeds/realtimefeed.js +3 -3
  44. package/node_modules/tardis-dev/dist/realtimefeeds/realtimefeed.js.map +1 -1
  45. package/node_modules/tardis-dev/dist/worker.js +2 -2
  46. package/node_modules/tardis-dev/dist/worker.js.map +1 -1
  47. package/node_modules/tardis-dev/package.json +3 -7
  48. package/node_modules/tardis-dev/src/debug.ts +16 -1
  49. package/node_modules/tardis-dev/src/external-modules.d.ts +5 -0
  50. package/node_modules/tardis-dev/src/handy.ts +93 -89
  51. package/node_modules/tardis-dev/src/realtimefeeds/mexc.ts +2 -72
  52. package/node_modules/tardis-dev/src/realtimefeeds/mexcprotobuf.ts +364 -0
  53. package/node_modules/tardis-dev/src/realtimefeeds/realtimefeed.ts +5 -5
  54. package/node_modules/tardis-dev/src/worker.ts +2 -2
  55. package/package.json +8 -34
  56. package/src/tardismachine.ts +25 -66
  57. package/src/ws/replay.ts +13 -8
  58. package/src/ws/replaynormalized.ts +8 -6
  59. package/src/ws/server.ts +173 -0
  60. package/src/ws/streamnormalized.ts +8 -6
  61. package/src/ws/subscriptionsmappers.ts +1 -1
  62. package/node_modules/follow-redirects/LICENSE +0 -18
  63. package/node_modules/follow-redirects/README.md +0 -157
  64. package/node_modules/follow-redirects/debug.js +0 -15
  65. package/node_modules/follow-redirects/http.js +0 -1
  66. package/node_modules/follow-redirects/https.js +0 -1
  67. package/node_modules/follow-redirects/index.js +0 -709
  68. package/node_modules/follow-redirects/package.json +0 -58
  69. package/node_modules/long/LICENSE +0 -202
  70. package/node_modules/long/README.md +0 -286
  71. package/node_modules/long/index.d.ts +0 -2
  72. package/node_modules/long/index.js +0 -1581
  73. package/node_modules/long/package.json +0 -58
  74. package/node_modules/long/types.d.ts +0 -474
  75. package/node_modules/long/umd/index.d.ts +0 -3
  76. package/node_modules/long/umd/index.js +0 -1622
  77. package/node_modules/long/umd/package.json +0 -3
  78. package/node_modules/long/umd/types.d.ts +0 -474
  79. package/node_modules/protobufjs/LICENSE +0 -39
  80. package/node_modules/protobufjs/README.md +0 -440
  81. package/node_modules/protobufjs/dist/light/protobuf.js +0 -9276
  82. package/node_modules/protobufjs/dist/light/protobuf.js.map +0 -1
  83. package/node_modules/protobufjs/dist/light/protobuf.min.js +0 -8
  84. package/node_modules/protobufjs/dist/light/protobuf.min.js.map +0 -1
  85. package/node_modules/protobufjs/dist/minimal/protobuf.js +0 -3556
  86. package/node_modules/protobufjs/dist/minimal/protobuf.js.map +0 -1
  87. package/node_modules/protobufjs/dist/minimal/protobuf.min.js +0 -8
  88. package/node_modules/protobufjs/dist/minimal/protobuf.min.js.map +0 -1
  89. package/node_modules/protobufjs/dist/protobuf.js +0 -11233
  90. package/node_modules/protobufjs/dist/protobuf.js.map +0 -1
  91. package/node_modules/protobufjs/dist/protobuf.min.js +0 -8
  92. package/node_modules/protobufjs/dist/protobuf.min.js.map +0 -1
  93. package/node_modules/protobufjs/ext/README.md +0 -70
  94. package/node_modules/protobufjs/ext/debug/README.md +0 -4
  95. package/node_modules/protobufjs/ext/debug/index.js +0 -71
  96. package/node_modules/protobufjs/ext/descriptor/README.md +0 -5
  97. package/node_modules/protobufjs/ext/descriptor/index.d.ts +0 -2
  98. package/node_modules/protobufjs/ext/descriptor/index.js +0 -2
  99. package/node_modules/protobufjs/ext/descriptor.d.ts +0 -87
  100. package/node_modules/protobufjs/ext/descriptor.generated.d.ts +0 -417
  101. package/node_modules/protobufjs/ext/descriptor.js +0 -1356
  102. package/node_modules/protobufjs/ext/protojson.LICENSE +0 -201
  103. package/node_modules/protobufjs/ext/protojson.d.ts +0 -20
  104. package/node_modules/protobufjs/ext/protojson.generated.d.ts +0 -49
  105. package/node_modules/protobufjs/ext/protojson.js +0 -951
  106. package/node_modules/protobufjs/ext/textformat.d.ts +0 -19
  107. package/node_modules/protobufjs/ext/textformat.generated.d.ts +0 -33
  108. package/node_modules/protobufjs/ext/textformat.js +0 -1276
  109. package/node_modules/protobufjs/google/LICENSE +0 -27
  110. package/node_modules/protobufjs/google/README.md +0 -1
  111. package/node_modules/protobufjs/google/api/annotations.json +0 -85
  112. package/node_modules/protobufjs/google/api/annotations.proto +0 -11
  113. package/node_modules/protobufjs/google/api/http.json +0 -87
  114. package/node_modules/protobufjs/google/api/http.proto +0 -31
  115. package/node_modules/protobufjs/google/protobuf/api.json +0 -124
  116. package/node_modules/protobufjs/google/protobuf/api.proto +0 -34
  117. package/node_modules/protobufjs/google/protobuf/compiler/plugin.json +0 -126
  118. package/node_modules/protobufjs/google/protobuf/compiler/plugin.proto +0 -47
  119. package/node_modules/protobufjs/google/protobuf/descriptor.json +0 -1465
  120. package/node_modules/protobufjs/google/protobuf/descriptor.proto +0 -527
  121. package/node_modules/protobufjs/google/protobuf/source_context.json +0 -21
  122. package/node_modules/protobufjs/google/protobuf/source_context.proto +0 -7
  123. package/node_modules/protobufjs/google/protobuf/type.json +0 -209
  124. package/node_modules/protobufjs/google/protobuf/type.proto +0 -89
  125. package/node_modules/protobufjs/index.d.ts +0 -3203
  126. package/node_modules/protobufjs/index.js +0 -4
  127. package/node_modules/protobufjs/light.d.ts +0 -2
  128. package/node_modules/protobufjs/light.js +0 -4
  129. package/node_modules/protobufjs/minimal.d.ts +0 -2
  130. package/node_modules/protobufjs/minimal.js +0 -4
  131. package/node_modules/protobufjs/package.json +0 -93
  132. package/node_modules/protobufjs/src/common.js +0 -405
  133. package/node_modules/protobufjs/src/converter.js +0 -352
  134. package/node_modules/protobufjs/src/decoder.js +0 -279
  135. package/node_modules/protobufjs/src/encoder.js +0 -123
  136. package/node_modules/protobufjs/src/enum.js +0 -239
  137. package/node_modules/protobufjs/src/field.js +0 -505
  138. package/node_modules/protobufjs/src/index-light.js +0 -104
  139. package/node_modules/protobufjs/src/index-minimal.js +0 -35
  140. package/node_modules/protobufjs/src/index.js +0 -12
  141. package/node_modules/protobufjs/src/mapfield.js +0 -144
  142. package/node_modules/protobufjs/src/message.js +0 -137
  143. package/node_modules/protobufjs/src/method.js +0 -183
  144. package/node_modules/protobufjs/src/namespace.js +0 -574
  145. package/node_modules/protobufjs/src/object.js +0 -392
  146. package/node_modules/protobufjs/src/oneof.js +0 -233
  147. package/node_modules/protobufjs/src/parse.js +0 -1111
  148. package/node_modules/protobufjs/src/reader.js +0 -788
  149. package/node_modules/protobufjs/src/reader_buffer.js +0 -77
  150. package/node_modules/protobufjs/src/root.js +0 -424
  151. package/node_modules/protobufjs/src/roots.js +0 -18
  152. package/node_modules/protobufjs/src/rpc/service.js +0 -155
  153. package/node_modules/protobufjs/src/rpc.js +0 -36
  154. package/node_modules/protobufjs/src/service.js +0 -206
  155. package/node_modules/protobufjs/src/tokenize.js +0 -421
  156. package/node_modules/protobufjs/src/type.js +0 -676
  157. package/node_modules/protobufjs/src/types.js +0 -196
  158. package/node_modules/protobufjs/src/typescript.js +0 -25
  159. package/node_modules/protobufjs/src/util/aspromise.d.ts +0 -13
  160. package/node_modules/protobufjs/src/util/aspromise.js +0 -52
  161. package/node_modules/protobufjs/src/util/base64.d.ts +0 -32
  162. package/node_modules/protobufjs/src/util/base64.js +0 -146
  163. package/node_modules/protobufjs/src/util/codegen.d.ts +0 -31
  164. package/node_modules/protobufjs/src/util/codegen.js +0 -118
  165. package/node_modules/protobufjs/src/util/eventemitter.d.ts +0 -45
  166. package/node_modules/protobufjs/src/util/eventemitter.js +0 -86
  167. package/node_modules/protobufjs/src/util/fetch.d.ts +0 -56
  168. package/node_modules/protobufjs/src/util/fetch.js +0 -112
  169. package/node_modules/protobufjs/src/util/float.d.ts +0 -83
  170. package/node_modules/protobufjs/src/util/float.js +0 -335
  171. package/node_modules/protobufjs/src/util/fs.js +0 -11
  172. package/node_modules/protobufjs/src/util/longbits.js +0 -204
  173. package/node_modules/protobufjs/src/util/minimal.js +0 -511
  174. package/node_modules/protobufjs/src/util/path.d.ts +0 -22
  175. package/node_modules/protobufjs/src/util/path.js +0 -100
  176. package/node_modules/protobufjs/src/util/patterns.js +0 -7
  177. package/node_modules/protobufjs/src/util/pool.d.ts +0 -32
  178. package/node_modules/protobufjs/src/util/pool.js +0 -48
  179. package/node_modules/protobufjs/src/util/utf8.d.ts +0 -24
  180. package/node_modules/protobufjs/src/util/utf8.js +0 -190
  181. package/node_modules/protobufjs/src/util.js +0 -264
  182. package/node_modules/protobufjs/src/verifier.js +0 -185
  183. package/node_modules/protobufjs/src/wrappers.js +0 -106
  184. package/node_modules/protobufjs/src/writer.js +0 -700
  185. package/node_modules/protobufjs/src/writer_buffer.js +0 -100
  186. package/node_modules/protobufjs/tsconfig.json +0 -6
  187. package/node_modules/uWebSockets.js/ESM_wrapper.mjs +0 -53
  188. package/node_modules/uWebSockets.js/LICENSE +0 -201
  189. package/node_modules/uWebSockets.js/README.md +0 -6
  190. package/node_modules/uWebSockets.js/index.d.ts +0 -468
  191. package/node_modules/uWebSockets.js/package.json +0 -12
  192. package/node_modules/uWebSockets.js/source_commit +0 -1
  193. package/node_modules/uWebSockets.js/uws.js +0 -76
  194. package/node_modules/uWebSockets.js/uws_darwin_arm64_127.node +0 -0
  195. package/node_modules/uWebSockets.js/uws_darwin_arm64_137.node +0 -0
  196. package/node_modules/uWebSockets.js/uws_darwin_arm64_147.node +0 -0
  197. package/node_modules/uWebSockets.js/uws_darwin_x64_127.node +0 -0
  198. package/node_modules/uWebSockets.js/uws_darwin_x64_137.node +0 -0
  199. package/node_modules/uWebSockets.js/uws_darwin_x64_147.node +0 -0
  200. package/node_modules/uWebSockets.js/uws_linux_arm64_127.node +0 -0
  201. package/node_modules/uWebSockets.js/uws_linux_arm64_137.node +0 -0
  202. package/node_modules/uWebSockets.js/uws_linux_arm64_147.node +0 -0
  203. package/node_modules/uWebSockets.js/uws_linux_x64_127.node +0 -0
  204. package/node_modules/uWebSockets.js/uws_linux_x64_137.node +0 -0
  205. package/node_modules/uWebSockets.js/uws_linux_x64_147.node +0 -0
  206. package/node_modules/uWebSockets.js/uws_win32_x64_127.node +0 -0
  207. package/node_modules/uWebSockets.js/uws_win32_x64_137.node +0 -0
  208. package/node_modules/uWebSockets.js/uws_win32_x64_147.node +0 -0
@@ -1,1276 +0,0 @@
1
- "use strict";
2
- var protobuf = require("../light");
3
-
4
- var Type = protobuf.Type,
5
- Enum = protobuf.Enum,
6
- Field = protobuf.Field,
7
- Reader = protobuf.Reader,
8
- types = protobuf.types,
9
- util = protobuf.util;
10
-
11
- var textformat = protobuf.textformat = module.exports = {};
12
-
13
- /**
14
- * Maximum recursion depth for formatting length-delimited unknown fields.
15
- * @name unknownRecursionLimit
16
- * @type {number}
17
- */
18
- textformat.unknownRecursionLimit = 10;
19
-
20
- var punct = {
21
- "{": true,
22
- "}": true,
23
- "<": true,
24
- ">": true,
25
- ":": true,
26
- "[": true,
27
- "]": true,
28
- ",": true,
29
- ";": true,
30
- "-": true
31
- };
32
-
33
- var identRe = /^[A-Za-z_][A-Za-z0-9_]*$/,
34
- numberStartRe = /[0-9]/,
35
- wordCharRe = /[A-Za-z0-9_]/,
36
- hexRe = /^[0-9A-Fa-f]$/,
37
- octRe = /^[0-7]$/,
38
- floatRe = /^(?:[0-9]+\.[0-9]*(?:[eE][+-]?[0-9]+)?[fF]?|\.[0-9]+(?:[eE][+-]?[0-9]+)?[fF]?|[0-9]+[eE][+-]?[0-9]+[fF]?|[0-9]+[fF])/,
39
- hexIntRe = /^0[xX][0-9A-Fa-f]+/,
40
- octIntRe = /^0[0-7]+/,
41
- decIntRe = /^(?:0|[1-9][0-9]*)/;
42
-
43
- /**
44
- * Parses protobuf text format using the specified reflected message type.
45
- * @param {Type} type Reflected message type
46
- * @param {string} text Text format input
47
- * @returns {Message<{}>} Message instance
48
- * @private
49
- */
50
- function parseText(type, text) {
51
- if (!(type instanceof Type))
52
- throw TypeError("type must be a Type");
53
- type.root.resolveAll();
54
- var parser = new Parser(String(text));
55
- var object = parser.parseMessage(type, null, 0);
56
- parser.expectEnd();
57
- var err = verifyTextMessage(type, object),
58
- message;
59
- if (err)
60
- throw Error(err);
61
- message = type.create(object);
62
- return message;
63
- }
64
-
65
- /**
66
- * Text format options.
67
- * @interface ITextFormatOptions
68
- * @property {boolean} [unknowns=false] Also includes and formats unknown fields.
69
- */
70
-
71
- /**
72
- * Parses a message from protobuf text format using the specified reflected type.
73
- * @function fromText
74
- * @name fromText
75
- * @param {$protobuf.Type} type Reflected message type
76
- * @param {string} text Text format input
77
- * @returns {$protobuf.Message<{}>} Message instance
78
- */
79
- textformat.fromText = function fromText(type, text) {
80
- return parseText(type, text);
81
- };
82
-
83
- /**
84
- * Formats a message as protobuf text format using the specified reflected type.
85
- * @function toText
86
- * @name toText
87
- * @param {$protobuf.Type} type Reflected message type
88
- * @param {$protobuf.Message<{}>|Object.<string,*>} message Message instance or plain object
89
- * @param {ITextFormatOptions} [options] Text format options
90
- * @returns {string} Text format output
91
- */
92
- textformat.toText = function toText(type, message, options) {
93
- if (!(type instanceof Type))
94
- throw TypeError("type must be a Type");
95
- type.root.resolveAll();
96
- var lines = [];
97
- writeMessage(type, message, lines, 0, options || {}, 0);
98
- return lines.join("\n");
99
- };
100
-
101
- /**
102
- * Installs reflected {@link Type} convenience methods.
103
- * @function install
104
- * @name install
105
- * @returns {undefined}
106
- */
107
- textformat.install = function install() {
108
- /**
109
- * Parses a message of this type from protobuf text format. Convenience for {@link textformat.fromText}.
110
- * @param {string} text Text format input
111
- * @returns {Message<{}>} Message instance
112
- */
113
- Type.prototype.fromText = function fromText(text) {
114
- return textformat.fromText(this, text);
115
- };
116
-
117
- /**
118
- * Formats a message of this type as protobuf text format. Convenience for {@link textformat.toText}.
119
- * @param {Message<{}>|Object.<string,*>} message Message instance or plain object
120
- * @param {ITextFormatOptions} [options] Text format options
121
- * @returns {string} Text format output
122
- */
123
- Type.prototype.toText = function toText(message, options) {
124
- return textformat.toText(this, message, options);
125
- };
126
- };
127
-
128
- function Tokenizer(source) {
129
- this.source = source;
130
- this.pos = 0;
131
- this.len = source.length;
132
- this.line = 1;
133
- this.stack = [];
134
- }
135
-
136
- Tokenizer.prototype.error = function error(message) {
137
- return Error(message + " (line " + this.line + ")");
138
- };
139
-
140
- Tokenizer.prototype.peek = function peek() {
141
- var token = this.next();
142
- this.push(token);
143
- return token;
144
- };
145
-
146
- Tokenizer.prototype.push = function push(token) {
147
- if (token)
148
- this.stack.unshift(token);
149
- };
150
-
151
- Tokenizer.prototype.next = function next() {
152
- if (this.stack.length)
153
- return this.stack.shift();
154
- this.skipSpace();
155
- if (this.pos >= this.len)
156
- return null;
157
-
158
- var ch = this.source.charAt(this.pos);
159
- if (ch === "\"" || ch === "'")
160
- return this.readString(ch);
161
- if (ch === "." && numberStartRe.test(this.source.charAt(this.pos + 1)) || numberStartRe.test(ch))
162
- return this.readNumber();
163
- if (punct[ch]) {
164
- ++this.pos;
165
- return { type: "punct", value: ch, raw: ch, line: this.line };
166
- }
167
- return this.readWord();
168
- };
169
-
170
- Tokenizer.prototype.expect = function expect(value) {
171
- var token = this.next();
172
- if (!token || token.value !== value)
173
- throw this.error("expected '" + value + "'");
174
- return token;
175
- };
176
-
177
- Tokenizer.prototype.skip = function skip(value) {
178
- var token = this.next();
179
- if (token && token.value === value)
180
- return true;
181
- this.push(token);
182
- return false;
183
- };
184
-
185
- Tokenizer.prototype.skipSpace = function skipSpace() {
186
- var ch;
187
- while (this.pos < this.len) {
188
- ch = this.source.charAt(this.pos);
189
- if (ch === "#") {
190
- while (this.pos < this.len && this.source.charAt(this.pos) !== "\n")
191
- ++this.pos;
192
- } else if (ch === " " || ch === "\t" || ch === "\v" || ch === "\f" || ch === "\r") {
193
- ++this.pos;
194
- } else if (ch === "\n") {
195
- ++this.pos;
196
- ++this.line;
197
- } else
198
- break;
199
- }
200
- };
201
-
202
- Tokenizer.prototype.readWord = function readWord() {
203
- var start = this.pos,
204
- ch;
205
- while (this.pos < this.len) {
206
- ch = this.source.charAt(this.pos);
207
- if (ch === "#" || ch === "\"" || ch === "'" || punct[ch] || /\s/.test(ch))
208
- break;
209
- ++this.pos;
210
- }
211
- if (start === this.pos)
212
- throw this.error("illegal character '" + this.source.charAt(this.pos) + "'");
213
- var raw = this.source.substring(start, this.pos);
214
- return { type: "word", value: raw, raw: raw, line: this.line };
215
- };
216
-
217
- Tokenizer.prototype.readNumber = function readNumber() {
218
- var rest = this.source.substring(this.pos),
219
- matches = [
220
- floatRe.exec(rest),
221
- hexIntRe.exec(rest),
222
- octIntRe.exec(rest),
223
- decIntRe.exec(rest)
224
- ],
225
- raw = null;
226
- for (var i = 0; i < matches.length; ++i)
227
- if (matches[i] && (!raw || matches[i][0].length > raw.length))
228
- raw = matches[i][0];
229
- if (!raw)
230
- throw this.error("illegal number");
231
- this.pos += raw.length;
232
- if (this.pos < this.len && wordCharRe.test(this.source.charAt(this.pos)))
233
- throw this.error("illegal number '" + raw + this.source.charAt(this.pos) + "'");
234
- return { type: "number", value: raw, raw: raw, line: this.line };
235
- };
236
-
237
- Tokenizer.prototype.readString = function readString(quote) {
238
- var bytes = [],
239
- ch,
240
- code;
241
- ++this.pos;
242
- while (this.pos < this.len) {
243
- ch = this.source.charAt(this.pos++);
244
- if (ch === quote)
245
- return { type: "string", value: bytes, raw: quote, line: this.line };
246
- if (ch === "\n")
247
- throw this.error("illegal string");
248
- if (ch !== "\\") {
249
- code = ch.charCodeAt(0);
250
- if ((code & 0xFC00) === 0xD800 && this.pos < this.len) {
251
- var next = this.source.charCodeAt(this.pos);
252
- if ((next & 0xFC00) === 0xDC00) {
253
- ++this.pos;
254
- code = 0x10000 + ((code & 0x03FF) << 10) + (next & 0x03FF);
255
- }
256
- }
257
- pushUtf8(bytes, code);
258
- continue;
259
- }
260
- if (this.pos >= this.len)
261
- throw this.error("illegal string escape");
262
- ch = this.source.charAt(this.pos++);
263
- switch (ch) {
264
- case "a": bytes.push(7); break;
265
- case "b": bytes.push(8); break;
266
- case "f": bytes.push(12); break;
267
- case "n": bytes.push(10); break;
268
- case "r": bytes.push(13); break;
269
- case "t": bytes.push(9); break;
270
- case "v": bytes.push(11); break;
271
- case "?": bytes.push(63); break;
272
- case "\\": bytes.push(92); break;
273
- case "'": bytes.push(39); break;
274
- case "\"": bytes.push(34); break;
275
- case "x":
276
- case "X":
277
- bytes.push(this.readHexEscape());
278
- break;
279
- case "u":
280
- pushUtf8(bytes, this.readCodePoint(4));
281
- break;
282
- case "U":
283
- pushUtf8(bytes, this.readCodePoint(8));
284
- break;
285
- default:
286
- if (octRe.test(ch)) {
287
- bytes.push(this.readOctEscape(ch));
288
- break;
289
- }
290
- throw this.error("illegal string escape '\\" + ch + "'");
291
- }
292
- }
293
- throw this.error("unterminated string");
294
- };
295
-
296
- Tokenizer.prototype.readHexEscape = function readHexEscape() {
297
- var value = 0,
298
- count = 0,
299
- ch;
300
- while (count < 2 && this.pos < this.len && hexRe.test(ch = this.source.charAt(this.pos))) {
301
- value = value * 16 + parseInt(ch, 16);
302
- ++this.pos;
303
- ++count;
304
- }
305
- if (!count)
306
- throw this.error("illegal hex escape");
307
- return value;
308
- };
309
-
310
- Tokenizer.prototype.readOctEscape = function readOctEscape(first) {
311
- var value = parseInt(first, 8),
312
- count = 1,
313
- ch;
314
- while (count < 3 && this.pos < this.len && octRe.test(ch = this.source.charAt(this.pos))) {
315
- value = value * 8 + parseInt(ch, 8);
316
- ++this.pos;
317
- ++count;
318
- }
319
- return value & 255;
320
- };
321
-
322
- Tokenizer.prototype.readCodePoint = function readCodePoint(size) {
323
- var raw = this.source.substring(this.pos, this.pos + size);
324
- if (raw.length !== size || !/^[0-9A-Fa-f]+$/.test(raw))
325
- throw this.error("illegal unicode escape");
326
- this.pos += size;
327
- var code = parseInt(raw, 16);
328
- if (code > 0x10FFFF || code >= 0xD800 && code <= 0xDFFF)
329
- throw this.error("illegal unicode escape");
330
- return code;
331
- };
332
-
333
- function Parser(source) {
334
- this.tn = new Tokenizer(source);
335
- }
336
-
337
- Parser.prototype.error = function error(message) {
338
- throw this.tn.error(message);
339
- };
340
-
341
- Parser.prototype.expectEnd = function expectEnd() {
342
- var token = this.tn.next();
343
- if (token)
344
- this.error("unexpected token '" + token.value + "'");
345
- };
346
-
347
- Parser.prototype.parseMessage = function parseMessage(type, end, depth) {
348
- if (depth > util.recursionLimit)
349
- this.error("max depth exceeded");
350
- var object = {},
351
- seen = {};
352
- for (;;) {
353
- var token = this.tn.peek();
354
- if (!token) {
355
- if (end)
356
- this.error("expected '" + end + "'");
357
- return object;
358
- }
359
- if (end && token.value === end) {
360
- this.tn.next();
361
- return object;
362
- }
363
- if (isSeparator(token))
364
- this.error("unexpected separator");
365
- this.parseField(type, object, seen, depth);
366
- token = this.tn.peek();
367
- if (token && isSeparator(token)) {
368
- this.tn.next();
369
- token = this.tn.peek();
370
- if (token && isSeparator(token))
371
- this.error("unexpected separator");
372
- }
373
- }
374
- };
375
-
376
- Parser.prototype.parseField = function parseField(type, object, seen, depth) {
377
- var info = this.readFieldName(type);
378
- if (info.any) {
379
- this.parseAny(type, info.name, object, seen, depth);
380
- return;
381
- }
382
- if (!info.field) {
383
- this.skipReservedValue(depth);
384
- return;
385
- }
386
-
387
- var field = info.field.resolve();
388
- if (field.map) {
389
- this.parseMessageFieldDelimiter();
390
- this.parseMapField(field, object, depth);
391
- } else if (field.resolvedType instanceof Type) {
392
- this.parseMessageFieldDelimiter();
393
- if (this.tn.skip("[")) {
394
- if (!field.repeated)
395
- this.error(field.fullName + ": list value specified for singular field");
396
- if (!this.tn.skip("]")) {
397
- do {
398
- addField(object, seen, field, this.parseMessageValue(field.resolvedType, depth));
399
- } while (this.tn.skip(","));
400
- this.tn.expect("]");
401
- }
402
- } else
403
- addField(object, seen, field, this.parseMessageValue(field.resolvedType, depth));
404
- } else {
405
- this.tn.expect(":");
406
- if (this.tn.skip("[")) {
407
- if (!field.repeated)
408
- this.error(field.fullName + ": list value specified for singular field");
409
- if (!this.tn.skip("]")) {
410
- do {
411
- addField(object, seen, field, this.parseScalar(field));
412
- } while (this.tn.skip(","));
413
- this.tn.expect("]");
414
- }
415
- } else
416
- addField(object, seen, field, this.parseScalar(field));
417
- }
418
- };
419
-
420
- Parser.prototype.readFieldName = function readFieldName(type) {
421
- var token = this.tn.next();
422
- if (!token)
423
- this.error("expected field name");
424
- if (token.value === "[") {
425
- var bracketName = this.readBracketName();
426
- if (type.fullName === ".google.protobuf.Any" && bracketName.indexOf("/") >= 0)
427
- return { any: true, name: bracketName };
428
- return { field: lookupExtension(type, bracketName), name: bracketName };
429
- }
430
- if (token.type !== "word" || !identRe.test(token.value))
431
- this.error("illegal field name '" + token.value + "'");
432
-
433
- var field = lookupField(type, token.value);
434
- if (!field) {
435
- if (type.isReservedName(token.value))
436
- return { name: token.value };
437
- this.error(type.fullName + ": unknown field '" + token.value + "'");
438
- }
439
- return { field: field, name: token.value };
440
- };
441
-
442
- Parser.prototype.readBracketName = function readBracketName() {
443
- var parts = [],
444
- token;
445
- while (token = this.tn.next()) {
446
- if (token.value === "]")
447
- return parts.join("");
448
- parts.push(token.raw || token.value);
449
- }
450
- this.error("unterminated bracketed field name");
451
- return null;
452
- };
453
-
454
- Parser.prototype.parseAny = function parseAny(type, typeUrl, object, seen, depth) {
455
- this.parseMessageFieldDelimiter();
456
- validateTypeUrl(typeUrl);
457
- var typeName = typeUrl.substring(typeUrl.lastIndexOf("/") + 1),
458
- embeddedType = type.root.lookupType(typeName.charAt(0) === "." ? typeName : "." + typeName),
459
- embedded = this.parseMessageValue(embeddedType, depth),
460
- typeUrlField = lookupField(type, "type_url") || lookupField(type, "typeUrl"),
461
- valueField = lookupField(type, "value");
462
- addField(object, seen, typeUrlField, typeUrl);
463
- addField(object, seen, valueField, embeddedType.encode(embedded).finish());
464
- };
465
-
466
- Parser.prototype.parseMapField = function parseMapField(field, object, depth) {
467
- var map = getField(object, field, {});
468
- if (this.tn.skip("[")) {
469
- if (!this.tn.skip("]")) {
470
- do {
471
- addMapEntry(field, map, this.parseMapEntry(field, depth + 1));
472
- } while (this.tn.skip(","));
473
- this.tn.expect("]");
474
- }
475
- } else
476
- addMapEntry(field, map, this.parseMapEntry(field, depth + 1));
477
- };
478
-
479
- Parser.prototype.parseMessageFieldDelimiter = function parseMessageFieldDelimiter() {
480
- this.tn.skip(":");
481
- };
482
-
483
- Parser.prototype.parseMapEntry = function parseMapEntry(field, depth) {
484
- if (depth > util.recursionLimit)
485
- this.error("max depth exceeded");
486
- var end;
487
- if (this.tn.skip("{"))
488
- end = "}";
489
- else if (this.tn.skip("<"))
490
- end = ">";
491
- else
492
- this.error("expected map entry");
493
-
494
- var entry = {},
495
- token,
496
- valueField = mapValueField(field),
497
- keyField = mapKeyField(field);
498
- for (;;) {
499
- token = this.tn.peek();
500
- if (!token)
501
- this.error("expected '" + end + "'");
502
- if (token.value === end) {
503
- this.tn.next();
504
- return entry;
505
- }
506
- if (isSeparator(token))
507
- this.error("unexpected separator");
508
- token = this.tn.next();
509
- if (token.type !== "word")
510
- this.error("expected map field name");
511
- if (token.value === "key") {
512
- this.tn.expect(":");
513
- entry.key = this.parseScalar(keyField);
514
- } else if (token.value === "value") {
515
- if (valueField.resolvedType instanceof Type) {
516
- this.parseMessageFieldDelimiter();
517
- entry.value = this.parseMessageValue(valueField.resolvedType, depth);
518
- } else {
519
- this.tn.expect(":");
520
- entry.value = this.parseScalar(valueField);
521
- }
522
- } else
523
- this.error("unknown map field '" + token.value + "'");
524
- token = this.tn.peek();
525
- if (token && isSeparator(token)) {
526
- this.tn.next();
527
- token = this.tn.peek();
528
- if (token && isSeparator(token))
529
- this.error("unexpected separator");
530
- }
531
- }
532
- };
533
-
534
- Parser.prototype.parseMessageValue = function parseMessageValue(type, depth) {
535
- var end;
536
- if (this.tn.skip("{"))
537
- end = "}";
538
- else if (this.tn.skip("<"))
539
- end = ">";
540
- else
541
- this.error("expected message value");
542
- return this.parseMessage(type, end, depth + 1);
543
- };
544
-
545
- Parser.prototype.parseScalar = function parseScalar(field) {
546
- if (field.type === "string" || field.type === "bytes") {
547
- var bytes = this.readStringBytes();
548
- return field.type === "bytes" ? util.newBuffer(bytes) : utf8Read(bytes, true);
549
- }
550
-
551
- var sign = 1;
552
- if (this.tn.skip("-"))
553
- sign = -1;
554
-
555
- var token = this.tn.next();
556
- if (!token)
557
- this.error("expected value");
558
-
559
- if (field.resolvedType instanceof Enum)
560
- return parseEnum(field, token, sign);
561
-
562
- switch (field.type) {
563
- case "double":
564
- case "float":
565
- return parseFloatValue(token, sign);
566
- case "bool":
567
- return parseBool(token, sign);
568
- case "int32":
569
- case "sint32":
570
- case "sfixed32":
571
- return parseInteger(token, sign, false, 32);
572
- case "uint32":
573
- case "fixed32":
574
- return parseInteger(token, sign, true, 32);
575
- case "int64":
576
- case "sint64":
577
- case "sfixed64":
578
- return parseInteger(token, sign, false, 64);
579
- case "uint64":
580
- case "fixed64":
581
- return parseInteger(token, sign, true, 64);
582
- default:
583
- this.error(field.fullName + ": unsupported scalar type");
584
- }
585
- return undefined;
586
- };
587
-
588
- Parser.prototype.readStringBytes = function readStringBytes() {
589
- var token = this.tn.next(),
590
- bytes = [];
591
- if (!token || token.type !== "string")
592
- this.error("expected string");
593
- Array.prototype.push.apply(bytes, token.value);
594
- while ((token = this.tn.peek()) && token.type === "string") {
595
- token = this.tn.next();
596
- Array.prototype.push.apply(bytes, token.value);
597
- }
598
- return bytes;
599
- };
600
-
601
- Parser.prototype.skipReservedValue = function skipReservedValue(depth) {
602
- this.tn.skip(":");
603
- var token = this.tn.peek();
604
- if (!token)
605
- return;
606
- if (token.value === "{") {
607
- this.skipBalanced("{", "}", depth);
608
- } else if (token.value === "<") {
609
- this.skipBalanced("<", ">", depth);
610
- } else if (token.value === "[") {
611
- this.skipBalanced("[", "]", depth);
612
- } else if (token.type === "string") {
613
- this.readStringBytes();
614
- } else {
615
- if (token.value === "-")
616
- this.tn.next();
617
- this.tn.next();
618
- }
619
- };
620
-
621
- Parser.prototype.skipBalanced = function skipBalanced(open, close, depth) {
622
- var balance = 0,
623
- token;
624
- do {
625
- token = this.tn.next();
626
- if (!token)
627
- this.error("expected '" + close + "'");
628
- if (token.value === open) {
629
- if (depth + ++balance > util.recursionLimit)
630
- this.error("max depth exceeded");
631
- }
632
- else if (token.value === close)
633
- --balance;
634
- } while (balance);
635
- };
636
-
637
- function addField(object, seen, field, value) {
638
- if (field.repeated) {
639
- getField(object, field, []).push(value);
640
- return;
641
- }
642
- if (Object.prototype.hasOwnProperty.call(seen, field.name))
643
- throw Error(field.fullName + ": multiple values");
644
- if (field.partOf) {
645
- var oneofName = "$oneof:" + field.partOf.name;
646
- if (seen[oneofName])
647
- throw Error(field.partOf.name + ": multiple values");
648
- seen[oneofName] = true;
649
- }
650
- seen[field.name] = true;
651
- setField(object, field, value);
652
- }
653
-
654
- function getField(object, field, value) {
655
- if (!Object.prototype.hasOwnProperty.call(object, field.name))
656
- setField(object, field, value);
657
- return object[field.name];
658
- }
659
-
660
- function setField(object, field, value) {
661
- if (field.name === "__proto__")
662
- util.makeProp(object, field.name);
663
- object[field.name] = value;
664
- }
665
-
666
- function addMapEntry(field, map, entry) {
667
- var keyField = mapKeyField(field),
668
- valueField = mapValueField(field),
669
- key = Object.prototype.hasOwnProperty.call(entry, "key")
670
- ? entry.key
671
- : defaultScalarValue(keyField),
672
- value = Object.prototype.hasOwnProperty.call(entry, "value")
673
- ? entry.value
674
- : defaultMapValue(valueField);
675
- var outKey = mapKey(keyField, key);
676
- if (outKey === "__proto__")
677
- util.makeProp(map, outKey);
678
- map[outKey] = value;
679
- }
680
-
681
- function defaultMapValue(field) {
682
- if (field.resolvedType instanceof Type)
683
- return field.resolvedType.create();
684
- if (field.resolvedType instanceof Enum)
685
- return field.typeDefault;
686
- return defaultScalarValue(field);
687
- }
688
-
689
- function defaultScalarValue(field) {
690
- if (field.type === "string")
691
- return "";
692
- if (field.type === "bytes")
693
- return util.newBuffer([]);
694
- if (field.type === "bool")
695
- return false;
696
- return field.defaultValue;
697
- }
698
-
699
- function mapKeyField(field) {
700
- return {
701
- name: "key",
702
- fullName: field.fullName + ".key",
703
- type: field.keyType,
704
- resolvedType: null,
705
- defaultValue: field.keyType === "bool" ? false : field.keyType === "string" ? "" : 0
706
- };
707
- }
708
-
709
- function mapValueField(field) {
710
- return {
711
- name: "value",
712
- fullName: field.fullName + ".value",
713
- type: field.type,
714
- resolvedType: field.resolvedType,
715
- defaultValue: field.resolvedType instanceof Enum ? field.resolvedType.values[Object.keys(field.resolvedType.values)[0]] : field.typeDefault
716
- };
717
- }
718
-
719
- function mapKey(field, value) {
720
- if (types.long[field.type] !== undefined)
721
- return String(value);
722
- if (field.type === "bool")
723
- return value ? "true" : "false";
724
- return String(value);
725
- }
726
-
727
- function lookupField(type, name) {
728
- var fields = type.fieldsArray;
729
- for (var i = 0; i < fields.length; ++i) {
730
- var field = fields[i].resolve();
731
- if (field.name === name || underScore(field.name) === name)
732
- return field;
733
- if (field.delimited && field.resolvedType instanceof Type) {
734
- var groupName = field.resolvedType.name,
735
- lowerName = name.toLowerCase(),
736
- compactName = compact(name);
737
- if (groupName === name || underScore(groupName) === name
738
- || field.name.toLowerCase() === lowerName
739
- || groupName.toLowerCase() === lowerName
740
- || compact(field.name) === compactName
741
- || compact(groupName) === compactName)
742
- return field;
743
- }
744
- }
745
- return null;
746
- }
747
-
748
- function lookupExtension(type, name) {
749
- var fullName = name.charAt(0) === "." ? name : "." + name,
750
- camelName = camelCaseLastPath(fullName),
751
- field = type.get(fullName) || type.root.lookup(fullName, Field)
752
- || type.get(camelName) || type.root.lookup(camelName, Field);
753
- if (!field)
754
- field = lookupExtensionField(type, fullName);
755
- if (field && field.extend !== undefined && field.extensionField)
756
- field = field.extensionField;
757
- if (!field || field.parent !== type)
758
- throw Error(type.fullName + ": unknown extension '" + name + "'");
759
- return field;
760
- }
761
-
762
- function camelCaseLastPath(name) {
763
- var dot = name.lastIndexOf(".");
764
- return name.substring(0, dot + 1) + util.camelCase(name.substring(dot + 1));
765
- }
766
-
767
- function parseEnum(field, token, sign) {
768
- if (sign < 0) {
769
- if (token.type !== "number")
770
- throw Error(field.fullName + ": enum value expected");
771
- return checkEnumValue(field, parseInteger(token, sign, false, 32));
772
- }
773
- if (token.type === "word") {
774
- var value = field.resolvedType.values[token.value];
775
- if (value === undefined)
776
- throw Error(field.fullName + ": enum value expected");
777
- return value;
778
- }
779
- return checkEnumValue(field, parseInteger(token, sign, false, 32));
780
- }
781
-
782
- function parseBool(token, sign) {
783
- if (sign < 0)
784
- throw Error("bool value expected");
785
- if (token.type === "word") {
786
- switch (token.value) {
787
- case "true":
788
- case "True":
789
- case "t":
790
- return true;
791
- case "false":
792
- case "False":
793
- case "f":
794
- return false;
795
- }
796
- } else if (token.type === "number") {
797
- var value = parseInteger(token, 1, true, 32);
798
- if (value === 0)
799
- return false;
800
- if (value === 1)
801
- return true;
802
- }
803
- throw Error("bool value expected");
804
- }
805
-
806
- function parseFloatValue(token, sign) {
807
- if (token.type === "word") {
808
- switch (token.value.toLowerCase()) {
809
- case "inf":
810
- case "infinity":
811
- return sign * Infinity;
812
- case "nan":
813
- return NaN;
814
- }
815
- throw Error("float value expected");
816
- }
817
- if (token.type !== "number")
818
- throw Error("float value expected");
819
- var raw = token.value;
820
- if (/^0[xX]/.test(raw) || /^0[0-7]/.test(raw) && raw.length > 1)
821
- throw Error("float value expected");
822
- if (/[fF]$/.test(raw))
823
- raw = raw.substring(0, raw.length - 1);
824
- return sign * Number(raw);
825
- }
826
-
827
- function parseInteger(token, sign, unsigned, bits) {
828
- if (token.type !== "number")
829
- throw Error("integer value expected");
830
- if (sign < 0 && unsigned)
831
- throw Error("unsigned integer value expected");
832
-
833
- var raw = token.value,
834
- radix = 10,
835
- digits = raw;
836
- if (/^0[xX]/.test(raw)) {
837
- radix = 16;
838
- digits = raw.substring(2);
839
- } else if (/^0[0-7]/.test(raw) && raw.length > 1) {
840
- radix = 8;
841
- digits = raw.substring(1);
842
- } else if (!/^(?:0|[1-9][0-9]*)$/.test(raw))
843
- throw Error("integer value expected");
844
-
845
- if (typeof util.global.BigInt === "function")
846
- return parseIntegerBigInt(digits, radix, sign, unsigned, bits);
847
-
848
- var value = parseInt(digits, radix) * sign;
849
- if (bits === 32) {
850
- var min = unsigned ? 0 : -2147483648,
851
- max = unsigned ? 4294967295 : 2147483647;
852
- if (value < min || value > max)
853
- throw Error((unsigned ? "unsigned " : "") + "integer value out of range");
854
- }
855
- if (bits === 64 && util.Long)
856
- return util.Long.fromString(String(value), unsigned);
857
- return value;
858
- }
859
-
860
- function parseIntegerBigInt(digits, radix, sign, unsigned, bits) {
861
- var value,
862
- BigInt = util.global.BigInt;
863
- if (radix === 16)
864
- value = BigInt("0x" + digits);
865
- else if (radix === 8)
866
- value = BigInt("0o" + digits);
867
- else
868
- value = BigInt(digits);
869
- if (sign < 0)
870
- value = -value;
871
-
872
- var min,
873
- max;
874
- if (bits === 32) {
875
- min = unsigned ? BigInt(0) : -BigInt(2147483648);
876
- max = unsigned ? BigInt(4294967295) : BigInt(2147483647);
877
- } else {
878
- min = unsigned ? BigInt(0) : -BigInt("9223372036854775808");
879
- max = unsigned ? BigInt("18446744073709551615") : BigInt("9223372036854775807");
880
- }
881
- if (value < min || value > max)
882
- throw Error((unsigned ? "unsigned " : "") + "integer value out of range");
883
- if (bits === 64 && util.Long)
884
- return util.Long.fromString(value.toString(), unsigned, 10);
885
- return Number(value);
886
- }
887
-
888
- function writeMessage(type, message, lines, indent, options, depth) {
889
- if (depth > util.recursionLimit)
890
- throw Error("max depth exceeded");
891
- var fields = type.fieldsArray.slice().sort(util.compareFieldsById);
892
- for (var i = 0; i < fields.length; ++i) {
893
- var field = fields[i].resolve(),
894
- value = message[field.name];
895
- if (value == null || !Object.prototype.hasOwnProperty.call(message, field.name))
896
- continue;
897
- if (field.map)
898
- writeMapField(field, value, lines, indent, options, depth);
899
- else if (field.repeated)
900
- for (var j = 0; j < value.length; ++j)
901
- writeField(field, value[j], lines, indent, options, depth);
902
- else
903
- writeField(field, value, lines, indent, options, depth);
904
- }
905
- if (options.unknowns && message.$unknowns != null && Object.prototype.hasOwnProperty.call(message, "$unknowns"))
906
- writeUnknowns(message.$unknowns, lines, indent);
907
- }
908
-
909
- function writeMapField(field, map, lines, indent, options, depth) {
910
- var keys = Object.keys(map).sort(),
911
- keyField = mapKeyField(field),
912
- valueField = mapValueField(field),
913
- name = formatFieldName(field),
914
- sp = spaces(indent);
915
- for (var i = 0; i < keys.length; ++i) {
916
- if (depth + 1 > util.recursionLimit)
917
- throw Error("max depth exceeded");
918
- var key = keyField.long ? util.longFromKey(keys[i], keyField.type === "uint64" || keyField.type === "fixed64") : keys[i];
919
- if (keyField.type === "bool")
920
- key = util.boolFromKey(keys[i]);
921
- lines.push(sp + name + " {");
922
- lines.push(spaces(indent + 2) + "key: " + formatScalar(keyField, key));
923
- if (valueField.resolvedType instanceof Type) {
924
- lines.push(spaces(indent + 2) + "value {");
925
- writeMessage(valueField.resolvedType, map[keys[i]], lines, indent + 4, options, depth + 2);
926
- lines.push(spaces(indent + 2) + "}");
927
- } else
928
- lines.push(spaces(indent + 2) + "value: " + formatScalar(valueField, map[keys[i]]));
929
- lines.push(sp + "}");
930
- }
931
- }
932
-
933
- function writeField(field, value, lines, indent, options, depth) {
934
- var name = formatFieldName(field),
935
- sp = spaces(indent);
936
- if (field.resolvedType instanceof Type) {
937
- lines.push(sp + name + " {");
938
- writeMessage(field.resolvedType, value, lines, indent + 2, options, depth + 1);
939
- lines.push(sp + "}");
940
- } else
941
- lines.push(sp + name + ": " + formatScalar(field, value));
942
- }
943
-
944
- function writeUnknowns(unknowns, lines, indent) {
945
- for (var i = 0; i < unknowns.length; ++i) {
946
- var reader = Reader.create(unknowns[i]);
947
- writeUnknownFieldSet(reader, lines, indent, undefined, textformat.unknownRecursionLimit);
948
- if (reader.pos !== reader.len)
949
- throw Error("invalid unknown field data");
950
- }
951
- }
952
-
953
- function writeUnknownFieldSet(reader, lines, indent, endGroup, recursionBudget) {
954
- if (recursionBudget < 0)
955
- throw Error("max depth exceeded");
956
- while (reader.pos < reader.len) {
957
- var tag = reader.tag(),
958
- fieldNumber = tag >>> 3,
959
- wireType = tag & 7;
960
- if (!fieldNumber)
961
- throw Error("illegal tag: field number 0");
962
- if (wireType === 4) {
963
- if (fieldNumber !== endGroup)
964
- throw Error("invalid end group tag");
965
- return;
966
- }
967
- writeUnknownField(reader, fieldNumber, wireType, lines, indent, recursionBudget);
968
- }
969
- if (endGroup !== undefined)
970
- throw Error("missing end group tag");
971
- }
972
-
973
- function writeUnknownField(reader, fieldNumber, wireType, lines, indent, recursionBudget) {
974
- var sp = spaces(indent),
975
- lo;
976
- switch (wireType) {
977
- case 0:
978
- lines.push(sp + fieldNumber + ": " + readUnknownVarint(reader));
979
- break;
980
- case 1:
981
- lo = reader.fixed32();
982
- lines.push(sp + fieldNumber + ": 0x" + hexPad(reader.fixed32(), 8) + hexPad(lo, 8));
983
- break;
984
- case 2:
985
- writeUnknownLengthDelimited(reader, fieldNumber, lines, indent, recursionBudget);
986
- break;
987
- case 3:
988
- lines.push(sp + fieldNumber + " {");
989
- writeUnknownFieldSet(reader, lines, indent + 2, fieldNumber, recursionBudget - 1);
990
- lines.push(sp + "}");
991
- break;
992
- case 5:
993
- lines.push(sp + fieldNumber + ": 0x" + hexPad(reader.fixed32(), 8));
994
- break;
995
- default:
996
- throw Error("invalid wire type " + wireType);
997
- }
998
- }
999
-
1000
- function writeUnknownLengthDelimited(reader, fieldNumber, lines, indent, recursionBudget) {
1001
- var value = reader.bytes(),
1002
- nested = value.length && recursionBudget > 0
1003
- ? tryFormatUnknownMessage(value, indent + 2, recursionBudget - 1)
1004
- : null,
1005
- sp = spaces(indent);
1006
- if (nested) {
1007
- lines.push(sp + fieldNumber + " {");
1008
- for (var i = 0; i < nested.length; ++i)
1009
- lines.push(nested[i]);
1010
- lines.push(sp + "}");
1011
- } else
1012
- lines.push(sp + fieldNumber + ": " + quoteBytes(value));
1013
- }
1014
-
1015
- function tryFormatUnknownMessage(bytes, indent, recursionBudget) {
1016
- var reader = Reader.create(bytes),
1017
- lines = [];
1018
- try {
1019
- writeUnknownFieldSet(reader, lines, indent, undefined, recursionBudget);
1020
- return reader.pos === reader.len ? lines : null;
1021
- } catch (e) {
1022
- return null;
1023
- }
1024
- }
1025
-
1026
- function readUnknownVarint(reader) {
1027
- var BigInt = util.global.BigInt;
1028
- if (typeof BigInt !== "function")
1029
- return String(reader.uint64());
1030
-
1031
- var value = BigInt(0),
1032
- shift = BigInt(0),
1033
- b;
1034
- for (var i = 0; i < 10; ++i) {
1035
- if (reader.pos >= reader.len)
1036
- throw Error("invalid varint encoding");
1037
- b = reader.buf[reader.pos++];
1038
- value += BigInt(b & 127) << shift;
1039
- if (b < 128)
1040
- return value.toString();
1041
- shift += BigInt(7);
1042
- }
1043
- throw Error("invalid varint encoding");
1044
- }
1045
-
1046
- function hexPad(value, length) {
1047
- var str = value.toString(16);
1048
- while (str.length < length)
1049
- str = "0" + str;
1050
- return str;
1051
- }
1052
-
1053
- function formatFieldName(field) {
1054
- if (field.declaringField || field.name.charAt(0) === ".")
1055
- return "[" + formatExtensionName(field) + "]";
1056
- if (field.delimited && field.resolvedType instanceof Type && compact(field.name) === compact(field.resolvedType.name))
1057
- return field.resolvedType.name;
1058
- return underScore(field.name);
1059
- }
1060
-
1061
- function formatExtensionName(field) {
1062
- var name = field.name.replace(/^\./, ""),
1063
- dot = name.lastIndexOf("."),
1064
- path = name.substring(0, dot + 1),
1065
- last = name.substring(dot + 1);
1066
- if (field.delimited && field.resolvedType instanceof Type && field.resolvedType.group)
1067
- last = last.toLowerCase();
1068
- else
1069
- last = underScore(last);
1070
- return path + last;
1071
- }
1072
-
1073
- function formatScalar(field, value) {
1074
- if (field.resolvedType instanceof Enum) {
1075
- var name = field.resolvedType.valuesById[value];
1076
- return name === undefined ? String(value) : name;
1077
- }
1078
- switch (field.type) {
1079
- case "string":
1080
- return quoteBytes(utf8Bytes(String(value)));
1081
- case "bytes":
1082
- return quoteBytes(bytesValue(value));
1083
- case "bool":
1084
- return value ? "true" : "false";
1085
- case "double":
1086
- case "float":
1087
- if (isNaN(value))
1088
- return "nan";
1089
- if (value === 0 && 1 / value === -Infinity)
1090
- return "-0";
1091
- if (value === Infinity)
1092
- return "inf";
1093
- if (value === -Infinity)
1094
- return "-inf";
1095
- return String(value);
1096
- default:
1097
- return String(value);
1098
- }
1099
- }
1100
-
1101
- function bytesValue(value) {
1102
- if (typeof value === "string") {
1103
- var length = util.base64.length(value),
1104
- buffer = util.newBuffer(length);
1105
- util.base64.decode(value, buffer, 0);
1106
- return buffer;
1107
- }
1108
- return value;
1109
- }
1110
-
1111
- function quoteBytes(bytes) {
1112
- var out = "\"",
1113
- oct;
1114
- for (var i = 0; i < bytes.length; ++i) {
1115
- var b = bytes[i];
1116
- switch (b) {
1117
- case 9: out += "\\t"; break;
1118
- case 10: out += "\\n"; break;
1119
- case 13: out += "\\r"; break;
1120
- case 34: out += "\\\""; break;
1121
- case 92: out += "\\\\"; break;
1122
- default:
1123
- if (b >= 32 && b <= 126)
1124
- out += String.fromCharCode(b);
1125
- else {
1126
- oct = b.toString(8);
1127
- out += "\\" + (oct.length < 2 ? "00" : oct.length < 3 ? "0" : "") + oct;
1128
- }
1129
- break;
1130
- }
1131
- }
1132
- return out + "\"";
1133
- }
1134
-
1135
- function spaces(indent) {
1136
- var str = "";
1137
- while (str.length < indent)
1138
- str += " ";
1139
- return str;
1140
- }
1141
-
1142
- function utf8Read(bytes, strict) {
1143
- if (strict && !validUtf8(bytes))
1144
- throw Error("invalid UTF-8 string");
1145
- var buffer = util.newBuffer(bytes);
1146
- return util.utf8.read(buffer, 0, buffer.length);
1147
- }
1148
-
1149
- function validUtf8(bytes) {
1150
- for (var i = 0, b; i < bytes.length;) {
1151
- b = bytes[i++];
1152
- if (b <= 0x7F)
1153
- continue;
1154
- if (b >= 0xC2 && b <= 0xDF) {
1155
- if ((b = bytes[i++]) < 0x80 || b > 0xBF)
1156
- return false;
1157
- } else if (b === 0xE0) {
1158
- if ((b = bytes[i++]) < 0xA0 || b > 0xBF || (b = bytes[i++]) < 0x80 || b > 0xBF)
1159
- return false;
1160
- } else if (b >= 0xE1 && b <= 0xEC || b >= 0xEE && b <= 0xEF) {
1161
- if ((b = bytes[i++]) < 0x80 || b > 0xBF || (b = bytes[i++]) < 0x80 || b > 0xBF)
1162
- return false;
1163
- } else if (b === 0xED) {
1164
- if ((b = bytes[i++]) < 0x80 || b > 0x9F || (b = bytes[i++]) < 0x80 || b > 0xBF)
1165
- return false;
1166
- } else if (b === 0xF0) {
1167
- if ((b = bytes[i++]) < 0x90 || b > 0xBF || (b = bytes[i++]) < 0x80 || b > 0xBF || (b = bytes[i++]) < 0x80 || b > 0xBF)
1168
- return false;
1169
- } else if (b >= 0xF1 && b <= 0xF3) {
1170
- if ((b = bytes[i++]) < 0x80 || b > 0xBF || (b = bytes[i++]) < 0x80 || b > 0xBF || (b = bytes[i++]) < 0x80 || b > 0xBF)
1171
- return false;
1172
- } else if (b === 0xF4) {
1173
- if ((b = bytes[i++]) < 0x80 || b > 0x8F || (b = bytes[i++]) < 0x80 || b > 0xBF || (b = bytes[i++]) < 0x80 || b > 0xBF)
1174
- return false;
1175
- } else
1176
- return false;
1177
- }
1178
- return true;
1179
- }
1180
-
1181
- function utf8Bytes(str) {
1182
- var buffer = util.newBuffer(util.utf8.length(str));
1183
- util.utf8.write(str, buffer, 0);
1184
- return buffer;
1185
- }
1186
-
1187
- function pushUtf8(bytes, code) {
1188
- if (code < 0x80) {
1189
- bytes.push(code);
1190
- } else if (code < 0x800) {
1191
- bytes.push(code >> 6 | 192, code & 63 | 128);
1192
- } else if (code < 0x10000) {
1193
- bytes.push(code >> 12 | 224, code >> 6 & 63 | 128, code & 63 | 128);
1194
- } else {
1195
- bytes.push(code >> 18 | 240, code >> 12 & 63 | 128, code >> 6 & 63 | 128, code & 63 | 128);
1196
- }
1197
- }
1198
-
1199
- function verifyTextMessage(type, message) {
1200
- var fields = type.fieldsArray;
1201
- for (var i = 0; i < fields.length; ++i) {
1202
- var field = fields[i].resolve(),
1203
- value = message[field.name];
1204
- if (value == null || !Object.prototype.hasOwnProperty.call(message, field.name)) {
1205
- if (field.required)
1206
- return field.fullName + ": missing required field";
1207
- continue;
1208
- }
1209
- if (field.map) {
1210
- if (field.resolvedType instanceof Type)
1211
- for (var key in value)
1212
- if (Object.prototype.hasOwnProperty.call(value, key)) {
1213
- var mapErr = verifyTextMessage(field.resolvedType, value[key]);
1214
- if (mapErr)
1215
- return mapErr;
1216
- }
1217
- } else if (field.repeated) {
1218
- if (field.resolvedType instanceof Type)
1219
- for (var j = 0; j < value.length; ++j) {
1220
- var repeatedErr = verifyTextMessage(field.resolvedType, value[j]);
1221
- if (repeatedErr)
1222
- return repeatedErr;
1223
- }
1224
- } else if (field.resolvedType instanceof Type) {
1225
- var err = verifyTextMessage(field.resolvedType, value);
1226
- if (err)
1227
- return err;
1228
- }
1229
- }
1230
- return null;
1231
- }
1232
-
1233
- function isSeparator(token) {
1234
- return token.value === "," || token.value === ";";
1235
- }
1236
-
1237
- function validateTypeUrl(typeUrl) {
1238
- for (var i = 0; i < typeUrl.length; ++i)
1239
- if (typeUrl.charAt(i) === "%") {
1240
- if (i + 2 >= typeUrl.length || !hexRe.test(typeUrl.charAt(i + 1)) || !hexRe.test(typeUrl.charAt(i + 2)))
1241
- throw Error("invalid Any type URL");
1242
- i += 2;
1243
- }
1244
- }
1245
-
1246
- function lookupExtensionField(type, fullName) {
1247
- var fields = type.fieldsArray;
1248
- for (var i = 0; i < fields.length; ++i) {
1249
- var field = fields[i].resolve();
1250
- if (field.name.charAt(0) === "." && "." + formatExtensionName(field) === fullName)
1251
- return field;
1252
- }
1253
- return null;
1254
- }
1255
-
1256
- function checkEnumValue(field, value) {
1257
- if (isClosedEnum(field) && field.resolvedType.valuesById[value] === undefined)
1258
- throw Error(field.fullName + ": enum value expected");
1259
- return value;
1260
- }
1261
-
1262
- function isClosedEnum(field) {
1263
- return field.resolvedType
1264
- && field.resolvedType._features
1265
- && field.resolvedType._features.enum_type === "CLOSED";
1266
- }
1267
-
1268
- function compact(str) {
1269
- return str.replace(/_/g, "").toLowerCase();
1270
- }
1271
-
1272
- function underScore(str) {
1273
- return str.substring(0, 1)
1274
- + str.substring(1)
1275
- .replace(/([A-Z])(?=[a-z]|$)/g, function($0, $1) { return "_" + $1.toLowerCase(); });
1276
- }