stage-browser 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/README.md +92 -45
  2. package/build/icon.icns +0 -0
  3. package/build/icon.png +0 -0
  4. package/dist/bin/stage.js +111 -0
  5. package/dist/main/cli-args.js +28 -0
  6. package/dist/main/context-menu.js +68 -0
  7. package/dist/main/context.js +2 -0
  8. package/dist/main/extensions.js +78 -0
  9. package/dist/main/ipc.js +73 -0
  10. package/dist/main/main.js +282 -0
  11. package/dist/main/menu.js +140 -0
  12. package/dist/main/reveal.js +170 -0
  13. package/dist/main/selftest.js +570 -0
  14. package/dist/main/store.js +85 -0
  15. package/dist/main/suggest.js +43 -0
  16. package/dist/main/tabs.js +324 -0
  17. package/dist/main/ui-window.js +133 -0
  18. package/dist/main/url.js +38 -0
  19. package/dist/main/window.js +133 -0
  20. package/dist/preload/extension-compat.js +163 -0
  21. package/dist/preload/ui-preload.js +34 -0
  22. package/dist/shared/contract.js +6 -0
  23. package/dist/ui/app.js +692 -0
  24. package/{src → dist}/ui/index.html +15 -5
  25. package/dist/ui/mock.js +500 -0
  26. package/{src → dist}/ui/styles.css +55 -14
  27. package/node_modules/@types/chrome/LICENSE +21 -0
  28. package/node_modules/@types/chrome/README.md +15 -0
  29. package/node_modules/@types/chrome/chrome-cast/index.d.ts +1156 -0
  30. package/node_modules/@types/chrome/har-format/index.d.ts +6 -0
  31. package/node_modules/@types/chrome/index.d.ts +13786 -0
  32. package/node_modules/@types/chrome/package.json +99 -0
  33. package/node_modules/@types/filesystem/LICENSE +21 -0
  34. package/node_modules/@types/filesystem/README.md +15 -0
  35. package/node_modules/@types/filesystem/index.d.ts +564 -0
  36. package/node_modules/@types/filesystem/package.json +26 -0
  37. package/node_modules/@types/filewriter/LICENSE +21 -0
  38. package/node_modules/@types/filewriter/README.md +15 -0
  39. package/node_modules/@types/filewriter/index.d.ts +166 -0
  40. package/node_modules/@types/filewriter/package.json +25 -0
  41. package/node_modules/@types/har-format/LICENSE +21 -0
  42. package/node_modules/@types/har-format/README.md +15 -0
  43. package/node_modules/@types/har-format/index.d.ts +938 -0
  44. package/node_modules/@types/har-format/package.json +31 -0
  45. package/node_modules/adm-zip/LICENSE +21 -0
  46. package/node_modules/adm-zip/README.md +90 -0
  47. package/node_modules/adm-zip/adm-zip.js +1020 -0
  48. package/node_modules/adm-zip/headers/entryHeader.js +396 -0
  49. package/node_modules/adm-zip/headers/index.js +2 -0
  50. package/node_modules/adm-zip/headers/mainHeader.js +177 -0
  51. package/node_modules/adm-zip/methods/deflater.js +33 -0
  52. package/node_modules/adm-zip/methods/index.js +3 -0
  53. package/node_modules/adm-zip/methods/inflater.js +64 -0
  54. package/node_modules/adm-zip/methods/zipcrypto.js +175 -0
  55. package/node_modules/adm-zip/package.json +56 -0
  56. package/node_modules/adm-zip/types.d.ts +223 -0
  57. package/node_modules/adm-zip/util/constants.js +142 -0
  58. package/node_modules/adm-zip/util/decoder.js +5 -0
  59. package/node_modules/adm-zip/util/errors.js +66 -0
  60. package/node_modules/adm-zip/util/fattr.js +76 -0
  61. package/node_modules/adm-zip/util/index.js +5 -0
  62. package/node_modules/adm-zip/util/utils.js +481 -0
  63. package/node_modules/adm-zip/zipEntry.js +415 -0
  64. package/node_modules/adm-zip/zipFile.js +466 -0
  65. package/node_modules/debug/LICENSE +20 -0
  66. package/node_modules/debug/README.md +481 -0
  67. package/node_modules/debug/package.json +64 -0
  68. package/node_modules/debug/src/browser.js +272 -0
  69. package/node_modules/debug/src/common.js +292 -0
  70. package/node_modules/debug/src/index.js +10 -0
  71. package/node_modules/debug/src/node.js +263 -0
  72. package/node_modules/electron-chrome-web-store/README.md +143 -0
  73. package/node_modules/electron-chrome-web-store/dist/chrome-web-store.preload.js +299 -0
  74. package/node_modules/electron-chrome-web-store/dist/cjs/browser/index.js +1674 -0
  75. package/node_modules/electron-chrome-web-store/dist/esm/browser/index.mjs +950 -0
  76. package/node_modules/electron-chrome-web-store/dist/types/browser/api.d.ts +2 -0
  77. package/node_modules/electron-chrome-web-store/dist/types/browser/crx3.d.ts +20 -0
  78. package/node_modules/electron-chrome-web-store/dist/types/browser/id.d.ts +9 -0
  79. package/node_modules/electron-chrome-web-store/dist/types/browser/index.d.ts +62 -0
  80. package/node_modules/electron-chrome-web-store/dist/types/browser/installer.d.ts +29 -0
  81. package/node_modules/electron-chrome-web-store/dist/types/browser/loader.d.ts +18 -0
  82. package/node_modules/electron-chrome-web-store/dist/types/browser/types.d.ts +21 -0
  83. package/node_modules/electron-chrome-web-store/dist/types/browser/updater.d.ts +6 -0
  84. package/node_modules/electron-chrome-web-store/dist/types/browser/utils.d.ts +4 -0
  85. package/node_modules/electron-chrome-web-store/dist/types/common/constants.d.ts +44 -0
  86. package/node_modules/electron-chrome-web-store/dist/types/renderer/chrome-web-store.preload.d.ts +1 -0
  87. package/node_modules/electron-chrome-web-store/package.json +43 -0
  88. package/node_modules/ms/index.js +162 -0
  89. package/node_modules/ms/license.md +21 -0
  90. package/node_modules/ms/package.json +38 -0
  91. package/node_modules/ms/readme.md +59 -0
  92. package/node_modules/pbf/LICENSE +27 -0
  93. package/node_modules/pbf/README.md +293 -0
  94. package/node_modules/pbf/bin/pbf +17 -0
  95. package/node_modules/pbf/compile.js +441 -0
  96. package/node_modules/pbf/dist/pbf-dev.js +838 -0
  97. package/node_modules/pbf/dist/pbf.js +1 -0
  98. package/node_modules/pbf/index.d.ts +206 -0
  99. package/node_modules/pbf/index.js +830 -0
  100. package/node_modules/pbf/package.json +63 -0
  101. package/node_modules/protocol-buffers-schema/.travis.yml +7 -0
  102. package/node_modules/protocol-buffers-schema/LICENSE +21 -0
  103. package/node_modules/protocol-buffers-schema/README.md +122 -0
  104. package/node_modules/protocol-buffers-schema/example.js +11 -0
  105. package/node_modules/protocol-buffers-schema/example.proto +13 -0
  106. package/node_modules/protocol-buffers-schema/index.js +6 -0
  107. package/node_modules/protocol-buffers-schema/package.json +31 -0
  108. package/node_modules/protocol-buffers-schema/parse.js +769 -0
  109. package/node_modules/protocol-buffers-schema/stringify.js +206 -0
  110. package/node_modules/protocol-buffers-schema/test/fixtures/basic.json +90 -0
  111. package/node_modules/protocol-buffers-schema/test/fixtures/basic.proto +11 -0
  112. package/node_modules/protocol-buffers-schema/test/fixtures/comments.json +99 -0
  113. package/node_modules/protocol-buffers-schema/test/fixtures/comments.proto +19 -0
  114. package/node_modules/protocol-buffers-schema/test/fixtures/complex.json +125 -0
  115. package/node_modules/protocol-buffers-schema/test/fixtures/complex.proto +30 -0
  116. package/node_modules/protocol-buffers-schema/test/fixtures/enum.json +42 -0
  117. package/node_modules/protocol-buffers-schema/test/fixtures/enum.proto +8 -0
  118. package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.json +32 -0
  119. package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.proto +3 -0
  120. package/node_modules/protocol-buffers-schema/test/fixtures/extend.json +167 -0
  121. package/node_modules/protocol-buffers-schema/test/fixtures/extend.proto +19 -0
  122. package/node_modules/protocol-buffers-schema/test/fixtures/import.json +30 -0
  123. package/node_modules/protocol-buffers-schema/test/fixtures/import.proto +6 -0
  124. package/node_modules/protocol-buffers-schema/test/fixtures/map.json +33 -0
  125. package/node_modules/protocol-buffers-schema/test/fixtures/map.proto +3 -0
  126. package/node_modules/protocol-buffers-schema/test/fixtures/no-tags.proto +8 -0
  127. package/node_modules/protocol-buffers-schema/test/fixtures/oneof.json +41 -0
  128. package/node_modules/protocol-buffers-schema/test/fixtures/oneof.proto +6 -0
  129. package/node_modules/protocol-buffers-schema/test/fixtures/option.json +364 -0
  130. package/node_modules/protocol-buffers-schema/test/fixtures/option.proto +72 -0
  131. package/node_modules/protocol-buffers-schema/test/fixtures/options.json +47 -0
  132. package/node_modules/protocol-buffers-schema/test/fixtures/options.proto +8 -0
  133. package/node_modules/protocol-buffers-schema/test/fixtures/pheromon-trajectories.proto +8 -0
  134. package/node_modules/protocol-buffers-schema/test/fixtures/reserved.json +55 -0
  135. package/node_modules/protocol-buffers-schema/test/fixtures/reserved.proto +17 -0
  136. package/node_modules/protocol-buffers-schema/test/fixtures/search.json +30 -0
  137. package/node_modules/protocol-buffers-schema/test/fixtures/search.proto +5 -0
  138. package/node_modules/protocol-buffers-schema/test/fixtures/service.json +108 -0
  139. package/node_modules/protocol-buffers-schema/test/fixtures/service.proto +30 -0
  140. package/node_modules/protocol-buffers-schema/test/fixtures/valid-packed.proto +35 -0
  141. package/node_modules/protocol-buffers-schema/test/fixtures/version.json +90 -0
  142. package/node_modules/protocol-buffers-schema/test/fixtures/version.proto +13 -0
  143. package/node_modules/protocol-buffers-schema/test/index.js +157 -0
  144. package/node_modules/protocol-buffers-schema/tokenize.js +56 -0
  145. package/node_modules/resolve-protobuf-schema/.travis.yml +3 -0
  146. package/node_modules/resolve-protobuf-schema/LICENSE +21 -0
  147. package/node_modules/resolve-protobuf-schema/README.md +49 -0
  148. package/node_modules/resolve-protobuf-schema/index.js +50 -0
  149. package/node_modules/resolve-protobuf-schema/package.json +25 -0
  150. package/node_modules/resolve-protobuf-schema/test/a.proto +5 -0
  151. package/node_modules/resolve-protobuf-schema/test/b.proto +5 -0
  152. package/node_modules/resolve-protobuf-schema/test/c.proto +3 -0
  153. package/node_modules/resolve-protobuf-schema/test/index.js +49 -0
  154. package/package.json +35 -11
  155. package/bin/stage.js +0 -52
  156. package/src/main/cli-args.js +0 -28
  157. package/src/main/context-menu.js +0 -64
  158. package/src/main/extensions.js +0 -61
  159. package/src/main/main.js +0 -310
  160. package/src/main/menu.js +0 -171
  161. package/src/main/reveal.js +0 -141
  162. package/src/main/selftest.js +0 -383
  163. package/src/main/store.js +0 -77
  164. package/src/main/suggest.js +0 -50
  165. package/src/main/tabs.js +0 -351
  166. package/src/main/url.js +0 -37
  167. package/src/main/window.js +0 -71
  168. package/src/preload/extension-compat.js +0 -34
  169. package/src/preload/ui-preload.js +0 -40
  170. package/src/ui/app.js +0 -588
  171. package/src/ui/mock.js +0 -455
  172. /package/{src → dist}/ui/newtab.html +0 -0
