@yume-chan/adb 0.0.13 → 0.0.16

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 (116) hide show
  1. package/CHANGELOG.json +57 -0
  2. package/CHANGELOG.md +29 -1
  3. package/LICENSE +21 -21
  4. package/README.md +247 -247
  5. package/esm/adb.d.ts +12 -4
  6. package/esm/adb.d.ts.map +1 -1
  7. package/esm/adb.js +29 -19
  8. package/esm/adb.js.map +1 -1
  9. package/esm/commands/reverse.d.ts +9 -12
  10. package/esm/commands/reverse.d.ts.map +1 -1
  11. package/esm/commands/reverse.js +44 -36
  12. package/esm/commands/reverse.js.map +1 -1
  13. package/esm/commands/subprocess/protocols/none.d.ts +2 -1
  14. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  15. package/esm/commands/subprocess/protocols/none.js +10 -9
  16. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  17. package/esm/commands/sync/pull.d.ts.map +1 -1
  18. package/esm/commands/sync/pull.js +6 -3
  19. package/esm/commands/sync/pull.js.map +1 -1
  20. package/esm/commands/sync/stat.d.ts.map +1 -1
  21. package/esm/commands/sync/stat.js +2 -2
  22. package/esm/commands/sync/stat.js.map +1 -1
  23. package/esm/commands/sync/sync.js +1 -1
  24. package/esm/commands/sync/sync.js.map +1 -1
  25. package/esm/socket/dispatcher.d.ts +25 -14
  26. package/esm/socket/dispatcher.d.ts.map +1 -1
  27. package/esm/socket/dispatcher.js +94 -63
  28. package/esm/socket/dispatcher.js.map +1 -1
  29. package/esm/socket/socket.d.ts +18 -4
  30. package/esm/socket/socket.d.ts.map +1 -1
  31. package/esm/socket/socket.js +36 -21
  32. package/esm/socket/socket.js.map +1 -1
  33. package/esm/stream/buffered.d.ts +7 -3
  34. package/esm/stream/buffered.d.ts.map +1 -1
  35. package/esm/stream/buffered.js +69 -49
  36. package/esm/stream/buffered.js.map +1 -1
  37. package/esm/stream/detect.js +1 -0
  38. package/esm/stream/detect.js.map +1 -1
  39. package/esm/stream/detect.polyfill.d.ts +1 -1
  40. package/esm/stream/detect.polyfill.d.ts.map +1 -1
  41. package/esm/stream/detect.polyfill.js +0 -5
  42. package/esm/stream/detect.polyfill.js.map +1 -1
  43. package/esm/stream/transform.d.ts +39 -13
  44. package/esm/stream/transform.d.ts.map +1 -1
  45. package/esm/stream/transform.js +67 -90
  46. package/esm/stream/transform.js.map +1 -1
  47. package/esm/utils/base64.js +5 -5
  48. package/esm/utils/base64.js.map +1 -1
  49. package/package.json +10 -10
  50. package/src/adb.ts +280 -270
  51. package/src/auth.ts +173 -173
  52. package/src/backend.ts +11 -11
  53. package/src/commands/base.ts +11 -11
  54. package/src/commands/framebuffer.ts +73 -73
  55. package/src/commands/index.ts +8 -8
  56. package/src/commands/install.ts +30 -30
  57. package/src/commands/power.ts +54 -54
  58. package/src/commands/reverse.ts +140 -136
  59. package/src/commands/subprocess/index.ts +139 -139
  60. package/src/commands/subprocess/protocols/none.ts +70 -68
  61. package/src/commands/subprocess/protocols/shell.ts +190 -190
  62. package/src/commands/subprocess/protocols/types.ts +59 -59
  63. package/src/commands/subprocess/utils.ts +20 -20
  64. package/src/commands/sync/index.ts +7 -7
  65. package/src/commands/sync/list.ts +86 -86
  66. package/src/commands/sync/pull.ts +46 -43
  67. package/src/commands/sync/push.ts +41 -41
  68. package/src/commands/sync/request.ts +50 -50
  69. package/src/commands/sync/response.ts +64 -64
  70. package/src/commands/sync/stat.ts +151 -150
  71. package/src/commands/sync/sync.ts +181 -181
  72. package/src/commands/tcpip.ts +21 -21
  73. package/src/crypto.ts +296 -296
  74. package/src/features.ts +9 -9
  75. package/src/index.ts +11 -11
  76. package/src/packet.ts +80 -80
  77. package/src/socket/dispatcher.ts +305 -270
  78. package/src/socket/index.ts +2 -2
  79. package/src/socket/socket.ts +155 -136
  80. package/src/stream/buffered.ts +168 -147
  81. package/src/stream/detect.native.ts +362 -362
  82. package/src/stream/detect.polyfill.ts +22 -28
  83. package/src/stream/detect.ts +7 -5
  84. package/src/stream/index.ts +3 -3
  85. package/src/stream/transform.ts +474 -483
  86. package/src/utils/auto-reset-event.ts +41 -41
  87. package/src/utils/base64.ts +306 -306
  88. package/src/utils/index.ts +3 -3
  89. package/tsconfig.build.json +3 -3
  90. package/tsconfig.build.tsbuildinfo +1 -1
  91. package/tsconfig.test.json +9 -10
  92. package/web-streams-polyfill-4.0.0-beta.2.tgz +0 -0
  93. package/esm/commands/subprocess/legacy.d.ts +0 -35
  94. package/esm/commands/subprocess/legacy.d.ts.map +0 -1
  95. package/esm/commands/subprocess/legacy.js +0 -53
  96. package/esm/commands/subprocess/legacy.js.map +0 -1
  97. package/esm/commands/subprocess/protocol.d.ts +0 -39
  98. package/esm/commands/subprocess/protocol.d.ts.map +0 -1
  99. package/esm/commands/subprocess/protocol.js +0 -157
  100. package/esm/commands/subprocess/protocol.js.map +0 -1
  101. package/esm/commands/subprocess/types.d.ts +0 -50
  102. package/esm/commands/subprocess/types.d.ts.map +0 -1
  103. package/esm/commands/subprocess/types.js +0 -2
  104. package/esm/commands/subprocess/types.js.map +0 -1
  105. package/esm/socket/controller.d.ts +0 -37
  106. package/esm/socket/controller.d.ts.map +0 -1
  107. package/esm/socket/controller.js +0 -66
  108. package/esm/socket/controller.js.map +0 -1
  109. package/esm/stream/detect.bak.d.ts +0 -240
  110. package/esm/stream/detect.bak.d.ts.map +0 -1
  111. package/esm/stream/detect.bak.js +0 -60
  112. package/esm/stream/detect.bak.js.map +0 -1
  113. package/esm/utils/encoding.d.ts +0 -3
  114. package/esm/utils/encoding.d.ts.map +0 -1
  115. package/esm/utils/encoding.js +0 -11
  116. package/esm/utils/encoding.js.map +0 -1
