@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.2-alpha.2

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 (138) hide show
  1. package/dist/cjs/binary-encoding.js +414 -409
  2. package/dist/cjs/binary-format.js +13 -0
  3. package/dist/cjs/descriptor-registry.js +420 -449
  4. package/dist/cjs/descriptor-set.js +429 -513
  5. package/dist/cjs/enum.js +13 -0
  6. package/dist/cjs/field-list.js +13 -0
  7. package/dist/cjs/field.js +43 -30
  8. package/dist/cjs/google/protobuf/any_pb.js +122 -140
  9. package/dist/cjs/google/protobuf/api_pb.js +177 -224
  10. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
  11. package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
  12. package/dist/cjs/google/protobuf/duration_pb.js +72 -88
  13. package/dist/cjs/google/protobuf/empty_pb.js +17 -17
  14. package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
  15. package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
  16. package/dist/cjs/google/protobuf/struct_pb.js +144 -181
  17. package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
  18. package/dist/cjs/google/protobuf/type_pb.js +406 -462
  19. package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
  20. package/dist/cjs/google/varint.js +166 -171
  21. package/dist/cjs/index.js +41 -122
  22. package/dist/cjs/json-format.js +13 -0
  23. package/dist/cjs/message-type.js +13 -0
  24. package/dist/cjs/message.js +89 -85
  25. package/dist/cjs/private/assert.js +33 -26
  26. package/dist/cjs/private/base64.js +93 -79
  27. package/dist/cjs/private/binary-format-common.js +208 -208
  28. package/dist/cjs/private/binary-format-proto2.js +88 -109
  29. package/dist/cjs/private/binary-format-proto3.js +70 -90
  30. package/dist/cjs/private/enum.js +77 -44
  31. package/dist/cjs/private/field-list.js +64 -50
  32. package/dist/cjs/private/field-wrapper.js +21 -10
  33. package/dist/cjs/private/field.js +35 -25
  34. package/dist/cjs/private/json-format-common.js +427 -440
  35. package/dist/cjs/private/json-format-proto2.js +91 -89
  36. package/dist/cjs/private/json-format-proto3.js +82 -93
  37. package/dist/cjs/private/message-type.js +39 -27
  38. package/dist/cjs/private/names.js +71 -62
  39. package/dist/cjs/private/options-map.js +13 -0
  40. package/dist/cjs/private/proto-runtime.js +25 -16
  41. package/dist/cjs/private/scalars.js +113 -103
  42. package/dist/cjs/private/util-common.js +193 -210
  43. package/dist/cjs/private/util.js +13 -0
  44. package/dist/cjs/proto-int64.js +123 -111
  45. package/dist/cjs/proto2.js +63 -63
  46. package/dist/cjs/proto3.js +74 -74
  47. package/dist/cjs/service-type.js +27 -17
  48. package/dist/cjs/type-registry.js +46 -31
  49. package/dist/esm/binary-encoding.js +414 -414
  50. package/dist/esm/binary-format.js +13 -0
  51. package/dist/esm/descriptor-registry.js +422 -458
  52. package/dist/esm/descriptor-set.js +430 -478
  53. package/dist/esm/enum.js +13 -0
  54. package/dist/esm/field-list.js +13 -0
  55. package/dist/esm/field.js +42 -29
  56. package/dist/esm/google/protobuf/any_pb.js +122 -140
  57. package/dist/esm/google/protobuf/api_pb.js +177 -197
  58. package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
  59. package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
  60. package/dist/esm/google/protobuf/duration_pb.js +72 -88
  61. package/dist/esm/google/protobuf/empty_pb.js +17 -17
  62. package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
  63. package/dist/esm/google/protobuf/source_context_pb.js +25 -25
  64. package/dist/esm/google/protobuf/struct_pb.js +143 -180
  65. package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
  66. package/dist/esm/google/protobuf/type_pb.js +401 -421
  67. package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
  68. package/dist/esm/google/varint.js +165 -164
  69. package/dist/esm/index.js +14 -1
  70. package/dist/esm/json-format.js +13 -0
  71. package/dist/esm/message-type.js +13 -0
  72. package/dist/esm/message.js +89 -85
  73. package/dist/esm/private/assert.js +32 -21
  74. package/dist/esm/private/base64.js +92 -78
  75. package/dist/esm/private/binary-format-common.js +209 -207
  76. package/dist/esm/private/binary-format-proto2.js +89 -81
  77. package/dist/esm/private/binary-format-proto3.js +71 -67
  78. package/dist/esm/private/enum.js +75 -43
  79. package/dist/esm/private/field-list.js +64 -50
  80. package/dist/esm/private/field-wrapper.js +21 -10
  81. package/dist/esm/private/field.js +35 -22
  82. package/dist/esm/private/json-format-common.js +427 -435
  83. package/dist/esm/private/json-format-proto2.js +89 -81
  84. package/dist/esm/private/json-format-proto3.js +79 -80
  85. package/dist/esm/private/message-type.js +40 -28
  86. package/dist/esm/private/names.js +70 -57
  87. package/dist/esm/private/options-map.js +13 -0
  88. package/dist/esm/private/proto-runtime.js +26 -12
  89. package/dist/esm/private/scalars.js +112 -99
  90. package/dist/esm/private/util-common.js +193 -192
  91. package/dist/esm/private/util.js +13 -0
  92. package/dist/esm/proto-int64.js +123 -111
  93. package/dist/esm/proto2.js +63 -57
  94. package/dist/esm/proto3.js +74 -68
  95. package/dist/esm/service-type.js +25 -12
  96. package/dist/esm/type-registry.js +46 -31
  97. package/dist/types/binary-encoding.d.ts +398 -398
  98. package/dist/types/binary-format.d.ts +69 -91
  99. package/dist/types/descriptor-registry.d.ts +23 -29
  100. package/dist/types/descriptor-set.d.ts +107 -128
  101. package/dist/types/enum.d.ts +21 -21
  102. package/dist/types/field-list.d.ts +21 -21
  103. package/dist/types/field.d.ts +191 -231
  104. package/dist/types/google/protobuf/any_pb.d.ts +56 -77
  105. package/dist/types/google/protobuf/api_pb.d.ts +144 -187
  106. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
  107. package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
  108. package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
  109. package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
  110. package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
  111. package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
  112. package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
  113. package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
  114. package/dist/types/google/protobuf/type_pb.d.ts +355 -422
  115. package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
  116. package/dist/types/google/varint.d.ts +6 -13
  117. package/dist/types/index.d.ts +4 -27
  118. package/dist/types/json-format.d.ts +70 -89
  119. package/dist/types/message-type.d.ts +37 -45
  120. package/dist/types/message.d.ts +62 -87
  121. package/dist/types/private/assert.d.ts +1 -4
  122. package/dist/types/private/binary-format-common.d.ts +6 -32
  123. package/dist/types/private/enum.d.ts +13 -11
  124. package/dist/types/private/field-list.d.ts +14 -21
  125. package/dist/types/private/field-wrapper.d.ts +4 -10
  126. package/dist/types/private/field.d.ts +12 -12
  127. package/dist/types/private/json-format-common.d.ts +5 -27
  128. package/dist/types/private/message-type.d.ts +12 -7
  129. package/dist/types/private/names.d.ts +1 -4
  130. package/dist/types/private/options-map.d.ts +1 -1
  131. package/dist/types/private/proto-runtime.d.ts +34 -42
  132. package/dist/types/private/scalars.d.ts +5 -12
  133. package/dist/types/private/util-common.d.ts +1 -4
  134. package/dist/types/private/util.d.ts +29 -41
  135. package/dist/types/proto-int64.d.ts +42 -42
  136. package/dist/types/service-type.d.ts +36 -56
  137. package/dist/types/type-registry.d.ts +13 -15
  138. package/package.json +2 -3
