@bufbuild/protobuf 0.0.2-alpha.2 → 0.0.4

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 (63) hide show
  1. package/README.md +5 -2
  2. package/dist/cjs/descriptor-registry.js +13 -0
  3. package/dist/cjs/google/protobuf/any_pb.js +19 -39
  4. package/dist/cjs/google/protobuf/api_pb.js +28 -48
  5. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +32 -67
  6. package/dist/cjs/google/protobuf/descriptor_pb.js +161 -187
  7. package/dist/cjs/google/protobuf/duration_pb.js +21 -41
  8. package/dist/cjs/google/protobuf/empty_pb.js +16 -36
  9. package/dist/cjs/google/protobuf/field_mask_pb.js +17 -37
  10. package/dist/cjs/google/protobuf/source_context_pb.js +16 -36
  11. package/dist/cjs/google/protobuf/struct_pb.js +31 -51
  12. package/dist/cjs/google/protobuf/timestamp_pb.js +20 -40
  13. package/dist/cjs/google/protobuf/type_pb.js +43 -63
  14. package/dist/cjs/google/protobuf/wrappers_pb.js +76 -105
  15. package/dist/cjs/index-runtime.js +42 -0
  16. package/dist/cjs/index-wkt.js +42 -0
  17. package/dist/cjs/index.js +4 -38
  18. package/dist/cjs/private/json-format-common.js +3 -3
  19. package/dist/cjs/private/util-common.js +1 -1
  20. package/dist/cjs/proto-base64.js +138 -0
  21. package/dist/esm/descriptor-registry.js +13 -0
  22. package/dist/esm/google/protobuf/any_pb.js +14 -34
  23. package/dist/esm/google/protobuf/api_pb.js +11 -31
  24. package/dist/esm/google/protobuf/compiler/plugin_pb.js +11 -46
  25. package/dist/esm/google/protobuf/descriptor_pb.js +14 -40
  26. package/dist/esm/google/protobuf/duration_pb.js +11 -31
  27. package/dist/esm/google/protobuf/empty_pb.js +11 -31
  28. package/dist/esm/google/protobuf/field_mask_pb.js +11 -31
  29. package/dist/esm/google/protobuf/source_context_pb.js +11 -31
  30. package/dist/esm/google/protobuf/struct_pb.js +11 -31
  31. package/dist/esm/google/protobuf/timestamp_pb.js +11 -31
  32. package/dist/esm/google/protobuf/type_pb.js +11 -31
  33. package/dist/esm/google/protobuf/wrappers_pb.js +11 -40
  34. package/dist/esm/index-runtime.js +25 -0
  35. package/dist/esm/index-wkt.js +26 -0
  36. package/dist/esm/index.js +4 -24
  37. package/dist/esm/private/json-format-common.js +3 -3
  38. package/dist/esm/private/util-common.js +1 -1
  39. package/dist/esm/proto-base64.js +135 -0
  40. package/dist/types/descriptor-registry.d.ts +7 -0
  41. package/dist/types/google/protobuf/any_pb.d.ts +5 -5
  42. package/dist/types/google/protobuf/api_pb.d.ts +2 -2
  43. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +2 -2
  44. package/dist/types/google/protobuf/descriptor_pb.d.ts +18 -5
  45. package/dist/types/google/protobuf/duration_pb.d.ts +2 -2
  46. package/dist/types/google/protobuf/empty_pb.d.ts +2 -2
  47. package/dist/types/google/protobuf/field_mask_pb.d.ts +2 -2
  48. package/dist/types/google/protobuf/source_context_pb.d.ts +2 -2
  49. package/dist/types/google/protobuf/struct_pb.d.ts +2 -2
  50. package/dist/types/google/protobuf/timestamp_pb.d.ts +2 -2
  51. package/dist/types/google/protobuf/type_pb.d.ts +2 -2
  52. package/dist/types/google/protobuf/wrappers_pb.d.ts +2 -2
  53. package/dist/types/index-runtime.d.ts +19 -0
  54. package/dist/types/index-wkt.d.ts +12 -0
  55. package/dist/types/index.d.ts +2 -30
  56. package/dist/types/message-type.d.ts +2 -1
  57. package/dist/types/message.d.ts +4 -6
  58. package/dist/types/private/util.d.ts +2 -2
  59. package/dist/types/proto-base64.d.ts +26 -0
  60. package/package.json +3 -2
  61. package/dist/cjs/private/base64.js +0 -132
  62. package/dist/esm/private/base64.js +0 -127
  63. package/dist/types/private/base64.d.ts +0 -18
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * The protocol compiler can output a FileDescriptorSet containing the .proto
5
5
  * files it parses.
