Skip to content

Commit

Permalink
Avoid copying existing buffer in base64 encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Nov 30, 2023
1 parent 025e498 commit 5b64d40
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/driver/src/primitives/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* limitations under the License.
*/

import char, * as chars from "./chars";
import type char from "./chars";
import * as chars from "./chars";
import * as bi from "./bigint";
import * as compat from "../compat";
import { LegacyHeaderCodes } from "../ifaces";
Expand All @@ -32,14 +33,15 @@ export const utf8Decoder = new TextDecoder("utf8");
let decodeB64: (b64: string) => Uint8Array;
let encodeB64: (data: Uint8Array) => string;

if (typeof btoa === "undefined") {
if (typeof globalThis.Buffer === "function") {
decodeB64 = (b64: string): Uint8Array => {
// @ts-ignore
return Buffer.from(b64, "base64");
};
encodeB64 = (data: Uint8Array): string => {
// @ts-ignore
return Buffer.from(data).toString("base64");
const buf = !Buffer.isBuffer(data)
? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
: data;
return buf.toString("base64");
};
} else {
decodeB64 = (b64: string): Uint8Array => {
Expand Down

0 comments on commit 5b64d40

Please sign in to comment.