@@ -1,4 +1,17 @@
1
1
  "use strict";
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
2
15
  Object.defineProperty(exports, "__esModule", { value: true });
3
16
  exports.Message = void 0;
4
17
  /**
@@ -9,90 +22,81 @@ exports.Message = void 0;
9
22
  * run time, use proto3.makeMessageType().
10
23
  */
11
24
  class Message {
12
- /**
13
- * Compare with a message of the same type.
14
- */
15
- equals(other) {
16
- return this.getType().runtime.util.equals(this.getType(), this, other);
17
- }
18
- /**
19
- * Create a deep copy.
20
- */
21
- clone() {
22
- // return this.getType().runtime.util.clone(this);
23
- return this.getType().runtime.util.clone(this);
24
- }
25
- /**
26
- * Parse from binary data, merging fields.
27
- *
28
- * Repeated fields are appended. Map entries are added, overwriting
29
- * existing keys.
30
- *
31
- * If a message field is already present, it will be merged with the
32
- * new data.
33
- */
34
- fromBinary(bytes, options) {
35
- const type = this.getType(),
36
- format = type.runtime.bin,
37
- opt = format.makeReadOptions(options);
38
- format.readMessage(this, opt.readerFactory(bytes), bytes.byteLength, opt);
39
- return this;
40
- }
41
- /**
42
- * Parse a message from a JSON value.
43
- */
44
- fromJson(jsonValue, options) {
45
- const type = this.getType(),
46
- format = type.runtime.json,
47
- opt = format.makeReadOptions(options);
48
- format.readMessage(type, jsonValue, opt, this);
49
- return this;
50
- }
51
- /**
52
- * Parse a message from a JSON string.
53
- */
54
- fromJsonString(jsonString, options) {
55
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- assigning to JsonValue is safe here
56
- return this.fromJson(JSON.parse(jsonString), options);
57
- }
58
- /**
59
- * Serialize the message to binary data.
60
- */
61
- toBinary(options) {
62
- const type = this.getType(),
63
- bin = type.runtime.bin,
64
- opt = bin.makeWriteOptions(options),
65
- writer = opt.writerFactory();
66
- bin.writeMessage(this, writer, opt);
67
- return writer.finish();
68
- }
69
- /**
70
- * Serialize the message to a JSON value, a JavaScript value that can be
71
- * passed to JSON.stringify().
72
- */
73
- toJson(options) {
74
- const type = this.getType(),
75
- json = type.runtime.json,
76
- opt = json.makeWriteOptions(options);
77
- return json.writeMessage(this, opt);
78
- }
79
- /**
80
- * Serialize the message to a JSON string.
81
- */
82
- toJsonString(options) {
83
- const value = this.toJson(options);
84
- return JSON.stringify(value, null, options?.prettySpaces ?? 0);
85
- }
86
- /**
87
- * Retrieve the MessageType of this message - a singleton that represents
88
- * the protobuf message declaration and provides metadata for reflection-
89
- * based operations.
90
- */
91
- getType() {
92
- // Any class that extends Message _must_ provide a complete static
93
- // implementation of MessageType.
94
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
95
- return Object.getPrototypeOf(this).constructor;
96
- }
25
+ /**
26
+ * Compare with a message of the same type.
27
+ */
28
+ equals(other) {
29
+ return this.getType().runtime.util.equals(this.getType(), this, other);
30
+ }
31
+ /**
32
+ * Create a deep copy.
33
+ */
34
+ clone() {
35
+ // return this.getType().runtime.util.clone(this);
36
+ return this.getType().runtime.util.clone(this);
37
+ }
38
+ /**
39
+ * Parse from binary data, merging fields.
40
+ *
41
+ * Repeated fields are appended. Map entries are added, overwriting
42
+ * existing keys.
43
+ *
44
+ * If a message field is already present, it will be merged with the
45
+ * new data.
46
+ */
47
+ fromBinary(bytes, options) {
48
+ const type = this.getType(), format = type.runtime.bin, opt = format.makeReadOptions(options);
49
+ format.readMessage(this, opt.readerFactory(bytes), bytes.byteLength, opt);
50
+ return this;
51
+ }
52
+ /**
53
+ * Parse a message from a JSON value.
54
+ */
55
+ fromJson(jsonValue, options) {
56
+ const type = this.getType(), format = type.runtime.json, opt = format.makeReadOptions(options);
57
+ format.readMessage(type, jsonValue, opt, this);
58
+ return this;
59
+ }
60
+ /**
61
+ * Parse a message from a JSON string.
62
+ */
63
+ fromJsonString(jsonString, options) {
64
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- assigning to JsonValue is safe here
65
+ return this.fromJson(JSON.parse(jsonString), options);
66
+ }
67
+ /**
68
+ * Serialize the message to binary data.
69
+ */
70
+ toBinary(options) {
71
+ const type = this.getType(), bin = type.runtime.bin, opt = bin.makeWriteOptions(options), writer = opt.writerFactory();
72
+ bin.writeMessage(this, writer, opt);
73
+ return writer.finish();
74
+ }
75
+ /**
76
+ * Serialize the message to a JSON value, a JavaScript value that can be
77
+ * passed to JSON.stringify().
78
+ */
79
+ toJson(options) {
80
+ const type = this.getType(), json = type.runtime.json, opt = json.makeWriteOptions(options);
81
+ return json.writeMessage(this, opt);
82
+ }
83
+ /**
84
+ * Serialize the message to a JSON string.
85
+ */
86
+ toJsonString(options) {
87
+ const value = this.toJson(options);
88
+ return JSON.stringify(value, null, options?.prettySpaces ?? 0);
89
+ }
90
+ /**
91
+ * Retrieve the MessageType of this message - a singleton that represents
92
+ * the protobuf message declaration and provides metadata for reflection-
93
+ * based operations.
94
+ */
95
+ getType() {
96
+ // Any class that extends Message _must_ provide a complete static
97
+ // implementation of MessageType.
98
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
99
+ return Object.getPrototypeOf(this).constructor;
100
+ }
97
101
  }
