| 1 |
"use strict"; |
| 2 |
// Copyright 2021-2026 Buf Technologies, Inc. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 16 |
exports.Writer = void 0; |
| 17 |
exports.quoteString = quoteString; |
| 18 |
exports.quoteBytes = quoteBytes; |
| 19 |
const indentUnit = " "; |
| 20 |
/** |
| 21 |
* A writer for the protobuf text format. |
| 22 |
* |
| 23 |
* The Writer owns layout: indentation, line breaks, and braces. Its output |
| 24 |
* matches the default formatting of txtpbfmt and the multi-line output of |
| 25 |
* protobuf-go: two-space indentation, `name: value` with a single space after |
| 26 |
* the colon, submessages as `name: {` with the body indented and `}` aligned |
| 27 |
* under the field name, and a trailing newline. It never inserts the randomized |
| 28 |
* extra spaces that protobuf-go adds to discourage parsing its output as |
| 29 |
* canonical. |
| 30 |
* |
| 31 |
* Output accumulates into a single buffer of lines, so a deep tree costs no |
| 32 |
* more than the bytes it prints. To decide between `name: {}` and an indented |
| 33 |
* block, the caller renders the body, then rolls back with mark()/reset() if it |
| 34 |
* turned out empty — an O(1) decision that needs no separate child buffer. |
| 35 |
*/ |
| 36 |
class Writer { |
| 37 |
constructor() { |
| 38 |
this.chunks = []; |
| 39 |
this.depth = 0; |
| 40 |
} |
| 41 |
/** |
| 42 |
* Write a scalar field: `<indent>name: value` followed by a newline. |
| 43 |
*/ |
| 44 |
scalar(name, value) { |
| 45 |
this.chunks.push(this.indent() + name + ": " + value + "\n"); |
| 46 |
} |
| 47 |
/** |
| 48 |
* Write an empty message field: `<indent>name: {}` followed by a newline. |
| 49 |
*/ |
| 50 |
emptyMessage(name) { |
| 51 |
this.chunks.push(this.indent() + name + ": {}\n"); |
| 52 |
} |
| 53 |
/** |
| 54 |
* Open a message field: `<indent>name: {` followed by a newline, then indent |
| 55 |
* the body. Close it with end(). |
| 56 |
*/ |
| 57 |
openMessage(name) { |
| 58 |
this.chunks.push(this.indent() + name + ": {\n"); |
| 59 |
this.depth++; |
| 60 |
} |
| 61 |
/** |
| 62 |
* Close a message opened with openMessage(): outdent and write `<indent>}` |
| 63 |
* followed by a newline. |
| 64 |
*/ |
| 65 |
end() { |
| 66 |
this.depth--; |
| 67 |
this.chunks.push(this.indent() + "}\n"); |
| 68 |
} |
| 69 |
/** |
| 70 |
* Capture the current output position so it can be rolled back with reset(). |
| 71 |
*/ |
| 72 |
mark() { |
| 73 |
return { size: this.chunks.length, depth: this.depth }; |
| 74 |
} |
| 75 |
/** |
| 76 |
* Roll back to a position captured with mark(). |
| 77 |
*/ |
| 78 |
reset(mark) { |
| 79 |
this.chunks.length = mark.size; |
| 80 |
this.depth = mark.depth; |
| 81 |
} |
| 82 |
/** |
| 83 |
* The number of lines written since the given mark. |
| 84 |
*/ |
| 85 |
writesSince(mark) { |
| 86 |
return this.chunks.length - mark.size; |
| 87 |
} |
| 88 |
toString() { |
| 89 |
return this.chunks.join(""); |
| 90 |
} |
| 91 |
indent() { |
| 92 |
return indentUnit.repeat(this.depth); |
| 93 |
} |
| 94 |
} |
| 95 |
exports.Writer = Writer; |
| 96 |
/** |
| 97 |
* Quote and escape a string field value as a double-quoted text format literal. |
| 98 |
* |
| 99 |
* Uses the single escaping decision in escapeCodePoint, so string fields, bytes |
| 100 |
* fields, and unknown length-delimited rendering can never drift apart. Valid |
| 101 |
* non-ASCII passes through as raw UTF-8; surrogates are never escaped. |
| 102 |
*/ |
| 103 |
function quoteString(value) { |
| 104 |
var _a; |
| 105 |
let out = '"'; |
| 106 |
for (const ch of value) { |
| 107 |
out += (_a = escapeCodePoint(ch.codePointAt(0))) !== null && _a !== void 0 ? _a : ch; |
| 108 |
} |
| 109 |
return out + '"'; |
| 110 |
} |
| 111 |
/** |
| 112 |
* Quote and escape a bytes field value as a double-quoted text format literal. |
| 113 |
* |
| 114 |
* Valid UTF-8 runs are emitted with escapeCodePoint (so they read identically |
| 115 |
* to a string field); any byte that is not part of a valid UTF-8 sequence is |
| 116 |
* emitted as `\xHH`, keeping the output plain ASCII that round-trips exactly. |
| 117 |
*/ |
| 118 |
function quoteBytes(value) { |
| 119 |
var _a; |
| 120 |
let out = '"'; |
| 121 |
for (let i = 0; i < value.length;) { |
| 122 |
const rune = decodeUtf8(value, i); |
| 123 |
if (rune === undefined) { |
| 124 |
out += "\\x" + hex2(value[i]); |
| 125 |
i++; |
| 126 |
continue; |
| 127 |
} |
| 128 |
out += (_a = escapeCodePoint(rune.code)) !== null && _a !== void 0 ? _a : String.fromCodePoint(rune.code); |
| 129 |
i += rune.size; |
| 130 |
} |
| 131 |
return out + '"'; |
| 132 |
} |
| 133 |
/** |
| 134 |
* The single source of truth for escaping a code point in a text format string |
| 135 |
* literal. Returns the escape sequence, or undefined when the code point may be |
| 136 |
* emitted raw. |
| 137 |
* |
| 138 |
* Escapes the conventional sequences, all C0 controls and DEL as `\xHH`, and |
| 139 |
* the C1 controls (U+0080–U+009F) as `\u00HH`. Surrogates and everything else |
| 140 |
* pass through raw. |
| 141 |
*/ |
| 142 |
function escapeCodePoint(code) { |
| 143 |
switch (code) { |
| 144 |
case 0x5c: |
| 145 |
return "\\\\"; |
| 146 |
case 0x22: |
| 147 |
return '\\"'; |
| 148 |
case 0x0a: |
| 149 |
return "\\n"; |
| 150 |
case 0x0d: |
| 151 |
return "\\r"; |
| 152 |
case 0x09: |
| 153 |
return "\\t"; |
| 154 |
} |
| 155 |
if (code < 0x20 || code === 0x7f) { |
| 156 |
return "\\x" + hex2(code); |
| 157 |
} |
| 158 |
if (code >= 0x80 && code <= 0x9f) { |
| 159 |
return "\\u" + code.toString(16).padStart(4, "0"); |
| 160 |
} |
| 161 |
return undefined; |
| 162 |
} |
| 163 |
/** |
| 164 |
* Decode the UTF-8 sequence starting at `offset`, returning the code point and |
| 165 |
* its byte length, or undefined if the bytes there are not valid UTF-8. We |
| 166 |
* decode manually (rather than via TextDecoder) so an invalid byte can be |
| 167 |
* pinpointed and escaped individually. |
| 168 |
*/ |
| 169 |
function decodeUtf8(bytes, offset) { |
| 170 |
const b0 = bytes[offset]; |
| 171 |
if (b0 < 0x80) { |
| 172 |
return { code: b0, size: 1 }; |
| 173 |
} |
| 174 |
if (b0 < 0xc0) { |
| 175 |
return undefined; |
| 176 |
} |
| 177 |
if (b0 < 0xe0) { |
| 178 |
const b1 = bytes[offset + 1]; |
| 179 |
if (!isContinuation(b1)) { |
| 180 |
return undefined; |
| 181 |
} |
| 182 |
const code = ((b0 & 0x1f) << 6) | (b1 & 0x3f); |
| 183 |
return code < 0x80 ? undefined : { code, size: 2 }; |
| 184 |
} |
| 185 |
if (b0 < 0xf0) { |
| 186 |
const b1 = bytes[offset + 1]; |
| 187 |
const b2 = bytes[offset + 2]; |
| 188 |
if (!isContinuation(b1) || !isContinuation(b2)) { |
| 189 |
return undefined; |
| 190 |
} |
| 191 |
const code = ((b0 & 0x0f) << 12) | ((b1 & 0x3f) << 6) | (b2 & 0x3f); |
| 192 |
if (code < 0x800 || (code >= 0xd800 && code <= 0xdfff)) { |
| 193 |
return undefined; |
| 194 |
} |
| 195 |
return { code, size: 3 }; |
| 196 |
} |
| 197 |
if (b0 < 0xf8) { |
| 198 |
const b1 = bytes[offset + 1]; |
| 199 |
const b2 = bytes[offset + 2]; |
| 200 |
const b3 = bytes[offset + 3]; |
| 201 |
if (!isContinuation(b1) || !isContinuation(b2) || !isContinuation(b3)) { |
| 202 |
return undefined; |
| 203 |
} |
| 204 |
const code = ((b0 & 0x07) << 18) | |
| 205 |
((b1 & 0x3f) << 12) | |
| 206 |
((b2 & 0x3f) << 6) | |
| 207 |
(b3 & 0x3f); |
| 208 |
if (code < 0x10000 || code > 0x10ffff) { |
| 209 |
return undefined; |
| 210 |
} |
| 211 |
return { code, size: 4 }; |
| 212 |
} |
| 213 |
return undefined; |
| 214 |
} |
| 215 |
function isContinuation(byte) { |
| 216 |
return byte !== undefined && (byte & 0xc0) === 0x80; |
| 217 |
} |
| 218 |
function hex2(value) { |
| 219 |
return value.toString(16).padStart(2, "0"); |
| 220 |
} |