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.

feat(varint): unroll varint encode/decode

Mary b6222920 f7167258

+93 -7
+5
.changeset/silent-plants-cut.md
··· 1 + --- 2 + '@atcute/varint': patch 3 + --- 4 + 5 + unroll varint encode/decode
+88 -7
packages/utilities/varint/lib/index.ts
··· 27 27 28 28 const start = offset; 29 29 30 + if (num < N1) { 31 + buf[offset] = num; 32 + return 1; 33 + } 34 + 35 + if (num < N2) { 36 + buf[offset] = (num & REST) | MSB; 37 + buf[offset + 1] = num >>> 7; 38 + return 2; 39 + } 40 + 41 + if (num < N3) { 42 + buf[offset] = (num & REST) | MSB; 43 + buf[offset + 1] = ((num >>> 7) & REST) | MSB; 44 + buf[offset + 2] = num >>> 14; 45 + return 3; 46 + } 47 + 48 + if (num < N4) { 49 + buf[offset] = (num & REST) | MSB; 50 + buf[offset + 1] = ((num >>> 7) & REST) | MSB; 51 + buf[offset + 2] = ((num >>> 14) & REST) | MSB; 52 + buf[offset + 3] = num >>> 21; 53 + return 4; 54 + } 55 + 56 + if (num < INT) { 57 + buf[offset] = (num & REST) | MSB; 58 + buf[offset + 1] = ((num >>> 7) & REST) | MSB; 59 + buf[offset + 2] = ((num >>> 14) & REST) | MSB; 60 + buf[offset + 3] = ((num >>> 21) & REST) | MSB; 61 + buf[offset + 4] = num >>> 28; 62 + return 5; 63 + } 64 + 30 65 while (num >= INT) { 31 66 buf[offset++] = (num & 0xff) | MSB; 32 67 num /= 128; ··· 48 83 * @returns A tuple containing the resulting number, and the amount of bytes read 49 84 */ 50 85 export const decode = (buf: Uint8Array | number[], offset = 0): [num: number, read: number] => { 51 - // deno-lint-ignore prefer-const 52 - let l = buf.length; 86 + const l = buf.length; 87 + let counter = offset; 88 + 89 + if (counter >= l) { 90 + throw new RangeError('could not decode varint'); 91 + } 92 + 93 + let b = buf[counter++]; 94 + let res = b & REST; 95 + if (b < MSB) { 96 + return [res, 1]; 97 + } 98 + 99 + if (counter >= l) { 100 + throw new RangeError('could not decode varint'); 101 + } 102 + 103 + b = buf[counter++]; 104 + res |= (b & REST) << 7; 105 + if (b < MSB) { 106 + return [res, 2]; 107 + } 108 + 109 + if (counter >= l) { 110 + throw new RangeError('could not decode varint'); 111 + } 112 + 113 + b = buf[counter++]; 114 + res |= (b & REST) << 14; 115 + if (b < MSB) { 116 + return [res, 3]; 117 + } 118 + 119 + if (counter >= l) { 120 + throw new RangeError('could not decode varint'); 121 + } 122 + 123 + b = buf[counter++]; 124 + res |= (b & REST) << 21; 125 + if (b < MSB) { 126 + return [res, 4]; 127 + } 53 128 54 - let res = 0; 55 - let shift = 0; 56 - let counter = offset; 57 - let b: number; 129 + if (counter >= l) { 130 + throw new RangeError('could not decode varint'); 131 + } 132 + 133 + b = buf[counter++]; 134 + res += (b & REST) * N4; 135 + if (b < MSB) { 136 + return [res, 5]; 137 + } 58 138 139 + let shift = 35; 59 140 do { 60 141 if (counter >= l) { 61 142 throw new RangeError('could not decode varint'); 62 143 } 63 144 64 145 b = buf[counter++]; 65 - res += shift < 28 ? (b & REST) << shift : (b & REST) * Math.pow(2, shift); 146 + res += (b & REST) * 2 ** shift; 66 147 shift += 7; 67 148 } while (b >= MSB); 68 149