98
102
  exports.Message = Message;
@@ -1,52 +1,59 @@
1
1
  "use strict";
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
2
15
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assertFloat32 =
4
- exports.assertUInt32 =
5
- exports.assertInt32 =
6
- exports.assert =
7
- void 0;
16
+ exports.assertFloat32 = exports.assertUInt32 = exports.assertInt32 = exports.assert = void 0;
8
17
  /**
9
18
  * Assert that condition is truthy or throw error (with message)
10
19
  */
11
20
  function assert(condition, msg) {
12
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
13
- if (!condition) {
14
- throw new Error(msg);
15
- }
21
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
22
+ if (!condition) {
23
+ throw new Error(msg);
24
+ }
16
25
  }
17
26
  exports.assert = assert;
18
- const FLOAT32_MAX = 3.4028234663852886e38,
19
- FLOAT32_MIN = -3.4028234663852886e38,
20
- UINT32_MAX = 0xffffffff,
21
- INT32_MAX = 0x7fffffff,
22
- INT32_MIN = -0x80000000;
27
+ const FLOAT32_MAX = 3.4028234663852886e38, FLOAT32_MIN = -3.4028234663852886e38, UINT32_MAX = 0xffffffff, INT32_MAX = 0x7fffffff, INT32_MIN = -0x80000000;
23
28
  /**
24
29
  * Assert a valid signed protobuf 32-bit integer.
25
30
  */