@@ -286,7 +286,6 @@ export declare class FieldDescriptorProto extends Message<FieldDescriptorProto>
286
286
  * For booleans, "true" or "false".
287
287
  * For strings, contains the default text contents (not escaped in any way).
288
288
  * For bytes, contains the C escaped value. All bytes >= 128 are escaped.
289
- * TODO(kenton): Base-64 encode?
290
289
  *
291
290
  * @generated from field: optional string default_value = 7;
292
291
  */
@@ -1020,9 +1019,23 @@ export declare class FieldOptions extends Message<FieldOptions> {
1020
1019
  * check its required fields, regardless of whether or not the message has
1021
1020
  * been parsed.
1022
1021
  *
1022
+ * As of 2021, lazy does no correctness checks on the byte stream during
1023
+ * parsing. This may lead to crashes if and when an invalid byte stream is
1024
+ * finally parsed upon access.
1025
+ *
1026
+ * TODO(b/211906113): Enable validation on lazy fields.
1027
+ *
1023
1028
  * @generated from field: optional bool lazy = 5 [default = false];
1024
1029
  */
1025
1030
  lazy?: boolean;
1031
+ /**
1032
+ * unverified_lazy does no correctness checks on the byte stream. This should
1033
+ * only be used where lazy with verification is prohibitive for performance
1034
+ * reasons.
1035
+ *
1036
+ * @generated from field: optional bool unverified_lazy = 15 [default = false];
1037
+ */
1038
+ unverifiedLazy?: boolean;
1026
1039
  /**
1027
1040
  * Is this field deprecated?
1028
1041
  * Depending on the target platform, this can emit Deprecated annotations
@@ -1413,8 +1426,8 @@ export declare class SourceCodeInfo_Location extends Message<SourceCodeInfo_Loca
1413
1426
  * location.
1414
1427
  *
1415
1428
  * Each element is a field number or an index. They form a path from
1416
- * the root FileDescriptorProto to the place where the definition. For
1417
- * example, this path:
1429
+ * the root FileDescriptorProto to the place where the definition occurs.
1430
+ * For example, this path:
1418
1431
  * [ 4, 3, 2, 7, 1 ]
1419
1432
  * refers to:
1420
1433
  * file.message_type(3) // 4, 3
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * A Duration represents a signed, fixed-length span of time represented
5
5
  * as a count of seconds and fractions of seconds at nanosecond
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * A generic empty message that you can re-use to avoid defining duplicated
5
5
  * empty messages in your APIs. A typical example is to use it as the request
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * `FieldMask` represents a set of symbolic field paths, for example:
5
5
  *
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * `SourceContext` represents information about the source of a
5
5
  * protobuf element, like the file in which it is defined.
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * `NullValue` is a singleton enumeration to represent the null value for the
5
5
  * `Value` type union.
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * A Timestamp represents a point in time independent of any time zone or local
5
5
  * calendar, encoded as a count of seconds and fractions of seconds at
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  import { SourceContext } from "./source_context_pb.js";
4
4
  import { Any } from "./any_pb.js";
5
5
  /**
@@ -1,5 +1,5 @@
1
- import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index.js";
2
- import { Message } from "../../index.js";
1
+ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, JsonWriteOptions, PartialMessage, PlainMessage } from "../../index-runtime.js";
2
+ import { Message } from "../../index-runtime.js";
3
3
  /**
4
4
  * Wrapper message for `double`.
5
5
  *
@@ -0,0 +1,19 @@
1
+ export { proto3 } from "./proto3.js";
2
+ export { proto2 } from "./proto2.js";
3
+ export { protoInt64 } from "./proto-int64.js";
4
+ export { protoBase64 } from "./proto-base64.js";
5
+ export { Message, AnyMessage, PartialMessage, PlainMessage, } from "./message.js";
6
+ export type { FieldInfo } from "./field.js";
7
+ export type { FieldList } from "./field-list.js";
8
+ export { ScalarType } from "./field.js";
9
+ export type { MessageType } from "./message-type.js";
10
+ export type { EnumType, EnumValueInfo } from "./enum.js";
11
+ export type { ServiceType, MethodInfo, MethodInfoUnary, MethodInfoServerStreaming, MethodInfoClientStreaming, MethodInfoBiDiStreaming, } from "./service-type.js";
12
+ export { MethodKind, MethodIdempotency } from "./service-type.js";
13
+ export { TypeRegistry, IMessageTypeRegistry } from "./type-registry.js";
14
+ export { DescriptorRegistry } from "./descriptor-registry.js";
15
+ export { DescriptorSet } from "./descriptor-set.js";
16
+ export { WireType, BinaryWriter, BinaryReader } from "./binary-encoding.js";
17
+ export type { IBinaryReader, IBinaryWriter } from "./binary-encoding.js";
18
+ export type { BinaryFormat, BinaryWriteOptions, BinaryReadOptions, } from "./binary-format.js";
19
+ export { JsonFormat, JsonObject, JsonValue, JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions, } from "./json-format.js";
@@ -0,0 +1,12 @@
1
+ export * from "./google/protobuf/compiler/plugin_pb.js";
2
+ export * from "./google/protobuf/api_pb.js";
3
+ export * from "./google/protobuf/any_pb.js";
4
+ export * from "./google/protobuf/descriptor_pb.js";
5
+ export * from "./google/protobuf/duration_pb.js";
6
+ export * from "./google/protobuf/empty_pb.js";
7
+ export * from "./google/protobuf/field_mask_pb.js";
8
+ export * from "./google/protobuf/source_context_pb.js";
9
+ export * from "./google/protobuf/struct_pb.js";
10
+ export * from "./google/protobuf/timestamp_pb.js";
11
+ export * from "./google/protobuf/type_pb.js";
12
+ export * from "./google/protobuf/wrappers_pb.js";
@@ -1,30 +1,2 @@
1
- export { proto3 } from "./proto3.js";
2
- export { proto2 } from "./proto2.js";
3
- export { protoInt64 } from "./proto-int64.js";
4
- export { Message, AnyMessage, PartialMessage, PlainMessage, } from "./message.js";
5
- export type { FieldInfo } from "./field.js";
6
- export type { FieldList } from "./field-list.js";
7
- export { ScalarType } from "./field.js";
8
- export type { MessageType } from "./message-type.js";
9
- export type { EnumType, EnumValueInfo } from "./enum.js";
10
- export type { ServiceType, MethodInfo, MethodInfoUnary, MethodInfoServerStreaming, MethodInfoClientStreaming, MethodInfoBiDiStreaming, } from "./service-type.js";
11
- export { MethodKind, MethodIdempotency } from "./service-type.js";
12
- export { TypeRegistry, IMessageTypeRegistry } from "./type-registry.js";
13
- export { DescriptorRegistry } from "./descriptor-registry.js";
14
- export { DescriptorSet } from "./descriptor-set.js";
15
- export { WireType, BinaryWriter, BinaryReader } from "./binary-encoding.js";
16
- export type { IBinaryReader, IBinaryWriter } from "./binary-encoding.js";
17
- export type { BinaryFormat, BinaryWriteOptions, BinaryReadOptions, } from "./binary-format.js";
18
- export { JsonFormat, JsonObject, JsonValue, JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions, } from "./json-format.js";
19
- export * from "./google/protobuf/compiler/plugin_pb.js";
20
- export * from "./google/protobuf/api_pb.js";
21
- export * from "./google/protobuf/any_pb.js";
22
- export * from "./google/protobuf/descriptor_pb.js";
23
- export * from "./google/protobuf/duration_pb.js";
24
- export * from "./google/protobuf/empty_pb.js";
25
- export * from "./google/protobuf/field_mask_pb.js";
26
- export * from "./google/protobuf/source_context_pb.js";
27
- export * from "./google/protobuf/struct_pb.js";
28
- export * from "./google/protobuf/timestamp_pb.js";
29
- export * from "./google/protobuf/type_pb.js";
30
- export * from "./google/protobuf/wrappers_pb.js";
1
+ export * from "./index-runtime.js";
2
+ export * from "./index-wkt.js";
@@ -45,6 +45,7 @@ export interface MessageType<T extends Message<T> = AnyMessage> {
45
45
  fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): T;
46
46
  /**
47
47
  * Returns true if the given arguments have equal field values, recursively.
48
+ * Will also return true if both messages are `undefined` or `null`.
48
49
  */
49
- equals(a: T | PlainMessage<T> | undefined, b: T | PlainMessage<T> | undefined): boolean;
50
+ equals(a: T | PlainMessage<T> | undefined | null, b: T | PlainMessage<T> | undefined | null): boolean;
50
51
  }
