@rulecad/cad 0.8.0 → 0.10.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.
Files changed (102) hide show
  1. package/AGENTS.md +45 -16
  2. package/CHANGELOG.md +724 -0
  3. package/EXAMPLES.md +61 -54
  4. package/README.md +376 -98
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2664 -541
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1883 -435
  9. package/lib/beams.mjs +256 -52
  10. package/lib/belts.mjs +66 -15
  11. package/lib/board-work.mjs +12 -0
  12. package/lib/bought.mjs +309 -53
  13. package/lib/buildpage.mjs +179 -29
  14. package/lib/cache-file.mjs +105 -0
  15. package/lib/carried.mjs +201 -0
  16. package/lib/clone.mjs +194 -42
  17. package/lib/code-text.mjs +28 -0
  18. package/lib/design-imports.mjs +127 -0
  19. package/lib/design.mjs +871 -147
  20. package/lib/designshapes.mjs +142 -0
  21. package/lib/drive.mjs +187 -63
  22. package/lib/edit.mjs +922 -192
  23. package/lib/errors.mjs +16 -3
  24. package/lib/estimates.mjs +51 -0
  25. package/lib/explain.mjs +62 -13
  26. package/lib/fasten.mjs +2708 -507
  27. package/lib/fe-key.mjs +29 -0
  28. package/lib/fittings.mjs +721 -122
  29. package/lib/fixings.mjs +38 -13
  30. package/lib/framebuild.mjs +790 -175
  31. package/lib/framecheck.mjs +1901 -368
  32. package/lib/framegen.mjs +108 -22
  33. package/lib/frameheat.mjs +225 -42
  34. package/lib/frameprint.mjs +513 -108
  35. package/lib/frames.mjs +86 -18
  36. package/lib/framesize.mjs +719 -120
  37. package/lib/frametip.mjs +235 -0
  38. package/lib/gait.mjs +135 -23
  39. package/lib/grip.mjs +62 -13
  40. package/lib/guides.mjs +1403 -230
  41. package/lib/host.mjs +129 -23
  42. package/lib/inplace.mjs +171 -36
  43. package/lib/jigs.mjs +360 -78
  44. package/lib/joins.mjs +697 -118
  45. package/lib/jsonedit.mjs +88 -26
  46. package/lib/kept.mjs +84 -0
  47. package/lib/kit.mjs +439 -69
  48. package/lib/layout.mjs +2 -1
  49. package/lib/links.mjs +149 -42
  50. package/lib/list.mjs +334 -64
  51. package/lib/live-guard.mjs +68 -0
  52. package/lib/live-shape.mjs +96 -24
  53. package/lib/live.mjs +189 -59
  54. package/lib/loads.mjs +597 -136
  55. package/lib/machine.mjs +372 -74
  56. package/lib/magnets.mjs +162 -44
  57. package/lib/measured.mjs +1080 -117
  58. package/lib/mesh.mjs +526 -119
  59. package/lib/meshcmd.mjs +215 -60
  60. package/lib/motors.mjs +1002 -214
  61. package/lib/motorsvg.mjs +114 -32
  62. package/lib/names.mjs +18 -1
  63. package/lib/optimise.mjs +991 -287
  64. package/lib/order.mjs +515 -114
  65. package/lib/panels.mjs +482 -94
  66. package/lib/parts.mjs +81 -23
  67. package/lib/pins.mjs +163 -44
  68. package/lib/plates.mjs +223 -35
  69. package/lib/player.mjs +37 -9
  70. package/lib/power.mjs +64 -13
  71. package/lib/preview.mjs +83 -16
  72. package/lib/profilemeasure.mjs +572 -129
  73. package/lib/rules.mjs +17 -6
  74. package/lib/sandbox-hooks.mjs +179 -27
  75. package/lib/sandbox-lock.mjs +225 -0
  76. package/lib/sandbox-preload.mjs +2 -2
  77. package/lib/sandbox-run.mjs +122 -25
  78. package/lib/sandbox.mjs +667 -75
  79. package/lib/schema.mjs +42 -11
  80. package/lib/seats.mjs +227 -49
  81. package/lib/services.mjs +261 -52
  82. package/lib/sheet.mjs +963 -179
  83. package/lib/slotnuts.mjs +213 -34
  84. package/lib/targets/gist.mjs +4 -1
  85. package/lib/targets.mjs +22 -4
  86. package/lib/task.mjs +376 -79
  87. package/lib/versions.mjs +285 -39
  88. package/lib/wires.mjs +228 -45
  89. package/lib/workspace.mjs +200 -14
  90. package/package.json +19 -19
  91. package/schemas/assembly.schema.json +2 -2
  92. package/schemas/project.schema.json +6 -0
  93. package/templates/coupons/MEASURE.md +109 -0
  94. package/templates/coupons/assemblies/fixings-test.json +20 -0
  95. package/templates/coupons/assemblies/slot-test.json +11 -0
  96. package/templates/coupons/objects/fixings-coupon.js +88 -0
  97. package/templates/coupons/objects/fixings-coupon.json +26 -0
  98. package/templates/coupons/objects/holds-coupon.js +130 -0
  99. package/templates/coupons/objects/holds-coupon.json +42 -0
  100. package/templates/coupons/objects/slot-coupon.js +89 -0
  101. package/templates/coupons/objects/slot-coupon.json +29 -0
  102. package/templates/coupons/project.json +10 -0