@@ -0,0 +1,838 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Pbf = factory());
5
+ })(this, (function () { 'use strict';
6
+
7
+ const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
8
+ const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
9
+
10
+ // Threshold chosen based on both benchmarking and knowledge about browser string
11
+ // data structures (which currently switch structure types at 12 bytes or more)
12
+ const TEXT_DECODER_MIN_LENGTH = 12;
13
+ const utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8');
14
+
15
+ const PBF_VARINT = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum
16
+ const PBF_FIXED64 = 1; // 64-bit: double, fixed64, sfixed64
17
+ const PBF_BYTES = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields
18
+ const PBF_FIXED32 = 5; // 32-bit: float, fixed32, sfixed32
19
+
20
+ class Pbf {
21
+ /**
22
+ * @param {Uint8Array | ArrayBuffer} [buf]
23
+ */
24
+ constructor(buf = new Uint8Array(16)) {
25
+ this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
26
+ this.dataView = new DataView(this.buf.buffer);
27
+ this.pos = 0;
28
+ this.type = 0;
29
+ this.length = this.buf.length;
30
+ }
31
+
32
+ // === READING =================================================================
33
+
34
+ /**
35
+ * @template T
36
+ * @param {(tag: number, result: T, pbf: Pbf) => void} readField
37
+ * @param {T} result
38
+ * @param {number} [end]
39
+ */
40
+ readFields(readField, result, end = this.length) {
41
+ while (this.pos < end) {
42
+ const val = this.readVarint(),
43
+ tag = val >> 3,
44
+ startPos = this.pos;
45
+
46
+ this.type = val & 0x7;
47
+ readField(tag, result, this);
48
+
49
+ if (this.pos === startPos) this.skip(val);
50
+ }
51
+ return result;
52
+ }
53
+
54
+ /**
55
+ * @template T
56
+ * @param {(tag: number, result: T, pbf: Pbf) => void} readField
57
+ * @param {T} result
58
+ */
59
+ readMessage(readField, result) {
60
+ return this.readFields(readField, result, this.readVarint() + this.pos);
61
+ }
62
+
63
+ readFixed32() {
64
+ const val = this.dataView.getUint32(this.pos, true);
65
+ this.pos += 4;
66
+ return val;
67
+ }
68
+
69
+ readSFixed32() {
70
+ const val = this.dataView.getInt32(this.pos, true);
71
+ this.pos += 4;
72
+ return val;
73
+ }
74
+
75
+ // 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
76
+
77
+ readFixed64() {
78
+ const val = this.dataView.getUint32(this.pos, true) + this.dataView.getUint32(this.pos + 4, true) * SHIFT_LEFT_32;
79
+ this.pos += 8;
80
+ return val;
81
+ }
82
+
83
+ readSFixed64() {
84
+ const val = this.dataView.getUint32(this.pos, true) + this.dataView.getInt32(this.pos + 4, true) * SHIFT_LEFT_32;
85
+ this.pos += 8;
86
+ return val;
87
+ }
88
+
89
+ readFloat() {
90
+ const val = this.dataView.getFloat32(this.pos, true);
91
+ this.pos += 4;
92
+ return val;
93
+ }
94
+
95
+ readDouble() {
96
+ const val = this.dataView.getFloat64(this.pos, true);
97
+ this.pos += 8;
98
+ return val;
99
+ }
100
+
101
+ /**
102
+ * @param {boolean} [isSigned]
103
+ */
104
+ readVarint(isSigned) {
105
+ const buf = this.buf;
106
+ let val, b;
107
+
108
+ b = buf[this.pos++]; val = b & 0x7f; if (b < 0x80) return val;
109
+ b = buf[this.pos++]; val |= (b & 0x7f) << 7; if (b < 0x80) return val;
110
+ b = buf[this.pos++]; val |= (b & 0x7f) << 14; if (b < 0x80) return val;
111
+ b = buf[this.pos++]; val |= (b & 0x7f) << 21; if (b < 0x80) return val;
112
+ b = buf[this.pos]; val |= (b & 0x0f) << 28;
113
+
114
+ return readVarintRemainder(val, isSigned, this);
115
+ }
116
+
117
+ readVarint64() { // for compatibility with v2.0.1
118
+ return this.readVarint(true);
119
+ }
120
+
121
+ readSVarint() {
122
+ const num = this.readVarint();
123
+ return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding
124
+ }
125
+
126
+ readBoolean() {
127
+ return Boolean(this.readVarint());
128
+ }
129
+
130
+ readString() {
131
+ const end = this.readVarint() + this.pos;
132
+ const pos = this.pos;
133
+ this.pos = end;
134
+
135
+ if (end - pos >= TEXT_DECODER_MIN_LENGTH && utf8TextDecoder) {
136
+ // longer strings are fast with the built-in browser TextDecoder API
137
+ return utf8TextDecoder.decode(this.buf.subarray(pos, end));
138
+ }
139
+ // short strings are fast with our custom implementation
140
+ return readUtf8(this.buf, pos, end);
141
+ }
142
+
143
+ readBytes() {
144
+ const end = this.readVarint() + this.pos,
145
+ buffer = this.buf.subarray(this.pos, end);
146
+ this.pos = end;
147
+ return buffer;
148
+ }
149
+
150
+ // verbose for performance reasons; doesn't affect gzipped size
151
+
152
+ /**
153
+ * @param {number[]} [arr]
154
+ * @param {boolean} [isSigned]
155
+ */
156
+ readPackedVarint(arr = [], isSigned) {
157
+ const end = this.readPackedEnd();
158
+ while (this.pos < end) arr.push(this.readVarint(isSigned));
159
+ return arr;
160
+ }
161
+ /** @param {number[]} [arr] */
162
+ readPackedSVarint(arr = []) {
163
+ const end = this.readPackedEnd();
164
+ while (this.pos < end) arr.push(this.readSVarint());
165
+ return arr;
166
+ }
167
+ /** @param {boolean[]} [arr] */
168
+ readPackedBoolean(arr = []) {
169
+ const end = this.readPackedEnd();
170
+ while (this.pos < end) arr.push(this.readBoolean());
171
+ return arr;
172
+ }
173
+ /** @param {number[]} [arr] */
174
+ readPackedFloat(arr = []) {
175
+ const end = this.readPackedEnd();
176
+ while (this.pos < end) arr.push(this.readFloat());
177
+ return arr;
178
+ }
179
+ /** @param {number[]} [arr] */
180
+ readPackedDouble(arr = []) {
181
+ const end = this.readPackedEnd();
182
+ while (this.pos < end) arr.push(this.readDouble());
183
+ return arr;
184
+ }
185
+ /** @param {number[]} [arr] */
186
+ readPackedFixed32(arr = []) {
187
+ const end = this.readPackedEnd();
188
+ while (this.pos < end) arr.push(this.readFixed32());
189
+ return arr;
190
+ }
191
+ /** @param {number[]} [arr] */
192
+ readPackedSFixed32(arr = []) {
193
+ const end = this.readPackedEnd();
194
+ while (this.pos < end) arr.push(this.readSFixed32());
195
+ return arr;
196
+ }
197
+ /** @param {number[]} [arr] */
198
+ readPackedFixed64(arr = []) {
199
+ const end = this.readPackedEnd();
200
+ while (this.pos < end) arr.push(this.readFixed64());
201
+ return arr;
202
+ }
203
+ /** @param {number[]} [arr] */
204
+ readPackedSFixed64(arr = []) {
205
+ const end = this.readPackedEnd();
206
+ while (this.pos < end) arr.push(this.readSFixed64());
207
+ return arr;
208
+ }
209
+ readPackedEnd() {
210
+ return this.type === PBF_BYTES ? this.readVarint() + this.pos : this.pos + 1;
211
+ }
212
+
213
+ /** @param {number} val */
214
+ skip(val) {
215
+ const type = val & 0x7;
216
+ if (type === PBF_VARINT) while (this.buf[this.pos++] > 0x7f) {}
217
+ else if (type === PBF_BYTES) this.pos = this.readVarint() + this.pos;
218
+ else if (type === PBF_FIXED32) this.pos += 4;
219
+ else if (type === PBF_FIXED64) this.pos += 8;
220
+ else throw new Error(`Unimplemented type: ${type}`);
221
+ }
222
+
223
+ // === WRITING =================================================================
224
+
225
+ /**
226
+ * @param {number} tag
227
+ * @param {number} type
228
+ */
229
+ writeTag(tag, type) {
230
+ this.writeVarint((tag << 3) | type);
231
+ }
232
+
233
+ /** @param {number} min */
234
+ realloc(min) {
235
+ let length = this.length || 16;
236
+
237
+ while (length < this.pos + min) length *= 2;
238
+
239
+ if (length !== this.length) {
240
+ const buf = new Uint8Array(length);
241
+ buf.set(this.buf);
242
+ this.buf = buf;
243
+ this.dataView = new DataView(buf.buffer);
244
+ this.length = length;
245
+ }
246
+ }
247
+
248
+ finish() {
249
+ this.length = this.pos;
250
+ this.pos = 0;
251
+ return this.buf.subarray(0, this.length);
252
+ }
253
+
254
+ /** @param {number} val */
255
+ writeFixed32(val) {
256
+ this.realloc(4);
257
+ this.dataView.setInt32(this.pos, val, true);
258
+ this.pos += 4;
259
+ }
260
+
261
+ /** @param {number} val */
262
+ writeSFixed32(val) {
263
+ this.realloc(4);
264
+ this.dataView.setInt32(this.pos, val, true);
265
+ this.pos += 4;
266
+ }
267
+
268
+ /** @param {number} val */
269
+ writeFixed64(val) {
270
+ this.realloc(8);
271
+ this.dataView.setInt32(this.pos, val & -1, true);
272
+ this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
273
+ this.pos += 8;
274
+ }
275
+
276
+ /** @param {number} val */
277
+ writeSFixed64(val) {
278
+ this.realloc(8);
279
+ this.dataView.setInt32(this.pos, val & -1, true);
280
+ this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
281
+ this.pos += 8;
282
+ }
283
+
284
+ /** @param {number} val */
285
+ writeVarint(val) {
286
+ val = +val || 0;
287
+
288
+ if (val > 0xfffffff || val < 0) {
289
+ writeBigVarint(val, this);
290
+ return;
291
+ }
292
+
293
+ this.realloc(4);
294
+
295
+ this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
296
+ this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
297
+ this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
298
+ this.buf[this.pos++] = (val >>> 7) & 0x7f;
299
+ }
300
+
301
+ /** @param {number} val */
302
+ writeSVarint(val) {
303
+ this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
304
+ }
305
+
306
+ /** @param {boolean} val */
307
+ writeBoolean(val) {
308
+ this.writeVarint(+val);
309
+ }
310
+
311
+ /** @param {string} str */
312
+ writeString(str) {
313
+ str = String(str);
314
+ this.realloc(str.length * 4);
315
+
316
+ this.pos++; // reserve 1 byte for short string length
317
+
318
+ const startPos = this.pos;
319
+ // write the string directly to the buffer and see how much was written
320
+ this.pos = writeUtf8(this.buf, str, this.pos);
321
+ const len = this.pos - startPos;
322
+
323
+ if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
324
+
325
+ // finally, write the message length in the reserved place and restore the position
326
+ this.pos = startPos - 1;
327
+ this.writeVarint(len);
328
+ this.pos += len;
329
+ }
330
+
331
+ /** @param {number} val */
332
+ writeFloat(val) {
333
+ this.realloc(4);
334
+ this.dataView.setFloat32(this.pos, val, true);
335
+ this.pos += 4;
336
+ }
337
+
338
+ /** @param {number} val */
339
+ writeDouble(val) {
340
+ this.realloc(8);
341
+ this.dataView.setFloat64(this.pos, val, true);
342
+ this.pos += 8;
343
+ }
344
+
345
+ /** @param {Uint8Array} buffer */
346
+ writeBytes(buffer) {
347
+ const len = buffer.length;
348
+ this.writeVarint(len);
349
+ this.realloc(len);
350
+ for (let i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
351
+ }
352
+
353
+ /**
354
+ * @template T
355
+ * @param {(obj: T, pbf: Pbf) => void} fn
356
+ * @param {T} obj
357
+ */
358
+ writeRawMessage(fn, obj) {
359
+ this.pos++; // reserve 1 byte for short message length
360
+
361
+ // write the message directly to the buffer and see how much was written
362
+ const startPos = this.pos;
363
+ fn(obj, this);
364
+ const len = this.pos - startPos;
365
+
366
+ if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
367
+
368
+ // finally, write the message length in the reserved place and restore the position
369
+ this.pos = startPos - 1;
370
+ this.writeVarint(len);
371
+ this.pos += len;
372
+ }
373
+
374
+ /**
375
+ * @template T
376
+ * @param {number} tag
377
+ * @param {(obj: T, pbf: Pbf) => void} fn
378
+ * @param {T} obj
379
+ */
380
+ writeMessage(tag, fn, obj) {
381
+ this.writeTag(tag, PBF_BYTES);
382
+ this.writeRawMessage(fn, obj);
383
+ }
384
+
385
+ /**
386
+ * @param {number} tag
387
+ * @param {number[]} arr
388
+ */
389
+ writePackedVarint(tag, arr) {
390
+ if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
391
+ }
392
+ /**
393
+ * @param {number} tag
394
+ * @param {number[]} arr
395
+ */
396
+ writePackedSVarint(tag, arr) {
397
+ if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
398
+ }
399
+ /**
400
+ * @param {number} tag
401
+ * @param {boolean[]} arr
402
+ */
403
+ writePackedBoolean(tag, arr) {
404
+ if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
405
+ }
406
+ /**
407
+ * @param {number} tag
408
+ * @param {number[]} arr
409
+ */
410
+ writePackedFloat(tag, arr) {
411
+ if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
412
+ }
413
+ /**
414
+ * @param {number} tag
415
+ * @param {number[]} arr
416
+ */
417
+ writePackedDouble(tag, arr) {
418
+ if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
419
+ }
420
+ /**
421
+ * @param {number} tag
422
+ * @param {number[]} arr
423
+ */
424
+ writePackedFixed32(tag, arr) {
425
+ if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
426
+ }
427
+ /**
428
+ * @param {number} tag
429
+ * @param {number[]} arr
430
+ */
431
+ writePackedSFixed32(tag, arr) {
432
+ if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
433
+ }
434
+ /**
435
+ * @param {number} tag
436
+ * @param {number[]} arr
437
+ */
438
+ writePackedFixed64(tag, arr) {
439
+ if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
440
+ }
441
+ /**
442
+ * @param {number} tag
443
+ * @param {number[]} arr
444
+ */
445
+ writePackedSFixed64(tag, arr) {
446
+ if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
447
+ }
448
+
449
+ /**
450
+ * @param {number} tag
451
+ * @param {Uint8Array} buffer
452
+ */
453
+ writeBytesField(tag, buffer) {
454
+ this.writeTag(tag, PBF_BYTES);
455
+ this.writeBytes(buffer);
456
+ }
457
+ /**
458
+ * @param {number} tag
459
+ * @param {number} val
460
+ */
461
+ writeFixed32Field(tag, val) {
462
+ this.writeTag(tag, PBF_FIXED32);
463
+ this.writeFixed32(val);
464
+ }
465
+ /**
466
+ * @param {number} tag
467
+ * @param {number} val
468
+ */
469
+ writeSFixed32Field(tag, val) {
470
+ this.writeTag(tag, PBF_FIXED32);
471
+ this.writeSFixed32(val);
472
+ }
473
+ /**
474
+ * @param {number} tag
475
+ * @param {number} val
476
+ */
477
+ writeFixed64Field(tag, val) {
478
+ this.writeTag(tag, PBF_FIXED64);
479
+ this.writeFixed64(val);
480
+ }
481
+ /**
482
+ * @param {number} tag
483
+ * @param {number} val
484
+ */
485
+ writeSFixed64Field(tag, val) {
486
+ this.writeTag(tag, PBF_FIXED64);
487
+ this.writeSFixed64(val);
488
+ }
489
+ /**
490
+ * @param {number} tag
491
+ * @param {number} val
492
+ */
493
+ writeVarintField(tag, val) {
494
+ this.writeTag(tag, PBF_VARINT);
495
+ this.writeVarint(val);
496
+ }
497
+ /**
498
+ * @param {number} tag
499
+ * @param {number} val
500
+ */
501
+ writeSVarintField(tag, val) {
502
+ this.writeTag(tag, PBF_VARINT);
503
+ this.writeSVarint(val);
504
+ }
505
+ /**
506
+ * @param {number} tag
507
+ * @param {string} str
508
+ */
509
+ writeStringField(tag, str) {
510
+ this.writeTag(tag, PBF_BYTES);
511
+ this.writeString(str);
512
+ }
513
+ /**
514
+ * @param {number} tag
515
+ * @param {number} val
516
+ */
517
+ writeFloatField(tag, val) {
518
+ this.writeTag(tag, PBF_FIXED32);
519
+ this.writeFloat(val);
520
+ }
521
+ /**
522
+ * @param {number} tag
523
+ * @param {number} val
524
+ */
525
+ writeDoubleField(tag, val) {
526
+ this.writeTag(tag, PBF_FIXED64);
527
+ this.writeDouble(val);
528
+ }
529
+ /**
530
+ * @param {number} tag
531
+ * @param {boolean} val
532
+ */
533
+ writeBooleanField(tag, val) {
534
+ this.writeVarintField(tag, +val);
535
+ }
536
+ }
537
+ /**
538
+ * @param {number} l
539
+ * @param {boolean | undefined} s
540
+ * @param {Pbf} p
541
+ */
542
+ function readVarintRemainder(l, s, p) {
543
+ const buf = p.buf;
544
+ let h, b;
545
+
546
+ b = buf[p.pos++]; h = (b & 0x70) >> 4; if (b < 0x80) return toNum(l, h, s);
547
+ b = buf[p.pos++]; h |= (b & 0x7f) << 3; if (b < 0x80) return toNum(l, h, s);
548
+ b = buf[p.pos++]; h |= (b & 0x7f) << 10; if (b < 0x80) return toNum(l, h, s);
549
+ b = buf[p.pos++]; h |= (b & 0x7f) << 17; if (b < 0x80) return toNum(l, h, s);
550
+ b = buf[p.pos++]; h |= (b & 0x7f) << 24; if (b < 0x80) return toNum(l, h, s);
551
+ b = buf[p.pos++]; h |= (b & 0x01) << 31; if (b < 0x80) return toNum(l, h, s);
552
+
553
+ throw new Error('Expected varint not more than 10 bytes');
554
+ }
555
+
556
+ /**
557
+ * @param {number} low
558
+ * @param {number} high
559
+ * @param {boolean} [isSigned]
560
+ */
561
+ function toNum(low, high, isSigned) {
562
+ return isSigned ? high * 0x100000000 + (low >>> 0) : ((high >>> 0) * 0x100000000) + (low >>> 0);
563
+ }
564
+
565
+ /**
566
+ * @param {number} val
567
+ * @param {Pbf} pbf
568
+ */
569
+ function writeBigVarint(val, pbf) {
570
+ let low, high;
571
+
572
+ if (val >= 0) {
573
+ low = (val % 0x100000000) | 0;
574
+ high = (val / 0x100000000) | 0;
575
+ } else {
576
+ low = ~(-val % 0x100000000);
577
+ high = ~(-val / 0x100000000);
578
+
579
+ if (low ^ 0xffffffff) {
580
+ low = (low + 1) | 0;
581
+ } else {
582
+ low = 0;
583
+ high = (high + 1) | 0;
584
+ }
585
+ }
586
+
587
+ if (val >= 0x10000000000000000 || val < -18446744073709552e3) {
588
+ throw new Error('Given varint doesn\'t fit into 10 bytes');
589
+ }
590
+
591
+ pbf.realloc(10);
592
+
593
+ writeBigVarintLow(low, high, pbf);
594
+ writeBigVarintHigh(high, pbf);
595
+ }
596
+
597
+ /**
598
+ * @param {number} high
599
+ * @param {number} low
600
+ * @param {Pbf} pbf
601
+ */
602
+ function writeBigVarintLow(low, high, pbf) {
603
+ pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
604
+ pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
605
+ pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
606
+ pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
607
+ pbf.buf[pbf.pos] = low & 0x7f;
608
+ }
609
+
610
+ /**
611
+ * @param {number} high
612
+ * @param {Pbf} pbf
613
+ */
614
+ function writeBigVarintHigh(high, pbf) {
615
+ const lsb = (high & 0x07) << 4;
616
+
617
+ pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 0x80 : 0); if (!high) return;
618
+ pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
619
+ pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
620
+ pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
621
+ pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
622
+ pbf.buf[pbf.pos++] = high & 0x7f;
623
+ }
624
+
625
+ /**
626
+ * @param {number} startPos
627
+ * @param {number} len
628
+ * @param {Pbf} pbf
629
+ */
630
+ function makeRoomForExtraLength(startPos, len, pbf) {
631
+ const extraLen =
632
+ len <= 0x3fff ? 1 :
633
+ len <= 0x1fffff ? 2 :
634
+ len <= 0xfffffff ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
635
+
636
+ // if 1 byte isn't enough for encoding message length, shift the data to the right
637
+ pbf.realloc(extraLen);
638
+ for (let i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
639
+ }
640
+
641
+ /**
642
+ * @param {number[]} arr
643
+ * @param {Pbf} pbf
644
+ */
645
+ function writePackedVarint(arr, pbf) {
646
+ for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]);
647
+ }
648
+ /**
649
+ * @param {number[]} arr
650
+ * @param {Pbf} pbf
651
+ */
652
+ function writePackedSVarint(arr, pbf) {
653
+ for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]);
654
+ }
655
+ /**
656
+ * @param {number[]} arr
657
+ * @param {Pbf} pbf
658
+ */
659
+ function writePackedFloat(arr, pbf) {
660
+ for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]);
661
+ }
662
+ /**
663
+ * @param {number[]} arr
664
+ * @param {Pbf} pbf
665
+ */
666
+ function writePackedDouble(arr, pbf) {
667
+ for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]);
668
+ }
669
+ /**
670
+ * @param {boolean[]} arr
671
+ * @param {Pbf} pbf
672
+ */
673
+ function writePackedBoolean(arr, pbf) {
674
+ for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]);
675
+ }
676
+ /**
677
+ * @param {number[]} arr
678
+ * @param {Pbf} pbf
679
+ */
680
+ function writePackedFixed32(arr, pbf) {
681
+ for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]);
682
+ }
683
+ /**
684
+ * @param {number[]} arr
685
+ * @param {Pbf} pbf
686
+ */
687
+ function writePackedSFixed32(arr, pbf) {
688
+ for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]);
689
+ }
690
+ /**
691
+ * @param {number[]} arr
692
+ * @param {Pbf} pbf
693
+ */
694
+ function writePackedFixed64(arr, pbf) {
695
+ for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]);
696
+ }
697
+ /**
698
+ * @param {number[]} arr
699
+ * @param {Pbf} pbf
700
+ */
701
+ function writePackedSFixed64(arr, pbf) {
702
+ for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]);
703
+ }
704
+
705
+ // Buffer code below from https://github.com/feross/buffer, MIT-licensed
706
+
707
+ /**
708
+ * @param {Uint8Array} buf
709
+ * @param {number} pos
710
+ * @param {number} end
711
+ */
712
+ function readUtf8(buf, pos, end) {
713
+ let str = '';
714
+ let i = pos;
715
+
716
+ while (i < end) {
717
+ const b0 = buf[i];
718
+ let c = null; // codepoint
719
+ let bytesPerSequence =
720
+ b0 > 0xEF ? 4 :
721
+ b0 > 0xDF ? 3 :
722
+ b0 > 0xBF ? 2 : 1;
723
+
724
+ if (i + bytesPerSequence > end) break;
725
+
726
+ let b1, b2, b3;
727
+
728
+ if (bytesPerSequence === 1) {
729
+ if (b0 < 0x80) {
730
+ c = b0;
731
+ }
732
+ } else if (bytesPerSequence === 2) {
733
+ b1 = buf[i + 1];
734
+ if ((b1 & 0xC0) === 0x80) {
735
+ c = (b0 & 0x1F) << 0x6 | (b1 & 0x3F);
736
+ if (c <= 0x7F) {
737
+ c = null;
738
+ }
739
+ }
740
+ } else if (bytesPerSequence === 3) {
741
+ b1 = buf[i + 1];
742
+ b2 = buf[i + 2];
743
+ if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80) {
744
+ c = (b0 & 0xF) << 0xC | (b1 & 0x3F) << 0x6 | (b2 & 0x3F);
745
+ if (c <= 0x7FF || (c >= 0xD800 && c <= 0xDFFF)) {
746
+ c = null;
747
+ }
748
+ }
749
+ } else if (bytesPerSequence === 4) {
750
+ b1 = buf[i + 1];
751
+ b2 = buf[i + 2];
752
+ b3 = buf[i + 3];
753
+ if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
754
+ c = (b0 & 0xF) << 0x12 | (b1 & 0x3F) << 0xC | (b2 & 0x3F) << 0x6 | (b3 & 0x3F);
755
+ if (c <= 0xFFFF || c >= 0x110000) {
756
+ c = null;
757
+ }
758
+ }
759
+ }
760
+
761
+ if (c === null) {
762
+ c = 0xFFFD;
763
+ bytesPerSequence = 1;
764
+
765
+ } else if (c > 0xFFFF) {
766
+ c -= 0x10000;
767
+ str += String.fromCharCode(c >>> 10 & 0x3FF | 0xD800);
768
+ c = 0xDC00 | c & 0x3FF;
769
+ }
770
+
771
+ str += String.fromCharCode(c);
772
+ i += bytesPerSequence;
773
+ }
774
+
775
+ return str;
776
+ }
777
+
778
+ /**
779
+ * @param {Uint8Array} buf
780
+ * @param {string} str
781
+ * @param {number} pos
782
+ */
783
+ function writeUtf8(buf, str, pos) {
784
+ for (let i = 0, c, lead; i < str.length; i++) {
785
+ c = str.charCodeAt(i); // code point
786
+
787
+ if (c > 0xD7FF && c < 0xE000) {
788
+ if (lead) {
789
+ if (c < 0xDC00) {
790
+ buf[pos++] = 0xEF;
791
+ buf[pos++] = 0xBF;
792
+ buf[pos++] = 0xBD;
793
+ lead = c;
794
+ continue;
795
+ } else {
796
+ c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
797
+ lead = null;
798
+ }
799
+ } else {
800
+ if (c > 0xDBFF || (i + 1 === str.length)) {
801
+ buf[pos++] = 0xEF;
802
+ buf[pos++] = 0xBF;
803
+ buf[pos++] = 0xBD;
804
+ } else {
805
+ lead = c;
806
+ }
807
+ continue;
808
+ }
809
+ } else if (lead) {
810
+ buf[pos++] = 0xEF;
811
+ buf[pos++] = 0xBF;
812
+ buf[pos++] = 0xBD;
813
+ lead = null;
814
+ }
815
+
816
+ if (c < 0x80) {
817
+ buf[pos++] = c;
818
+ } else {
819
+ if (c < 0x800) {
820
+ buf[pos++] = c >> 0x6 | 0xC0;
821
+ } else {
822
+ if (c < 0x10000) {
823
+ buf[pos++] = c >> 0xC | 0xE0;
824
+ } else {
825
+ buf[pos++] = c >> 0x12 | 0xF0;
826
+ buf[pos++] = c >> 0xC & 0x3F | 0x80;
827
+ }
828
+ buf[pos++] = c >> 0x6 & 0x3F | 0x80;
829
+ }
830
+ buf[pos++] = c & 0x3F | 0x80;
831
+ }
832
+ }
833
+ return pos;
834
+ }
835
+
836
+ return Pbf;
837
+
838
+ }));