@dudko.dev/baton-format 0.1.2 → 0.3.0

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.
@@ -90,7 +90,10 @@ class Out {
90
90
  * or nothing but newlines, which reads back as the empty string.
91
91
  */
92
92
  block(indent, key, text) {
93
- if (text.includes("\r") || text.replace(/\n+$/, "") === "") {
93
+ // A carriage return or any other control character (a response body is
94
+ // whatever the API sent — ANSI colors, NUL bytes) cannot live in a block
95
+ // scalar; quoted, it round-trips byte for byte.
96
+ if (/[\x00-\x08\x0b-\x1f\x7f]/.test(text) || text.replace(/\n+$/, "") === "") {
94
97
  this.line(indent, `${key}: ${JSON.stringify(text)}`);
95
98
  return;
96
99
  }
@@ -279,6 +282,8 @@ export function serializeCollectionFile(col) {
279
282
  out.kv(0, "format", col.format);
280
283
  out.kv(0, "id", col.id);
281
284
  out.kv(0, "name", col.name);
285
+ if (col.spec !== undefined)
286
+ out.kv(0, "spec", col.spec);
282
287
  emitAuth(out, 0, col.auth);
283
288
  emitVariables(out, 0, "variables", col.variables);
284
289
  if (col.order.length > 0)
@@ -290,9 +295,22 @@ export function serializeEnvironmentFile(env) {
290
295
  const out = new Out();
291
296
  out.kv(0, "format", env.format);
292
297
  out.kv(0, "name", env.name);
298
+ if (env.production === true)
299
+ out.kv(0, "production", true);
293
300
  emitVariables(out, 0, "variables", env.variables);
294
- if (env.secrets.length > 0)
295
- out.list(0, "secrets", env.secrets);
301
+ if (env.secrets.length > 0) {
302
+ out.line(0, "secrets:");
303
+ for (const name of env.secrets) {
304
+ const ref = env.vault?.[name];
305
+ if (ref === undefined) {
306
+ out.line(2, `- ${scalar(name)}`);
307
+ }
308
+ else {
309
+ out.line(2, `- name: ${scalar(name)}`);
310
+ out.kv(4, "vault", ref);
311
+ }
312
+ }
313
+ }
296
314
  return out.toString();
297
315
  }
298
316
  /** Serialize a normalized workspace manifest to canonical bytes. */
@@ -314,7 +332,8 @@ export function serializeWorkspaceManifest(ws) {
314
332
  const tls = s.tls ?? { clientCertificates: [] };
315
333
  const tlsEmpty = tls.caFile === undefined && tls.clientCertificates.length === 0;
316
334
  const proxy = s.proxy;
317
- if (nonDefault.length > 0 || !tlsEmpty || proxy !== undefined) {
335
+ const vault = s.vault;
336
+ if (nonDefault.length > 0 || !tlsEmpty || proxy !== undefined || vault !== undefined) {
318
337
  out.line(0, "settings:");
319
338
  for (const [key, value] of nonDefault)
320
339
  out.kv(2, key, value);
@@ -349,6 +368,12 @@ export function serializeWorkspaceManifest(ws) {
349
368
  out.kv(4, "passwordSecret", proxy.passwordSecret);
350
369
  }
351
370
  }
371
+ if (vault !== undefined) {
372
+ out.line(2, "vault:");
373
+ out.kv(4, "address", vault.address);
374
+ if (vault.namespace !== undefined)
375
+ out.kv(4, "namespace", vault.namespace);
376
+ }
352
377
  }
353
378
  return out.toString();
354
379
  }
@@ -370,6 +395,8 @@ export function serializeSchedulesFile(file) {
370
395
  out.kv(6, "collection", sched.run.collection);
371
396
  if (sched.run.request !== undefined)
372
397
  out.kv(6, "request", sched.run.request);
398
+ if (sched.run.flow !== undefined)
399
+ out.kv(6, "flow", sched.run.flow);
373
400
  out.kv(6, "environment", sched.run.environment);
374
401
  if (sched.notify?.onFailure !== undefined) {
375
402
  out.line(4, "notify:");
@@ -380,4 +407,100 @@ export function serializeSchedulesFile(file) {
380
407
  }
381
408
  return out.toString();
382
409
  }
410
+ /** Serialize a request's examples to canonical bytes. */
411
+ export function serializeExamplesFile(file) {
412
+ const out = new Out();
413
+ out.kv(0, "format", file.format);
414
+ if (file.examples.length > 0) {
415
+ out.line(0, "examples:");
416
+ for (const example of file.examples) {
417
+ out.line(2, `- name: ${scalar(example.name)}`);
418
+ out.kv(4, "status", example.status);
419
+ emitKvRows(out, 4, "headers", example.headers);
420
+ if (example.body.length > 0)
421
+ out.block(4, "body", example.body);
422
+ }
423
+ }
424
+ return out.toString();
425
+ }
426
+ /** Serialize a mock definition to canonical bytes. */
427
+ export function serializeMockFile(mock) {
428
+ const out = new Out();
429
+ out.kv(0, "format", mock.format);
430
+ out.kv(0, "name", mock.name);
431
+ out.kv(0, "port", mock.port);
432
+ if (mock.collections.length > 0)
433
+ out.list(0, "collections", mock.collections);
434
+ if (mock.example !== undefined)
435
+ out.kv(0, "example", mock.example);
436
+ return out.toString();
437
+ }
438
+ /** Serialize a normalized flow to canonical bytes (ADR-0008, ADR-0009). */
439
+ export function serializeFlowFile(flow) {
440
+ const out = new Out();
441
+ out.kv(0, "format", flow.format);
442
+ out.kv(0, "id", flow.id);
443
+ out.kv(0, "name", flow.name);
444
+ if (flow.description !== undefined)
445
+ out.kv(0, "description", flow.description);
446
+ if (flow.environment !== undefined)
447
+ out.kv(0, "environment", flow.environment);
448
+ // A name such as `null` or `true` is a keyword to YAML: written bare, it
449
+ // comes back as something that is not a name at all.
450
+ const map = (indent, values) => {
451
+ for (const [key, value] of Object.entries(values))
452
+ out.kv(indent, scalar(key), value);
453
+ };
454
+ if (flow.steps.length > 0) {
455
+ out.line(0, "steps:");
456
+ for (const step of flow.steps) {
457
+ out.line(2, `- id: ${scalar(step.id)}`);
458
+ if (step.name !== undefined)
459
+ out.kv(4, "name", step.name);
460
+ switch (step.kind) {
461
+ case "request":
462
+ out.kv(4, "request", step.request);
463
+ break;
464
+ case "log":
465
+ out.kv(4, "log", step.log);
466
+ break;
467
+ case "if":
468
+ out.kv(4, "if", step.if);
469
+ break;
470
+ case "switch":
471
+ out.line(4, "switch:");
472
+ out.kv(6, "value", step.switch.value);
473
+ out.line(6, "cases:");
474
+ map(8, step.switch.cases);
475
+ break;
476
+ case "loop":
477
+ out.line(4, "loop:");
478
+ out.kv(6, "items", step.loop.items);
479
+ break;
480
+ case "get":
481
+ out.line(4, "get:");
482
+ out.kv(6, "from", step.get.from);
483
+ out.line(6, "values:");
484
+ map(8, step.get.values);
485
+ break;
486
+ case "set":
487
+ out.line(4, "set:");
488
+ map(6, step.set);
489
+ break;
490
+ }
491
+ if (step.after.length > 0)
492
+ out.line(4, `after: ${flowList(step.after)}`);
493
+ if (step.when !== undefined)
494
+ out.kv(4, "when", step.when);
495
+ if (step.expect !== undefined) {
496
+ out.line(4, "expect:");
497
+ if (step.expect.status !== undefined)
498
+ out.kv(6, "status", step.expect.status);
499
+ }
500
+ if (step.continueOnFailure)
501
+ out.kv(4, "continueOnFailure", true);
502
+ }
503
+ }
504
+ return out.toString();
505
+ }
383
506
  //# sourceMappingURL=serialize.js.map
@@ -129,6 +129,12 @@ export interface CollectionFile {
129
129
  format: number;
130
130
  id: string;
131
131
  name: string;
132
+ /**
133
+ * An OpenAPI document this collection follows: a workspace-relative path
134
+ * or an http(s) URL. The app lists the drift between the two; nothing here
135
+ * writes to it. Absent when the collection is hand-made.
136
+ */
137
+ spec?: string;
132
138
  auth: AuthConfig;
133
139
  variables: Variable[];
134
140
  order: string[];
@@ -140,6 +146,28 @@ export interface EnvironmentFile {
140
146
  variables: Variable[];
141
147
  /** Secret names only — values never live in the workspace. */
142
148
  secrets: string[];
149
+ /**
150
+ * Marks an environment whose targets are real: a send into it from an
151
+ * agent is confirmed in the app first. Absent means false.
152
+ */
153
+ production?: boolean;
154
+ /**
155
+ * Secrets read from Vault rather than the device store, by name:
156
+ * `apiToken` → `kv/billing/prod#password`. A location, not a value. Every
157
+ * key also appears in `secrets`, which keeps the order. Absent when empty.
158
+ */
159
+ vault?: Record<string, string>;
160
+ }
161
+ /**
162
+ * Where the workspace's Vault is — `settings.vault`. Infrastructure, so it is
163
+ * committed like the proxy; the token is a secret of its own
164
+ * (`BATON_VAULT_TOKEN` on the worker, the OS store in the app).
165
+ */
166
+ export interface VaultSettings {
167
+ /** `https://vault.example:8200` — scheme and host, no trailing path. */
168
+ address: string;
169
+ /** Vault Enterprise / HCP namespace, sent as `X-Vault-Namespace`. */
170
+ namespace?: string;
143
171
  }
144
172
  export interface ClientCertificateConfig {
145
173
  /** `api.corp.example` exact, `*.corp.example` for subdomains, `*` for all. */
@@ -171,6 +199,8 @@ export interface WorkspaceSettings {
171
199
  tls?: TlsSettings;
172
200
  /** Outbound proxy; absent means direct (or the platform environment). */
173
201
  proxy?: ProxyConfig;
202
+ /** The workspace's Vault; absent means no secret may name a Vault path. */
203
+ vault?: VaultSettings;
174
204
  }
175
205
  export declare const DEFAULT_SETTINGS: WorkspaceSettings;
176
206
  /** `baton.yaml` — the workspace manifest. */
@@ -191,15 +221,125 @@ export interface Schedule {
191
221
  /** IANA zone name. Default: UTC. */
192
222
  timezone: string;
193
223
  enabled: boolean;
224
+ /** Exactly one of `collection`, `request` or `flow` is set. */
194
225
  run: {
195
226
  collection?: string;
196
227
  request?: string;
228
+ flow?: string;
197
229
  environment: string;
198
230
  };
199
231
  notify?: ScheduleNotify;
200
232
  }
233
+ /**
234
+ * What a step's response must be: an exact status, or a class such as `2xx`.
235
+ * Without one, any status below 400 passes (ADR-0008).
236
+ */
237
+ export interface FlowExpect {
238
+ status?: number | string;
239
+ }
240
+ /** What every kind of step has (ADR-0008, ADR-0009). */
241
+ interface FlowStepBase {
242
+ /** Unique within the flow; the name edges, conditions and `{{steps.<id>.…}}` use. */
243
+ id: string;
244
+ /** Display label; a request step shows its request's own name without one. */
245
+ name?: string;
246
+ /**
247
+ * Steps this one waits for, as `id` or — out of an If, a Switch or a Loop —
248
+ * `id.port`. Empty: it starts with the flow.
249
+ */
250
+ after: string[];
251
+ /** Request steps only: a template condition; the step is skipped when it is false. */
252
+ when?: string;
253
+ /** Request steps only. */
254
+ expect?: FlowExpect;
255
+ /** Request and Loop steps: a failure here does not skip the steps after it. Default false. */
256
+ continueOnFailure: boolean;
257
+ }
258
+ /** Sends a request. */
259
+ export interface FlowRequestStep extends FlowStepBase {
260
+ kind: "request";
261
+ /** Workspace-relative path of the `*.req.yaml` this step sends. */
262
+ request: string;
263
+ }
264
+ /** Writes a message template into the run's result. */
265
+ export interface FlowLogStep extends FlowStepBase {
266
+ kind: "log";
267
+ log: string;
268
+ }
269
+ /** Takes its `true` or `false` port. */
270
+ export interface FlowIfStep extends FlowStepBase {
271
+ kind: "if";
272
+ if: string;
273
+ }
274
+ /** Takes the port of the first case equal to `value`, else `default`. */
275
+ export interface FlowSwitchStep extends FlowStepBase {
276
+ kind: "switch";
277
+ switch: {
278
+ value: string;
279
+ cases: Record<string, string>;
280
+ };
281
+ }
282
+ /** Walks a JSON list through its `each` port, then takes `done`. */
283
+ export interface FlowLoopStep extends FlowStepBase {
284
+ kind: "loop";
285
+ loop: {
286
+ items: string;
287
+ };
288
+ }
289
+ /** Variable name → dotted path into the JSON body of the request step `from`. */
290
+ export interface FlowGetStep extends FlowStepBase {
291
+ kind: "get";
292
+ get: {
293
+ from: string;
294
+ values: Record<string, string>;
295
+ };
296
+ }
297
+ /** Variable name → template. */
298
+ export interface FlowSetStep extends FlowStepBase {
299
+ kind: "set";
300
+ set: Record<string, string>;
301
+ }
302
+ /** One node of a flow, and the edges that lead into it. */
303
+ export type FlowStep = FlowRequestStep | FlowLogStep | FlowIfStep | FlowSwitchStep | FlowLoopStep | FlowGetStep | FlowSetStep;
304
+ export type FlowStepKind = FlowStep["kind"];
305
+ /** `flows/<slug>.flow.yaml` — a graph of steps (ADR-0008, ADR-0009). */
306
+ export interface FlowFile {
307
+ format: number;
308
+ id: string;
309
+ name: string;
310
+ description?: string;
311
+ /** The environment a run uses when it names none. */
312
+ environment?: string;
313
+ steps: FlowStep[];
314
+ }
201
315
  /** `schedules.yaml`. */
202
316
  export interface SchedulesFile {
203
317
  format: number;
204
318
  schedules: Schedule[];
205
319
  }
320
+ /**
321
+ * One saved response for a request: what a real call answered, or what the
322
+ * API is meant to answer. Lives beside the request in `<slug>.examples.yaml`.
323
+ */
324
+ export interface ResponseExample {
325
+ name: string;
326
+ status: number;
327
+ headers: KeyValueRow[];
328
+ body: string;
329
+ }
330
+ /** `<slug>.examples.yaml`: the examples a request carries, in order. */
331
+ export interface ExamplesFile {
332
+ format: number;
333
+ examples: ResponseExample[];
334
+ }
335
+ /** `mocks/<name>.yaml`: a mock server stood up from examples. */
336
+ export interface MockFile {
337
+ format: number;
338
+ name: string;
339
+ port: number;
340
+ /** Workspace-relative collection paths whose requests the mock answers. */
341
+ collections: string[];
342
+ /** The example to answer with, by name; the first one when absent. */
343
+ example?: string;
344
+ }
345
+ export {};
@@ -0,0 +1,15 @@
1
+ /** A reference into KV v2: `<mount>/<path>#<key>` — a location, never a value. */
2
+ export interface VaultRef {
3
+ mount: string;
4
+ path: string;
5
+ key: string;
6
+ }
7
+ /**
8
+ * Parses `<mount>/<path>#<key>`; throws a FormatError at `fieldPath` on
9
+ * anything else. Mount and path parts may not be empty, contain whitespace,
10
+ * or walk upward (`..`), so a reference cannot escape the mount it names.
11
+ * Mirrors `VaultRef.parse` in baton_core (invariant 6).
12
+ */
13
+ export declare function parseVaultRef(text: string, fieldPath: string): VaultRef;
14
+ /** The canonical text form — what the writer emits. */
15
+ export declare function formatVaultRef(ref: VaultRef): string;
@@ -0,0 +1,32 @@
1
+ import { FormatError } from "./errors.js";
2
+ /**
3
+ * Parses `<mount>/<path>#<key>`; throws a FormatError at `fieldPath` on
4
+ * anything else. Mount and path parts may not be empty, contain whitespace,
5
+ * or walk upward (`..`), so a reference cannot escape the mount it names.
6
+ * Mirrors `VaultRef.parse` in baton_core (invariant 6).
7
+ */
8
+ export function parseVaultRef(text, fieldPath) {
9
+ const bad = (reason) => new FormatError("invalid-field", fieldPath, reason);
10
+ const hash = text.indexOf("#");
11
+ if (hash < 0 || text.indexOf("#", hash + 1) >= 0)
12
+ throw bad("must be <mount>/<path>#<key>");
13
+ const location = text.slice(0, hash);
14
+ const key = text.slice(hash + 1);
15
+ if (key === "" || /\s/.test(key))
16
+ throw bad("key after # must be a non-empty word");
17
+ const slash = location.indexOf("/");
18
+ if (slash <= 0 || slash === location.length - 1)
19
+ throw bad("must be <mount>/<path>#<key>");
20
+ const segments = location.split("/");
21
+ for (const segment of segments) {
22
+ if (segment === "" || segment === "." || segment === ".." || /\s/.test(segment)) {
23
+ throw bad("mount and path segments must be non-empty words");
24
+ }
25
+ }
26
+ return { mount: segments[0], path: segments.slice(1).join("/"), key };
27
+ }
28
+ /** The canonical text form — what the writer emits. */
29
+ export function formatVaultRef(ref) {
30
+ return `${ref.mount}/${ref.path}#${ref.key}`;
31
+ }
32
+ //# sourceMappingURL=vault.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dudko.dev/baton-format",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "Baton workspace file format: parsing, validation, and deterministic serialization.",
5
5
  "keywords": ["baton", "api-client", "yaml", "workspace", "format"],
6
6
  "homepage": "https://baton.dudko.dev",