@yume-chan/adb 0.0.18 → 0.0.20
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.
- package/CHANGELOG.json +54 -0
- package/CHANGELOG.md +25 -1
- package/README.md +220 -103
- package/esm/adb.d.ts +43 -60
- package/esm/adb.d.ts.map +1 -1
- package/esm/adb.js +68 -249
- package/esm/adb.js.map +1 -1
- package/esm/banner.d.ts +17 -0
- package/esm/banner.d.ts.map +1 -0
- package/esm/banner.js +67 -0
- package/esm/banner.js.map +1 -0
- package/esm/commands/base.d.ts +6 -6
- package/esm/commands/base.d.ts.map +1 -1
- package/esm/commands/base.js +8 -8
- package/esm/commands/framebuffer.d.ts +53 -53
- package/esm/commands/framebuffer.d.ts.map +1 -1
- package/esm/commands/framebuffer.js +46 -46
- package/esm/commands/framebuffer.js.map +1 -1
- package/esm/commands/index.d.ts +7 -8
- package/esm/commands/index.d.ts.map +1 -1
- package/esm/commands/index.js +7 -8
- package/esm/commands/index.js.map +1 -1
- package/esm/commands/power.d.ts +22 -22
- package/esm/commands/power.js +48 -48
- package/esm/commands/reverse.d.ts +40 -28
- package/esm/commands/reverse.d.ts.map +1 -1
- package/esm/commands/reverse.js +130 -100
- package/esm/commands/reverse.js.map +1 -1
- package/esm/commands/subprocess/command.d.ts +57 -51
- package/esm/commands/subprocess/command.d.ts.map +1 -1
- package/esm/commands/subprocess/command.js +86 -80
- package/esm/commands/subprocess/command.js.map +1 -1
- package/esm/commands/subprocess/index.d.ts +3 -3
- package/esm/commands/subprocess/index.js +3 -3
- package/esm/commands/subprocess/protocols/index.d.ts +3 -3
- package/esm/commands/subprocess/protocols/index.js +3 -3
- package/esm/commands/subprocess/protocols/none.d.ts +30 -35
- package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/none.js +65 -55
- package/esm/commands/subprocess/protocols/none.js.map +1 -1
- package/esm/commands/subprocess/protocols/shell.d.ts +33 -38
- package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/shell.js +172 -162
- package/esm/commands/subprocess/protocols/shell.js.map +1 -1
- package/esm/commands/subprocess/protocols/types.d.ts +48 -49
- package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/types.js +1 -1
- package/esm/commands/subprocess/utils.d.ts +1 -1
- package/esm/commands/subprocess/utils.js +18 -18
- package/esm/commands/sync/index.d.ts +8 -7
- package/esm/commands/sync/index.d.ts.map +1 -1
- package/esm/commands/sync/index.js +8 -7
- package/esm/commands/sync/index.js.map +1 -1
- package/esm/commands/sync/list.d.ts +42 -40
- package/esm/commands/sync/list.d.ts.map +1 -1
- package/esm/commands/sync/list.js +67 -47
- package/esm/commands/sync/list.js.map +1 -1
- package/esm/commands/sync/pull.d.ts +13 -11
- package/esm/commands/sync/pull.d.ts.map +1 -1
- package/esm/commands/sync/pull.js +35 -37
- package/esm/commands/sync/pull.js.map +1 -1
- package/esm/commands/sync/push.d.ts +57 -7
- package/esm/commands/sync/push.d.ts.map +1 -1
- package/esm/commands/sync/push.js +89 -22
- package/esm/commands/sync/push.js.map +1 -1
- package/esm/commands/sync/request.d.ts +26 -23
- package/esm/commands/sync/request.d.ts.map +1 -1
- package/esm/commands/sync/request.js +40 -42
- package/esm/commands/sync/request.js.map +1 -1
- package/esm/commands/sync/response.d.ts +19 -19
- package/esm/commands/sync/response.d.ts.map +1 -1
- package/esm/commands/sync/response.js +54 -54
- package/esm/commands/sync/response.js.map +1 -1
- package/esm/commands/sync/socket.d.ts +22 -0
- package/esm/commands/sync/socket.d.ts.map +1 -0
- package/esm/commands/sync/socket.js +69 -0
- package/esm/commands/sync/socket.js.map +1 -0
- package/esm/commands/sync/stat.d.ts +72 -72
- package/esm/commands/sync/stat.d.ts.map +1 -1
- package/esm/commands/sync/stat.js +116 -104
- package/esm/commands/sync/stat.js.map +1 -1
- package/esm/commands/sync/sync.d.ts +65 -60
- package/esm/commands/sync/sync.d.ts.map +1 -1
- package/esm/commands/sync/sync.js +123 -149
- package/esm/commands/sync/sync.js.map +1 -1
- package/esm/commands/tcpip.d.ts +5 -5
- package/esm/commands/tcpip.d.ts.map +1 -1
- package/esm/commands/tcpip.js +20 -18
- package/esm/commands/tcpip.js.map +1 -1
- package/esm/{auth.d.ts → daemon/auth.d.ts} +50 -51
- package/esm/daemon/auth.d.ts.map +1 -0
- package/esm/{auth.js → daemon/auth.js} +95 -96
- package/esm/daemon/auth.js.map +1 -0
- package/esm/daemon/connection.d.ts +11 -0
- package/esm/daemon/connection.d.ts.map +1 -0
- package/esm/daemon/connection.js +2 -0
- package/esm/daemon/connection.js.map +1 -0
- package/esm/{crypto.d.ts → daemon/crypto.d.ts} +36 -36
- package/esm/daemon/crypto.d.ts.map +1 -0
- package/esm/{crypto.js → daemon/crypto.js} +244 -248
- package/esm/daemon/crypto.js.map +1 -0
- package/esm/daemon/device.d.ts +9 -0
- package/esm/daemon/device.d.ts.map +1 -0
- package/esm/daemon/device.js +2 -0
- package/esm/daemon/device.js.map +1 -0
- package/esm/daemon/dispatcher.d.ts +42 -0
- package/esm/daemon/dispatcher.d.ts.map +1 -0
- package/esm/{socket → daemon}/dispatcher.js +225 -234
- package/esm/daemon/dispatcher.js.map +1 -0
- package/esm/daemon/index.d.ts +8 -0
- package/esm/daemon/index.d.ts.map +1 -0
- package/esm/daemon/index.js +8 -0
- package/esm/daemon/index.js.map +1 -0
- package/esm/{packet.d.ts → daemon/packet.d.ts} +45 -46
- package/esm/daemon/packet.d.ts.map +1 -0
- package/esm/{packet.js → daemon/packet.js} +43 -48
- package/esm/daemon/packet.js.map +1 -0
- package/esm/daemon/socket.d.ts +58 -0
- package/esm/daemon/socket.d.ts.map +1 -0
- package/esm/{socket → daemon}/socket.js +121 -118
- package/esm/daemon/socket.js.map +1 -0
- package/esm/daemon/transport.d.ts +45 -0
- package/esm/daemon/transport.d.ts.map +1 -0
- package/esm/daemon/transport.js +184 -0
- package/esm/daemon/transport.js.map +1 -0
- package/esm/features.d.ts +10 -7
- package/esm/features.d.ts.map +1 -1
- package/esm/features.js +13 -10
- package/esm/features.js.map +1 -1
- package/esm/index.d.ts +7 -9
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +7 -9
- package/esm/index.js.map +1 -1
- package/esm/server/client.d.ts +78 -0
- package/esm/server/client.d.ts.map +1 -0
- package/esm/server/client.js +329 -0
- package/esm/server/client.js.map +1 -0
- package/esm/server/index.d.ts +3 -0
- package/esm/server/index.d.ts.map +1 -0
- package/esm/server/index.js +3 -0
- package/esm/server/index.js.map +1 -0
- package/esm/server/transport.d.ts +19 -0
- package/esm/server/transport.d.ts.map +1 -0
- package/esm/server/transport.js +44 -0
- package/esm/server/transport.js.map +1 -0
- package/esm/utils/auto-reset-event.d.ts +8 -9
- package/esm/utils/auto-reset-event.d.ts.map +1 -1
- package/esm/utils/auto-reset-event.js +33 -33
- package/esm/utils/auto-reset-event.js.map +1 -1
- package/esm/utils/base64.d.ts +27 -27
- package/esm/utils/base64.js +246 -246
- package/esm/utils/base64.js.map +1 -1
- package/esm/utils/conditional-variable.d.ts +8 -0
- package/esm/utils/conditional-variable.d.ts.map +1 -0
- package/esm/utils/conditional-variable.js +34 -0
- package/esm/utils/conditional-variable.js.map +1 -0
- package/esm/utils/hex.d.ts +3 -0
- package/esm/utils/hex.d.ts.map +1 -0
- package/esm/utils/hex.js +55 -0
- package/esm/utils/hex.js.map +1 -0
- package/esm/utils/index.d.ts +6 -3
- package/esm/utils/index.d.ts.map +1 -1
- package/esm/utils/index.js +6 -3
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/no-op.d.ts +7 -0
- package/esm/utils/no-op.d.ts.map +1 -0
- package/esm/utils/no-op.js +12 -0
- package/esm/utils/no-op.js.map +1 -0
- package/package.json +14 -12
- package/src/adb.ts +47 -248
- package/src/banner.ts +82 -0
- package/src/commands/base.ts +1 -1
- package/src/commands/framebuffer.ts +4 -4
- package/src/commands/index.ts +7 -8
- package/src/commands/power.ts +2 -2
- package/src/commands/reverse.ts +90 -70
- package/src/commands/subprocess/command.ts +12 -4
- package/src/commands/subprocess/index.ts +3 -3
- package/src/commands/subprocess/protocols/index.ts +3 -3
- package/src/commands/subprocess/protocols/none.ts +36 -23
- package/src/commands/subprocess/protocols/shell.ts +94 -74
- package/src/commands/subprocess/protocols/types.ts +7 -7
- package/src/commands/subprocess/utils.ts +1 -1
- package/src/commands/sync/index.ts +8 -7
- package/src/commands/sync/list.ts +44 -25
- package/src/commands/sync/pull.ts +40 -45
- package/src/commands/sync/push.ts +158 -39
- package/src/commands/sync/request.ts +21 -16
- package/src/commands/sync/response.ts +10 -9
- package/src/commands/sync/socket.ts +99 -0
- package/src/commands/sync/stat.ts +47 -42
- package/src/commands/sync/sync.ts +73 -116
- package/src/commands/tcpip.ts +9 -7
- package/src/{auth.ts → daemon/auth.ts} +24 -23
- package/src/{crypto.ts → daemon/crypto.ts} +25 -33
- package/src/daemon/device.ts +14 -0
- package/src/{socket → daemon}/dispatcher.ts +95 -117
- package/src/daemon/index.ts +7 -0
- package/src/daemon/packet.ts +75 -0
- package/src/{socket → daemon}/socket.ts +71 -56
- package/src/daemon/transport.ts +269 -0
- package/src/features.ts +9 -6
- package/src/index.ts +3 -5
- package/src/server/client.ts +476 -0
- package/src/server/index.ts +2 -0
- package/src/server/transport.ts +77 -0
- package/src/utils/auto-reset-event.ts +14 -15
- package/src/utils/conditional-variable.ts +45 -0
- package/src/utils/hex.ts +58 -0
- package/src/utils/index.ts +6 -3
- package/src/utils/no-op.ts +12 -0
- package/tsconfig.build.json +12 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/esm/auth.d.ts.map +0 -1
- package/esm/auth.js.map +0 -1
- package/esm/backend.d.ts +0 -9
- package/esm/backend.d.ts.map +0 -1
- package/esm/backend.js +0 -2
- package/esm/backend.js.map +0 -1
- package/esm/commands/install.d.ts +0 -4
- package/esm/commands/install.d.ts.map +0 -1
- package/esm/commands/install.js +0 -29
- package/esm/commands/install.js.map +0 -1
- package/esm/crypto.d.ts.map +0 -1
- package/esm/crypto.js.map +0 -1
- package/esm/packet.d.ts.map +0 -1
- package/esm/packet.js.map +0 -1
- package/esm/socket/dispatcher.d.ts +0 -60
- package/esm/socket/dispatcher.d.ts.map +0 -1
- package/esm/socket/dispatcher.js.map +0 -1
- package/esm/socket/index.d.ts +0 -3
- package/esm/socket/index.d.ts.map +0 -1
- package/esm/socket/index.js +0 -3
- package/esm/socket/index.js.map +0 -1
- package/esm/socket/socket.d.ts +0 -60
- package/esm/socket/socket.d.ts.map +0 -1
- package/esm/socket/socket.js.map +0 -1
- package/src/backend.ts +0 -14
- package/src/commands/install.ts +0 -40
- package/src/packet.ts +0 -90
- package/src/socket/index.ts +0 -2
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class AutoResetEvent implements Disposable {
|
|
3
|
-
private
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
1
|
+
import type { Disposable } from "@yume-chan/event";
|
|
2
|
+
export declare class AutoResetEvent implements Disposable {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(initialSet?: boolean);
|
|
5
|
+
wait(): Promise<void>;
|
|
6
|
+
notifyOne(): void;
|
|
7
|
+
dispose(): void;
|
|
8
|
+
}
|
|
10
9
|
//# sourceMappingURL=auto-reset-event.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-reset-event.d.ts","sourceRoot":"","sources":["../../src/utils/auto-reset-event.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"auto-reset-event.d.ts","sourceRoot":"","sources":["../../src/utils/auto-reset-event.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,qBAAa,cAAe,YAAW,UAAU;;gBAI1B,UAAU,UAAQ;IAI9B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAcrB,SAAS;IAQT,OAAO;CAMjB"}
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { PromiseResolver } from "@yume-chan/async";
|
|
2
|
-
export class AutoResetEvent {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
constructor(initialSet = false) {
|
|
6
|
-
this
|
|
7
|
-
}
|
|
8
|
-
wait() {
|
|
9
|
-
if (!this
|
|
10
|
-
this
|
|
11
|
-
if (this.
|
|
12
|
-
return Promise.resolve();
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
const resolver = new PromiseResolver();
|
|
16
|
-
this.
|
|
17
|
-
return resolver.promise;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (this.
|
|
21
|
-
this.
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
this
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
dispose() {
|
|
28
|
-
for (const item of this
|
|
29
|
-
item.reject(new Error("The AutoResetEvent has been disposed"));
|
|
30
|
-
}
|
|
31
|
-
this.
|
|
32
|
-
}
|
|
33
|
-
}
|
|
1
|
+
import { PromiseResolver } from "@yume-chan/async";
|
|
2
|
+
export class AutoResetEvent {
|
|
3
|
+
#set;
|
|
4
|
+
#queue = [];
|
|
5
|
+
constructor(initialSet = false) {
|
|
6
|
+
this.#set = initialSet;
|
|
7
|
+
}
|
|
8
|
+
wait() {
|
|
9
|
+
if (!this.#set) {
|
|
10
|
+
this.#set = true;
|
|
11
|
+
if (this.#queue.length === 0) {
|
|
12
|
+
return Promise.resolve();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const resolver = new PromiseResolver();
|
|
16
|
+
this.#queue.push(resolver);
|
|
17
|
+
return resolver.promise;
|
|
18
|
+
}
|
|
19
|
+
notifyOne() {
|
|
20
|
+
if (this.#queue.length !== 0) {
|
|
21
|
+
this.#queue.pop().resolve();
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.#set = false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
dispose() {
|
|
28
|
+
for (const item of this.#queue) {
|
|
29
|
+
item.reject(new Error("The AutoResetEvent has been disposed"));
|
|
30
|
+
}
|
|
31
|
+
this.#queue.length = 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
34
|
//# sourceMappingURL=auto-reset-event.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-reset-event.js","sourceRoot":"","sources":["../../src/utils/auto-reset-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAGnD,MAAM,OAAO,cAAc;
|
|
1
|
+
{"version":3,"file":"auto-reset-event.js","sourceRoot":"","sources":["../../src/utils/auto-reset-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAGnD,MAAM,OAAO,cAAc;IACvB,IAAI,CAAU;IACL,MAAM,GAA4B,EAAE,CAAC;IAE9C,YAAmB,UAAU,GAAG,KAAK;QACjC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;IAC3B,CAAC;IAEM,IAAI;QACP,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;YACZ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YAEjB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;gBAC1B,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;aAC5B;SACJ;QAED,MAAM,QAAQ,GAAG,IAAI,eAAe,EAAQ,CAAC;QAC7C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC3B,OAAO,QAAQ,CAAC,OAAO,CAAC;IAC5B,CAAC;IAEM,SAAS;QACZ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;YAC1B,IAAI,CAAC,MAAM,CAAC,GAAG,EAAG,CAAC,OAAO,EAAE,CAAC;SAChC;aAAM;YACH,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;SACrB;IACL,CAAC;IAEM,OAAO;QACV,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE;YAC5B,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC,CAAC;SAClE;QACD,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IAC3B,CAAC;CACJ"}
|
package/esm/utils/base64.d.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Calculate the required length of the output buffer for the given input length.
|
|
3
|
-
*
|
|
4
|
-
* @param inputLength Length of the input in bytes
|
|
5
|
-
* @returns Length of the output in bytes
|
|
6
|
-
*/
|
|
7
|
-
export declare function calculateBase64EncodedLength(inputLength: number): [outputLength: number, paddingLength: number];
|
|
8
|
-
/**
|
|
9
|
-
* Encode the given input buffer into base64.
|
|
10
|
-
*
|
|
11
|
-
* @param input The input buffer
|
|
12
|
-
* @returns The encoded output buffer
|
|
13
|
-
*/
|
|
14
|
-
export declare function encodeBase64(input: Uint8Array): Uint8Array;
|
|
15
|
-
/**
|
|
16
|
-
* Encode the given input into base64 and write it to the output buffer.
|
|
17
|
-
*
|
|
18
|
-
* The output buffer must be at least as long as the value returned by `calculateBase64EncodedLength`.
|
|
19
|
-
* It can points to the same buffer as the input, as long as `output.offset <= input.offset - input.length / 3`,
|
|
20
|
-
* or `output.offset >= input.offset - 1`
|
|
21
|
-
*
|
|
22
|
-
* @param input The input buffer
|
|
23
|
-
* @param output The output buffer
|
|
24
|
-
* @returns The number of bytes written to the output buffer
|
|
25
|
-
*/
|
|
26
|
-
export declare function encodeBase64(input: Uint8Array, output: Uint8Array): number;
|
|
27
|
-
export declare function decodeBase64(input: string): Uint8Array;
|
|
1
|
+
/**
|
|
2
|
+
* Calculate the required length of the output buffer for the given input length.
|
|
3
|
+
*
|
|
4
|
+
* @param inputLength Length of the input in bytes
|
|
5
|
+
* @returns Length of the output in bytes
|
|
6
|
+
*/
|
|
7
|
+
export declare function calculateBase64EncodedLength(inputLength: number): [outputLength: number, paddingLength: number];
|
|
8
|
+
/**
|
|
9
|
+
* Encode the given input buffer into base64.
|
|
10
|
+
*
|
|
11
|
+
* @param input The input buffer
|
|
12
|
+
* @returns The encoded output buffer
|
|
13
|
+
*/
|
|
14
|
+
export declare function encodeBase64(input: Uint8Array): Uint8Array;
|
|
15
|
+
/**
|
|
16
|
+
* Encode the given input into base64 and write it to the output buffer.
|
|
17
|
+
*
|
|
18
|
+
* The output buffer must be at least as long as the value returned by `calculateBase64EncodedLength`.
|
|
19
|
+
* It can points to the same buffer as the input, as long as `output.offset <= input.offset - input.length / 3`,
|
|
20
|
+
* or `output.offset >= input.offset - 1`
|
|
21
|
+
*
|
|
22
|
+
* @param input The input buffer
|
|
23
|
+
* @param output The output buffer
|
|
24
|
+
* @returns The number of bytes written to the output buffer
|
|
25
|
+
*/
|
|
26
|
+
export declare function encodeBase64(input: Uint8Array, output: Uint8Array): number;
|
|
27
|
+
export declare function decodeBase64(input: string): Uint8Array;
|
|
28
28
|
//# sourceMappingURL=base64.d.ts.map
|
package/esm/utils/base64.js
CHANGED
|
@@ -1,247 +1,247 @@
|
|
|
1
|
-
// Array is faster than object literal or `Map`
|
|
2
|
-
const charToIndex = [];
|
|
3
|
-
const indexToChar = [];
|
|
4
|
-
const paddingChar = "=".charCodeAt(0);
|
|
5
|
-
function addRange(start, end) {
|
|
6
|
-
const charCodeStart = start.charCodeAt(0);
|
|
7
|
-
const charCodeEnd = end.charCodeAt(0);
|
|
8
|
-
for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
|
|
9
|
-
charToIndex[charCode] = indexToChar.length;
|
|
10
|
-
indexToChar.push(charCode);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
addRange("A", "Z");
|
|
14
|
-
addRange("a", "z");
|
|
15
|
-
addRange("0", "9");
|
|
16
|
-
addRange("+", "+");
|
|
17
|
-
addRange("/", "/");
|
|
18
|
-
/**
|
|
19
|
-
* Calculate the required length of the output buffer for the given input length.
|
|
20
|
-
*
|
|
21
|
-
* @param inputLength Length of the input in bytes
|
|
22
|
-
* @returns Length of the output in bytes
|
|
23
|
-
*/
|
|
24
|
-
export function calculateBase64EncodedLength(inputLength) {
|
|
25
|
-
const remainder = inputLength % 3;
|
|
26
|
-
const paddingLength = remainder !== 0 ? 3 - remainder : 0;
|
|
27
|
-
return [((inputLength + paddingLength) / 3) * 4, paddingLength];
|
|
28
|
-
}
|
|
29
|
-
export function encodeBase64(input, output) {
|
|
30
|
-
const [outputLength, paddingLength] = calculateBase64EncodedLength(input.length);
|
|
31
|
-
if (!output) {
|
|
32
|
-
output = new Uint8Array(outputLength);
|
|
33
|
-
encodeForward(input, output, paddingLength);
|
|
34
|
-
return output;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
if (output.length < outputLength) {
|
|
38
|
-
throw new Error("output buffer is too small");
|
|
39
|
-
}
|
|
40
|
-
output = output.subarray(0, outputLength);
|
|
41
|
-
// When input and output are on same ArrayBuffer,
|
|
42
|
-
// we check if it's possible to encode in-place.
|
|
43
|
-
if (input.buffer !== output.buffer) {
|
|
44
|
-
encodeForward(input, output, paddingLength);
|
|
45
|
-
}
|
|
46
|
-
else if (output.byteOffset + output.length - (paddingLength + 1) <=
|
|
47
|
-
input.byteOffset + input.length) {
|
|
48
|
-
// Output ends before input ends
|
|
49
|
-
// So output won't catch up with input.
|
|
50
|
-
// Depends on padding length,
|
|
51
|
-
// it's possible to write 1-3 bytes after input ends.
|
|
52
|
-
// spell: disable-next-line
|
|
53
|
-
// | aaaaaabb | | | |
|
|
54
|
-
// | aaaaaa | bb0000 | = | = |
|
|
55
|
-
//
|
|
56
|
-
// spell: disable-next-line
|
|
57
|
-
// | aaaaaabb | bbbbcccc | | |
|
|
58
|
-
// | aaaaaa | bbbbbb | cccc00 | = |
|
|
59
|
-
//
|
|
60
|
-
// spell: disable-next-line
|
|
61
|
-
// | aaaaaabb | bbbbcccc | ccdddddd | |
|
|
62
|
-
// | aaaaaa | bbbbbb | cccccc | dddddd |
|
|
63
|
-
// Must encode forwards.
|
|
64
|
-
encodeForward(input, output, paddingLength);
|
|
65
|
-
}
|
|
66
|
-
else if (output.byteOffset >= input.byteOffset - 1) {
|
|
67
|
-
// Output starts after input starts
|
|
68
|
-
// So in backwards, output can't catch up with input.
|
|
69
|
-
// Because first 3 bytes becomes 4 bytes,
|
|
70
|
-
// it's possible to write 1 byte before input starts.
|
|
71
|
-
// spell: disable-next-line
|
|
72
|
-
// | | aaaaaabb | bbbbcccc | ccdddddd |
|
|
73
|
-
// | aaaaaa | bbbbbb | cccccc | dddddd |
|
|
74
|
-
// Must encode backwards.
|
|
75
|
-
encodeBackward(input, output, paddingLength);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
// Input is in the middle of output,
|
|
79
|
-
// not possible to read neither first or last three bytes,
|
|
80
|
-
throw new Error("input and output cannot overlap");
|
|
81
|
-
}
|
|
82
|
-
return outputLength;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
function encodeForward(input, output, paddingLength) {
|
|
86
|
-
let inputIndex = 0;
|
|
87
|
-
let outputIndex = 0;
|
|
88
|
-
while (inputIndex < input.length - 2) {
|
|
89
|
-
/* cspell: disable-next-line */
|
|
90
|
-
// aaaaaabb
|
|
91
|
-
const x = input[inputIndex];
|
|
92
|
-
inputIndex += 1;
|
|
93
|
-
/* cspell: disable-next-line */
|
|
94
|
-
// bbbbcccc
|
|
95
|
-
const y = input[inputIndex];
|
|
96
|
-
inputIndex += 1;
|
|
97
|
-
/* cspell: disable-next-line */
|
|
98
|
-
// ccdddddd
|
|
99
|
-
const z = input[inputIndex];
|
|
100
|
-
inputIndex += 1;
|
|
101
|
-
output[outputIndex] = indexToChar[x >> 2];
|
|
102
|
-
outputIndex += 1;
|
|
103
|
-
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
104
|
-
outputIndex += 1;
|
|
105
|
-
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)];
|
|
106
|
-
outputIndex += 1;
|
|
107
|
-
output[outputIndex] = indexToChar[z & 0b111111];
|
|
108
|
-
outputIndex += 1;
|
|
109
|
-
}
|
|
110
|
-
if (paddingLength === 2) {
|
|
111
|
-
/* cspell: disable-next-line */
|
|
112
|
-
// aaaaaabb
|
|
113
|
-
const x = input[inputIndex];
|
|
114
|
-
inputIndex += 1;
|
|
115
|
-
output[outputIndex] = indexToChar[x >> 2];
|
|
116
|
-
outputIndex += 1;
|
|
117
|
-
output[outputIndex] = indexToChar[(x & 0b11) << 4];
|
|
118
|
-
outputIndex += 1;
|
|
119
|
-
output[outputIndex] = paddingChar;
|
|
120
|
-
outputIndex += 1;
|
|
121
|
-
output[outputIndex] = paddingChar;
|
|
122
|
-
}
|
|
123
|
-
else if (paddingLength === 1) {
|
|
124
|
-
/* cspell: disable-next-line */
|
|
125
|
-
// aaaaaabb
|
|
126
|
-
const x = input[inputIndex];
|
|
127
|
-
inputIndex += 1;
|
|
128
|
-
/* cspell: disable-next-line */
|
|
129
|
-
// bbbbcccc
|
|
130
|
-
const y = input[inputIndex];
|
|
131
|
-
inputIndex += 1;
|
|
132
|
-
output[outputIndex] = indexToChar[x >> 2];
|
|
133
|
-
outputIndex += 1;
|
|
134
|
-
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
135
|
-
outputIndex += 1;
|
|
136
|
-
output[outputIndex] = indexToChar[(y & 0b1111) << 2];
|
|
137
|
-
outputIndex += 1;
|
|
138
|
-
output[outputIndex] = paddingChar;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
function encodeBackward(input, output, paddingLength) {
|
|
142
|
-
let inputIndex = input.length - 1;
|
|
143
|
-
let outputIndex = output.length - 1;
|
|
144
|
-
if (paddingLength === 2) {
|
|
145
|
-
/* cspell: disable-next-line */
|
|
146
|
-
// aaaaaabb
|
|
147
|
-
const x = input[inputIndex];
|
|
148
|
-
inputIndex -= 1;
|
|
149
|
-
output[outputIndex] = paddingChar;
|
|
150
|
-
outputIndex -= 1;
|
|
151
|
-
output[outputIndex] = paddingChar;
|
|
152
|
-
outputIndex -= 1;
|
|
153
|
-
output[outputIndex] = indexToChar[(x & 0b11) << 4];
|
|
154
|
-
outputIndex -= 1;
|
|
155
|
-
output[outputIndex] = indexToChar[x >> 2];
|
|
156
|
-
outputIndex -= 1;
|
|
157
|
-
}
|
|
158
|
-
else if (paddingLength === 1) {
|
|
159
|
-
/* cspell: disable-next-line */
|
|
160
|
-
// bbbbcccc
|
|
161
|
-
const y = input[inputIndex];
|
|
162
|
-
inputIndex -= 1;
|
|
163
|
-
/* cspell: disable-next-line */
|
|
164
|
-
// aaaaaabb
|
|
165
|
-
const x = input[inputIndex];
|
|
166
|
-
inputIndex -= 1;
|
|
167
|
-
output[outputIndex] = paddingChar;
|
|
168
|
-
outputIndex -= 1;
|
|
169
|
-
output[outputIndex] = indexToChar[(y & 0b1111) << 2];
|
|
170
|
-
outputIndex -= 1;
|
|
171
|
-
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
172
|
-
outputIndex -= 1;
|
|
173
|
-
output[outputIndex] = indexToChar[x >> 2];
|
|
174
|
-
outputIndex -= 1;
|
|
175
|
-
}
|
|
176
|
-
while (inputIndex >= 0) {
|
|
177
|
-
/* cspell: disable-next-line */
|
|
178
|
-
// ccdddddd
|
|
179
|
-
const z = input[inputIndex];
|
|
180
|
-
inputIndex -= 1;
|
|
181
|
-
/* cspell: disable-next-line */
|
|
182
|
-
// bbbbcccc
|
|
183
|
-
const y = input[inputIndex];
|
|
184
|
-
inputIndex -= 1;
|
|
185
|
-
/* cspell: disable-next-line */
|
|
186
|
-
// aaaaaabb
|
|
187
|
-
const x = input[inputIndex];
|
|
188
|
-
inputIndex -= 1;
|
|
189
|
-
output[outputIndex] = indexToChar[z & 0b111111];
|
|
190
|
-
outputIndex -= 1;
|
|
191
|
-
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)];
|
|
192
|
-
outputIndex -= 1;
|
|
193
|
-
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
194
|
-
outputIndex -= 1;
|
|
195
|
-
output[outputIndex] = indexToChar[x >> 2];
|
|
196
|
-
outputIndex -= 1;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
export function decodeBase64(input) {
|
|
200
|
-
let padding;
|
|
201
|
-
if (input[input.length - 2] === "=") {
|
|
202
|
-
padding = 2;
|
|
203
|
-
}
|
|
204
|
-
else if (input[input.length - 1] === "=") {
|
|
205
|
-
padding = 1;
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
padding = 0;
|
|
209
|
-
}
|
|
210
|
-
const result = new Uint8Array((input.length / 4) * 3 - padding);
|
|
211
|
-
let sIndex = 0;
|
|
212
|
-
let dIndex = 0;
|
|
213
|
-
while (sIndex < input.length - (padding !== 0 ? 4 : 0)) {
|
|
214
|
-
const a = charToIndex[input.charCodeAt(sIndex)];
|
|
215
|
-
sIndex += 1;
|
|
216
|
-
const b = charToIndex[input.charCodeAt(sIndex)];
|
|
217
|
-
sIndex += 1;
|
|
218
|
-
const c = charToIndex[input.charCodeAt(sIndex)];
|
|
219
|
-
sIndex += 1;
|
|
220
|
-
const d = charToIndex[input.charCodeAt(sIndex)];
|
|
221
|
-
sIndex += 1;
|
|
222
|
-
result[dIndex] = (a << 2) | ((b &
|
|
223
|
-
dIndex += 1;
|
|
224
|
-
result[dIndex] = ((b & 0b1111) << 4) | ((c &
|
|
225
|
-
dIndex += 1;
|
|
226
|
-
result[dIndex] = ((c & 0b11) << 6) | d;
|
|
227
|
-
dIndex += 1;
|
|
228
|
-
}
|
|
229
|
-
if (padding === 1) {
|
|
230
|
-
const a = charToIndex[input.charCodeAt(sIndex)];
|
|
231
|
-
sIndex += 1;
|
|
232
|
-
const b = charToIndex[input.charCodeAt(sIndex)];
|
|
233
|
-
sIndex += 1;
|
|
234
|
-
const c = charToIndex[input.charCodeAt(sIndex)];
|
|
235
|
-
result[dIndex] = (a << 2) | ((b &
|
|
236
|
-
dIndex += 1;
|
|
237
|
-
result[dIndex] = ((b & 0b1111) << 4) | ((c &
|
|
238
|
-
}
|
|
239
|
-
else if (padding === 2) {
|
|
240
|
-
const a = charToIndex[input.charCodeAt(sIndex)];
|
|
241
|
-
sIndex += 1;
|
|
242
|
-
const b = charToIndex[input.charCodeAt(sIndex)];
|
|
243
|
-
result[dIndex] = (a << 2) | ((b &
|
|
244
|
-
}
|
|
245
|
-
return result;
|
|
246
|
-
}
|
|
1
|
+
// Array is faster than object literal or `Map`
|
|
2
|
+
const charToIndex = [];
|
|
3
|
+
const indexToChar = [];
|
|
4
|
+
const paddingChar = "=".charCodeAt(0);
|
|
5
|
+
function addRange(start, end) {
|
|
6
|
+
const charCodeStart = start.charCodeAt(0);
|
|
7
|
+
const charCodeEnd = end.charCodeAt(0);
|
|
8
|
+
for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
|
|
9
|
+
charToIndex[charCode] = indexToChar.length;
|
|
10
|
+
indexToChar.push(charCode);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
addRange("A", "Z");
|
|
14
|
+
addRange("a", "z");
|
|
15
|
+
addRange("0", "9");
|
|
16
|
+
addRange("+", "+");
|
|
17
|
+
addRange("/", "/");
|
|
18
|
+
/**
|
|
19
|
+
* Calculate the required length of the output buffer for the given input length.
|
|
20
|
+
*
|
|
21
|
+
* @param inputLength Length of the input in bytes
|
|
22
|
+
* @returns Length of the output in bytes
|
|
23
|
+
*/
|
|
24
|
+
export function calculateBase64EncodedLength(inputLength) {
|
|
25
|
+
const remainder = inputLength % 3;
|
|
26
|
+
const paddingLength = remainder !== 0 ? 3 - remainder : 0;
|
|
27
|
+
return [((inputLength + paddingLength) / 3) * 4, paddingLength];
|
|
28
|
+
}
|
|
29
|
+
export function encodeBase64(input, output) {
|
|
30
|
+
const [outputLength, paddingLength] = calculateBase64EncodedLength(input.length);
|
|
31
|
+
if (!output) {
|
|
32
|
+
output = new Uint8Array(outputLength);
|
|
33
|
+
encodeForward(input, output, paddingLength);
|
|
34
|
+
return output;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (output.length < outputLength) {
|
|
38
|
+
throw new Error("output buffer is too small");
|
|
39
|
+
}
|
|
40
|
+
output = output.subarray(0, outputLength);
|
|
41
|
+
// When input and output are on same ArrayBuffer,
|
|
42
|
+
// we check if it's possible to encode in-place.
|
|
43
|
+
if (input.buffer !== output.buffer) {
|
|
44
|
+
encodeForward(input, output, paddingLength);
|
|
45
|
+
}
|
|
46
|
+
else if (output.byteOffset + output.length - (paddingLength + 1) <=
|
|
47
|
+
input.byteOffset + input.length) {
|
|
48
|
+
// Output ends before input ends
|
|
49
|
+
// So output won't catch up with input.
|
|
50
|
+
// Depends on padding length,
|
|
51
|
+
// it's possible to write 1-3 bytes after input ends.
|
|
52
|
+
// spell: disable-next-line
|
|
53
|
+
// | aaaaaabb | | | |
|
|
54
|
+
// | aaaaaa | bb0000 | = | = |
|
|
55
|
+
//
|
|
56
|
+
// spell: disable-next-line
|
|
57
|
+
// | aaaaaabb | bbbbcccc | | |
|
|
58
|
+
// | aaaaaa | bbbbbb | cccc00 | = |
|
|
59
|
+
//
|
|
60
|
+
// spell: disable-next-line
|
|
61
|
+
// | aaaaaabb | bbbbcccc | ccdddddd | |
|
|
62
|
+
// | aaaaaa | bbbbbb | cccccc | dddddd |
|
|
63
|
+
// Must encode forwards.
|
|
64
|
+
encodeForward(input, output, paddingLength);
|
|
65
|
+
}
|
|
66
|
+
else if (output.byteOffset >= input.byteOffset - 1) {
|
|
67
|
+
// Output starts after input starts
|
|
68
|
+
// So in backwards, output can't catch up with input.
|
|
69
|
+
// Because first 3 bytes becomes 4 bytes,
|
|
70
|
+
// it's possible to write 1 byte before input starts.
|
|
71
|
+
// spell: disable-next-line
|
|
72
|
+
// | | aaaaaabb | bbbbcccc | ccdddddd |
|
|
73
|
+
// | aaaaaa | bbbbbb | cccccc | dddddd |
|
|
74
|
+
// Must encode backwards.
|
|
75
|
+
encodeBackward(input, output, paddingLength);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// Input is in the middle of output,
|
|
79
|
+
// not possible to read neither first or last three bytes,
|
|
80
|
+
throw new Error("input and output cannot overlap");
|
|
81
|
+
}
|
|
82
|
+
return outputLength;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function encodeForward(input, output, paddingLength) {
|
|
86
|
+
let inputIndex = 0;
|
|
87
|
+
let outputIndex = 0;
|
|
88
|
+
while (inputIndex < input.length - 2) {
|
|
89
|
+
/* cspell: disable-next-line */
|
|
90
|
+
// aaaaaabb
|
|
91
|
+
const x = input[inputIndex];
|
|
92
|
+
inputIndex += 1;
|
|
93
|
+
/* cspell: disable-next-line */
|
|
94
|
+
// bbbbcccc
|
|
95
|
+
const y = input[inputIndex];
|
|
96
|
+
inputIndex += 1;
|
|
97
|
+
/* cspell: disable-next-line */
|
|
98
|
+
// ccdddddd
|
|
99
|
+
const z = input[inputIndex];
|
|
100
|
+
inputIndex += 1;
|
|
101
|
+
output[outputIndex] = indexToChar[x >> 2];
|
|
102
|
+
outputIndex += 1;
|
|
103
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
104
|
+
outputIndex += 1;
|
|
105
|
+
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)];
|
|
106
|
+
outputIndex += 1;
|
|
107
|
+
output[outputIndex] = indexToChar[z & 0b111111];
|
|
108
|
+
outputIndex += 1;
|
|
109
|
+
}
|
|
110
|
+
if (paddingLength === 2) {
|
|
111
|
+
/* cspell: disable-next-line */
|
|
112
|
+
// aaaaaabb
|
|
113
|
+
const x = input[inputIndex];
|
|
114
|
+
inputIndex += 1;
|
|
115
|
+
output[outputIndex] = indexToChar[x >> 2];
|
|
116
|
+
outputIndex += 1;
|
|
117
|
+
output[outputIndex] = indexToChar[(x & 0b11) << 4];
|
|
118
|
+
outputIndex += 1;
|
|
119
|
+
output[outputIndex] = paddingChar;
|
|
120
|
+
outputIndex += 1;
|
|
121
|
+
output[outputIndex] = paddingChar;
|
|
122
|
+
}
|
|
123
|
+
else if (paddingLength === 1) {
|
|
124
|
+
/* cspell: disable-next-line */
|
|
125
|
+
// aaaaaabb
|
|
126
|
+
const x = input[inputIndex];
|
|
127
|
+
inputIndex += 1;
|
|
128
|
+
/* cspell: disable-next-line */
|
|
129
|
+
// bbbbcccc
|
|
130
|
+
const y = input[inputIndex];
|
|
131
|
+
inputIndex += 1;
|
|
132
|
+
output[outputIndex] = indexToChar[x >> 2];
|
|
133
|
+
outputIndex += 1;
|
|
134
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
135
|
+
outputIndex += 1;
|
|
136
|
+
output[outputIndex] = indexToChar[(y & 0b1111) << 2];
|
|
137
|
+
outputIndex += 1;
|
|
138
|
+
output[outputIndex] = paddingChar;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function encodeBackward(input, output, paddingLength) {
|
|
142
|
+
let inputIndex = input.length - 1;
|
|
143
|
+
let outputIndex = output.length - 1;
|
|
144
|
+
if (paddingLength === 2) {
|
|
145
|
+
/* cspell: disable-next-line */
|
|
146
|
+
// aaaaaabb
|
|
147
|
+
const x = input[inputIndex];
|
|
148
|
+
inputIndex -= 1;
|
|
149
|
+
output[outputIndex] = paddingChar;
|
|
150
|
+
outputIndex -= 1;
|
|
151
|
+
output[outputIndex] = paddingChar;
|
|
152
|
+
outputIndex -= 1;
|
|
153
|
+
output[outputIndex] = indexToChar[(x & 0b11) << 4];
|
|
154
|
+
outputIndex -= 1;
|
|
155
|
+
output[outputIndex] = indexToChar[x >> 2];
|
|
156
|
+
outputIndex -= 1;
|
|
157
|
+
}
|
|
158
|
+
else if (paddingLength === 1) {
|
|
159
|
+
/* cspell: disable-next-line */
|
|
160
|
+
// bbbbcccc
|
|
161
|
+
const y = input[inputIndex];
|
|
162
|
+
inputIndex -= 1;
|
|
163
|
+
/* cspell: disable-next-line */
|
|
164
|
+
// aaaaaabb
|
|
165
|
+
const x = input[inputIndex];
|
|
166
|
+
inputIndex -= 1;
|
|
167
|
+
output[outputIndex] = paddingChar;
|
|
168
|
+
outputIndex -= 1;
|
|
169
|
+
output[outputIndex] = indexToChar[(y & 0b1111) << 2];
|
|
170
|
+
outputIndex -= 1;
|
|
171
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
172
|
+
outputIndex -= 1;
|
|
173
|
+
output[outputIndex] = indexToChar[x >> 2];
|
|
174
|
+
outputIndex -= 1;
|
|
175
|
+
}
|
|
176
|
+
while (inputIndex >= 0) {
|
|
177
|
+
/* cspell: disable-next-line */
|
|
178
|
+
// ccdddddd
|
|
179
|
+
const z = input[inputIndex];
|
|
180
|
+
inputIndex -= 1;
|
|
181
|
+
/* cspell: disable-next-line */
|
|
182
|
+
// bbbbcccc
|
|
183
|
+
const y = input[inputIndex];
|
|
184
|
+
inputIndex -= 1;
|
|
185
|
+
/* cspell: disable-next-line */
|
|
186
|
+
// aaaaaabb
|
|
187
|
+
const x = input[inputIndex];
|
|
188
|
+
inputIndex -= 1;
|
|
189
|
+
output[outputIndex] = indexToChar[z & 0b111111];
|
|
190
|
+
outputIndex -= 1;
|
|
191
|
+
output[outputIndex] = indexToChar[((y & 0b1111) << 2) | (z >> 6)];
|
|
192
|
+
outputIndex -= 1;
|
|
193
|
+
output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)];
|
|
194
|
+
outputIndex -= 1;
|
|
195
|
+
output[outputIndex] = indexToChar[x >> 2];
|
|
196
|
+
outputIndex -= 1;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
export function decodeBase64(input) {
|
|
200
|
+
let padding;
|
|
201
|
+
if (input[input.length - 2] === "=") {
|
|
202
|
+
padding = 2;
|
|
203
|
+
}
|
|
204
|
+
else if (input[input.length - 1] === "=") {
|
|
205
|
+
padding = 1;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
padding = 0;
|
|
209
|
+
}
|
|
210
|
+
const result = new Uint8Array((input.length / 4) * 3 - padding);
|
|
211
|
+
let sIndex = 0;
|
|
212
|
+
let dIndex = 0;
|
|
213
|
+
while (sIndex < input.length - (padding !== 0 ? 4 : 0)) {
|
|
214
|
+
const a = charToIndex[input.charCodeAt(sIndex)];
|
|
215
|
+
sIndex += 1;
|
|
216
|
+
const b = charToIndex[input.charCodeAt(sIndex)];
|
|
217
|
+
sIndex += 1;
|
|
218
|
+
const c = charToIndex[input.charCodeAt(sIndex)];
|
|
219
|
+
sIndex += 1;
|
|
220
|
+
const d = charToIndex[input.charCodeAt(sIndex)];
|
|
221
|
+
sIndex += 1;
|
|
222
|
+
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
|
|
223
|
+
dIndex += 1;
|
|
224
|
+
result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
|
|
225
|
+
dIndex += 1;
|
|
226
|
+
result[dIndex] = ((c & 0b11) << 6) | d;
|
|
227
|
+
dIndex += 1;
|
|
228
|
+
}
|
|
229
|
+
if (padding === 1) {
|
|
230
|
+
const a = charToIndex[input.charCodeAt(sIndex)];
|
|
231
|
+
sIndex += 1;
|
|
232
|
+
const b = charToIndex[input.charCodeAt(sIndex)];
|
|
233
|
+
sIndex += 1;
|
|
234
|
+
const c = charToIndex[input.charCodeAt(sIndex)];
|
|
235
|
+
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
|
|
236
|
+
dIndex += 1;
|
|
237
|
+
result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
|
|
238
|
+
}
|
|
239
|
+
else if (padding === 2) {
|
|
240
|
+
const a = charToIndex[input.charCodeAt(sIndex)];
|
|
241
|
+
sIndex += 1;
|
|
242
|
+
const b = charToIndex[input.charCodeAt(sIndex)];
|
|
243
|
+
result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
|
|
244
|
+
}
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
247
|
//# sourceMappingURL=base64.js.map
|