Prettier shot and add prettier to justfile

This commit is contained in:
Mike Kalange 2024-05-21 16:56:22 -06:00
parent e0603de034
commit 81742efdcb
2 changed files with 132 additions and 72 deletions

View File

@ -1,5 +1,5 @@
NIX := "LD_LIBRARY_PATH='' nix" NIX := "LD_LIBRARY_PATH='' nix"
PROTOBUF_DIR := "$PWD/protobuf" PROTOBUF_DIR := "$PWD/rbproto"
prettier-gql: prettier-gql:
#!/usr/bin/env bash #!/usr/bin/env bash
@ -20,7 +20,7 @@ protobuf-compile-a-python name:
protobuf-compile-a-js name: protobuf-compile-a-js name:
#!/usr/bin/env bash #!/usr/bin/env bash
set -euxo pipefail set -euxo pipefail
{{NIX}} develop --impure --command bash -c 'yarn pbjs {{ PROTOBUF_DIR }}/shot.proto --ts {{ PROTOBUF_DIR }}/ts/shot.ts' {{NIX}} develop --impure --command bash -c 'yarn pbjs {{ PROTOBUF_DIR }}/shot.proto --ts {{ PROTOBUF_DIR }}/ts/shot.ts && yarn prettier ./rbproto --write'
protobuf-compile-all-js: protobuf-compile-all-js:
#!/usr/bin/env bash #!/usr/bin/env bash

View File

@ -346,11 +346,20 @@ function pushTemporaryLength(bb: ByteBuffer): number {
function skipUnknownField(bb: ByteBuffer, type: number): void { function skipUnknownField(bb: ByteBuffer, type: number): void {
switch (type) { switch (type) {
case 0: while (readByte(bb) & 0x80) { } break; case 0:
case 2: skip(bb, readVarint32(bb)); break; while (readByte(bb) & 0x80) {}
case 5: skip(bb, 4); break; break;
case 1: skip(bb, 8); break; case 2:
default: throw new Error("Unimplemented type: " + type); skip(bb, readVarint32(bb));
break;
case 5:
skip(bb, 4);
break;
case 1:
skip(bb, 8);
break;
default:
throw new Error("Unimplemented type: " + type);
} }
} }
@ -366,10 +375,11 @@ function longToString(value: Long): string {
let low = value.low; let low = value.low;
let high = value.high; let high = value.high;
return String.fromCharCode( return String.fromCharCode(
low & 0xFFFF, low & 0xffff,
low >>> 16, low >>> 16,
high & 0xFFFF, high & 0xffff,
high >>> 16); high >>> 16,
);
} }
// The code below was modified from https://github.com/protobufjs/bytebuffer.js // The code below was modified from https://github.com/protobufjs/bytebuffer.js
@ -415,7 +425,7 @@ function toUint8Array(bb: ByteBuffer): Uint8Array {
function skip(bb: ByteBuffer, offset: number): void { function skip(bb: ByteBuffer, offset: number): void {
if (bb.offset + offset > bb.limit) { if (bb.offset + offset > bb.limit) {
throw new Error('Skip past limit'); throw new Error("Skip past limit");
} }
bb.offset += offset; bb.offset += offset;
} }
@ -444,7 +454,7 @@ function grow(bb: ByteBuffer, count: number): number {
function advance(bb: ByteBuffer, count: number): number { function advance(bb: ByteBuffer, count: number): number {
let offset = bb.offset; let offset = bb.offset;
if (offset + count > bb.limit) { if (offset + count > bb.limit) {
throw new Error('Read past limit'); throw new Error("Read past limit");
} }
bb.offset += count; bb.offset += count;
return offset; return offset;
@ -465,11 +475,15 @@ function readString(bb: ByteBuffer, count: number): string {
let offset = advance(bb, count); let offset = advance(bb, count);
let fromCharCode = String.fromCharCode; let fromCharCode = String.fromCharCode;
let bytes = bb.bytes; let bytes = bb.bytes;
let invalid = '\uFFFD'; let invalid = "\uFFFD";
let text = ''; let text = "";
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
let c1 = bytes[i + offset], c2: number, c3: number, c4: number, c: number; let c1 = bytes[i + offset],
c2: number,
c3: number,
c4: number,
c: number;
// 1 byte // 1 byte
if ((c1 & 0x80) === 0) { if ((c1 & 0x80) === 0) {
@ -477,13 +491,13 @@ function readString(bb: ByteBuffer, count: number): string {
} }
// 2 bytes // 2 bytes
else if ((c1 & 0xE0) === 0xC0) { else if ((c1 & 0xe0) === 0xc0) {
if (i + 1 >= count) text += invalid; if (i + 1 >= count) text += invalid;
else { else {
c2 = bytes[i + offset + 1]; c2 = bytes[i + offset + 1];
if ((c2 & 0xC0) !== 0x80) text += invalid; if ((c2 & 0xc0) !== 0x80) text += invalid;
else { else {
c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); c = ((c1 & 0x1f) << 6) | (c2 & 0x3f);
if (c < 0x80) text += invalid; if (c < 0x80) text += invalid;
else { else {
text += fromCharCode(c); text += fromCharCode(c);
@ -494,15 +508,15 @@ function readString(bb: ByteBuffer, count: number): string {
} }
// 3 bytes // 3 bytes
else if ((c1 & 0xF0) == 0xE0) { else if ((c1 & 0xf0) == 0xe0) {
if (i + 2 >= count) text += invalid; if (i + 2 >= count) text += invalid;
else { else {
c2 = bytes[i + offset + 1]; c2 = bytes[i + offset + 1];
c3 = bytes[i + offset + 2]; c3 = bytes[i + offset + 2];
if (((c2 | (c3 << 8)) & 0xC0C0) !== 0x8080) text += invalid; if (((c2 | (c3 << 8)) & 0xc0c0) !== 0x8080) text += invalid;
else { else {
c = ((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F); c = ((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f);
if (c < 0x0800 || (c >= 0xD800 && c <= 0xDFFF)) text += invalid; if (c < 0x0800 || (c >= 0xd800 && c <= 0xdfff)) text += invalid;
else { else {
text += fromCharCode(c); text += fromCharCode(c);
i += 2; i += 2;
@ -512,26 +526,29 @@ function readString(bb: ByteBuffer, count: number): string {
} }
// 4 bytes // 4 bytes
else if ((c1 & 0xF8) == 0xF0) { else if ((c1 & 0xf8) == 0xf0) {
if (i + 3 >= count) text += invalid; if (i + 3 >= count) text += invalid;
else { else {
c2 = bytes[i + offset + 1]; c2 = bytes[i + offset + 1];
c3 = bytes[i + offset + 2]; c3 = bytes[i + offset + 2];
c4 = bytes[i + offset + 3]; c4 = bytes[i + offset + 3];
if (((c2 | (c3 << 8) | (c4 << 16)) & 0xC0C0C0) !== 0x808080) text += invalid; if (((c2 | (c3 << 8) | (c4 << 16)) & 0xc0c0c0) !== 0x808080)
text += invalid;
else { else {
c = ((c1 & 0x07) << 0x12) | ((c2 & 0x3F) << 0x0C) | ((c3 & 0x3F) << 0x06) | (c4 & 0x3F); c =
if (c < 0x10000 || c > 0x10FFFF) text += invalid; ((c1 & 0x07) << 0x12) |
((c2 & 0x3f) << 0x0c) |
((c3 & 0x3f) << 0x06) |
(c4 & 0x3f);
if (c < 0x10000 || c > 0x10ffff) text += invalid;
else { else {
c -= 0x10000; c -= 0x10000;
text += fromCharCode((c >> 10) + 0xD800, (c & 0x3FF) + 0xDC00); text += fromCharCode((c >> 10) + 0xd800, (c & 0x3ff) + 0xdc00);
i += 3; i += 3;
} }
} }
} }
} } else text += invalid;
else text += invalid;
} }
return text; return text;
@ -545,8 +562,8 @@ function writeString(bb: ByteBuffer, text: string): void {
// Write the byte count first // Write the byte count first
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
let c = text.charCodeAt(i); let c = text.charCodeAt(i);
if (c >= 0xD800 && c <= 0xDBFF && i + 1 < n) { if (c >= 0xd800 && c <= 0xdbff && i + 1 < n) {
c = (c << 10) + text.charCodeAt(++i) - 0x35FDC00; c = (c << 10) + text.charCodeAt(++i) - 0x35fdc00;
} }
byteCount += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4; byteCount += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
} }
@ -558,24 +575,24 @@ function writeString(bb: ByteBuffer, text: string): void {
// Then write the bytes // Then write the bytes
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
let c = text.charCodeAt(i); let c = text.charCodeAt(i);
if (c >= 0xD800 && c <= 0xDBFF && i + 1 < n) { if (c >= 0xd800 && c <= 0xdbff && i + 1 < n) {
c = (c << 10) + text.charCodeAt(++i) - 0x35FDC00; c = (c << 10) + text.charCodeAt(++i) - 0x35fdc00;
} }
if (c < 0x80) { if (c < 0x80) {
bytes[offset++] = c; bytes[offset++] = c;
} else { } else {
if (c < 0x800) { if (c < 0x800) {
bytes[offset++] = ((c >> 6) & 0x1F) | 0xC0; bytes[offset++] = ((c >> 6) & 0x1f) | 0xc0;
} else { } else {
if (c < 0x10000) { if (c < 0x10000) {
bytes[offset++] = ((c >> 12) & 0x0F) | 0xE0; bytes[offset++] = ((c >> 12) & 0x0f) | 0xe0;
} else { } else {
bytes[offset++] = ((c >> 18) & 0x07) | 0xF0; bytes[offset++] = ((c >> 18) & 0x07) | 0xf0;
bytes[offset++] = ((c >> 12) & 0x3F) | 0x80; bytes[offset++] = ((c >> 12) & 0x3f) | 0x80;
} }
bytes[offset++] = ((c >> 6) & 0x3F) | 0x80; bytes[offset++] = ((c >> 6) & 0x3f) | 0x80;
} }
bytes[offset++] = (c & 0x3F) | 0x80; bytes[offset++] = (c & 0x3f) | 0x80;
} }
} }
} }
@ -695,7 +712,7 @@ function readVarint32(bb: ByteBuffer): number {
let b: number; let b: number;
do { do {
b = readByte(bb); b = readByte(bb);
if (c < 32) value |= (b & 0x7F) << c; if (c < 32) value |= (b & 0x7f) << c;
c += 7; c += 7;
} while (b & 0x80); } while (b & 0x80);
return value; return value;
@ -716,18 +733,35 @@ function readVarint64(bb: ByteBuffer, unsigned: boolean): Long {
let part2 = 0; let part2 = 0;
let b: number; let b: number;
b = readByte(bb); part0 = (b & 0x7F); if (b & 0x80) { b = readByte(bb);
b = readByte(bb); part0 |= (b & 0x7F) << 7; if (b & 0x80) { part0 = b & 0x7f;
b = readByte(bb); part0 |= (b & 0x7F) << 14; if (b & 0x80) { if (b & 0x80) {
b = readByte(bb); part0 |= (b & 0x7F) << 21; if (b & 0x80) { b = readByte(bb);
part0 |= (b & 0x7f) << 7;
b = readByte(bb); part1 = (b & 0x7F); if (b & 0x80) { if (b & 0x80) {
b = readByte(bb); part1 |= (b & 0x7F) << 7; if (b & 0x80) { b = readByte(bb);
b = readByte(bb); part1 |= (b & 0x7F) << 14; if (b & 0x80) { part0 |= (b & 0x7f) << 14;
b = readByte(bb); part1 |= (b & 0x7F) << 21; if (b & 0x80) { if (b & 0x80) {
b = readByte(bb);
b = readByte(bb); part2 = (b & 0x7F); if (b & 0x80) { part0 |= (b & 0x7f) << 21;
b = readByte(bb); part2 |= (b & 0x7F) << 7; if (b & 0x80) {
b = readByte(bb);
part1 = b & 0x7f;
if (b & 0x80) {
b = readByte(bb);
part1 |= (b & 0x7f) << 7;
if (b & 0x80) {
b = readByte(bb);
part1 |= (b & 0x7f) << 14;
if (b & 0x80) {
b = readByte(bb);
part1 |= (b & 0x7f) << 21;
if (b & 0x80) {
b = readByte(bb);
part2 = b & 0x7f;
if (b & 0x80) {
b = readByte(bb);
part2 |= (b & 0x7f) << 7;
} }
} }
} }
@ -752,30 +786,56 @@ function writeVarint64(bb: ByteBuffer, value: Long): void {
// ref: src/google/protobuf/io/coded_stream.cc // ref: src/google/protobuf/io/coded_stream.cc
let size = let size =
part2 === 0 ? part2 === 0
part1 === 0 ? ? part1 === 0
part0 < 1 << 14 ? ? part0 < 1 << 14
part0 < 1 << 7 ? 1 : 2 : ? part0 < 1 << 7
part0 < 1 << 21 ? 3 : 4 : ? 1
part1 < 1 << 14 ? : 2
part1 < 1 << 7 ? 5 : 6 : : part0 < 1 << 21
part1 < 1 << 21 ? 7 : 8 : ? 3
part2 < 1 << 7 ? 9 : 10; : 4
: part1 < 1 << 14
? part1 < 1 << 7
? 5
: 6
: part1 < 1 << 21
? 7
: 8
: part2 < 1 << 7
? 9
: 10;
let offset = grow(bb, size); let offset = grow(bb, size);
let bytes = bb.bytes; let bytes = bb.bytes;
switch (size) { switch (size) {
case 10: bytes[offset + 9] = (part2 >>> 7) & 0x01; case 10:
case 9: bytes[offset + 8] = size !== 9 ? part2 | 0x80 : part2 & 0x7F; bytes[offset + 9] = (part2 >>> 7) & 0x01;
case 8: bytes[offset + 7] = size !== 8 ? (part1 >>> 21) | 0x80 : (part1 >>> 21) & 0x7F; case 9:
case 7: bytes[offset + 6] = size !== 7 ? (part1 >>> 14) | 0x80 : (part1 >>> 14) & 0x7F; bytes[offset + 8] = size !== 9 ? part2 | 0x80 : part2 & 0x7f;
case 6: bytes[offset + 5] = size !== 6 ? (part1 >>> 7) | 0x80 : (part1 >>> 7) & 0x7F; case 8:
case 5: bytes[offset + 4] = size !== 5 ? part1 | 0x80 : part1 & 0x7F; bytes[offset + 7] =
case 4: bytes[offset + 3] = size !== 4 ? (part0 >>> 21) | 0x80 : (part0 >>> 21) & 0x7F; size !== 8 ? (part1 >>> 21) | 0x80 : (part1 >>> 21) & 0x7f;
case 3: bytes[offset + 2] = size !== 3 ? (part0 >>> 14) | 0x80 : (part0 >>> 14) & 0x7F; case 7:
case 2: bytes[offset + 1] = size !== 2 ? (part0 >>> 7) | 0x80 : (part0 >>> 7) & 0x7F; bytes[offset + 6] =
case 1: bytes[offset] = size !== 1 ? part0 | 0x80 : part0 & 0x7F; size !== 7 ? (part1 >>> 14) | 0x80 : (part1 >>> 14) & 0x7f;
case 6:
bytes[offset + 5] =
size !== 6 ? (part1 >>> 7) | 0x80 : (part1 >>> 7) & 0x7f;
case 5:
bytes[offset + 4] = size !== 5 ? part1 | 0x80 : part1 & 0x7f;
case 4:
bytes[offset + 3] =
size !== 4 ? (part0 >>> 21) | 0x80 : (part0 >>> 21) & 0x7f;
case 3:
bytes[offset + 2] =
size !== 3 ? (part0 >>> 14) | 0x80 : (part0 >>> 14) & 0x7f;
case 2:
bytes[offset + 1] =
size !== 2 ? (part0 >>> 7) | 0x80 : (part0 >>> 7) & 0x7f;
case 1:
bytes[offset] = size !== 1 ? part0 | 0x80 : part0 & 0x7f;
} }
} }