Skip to content
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
25 changes: 25 additions & 0 deletions packages/dev/core/src/LibDeclarations/browser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,37 @@ interface Window {
DracoDecoderModule: any;
}

interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> {
/**
* Converts the `Uint8Array` to a base64-encoded string.
* @param options If provided, sets the alphabet and padding behavior used.
* @returns A base64-encoded string.
*/
toBase64?(options?: { alphabet?: "base64" | "base64url" | undefined; omitPadding?: boolean | undefined }): string;
}

interface Int8ArrayConstructor {
new (data: number | ArrayLike<number> | ArrayBufferLike): Int8Array<ArrayBuffer>;
}

interface Uint8ArrayConstructor {
new (data: number | ArrayLike<number> | ArrayBufferLike): Uint8Array<ArrayBuffer>;

/**
* Creates a new `Uint8Array` from a base64-encoded string.
* @param string The base64-encoded string.
* @param options If provided, specifies the alphabet and handling of the last chunk.
* @returns A new `Uint8Array` instance.
* @throws {SyntaxError} If the input string contains characters outside the specified alphabet, or if the last
* chunk is inconsistent with the `lastChunkHandling` option.
*/
fromBase64?(
string: string,
options?: {
alphabet?: "base64" | "base64url" | undefined;
lastChunkHandling?: "loose" | "strict" | "stop-before-partial" | undefined;
}
): Uint8Array<ArrayBuffer>;
}

interface Float32ArrayConstructor {
Expand Down
41 changes: 25 additions & 16 deletions packages/dev/core/src/Misc/stringTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,11 @@ export const Decode = (buffer: Uint8Array | Uint16Array): string => {
return result;
};

/**
* Encode a buffer to a base64 string
* @param buffer defines the buffer to encode
* @returns the encoded string
*/
export const EncodeArrayBufferToBase64 = (buffer: ArrayBuffer | ArrayBufferView): string => {
function JsEncodeArrayBufferToBase64(bytes: Uint8Array): string {
const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
let output = "";
let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
let i = 0;
const bytes = ArrayBuffer.isView(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength) : new Uint8Array(buffer);

while (i < bytes.length) {
chr1 = bytes[i++];
Expand All @@ -74,6 +68,29 @@ export const EncodeArrayBufferToBase64 = (buffer: ArrayBuffer | ArrayBufferView)
}

return output;
}

function JsDecodeBase64ToBinary(base64Data: string): ArrayBuffer {
const decodedString = DecodeBase64ToString(base64Data);
const bufferLength = decodedString.length;
const bufferView = new Uint8Array(new ArrayBuffer(bufferLength));

for (let i = 0; i < bufferLength; i++) {
bufferView[i] = decodedString.charCodeAt(i);
}

return bufferView.buffer;
}

/**
* Encode a buffer to a base64 string
* @param buffer defines the buffer to encode
* @returns the encoded string
*/
export const EncodeArrayBufferToBase64 = (buffer: ArrayBuffer | ArrayBufferView): string => {
const bytes = ArrayBuffer.isView(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength) : new Uint8Array(buffer);

return bytes.toBase64 ? bytes.toBase64() : JsEncodeArrayBufferToBase64(bytes);
};

/**
Expand All @@ -91,15 +108,7 @@ export const DecodeBase64ToString = (base64Data: string): string => {
* @returns ArrayBuffer of byte data
*/
export const DecodeBase64ToBinary = (base64Data: string): ArrayBuffer => {
const decodedString = DecodeBase64ToString(base64Data);
const bufferLength = decodedString.length;
const bufferView = new Uint8Array(new ArrayBuffer(bufferLength));

for (let i = 0; i < bufferLength; i++) {
bufferView[i] = decodedString.charCodeAt(i);
}

return bufferView.buffer;
return Uint8Array.fromBase64 ? Uint8Array.fromBase64(base64Data).buffer : JsDecodeBase64ToBinary(base64Data);
};

/**
Expand Down
Loading