protobufjs 8.0.3-experimental → 8.0.3

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 (98) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +727 -727
  3. package/dist/light/protobuf.js +7724 -7352
  4. package/dist/light/protobuf.js.map +1 -1
  5. package/dist/light/protobuf.min.js +3 -3
  6. package/dist/light/protobuf.min.js.map +1 -1
  7. package/dist/minimal/protobuf.js +2606 -2546
  8. package/dist/minimal/protobuf.js.map +1 -1
  9. package/dist/minimal/protobuf.min.js +3 -3
  10. package/dist/minimal/protobuf.min.js.map +1 -1
  11. package/dist/protobuf.js +9588 -9131
  12. package/dist/protobuf.js.map +1 -1
  13. package/dist/protobuf.min.js +3 -3
  14. package/dist/protobuf.min.js.map +1 -1
  15. package/ext/debug/README.md +4 -4
  16. package/ext/debug/index.js +71 -71
  17. package/ext/descriptor/README.md +72 -72
  18. package/ext/descriptor/index.d.ts +195 -191
  19. package/ext/descriptor/index.js +1181 -1052
  20. package/ext/descriptor/test.js +54 -54
  21. package/google/LICENSE +27 -27
  22. package/google/README.md +1 -1
  23. package/google/api/annotations.json +82 -82
  24. package/google/api/annotations.proto +10 -10
  25. package/google/api/http.json +85 -85
  26. package/google/api/http.proto +30 -30
  27. package/google/protobuf/api.json +117 -117
  28. package/google/protobuf/api.proto +33 -33
  29. package/google/protobuf/descriptor.json +1381 -738
  30. package/google/protobuf/descriptor.proto +535 -286
  31. package/google/protobuf/source_context.json +19 -19
  32. package/google/protobuf/source_context.proto +7 -7
  33. package/google/protobuf/type.json +201 -201
  34. package/google/protobuf/type.proto +89 -89
  35. package/index.d.ts +2817 -2792
  36. package/index.js +4 -4
  37. package/light.d.ts +2 -2
  38. package/light.js +3 -3
  39. package/minimal.d.ts +2 -2
  40. package/minimal.js +4 -4
  41. package/package.json +96 -112
  42. package/scripts/postinstall.js +32 -32
  43. package/src/common.js +399 -399
  44. package/src/converter.js +310 -301
  45. package/src/decoder.js +135 -127
  46. package/src/encoder.js +100 -100
  47. package/src/enum.js +226 -241
  48. package/src/field.js +453 -432
  49. package/src/index-light.js +104 -104
  50. package/src/index-minimal.js +36 -36
  51. package/src/index.js +12 -12
  52. package/src/mapfield.js +126 -126
  53. package/src/message.js +139 -139
  54. package/src/method.js +160 -160
  55. package/src/namespace.js +550 -434
  56. package/src/object.js +385 -330
  57. package/src/oneof.js +222 -222
  58. package/src/parse.js +1024 -944
  59. package/src/reader.js +426 -416
  60. package/src/reader_buffer.js +51 -51
  61. package/src/root.js +404 -384
  62. package/src/roots.js +17 -17
  63. package/src/rpc/service.js +142 -142
  64. package/src/rpc.js +36 -36
  65. package/src/service.js +193 -168
  66. package/src/tokenize.js +421 -416
  67. package/src/type.js +625 -590
  68. package/src/types.js +196 -196
  69. package/src/typescript.jsdoc +15 -15
  70. package/src/util/aspromise.d.ts +13 -0
  71. package/src/util/aspromise.js +52 -0
  72. package/src/util/base64.d.ts +32 -0
  73. package/src/util/base64.js +139 -0
  74. package/src/util/codegen.d.ts +31 -0
  75. package/src/util/codegen.js +113 -0
  76. package/src/util/eventemitter.d.ts +45 -0
  77. package/src/util/eventemitter.js +84 -0
  78. package/src/util/fetch.d.ts +56 -0
  79. package/src/util/fetch.js +114 -0
  80. package/src/util/float.d.ts +83 -0
  81. package/src/util/float.js +335 -0
  82. package/src/util/inquire.d.ts +9 -0
  83. package/src/util/inquire.js +37 -0
  84. package/src/util/longbits.js +200 -200
  85. package/src/util/minimal.js +461 -438
  86. package/src/util/path.d.ts +22 -0
  87. package/src/util/path.js +72 -0
  88. package/src/util/patterns.js +8 -0
  89. package/src/util/pool.d.ts +32 -0
  90. package/src/util/pool.js +48 -0
  91. package/src/util/utf8.d.ts +24 -0
  92. package/src/util/utf8.js +104 -0
  93. package/src/util.js +215 -213
  94. package/src/verifier.js +180 -177
  95. package/src/wrappers.js +103 -102
  96. package/src/writer.js +465 -465
  97. package/src/writer_buffer.js +85 -85
  98. package/tsconfig.json +8 -8
package/src/types.js CHANGED
@@ -1,196 +1,196 @@
1
- "use strict";
2
-
3
- /**
4
- * Common type constants.
5
- * @namespace
6
- */
7
- var types = exports;
8
-
9
- var util = require("./util");
10
-
11
- var s = [
12
- "double", // 0
13
- "float", // 1
14
- "int32", // 2
15
- "uint32", // 3
16
- "sint32", // 4
17
- "fixed32", // 5
18
- "sfixed32", // 6
19
- "int64", // 7
20
- "uint64", // 8
21
- "sint64", // 9
22
- "fixed64", // 10
23
- "sfixed64", // 11
24
- "bool", // 12
25
- "string", // 13
26
- "bytes" // 14
27
- ];
28
-
29
- function bake(values, offset) {
30
- var i = 0, o = {};
31
- offset |= 0;
32
- while (i < values.length) o[s[i + offset]] = values[i++];
33
- return o;
34
- }
35
-
36
- /**
37
- * Basic type wire types.
38
- * @type {Object.<string,number>}
39
- * @const
40
- * @property {number} double=1 Fixed64 wire type
41
- * @property {number} float=5 Fixed32 wire type
42
- * @property {number} int32=0 Varint wire type
43
- * @property {number} uint32=0 Varint wire type
44
- * @property {number} sint32=0 Varint wire type
45
- * @property {number} fixed32=5 Fixed32 wire type
46
- * @property {number} sfixed32=5 Fixed32 wire type
47
- * @property {number} int64=0 Varint wire type
48
- * @property {number} uint64=0 Varint wire type
49
- * @property {number} sint64=0 Varint wire type
50
- * @property {number} fixed64=1 Fixed64 wire type
51
- * @property {number} sfixed64=1 Fixed64 wire type
52
- * @property {number} bool=0 Varint wire type
53
- * @property {number} string=2 Ldelim wire type
54
- * @property {number} bytes=2 Ldelim wire type
55
- */
56
- types.basic = bake([
57
- /* double */ 1,
58
- /* float */ 5,
59
- /* int32 */ 0,
60
- /* uint32 */ 0,
61
- /* sint32 */ 0,
62
- /* fixed32 */ 5,
63
- /* sfixed32 */ 5,
64
- /* int64 */ 0,
65
- /* uint64 */ 0,
66
- /* sint64 */ 0,
67
- /* fixed64 */ 1,
68
- /* sfixed64 */ 1,
69
- /* bool */ 0,
70
- /* string */ 2,
71
- /* bytes */ 2
72
- ]);
73
-
74
- /**
75
- * Basic type defaults.
76
- * @type {Object.<string,*>}
77
- * @const
78
- * @property {number} double=0 Double default
79
- * @property {number} float=0 Float default
80
- * @property {number} int32=0 Int32 default
81
- * @property {number} uint32=0 Uint32 default
82
- * @property {number} sint32=0 Sint32 default
83
- * @property {number} fixed32=0 Fixed32 default
84
- * @property {number} sfixed32=0 Sfixed32 default
85
- * @property {number} int64=0 Int64 default
86
- * @property {number} uint64=0 Uint64 default
87
- * @property {number} sint64=0 Sint32 default
88
- * @property {number} fixed64=0 Fixed64 default
89
- * @property {number} sfixed64=0 Sfixed64 default
90
- * @property {boolean} bool=false Bool default
91
- * @property {string} string="" String default
92
- * @property {Array.<number>} bytes=Array(0) Bytes default
93
- * @property {null} message=null Message default
94
- */
95
- types.defaults = bake([
96
- /* double */ 0,
97
- /* float */ 0,
98
- /* int32 */ 0,
99
- /* uint32 */ 0,
100
- /* sint32 */ 0,
101
- /* fixed32 */ 0,
102
- /* sfixed32 */ 0,
103
- /* int64 */ 0,
104
- /* uint64 */ 0,
105
- /* sint64 */ 0,
106
- /* fixed64 */ 0,
107
- /* sfixed64 */ 0,
108
- /* bool */ false,
109
- /* string */ "",
110
- /* bytes */ util.emptyArray,
111
- /* message */ null
112
- ]);
113
-
114
- /**
115
- * Basic long type wire types.
116
- * @type {Object.<string,number>}
117
- * @const
118
- * @property {number} int64=0 Varint wire type
119
- * @property {number} uint64=0 Varint wire type
120
- * @property {number} sint64=0 Varint wire type
121
- * @property {number} fixed64=1 Fixed64 wire type
122
- * @property {number} sfixed64=1 Fixed64 wire type
123
- */
124
- types.long = bake([
125
- /* int64 */ 0,
126
- /* uint64 */ 0,
127
- /* sint64 */ 0,
128
- /* fixed64 */ 1,
129
- /* sfixed64 */ 1
130
- ], 7);
131
-
132
- /**
133
- * Allowed types for map keys with their associated wire type.
134
- * @type {Object.<string,number>}
135
- * @const
136
- * @property {number} int32=0 Varint wire type
137
- * @property {number} uint32=0 Varint wire type
138
- * @property {number} sint32=0 Varint wire type
139
- * @property {number} fixed32=5 Fixed32 wire type
140
- * @property {number} sfixed32=5 Fixed32 wire type
141
- * @property {number} int64=0 Varint wire type
142
- * @property {number} uint64=0 Varint wire type
143
- * @property {number} sint64=0 Varint wire type
144
- * @property {number} fixed64=1 Fixed64 wire type
145
- * @property {number} sfixed64=1 Fixed64 wire type
146
- * @property {number} bool=0 Varint wire type
147
- * @property {number} string=2 Ldelim wire type
148
- */
149
- types.mapKey = bake([
150
- /* int32 */ 0,
151
- /* uint32 */ 0,
152
- /* sint32 */ 0,
153
- /* fixed32 */ 5,
154
- /* sfixed32 */ 5,
155
- /* int64 */ 0,
156
- /* uint64 */ 0,
157
- /* sint64 */ 0,
158
- /* fixed64 */ 1,
159
- /* sfixed64 */ 1,
160
- /* bool */ 0,
161
- /* string */ 2
162
- ], 2);
163
-
164
- /**
165
- * Allowed types for packed repeated fields with their associated wire type.
166
- * @type {Object.<string,number>}
167
- * @const
168
- * @property {number} double=1 Fixed64 wire type
169
- * @property {number} float=5 Fixed32 wire type
170
- * @property {number} int32=0 Varint wire type
171
- * @property {number} uint32=0 Varint wire type
172
- * @property {number} sint32=0 Varint wire type
173
- * @property {number} fixed32=5 Fixed32 wire type
174
- * @property {number} sfixed32=5 Fixed32 wire type
175
- * @property {number} int64=0 Varint wire type
176
- * @property {number} uint64=0 Varint wire type
177
- * @property {number} sint64=0 Varint wire type
178
- * @property {number} fixed64=1 Fixed64 wire type
179
- * @property {number} sfixed64=1 Fixed64 wire type
180
- * @property {number} bool=0 Varint wire type
181
- */
182
- types.packed = bake([
183
- /* double */ 1,
184
- /* float */ 5,
185
- /* int32 */ 0,
186
- /* uint32 */ 0,
187
- /* sint32 */ 0,
188
- /* fixed32 */ 5,
189
- /* sfixed32 */ 5,
190
- /* int64 */ 0,
191
- /* uint64 */ 0,
192
- /* sint64 */ 0,
193
- /* fixed64 */ 1,
194
- /* sfixed64 */ 1,
195
- /* bool */ 0
196
- ]);
1
+ "use strict";
2
+
3
+ /**
4
+ * Common type constants.
5
+ * @namespace
6
+ */
7
+ var types = exports;
8
+
9
+ var util = require("./util");
10
+
11
+ var s = [
12
+ "double", // 0
13
+ "float", // 1
14
+ "int32", // 2
15
+ "uint32", // 3
16
+ "sint32", // 4
17
+ "fixed32", // 5
18
+ "sfixed32", // 6
19
+ "int64", // 7
20
+ "uint64", // 8
21
+ "sint64", // 9
22
+ "fixed64", // 10
23
+ "sfixed64", // 11
24
+ "bool", // 12
25
+ "string", // 13
26
+ "bytes" // 14
27
+ ];
28
+
29
+ function bake(values, offset) {
30
+ var i = 0, o = Object.create(null);
31
+ offset |= 0;
32
+ while (i < values.length) o[s[i + offset]] = values[i++];
33
+ return o;
34
+ }
35
+
36
+ /**
37
+ * Basic type wire types.
38
+ * @type {Object.<string,number>}
39
+ * @const
40
+ * @property {number} double=1 Fixed64 wire type
41
+ * @property {number} float=5 Fixed32 wire type
42
+ * @property {number} int32=0 Varint wire type
43
+ * @property {number} uint32=0 Varint wire type
44
+ * @property {number} sint32=0 Varint wire type
45
+ * @property {number} fixed32=5 Fixed32 wire type
46
+ * @property {number} sfixed32=5 Fixed32 wire type
47
+ * @property {number} int64=0 Varint wire type
48
+ * @property {number} uint64=0 Varint wire type
49
+ * @property {number} sint64=0 Varint wire type
50
+ * @property {number} fixed64=1 Fixed64 wire type
51
+ * @property {number} sfixed64=1 Fixed64 wire type
52
+ * @property {number} bool=0 Varint wire type
53
+ * @property {number} string=2 Ldelim wire type
54
+ * @property {number} bytes=2 Ldelim wire type
55
+ */
56
+ types.basic = bake([
57
+ /* double */ 1,
58
+ /* float */ 5,
59
+ /* int32 */ 0,
60
+ /* uint32 */ 0,
61
+ /* sint32 */ 0,
62
+ /* fixed32 */ 5,
63
+ /* sfixed32 */ 5,
64
+ /* int64 */ 0,
65
+ /* uint64 */ 0,
66
+ /* sint64 */ 0,
67
+ /* fixed64 */ 1,
68
+ /* sfixed64 */ 1,
69
+ /* bool */ 0,
70
+ /* string */ 2,
71
+ /* bytes */ 2
72
+ ]);
73
+
74
+ /**
75
+ * Basic type defaults.
76
+ * @type {Object.<string,*>}
77
+ * @const
78
+ * @property {number} double=0 Double default
79
+ * @property {number} float=0 Float default
80
+ * @property {number} int32=0 Int32 default
81
+ * @property {number} uint32=0 Uint32 default
82
+ * @property {number} sint32=0 Sint32 default
83
+ * @property {number} fixed32=0 Fixed32 default
84
+ * @property {number} sfixed32=0 Sfixed32 default
85
+ * @property {number} int64=0 Int64 default
86
+ * @property {number} uint64=0 Uint64 default
87
+ * @property {number} sint64=0 Sint32 default
88
+ * @property {number} fixed64=0 Fixed64 default
89
+ * @property {number} sfixed64=0 Sfixed64 default
90
+ * @property {boolean} bool=false Bool default
91
+ * @property {string} string="" String default
92
+ * @property {Array.<number>} bytes=Array(0) Bytes default
93
+ * @property {null} message=null Message default
94
+ */
95
+ types.defaults = bake([
96
+ /* double */ 0,
97
+ /* float */ 0,
98
+ /* int32 */ 0,
99
+ /* uint32 */ 0,
100
+ /* sint32 */ 0,
101
+ /* fixed32 */ 0,
102
+ /* sfixed32 */ 0,
103
+ /* int64 */ 0,
104
+ /* uint64 */ 0,
105
+ /* sint64 */ 0,
106
+ /* fixed64 */ 0,
107
+ /* sfixed64 */ 0,
108
+ /* bool */ false,
109
+ /* string */ "",
110
+ /* bytes */ util.emptyArray,
111
+ /* message */ null
112
+ ]);
113
+
114
+ /**
115
+ * Basic long type wire types.
116
+ * @type {Object.<string,number>}
117
+ * @const
118
+ * @property {number} int64=0 Varint wire type
119
+ * @property {number} uint64=0 Varint wire type
120
+ * @property {number} sint64=0 Varint wire type
121
+ * @property {number} fixed64=1 Fixed64 wire type
122
+ * @property {number} sfixed64=1 Fixed64 wire type
123
+ */
124
+ types.long = bake([
125
+ /* int64 */ 0,
126
+ /* uint64 */ 0,
127
+ /* sint64 */ 0,
128
+ /* fixed64 */ 1,
129
+ /* sfixed64 */ 1
130
+ ], 7);
131
+
132
+ /**
133
+ * Allowed types for map keys with their associated wire type.
134
+ * @type {Object.<string,number>}
135
+ * @const
136
+ * @property {number} int32=0 Varint wire type
137
+ * @property {number} uint32=0 Varint wire type
138
+ * @property {number} sint32=0 Varint wire type
139
+ * @property {number} fixed32=5 Fixed32 wire type
140
+ * @property {number} sfixed32=5 Fixed32 wire type
141
+ * @property {number} int64=0 Varint wire type
142
+ * @property {number} uint64=0 Varint wire type
143
+ * @property {number} sint64=0 Varint wire type
144
+ * @property {number} fixed64=1 Fixed64 wire type
145
+ * @property {number} sfixed64=1 Fixed64 wire type
146
+ * @property {number} bool=0 Varint wire type
147
+ * @property {number} string=2 Ldelim wire type
148
+ */
149
+ types.mapKey = bake([
150
+ /* int32 */ 0,
151
+ /* uint32 */ 0,
152
+ /* sint32 */ 0,
153
+ /* fixed32 */ 5,
154
+ /* sfixed32 */ 5,
155
+ /* int64 */ 0,
156
+ /* uint64 */ 0,
157
+ /* sint64 */ 0,
158
+ /* fixed64 */ 1,
159
+ /* sfixed64 */ 1,
160
+ /* bool */ 0,
161
+ /* string */ 2
162
+ ], 2);
163
+
164
+ /**
165
+ * Allowed types for packed repeated fields with their associated wire type.
166
+ * @type {Object.<string,number>}
167
+ * @const
168
+ * @property {number} double=1 Fixed64 wire type
169
+ * @property {number} float=5 Fixed32 wire type
170
+ * @property {number} int32=0 Varint wire type
171
+ * @property {number} uint32=0 Varint wire type
172
+ * @property {number} sint32=0 Varint wire type
173
+ * @property {number} fixed32=5 Fixed32 wire type
174
+ * @property {number} sfixed32=5 Fixed32 wire type
175
+ * @property {number} int64=0 Varint wire type
176
+ * @property {number} uint64=0 Varint wire type
177
+ * @property {number} sint64=0 Varint wire type
178
+ * @property {number} fixed64=1 Fixed64 wire type
179
+ * @property {number} sfixed64=1 Fixed64 wire type
180
+ * @property {number} bool=0 Varint wire type
181
+ */
182
+ types.packed = bake([
183
+ /* double */ 1,
184
+ /* float */ 5,
185
+ /* int32 */ 0,
186
+ /* uint32 */ 0,
187
+ /* sint32 */ 0,
188
+ /* fixed32 */ 5,
189
+ /* sfixed32 */ 5,
190
+ /* int64 */ 0,
191
+ /* uint64 */ 0,
192
+ /* sint64 */ 0,
193
+ /* fixed64 */ 1,
194
+ /* sfixed64 */ 1,
195
+ /* bool */ 0
196
+ ]);
@@ -1,15 +1,15 @@
1
- /**
2
- * Constructor type.
3
- * @interface Constructor
4
- * @extends Function
5
- * @template T
6
- * @tstype new(...params: any[]): T; prototype: T;
7
- */
8
-
9
- /**
10
- * Properties type.
11
- * @typedef Properties
12
- * @template T
13
- * @type {Object.<string,*>}
14
- * @tstype { [P in keyof T]?: T[P] }
15
- */
1
+ /**
2
+ * Constructor type.
3
+ * @interface Constructor
4
+ * @extends Function
5
+ * @template T
6
+ * @tstype new(...params: any[]): T; prototype: T;
7
+ */
8
+
9
+ /**
10
+ * Properties type.
11
+ * @typedef Properties
12
+ * @template T
13
+ * @type {Object.<string,*>}
14
+ * @tstype { [P in keyof T]?: T[P] }
15
+ */
@@ -0,0 +1,13 @@
1
+ export = asPromise;
2
+
3
+ type asPromiseCallback = (error: Error | null, ...params: any[]) => {};
4
+
5
+ /**
6
+ * Returns a promise from a node-style callback function.
7
+ * @memberof util
8
+ * @param {asPromiseCallback} fn Function to call
9
+ * @param {*} ctx Function context
10
+ * @param {...*} params Function arguments
11
+ * @returns {Promise<*>} Promisified function
12
+ */
13
+ declare function asPromise(fn: asPromiseCallback, ctx: any, ...params: any[]): Promise<any>;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ module.exports = asPromise;
3
+
4
+ /**
5
+ * Callback as used by {@link util.asPromise}.
6
+ * @typedef asPromiseCallback
7
+ * @type {function}
8
+ * @param {Error|null} error Error, if any
9
+ * @param {...*} params Additional arguments
10
+ * @returns {undefined}
11
+ */
12
+
13
+ /**
14
+ * Returns a promise from a node-style callback function.
15
+ * @memberof util
16
+ * @param {asPromiseCallback} fn Function to call
17
+ * @param {*} ctx Function context
18
+ * @param {...*} params Function arguments
19
+ * @returns {Promise<*>} Promisified function
20
+ */
21
+ function asPromise(fn, ctx/*, varargs */) {
22
+ var params = new Array(arguments.length - 1),
23
+ offset = 0,
24
+ index = 2,
25
+ pending = true;
26
+ while (index < arguments.length)
27
+ params[offset++] = arguments[index++];
28
+ return new Promise(function executor(resolve, reject) {
29
+ params[offset] = function callback(err/*, varargs */) {
30
+ if (pending) {
31
+ pending = false;
32
+ if (err)
33
+ reject(err);
34
+ else {
35
+ var params = new Array(arguments.length - 1),
36
+ offset = 0;
37
+ while (offset < params.length)
38
+ params[offset++] = arguments[offset];
39
+ resolve.apply(null, params);
40
+ }
41
+ }
42
+ };
43
+ try {
44
+ fn.apply(ctx || null, params);
45
+ } catch (err) {
46
+ if (pending) {
47
+ pending = false;
48
+ reject(err);
49
+ }
50
+ }
51
+ });
52
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Calculates the byte length of a base64 encoded string.
3
+ * @param {string} string Base64 encoded string
4
+ * @returns {number} Byte length
5
+ */
6
+ export function length(string: string): number;
7
+
8
+ /**
9
+ * Encodes a buffer to a base64 encoded string.
10
+ * @param {Uint8Array} buffer Source buffer
11
+ * @param {number} start Source start
12
+ * @param {number} end Source end
13
+ * @returns {string} Base64 encoded string
14
+ */
15
+ export function encode(buffer: Uint8Array, start: number, end: number): string;
16
+
17
+ /**
18
+ * Decodes a base64 encoded string to a buffer.
19
+ * @param {string} string Source string
20
+ * @param {Uint8Array} buffer Destination buffer
21
+ * @param {number} offset Destination offset
22
+ * @returns {number} Number of bytes written
23
+ * @throws {Error} If encoding is invalid
24
+ */
25
+ export function decode(string: string, buffer: Uint8Array, offset: number): number;
26
+
27
+ /**
28
+ * Tests if the specified string appears to be base64 encoded.
29
+ * @param {string} string String to test
30
+ * @returns {boolean} `true` if it appears to be base64 encoded, otherwise false
31
+ */
32
+ export function test(string: string): boolean;
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * A minimal base64 implementation for number arrays.
5
+ * @memberof util
6
+ * @namespace
7
+ */
8
+ var base64 = exports;
9
+
10
+ /**
11
+ * Calculates the byte length of a base64 encoded string.
12
+ * @param {string} string Base64 encoded string
13
+ * @returns {number} Byte length
14
+ */
15
+ base64.length = function length(string) {
16
+ var p = string.length;
17
+ if (!p)
18
+ return 0;
19
+ var n = 0;
20
+ while (--p % 4 > 1 && string.charAt(p) === "=")
21
+ ++n;
22
+ return Math.ceil(string.length * 3) / 4 - n;
23
+ };
24
+
25
+ // Base64 encoding table
26
+ var b64 = new Array(64);
27
+
28
+ // Base64 decoding table
29
+ var s64 = new Array(123);
30
+
31
+ // 65..90, 97..122, 48..57, 43, 47
32
+ for (var i = 0; i < 64;)
33
+ s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
34
+
35
+ /**
36
+ * Encodes a buffer to a base64 encoded string.
37
+ * @param {Uint8Array} buffer Source buffer
38
+ * @param {number} start Source start
39
+ * @param {number} end Source end
40
+ * @returns {string} Base64 encoded string
41
+ */
42
+ base64.encode = function encode(buffer, start, end) {
43
+ var parts = null,
44
+ chunk = [];
45
+ var i = 0, // output index
46
+ j = 0, // goto index
47
+ t; // temporary
48
+ while (start < end) {
49
+ var b = buffer[start++];
50
+ switch (j) {
51
+ case 0:
52
+ chunk[i++] = b64[b >> 2];
53
+ t = (b & 3) << 4;
54
+ j = 1;
55
+ break;
56
+ case 1:
57
+ chunk[i++] = b64[t | b >> 4];
58
+ t = (b & 15) << 2;
59
+ j = 2;
60
+ break;
61
+ case 2:
62
+ chunk[i++] = b64[t | b >> 6];
63
+ chunk[i++] = b64[b & 63];
64
+ j = 0;
65
+ break;
66
+ }
67
+ if (i > 8191) {
68
+ (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
69
+ i = 0;
70
+ }
71
+ }
72
+ if (j) {
73
+ chunk[i++] = b64[t];
74
+ chunk[i++] = 61;
75
+ if (j === 1)
76
+ chunk[i++] = 61;
77
+ }
78
+ if (parts) {
79
+ if (i)
80
+ parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
81
+ return parts.join("");
82
+ }
83
+ return String.fromCharCode.apply(String, chunk.slice(0, i));
84
+ };
85
+
86
+ var invalidEncoding = "invalid encoding";
87
+
88
+ /**
89
+ * Decodes a base64 encoded string to a buffer.
90
+ * @param {string} string Source string
91
+ * @param {Uint8Array} buffer Destination buffer
92
+ * @param {number} offset Destination offset
93
+ * @returns {number} Number of bytes written
94
+ * @throws {Error} If encoding is invalid
95
+ */
96
+ base64.decode = function decode(string, buffer, offset) {
97
+ var start = offset;
98
+ var j = 0, // goto index
99
+ t; // temporary
100
+ for (var i = 0; i < string.length;) {
101
+ var c = string.charCodeAt(i++);
102
+ if (c === 61 && j > 1)
103
+ break;
104
+ if ((c = s64[c]) === undefined)
105
+ throw Error(invalidEncoding);
106
+ switch (j) {
107
+ case 0:
108
+ t = c;
109
+ j = 1;
110
+ break;
111
+ case 1:
112
+ buffer[offset++] = t << 2 | (c & 48) >> 4;
113
+ t = c;
114
+ j = 2;
115
+ break;
116
+ case 2:
117
+ buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
118
+ t = c;
119
+ j = 3;
120
+ break;
121
+ case 3:
122
+ buffer[offset++] = (t & 3) << 6 | c;
123
+ j = 0;
124
+ break;
125
+ }
126
+ }
127
+ if (j === 1)
128
+ throw Error(invalidEncoding);
129
+ return offset - start;
130
+ };
131
+
132
+ /**
133
+ * Tests if the specified string appears to be base64 encoded.
134
+ * @param {string} string String to test
135
+ * @returns {boolean} `true` if probably base64 encoded, otherwise false
136
+ */
137
+ base64.test = function test(string) {
138
+ return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(string);
139
+ };