@@ -20,7 +20,7 @@ export declare class Message<T extends Message<T> = AnyMessage> {
20
20
  /**
21
21
  * Compare with a message of the same type.
22
22
  */
23
- equals(other: T | PlainMessage<T> | undefined): boolean;
23
+ equals(other: T | PlainMessage<T> | undefined | null): boolean;
24
24
  /**
25
25
  * Create a deep copy.
26
26
  */
@@ -67,9 +67,7 @@ export declare class Message<T extends Message<T> = AnyMessage> {
67
67
  * PlainMessage<T> strips all methods from a message, leaving only fields
68
68
  * and oneof groups.
69
69
  */
70
- export declare type PlainMessage<T extends Message> = {
71
- [P in keyof T as T[P] extends Function ? never : P]: T[P];
72
- };
70
+ export declare type PlainMessage<T extends Message> = Omit<T, keyof Message>;
73
71
  /**
74
72
  * PartialMessage<T> constructs a type from a message. The resulting type
75
73
  * only contains the protobuf field members of the message, and all of them
@@ -81,13 +79,13 @@ export declare type PlainMessage<T extends Message> = {
81
79
  export declare type PartialMessage<T extends Message> = {
82
80
  [P in keyof T as T[P] extends Function ? never : P]?: PartialField<T[P]>;
83
81
  };
84
- declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PartialField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : F extends OneofSelectedMessage<infer C, infer V> ? {
82
+ declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PartialField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : F extends Message ? PartialMessage<F> : F extends OneofSelectedMessage<infer C, infer V> ? {
85
83
  case: C;
86
84
  value: PartialMessage<V>;
87
85
  } : F extends {
88
86
  case: string | undefined;
89
87
  value?: unknown;
90
- } ? F : F extends Message ? PartialMessage<F> : F extends {
88
+ } ? F : F extends {
91
89
  [key: string | number]: Message<infer U>;
92
90
  } ? {
93
91
  [key: string | number]: PartialMessage<U>;
@@ -33,9 +33,9 @@ export interface Util {
33
33
  initPartial<T extends Message<T>>(source: PartialMessage<T> | undefined, target: T): void;
34
34
  /**
35
35
  * Compares two messages of the same type recursively.
36
- * Will also return true if both messages are `undefined`.
36
+ * Will also return true if both messages are `undefined` or `null`.
37
37
  */
38
- equals<T extends Message<T>>(type: MessageType, a: T | PlainMessage<T> | undefined, b: T | PlainMessage<T> | undefined): boolean;
38
+ equals<T extends Message<T>>(type: MessageType, a: T | PlainMessage<T> | undefined | null, b: T | PlainMessage<T> | undefined | null): boolean;
39
39
  /**
40
40
  * Create a deep copy.
41
41
  */
@@ -0,0 +1,26 @@
1
+ export declare const protoBase64: {
2
+ /**
3
+ * Decodes a base64 string to a byte array.
4
+ *
5
+ * - ignores white-space, including line breaks and tabs
6
+ * - allows inner padding (can decode concatenated base64 strings)
7
+ * - does not require padding
8
+ * - understands base64url encoding:
9
+ * "-" instead of "+",
10
+ * "_" instead of "/",
11
+ * no padding
12
+ */
13
+ readonly dec: (base64Str: string) => Uint8Array;
14
+ /**
15
+ * Decodes a base64 string to a byte array.
16
+ *
17
+ * - ignores white-space, including line breaks and tabs
18
+ * - allows inner padding (can decode concatenated base64 strings)
19
+ * - does not require padding
20
+ * - understands base64url encoding:
21
+ * "-" instead of "+",
22
+ * "_" instead of "/",
23
+ * no padding
24
+ */
25
+ readonly enc: (bytes: Uint8Array) => string;
26
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protobuf",
3
- "version": "0.0.2-alpha.2",
3
+ "version": "0.0.4",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "A complete implementation of protocol buffers in TypeScript, suitable for web browsers and Node.js.",
6
6
  "repository": {
@@ -8,6 +8,7 @@
8
8
  "url": "https://github.com/bufbuild/protobuf-es.git",
9
9
  "directory": "packages/protobuf"
10
10
  },
11
+ "sideEffects": false,
11
12
  "scripts": {
12
13
  "clean": "rm -rf ./dist/cjs/* ./dist/esm/* ./dist/types/*",
13
14
  "build": "npm run build:cjs && npm run build:esm+types",
@@ -22,7 +23,7 @@
22
23
  "default": "./dist/esm/index.js"
23
24
  },
24
25
  "devDependencies": {
25
- "typescript": "^4.6.3"
26
+ "typescript": "^4.6.4"
26
27
  },
27
28
  "files": [
28
29
  "dist/**/"
@@ -1,132 +0,0 @@
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.
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.base64encode = exports.base64decode = void 0;
17
- /* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
18
- // lookup table from base64 character to byte
19
- let encTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
20
- // lookup table from base64 character *code* to byte because lookup by number is fast
21
- let decTable = [];
22
- for (let i = 0; i < encTable.length; i++)
23
- decTable[encTable[i].charCodeAt(0)] = i;
24
- // support base64url variants
25
- decTable["-".charCodeAt(0)] = encTable.indexOf("+");
26
- decTable["_".charCodeAt(0)] = encTable.indexOf("/");
27
- /**
28
- * Decodes a base64 string to a byte array.
29
- *
30
- * - ignores white-space, including line breaks and tabs
31
- * - allows inner padding (can decode concatenated base64 strings)
32
- * - does not require padding
33
- * - understands base64url encoding:
34
- * "-" instead of "+",
35
- * "_" instead of "/",
36
- * no padding
37
- */
38
- function base64decode(base64Str) {
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
48
- groupPos = 0, // position in base64 group
49
- b, // current byte
50
- p = 0; // previous byte
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
- }
88
- }
89
- if (groupPos == 1)
90
- throw Error("invalid base64 string.");
91
- return bytes.subarray(0, bytePos);
92
- }
93
- exports.base64decode = base64decode;
94
- /**
95
- * Encodes a byte array to a base64 string.
96
- * Adds padding at the end.
97
- * Does not insert newlines.
98
- */
99
- function base64encode(bytes) {
100
- let base64 = "", groupPos = 0, // position in base64 group
101
- b, // current byte
102
- p = 0; // carry over from previous byte
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 += "=";
129
- }
130
- return base64;
131
- }
132
- exports.base64encode = base64encode;
@@ -1,127 +0,0 @@
1
- // Copyright 2021-2022 Buf Technologies, Inc.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
- /* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
15
- // lookup table from base64 character to byte
16
- let encTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
17
- // lookup table from base64 character *code* to byte because lookup by number is fast
18
- let decTable = [];
19
- for (let i = 0; i < encTable.length; i++)
20
- decTable[encTable[i].charCodeAt(0)] = i;
21
- // support base64url variants
22
- decTable["-".charCodeAt(0)] = encTable.indexOf("+");
23
- decTable["_".charCodeAt(0)] = encTable.indexOf("/");
24
- /**
25
- * Decodes a base64 string to a byte array.
26
- *
27
- * - ignores white-space, including line breaks and tabs
28
- * - allows inner padding (can decode concatenated base64 strings)
29
- * - does not require padding
30
- * - understands base64url encoding:
31
- * "-" instead of "+",
32
- * "_" instead of "/",
33
- * no padding
34
- */
35
- export function base64decode(base64Str) {
36
- // estimate byte size, not accounting for inner padding and whitespace
37
- let es = (base64Str.length * 3) / 4;
38
- // if (es % 3 !== 0)
39
- // throw new Error("invalid base64 string");
40
- if (base64Str[base64Str.length - 2] == "=")
41
- es -= 2;
42
- else if (base64Str[base64Str.length - 1] == "=")
43
- es -= 1;
44
- let bytes = new Uint8Array(es), bytePos = 0, // position in byte array
45
- groupPos = 0, // position in base64 group
46
- b, // current byte
47
- p = 0; // previous byte
48
- for (let i = 0; i < base64Str.length; i++) {
49
- b = decTable[base64Str.charCodeAt(i)];
50
- if (b === undefined) {
51
- switch (base64Str[i]) {
52
- // @ts-ignore TS7029: Fallthrough case in switch
53
- case "=":
54
- groupPos = 0; // reset state when padding found
55
- // @ts-ignore TS7029: Fallthrough case in switch
56
- case "\n":
57
- case "\r":
58
- case "\t":
59
- case " ":
60
- continue; // skip white-space, and padding
61
- default:
62
- throw Error("invalid base64 string.");
63
- }
64
- }
65
- switch (groupPos) {
66
- case 0:
67
- p = b;
68
- groupPos = 1;
69
- break;
70
- case 1:
71
- bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
72
- p = b;
73
- groupPos = 2;
74
- break;
75
- case 2:
76
- bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
77
- p = b;
78
- groupPos = 3;
79
- break;
80
- case 3:
81
- bytes[bytePos++] = ((p & 3) << 6) | b;
82
- groupPos = 0;
83
- break;
84
- }
85
- }
86
- if (groupPos == 1)
87
- throw Error("invalid base64 string.");
88
- return bytes.subarray(0, bytePos);
89
- }
90
- /**
91
- * Encodes a byte array to a base64 string.
92
- * Adds padding at the end.
93
- * Does not insert newlines.
94
- */
95
- export function base64encode(bytes) {
96
- let base64 = "", groupPos = 0, // position in base64 group
97
- b, // current byte
98
- p = 0; // carry over from previous byte
99
- for (let i = 0; i < bytes.length; i++) {
100
- b = bytes[i];
101
- switch (groupPos) {
102
- case 0:
103
- base64 += encTable[b >> 2];
104
- p = (b & 3) << 4;
105
- groupPos = 1;
106
- break;
107
- case 1:
108
- base64 += encTable[p | (b >> 4)];
109
- p = (b & 15) << 2;
110
- groupPos = 2;
111
- break;
112
- case 2:
113
- base64 += encTable[p | (b >> 6)];
114
- base64 += encTable[b & 63];
115
- groupPos = 0;
116
- break;
117
- }
118
- }
119
- // padding required?
120
- if (groupPos) {
121
- base64 += encTable[p];
122
- base64 += "=";
123
- if (groupPos == 1)
124
- base64 += "=";
125
- }
126
- return base64;
127
- }
@@ -1,18 +0,0 @@
1
- /**
2
- * Decodes a base64 string to a byte array.
3
- *
4
- * - ignores white-space, including line breaks and tabs
5
- * - allows inner padding (can decode concatenated base64 strings)
6
- * - does not require padding
7
- * - understands base64url encoding:
8
- * "-" instead of "+",
9
- * "_" instead of "/",
10
- * no padding
11
- */
12
- export declare function base64decode(base64Str: string): Uint8Array;
13
- /**
14
- * Encodes a byte array to a base64 string.
15
- * Adds padding at the end.
16
- * Does not insert newlines.
17
- */
18
- export declare function base64encode(bytes: Uint8Array): string;