package/lib/jsonedit.mjs CHANGED
@@ -5,24 +5,54 @@
5
5
  // Where every value is: [{ path: ['variables', 'w', 'value'], start, end }], in the order they appear
6
6
  function spans(text) {
7
7
  // (the scanner below takes the text to be JSON: one that isn't would send it past the end, so that's settled first)
8
- try { JSON.parse(text); } catch (e) { throw Object.assign(new Error(`not valid JSON, so it can't be edited: ${e.message}`), { code: 'json' }); }
8
+ try {
9
+ JSON.parse(text);
10
+ } catch (e) {
11
+ throw Object.assign(new Error(`not valid JSON, so it can't be edited: ${e.message}`), { code: 'json' });
12
+ }
9
13
  const out = [];
10
14
  let i = 0;
11
- const ws = () => { while (i < text.length && /\s/.test(text[i])) i++; };
12
- const str = () => { const s = i++; while (text[i] !== '"') i += text[i] === '\\' ? 2 : 1; i++; return JSON.parse(text.slice(s, i)); };
15
+ const ws = () => {
16
+ while (i < text.length && /\s/.test(text[i])) i++;
17
+ };
18
+ const str = () => {
19
+ const s = i++;
20
+ while (text[i] !== '"') i += text[i] === '\\' ? 2 : 1;
21
+ i++;
22
+ return JSON.parse(text.slice(s, i));
23
+ };
13
24
  const value = (path) => {
14
25
  ws();
15
- const start = i, c = text[i];
26
+ const start = i,
27
+ c = text[i];
16
28
  if (c === '{') {
17
- i++; ws();
29
+ i++;
30
+ ws();
18
31
  if (text[i] === '}') i++;
19
- else for (;;) { ws(); const k = str(); ws(); i++; value([...path, k]); ws(); if (text[i++] === '}') break; }
32
+ else
33
+ for (;;) {
34
+ ws();
35
+ const k = str();
36
+ ws();
37
+ i++;
38
+ value([...path, k]);
39
+ ws();
40
+ if (text[i++] === '}') break;
41
+ }
20
42
  } else if (c === '[') {
21
- i++; ws();
43
+ i++;
44
+ ws();
22
45
  if (text[i] === ']') i++;
23
- else for (let n = 0; ; n++) { value([...path, n]); ws(); if (text[i++] === ']') break; }
46
+ else
47
+ for (let n = 0; ; n++) {
48
+ value([...path, n]);
49
+ ws();
50
+ if (text[i++] === ']') break;
51
+ }
24
52
  } else if (c === '"') str();
25
- else { while (i < text.length && /[^\s,\]}]/.test(text[i])) i++; }
53
+ else {
54
+ while (i < text.length && /[^\s,\]}]/.test(text[i])) i++;
55
+ }
26
56
  out.push({ path, start, end: i });
27
57
  };
28
58
  value([]);
