@rulecad/cad 0.8.0 → 0.9.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.
- package/AGENTS.md +24 -10
- package/CHANGELOG.md +404 -0
- package/EXAMPLES.md +40 -38
- package/README.md +193 -67
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2250 -512
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1847 -434
- package/lib/beams.mjs +256 -52
- package/lib/belts.mjs +66 -15
- package/lib/board-work.mjs +12 -0
- package/lib/bought.mjs +309 -53
- package/lib/buildpage.mjs +179 -29
- package/lib/carried.mjs +201 -0
- package/lib/clone.mjs +194 -42
- package/lib/design.mjs +649 -121
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +899 -191
- package/lib/errors.mjs +16 -3
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2702 -507
- package/lib/fittings.mjs +715 -121
- package/lib/fixings.mjs +21 -9
- package/lib/framebuild.mjs +790 -175
- package/lib/framecheck.mjs +1901 -368
- package/lib/framegen.mjs +100 -22
- package/lib/frameheat.mjs +225 -42
- package/lib/frameprint.mjs +513 -108
- package/lib/frames.mjs +81 -17
- package/lib/framesize.mjs +792 -120
- package/lib/frametip.mjs +235 -0
- package/lib/gait.mjs +135 -23
- package/lib/grip.mjs +62 -13
- package/lib/guides.mjs +1403 -230
- package/lib/host.mjs +123 -21
- package/lib/inplace.mjs +171 -36
- package/lib/jigs.mjs +360 -78
- package/lib/joins.mjs +697 -118
- package/lib/jsonedit.mjs +88 -26
- package/lib/kept.mjs +84 -0
- package/lib/kit.mjs +438 -68
- package/lib/layout.mjs +2 -1
- package/lib/links.mjs +149 -42
- package/lib/list.mjs +326 -64
- package/lib/live-guard.mjs +68 -0
- package/lib/live-shape.mjs +96 -24
- package/lib/live.mjs +189 -59
- package/lib/loads.mjs +579 -130
- package/lib/machine.mjs +372 -74
- package/lib/magnets.mjs +162 -44
- package/lib/measured.mjs +417 -94
- package/lib/mesh.mjs +526 -119
- package/lib/meshcmd.mjs +215 -60
- package/lib/motors.mjs +1021 -213
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +3 -1
- package/lib/optimise.mjs +978 -283
- package/lib/order.mjs +519 -112
- package/lib/panels.mjs +482 -94
- package/lib/parts.mjs +78 -23
- package/lib/pins.mjs +163 -44
- package/lib/plates.mjs +223 -35
- package/lib/player.mjs +37 -9
- package/lib/power.mjs +64 -13
- package/lib/preview.mjs +83 -16
- package/lib/profilemeasure.mjs +572 -129
- package/lib/rules.mjs +15 -5
- package/lib/sandbox-hooks.mjs +77 -19
- package/lib/sandbox-run.mjs +117 -18
- package/lib/sandbox.mjs +509 -60
- package/lib/schema.mjs +42 -11
- package/lib/seats.mjs +227 -49
- package/lib/services.mjs +261 -52
- package/lib/sheet.mjs +851 -178
- package/lib/slotnuts.mjs +213 -34
- package/lib/targets/gist.mjs +4 -1
- package/lib/targets.mjs +22 -4
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +111 -28
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +197 -12
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
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 {
|
|
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 = () => {
|
|
12
|
-
|
|
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,
|
|
26
|
+
const start = i,
|
|
27
|
+
c = text[i];
|
|
16
28
|
if (c === '{') {
|
|
17
|
-
i++;
|
|
29
|
+
i++;
|
|
30
|
+
ws();
|
|
18
31
|
if (text[i] === '}') i++;
|
|
19
|
-
else
|
|
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++;
|
|
43
|
+
i++;
|
|
44
|
+
ws();
|
|
22
45
|
if (text[i] === ']') i++;
|
|
23
|
-
else
|
|
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 {
|
|
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) => {
|
|
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) => ({
|
|
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)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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)
|
|
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] !== '{')
|
|
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,
|
|
79
|
-
|
|
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)
|
|
82
|
-
|
|
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),
|
|
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
|
|
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)
|
|
99
|
-
|
|
100
|
-
const
|
|
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
|
+
}
|