Skip to content

Commit

Permalink
chore(sdk): Removes base64-js dep (#402)
Browse files Browse the repository at this point in the history
We already have an internal library that provides this

Signed-off-by: Elizabeth Healy <[email protected]>
  • Loading branch information
dmihalcik-virtru authored and elizabethhealy committed Dec 13, 2024
1 parent 5dd9610 commit bf53a46
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"watch": "(trap 'kill 0' SIGINT; npm run build && (npm run build:watch & npm run test -- --watch))"
},
"dependencies": {
"base64-js": "^1.5.1",
"browser-fs-access": "^0.34.1",
"buffer-crc32": "^0.2.13",
"dpop": "^1.2.0",
Expand Down
22 changes: 17 additions & 5 deletions lib/tdf3/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { toByteArray, fromByteArray } from 'base64-js';
import * as WebCryptoService from '../crypto/index.js';
import { KeyInfo, SplitKey } from '../models/index.js';

import { AesGcmCipher } from '../ciphers/aes-gcm-cipher.js';
import { ConfigurationError } from '../../../src/errors.js';
import { decodeArrayBuffer, encodeArrayBuffer } from '../../../src/encodings/base64.js';

export { ZipReader, readUInt64LE } from './zip-reader.js';
export { ZipWriter } from './zip-writer.js';
Expand Down Expand Up @@ -116,9 +116,9 @@ function latin1Slice(buf: Uint8Array, start: number, end: number): string {

function base64Slice(buf: Uint8Array, start: number, end: number): string {
if (start === 0 && end === buf.length) {
return fromByteArray(buf);
return encodeArrayBuffer(buf);
} else {
return fromByteArray(buf.slice(start, end));
return encodeArrayBuffer(buf.slice(start, end));
}
}

Expand Down Expand Up @@ -260,6 +260,18 @@ function decodeCodePointsArray(codePoints: number[]): string {

const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;

/**
* Cleans a Base64 encoded string to ensure it is properly formatted.
*
* This function performs the following operations:
* 1. Removes any trailing equal signs (`=`) which are used as padding in Base64 encoding.
* 2. Trims whitespace and removes invalid Base64 characters (e.g., `\n`, `\t`).
* 3. Returns an empty string if the cleaned string has a length less than 2.
* 4. Adds padding equal signs (`=`) to the end of the string if its length is not a multiple of 4.
*
* @param str The Base64 encoded string to clean.
* @returns A clean Base64 encoded string.
*/
function base64clean(str: string) {
// Node takes equal signs as end of the Base64 encoding
str = str.split('=')[0];
Expand All @@ -274,8 +286,8 @@ function base64clean(str: string) {
return str;
}

export function base64ToBytes(str: string) {
return toByteArray(base64clean(str));
export function base64ToBytes(str: string): Uint8Array {
return new Uint8Array(decodeArrayBuffer(base64clean(str)));
}

/**
Expand Down

0 comments on commit bf53a46

Please sign in to comment.