a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(multibase): faster base64 padding trim

Mary 0c56e4b6 a46b7efd

+23 -5
+5
.changeset/yummy-frogs-allow.md
··· 1 + --- 2 + '@atcute/multibase': patch 3 + --- 4 + 5 + faster base64 padding trim
+18 -5
packages/utilities/multibase/lib/bases/base64-node.ts
··· 3 3 import { allocUnsafe } from '@atcute/uint8array'; 4 4 5 5 // `base64` has padding 6 - const _base64Slice = NodeBuffer.prototype.base64Slice; 7 - const _base64Write = NodeBuffer.prototype.base64Write; 6 + const _base64Slice = /*#__PURE__*/ NodeBuffer.prototype.base64Slice; 7 + const _base64Write = /*#__PURE__*/ NodeBuffer.prototype.base64Write; 8 8 9 9 // `base64url` has no padding 10 - const _base64UrlSlice = NodeBuffer.prototype.base64urlSlice; 11 - const _base64UrlWrite = NodeBuffer.prototype.base64urlWrite; 10 + const _base64UrlSlice = /*#__PURE__*/ NodeBuffer.prototype.base64urlSlice; 11 + const _base64UrlWrite = /*#__PURE__*/ NodeBuffer.prototype.base64urlWrite; 12 12 13 13 const getBase64ByteLength = (str: string, padded: boolean): number => { 14 14 let length = str.length; ··· 24 24 return (length * 3) >>> 2; 25 25 }; 26 26 27 + const trimBase64Padding = (str: string): string => { 28 + const end = str.length - 1; 29 + if (str.charCodeAt(end) !== 0x3d) { 30 + return str; 31 + } 32 + 33 + if (str.charCodeAt(end - 1) !== 0x3d) { 34 + return str.slice(0, end); 35 + } 36 + 37 + return str.slice(0, end - 1); 38 + }; 39 + 27 40 export const fromBase64 = (str: string): Uint8Array<ArrayBuffer> => { 28 41 const length = getBase64ByteLength(str, false); 29 42 const bytes = allocUnsafe(length); ··· 33 46 }; 34 47 35 48 export const toBase64 = (bytes: Uint8Array): string => { 36 - return _base64Slice.call(bytes).replaceAll('=', ''); 49 + return trimBase64Padding(_base64Slice.call(bytes)); 37 50 }; 38 51 39 52 export const fromBase64Pad = (str: string): Uint8Array<ArrayBuffer> => {