Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 27x 27x 2673x 2673x 2673x 524x 2673x 5x 5x 5x 5x 5x 2673x 27x 214x 213x 213x 213x 213x 213x 213x 213x 213x 213x 213x 9231x 4401x 3971x 3971x 3971x 1297x 1297x 1297x 2674x 4401x 4401x 4401x 4401x 213x 213x 213x 213x 213x 1194x 1193x 981x 981x 981x 981x 4830x 4830x 4830x 4830x 4293x 4293x 4293x 3567x 3567x 3567x 3567x 3567x 3567x 4830x 4830x 4830x 212x 212x 213x 213x 213x 212x 212x 212x 212x 212x 212x 212x 2673x 2673x 2673x 2673x 2673x 2673x 2673x 2673x 2673x 2673x 2673x 2673x 2673x 1017x 212x | // Bounds apply to source bytes. Decoding and the final response need bounded
// additional storage; neither retains a reference to a network chunk.
export const TRACE_PAGE_BYTES = 1024 * 1024;
export const TRACE_LINE_BYTES = 16 * 1024;
interface Line {
begin: number;
end: number;
bytes: number;
}
interface ClippedLine {
line: number;
omittedPrefixBytes: number;
omittedSuffixBytes: number;
}
export interface TracePage {
lines: string[];
totalLines: number;
startLine: number;
truncation?: {
reason: 'byte_limits';
maxLineBytes: number;
maxPageBytes: number;
lines: ClippedLine[];
};
}
/** Decode a source interval without inventing characters at clipped UTF-8 edges. */
function decodeFragment(
bytes: Buffer,
prefixClipped: boolean,
suffixClipped: boolean,
decoder: TextDecoder,
) {
let begin = 0;
let end = bytes.length;
if (prefixClipped) {
while (begin < end && (bytes[begin] & 0xc0) === 0x80) begin++;
}
if (suffixClipped && end > begin) {
let lead = end - 1;
while (lead > begin && (bytes[lead] & 0xc0) === 0x80) lead--;
const byte = bytes[lead];
const width =
byte >= 0xc2 && byte <= 0xdf
? 2
: byte >= 0xe0 && byte <= 0xef
? 3
: byte >= 0xf0 && byte <= 0xf4
? 4
: 1;
if (end - lead < width) end = lead;
}
return {
text: decoder.decode(bytes.subarray(begin, end)),
skippedPrefix: begin,
skippedSuffix: bytes.length - end,
};
}
/**
* Scan through EOF for exact line totals. A byte ring bounds retained content,
* and a line ring bounds bookkeeping even for an arbitrarily large negative start.
*/
export async function readTracePage(
body: ReadableStream<Uint8Array> | null,
perPage: number,
start?: number,
): Promise<TracePage> {
if (!body) throw new Error('GitLab trace response has no readable body');
const tail = start === undefined || start < 0;
const capacity = start !== undefined && start < 0 ? Math.min(perPage, -start) : perPage;
const byteCapacity = Math.min(TRACE_PAGE_BYTES, capacity * TRACE_LINE_BYTES);
const storage = Buffer.allocUnsafe(byteCapacity);
const records: Line[] = new Array<Line>(capacity);
let written = 0;
let lineNumber = 0;
let lineBegin = 0;
let lineBytes = 0;
let captured = 0;
function selected() {
return tail || (lineNumber >= start && lineNumber - start < perPage);
}
function finishLine() {
if (selected()) {
const slot = tail ? lineNumber % capacity : lineNumber - start;
// Reuse the line ring's entries instead of allocating for every source line.
const record = records[slot];
if (record) {
record.begin = lineBegin;
record.end = written;
record.bytes = lineBytes;
} else records[slot] = { begin: lineBegin, end: written, bytes: lineBytes };
}
lineNumber++;
lineBegin = written;
lineBytes = 0;
captured = 0;
}
// Undici exposes a byte stream. Reuse its transferred BYOB allocation rather
// than allocating a new body buffer on every read of a large trace.
const reader = body.getReader({ mode: 'byob' });
let readBuffer = new ArrayBuffer(64 * 1024);
let completed = false;
try {
for (;;) {
const { value, done } = await reader.read(new Uint8Array(readBuffer));
if (done) break;
readBuffer = value.buffer;
// A view only: never copy an entire transport chunk, including a huge one.
const chunk = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
let offset = 0;
while (offset < chunk.length) {
const newline = chunk.indexOf(10, offset);
const end = newline === -1 ? chunk.length : newline;
const length = end - offset;
if (selected()) {
let keep = Math.min(length, TRACE_LINE_BYTES - captured);
if (!tail) keep = Math.min(keep, TRACE_PAGE_BYTES - written);
if (keep > 0) {
const at = written % byteCapacity;
const first = Math.min(keep, byteCapacity - at);
chunk.copy(storage, at, offset, offset + first);
if (first < keep) chunk.copy(storage, 0, offset + first, offset + keep);
written += keep;
captured += keep;
}
}
lineBytes += length;
if (newline !== -1) finishLine();
offset = end + 1;
}
}
// String.split('\n') counts one line for empty input and one after a final LF.
finishLine();
completed = true;
} finally {
try {
if (!completed) await reader.cancel();
} finally {
reader.releaseLock();
}
}
const startLine = tail ? Math.max(0, lineNumber - capacity) : start;
const count = Math.max(0, Math.min(perPage, lineNumber - startLine));
const lines: string[] = [];
const clipped: ClippedLine[] = [];
const earliest = tail ? Math.max(0, written - byteCapacity) : 0;
const decoder = new TextDecoder('utf-8', { ignoreBOM: true });
for (let index = 0; index < count; index++) {
const number = startLine + index;
const record = records[tail ? number % capacity : index];
const begin = Math.min(record.end, Math.max(record.begin, earliest));
const length = record.end - begin;
const at = begin % byteCapacity;
const bytes =
at + length <= byteCapacity
? storage.subarray(at, at + length)
: Buffer.concat([storage.subarray(at), storage.subarray(0, length - (byteCapacity - at))]);
const prefix = begin - record.begin;
const suffix = record.bytes - (record.end - record.begin);
const decoded = decodeFragment(bytes, prefix > 0, suffix > 0, decoder);
// Response.text() removes just the initial UTF-8 BOM, not BOMs in later lines.
lines.push(number === 0 && prefix === 0 ? decoded.text.replace(/^\uFEFF/, '') : decoded.text);
const omittedPrefixBytes = prefix + decoded.skippedPrefix;
const omittedSuffixBytes = suffix + decoded.skippedSuffix;
if (omittedPrefixBytes || omittedSuffixBytes) {
clipped.push({ line: number, omittedPrefixBytes, omittedSuffixBytes });
}
}
return {
lines,
totalLines: lineNumber,
startLine,
...(clipped.length
? {
truncation: {
reason: 'byte_limits' as const,
maxLineBytes: TRACE_LINE_BYTES,
maxPageBytes: TRACE_PAGE_BYTES,
lines: clipped,
},
}
: {}),
};
}
|