@@ -1,306 +1,306 @@
1
- // Prepare maps for O(1) searching
2
- const charToIndex: Record<string, number> = {};
3
- const indexToChar: number[] = [];
4
- const paddingChar = '='.charCodeAt(0);
5
-
6
- function addRange(start: string, end: string) {
7
- const charCodeStart = start.charCodeAt(0);
8
- const charCodeEnd = end.charCodeAt(0);
9
-
10
- for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
11
- charToIndex[String.fromCharCode(charCode)] = indexToChar.length;
12
- indexToChar.push(charCode);
13
- }
14
- }
15
-
16
- addRange('A', 'Z');
17
- addRange('a', 'z');
18
- addRange('0', '9');
19
- addRange('+', '+');
20
- addRange('/', '/');
21
-
22
- export function calculateBase64EncodedLength(inputLength: number): [outputLength: number, paddingLength: number] {
23
- const remainder = inputLength % 3;
24
- const paddingLength = remainder !== 0 ? 3 - remainder : 0;
25
- return [(inputLength + paddingLength) / 3 * 4, paddingLength];
26
- }
27
-
28
- export function encodeBase64(
29
- input: Uint8Array,
30
- ): Uint8Array;
31
- export function encodeBase64(
32
- input: Uint8Array,
33
- output: Uint8Array,
34
- ): number;
35
- export function encodeBase64(
36
- input: Uint8Array,
37
- output?: Uint8Array,
38
- ): Uint8Array | number {
39
- const [outputLength, paddingLength] = calculateBase64EncodedLength(input.length);
40
-
41
- if (!output) {
42
- output = new Uint8Array(outputLength);
43
- encodeForward(input, output, paddingLength);
44
- return output;
45
- } else {
46
- if (output.length < outputLength) {
47
- throw new Error('output buffer is too small');
48
- }
49
-
50
- output = output.subarray(0, outputLength);
51
-
52
- // When input and output are on same ArrayBuffer,
53
- // we check if it's possible to encode in-place.
54
- if (input.buffer !== output.buffer) {
55
- encodeForward(input, output, paddingLength);
56
- } else if (output.byteOffset + output.length - (paddingLength + 1) <= input.byteOffset + input.length) {
57
- // Output ends before input ends
58
- // So output won't catch up with input.
59
-
60
- // Depends on padding length,
61
- // it's possible to write 1-3 bytes after input ends.
62
- // spell: disable-next-line
63
- // | aaaaaabb | | | |
64
- // | aaaaaa | bb0000 | = | = |
65
- //
66
- // spell: disable-next-line
67
- // | aaaaaabb | bbbbcccc | | |
68
- // | aaaaaa | bbbbbb | cccc00 | = |
69
- //
70
- // spell: disable-next-line
71
- // | aaaaaabb | bbbbcccc | ccdddddd | |
72
- // | aaaaaa | bbbbbb | cccccc | dddddd |
73
-
74
- // Must encode forwards.
75
- encodeForward(input, output, paddingLength);
76
- } else if (output.byteOffset >= input.byteOffset - 1) {
77
- // Output starts after input starts
78
- // So in backwards, output can't catch up with input.
79
-
80
- // Because first 3 bytes becomes 4 bytes,
81
- // it's possible to write 1 byte before input starts.
82
- // spell: disable-next-line
83
- // | | aaaaaabb | bbbbcccc | ccdddddd |
84
- // | aaaaaa | bbbbbb | cccccc | dddddd |
85
-
86
- // Must encode backwards.
87
- encodeBackward(input, output, paddingLength);
88
- } else {
89
- // Input is in the middle of output,
90
- // not possible to read neither first or last three bytes,
91
- throw new Error('input and output cannot overlap');
92
- }
93
-
94
- return outputLength;
95
- }
96
- }
97
-
98
- function encodeForward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
99
- let inputIndex = 0;
100
- let outputIndex = 0;
101
-
102
- while (inputIndex < input.length - 2) {
103
- /* cspell: disable-next-line */
104
- // aaaaaabb
105
- const x = input[inputIndex]!;
106
- inputIndex += 1;
107
-
108
- /* cspell: disable-next-line */
109
- // bbbbcccc
110
- const y = input[inputIndex]!;
111
- inputIndex += 1;
112
-
113
- /* cspell: disable-next-line */
114
- // ccdddddd
115
- const z = input[inputIndex]!;
116
- inputIndex += 1;
117
-
118
- output[outputIndex] = indexToChar[x >> 2]!;
119
- outputIndex += 1;
120
-
121
- output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
122
- outputIndex += 1;
123
-
124
- output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)]!;
125
- outputIndex += 1;
126
-
127
- output[outputIndex] = indexToChar[z & 0b111111]!;
128
- outputIndex += 1;
129
- }
130
-
131
- if (paddingLength === 2) {
132
- /* cspell: disable-next-line */
133
- // aaaaaabb
134
- const x = input[inputIndex]!;
135
- inputIndex += 1;
136
-
137
- output[outputIndex] = indexToChar[x >> 2]!;
138
- outputIndex += 1;
139
-
140
- output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
141
- outputIndex += 1;
142
-
143
- output[outputIndex] = paddingChar;
144
- outputIndex += 1;
145
-
146
- output[outputIndex] = paddingChar;
147
- } else if (paddingLength === 1) {
148
- /* cspell: disable-next-line */
149
- // aaaaaabb
150
- const x = input[inputIndex]!;
151
- inputIndex += 1;
152
-
153
- /* cspell: disable-next-line */
154
- // bbbbcccc
155
- const y = input[inputIndex]!;
156
- inputIndex += 1;
157
-
158
- output[outputIndex] = indexToChar[x >> 2]!;
159
- outputIndex += 1;
160
-
161
- output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
162
- outputIndex += 1;
163
-
164
- output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
165
- outputIndex += 1;
166
-
167
- output[outputIndex] = paddingChar;
168
- }
169
- }
170
-
171
- function encodeBackward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
172
- let inputIndex = input.length - 1;
173
- let outputIndex = output.length - 1;
174
-
175
- if (paddingLength === 2) {
176
- /* cspell: disable-next-line */
177
- // aaaaaabb
178
- const x = input[inputIndex]!;
179
- inputIndex -= 1;
180
-
181
- output[outputIndex] = paddingChar;
182
- outputIndex -= 1;
183
-
184
- output[outputIndex] = paddingChar;
185
- outputIndex -= 1;
186
-
187
- output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
188
- outputIndex -= 1;
189
-
190
- output[outputIndex] = indexToChar[x >> 2]!;
191
- outputIndex -= 1;
192
- } else if (paddingLength === 1) {
193
- /* cspell: disable-next-line */
194
- // bbbbcccc
195
- const y = input[inputIndex]!;
196
- inputIndex -= 1;
197
-
198
- /* cspell: disable-next-line */
199
- // aaaaaabb
200
- const x = input[inputIndex]!;
201
- inputIndex -= 1;
202
-
203
- output[outputIndex] = paddingChar;
204
- outputIndex -= 1;
205
-
206
- output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
207
- outputIndex -= 1;
208
-
209
- output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
210
- outputIndex -= 1;
211
-
212
- output[outputIndex] = indexToChar[x >> 2]!;
213
- outputIndex -= 1;
214
- }
215
-
216
- while (inputIndex >= 0) {
217
- /* cspell: disable-next-line */
218
- // ccdddddd
219
- const z = input[inputIndex]!;
220
- inputIndex -= 1;
221
-
222
- /* cspell: disable-next-line */
223
- // bbbbcccc
224
- const y = input[inputIndex]!;
225
- inputIndex -= 1;
226
-
227
- /* cspell: disable-next-line */
228
- // aaaaaabb
229
- const x = input[inputIndex]!;
230
- inputIndex -= 1;
231
-
232
- output[outputIndex] = indexToChar[z & 0b111111]!;
233
- outputIndex -= 1;
234
-
235
- output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)]!;
236
- outputIndex -= 1;
237
-
238
- output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
239
- outputIndex -= 1;
240
-
241
- output[outputIndex] = indexToChar[x >> 2]!;
242
- outputIndex -= 1;
243
- }
244
- }
245
-
246
- export function decodeBase64(input: string): Uint8Array {
247
- let padding: number;
248
- if (input[input.length - 2] === '=') {
249
- padding = 2;
250
- } else if (input[input.length - 1] === '=') {
251
- padding = 1;
252
- } else {
253
- padding = 0;
254
- }
255
-
256
- const result = new Uint8Array(input.length / 4 * 3 - padding);
257
- let sIndex = 0;
258
- let dIndex = 0;
259
-
260
- while (sIndex < input.length - (padding !== 0 ? 4 : 0)) {
261
- const a = charToIndex[input[sIndex]!]!;
262
- sIndex += 1;
263
-
264
- const b = charToIndex[input[sIndex]!]!;
265
- sIndex += 1;
266
-
267
- const c = charToIndex[input[sIndex]!]!;
268
- sIndex += 1;
269
-
270
- const d = charToIndex[input[sIndex]!]!;
271
- sIndex += 1;
272
-
273
- result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
274
- dIndex += 1;
275
-
276
- result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
277
- dIndex += 1;
278
-
279
- result[dIndex] = ((c & 0b11) << 6) | d;
280
- dIndex += 1;
281
- }
282
-
283
- if (padding === 1) {
284
- const a = charToIndex[input[sIndex]!]!;
285
- sIndex += 1;
286
-
287
- const b = charToIndex[input[sIndex]!]!;
288
- sIndex += 1;
289
-
290
- const c = charToIndex[input[sIndex]!]!;
291
-
292
- result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
293
- dIndex += 1;
294
-
295
- result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
296
- } else if (padding === 2) {
297
- const a = charToIndex[input[sIndex]!]!;
298
- sIndex += 1;
299
-
300
- const b = charToIndex[input[sIndex]!]!;
301
-
302
- result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
303
- }
304
-
305
- return result;
306
- }
1
+ // Prepare maps for O(1) searching
2
+ const charToIndex: Record<string, number> = {};
3
+ const indexToChar: number[] = [];
4
+ const paddingChar = '='.charCodeAt(0);
5
+
6
+ function addRange(start: string, end: string) {
7
+ const charCodeStart = start.charCodeAt(0);
8
+ const charCodeEnd = end.charCodeAt(0);
9
+
10
+ for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
11
+ charToIndex[String.fromCharCode(charCode)] = indexToChar.length;
12
+ indexToChar.push(charCode);
13
+ }
14
+ }
15
+
16
+ addRange('A', 'Z');
17
+ addRange('a', 'z');
18
+ addRange('0', '9');
19
+ addRange('+', '+');
20
+ addRange('/', '/');
21
+
22
+ export function calculateBase64EncodedLength(inputLength: number): [outputLength: number, paddingLength: number] {
23
+ const remainder = inputLength % 3;
24
+ const paddingLength = remainder !== 0 ? 3 - remainder : 0;
25
+ return [(inputLength + paddingLength) / 3 * 4, paddingLength];
26
+ }
27
+
28
+ export function encodeBase64(
29
+ input: Uint8Array,
30
+ ): Uint8Array;
31
+ export function encodeBase64(
32
+ input: Uint8Array,
33
+ output: Uint8Array,
34
+ ): number;
35
+ export function encodeBase64(
36
+ input: Uint8Array,
37
+ output?: Uint8Array,
38
+ ): Uint8Array | number {
39
+ const [outputLength, paddingLength] = calculateBase64EncodedLength(input.length);
40
+
41
+ if (!output) {
42
+ output = new Uint8Array(outputLength);
43
+ encodeForward(input, output, paddingLength);
44
+ return output;
45
+ } else {
46
+ if (output.length < outputLength) {
47
+ throw new Error('output buffer is too small');
48
+ }
49
+
50
+ output = output.subarray(0, outputLength);
51
+
52
+ // When input and output are on same ArrayBuffer,
53
+ // we check if it's possible to encode in-place.
54
+ if (input.buffer !== output.buffer) {
55
+ encodeForward(input, output, paddingLength);
56
+ } else if (output.byteOffset + output.length - (paddingLength + 1) <= input.byteOffset + input.length) {
57
+ // Output ends before input ends
58
+ // So output won't catch up with input.
59
+
60
+ // Depends on padding length,
61
+ // it's possible to write 1-3 bytes after input ends.
62
+ // spell: disable-next-line
63
+ // | aaaaaabb | | | |
64
+ // | aaaaaa | bb0000 | = | = |
65
+ //
66
+ // spell: disable-next-line
67
+ // | aaaaaabb | bbbbcccc | | |
68
+ // | aaaaaa | bbbbbb | cccc00 | = |
69
+ //
70
+ // spell: disable-next-line
71
+ // | aaaaaabb | bbbbcccc | ccdddddd | |
72
+ // | aaaaaa | bbbbbb | cccccc | dddddd |
73
+
74
+ // Must encode forwards.
75
+ encodeForward(input, output, paddingLength);
76
+ } else if (output.byteOffset >= input.byteOffset - 1) {
77
+ // Output starts after input starts
78
+ // So in backwards, output can't catch up with input.
79
+
80
+ // Because first 3 bytes becomes 4 bytes,
81
+ // it's possible to write 1 byte before input starts.
82
+ // spell: disable-next-line
83
+ // | | aaaaaabb | bbbbcccc | ccdddddd |
84
+ // | aaaaaa | bbbbbb | cccccc | dddddd |
85
+
86
+ // Must encode backwards.
87
+ encodeBackward(input, output, paddingLength);
88
+ } else {
89
+ // Input is in the middle of output,
90
+ // not possible to read neither first or last three bytes,
91
+ throw new Error('input and output cannot overlap');
92
+ }
93
+
94
+ return outputLength;
95
+ }
96
+ }
97
+
98
+ function encodeForward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
99
+ let inputIndex = 0;
100
+ let outputIndex = 0;
101
+
102
+ while (inputIndex < input.length - 2) {
103
+ /* cspell: disable-next-line */
104
+ // aaaaaabb
105
+ const x = input[inputIndex]!;
106
+ inputIndex += 1;
107
+
108
+ /* cspell: disable-next-line */
109
+ // bbbbcccc
110
+ const y = input[inputIndex]!;
111
+ inputIndex += 1;
112
+
113
+ /* cspell: disable-next-line */
114
+ // ccdddddd
115
+ const z = input[inputIndex]!;
116
+ inputIndex += 1;
117
+
118
+ output[outputIndex] = indexToChar[x >> 2]!;
119
+ outputIndex += 1;
120
+
121
+ output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
122
+ outputIndex += 1;
123
+
124
+ output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)]!;
125
+ outputIndex += 1;
126
+
127
+ output[outputIndex] = indexToChar[z & 0b111111]!;
128
+ outputIndex += 1;
129
+ }
130
+
131
+ if (paddingLength === 2) {
132
+ /* cspell: disable-next-line */
133
+ // aaaaaabb
134
+ const x = input[inputIndex]!;
135
+ inputIndex += 1;
136
+
137
+ output[outputIndex] = indexToChar[x >> 2]!;
138
+ outputIndex += 1;
139
+
140
+ output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
141
+ outputIndex += 1;
142
+
143
+ output[outputIndex] = paddingChar;
144
+ outputIndex += 1;
145
+
146
+ output[outputIndex] = paddingChar;
147
+ } else if (paddingLength === 1) {
148
+ /* cspell: disable-next-line */
149
+ // aaaaaabb
150
+ const x = input[inputIndex]!;
151
+ inputIndex += 1;
152
+
153
+ /* cspell: disable-next-line */
154
+ // bbbbcccc
155
+ const y = input[inputIndex]!;
156
+ inputIndex += 1;
157
+
158
+ output[outputIndex] = indexToChar[x >> 2]!;
159
+ outputIndex += 1;
160
+
161
+ output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
162
+ outputIndex += 1;
163
+
164
+ output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
165
+ outputIndex += 1;
166
+
167
+ output[outputIndex] = paddingChar;
168
+ }
169
+ }
170
+
171
+ function encodeBackward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
172
+ let inputIndex = input.length - 1;
173
+ let outputIndex = output.length - 1;
174
+
175
+ if (paddingLength === 2) {
176
+ /* cspell: disable-next-line */
177
+ // aaaaaabb
178
+ const x = input[inputIndex]!;
179
+ inputIndex -= 1;
180
+
181
+ output[outputIndex] = paddingChar;
182
+ outputIndex -= 1;
183
+
184
+ output[outputIndex] = paddingChar;
185
+ outputIndex -= 1;
186
+
187
+ output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
188
+ outputIndex -= 1;
189
+
190
+ output[outputIndex] = indexToChar[x >> 2]!;
191
+ outputIndex -= 1;
192
+ } else if (paddingLength === 1) {
193
+ /* cspell: disable-next-line */
194
+ // bbbbcccc
195
+ const y = input[inputIndex]!;
196
+ inputIndex -= 1;
197
+
198
+ /* cspell: disable-next-line */
199
+ // aaaaaabb
200
+ const x = input[inputIndex]!;
201
+ inputIndex -= 1;
202
+
203
+ output[outputIndex] = paddingChar;
204
+ outputIndex -= 1;
205
+
206
+ output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
207
+ outputIndex -= 1;
208
+
209
+ output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
210
+ outputIndex -= 1;
211
+
212
+ output[outputIndex] = indexToChar[x >> 2]!;
213
+ outputIndex -= 1;
214
+ }
215
+
216
+ while (inputIndex >= 0) {
217
+ /* cspell: disable-next-line */
218
+ // ccdddddd
219
+ const z = input[inputIndex]!;
220
+ inputIndex -= 1;
221
+
222
+ /* cspell: disable-next-line */
223
+ // bbbbcccc
224
+ const y = input[inputIndex]!;
225
+ inputIndex -= 1;
226
+
227
+ /* cspell: disable-next-line */
228
+ // aaaaaabb
229
+ const x = input[inputIndex]!;
230
+ inputIndex -= 1;
231
+
232
+ output[outputIndex] = indexToChar[z & 0b111111]!;
233
+ outputIndex -= 1;
234
+
235
+ output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)]!;
236
+ outputIndex -= 1;
237
+
238
+ output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
239
+ outputIndex -= 1;
240
+
241
+ output[outputIndex] = indexToChar[x >> 2]!;
242
+ outputIndex -= 1;
243
+ }
244
+ }
245
+
246
+ export function decodeBase64(input: string): Uint8Array {
247
+ let padding: number;
248
+ if (input[input.length - 2] === '=') {
249
+ padding = 2;
250
+ } else if (input[input.length - 1] === '=') {
251
+ padding = 1;
252
+ } else {
253
+ padding = 0;
254
+ }
255
+
256
+ const result = new Uint8Array(input.length / 4 * 3 - padding);
257
+ let sIndex = 0;
258
+ let dIndex = 0;
259
+
260
+ while (sIndex < input.length - (padding !== 0 ? 4 : 0)) {
261
+ const a = charToIndex[input[sIndex]!]!;
262
+ sIndex += 1;
263
+
264
+ const b = charToIndex[input[sIndex]!]!;
265
+ sIndex += 1;
266
+
267
+ const c = charToIndex[input[sIndex]!]!;
268
+ sIndex += 1;
269
+
270
+ const d = charToIndex[input[sIndex]!]!;
271
+ sIndex += 1;
272
+
273
+ result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
274
+ dIndex += 1;
275
+
276
+ result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
277
+ dIndex += 1;
278
+
279
+ result[dIndex] = ((c & 0b11) << 6) | d;
280
+ dIndex += 1;
281
+ }
282
+
283
+ if (padding === 1) {
284
+ const a = charToIndex[input[sIndex]!]!;
285
+ sIndex += 1;
286
+
287
+ const b = charToIndex[input[sIndex]!]!;
288
+ sIndex += 1;
289
+
290
+ const c = charToIndex[input[sIndex]!]!;
291
+
292
+ result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
293
+ dIndex += 1;
294
+
295
+ result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
296
+ } else if (padding === 2) {
297
+ const a = charToIndex[input[sIndex]!]!;
298
+ sIndex += 1;
299
+
300
+ const b = charToIndex[input[sIndex]!]!;
301
+
302
+ result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
303
+ }
304
+
305
+ return result;
306
+ }
@@ -1,3 +1,3 @@
1
- export { decodeUtf8, encodeUtf8 } from '@yume-chan/struct';
2
- export * from './auto-reset-event.js';
3
- export * from './base64.js';
1
+ export { decodeUtf8, encodeUtf8 } from '@yume-chan/struct';
2
+ export * from './auto-reset-event.js';
3
+ export * from './base64.js';
@@ -1,3 +1,3 @@
1
- {
2
- "extends": "./node_modules/@yume-chan/ts-package-builder/tsconfig.base.json"
3
- }
1
+ {
2
+ "extends": "./node_modules/@yume-chan/ts-package-builder/tsconfig.base.json"
3
+ }