@@ -31,17 +61,33 @@ function spans(text) {
31
61
  const same = (a, b) => a.length === b.length && a.every((x, k) => x === b[k]);
32
62
  const find = (text, path) => spans(text).find((s) => same(s.path, path));
33
63
  // The indentation of the line a position is on
34
- const indentAt = (text, at) => { const line = text.lastIndexOf('\n', at - 1) + 1; return /^[ \t]*/.exec(text.slice(line))[0]; };
64
+ const indentAt = (text, at) => {
65
+ const line = text.lastIndexOf('\n', at - 1) + 1;
66
+ return /^[ \t]*/.exec(text.slice(line))[0];
67
+ };
35
68
  // How the file is laid out: its line ending, and what one level of indentation is (two spaces, unless its first
36
69
  // indented line says otherwise: a tab, four spaces)
37
- const styleOf = (text) => ({ eol: text.includes('\r\n') ? '\r\n' : '\n', unit: (/\n([ \t]+)\S/.exec(text) || [null, ' '])[1] });
70
+ const styleOf = (text) => ({
71
+ eol: text.includes('\r\n') ? '\r\n' : '\n',
72
+ unit: (/\n([ \t]+)\S/.exec(text) || [null, ' '])[1],
73
+ });
38
74
  // A value as JSON, laid out like the file (from the given indentation); short arrays of numbers and expressions stay
39
75
  // on one line, as people write them
40
76
  function render(v, indent, style) {
41
- if (Array.isArray(v) && v.every((x) => typeof x !== 'object' || x === null) && JSON.stringify(v).length <= 80) return `[${v.map((x) => JSON.stringify(x)).join(', ')}]`;
42
- const inner = `${indent}${style.unit}`, { eol } = style;
43
- if (Array.isArray(v)) return v.length ? `[${eol}${v.map((x) => `${inner}${render(x, inner, style)}`).join(`,${eol}`)}${eol}${indent}]` : '[]';
44
- if (v && typeof v === 'object') { const e = Object.entries(v); return e.length ? `{${eol}${e.map(([k, x]) => `${inner}${JSON.stringify(k)}: ${render(x, inner, style)}`).join(`,${eol}`)}${eol}${indent}}` : '{}'; }
77
+ if (Array.isArray(v) && v.every((x) => typeof x !== 'object' || x === null) && JSON.stringify(v).length <= 80)
78
+ return `[${v.map((x) => JSON.stringify(x)).join(', ')}]`;
79
+ const inner = `${indent}${style.unit}`,
80
+ { eol } = style;
81
+ if (Array.isArray(v))
82
+ return v.length
83
+ ? `[${eol}${v.map((x) => `${inner}${render(x, inner, style)}`).join(`,${eol}`)}${eol}${indent}]`
84
+ : '[]';
85
+ if (v && typeof v === 'object') {
86
+ const e = Object.entries(v);
87
+ return e.length
88
+ ? `{${eol}${e.map(([k, x]) => `${inner}${JSON.stringify(k)}: ${render(x, inner, style)}`).join(`,${eol}`)}${eol}${indent}}`
89
+ : '{}';
90
+ }
45
91
  return JSON.stringify(v);
46
92
  }
47
93
 
@@ -59,9 +105,13 @@ export function setAt(text, path, value) {
59
105
  const s = find(text, path);
60
106
  if (s) return text.slice(0, s.start) + render(value, indentAt(text, s.start), styleOf(text)) + text.slice(s.end);
61
107
  const parent = find(text, path.slice(0, -1));
62
- if (!parent) throw Object.assign(new Error(`no ${path.slice(0, -1).join('.') || 'top level'} to add ${path.at(-1)} to`), { code: 'edit.path' });
108
+ if (!parent)
109
+ throw Object.assign(new Error(`no ${path.slice(0, -1).join('.') || 'top level'} to add ${path.at(-1)} to`), {
110
+ code: 'edit.path',
111
+ });
63
112
  const body = text.slice(parent.start, parent.end);
64
- if (body[0] !== '{') throw Object.assign(new Error(`${path.slice(0, -1).join('.')} isn't an object`), { code: 'edit.path' });
113
+ if (body[0] !== '{')
114
+ throw Object.assign(new Error(`${path.slice(0, -1).join('.')} isn't an object`), { code: 'edit.path' });
65
115
  return insert(text, parent, `${JSON.stringify(path.at(-1))}: `, value);
66
116
  }
67
117
 
@@ -75,18 +125,26 @@ export function appendAt(text, path, value) {
75
125
 
76
126
  // Add a member before an object's } or an array's ], after its last one, indented like it
77
127
  function insert(text, s, key, value) {
78
- const close = s.end - 1, inner = text.slice(s.start + 1, close);
79
- const outer = indentAt(text, close), empty = !inner.trim();
128
+ const close = s.end - 1,
129
+ inner = text.slice(s.start + 1, close);
130
+ const outer = indentAt(text, close),
131
+ empty = !inner.trim();
80
132
  // Its own members (not theirs): the last one is where the new one goes after; the first says how they're indented
81
- const members = spans(text).filter((x) => x.path.length === s.path.length + 1 && same(x.path.slice(0, -1), s.path)).sort((a, b) => a.start - b.start);
82
- const last = members.at(-1), multi = empty || inner.includes('\n');
133
+ const members = spans(text)
134
+ .filter((x) => x.path.length === s.path.length + 1 && same(x.path.slice(0, -1), s.path))
135
+ .sort((a, b) => a.start - b.start);
136
+ const last = members.at(-1),
137
+ multi = empty || inner.includes('\n');
83
138
  const first = members.length ? text.lastIndexOf('\n', members[0].start) : -1;
84
- const style = styleOf(text), { eol } = style;
139
+ const style = styleOf(text),
140
+ { eol } = style;
85
141
  const indent = empty ? `${outer}${style.unit}` : multi ? /^[ \t]*/.exec(text.slice(first + 1))[0] : outer;
86
142
  const piece = `${key}${render(value, indent, style)}`;
87
143
  if (empty) return `${text.slice(0, s.start + 1)}${eol}${indent}${piece}${eol}${outer}${text.slice(close)}`;
88
144
  const after = last.end;
89
- return multi ? `${text.slice(0, after)},${eol}${indent}${piece}${text.slice(after)}` : `${text.slice(0, after)}, ${piece}${text.slice(after)}`;
145
+ return multi
146
+ ? `${text.slice(0, after)},${eol}${indent}${piece}${text.slice(after)}`
147
+ : `${text.slice(0, after)}, ${piece}${text.slice(after)}`;
90
148
  }
91
149
 
92
150
  /**
@@ -95,9 +153,13 @@ function insert(text, s, key, value) {
95
153
  */
96
154
  export function removeAt(text, path) {
97
155
  const s = find(text, path);
98
- if (!s || !path.length) throw Object.assign(new Error(`no ${path.join('.') || 'value'} to remove`), { code: 'edit.path' });
99
- const parent = find(text, path.slice(0, -1)), all = spans(text);
100
- const members = all.filter((x) => x.path.length === path.length && same(x.path.slice(0, -1), parent.path)).sort((a, b) => a.start - b.start);
156
+ if (!s || !path.length)
157
+ throw Object.assign(new Error(`no ${path.join('.') || 'value'} to remove`), { code: 'edit.path' });
158
+ const parent = find(text, path.slice(0, -1)),
159
+ all = spans(text);
160
+ const members = all
161
+ .filter((x) => x.path.length === path.length && same(x.path.slice(0, -1), parent.path))
162
+ .sort((a, b) => a.start - b.start);
101
163
  const keyed = text[parent.start] === '{';
102
164
  // where a member starts: at its key in an object (the last "key" before its value), else at its value
103
165
  const startOf = (m) => (keyed ? text.lastIndexOf(JSON.stringify(m.path.at(-1)), m.start) : m.start);
package/lib/kept.mjs ADDED
@@ -0,0 +1,84 @@
1
+ // What a sandboxed design may hand back for the tool's caches (issue #581): its lamina's slow stages and its board's
2
+ // routes, each up to a number and a size in all. The tool refuses a reply past either (sandbox.mjs checkReply); the
3
+ // sandbox's runner (sandbox-run.mjs) keeps no more than that, so an honest design with more stages than the cap takes
4
+ // is built all the same, and only what is past the cap is not kept (made again at its next build).
5
+ // How many and how much: the kit's largest launcher makes a handful of stages, its physics about 5 MB and its flood's
6
+ // probe about 9 (November 2026); a lamina makes at most four (kinematics, physics, frame, flood probe). Below the 60
7
+ // each design's folder of the cache keeps (design.mjs STAGES_KEEP), so one reply never pushes all the design's own out.
8
+
9
+ /** The most stages one sandboxed reply may hand back: their number and their text in all, in bytes. */
10
+ export const STAGES_MOST = { count: 40, bytes: 256 * 1024 * 1024 };
11
+ /** The most routes one sandboxed board's reply may hand back: their number and their text in all, in bytes. */
12
+ export const ROUTES_MOST = { count: 40, bytes: 64 * 1024 * 1024 };
13
+
14
+ // What the caches written from replies may hold on disk (design.mjs makeRoom): each design's folder, and every design's
15
+ // folders together. The cap on one reply above bounds one build; without these, a workspace of many designs (one cloned
16
+ // from someone else), or one design built again and again with other inputs, could still fill the disk 256 MB at a time.
17
+ // A design's folder holds two replies' worth; past the whole cache's limit, the files least lately used go, whosever
18
+ // they are (a stage that is gone is only worked out again: slower, never wrong).
19
+ /** The most a design's own folder of the stages, and the whole stages cache, may hold: files and bytes. */
20
+ export const STAGES_HELD = {
21
+ folder: { count: 100, bytes: 2 * STAGES_MOST.bytes },
22
+ all: { count: 5000, bytes: 2 * 1024 * 1024 * 1024 },
23
+ };
24
+ /** The most a design's own folder of the sandboxed routes, and all of them together, may hold: files and bytes. */
25
+ export const ROUTES_HELD = {
26
+ folder: { count: 100, bytes: 2 * ROUTES_MOST.bytes },
27
+ all: { count: 5000, bytes: 512 * 1024 * 1024 },
28
+ };
29
+
30
+ /**
31
+ * The files to remove from a cache before a file of `n` bytes is written in the design's own folder, so that the folder
32
+ * and the cache stay within `held`: the folder's own oldest first, then the oldest of the whole cache. Null when there
33
+ * is no room for it at all.
34
+ * @param {{ p: string, own: boolean, t: number, size: number }[]} files what the cache holds now (t: when last used)
35
+ * @param {number} n @param {{ folder: { count: number, bytes: number }, all: { count: number, bytes: number } }} held
36
+ * @returns {string[] | null}
37
+ */
38
+ export function roomFor(files, n, held) {
39
+ if (!(n <= held.folder.bytes && n <= held.all.bytes && held.folder.count >= 1 && held.all.count >= 1)) return null;
40
+ const gone = new Set(),
41
+ oldest = (list) => [...list].sort((a, b) => a.t - b.t);
42
+ const free = (list, most) => {
43
+ let count = list.filter((f) => !gone.has(f.p)).length + 1,
44
+ bytes = list.reduce((s, f) => s + (gone.has(f.p) ? 0 : f.size), n);
45
+ for (const f of oldest(list)) {
46
+ if (count <= most.count && bytes <= most.bytes) break;
47
+ if (gone.has(f.p)) continue;
48
+ gone.add(f.p);
49
+ count--;
50
+ bytes -= f.size;
51
+ }
52
+ };
53
+ free(
54
+ files.filter((f) => f.own),
55
+ held.folder,
56
+ );
57
+ free(files, held.all);
58
+ return [...gone];
59
+ }
60
+
61
+ /**
62
+ * A store for what a run makes, keeping each key's text while the store stays within `most` (a text that would take
63
+ * it past is not kept; a key kept again replaces its text).
64
+ * @param {{ count: number, bytes: number }} most
65
+ * @returns {{ kept: Record<string, string>, put: (k: string, text: string) => void }}
66
+ */
67
+ export function keeper(most) {
68
+ const kept = Object.create(null),
69
+ size = Object.create(null);
70
+ let count = 0,
71
+ bytes = 0;
72
+ return {
73
+ kept,
74
+ put: (k, text) => {
75
+ const n = Buffer.byteLength(text, 'utf8'),
76
+ had = k in size;
77
+ if ((had ? count : count + 1) > most.count || bytes - (size[k] || 0) + n > most.bytes) return;
78
+ if (!had) count++;
79
+ bytes += n - (size[k] || 0);
80
+ size[k] = n;
81
+ kept[k] = text;
82
+ },
83
+ };
84
+ }