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

Configure Feed

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

refactor(multibase): move base16 web native/polyfill into its own file

Mary bc54ef89 33455f07

+27 -29
+7
packages/utilities/multibase/lib/bases/base16-web-native.ts
··· 1 + export const fromBase16 = (str: string): Uint8Array => { 2 + return Uint8Array.fromHex(str); 3 + }; 4 + 5 + export const toBase16 = (bytes: Uint8Array): string => { 6 + return bytes.toHex(); 7 + };
+6
packages/utilities/multibase/lib/bases/base16-web-polyfill.ts
··· 1 + import { createRfc4648Decode, createRfc4648Encode } from '../utils.js'; 2 + 3 + const BASE16_CHARSET = '0123456789abcdef'; 4 + 5 + export const fromBase16 = /*#__PURE__*/ createRfc4648Decode(BASE16_CHARSET, 4, false); 6 + export const toBase16 = /*#__PURE__*/ createRfc4648Encode(BASE16_CHARSET, 4, false);
+5 -21
packages/utilities/multibase/lib/bases/base16-web.ts
··· 1 - import { createRfc4648Decode, createRfc4648Encode } from '../utils.js'; 2 - 3 - const HAS_UINT8_BASE16_SUPPORT = 'fromHex' in Uint8Array; 4 - 5 - const BASE16_CHARSET = '0123456789abcdef'; 6 - 7 - /** @internal */ 8 - export const _fromBase16Polyfill = /*#__PURE__*/ createRfc4648Decode(BASE16_CHARSET, 4, false); 9 - /** @internal */ 10 - export const _toBase16Polyfill = /*#__PURE__*/ createRfc4648Encode(BASE16_CHARSET, 4, false); 11 - 12 - /** @internal */ 13 - export const _fromBase16Native = (str: string): Uint8Array => { 14 - return Uint8Array.fromHex(str); 15 - }; 1 + import { fromBase16 as fromBase16Native, toBase16 as toBase16Native } from './base16-web-native.js'; 2 + import { fromBase16 as fromBase16Polyfill, toBase16 as toBase16Polyfill } from './base16-web-polyfill.js'; 16 3 17 - /** @internal */ 18 - export const _toBase16Native = (bytes: Uint8Array): string => { 19 - return bytes.toHex(); 20 - }; 4 + const HAS_NATIVE_SUPPORT = 'fromHex' in Uint8Array; 21 5 22 - export const fromBase16 = !HAS_UINT8_BASE16_SUPPORT ? _fromBase16Polyfill : _fromBase16Native; 23 - export const toBase16 = !HAS_UINT8_BASE16_SUPPORT ? _toBase16Polyfill : _toBase16Native; 6 + export const fromBase16 = !HAS_NATIVE_SUPPORT ? fromBase16Polyfill : fromBase16Native; 7 + export const toBase16 = !HAS_NATIVE_SUPPORT ? toBase16Polyfill : toBase16Native;
+9 -8
packages/utilities/multibase/lib/bases/base16.test.ts
··· 1 1 import { expect, it, mock } from 'bun:test'; 2 2 3 - import { fromBase16 as _fromBase16Node, toBase16 as _toBase16Node } from './base16-node.js'; 4 - import { _fromBase16Native, _fromBase16Polyfill, _toBase16Native, _toBase16Polyfill } from './base16-web.js'; 3 + import { fromBase16 as fromBase16Node, toBase16 as toBase16Node } from './base16-node.js'; 4 + import { fromBase16 as fromBase16Native, toBase16 as toBase16Native } from './base16-web-native.js'; 5 + import { fromBase16 as fromBase16Polyfill, toBase16 as toBase16Polyfill } from './base16-web-polyfill.js'; 5 6 6 7 const inputs = [ 7 8 { ··· 39 40 40 41 it('can encode', () => { 41 42 for (const { buffer, encoded } of inputs) { 42 - expect(_toBase16Polyfill(buffer)).toEqual(encoded); 43 - expect(_toBase16Node(buffer)).toEqual(encoded); 44 - expect(_toBase16Native(buffer)).toEqual(encoded); 43 + expect(toBase16Polyfill(buffer)).toEqual(encoded); 44 + expect(toBase16Node(buffer)).toEqual(encoded); 45 + expect(toBase16Native(buffer)).toEqual(encoded); 45 46 } 46 47 }); 47 48 48 49 it('can decode', () => { 49 50 for (const { buffer, encoded } of inputs) { 50 - expect(_fromBase16Polyfill(encoded)).toEqual(buffer); 51 - expect(_fromBase16Node(encoded)).toEqual(buffer); 52 - expect(_fromBase16Native(encoded)).toEqual(buffer); 51 + expect(fromBase16Polyfill(encoded)).toEqual(buffer); 52 + expect(fromBase16Node(encoded)).toEqual(buffer); 53 + expect(fromBase16Native(encoded)).toEqual(buffer); 53 54 } 54 55 });