Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk): Removes base64-js dep #402

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading