Skip to content
Myr

pkt-line.ts

This file

Path

packages/git/src/pkt-line.ts

109 lines · 2.5 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–82why, one step back
1/**
2 * git's pkt-line framing (gitprotocol-common): four hex digits giving the3 * line's length including themselves, then the payload; `0000` is a flush.4 */5import { Option } from "effect";67const encoder = new TextEncoder();89export const FLUSH = encoder.encode("0000");1011export const concat = (parts: readonly Uint8Array[]) => {12 const all = new Uint8Array(parts.reduce((n, part) => n + part.length, 0));13 let at = 0;1415 for (const part of parts) {16 all.set(part, at);17 at += part.length;18 }1920 return all;21};2223export const utf8 = (text: string) => encoder.encode(text);2425export const pkt = (payload: Uint8Array) =>26 concat([27 encoder.encode((payload.length + 4).toString(16).padStart(4, "0")),28 payload,29 ]);3031export type Packet = Uint8Array | "Flush";3233export const packetAt = (34 bytes: Uint8Array,35 at: number36): Option.Option<{ readonly packet: Packet; readonly next: number }> => {37 if (bytes.length < at + 4) {38 return Option.none();39 }4041 const length = Number.parseInt(42 new TextDecoder().decode(bytes.subarray(at, at + 4)),43 1644 );4546 if (length === 0) {47 return Option.some({ next: at + 4, packet: "Flush" });48 }4950 if (Number.isNaN(length) || length < 4) {51 throw new RangeError(`not a pkt-line length at byte ${at}`);52 }5354 return bytes.length < at + length55 ? Option.none()56 : Option.some({57 next: at + length,58 packet: bytes.subarray(at + 4, at + length),59 });60};6162export const packets = (bytes: Uint8Array) => {63 const found: Packet[] = [];64 let at = 0;6566 while (at < bytes.length) {67 const next = packetAt(bytes, at);6869 if (Option.isNone(next)) {70 throw new RangeError(`a pkt-line runs past the end at byte ${at}`);71 }7273 found.push(next.value.packet);74 at = next.value.next;75 }7677 return found;78};7980const LENGTH = /^[0-9a-f]{4}$/u;8182/**

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

lines 83–84why, one step back
83 * Lenient: delimiter, response-end and flush packets carry no line, and
84 * reading stops where the framing breaks. `packets` is the strict reader.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 85–109why, one step back
85 */
86export const textLines = (bytes: Uint8Array): readonly string[] => {87 const decoder = new TextDecoder();88 const lines: string[] = [];89 let at = 0;9091 while (at + 4 <= bytes.length) {92 const hex = decoder.decode(bytes.subarray(at, at + 4));93 const length = Number.parseInt(hex, 16);9495 if (!LENGTH.test(hex) || length === 3 || at + length > bytes.length) {96 break;97 }9899 if (length > 3) {100 lines.push(101 decoder.decode(bytes.subarray(at + 4, at + length)).replace(/\n$/u, "")102 );103 }104105 at += Math.max(length, 4);106 }107108 return lines;109};