dsh-taskbridge 0.0.0-stage → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +763 -0
- package/LICENSE +21 -0
- package/README.md +622 -3
- package/bin/dsh-delegate-codex-agent.js +115 -0
- package/bin/dsh-delegate-control.d.ts +90 -0
- package/bin/dsh-delegate-control.js +450 -0
- package/bin/dsh-delegate-mcp.d.ts +121 -0
- package/bin/dsh-delegate-mcp.js +273 -0
- package/codex/dsh-taskbridge.toml +247 -0
- package/cordis.patch.yml +6 -0
- package/docs/INTRODUCTION.md +15 -0
- package/docs/INTRODUCTION.zh.md +34 -0
- package/docs/auth-contract.md +138 -0
- package/docs/claude-code.md +31 -0
- package/docs/codex-integration.md +937 -0
- package/docs/contracts-stage-b.md +443 -0
- package/docs/conversation-placement.md +304 -0
- package/docs/plugin-market.md +30 -0
- package/docs/redaction-contract.md +127 -0
- package/docs/runtime-compatibility.md +223 -0
- package/docs/silent-wait.md +191 -0
- package/docs/taskbridge-upgrade.md +28 -0
- package/docs/taskdispatch-upgrade.md +25 -0
- package/docs/worker-permissions.md +347 -0
- package/docs/workspace-contract.md +538 -0
- package/icon.svg +1 -0
- package/lib/client.d.ts +118 -0
- package/lib/client.js +3232 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +138 -4
- package/src/auth/fence.js +361 -0
- package/src/auth/grants.js +789 -0
- package/src/client/api.js +2322 -0
- package/src/client/index.jsx +2333 -0
- package/src/client/styles.js +251 -0
- package/src/codex/installer.js +3299 -0
- package/src/codex/seam.js +429 -0
- package/src/codex/toml.js +784 -0
- package/src/config/defaults.js +164 -0
- package/src/config/redact.js +184 -0
- package/src/config/settings.js +629 -0
- package/src/config/store.js +186 -0
- package/src/config/validate.js +763 -0
- package/src/credentials/seam.js +202 -0
- package/src/http/execution-plane.js +456 -0
- package/src/http/respond.js +175 -0
- package/src/http/user-plane.js +1122 -0
- package/src/index.d.ts +332 -0
- package/src/index.js +657 -0
- package/src/jobs/activity.js +1008 -0
- package/src/jobs/engine.js +3614 -0
- package/src/jobs/history.js +1467 -0
- package/src/jobs/title.js +17 -0
- package/src/mcp/server.js +1821 -0
- package/src/mcp/wait.js +536 -0
- package/src/model/connectivity.js +371 -0
- package/src/model/discovery.js +582 -0
- package/src/model/native-catalog.js +68 -0
- package/src/model/openai-adapter.js +671 -0
- package/src/model/probe.js +264 -0
- package/src/model/providers.js +756 -0
- package/src/model/routing.js +321 -0
- package/src/review/preset.js +868 -0
- package/src/review/prompts.js +126 -0
- package/src/review/reviewer.js +933 -0
- package/src/runtime/agent-runner.js +1740 -0
- package/src/runtime/endpoint-discovery.js +118 -0
- package/src/runtime/permissions.js +1429 -0
- package/src/runtime/sandbox.js +358 -0
- package/src/runtime/session-placement.js +1121 -0
- package/src/workspace/audit.js +775 -0
- package/src/workspace/locks.js +167 -0
- package/src/workspace/preflight.js +424 -0
- package/src/workspace/worktrees.js +613 -0
|
@@ -0,0 +1,784 @@
|
|
|
1
|
+
// Minimal TOML reader plus a *span-aware* editor for one document.
|
|
2
|
+
//
|
|
3
|
+
// Why this file exists
|
|
4
|
+
// --------------------
|
|
5
|
+
// dsh-delegate registers one stdio MCP server in Codex's `config.toml`. That file
|
|
6
|
+
// belongs to the operator and to Codex: it holds model settings, project trust
|
|
7
|
+
// levels, notifications and other MCP servers, usually with explanatory comments.
|
|
8
|
+
// Rewriting it from a parsed object (`JSON.parse`-style round trip) would silently
|
|
9
|
+
// drop every comment and reorder every table. So the editor below works on the
|
|
10
|
+
// original *text*: it locates the byte range of exactly the assignment it owns
|
|
11
|
+
// and replaces that range, leaving every other byte untouched.
|
|
12
|
+
//
|
|
13
|
+
// Scope of the parser
|
|
14
|
+
// -------------------
|
|
15
|
+
// It is a *structure locator*, not a validating TOML implementation. It must be
|
|
16
|
+
// accurate about the things a merge depends on:
|
|
17
|
+
//
|
|
18
|
+
// * where a table header starts and ends (`[a.b]`, `[[a.b]]`),
|
|
19
|
+
// * which keys live in which table, including dotted and quoted keys,
|
|
20
|
+
// * the full extent of a value, including multi-line strings, arrays, inline
|
|
21
|
+
// tables and trailing comments,
|
|
22
|
+
// * which values are a *definition of a table* (`mcp_servers.dsh-delegate` and
|
|
23
|
+
// any child of it), because exactly those are the ones this plugin owns.
|
|
24
|
+
//
|
|
25
|
+
// It deliberately does not evaluate values. `readToml` returns values as their raw
|
|
26
|
+
// text with a small type tag, which is all a diagnostic needs ("is this entry a
|
|
27
|
+
// string where a command must be?"), and `TomlDocument` never re-serializes text
|
|
28
|
+
// it did not itself create.
|
|
29
|
+
//
|
|
30
|
+
// Line endings
|
|
31
|
+
// ------------
|
|
32
|
+
// A Codex `config.toml` touched by a Windows editor is usually CRLF. The locator
|
|
33
|
+
// therefore splits on `\n` only, keeps the `\r` as part of each line, and treats a
|
|
34
|
+
// *terminal* `\r` as a line ending rather than a control character: it is skipped
|
|
35
|
+
// wherever whitespace ends a token, and every mutation re-joins with the document's
|
|
36
|
+
// original ending. A `\r` anywhere else is still a control character and still
|
|
37
|
+
// refuses the document, so mixed or broken files are diagnosed, not rewritten.
|
|
38
|
+
//
|
|
39
|
+
// Any syntax this locator cannot follow raises `TomlSyntaxError` with the 1-based
|
|
40
|
+
// line number. Callers treat that as "refuse to touch the file": editing a
|
|
41
|
+
// document we could not read correctly is how a user's config gets corrupted.
|
|
42
|
+
|
|
43
|
+
/** Raised when the document cannot be located structurally. */
|
|
44
|
+
export class TomlSyntaxError extends Error {
|
|
45
|
+
/**
|
|
46
|
+
* @param {string} message - what went wrong, without quoting file content.
|
|
47
|
+
* @param {number} line - 1-based line number.
|
|
48
|
+
*/
|
|
49
|
+
constructor(message, line) {
|
|
50
|
+
super(`${message} (line ${line})`);
|
|
51
|
+
this.name = 'TomlSyntaxError';
|
|
52
|
+
this.line = line;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Characters that may appear in a bare key. */
|
|
57
|
+
const BARE_KEY = /[A-Za-z0-9_-]/u;
|
|
58
|
+
|
|
59
|
+
/** The trailing byte of a CRLF file, which is a line ending rather than content. */
|
|
60
|
+
const CR = '\r';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Advance a scanner over one line, skipping spaces and a trailing comment.
|
|
64
|
+
*
|
|
65
|
+
* A comment yields a negative sentinel rather than the end offset, so callers can
|
|
66
|
+
* tell "nothing but a comment remains" apart from "this line ended", which are
|
|
67
|
+
* different outcomes: the first is a document with nothing left to read, the
|
|
68
|
+
* second may be a value continuing on the next line. The `\r` of a CRLF line
|
|
69
|
+
* ending is skipped like a space: it is not content.
|
|
70
|
+
* @param {string} line - one line of text.
|
|
71
|
+
* @param {number} index - start offset.
|
|
72
|
+
* @returns {number} offset of the first significant character, or a negative sentinel at a comment.
|
|
73
|
+
*/
|
|
74
|
+
function skipSpaceAndComment(line, index) {
|
|
75
|
+
let cursor = index;
|
|
76
|
+
while (cursor < line.length && (line[cursor] === ' ' || line[cursor] === '\t' || line[cursor] === CR)) cursor += 1;
|
|
77
|
+
if (cursor < line.length && line[cursor] === '#') return -line.length - 1;
|
|
78
|
+
return cursor;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Read one key segment starting at `index`.
|
|
83
|
+
* @param {string} line - line text.
|
|
84
|
+
* @param {number} index - offset of the first character of the segment.
|
|
85
|
+
* @returns {{ key: string, end: number }} decoded key, or throws.
|
|
86
|
+
*/
|
|
87
|
+
function readKeySegment(line, index, lineNumber = 1) {
|
|
88
|
+
const first = line[index];
|
|
89
|
+
if (first === '"' || first === "'") {
|
|
90
|
+
const { value, end } = readBasicOrLiteralString(line, index, lineNumber);
|
|
91
|
+
return { key: value, end };
|
|
92
|
+
}
|
|
93
|
+
let cursor = index;
|
|
94
|
+
while (cursor < line.length && BARE_KEY.test(line[cursor])) cursor += 1;
|
|
95
|
+
if (cursor === index) throw new TomlSyntaxError('a key is malformed', lineNumber);
|
|
96
|
+
return { key: line.slice(index, cursor), end: cursor };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Read a single-line basic (`"…"`) or literal (`'…'`) string.
|
|
101
|
+
* @param {string} line - line text.
|
|
102
|
+
* @param {number} index - offset of the opening quote.
|
|
103
|
+
* @returns {{ value: string, end: number }} decoded value and offset after the closing quote.
|
|
104
|
+
*/
|
|
105
|
+
function readBasicOrLiteralString(line, index, lineNumber = 1) {
|
|
106
|
+
const quote = line[index];
|
|
107
|
+
let cursor = index + 1;
|
|
108
|
+
let out = '';
|
|
109
|
+
while (cursor < line.length) {
|
|
110
|
+
const char = line[cursor];
|
|
111
|
+
if (quote === '"' && char === '\\') {
|
|
112
|
+
const next = line[cursor + 1];
|
|
113
|
+
if (next === undefined) break;
|
|
114
|
+
out += next === 'n' ? '\n' : next === 't' ? '\t' : next === 'r' ? '\r' : next;
|
|
115
|
+
cursor += 2;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (char === quote) return { value: out, end: cursor + 1 };
|
|
119
|
+
out += char;
|
|
120
|
+
cursor += 1;
|
|
121
|
+
}
|
|
122
|
+
throw new TomlSyntaxError('a string is not terminated on this line', lineNumber);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Read a dotted key (one or more segments separated by `.`).
|
|
127
|
+
* @param {string} line - line text.
|
|
128
|
+
* @param {number} index - start offset.
|
|
129
|
+
* @returns {{ path: string[], end: number }} decoded key path and end offset.
|
|
130
|
+
*/
|
|
131
|
+
function readKeyPath(line, index) {
|
|
132
|
+
const path = [];
|
|
133
|
+
let cursor = index;
|
|
134
|
+
for (;;) {
|
|
135
|
+
const segment = readKeySegment(line, cursor);
|
|
136
|
+
path.push(segment.key);
|
|
137
|
+
cursor = segment.end;
|
|
138
|
+
// Inter-segment padding is ordinary whitespace, never a comment: a `#` here
|
|
139
|
+
// would end the line before an `=`, which is a syntax error the caller reports.
|
|
140
|
+
while (cursor < line.length && (line[cursor] === ' ' || line[cursor] === '\t')) cursor += 1;
|
|
141
|
+
if (line[cursor] !== '.') return { path, end: cursor };
|
|
142
|
+
cursor += 1;
|
|
143
|
+
while (cursor < line.length && (line[cursor] === ' ' || line[cursor] === '\t')) cursor += 1;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Locate the end of a table header `[...]` or `[[...]]`.
|
|
149
|
+
* @param {string} line - line text, starting at `[`.
|
|
150
|
+
* @returns {{ path: string[], array: boolean, end: number }} header facts.
|
|
151
|
+
*/
|
|
152
|
+
function readTableHeader(line, index) {
|
|
153
|
+
const array = line.startsWith('[[', index);
|
|
154
|
+
const opener = array ? 2 : 1;
|
|
155
|
+
let cursor = index + opener;
|
|
156
|
+
const path = [];
|
|
157
|
+
/** Skip header padding; a comment cannot occur inside a header. */
|
|
158
|
+
const skip = (from) => {
|
|
159
|
+
let at = from;
|
|
160
|
+
while (at < line.length && (line[at] === ' ' || line[at] === '\t' || line[at] === CR)) at += 1;
|
|
161
|
+
return at;
|
|
162
|
+
};
|
|
163
|
+
for (;;) {
|
|
164
|
+
cursor = skip(cursor);
|
|
165
|
+
const segment = readKeySegment(line, cursor);
|
|
166
|
+
path.push(segment.key);
|
|
167
|
+
cursor = skip(segment.end);
|
|
168
|
+
if (line[cursor] === '.') {
|
|
169
|
+
cursor += 1;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
const closer = array ? ']]' : ']';
|
|
175
|
+
if (!line.startsWith(closer, cursor)) throw new TomlSyntaxError('a table header is not closed', 1);
|
|
176
|
+
const end = cursor + closer.length;
|
|
177
|
+
if (skipSpaceAndComment(line, end) !== line.length) throw new TomlSyntaxError('trailing text after a table header', 1);
|
|
178
|
+
return { path, array, end };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Read the extent of one value that begins on a line.
|
|
183
|
+
*
|
|
184
|
+
* The result is the offset just past the last significant character of the value,
|
|
185
|
+
* before any trailing comment. Multi-line values are consumed by advancing
|
|
186
|
+
* `current`, which is why this takes the document's line array rather than one line.
|
|
187
|
+
* @param {string[]} lines - document lines (no newline characters).
|
|
188
|
+
* @param {number} lineIndex - line the value starts on.
|
|
189
|
+
* @param {number} index - offset inside that line where the value starts.
|
|
190
|
+
* @returns {{ endLine: number, endColumn: number }} position just past the value.
|
|
191
|
+
*/
|
|
192
|
+
function readValueExtent(lines, lineIndex, index) {
|
|
193
|
+
const first = lines[lineIndex];
|
|
194
|
+
// Bracket depth of the *outer* value only. Parentheses are not TOML brackets, so
|
|
195
|
+
// they are ordinary characters here; guessing otherwise would swallow following
|
|
196
|
+
// text on a malformed document.
|
|
197
|
+
let depth = 0;
|
|
198
|
+
let cursor = index;
|
|
199
|
+
let line = first;
|
|
200
|
+
let current = lineIndex;
|
|
201
|
+
let quote = null;
|
|
202
|
+
let triple = false;
|
|
203
|
+
for (;;) {
|
|
204
|
+
if (cursor >= line.length) {
|
|
205
|
+
if (quote !== null) {
|
|
206
|
+
// Only a multi-line string continues past the end of a line; a single-line
|
|
207
|
+
// string or an unterminated array is a syntax error the locator must not
|
|
208
|
+
// guess about.
|
|
209
|
+
if (!triple) throw new TomlSyntaxError('a string is not terminated', current + 1);
|
|
210
|
+
current += 1;
|
|
211
|
+
if (current >= lines.length) throw new TomlSyntaxError('a multi-line string is not terminated', lineIndex + 1);
|
|
212
|
+
line = lines[current];
|
|
213
|
+
cursor = 0;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (depth > 0) {
|
|
217
|
+
current += 1;
|
|
218
|
+
if (current >= lines.length) throw new TomlSyntaxError('a value is not closed', lineIndex + 1);
|
|
219
|
+
line = lines[current];
|
|
220
|
+
cursor = 0;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
return { endLine: current, endColumn: cursor };
|
|
224
|
+
}
|
|
225
|
+
const char = line[cursor];
|
|
226
|
+
if (quote === null) {
|
|
227
|
+
// Outside a string, whitespace with nothing open ends the value, and a `#`
|
|
228
|
+
// with nothing open starts a trailing comment — except inside an open array,
|
|
229
|
+
// where a comment sits *between elements* and the value continues. A `\r` only
|
|
230
|
+
// ever occurs at the end of a line, so reaching one here means the value ended
|
|
231
|
+
// and the CRLF line ending begins.
|
|
232
|
+
if (depth === 0 && (char === '#' || char === ' ' || char === '\t' || char === CR)) {
|
|
233
|
+
return lastSignificant(lines, index, current, cursor);
|
|
234
|
+
}
|
|
235
|
+
if (char === '#' && depth > 0) {
|
|
236
|
+
cursor = line.length;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (quote !== null) {
|
|
241
|
+
if (triple) {
|
|
242
|
+
if (char === '\\' && quote === '"') {
|
|
243
|
+
cursor += 2;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (line.startsWith(quote.repeat(3), cursor)) {
|
|
247
|
+
cursor += 3;
|
|
248
|
+
// A basic multi-line string may end with up to two extra quotes: in
|
|
249
|
+
// `"""a""""` the fourth quote belongs to the value, not to the delimiter.
|
|
250
|
+
if (quote === '"' && line[cursor] === '"') cursor += 1;
|
|
251
|
+
quote = null;
|
|
252
|
+
triple = false;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
cursor += 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (quote === '"' && char === '\\') {
|
|
259
|
+
cursor += 2;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (char === quote) {
|
|
263
|
+
quote = null;
|
|
264
|
+
cursor += 1;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
cursor += 1;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (char === '#') {
|
|
271
|
+
if (depth === 0) {
|
|
272
|
+
// The value ended at its last significant character, which may be on an
|
|
273
|
+
// earlier line of a multi-line array. A `#` inside an open array is a
|
|
274
|
+
// comment *between elements* and the value continues on the next line.
|
|
275
|
+
return lastSignificant(lines, index, current, cursor);
|
|
276
|
+
}
|
|
277
|
+
// Skip a comment between array elements; it is not part of the structure but
|
|
278
|
+
// stays inside the entry's line range, so an edit preserves it.
|
|
279
|
+
cursor = line.length;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (char === '"' || char === "'") {
|
|
283
|
+
const nextTriple = line.startsWith(char.repeat(3), cursor);
|
|
284
|
+
quote = char;
|
|
285
|
+
triple = nextTriple;
|
|
286
|
+
cursor += nextTriple ? 3 : 1;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (char === '[' || char === '{') {
|
|
290
|
+
depth += 1;
|
|
291
|
+
cursor += 1;
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
if (char === ']' || char === '}') {
|
|
295
|
+
depth -= 1;
|
|
296
|
+
if (depth < 0) throw new TomlSyntaxError('a value has an unbalanced bracket', current + 1);
|
|
297
|
+
cursor += 1;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (depth === 0 && (char === ' ' || char === '\t' || char === CR)) {
|
|
301
|
+
// Trailing whitespace (or a CRLF line ending) after a complete value ends it.
|
|
302
|
+
return lastSignificant(lines, index, current, cursor);
|
|
303
|
+
}
|
|
304
|
+
cursor += 1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Walk backwards from a stopping offset to the last non-space character.
|
|
310
|
+
*
|
|
311
|
+
* Used so the replacement range never swallows the whitespace before a trailing
|
|
312
|
+
* comment: `key = "x" # note` keeps its ` # note`. The `\r` of a CRLF ending is
|
|
313
|
+
* not part of the value either, so a multi-line array in a CRLF file does not keep
|
|
314
|
+
* a stray carriage return inside its raw text.
|
|
315
|
+
* @param {string[]} lines - document lines.
|
|
316
|
+
* @param {number} startColumn - column the value starts on its first line.
|
|
317
|
+
* @param {number} stopLine - line the scan stopped on.
|
|
318
|
+
* @param {number} stopColumn - column the scan stopped at.
|
|
319
|
+
* @returns {{ endLine: number, endColumn: number }} position just past the value.
|
|
320
|
+
*/
|
|
321
|
+
function lastSignificant(lines, startColumn, stopLine, stopColumn) {
|
|
322
|
+
let lineIndex = stopLine;
|
|
323
|
+
let column = stopColumn;
|
|
324
|
+
for (;;) {
|
|
325
|
+
while (column > 0 && (lines[lineIndex][column - 1] === ' ' || lines[lineIndex][column - 1] === '\t' || lines[lineIndex][column - 1] === CR)) {
|
|
326
|
+
column -= 1;
|
|
327
|
+
}
|
|
328
|
+
if (column > 0) return { endLine: lineIndex, endColumn: column };
|
|
329
|
+
lineIndex -= 1;
|
|
330
|
+
if (lineIndex < 0) throw new TomlSyntaxError('a value is missing', stopLine + 1);
|
|
331
|
+
column = lines[lineIndex].length;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* One assignment or table header located in the source text.
|
|
337
|
+
* @typedef {object} TomlEntry
|
|
338
|
+
* @property {'table' | 'array-table' | 'assignment'} kind - entry kind.
|
|
339
|
+
* @property {string[]} path - full key path (table path, plus the key for an assignment).
|
|
340
|
+
* @property {string} [raw] - raw value text of an assignment.
|
|
341
|
+
* @property {number} startLine - first line (0-based).
|
|
342
|
+
* @property {number} endLine - last line (0-based).
|
|
343
|
+
* @property {number} startColumn - column of the first character.
|
|
344
|
+
* @property {number} endColumn - column just past the last character.
|
|
345
|
+
* @property {string[]} [leadingCommentLines] - contiguous comment lines directly above.
|
|
346
|
+
*/
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* The line ending a document already uses.
|
|
350
|
+
*
|
|
351
|
+
* CRLF is chosen when the document has at least one line ending and *none* of them
|
|
352
|
+
* is a bare LF — the same rule `finalNewline` uses, applied to the joiner. Mixed
|
|
353
|
+
* endings therefore stay LF, which is the conservative outcome: the file is written
|
|
354
|
+
* the way it mostly already was rather than converted wholesale.
|
|
355
|
+
* @param {string} text - document text.
|
|
356
|
+
* @returns {string} `'\r\n'` or `'\n'`.
|
|
357
|
+
*/
|
|
358
|
+
export function detectEol(text) {
|
|
359
|
+
if (typeof text !== 'string') return '\n';
|
|
360
|
+
// Every `\n` that is not preceded by a `\r` would be a bare LF. Splitting on `\n`
|
|
361
|
+
// and checking each line's predecessor is the cheapest exact test.
|
|
362
|
+
const lines = text.split('\n');
|
|
363
|
+
let crlf = 0;
|
|
364
|
+
let lf = 0;
|
|
365
|
+
for (let index = 0; index < lines.length - 1; index += 1) {
|
|
366
|
+
if (lines[index].endsWith('\r')) crlf += 1;
|
|
367
|
+
else lf += 1;
|
|
368
|
+
}
|
|
369
|
+
return crlf > 0 && lf === 0 ? '\r\n' : '\n';
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Parse a document into entries by source range.
|
|
374
|
+
*
|
|
375
|
+
* Only structure is decoded; comments and blank lines inside values are kept by
|
|
376
|
+
* the range logic, and nothing is normalized. The document's line ending is
|
|
377
|
+
* detected once and reported as `eol`, so an edit can reproduce it byte for byte.
|
|
378
|
+
* @param {string} text - TOML document.
|
|
379
|
+
* @returns {{ entries: TomlEntry[], lines: string[], finalNewline: boolean, eol: string }} structure.
|
|
380
|
+
* @throws {TomlSyntaxError} when the document cannot be located.
|
|
381
|
+
*/
|
|
382
|
+
export function parseTomlStructure(text) {
|
|
383
|
+
if (typeof text !== 'string') throw new TypeError('parseTomlStructure expects text');
|
|
384
|
+
const finalNewline = text.endsWith('\n');
|
|
385
|
+
const eol = detectEol(text);
|
|
386
|
+
const lines = text.split('\n');
|
|
387
|
+
if (finalNewline) lines.pop();
|
|
388
|
+
/** @type {TomlEntry[]} */
|
|
389
|
+
const entries = [];
|
|
390
|
+
let currentPath = [];
|
|
391
|
+
let lineIndex = 0;
|
|
392
|
+
while (lineIndex < lines.length) {
|
|
393
|
+
const line = lines[lineIndex];
|
|
394
|
+
// A raw control character (tab excepted) is not valid TOML text. A terminal CR
|
|
395
|
+
// is exempt: it is the second byte of a CRLF line ending, which Windows editors
|
|
396
|
+
// produce routinely. A CR anywhere else is still refused, so a document with
|
|
397
|
+
// mixed or broken endings is diagnosed instead of silently rewritten.
|
|
398
|
+
const content = line.endsWith(CR) ? line.slice(0, -1) : line;
|
|
399
|
+
if (/[\u0000-\u0008\u000a-\u001f]/u.test(content)) {
|
|
400
|
+
throw new TomlSyntaxError('the document contains a control character', lineIndex + 1);
|
|
401
|
+
}
|
|
402
|
+
const start = skipSpaceAndComment(line, 0);
|
|
403
|
+
// A blank line, or a line holding only a comment, produces the negative
|
|
404
|
+
// "nothing significant here" sentinel.
|
|
405
|
+
if (start < 0 || start >= line.length) {
|
|
406
|
+
lineIndex += 1;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (line[start] === '[') {
|
|
410
|
+
const header = readTableHeader(line, start);
|
|
411
|
+
currentPath = header.path;
|
|
412
|
+
entries.push({
|
|
413
|
+
kind: header.array ? 'array-table' : 'table',
|
|
414
|
+
path: [...header.path],
|
|
415
|
+
startLine: lineIndex,
|
|
416
|
+
endLine: lineIndex,
|
|
417
|
+
startColumn: start,
|
|
418
|
+
endColumn: header.end,
|
|
419
|
+
});
|
|
420
|
+
lineIndex += 1;
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
const key = readKeyPath(line, start);
|
|
424
|
+
// `readKeyPath` stops at the first significant character that cannot continue
|
|
425
|
+
// the key, which for a well-formed assignment is the `=` itself.
|
|
426
|
+
if (line[key.end] !== '=') throw new TomlSyntaxError('a key is not followed by "="', lineIndex + 1);
|
|
427
|
+
const valueStart = skipSpaceAndComment(line, key.end + 1);
|
|
428
|
+
if (valueStart < 0) throw new TomlSyntaxError('a value is missing', lineIndex + 1);
|
|
429
|
+
const extent = readValueExtent(lines, lineIndex, valueStart);
|
|
430
|
+
// The value's end column only has meaning on its last line; a single-line value
|
|
431
|
+
// spans one line, a multi-line value always ends at the end of its last line.
|
|
432
|
+
const endColumn = extent.endLine === lineIndex ? extent.endColumn : lines[extent.endLine].length;
|
|
433
|
+
// A multiline value is joined with plain `\n`, not the document's ending: `raw`
|
|
434
|
+
// is a rendering of the value, never a byte range that could be written back.
|
|
435
|
+
// Each line's own CR (present in a CRLF file) is dropped for the same reason —
|
|
436
|
+
// the CR is a line ending, not part of the value.
|
|
437
|
+
const rawParts = extent.endLine === lineIndex
|
|
438
|
+
? [lines[lineIndex].slice(valueStart, extent.endColumn)]
|
|
439
|
+
: [lines[lineIndex].slice(valueStart), ...lines.slice(lineIndex + 1, extent.endLine + 1)];
|
|
440
|
+
const raw = rawParts.map((part) => (part.endsWith('\r') ? part.slice(0, -1) : part)).join('\n');
|
|
441
|
+
entries.push({
|
|
442
|
+
kind: 'assignment',
|
|
443
|
+
path: [...currentPath, ...key.path],
|
|
444
|
+
raw,
|
|
445
|
+
startLine: lineIndex,
|
|
446
|
+
endLine: extent.endLine,
|
|
447
|
+
startColumn: start,
|
|
448
|
+
endColumn,
|
|
449
|
+
});
|
|
450
|
+
lineIndex = extent.endLine + 1;
|
|
451
|
+
}
|
|
452
|
+
return { entries, lines, finalNewline, eol };
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Read a bounded subset of the document as data, for diagnostics only.
|
|
457
|
+
*
|
|
458
|
+
* Values are returned as `{ type, text }` pairs: `type` is the TOML type name this
|
|
459
|
+
* reader recognizes (`string`, `integer`, `float`, `boolean`, `array`, `table`,
|
|
460
|
+
* `datetime`) and `text` is the raw source. No value is ever re-serialized from
|
|
461
|
+
* this representation, so the reader cannot be a lossy path back to disk.
|
|
462
|
+
* @param {string} text - TOML document.
|
|
463
|
+
* @returns {Record<string, { type: string, text: string }>} flat dotted-path map.
|
|
464
|
+
*/
|
|
465
|
+
export function readTomlValues(text) {
|
|
466
|
+
const { entries } = parseTomlStructure(text);
|
|
467
|
+
/** @type {Record<string, { type: string, text: string }>} */
|
|
468
|
+
const values = {};
|
|
469
|
+
for (const entry of entries) {
|
|
470
|
+
if (entry.kind !== 'assignment') continue;
|
|
471
|
+
values[entry.path.join('.')] = { type: tomlValueType(entry.raw), text: entry.raw };
|
|
472
|
+
}
|
|
473
|
+
return values;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Classify a raw TOML value.
|
|
478
|
+
* @param {string} raw - raw value text.
|
|
479
|
+
* @returns {string} type name.
|
|
480
|
+
*/
|
|
481
|
+
export function tomlValueType(raw) {
|
|
482
|
+
const text = typeof raw === 'string' ? raw.trim() : '';
|
|
483
|
+
if (text === '') return 'empty';
|
|
484
|
+
if (text.startsWith('"""') || text.startsWith("'''")) return 'string';
|
|
485
|
+
if (text.startsWith('"') || text.startsWith("'")) return 'string';
|
|
486
|
+
if (text.startsWith('[')) return 'array';
|
|
487
|
+
if (text.startsWith('{')) return 'table';
|
|
488
|
+
if (text === 'true' || text === 'false') return 'boolean';
|
|
489
|
+
if (/^[+-]?\d[\d_]*$/u.test(text)) return 'integer';
|
|
490
|
+
if (/^[+-]?(?:\d[\d_]*)?\.\d[\d_]*(?:[eE][+-]?\d+)?$/u.test(text)) return 'float';
|
|
491
|
+
if (/^\d{4}-\d{2}-\d{2}/u.test(text) || /^\d{2}:\d{2}:\d{2}/u.test(text)) return 'datetime';
|
|
492
|
+
return 'unknown';
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* A document being edited in place, one owned region at a time.
|
|
497
|
+
*
|
|
498
|
+
* Every mutation works on line/column ranges in the current text, so unrelated
|
|
499
|
+
* content (other tables, comments, blank lines, ordering) survives verbatim.
|
|
500
|
+
*/
|
|
501
|
+
export class TomlDocument {
|
|
502
|
+
/**
|
|
503
|
+
* @param {string} text - original document.
|
|
504
|
+
*/
|
|
505
|
+
constructor(text) {
|
|
506
|
+
this.text = typeof text === 'string' ? text : '';
|
|
507
|
+
this.reparse();
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** Re-read the structure after an edit. */
|
|
511
|
+
reparse() {
|
|
512
|
+
const structure = parseTomlStructure(this.text);
|
|
513
|
+
this.entries = structure.entries;
|
|
514
|
+
this.lines = structure.lines;
|
|
515
|
+
this.finalNewline = structure.finalNewline;
|
|
516
|
+
this.eol = structure.eol;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* The document text for a line array, using the document's own line endings.
|
|
521
|
+
*
|
|
522
|
+
* Every mutation goes through this, which is what makes an edit to a CRLF file
|
|
523
|
+
* stay a CRLF file. The line array keeps each trailing `\r`, so the `\r` is
|
|
524
|
+
* stripped here before the document's own ending is applied — otherwise the result
|
|
525
|
+
* would carry `\r\r\n` and the re-parse would (correctly) reject the stray CR.
|
|
526
|
+
* @param {string[]} lines - lines to join.
|
|
527
|
+
* @param {boolean} [trailing] - whether to end with a line ending.
|
|
528
|
+
* @returns {string} text.
|
|
529
|
+
*/
|
|
530
|
+
join(lines, trailing = true) {
|
|
531
|
+
const eol = this.eol ?? '\n';
|
|
532
|
+
const body = lines.map((line) => (line.endsWith('\r') ? line.slice(0, -1) : line)).join(eol);
|
|
533
|
+
return trailing ? `${body}${eol}` : body;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Every assignment whose path is `prefix` or a child of it.
|
|
538
|
+
* @param {string[]} prefix - key path.
|
|
539
|
+
* @returns {TomlEntry[]} matching assignments.
|
|
540
|
+
*/
|
|
541
|
+
assignmentsUnder(prefix) {
|
|
542
|
+
return this.entries.filter((entry) => entry.kind === 'assignment' && startsWithPath(entry.path, prefix));
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Every table header whose path is `prefix` or a child of it.
|
|
547
|
+
* @param {string[]} prefix - key path.
|
|
548
|
+
* @returns {TomlEntry[]} matching table headers.
|
|
549
|
+
*/
|
|
550
|
+
tablesUnder(prefix) {
|
|
551
|
+
return this.entries.filter((entry) => entry.kind !== 'assignment' && startsWithPath(entry.path, prefix));
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* The assignment at exactly `path`, if present.
|
|
556
|
+
* @param {string[]} path - key path.
|
|
557
|
+
* @returns {TomlEntry | undefined} entry.
|
|
558
|
+
*/
|
|
559
|
+
assignmentAt(path) {
|
|
560
|
+
return this.entries.find((entry) => entry.kind === 'assignment' && samePath(entry.path, path));
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Replace one line range with new lines, keeping the trailing newline policy.
|
|
565
|
+
* @param {number} startLine - first line to replace (0-based, inclusive).
|
|
566
|
+
* @param {number} endLine - last line to replace (0-based, inclusive).
|
|
567
|
+
* @param {string[]} replacement - new lines.
|
|
568
|
+
* @returns {void}
|
|
569
|
+
*/
|
|
570
|
+
replaceLines(startLine, endLine, replacement) {
|
|
571
|
+
const next = [...this.lines.slice(0, startLine), ...replacement, ...this.lines.slice(endLine + 1)];
|
|
572
|
+
this.text = this.join(next, this.finalNewline || next.length > 0);
|
|
573
|
+
this.reparse();
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Append lines at the end of the document, separated by exactly one blank line.
|
|
578
|
+
* @param {string[]} newLines - lines to append.
|
|
579
|
+
* @returns {void}
|
|
580
|
+
*/
|
|
581
|
+
appendBlock(newLines) {
|
|
582
|
+
const base = this.lines.slice();
|
|
583
|
+
while (base.length > 0 && base[base.length - 1].trim() === '') base.pop();
|
|
584
|
+
const combined = base.length === 0 ? newLines.slice() : [...base, '', ...newLines];
|
|
585
|
+
this.text = this.join(combined);
|
|
586
|
+
this.reparse();
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Replace the raw value of one assignment, keeping its key and indentation.
|
|
591
|
+
* @param {TomlEntry} entry - assignment entry.
|
|
592
|
+
* @param {string} rawValue - replacement value text.
|
|
593
|
+
* @returns {void}
|
|
594
|
+
*/
|
|
595
|
+
replaceValue(entry, rawValue) {
|
|
596
|
+
const lines = this.lines.slice();
|
|
597
|
+
const first = lines[entry.startLine];
|
|
598
|
+
// Keep the key exactly as the operator wrote it — indentation included — and
|
|
599
|
+
// keep whatever followed the old value on its last line (a trailing comment).
|
|
600
|
+
const equals = first.indexOf('=', entry.startColumn - 1);
|
|
601
|
+
const prefix = first.slice(0, equals + 1);
|
|
602
|
+
const trailing = entry.endLine === entry.startLine
|
|
603
|
+
? first.slice(entry.endColumn)
|
|
604
|
+
: lines[entry.endLine].slice(entry.endColumn);
|
|
605
|
+
const head = `${prefix} ${rawValue}${trailing}`;
|
|
606
|
+
if (entry.endLine === entry.startLine) lines[entry.startLine] = head;
|
|
607
|
+
else lines.splice(entry.startLine, entry.endLine - entry.startLine + 1, head);
|
|
608
|
+
this.text = this.join(lines);
|
|
609
|
+
this.reparse();
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Delete a contiguous run of lines, dropping the blank line it leaves behind.
|
|
614
|
+
* @param {number} startLine - first line (0-based).
|
|
615
|
+
* @param {number} endLine - last line (0-based).
|
|
616
|
+
* @returns {void}
|
|
617
|
+
*/
|
|
618
|
+
removeLines(startLine, endLine) {
|
|
619
|
+
const lines = this.lines.slice();
|
|
620
|
+
lines.splice(startLine, endLine - startLine + 1);
|
|
621
|
+
// Collapse the blank line a removal leaves behind, so the surrounding document
|
|
622
|
+
// keeps its spacing rather than growing a gap. Whichever side of the hole is
|
|
623
|
+
// blank is dropped; when both are, exactly one blank line survives, which is the
|
|
624
|
+
// separator the document already had.
|
|
625
|
+
const before = lines[startLine - 1];
|
|
626
|
+
const after = lines[startLine];
|
|
627
|
+
if (startLine > 0 && before.trim() === '' && after !== undefined && after.trim() === '') {
|
|
628
|
+
lines.splice(startLine, 1);
|
|
629
|
+
} else if (startLine > 0 && before.trim() === '' && (after === undefined || after.trim() !== '')) {
|
|
630
|
+
lines.splice(startLine - 1, 1);
|
|
631
|
+
}
|
|
632
|
+
while (lines.length > 0 && lines[lines.length - 1].trim() === '') lines.pop();
|
|
633
|
+
this.text = lines.length === 0 ? '' : this.join(lines);
|
|
634
|
+
this.reparse();
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* The comment lines directly above an entry, without a blank line between.
|
|
639
|
+
* @param {TomlEntry} entry - entry.
|
|
640
|
+
* @returns {number} first line of that comment block.
|
|
641
|
+
*/
|
|
642
|
+
leadingCommentStart(entry) {
|
|
643
|
+
let cursor = entry.startLine - 1;
|
|
644
|
+
while (cursor >= 0 && this.lines[cursor].trimStart().startsWith('#')) cursor -= 1;
|
|
645
|
+
return cursor + 1;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* The line range of one table header and everything that belongs to it.
|
|
650
|
+
*
|
|
651
|
+
* The range ends at the last line before the next header at the same or a
|
|
652
|
+
* shallower depth — *not* at the end of the document — so deleting a table never
|
|
653
|
+
* takes a sibling with it. Trailing blank lines inside the range are trimmed so a
|
|
654
|
+
* removal does not leave a growing gap.
|
|
655
|
+
* @param {number} headerIndex - index into {@link TomlDocument#entries}.
|
|
656
|
+
* @returns {{ startLine: number, endLine: number }} inclusive line range, including the comment block above the header.
|
|
657
|
+
*/
|
|
658
|
+
tableRange(headerIndex) {
|
|
659
|
+
const header = this.entries[headerIndex];
|
|
660
|
+
if (header === undefined || header.kind === 'assignment') throw new TypeError('tableRange expects a table header');
|
|
661
|
+
const end = this.tableBodyEnd(headerIndex);
|
|
662
|
+
return { startLine: this.leadingCommentStart(header), endLine: end };
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* The last line that belongs to one table: its body and child tables, not siblings.
|
|
667
|
+
* @param {number} headerIndex - index into {@link TomlDocument#entries}.
|
|
668
|
+
* @returns {number} last owned line, 0-based, trailing blank lines already trimmed.
|
|
669
|
+
*/
|
|
670
|
+
tableBodyEnd(headerIndex) {
|
|
671
|
+
const header = this.entries[headerIndex];
|
|
672
|
+
let end = this.lines.length - 1;
|
|
673
|
+
for (let index = headerIndex + 1; index < this.entries.length; index += 1) {
|
|
674
|
+
const entry = this.entries[index];
|
|
675
|
+
if (entry.kind === 'assignment') continue;
|
|
676
|
+
// A sibling (`[mcp_servers.other]`) or a shallower header (`[other]`) closes
|
|
677
|
+
// this table. A deeper one (`[mcp_servers.dsh-delegate.env]`) is its child
|
|
678
|
+
// and is owned by it.
|
|
679
|
+
if (entry.path.length <= header.path.length) {
|
|
680
|
+
end = entry.startLine - 1;
|
|
681
|
+
break;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
while (end > header.startLine && this.lines[end].trim() === '') end -= 1;
|
|
685
|
+
return end;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Is this line a comment carrying `marker`?
|
|
690
|
+
*
|
|
691
|
+
* Matching is exact after trimming: a line that merely mentions the marker text
|
|
692
|
+
* inside a longer comment is not the marker the plugin wrote.
|
|
693
|
+
* @param {number} lineIndex - line to test.
|
|
694
|
+
* @param {string} marker - marker comment, including its `#`.
|
|
695
|
+
* @returns {boolean} true when the line is that marker, alone.
|
|
696
|
+
*/
|
|
697
|
+
isMarkerLine(lineIndex, marker) {
|
|
698
|
+
const line = this.lines[lineIndex];
|
|
699
|
+
if (typeof line !== 'string') return false;
|
|
700
|
+
return line.trim() === marker.trim();
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* The line range of one table header together with *its own* marker comment only.
|
|
705
|
+
*
|
|
706
|
+
* Deliberately narrower than {@link TomlDocument#tableRange}: a comment block the
|
|
707
|
+
* operator wrote above the header may explain their own settings, and a removal
|
|
708
|
+
* must not take it. Only the exact plugin marker is owned, and only when it is the
|
|
709
|
+
* last comment line before the header.
|
|
710
|
+
* @param {number} headerIndex - index into {@link TomlDocument#entries}.
|
|
711
|
+
* @param {string} marker - the plugin's marker comment.
|
|
712
|
+
* @returns {{ startLine: number, endLine: number }} inclusive range.
|
|
713
|
+
*/
|
|
714
|
+
markedTableRange(headerIndex, marker) {
|
|
715
|
+
const header = this.entries[headerIndex];
|
|
716
|
+
let startLine = header.startLine;
|
|
717
|
+
if (this.isMarkerLine(startLine - 1, marker)) startLine -= 1;
|
|
718
|
+
return { startLine, endLine: this.tableBodyEnd(headerIndex) };
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* Is `path` equal to `prefix` or below it?
|
|
724
|
+
* @param {string[]} path - candidate path.
|
|
725
|
+
* @param {string[]} prefix - prefix path.
|
|
726
|
+
* @returns {boolean} true when the prefix matches.
|
|
727
|
+
*/
|
|
728
|
+
export function startsWithPath(path, prefix) {
|
|
729
|
+
if (path.length < prefix.length) return false;
|
|
730
|
+
return prefix.every((segment, index) => path[index] === segment);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Are two key paths identical?
|
|
735
|
+
* @param {string[]} left - first path.
|
|
736
|
+
* @param {string[]} right - second path.
|
|
737
|
+
* @returns {boolean} true when equal.
|
|
738
|
+
*/
|
|
739
|
+
export function samePath(left, right) {
|
|
740
|
+
return left.length === right.length && left.every((segment, index) => segment === right[index]);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Render a key path as a TOML bare/dotted key, quoting only where required.
|
|
745
|
+
* @param {string[]} path - key path.
|
|
746
|
+
* @returns {string} rendered key.
|
|
747
|
+
*/
|
|
748
|
+
export function renderKey(path) {
|
|
749
|
+
return path.map((segment) => (/^[A-Za-z0-9_-]+$/u.test(segment) ? segment : renderString(segment))).join('.');
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Render a TOML basic string.
|
|
754
|
+
*
|
|
755
|
+
* Windows paths appear in this file constantly (`node.exe`), and TOML basic
|
|
756
|
+
* strings treat `\` as an escape, so a raw path would corrupt the document. The
|
|
757
|
+
* writer always emits basic strings with escaped backslashes, which both TOML and
|
|
758
|
+
* Codex read back as the intended path.
|
|
759
|
+
* @param {string} value - string to render.
|
|
760
|
+
* @returns {string} quoted, escaped TOML string.
|
|
761
|
+
*/
|
|
762
|
+
export function renderString(value) {
|
|
763
|
+
const text = String(value);
|
|
764
|
+
let out = '"';
|
|
765
|
+
for (const char of text) {
|
|
766
|
+
if (char === '\\') out += '\\\\';
|
|
767
|
+
else if (char === '"') out += '\\"';
|
|
768
|
+
else if (char === '\n') out += '\\n';
|
|
769
|
+
else if (char === '\r') out += '\\r';
|
|
770
|
+
else if (char === '\t') out += '\\t';
|
|
771
|
+
else if (char.codePointAt(0) < 0x20) out += `\\u${char.codePointAt(0).toString(16).padStart(4, '0')}`;
|
|
772
|
+
else out += char;
|
|
773
|
+
}
|
|
774
|
+
return `${out}"`;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Render an array of strings.
|
|
779
|
+
* @param {string[]} values - strings.
|
|
780
|
+
* @returns {string} TOML array.
|
|
781
|
+
*/
|
|
782
|
+
export function renderStringArray(values) {
|
|
783
|
+
return `[${values.map(renderString).join(', ')}]`;
|
|
784
|
+
}
|