26
31
  function assertInt32(arg) {
27
- if (typeof arg !== "number") throw new Error("invalid int 32: " + typeof arg);
28
- if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
29
- throw new Error("invalid int 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
32
+ if (typeof arg !== "number")
33
+ throw new Error("invalid int 32: " + typeof arg);
34
+ if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
35
+ throw new Error("invalid int 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
30
36
  }
31
37
  exports.assertInt32 = assertInt32;
32
38
  /**
33
39
  * Assert a valid unsigned protobuf 32-bit integer.
34
40
  */
35
41
  function assertUInt32(arg) {
36
- if (typeof arg !== "number")
37
- throw new Error("invalid uint 32: " + typeof arg);
38
- if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
39
- throw new Error("invalid uint 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
42
+ if (typeof arg !== "number")
43
+ throw new Error("invalid uint 32: " + typeof arg);
44
+ if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
45
+ throw new Error("invalid uint 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
40
46
  }
41
47
  exports.assertUInt32 = assertUInt32;
42
48
  /**
43
49
  * Assert a valid protobuf float value.
44
50
  */
45
51
  function assertFloat32(arg) {
46
- if (typeof arg !== "number")
47
- throw new Error("invalid float 32: " + typeof arg);
48
- if (!Number.isFinite(arg)) return;
49
- if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
50
- throw new Error("invalid float 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
52
+ if (typeof arg !== "number")
53
+ throw new Error("invalid float 32: " + typeof arg);
54
+ if (!Number.isFinite(arg))
55
+ return;
56
+ if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
57
+ throw new Error("invalid float 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
51
58
  }
52
59
  exports.assertFloat32 = assertFloat32;
@@ -1,14 +1,26 @@
1
1
  "use strict";
2
- /* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
3
15
  Object.defineProperty(exports, "__esModule", { value: true });
4
16
  exports.base64encode = exports.base64decode = void 0;
17
+ /* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
5
18
  // lookup table from base64 character to byte
6
- let encTable =
7
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
19
+ let encTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
8
20
  // lookup table from base64 character *code* to byte because lookup by number is fast
9
21
  let decTable = [];
10
22
  for (let i = 0; i < encTable.length; i++)
11
- decTable[encTable[i].charCodeAt(0)] = i;
23
+ decTable[encTable[i].charCodeAt(0)] = i;
12
24
  // support base64url variants
13
25
  decTable["-".charCodeAt(0)] = encTable.indexOf("+");
14
26
  decTable["_".charCodeAt(0)] = encTable.indexOf("/");
@@ -24,57 +36,59 @@ decTable["_".charCodeAt(0)] = encTable.indexOf("/");
24
36
  * no padding
25
37
  */
26
38
  function base64decode(base64Str) {
27
- // estimate byte size, not accounting for inner padding and whitespace
28
- let es = (base64Str.length * 3) / 4;
29
- // if (es % 3 !== 0)
30
- // throw new Error("invalid base64 string");
31
- if (base64Str[base64Str.length - 2] == "=") es -= 2;
32
- else if (base64Str[base64Str.length - 1] == "=") es -= 1;
33
- let bytes = new Uint8Array(es),
34
- bytePos = 0, // position in byte array
39
+ // estimate byte size, not accounting for inner padding and whitespace
40
+ let es = (base64Str.length * 3) / 4;
41
+ // if (es % 3 !== 0)
42
+ // throw new Error("invalid base64 string");
43
+ if (base64Str[base64Str.length - 2] == "=")
44
+ es -= 2;
45
+ else if (base64Str[base64Str.length - 1] == "=")
46
+ es -= 1;
47
+ let bytes = new Uint8Array(es), bytePos = 0, // position in byte array
35
48
  groupPos = 0, // position in base64 group
36
49
  b, // current byte
37
50
  p = 0; // previous byte
38
- for (let i = 0; i < base64Str.length; i++) {
39
- b = decTable[base64Str.charCodeAt(i)];
40
- if (b === undefined) {
41
- switch (base64Str[i]) {
42
- // @ts-ignore TS7029: Fallthrough case in switch
43
- case "=":
44
- groupPos = 0; // reset state when padding found
45
- // @ts-ignore TS7029: Fallthrough case in switch
46
- case "\n":
47
- case "\r":
48
- case "\t":
49
- case " ":
50
- continue; // skip white-space, and padding
51
- default:
52
- throw Error("invalid base64 string.");
53
- }
54
- }
55
- switch (groupPos) {
56
- case 0:
57
- p = b;
58
- groupPos = 1;
59
- break;
60
- case 1:
61
- bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
62
- p = b;
63
- groupPos = 2;
64
- break;
65
- case 2:
66
- bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
67
- p = b;
68
- groupPos = 3;
69
- break;
70
- case 3:
71
- bytes[bytePos++] = ((p & 3) << 6) | b;
72
- groupPos = 0;
73
- break;
51
+ for (let i = 0; i < base64Str.length; i++) {
52
+ b = decTable[base64Str.charCodeAt(i)];
53
+ if (b === undefined) {
54
+ switch (base64Str[i]) {
55
+ // @ts-ignore TS7029: Fallthrough case in switch
56
+ case "=":
57
+ groupPos = 0; // reset state when padding found
58
+ // @ts-ignore TS7029: Fallthrough case in switch
59
+ case "\n":
60
+ case "\r":
61
+ case "\t":
62
+ case " ":
63
+ continue; // skip white-space, and padding
64
+ default:
65
+ throw Error("invalid base64 string.");
66
+ }
67
+ }
68
+ switch (groupPos) {
69
+ case 0:
70
+ p = b;
71
+ groupPos = 1;
72
+ break;
73
+ case 1:
74
+ bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
75
+ p = b;
76
+ groupPos = 2;
77
+ break;
78
+ case 2:
79
+ bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
80
+ p = b;
81
+ groupPos = 3;
82
+ break;
83
+ case 3:
84
+ bytes[bytePos++] = ((p & 3) << 6) | b;
85
+ groupPos = 0;
86
+ break;
87
+ }
74
88
  }
75
- }
76
- if (groupPos == 1) throw Error("invalid base64 string.");
77
- return bytes.subarray(0, bytePos);
89
+ if (groupPos == 1)
90
+ throw Error("invalid base64 string.");
91
+ return bytes.subarray(0, bytePos);
78
92
  }
79
93
  exports.base64decode = base64decode;
80
94
  /**
@@ -83,36 +97,36 @@ exports.base64decode = base64decode;
83
97
  * Does not insert newlines.
84
98
  */
85
99
  function base64encode(bytes) {
86
- let base64 = "",
87
- groupPos = 0, // position in base64 group
100
+ let base64 = "", groupPos = 0, // position in base64 group
88
101
  b, // current byte
89
102
  p = 0; // carry over from previous byte
90
- for (let i = 0; i < bytes.length; i++) {
91
- b = bytes[i];
92
- switch (groupPos) {
93
- case 0:
94
- base64 += encTable[b >> 2];
95
- p = (b & 3) << 4;
96
- groupPos = 1;
97
- break;
98
- case 1:
99
- base64 += encTable[p | (b >> 4)];
100
- p = (b & 15) << 2;
101
- groupPos = 2;
102
- break;
103
- case 2:
104
- base64 += encTable[p | (b >> 6)];
105
- base64 += encTable[b & 63];
106
- groupPos = 0;
107
- break;
103
+ for (let i = 0; i < bytes.length; i++) {
104
+ b = bytes[i];
105
+ switch (groupPos) {
106
+ case 0:
107
+ base64 += encTable[b >> 2];
108
+ p = (b & 3) << 4;
109
+ groupPos = 1;
110
+ break;
111
+ case 1:
112
+ base64 += encTable[p | (b >> 4)];
113
+ p = (b & 15) << 2;
114
+ groupPos = 2;
115
+ break;
116
+ case 2:
117
+ base64 += encTable[p | (b >> 6)];
118
+ base64 += encTable[b & 63];
119
+ groupPos = 0;
120
+ break;
121
+ }
122
+ }
123
+ // padding required?
124
+ if (groupPos) {
125
+ base64 += encTable[p];
126
+ base64 += "=";
127
+ if (groupPos == 1)
128
+ base64 += "=";
108
129
  }
109
- }
110
- // padding required?
111
- if (groupPos) {
112
- base64 += encTable[p];
113
- base64 += "=";
114
- if (groupPos == 1) base64 += "=";
115
- }
116
- return base64;
130
+ return base64;
117
131
  }
118
132
  exports.base64encode = base64encode;