@pbuilder/sdk 0.12.0 → 0.14.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.
@@ -4,12 +4,12 @@
4
4
  export function forceEntry(force) {
5
5
  return force !== undefined ? { force } : {};
6
6
  }
7
- // REQ-TOE-01: shallow top-level composite (array/plain-object) -> JSON string; everything
8
- // else passes through verbatim. Assembly uses Object.defineProperty (never spread/`result[k]=`)
7
+ // REQ-TOE-01: top-level composite (array/plain-object) values are copied as native JSON;
8
+ // everything else passes through verbatim. Assembly uses Object.defineProperty (never spread/`result[k]=`)
9
9
  // per REQ-TOE-05 — enumerable:true is load-bearing, defineProperty defaults it to false and an
10
10
  // unenumerable key silently drops from JSON.stringify at the wire boundary.
11
11
  // REQ-TOE-01.5: a null-prototype object (Object.create(null)) is a plain, dict-like data
12
- // container and encodes like any other plain object — only a prototype OUTSIDE
12
+ // container and is copied like any other plain object — only a prototype OUTSIDE
13
13
  // {Object.prototype, null} (Date/Map/class instances) is non-plain (REQ-TOE-04, S-002).
14
14
  function isPlainObject(v) {
15
15
  if (typeof v !== "object" || v === null || Array.isArray(v))
@@ -44,7 +44,9 @@ function rejectOption(optionKey, detail) {
44
44
  // ascent (add-recurse-delete, scoped per top-level call via the default parameter) — never a
45
45
  // global "ever-visited" set. Only a value that is its own ancestor (a true cycle) rejects; an
46
46
  // acyclic SHARED reference (`const s = {x:1}; {a:s,b:s}`) is revisited from a clean ancestor
47
- // path each time and encodes normally, matching native JSON.stringify.
47
+ // path each time and is copied normally, matching native JSON.stringify.
48
+ // Returns a structural copy of a valid value: composites are rebuilt (plain-object keys via
49
+ // Object.defineProperty, REQ-TOE-05), scalars are returned as they are.
48
50
  function assertEncodable(value, optionKey, ancestors = new Set(), path = optionKey) {
49
51
  if (value === undefined)
50
52
  rejectOption(optionKey, `undefined at ${path}`);
@@ -54,40 +56,42 @@ function assertEncodable(value, optionKey, ancestors = new Set(), path = optionK
54
56
  rejectOption(optionKey, `symbol at ${path}`);
55
57
  if (typeof value === "bigint")
56
58
  rejectOption(optionKey, `BigInt at ${path}`);
57
- if (value === null || typeof value !== "object")
58
- return; // string/number/boolean/null: fine
59
+ if (value === null || typeof value !== "object") {
60
+ // A nested -0 inside the old JSON-string form became 0; keep that, or the round-trip
61
+ // guard rejects it (-0 === 0, so this also catches -0). A top-level -0 was never encoded, so it is left alone.
62
+ return value === 0 && path !== optionKey ? 0 : value;
63
+ }
59
64
  if (Array.isArray(value) || isPlainObject(value)) {
60
65
  if (ancestors.has(value))
61
66
  rejectOption(optionKey, `a circular reference at ${path}`);
62
67
  ancestors.add(value);
63
68
  try {
64
69
  if (Array.isArray(value)) {
65
- value.forEach((item, i) => assertEncodable(item, optionKey, ancestors, `${path}[${i}]`));
66
- }
67
- else {
68
- for (const [k, v] of Object.entries(value)) {
69
- assertEncodable(v, optionKey, ancestors, `${path}.${k}`);
70
- }
70
+ return value.map((item, i) => assertEncodable(item, optionKey, ancestors, `${path}[${i}]`));
71
71
  }
72
+ return copyEntries(value, ([k, v]) => assertEncodable(v, optionKey, ancestors, `${path}.${k}`));
72
73
  }
73
74
  finally {
74
75
  ancestors.delete(value);
75
76
  }
76
- return;
77
77
  }
78
- rejectOption(optionKey, `${kindOf(value)} at ${path}`);
78
+ return rejectOption(optionKey, `${kindOf(value)} at ${path}`);
79
79
  }
80
- export function encodeOptions(options) {
81
- if (!isPlainObject(options))
82
- return options;
80
+ function copyEntries(source, copy) {
83
81
  const result = {};
84
- for (const [key, value] of Object.entries(options)) {
85
- assertEncodable(value, key);
86
- const encoded = Array.isArray(value) || isPlainObject(value) ? JSON.stringify(value) : value;
87
- Object.defineProperty(result, key, { value: encoded, enumerable: true, writable: true, configurable: true });
82
+ for (const entry of Object.entries(source)) {
83
+ Object.defineProperty(result, entry[0], { value: copy(entry), enumerable: true, writable: true, configurable: true });
88
84
  }
89
85
  return result;
90
86
  }
87
+ // ADR-0082: composites reach the wire as native JSON. The copy snapshots the author's value
88
+ // at scheduling time and keeps nested non-finite numbers, so the flush-time round-trip
89
+ // guard sees them (REQ-TOE-03.3).
90
+ export function encodeOptions(options) {
91
+ if (!isPlainObject(options))
92
+ return options;
93
+ return copyEntries(options, ([key, value]) => assertEncodable(value, key));
94
+ }
91
95
  export class DirectiveFactory {
92
96
  create(a) {
93
97
  return {
@@ -2,7 +2,7 @@
2
2
  "manifestVersion": 1,
3
3
  "algorithm": "sha256",
4
4
  "entry": "dist/bin/pbuilder-runner.js",
5
- "packageVersion": "0.12.0",
5
+ "packageVersion": "0.14.0",
6
6
  "files": [
7
7
  {
8
8
  "path": "dist/bin/pbuilder-runner.js",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  {
20
20
  "path": "dist/core/directive-factory.js",
21
- "sha256": "06f13f234f77bfd749139d937b7d2381a0d72b364d9f68912a0bccbc7ee43cf3"
21
+ "sha256": "9aad2b51eec7679dbf031e2b9e71276d86ac4ce3ad6f0411e88d4d8a966cb3ae"
22
22
  },
23
23
  {
24
24
  "path": "dist/core/emit-rejection.js",
@@ -98,7 +98,7 @@
98
98
  },
99
99
  {
100
100
  "path": "package.json",
101
- "sha256": "dd84ad976cc9ec3ae2fba6d4adc7c2d5a7ce3eceeac47212a62727e7a2e96a9c"
101
+ "sha256": "c02a0fe03d363197f2490851657b0dd76aa3907ea5103d28218b7383fcf4d378"
102
102
  }
103
103
  ]
104
104
  }
@@ -102,12 +102,8 @@ export function classifyTransport(params) {
102
102
  // `>` (not `>=`) — exactly-at-budget still fits (REQ-CCL-02.3).
103
103
  //
104
104
  // REQ-CCL-02.4: `options` is run through the SAME shared `encodeOptions` (fit-39's one
105
- // other sanctioned call site) real `create()` emission applies — never the caller's raw,
106
- // pre-encode shape. A composite option value's JSON-string form carries its own
107
- // quote/backslash-escaping inflation once re-embedded and serialized again here, closing
108
- // the pre-encode/post-encode misclassification gap: measuring the raw shape would
109
- // under-count and could wrongly classify by-value a directive that is actually over
110
- // budget once the real encode step runs.
105
+ // other sanctioned call site) real `create()` emission applies, so the measure and the
106
+ // emitted directive can never disagree on the options' wire form.
111
107
  const prospectiveDirective = {
112
108
  op: "create",
113
109
  create: { pathTemplate: destPath, template: content, options: encodeOptions(options), ...forceEntry(force) },
@@ -31,7 +31,7 @@ import { invalidInput } from "../core/authoring-error.js";
31
31
  import { forceEntry } from "../core/directive-factory.js";
32
32
  import { EMIT_BATCH_BUDGET_BYTES, serializedBatchSize } from "../core/wire.js";
33
33
  import { walkFolder } from "./walk.js";
34
- import { runFilenamePipeline, isIncluded, detectDestinationCollisions, translateTokens } from "./filename-pipeline.js";
34
+ import { runFilenamePipeline, isIncluded, detectDestinationCollisions, translateTokens, checkSourceNames, } from "./filename-pipeline.js";
35
35
  import { classifyTransport } from "./classify-transport.js";
36
36
  import { validateDestinationLexical, validateSourceLexical } from "./path-guards.js";
37
37
  function noResolutionAnchorMessage() {
@@ -143,6 +143,9 @@ export function runScaffold(args) {
143
143
  if (walked.length === 0) {
144
144
  return; // REQ-FSC-04.1: a truly-empty source folder is a silent no-op.
145
145
  }
146
+ // A-17 covers every name under the source folder, excluded ones included: an excluded
147
+ // `con.ts` still cannot be checked out on Windows.
148
+ checkSourceNames(walked.map((entry) => entry.relPath));
146
149
  const filtered = walked.filter((entry) => isIncluded(entry.relPath, args.include, args.exclude));
147
150
  if (filtered.length === 0) {
148
151
  throw invalidInput(filtersEliminatedEverythingMessage(args.include, args.exclude));
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Architecture A-17: rejects a source-relative path, as written on disk, when any of its
3
+ * components holds `{=` or `=}` or is a name Windows refuses — on every OS. A well-formed
4
+ * token holds only letters, digits, `_` and `@`, so it never trips this check.
5
+ */
6
+ export declare function checkSourceName(relPath: string): void;
1
7
  /**
2
8
  * Translates on-disk filename tokens to the wire `pathTemplate` syntax the ENGINE
3
9
  * substitutes (obs #915 ruling 4) — the SDK never renders these itself, only rewrites the
@@ -28,6 +34,8 @@ export interface PipelineResult {
28
34
  * (the marker must survive translation to be recognized).
29
35
  */
30
36
  export declare function runFilenamePipeline(relPath: string, rename: Record<string, string> | undefined): PipelineResult;
37
+ /** Checks every path, reporting the first bad one in byte-wise order, as the engine does. */
38
+ export declare function checkSourceNames(relPaths: readonly string[]): void;
31
39
  /**
32
40
  * REQ-FSC-03: include/exclude filtering — `exclude` WINS on overlap with `include`.
33
41
  * Default `include` matches everything (`["**"]`); default `exclude` matches nothing.
@@ -9,7 +9,61 @@ import { posix } from "node:path";
9
9
  import { invalidInput } from "../core/authoring-error.js";
10
10
  const TEMPLATE_SUFFIX = ".template";
11
11
  const TOKEN_MARKER = "__";
12
- const TOKEN_INNER = /^[A-Za-z_][A-Za-z0-9_]*(?:@[A-Za-z_][A-Za-z0-9_]*)*$/;
12
+ // A key starts with a letter, so an opening `__` inside a longer underscore run is its last
13
+ // two underscores (A-17); a filter name may start with `_`.
14
+ const TOKEN_INNER = /^[A-Za-z][A-Za-z0-9_]*(?:@[A-Za-z_][A-Za-z0-9_]*)*$/;
15
+ const FORBIDDEN_CHARACTER_NAMES = {
16
+ "|": "vertical bar",
17
+ '"': "double quote",
18
+ "<": "less-than sign",
19
+ ">": "greater-than sign",
20
+ ":": "colon",
21
+ "?": "question mark",
22
+ "*": "asterisk",
23
+ "\\": "backslash",
24
+ };
25
+ const RESERVED_DEVICE_NAMES = new Set([
26
+ "CON",
27
+ "PRN",
28
+ "AUX",
29
+ "NUL",
30
+ ...["COM", "LPT"].flatMap((prefix) => [..."0123456789¹²³"].map((digit) => prefix + digit)),
31
+ ]);
32
+ // The first A-17 rule `name` breaks, in the engine's `CheckPortableName` order and vocabulary
33
+ // (characters first, then reserved device names, then a trailing dot or space).
34
+ function portableNameViolation(name) {
35
+ for (const ch of name) {
36
+ const code = ch.charCodeAt(0);
37
+ if (code < 0x20) {
38
+ return { rule: "forbidden character", character: `control character 0x${code.toString(16).toUpperCase().padStart(2, "0")}` };
39
+ }
40
+ const character = FORBIDDEN_CHARACTER_NAMES[ch];
41
+ if (character !== undefined)
42
+ return { rule: "forbidden character", character };
43
+ }
44
+ const stem = name.split(".", 1)[0];
45
+ if (RESERVED_DEVICE_NAMES.has(stem.replace(/ +$/, "").toUpperCase()))
46
+ return { rule: "reserved device name" };
47
+ if (name.endsWith("."))
48
+ return { rule: "trailing dot" };
49
+ if (name.endsWith(" "))
50
+ return { rule: "trailing space" };
51
+ return undefined;
52
+ }
53
+ /**
54
+ * Architecture A-17: rejects a source-relative path, as written on disk, when any of its
55
+ * components holds `{=` or `=}` or is a name Windows refuses — on every OS. A well-formed
56
+ * token holds only letters, digits, `_` and `@`, so it never trips this check.
57
+ */
58
+ export function checkSourceName(relPath) {
59
+ for (const name of relPath.split("/")) {
60
+ const violation = name.includes("{=") || name.includes("=}") ? { rule: "template syntax" } : portableNameViolation(name);
61
+ if (violation === undefined)
62
+ continue;
63
+ const suffix = violation.character === undefined ? "" : ` (${violation.character})`;
64
+ throw invalidInput(`invalid input: scaffold file name "${relPath}": the name "${name}" breaks the rule "${violation.rule}"${suffix}`);
65
+ }
66
+ }
13
67
  /**
14
68
  * Translates on-disk filename tokens to the wire `pathTemplate` syntax the ENGINE
15
69
  * substitutes (obs #915 ruling 4) — the SDK never renders these itself, only rewrites the
@@ -77,6 +131,23 @@ export function runFilenamePipeline(relPath, rename) {
77
131
  const { stripped, wasTemplate } = stripTemplateSuffix(translated);
78
132
  return { sourceRelPath: relPath, destRelPath: stripped, isTemplateMarked: wasTemplate };
79
133
  }
134
+ // UTF-8 byte order is code-point order; the default sort compares UTF-16 code units, which
135
+ // puts astral characters before U+E000-U+FFFF.
136
+ function compareCodePoints(a, b) {
137
+ const x = [...a];
138
+ const y = [...b];
139
+ for (let i = 0; i < x.length && i < y.length; i++) {
140
+ const diff = x[i].codePointAt(0) - y[i].codePointAt(0);
141
+ if (diff !== 0)
142
+ return diff;
143
+ }
144
+ return x.length - y.length;
145
+ }
146
+ /** Checks every path, reporting the first bad one in byte-wise order, as the engine does. */
147
+ export function checkSourceNames(relPaths) {
148
+ for (const relPath of [...relPaths].sort(compareCodePoints))
149
+ checkSourceName(relPath);
150
+ }
80
151
  function escapeRegexChar(ch) {
81
152
  return /[.+^${}()|[\]\\]/.test(ch) ? `\\${ch}` : ch;
82
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbuilder/sdk",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "The public TypeScript/Bun authoring layer for Project Builder schematics. Authors file mutations (create, replaceContent, remove, rename, move, copy, copyIn), lowers them to IR, and talks to the Project Builder engine over framed stdio (see docs/engine-sdk-wire-spec.md).",
5
5
  "license": "MIT",
6
6
  "type": "module",