@rulecad/cad 0.10.0 → 0.11.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 +8 -4
- package/CHANGELOG.md +285 -1
- package/EXAMPLES.md +101 -89
- package/README.md +104 -27
- package/bin/cad.mjs +41 -13
- package/lib/assembly.mjs +418 -101
- package/lib/bays.mjs +124 -0
- package/lib/cache-file.mjs +66 -23
- package/lib/clone.mjs +93 -11
- package/lib/design.mjs +40 -4
- package/lib/edit.mjs +5 -3
- package/lib/framebuild.mjs +72 -91
- package/lib/framecheck.mjs +154 -77
- package/lib/framesize.mjs +37 -1
- package/lib/gait.mjs +17 -7
- package/lib/guides.mjs +84 -50
- package/lib/live-guard.mjs +18 -8
- package/lib/measured.mjs +2 -1
- package/lib/mesh.mjs +156 -56
- package/lib/motors.mjs +17 -4
- package/lib/node-version.mjs +52 -0
- package/lib/order.mjs +1 -2
- package/lib/picture-servers.mjs +66 -0
- package/lib/power.mjs +14 -7
- package/lib/preview.mjs +12 -25
- package/lib/programs.mjs +113 -0
- package/lib/sandbox-guard.mjs +43 -0
- package/lib/sandbox.mjs +172 -12
- package/lib/targets/gist.mjs +9 -6
- package/lib/versions.mjs +106 -10
- package/lib/workspace.mjs +3 -1
- package/package.json +13 -13
- package/schemas/assembly.schema.json +5 -4
- package/schemas/object.schema.json +1 -1
package/lib/sandbox.mjs
CHANGED
|
@@ -30,6 +30,7 @@ import { fromFaces } from './parts.mjs';
|
|
|
30
30
|
import { located } from './errors.mjs';
|
|
31
31
|
import { STAGES_MOST, ROUTES_MOST } from './kept.mjs';
|
|
32
32
|
import { ALLOWED } from './sandbox-hooks.mjs';
|
|
33
|
+
import { whereIs } from './programs.mjs';
|
|
33
34
|
|
|
34
35
|
const flag = (() => {
|
|
35
36
|
const f = process.allowedNodeEnvironmentFlags;
|
|
@@ -440,6 +441,141 @@ export function networkWarning(version = process.version) {
|
|
|
440
441
|
}
|
|
441
442
|
let warned = false; // (said once a run)
|
|
442
443
|
|
|
444
|
+
// What a sandbox that stopped without a reply printed, in a few words: Node's own error line where it printed one (its
|
|
445
|
+
// stack dump ends in a brace and Node's version), else its last three lines
|
|
446
|
+
function lastWords(said) {
|
|
447
|
+
const lines = said.trim().split('\n');
|
|
448
|
+
return (
|
|
449
|
+
lines.find((l) => /^\s*(?:[A-Z]\w*)?Error\b.*:/.test(l))?.trim() ||
|
|
450
|
+
lines.slice(-3).join(' ') ||
|
|
451
|
+
'it printed nothing'
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Every process started from here that this command has not yet seen end: a design's sandboxed process, the guard
|
|
456
|
+
// (below), a look at the design's memory (processMemory, watchMemory). A command that returned while one of them was
|
|
457
|
+
// still running left it behind for a moment in the folder the command ran in, the workspace, and Windows will not move
|
|
458
|
+
// or remove a folder a live process sits in: a script that ran `cad check` and removed the folder straight after was
|
|
459
|
+
// refused now and then (EPERM, issue #687). The design's own process was always waited for (runSandboxed settles when
|
|
460
|
+
// it has closed); the guard and a look in flight were not. So before this command returns it ends each one and waits
|
|
461
|
+
// until it has gone (endSandbox): when Node has nothing left to do ('beforeExit', in whatever script uses this
|
|
462
|
+
// library), and from the command itself before it exits on an error (bin/cad.mjs), which 'beforeExit' never sees.
|
|
463
|
+
// A command killed from outside can do none of this: there the guard ends the design's process, as before.
|
|
464
|
+
const ours = new Map(); // (each with how it is asked to end: 'kill', or 'close' for one that ends when its input closes)
|
|
465
|
+
function ourOwn(child, how = 'kill') {
|
|
466
|
+
if (child.pid == null) return child; // (it never started, as its 'error' says: nothing to wait for)
|
|
467
|
+
ours.set(child, how);
|
|
468
|
+
child.once('exit', () => ours.delete(child));
|
|
469
|
+
return child;
|
|
470
|
+
}
|
|
471
|
+
// How long a process that ends when its input closes has to go before it is stopped outright, and how long one stopped
|
|
472
|
+
// outright is waited for before the command returns without it (rather than never return), in milliseconds
|
|
473
|
+
const END_GRACE = 2000,
|
|
474
|
+
END_WAIT = 5000;
|
|
475
|
+
// The helpers started from here (the guard, the looks at memory) sit in a folder outside any workspace, never the one
|
|
476
|
+
// the command runs in: Node's own, which is there for as long as this command runs and which a running Node holds in
|
|
477
|
+
// place anyway. So even where the guard outlives the command (one killed from outside), it holds nothing of the
|
|
478
|
+
// workspace. And a program named without its folder (ps, tasklist) is looked for, on Windows, in the working folder it
|
|
479
|
+
// is given before the PATH: Node's own, then, not a workspace's, where a file of that name could be waiting. (They are
|
|
480
|
+
// started by the full path programs.mjs finds on PATH all the same, which passes over PATH's folders in the workspace.)
|
|
481
|
+
const AWAY = path.dirname(process.execPath);
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* End every process the sandbox has started that is still running, and wait until each has gone: a design still
|
|
485
|
+
* running and a look at its memory are stopped outright; the guard is ended by closing its input, and stopped outright
|
|
486
|
+
* if it has not gone within two seconds. A command calls this before it returns, so that nothing of it is left in the
|
|
487
|
+
* workspace's folder, which can then be moved or removed at once (Windows refuses while a process sits in it). Node
|
|
488
|
+
* calls it by itself when a script that used the sandbox has nothing left to do; a command that exits by
|
|
489
|
+
* process.exit() awaits it first. It never rejects, and gives up on a process that will not end after five seconds
|
|
490
|
+
* more.
|
|
491
|
+
* @returns {Promise<void>}
|
|
492
|
+
*/
|
|
493
|
+
export function endSandbox() {
|
|
494
|
+
const gone = [...ours].map(
|
|
495
|
+
([child, how]) =>
|
|
496
|
+
new Promise((resolve) => {
|
|
497
|
+
if (child.exitCode != null || child.signalCode != null) return resolve();
|
|
498
|
+
let timer = null;
|
|
499
|
+
const ended = () => {
|
|
500
|
+
clearTimeout(timer);
|
|
501
|
+
resolve();
|
|
502
|
+
};
|
|
503
|
+
const stop = () => {
|
|
504
|
+
try {
|
|
505
|
+
child.kill('SIGKILL');
|
|
506
|
+
} catch {
|
|
507
|
+
// (it has gone already)
|
|
508
|
+
}
|
|
509
|
+
timer = setTimeout(() => {
|
|
510
|
+
ours.delete(child); // (it will not end: left, rather than have the command never return)
|
|
511
|
+
child.removeListener('exit', ended);
|
|
512
|
+
child.unref();
|
|
513
|
+
resolve();
|
|
514
|
+
}, END_WAIT);
|
|
515
|
+
};
|
|
516
|
+
child.once('exit', ended);
|
|
517
|
+
child.ref(); // (the guard doesn't keep the command running: the command waits for it now all the same)
|
|
518
|
+
if (how === 'close' && child.stdin?.writable) {
|
|
519
|
+
if (guard === child) guard = null; // (a design run after this starts a guard of its own)
|
|
520
|
+
child.stdin.end();
|
|
521
|
+
timer = setTimeout(stop, END_GRACE);
|
|
522
|
+
} else stop();
|
|
523
|
+
}),
|
|
524
|
+
);
|
|
525
|
+
return Promise.all(gone).then(() => {});
|
|
526
|
+
}
|
|
527
|
+
process.on('beforeExit', () => {
|
|
528
|
+
if (ours.size) endSandbox(); // (Node goes on until each has gone, then comes back here with nothing left)
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
// The guard (sandbox-guard.mjs): one small process a command, started with the first design it runs sandboxed, that
|
|
532
|
+
// ends every sandboxed process still running when this command ends, however it ends. The time and memory limits are
|
|
533
|
+
// kept from here, so a command killed from outside (a closed terminal, a test or CI job stopped) would otherwise leave
|
|
534
|
+
// a design that loops for ever running, on a processor core, until someone killed it by hand. It is told of each
|
|
535
|
+
// process as it starts and ends, and holds the other end of a pipe from here: when the pipe closes, it ends them.
|
|
536
|
+
// Neither it nor its pipe keeps this command running; a command that comes to its own end closes the pipe and waits
|
|
537
|
+
// for the guard to go before it returns (endSandbox).
|
|
538
|
+
let guard = null;
|
|
539
|
+
function guardChild(pid) {
|
|
540
|
+
if (!guard) {
|
|
541
|
+
try {
|
|
542
|
+
guard = spawn(process.execPath, [fileURLToPath(new URL('./sandbox-guard.mjs', import.meta.url))], {
|
|
543
|
+
cwd: AWAY,
|
|
544
|
+
stdio: ['pipe', 'ignore', 'ignore'],
|
|
545
|
+
env: Object.fromEntries(
|
|
546
|
+
['PATH', 'SystemRoot', 'windir'].filter((k) => process.env[k] != null).map((k) => [k, process.env[k]]),
|
|
547
|
+
),
|
|
548
|
+
windowsHide: true,
|
|
549
|
+
});
|
|
550
|
+
} catch {
|
|
551
|
+
guard = null;
|
|
552
|
+
return () => {}; // (no guard: the run goes on as before, its limits kept from here)
|
|
553
|
+
}
|
|
554
|
+
const g = ourOwn(guard, 'close');
|
|
555
|
+
g.on('error', () => {
|
|
556
|
+
if (guard === g) guard = null; // (it couldn't start, or has gone: the next run starts another)
|
|
557
|
+
});
|
|
558
|
+
g.on('exit', () => {
|
|
559
|
+
if (guard === g) guard = null;
|
|
560
|
+
});
|
|
561
|
+
g.stdin.on('error', () => {
|
|
562
|
+
if (guard === g) guard = null;
|
|
563
|
+
});
|
|
564
|
+
g.unref();
|
|
565
|
+
g.stdin.unref?.();
|
|
566
|
+
}
|
|
567
|
+
const g = guard,
|
|
568
|
+
say = (line) => {
|
|
569
|
+
try {
|
|
570
|
+
if (g.stdin.writable) g.stdin.write(`${line}\n`);
|
|
571
|
+
} catch {
|
|
572
|
+
// (the guard has gone: nothing to tell it)
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
say(`+${pid}`);
|
|
576
|
+
return () => say(`-${pid}`);
|
|
577
|
+
}
|
|
578
|
+
|
|
443
579
|
// How long a design may run and how much memory it may use, in the sandbox. A design with an endless loop would
|
|
444
580
|
// otherwise hang every command that builds it, with no word said, and one that fills memory without end would grow
|
|
445
581
|
// until the system stopped it, taking other work with it. Past either limit the design's process is stopped, and the
|
|
@@ -499,9 +635,12 @@ const heldBack = (n, unit) =>
|
|
|
499
635
|
/**
|
|
500
636
|
* The memory a process holds, in bytes (its resident set: what it has in the machine's memory), or null where that
|
|
501
637
|
* can't be told. Read from /proc on Linux, from ps on other Unix systems and from tasklist on Windows.
|
|
502
|
-
* @param {number} pid
|
|
638
|
+
* @param {number} pid
|
|
639
|
+
* @param {(look: import('node:child_process').ChildProcess) => void} [started] given the process that looks, where
|
|
640
|
+
* one is started (ps, tasklist), for a caller that may want to end it before it answers
|
|
641
|
+
* @returns {Promise<number | null>}
|
|
503
642
|
*/
|
|
504
|
-
export function processMemory(pid) {
|
|
643
|
+
export function processMemory(pid, started) {
|
|
505
644
|
return new Promise((resolve) => {
|
|
506
645
|
if (process.platform === 'linux') {
|
|
507
646
|
try {
|
|
@@ -519,10 +658,13 @@ export function processMemory(pid) {
|
|
|
519
658
|
(t) => /"([\d.,\s\u00a0]+)\s*K"\s*$/m.exec(t)?.[1],
|
|
520
659
|
]
|
|
521
660
|
: ['ps', ['-o', 'rss=', '-p', String(pid)], (t) => /^\s*(\d+)\s*$/.exec(t)?.[1]];
|
|
522
|
-
|
|
661
|
+
const exe = whereIs(cmd); // (by its full path on PATH, never one in the workspace: programs.mjs)
|
|
662
|
+
if (!exe) return resolve(null);
|
|
663
|
+
const look = execFile(exe, args, { cwd: AWAY, windowsHide: true, timeout: 5000 }, (e, text) => {
|
|
523
664
|
const kb = e ? null : read(String(text));
|
|
524
665
|
resolve(kb ? Number(kb.replace(/\D/g, '')) * 1024 : null);
|
|
525
666
|
});
|
|
667
|
+
started?.(ourOwn(look));
|
|
526
668
|
});
|
|
527
669
|
}
|
|
528
670
|
// How often the design's process's memory is looked at, in milliseconds
|
|
@@ -537,7 +679,8 @@ const FOOTPRINT = '/usr/bin/footprint';
|
|
|
537
679
|
*/
|
|
538
680
|
function watchMemory(pid, most, over) {
|
|
539
681
|
let looking = false,
|
|
540
|
-
ended = false
|
|
682
|
+
ended = false,
|
|
683
|
+
look = null; // (the look in flight, where it is a process)
|
|
541
684
|
const past = (held) => {
|
|
542
685
|
if (!ended && held != null && held > most) {
|
|
543
686
|
ended = true;
|
|
@@ -547,8 +690,11 @@ function watchMemory(pid, most, over) {
|
|
|
547
690
|
const every = setInterval(() => {
|
|
548
691
|
if (looking || ended) return;
|
|
549
692
|
looking = true;
|
|
550
|
-
processMemory(pid
|
|
693
|
+
processMemory(pid, (p) => {
|
|
694
|
+
look = p;
|
|
695
|
+
}).then((held) => {
|
|
551
696
|
looking = false;
|
|
697
|
+
look = null;
|
|
552
698
|
past(held);
|
|
553
699
|
});
|
|
554
700
|
}, WATCH_EVERY);
|
|
@@ -556,9 +702,11 @@ function watchMemory(pid, most, over) {
|
|
|
556
702
|
let deep = null;
|
|
557
703
|
if (process.platform === 'darwin' && fs.existsSync(FOOTPRINT)) {
|
|
558
704
|
deep = spawn(FOOTPRINT, ['--sample', '1', '--noCategories', '-f', 'bytes', '-p', String(pid)], {
|
|
705
|
+
cwd: AWAY,
|
|
559
706
|
stdio: ['ignore', 'pipe', 'ignore'],
|
|
560
707
|
});
|
|
561
708
|
deep.on('error', () => {}); // (without it, ps alone)
|
|
709
|
+
ourOwn(deep);
|
|
562
710
|
let text = '';
|
|
563
711
|
deep.stdout.on('data', (b) => {
|
|
564
712
|
text += b;
|
|
@@ -572,6 +720,7 @@ function watchMemory(pid, most, over) {
|
|
|
572
720
|
return () => {
|
|
573
721
|
ended = true;
|
|
574
722
|
clearInterval(every);
|
|
723
|
+
look?.kill(); // (its answer is no longer wanted, and the command waits for every process it started: endSandbox)
|
|
575
724
|
if (deep && deep.exitCode == null) deep.kill();
|
|
576
725
|
};
|
|
577
726
|
}
|
|
@@ -595,6 +744,7 @@ const HEAP_DEATH = /JavaScript heap out of memory|Reached heap limit|Ineffective
|
|
|
595
744
|
* @param {{ frames?: object, fixings?: { sets?: object, printer?: object, material?: string | null } | null }} [request.bound] what the design's helpers are bound to: frames, the workspace's profile sets (lib/frames.mjs framesContext); fixings, what v.fixings is bound to (design.mjs fixingsContext), whose calls come back as fixings. A board's v.pcb.mount reads fixings.sets too
|
|
596
745
|
* @param {{ routes?: { dir: string, shared?: string | null, put: (k: string, text: string) => void } | null } | null} [request.board] set for a board design (engine pcb): routes, where the autorouter's results are kept (the child reads this design's own folder, dir, then the route cache the tool's own process writes, shared; what it routes is handed back and put in dir only)
|
|
597
746
|
* @param {string[]} [request.read] more folders the sandbox may read (a copy of a workspace held in memory)
|
|
747
|
+
* @param {string | null} [request.runner] the script the sandbox runs in place of sandbox-run.mjs (tests: a runner that fails), a file the sandbox may read
|
|
598
748
|
* @returns {Promise<{ parts: object[], read: Set<string>, imports: string[], mechanism: object | null, fixings: object[] | null, board?: { circuit: object[], matrix?: object, source?: object }, mounted?: boolean }>}
|
|
599
749
|
* imports: the files of its project the design imported, each by its path inside the project ('objects/box.js')
|
|
600
750
|
*/
|
|
@@ -608,6 +758,7 @@ export function runSandboxed({
|
|
|
608
758
|
bound = {},
|
|
609
759
|
board = null,
|
|
610
760
|
read: more = [],
|
|
761
|
+
runner = null,
|
|
611
762
|
}) {
|
|
612
763
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
613
764
|
const routes = board?.routes?.shared ? [path.resolve(board.routes.shared)] : []; // (a route cache the user put outside the workspace, RULECAD_ROUTE_CACHE: read there, or every build routes again)
|
|
@@ -658,7 +809,7 @@ export function runSandboxed({
|
|
|
658
809
|
...(inThread ? [] : ['--allow-worker']),
|
|
659
810
|
'--import',
|
|
660
811
|
pathToFileURL(path.join(here, 'sandbox-preload.mjs')).href,
|
|
661
|
-
path.join(here, 'sandbox-run.mjs'),
|
|
812
|
+
runner || path.join(here, 'sandbox-run.mjs'),
|
|
662
813
|
];
|
|
663
814
|
// The child gets none of this process's environment but what Node needs to start: tokens and keys stay out of it
|
|
664
815
|
const env = Object.fromEntries(
|
|
@@ -667,10 +818,13 @@ export function runSandboxed({
|
|
|
667
818
|
.map((k) => [k, process.env[k]]),
|
|
668
819
|
);
|
|
669
820
|
return new Promise((resolve, reject) => {
|
|
670
|
-
const child =
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
821
|
+
const child = ourOwn(
|
|
822
|
+
spawn(process.execPath, args, {
|
|
823
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
824
|
+
env: { ...env, CAD_SANDBOX_PROJECT: project, NODE_NO_WARNINGS: '1' },
|
|
825
|
+
}),
|
|
826
|
+
); // (its stderr is passed on: what the design prints, not Node's notes on its own flags)
|
|
827
|
+
const unguard = child.pid ? guardChild(child.pid) : () => {}; // (it ends when this command does)
|
|
674
828
|
const MB = 1024 * 1024,
|
|
675
829
|
memoryMessage = (what) =>
|
|
676
830
|
`the design ${what}, so it was stopped: it may make shapes or lists without end. If it needs more, raise the limit: "designMemory" (megabytes) in cad.config.json, or CAD_DESIGN_MEMORY${heldBack(limits.held?.memory, 'MB')}`;
|
|
@@ -710,6 +864,7 @@ export function runSandboxed({
|
|
|
710
864
|
const done = () => {
|
|
711
865
|
clearTimeout(timer);
|
|
712
866
|
unwatch();
|
|
867
|
+
unguard();
|
|
713
868
|
};
|
|
714
869
|
// Why the process was stopped from here, said as an error naming the design and the limit
|
|
715
870
|
const stopped = (why) =>
|
|
@@ -736,7 +891,10 @@ export function runSandboxed({
|
|
|
736
891
|
done();
|
|
737
892
|
reject(e);
|
|
738
893
|
});
|
|
739
|
-
|
|
894
|
+
// A process that ends before it has read its request (it couldn't start, or this Node can't run it) closes the
|
|
895
|
+
// pipe the request goes down, and the write fails: that is said, as any other end without a reply is, when it closes
|
|
896
|
+
child.stdin.on('error', () => {});
|
|
897
|
+
child.on('close', (status, signal) => {
|
|
740
898
|
done();
|
|
741
899
|
if (over) return reject(stopped(over));
|
|
742
900
|
let r;
|
|
@@ -755,7 +913,9 @@ export function runSandboxed({
|
|
|
755
913
|
);
|
|
756
914
|
return reject(
|
|
757
915
|
located(
|
|
758
|
-
new Error(
|
|
916
|
+
new Error(
|
|
917
|
+
`the design's sandbox stopped (${status != null ? `exit code ${status}` : `signal ${signal}`}) before it sent back the design's shapes: ${lastWords(said)}`,
|
|
918
|
+
),
|
|
759
919
|
d.js,
|
|
760
920
|
'design.sandbox',
|
|
761
921
|
),
|
package/lib/targets/gist.mjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
// Short links through a secret GitHub Gist (unlisted: only people with the link find it), made with the gh command
|
|
2
|
-
|
|
1
|
+
// Short links through a secret GitHub Gist (unlisted: only people with the link find it), made with the gh command
|
|
2
|
+
// (found on PATH, never in the workspace: ../programs.mjs).
|
|
3
|
+
import { runProgram } from '../programs.mjs';
|
|
3
4
|
|
|
4
5
|
export async function shorten(data, { desc }) {
|
|
5
|
-
const out =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const out = runProgram(
|
|
7
|
+
'gh',
|
|
8
|
+
['gist', 'create', '--filename', 'model.txt', '--desc', desc, '-'],
|
|
9
|
+
{ input: data, encoding: 'utf8' },
|
|
10
|
+
"gh, GitHub's command that makes the short link's Gist,",
|
|
11
|
+
).trim();
|
|
9
12
|
return out.split('/').pop();
|
|
10
13
|
}
|
package/lib/versions.mjs
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
// A project release (keyboard@1.0.0) records which version of each object it contains.
|
|
6
6
|
import fs from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
8
|
-
import { execFileSync } from 'node:child_process';
|
|
9
8
|
import { createRequire } from 'node:module';
|
|
10
9
|
import { fileURLToPath } from 'node:url';
|
|
11
10
|
import crypto from 'node:crypto';
|
|
@@ -15,9 +14,87 @@ import { readJson, readOwnText } from './host.mjs';
|
|
|
15
14
|
import { plainName, isPlainName, isWorkspacePath } from './names.mjs';
|
|
16
15
|
import { readLayout, hasLayout } from './layout.mjs';
|
|
17
16
|
import { newer } from '@rulecad/versions';
|
|
17
|
+
import { runProgram } from './programs.mjs';
|
|
18
|
+
import { notOwn, writeOwnFile, removeOwnFolder } from './cache-file.mjs';
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
// git, found by cad itself on PATH and never in the workspace (programs.mjs), run in the workspace
|
|
21
|
+
const GIT = 'git, which keeps releases,';
|
|
22
|
+
const runGit = (args) =>
|
|
23
|
+
runProgram('git', args, { cwd: WS, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }, GIT).trimEnd();
|
|
24
|
+
|
|
25
|
+
// git's own settings can name programs for it to run, and a workspace that comes with its .git folder (an archive, a
|
|
26
|
+
// copy) brings its .git/config and .git/hooks with it. Of the commands cad runs to read a workspace's releases and
|
|
27
|
+
// changes, git status (and write-tree) start such programs: the file system monitor (core.fsmonitor), a filter driver
|
|
28
|
+
// that cleans each file it compares (filter.<name>.clean, .process, chosen by the workspace's .gitattributes) and the
|
|
29
|
+
// post-index-change hook. So every git command cad runs to read has them closed on its command line: the monitor
|
|
30
|
+
// off, hooks looked for in a file (so none is found), no index written back (--no-optional-locks), no pager, and every
|
|
31
|
+
// filter and diff program git's settings name blanked. (for-each-ref, tag -l, show <tag>:<file>, ls-tree, rev-parse,
|
|
32
|
+
// cat-file and var start none of them; they are closed all the same.) What a release commits, tags and pushes runs as
|
|
33
|
+
// those commands would by hand: gitAsByHand.
|
|
34
|
+
let closing = null;
|
|
35
|
+
// A list of git's settings (git config --list -z, as bytes) cut into its entries
|
|
36
|
+
function split(list) {
|
|
37
|
+
const out = [];
|
|
38
|
+
for (let at = 0; at < list.length;) {
|
|
39
|
+
const end = list.indexOf(0, at);
|
|
40
|
+
out.push(list.subarray(at, end < 0 ? list.length : end));
|
|
41
|
+
at = end < 0 ? list.length : end + 1;
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
function closed() {
|
|
46
|
+
if (closing) return closing;
|
|
47
|
+
const args = [
|
|
48
|
+
'--no-pager',
|
|
49
|
+
'--no-optional-locks',
|
|
50
|
+
'-c',
|
|
51
|
+
'core.fsmonitor=', // (empty, not false: git before 2.36 would run a program named "false" as the monitor)
|
|
52
|
+
'-c',
|
|
53
|
+
`core.hooksPath=${process.execPath}`,
|
|
54
|
+
];
|
|
55
|
+
// (as bytes, not text: a name in the settings that is not UTF-8 could not be given back to git as written; and with
|
|
56
|
+
// room for a long list: cut short, it would leave the programs named past the cut running)
|
|
57
|
+
let list;
|
|
58
|
+
try {
|
|
59
|
+
list = runProgram(
|
|
60
|
+
'git',
|
|
61
|
+
['--no-pager', 'config', '--list', '-z'],
|
|
62
|
+
{
|
|
63
|
+
cwd: WS,
|
|
64
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
65
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
66
|
+
},
|
|
67
|
+
GIT,
|
|
68
|
+
);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
if (e.code === 'ENOENT') throw e; // (git missing: said in plain words)
|
|
71
|
+
// (settings git can't read, or too many to read: git is not run on them, as cad could not tell what they start)
|
|
72
|
+
throw new Error(
|
|
73
|
+
`git could not list its settings for this workspace, so cad does not run git here (they may name programs for git to start). ${String(e.stderr || e.message).trim()} Look at the settings (git config --list --show-origin) and put right what is wrong`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
for (const bytes of split(list)) {
|
|
77
|
+
const raw = bytes.subarray(0, bytes.indexOf(10) < 0 ? bytes.length : bytes.indexOf(10)),
|
|
78
|
+
key = raw.toString('utf8'),
|
|
79
|
+
m = /^filter\.(.*)\.(?:clean|smudge|process)$/s.exec(key) || /^diff\.(.*)\.(?:textconv|command)$/s.exec(key);
|
|
80
|
+
if (!m && key !== 'diff.external') continue;
|
|
81
|
+
if (key.includes('=') || /[\r\n]/.test(key) || !Buffer.from(key, 'utf8').equals(raw))
|
|
82
|
+
throw new Error(
|
|
83
|
+
`git's settings for this workspace name a program under a name cad cannot switch off (${JSON.stringify(key)}), so git is not run. Look at the settings (git config --list --show-origin) and remove that one`,
|
|
84
|
+
);
|
|
85
|
+
args.push('-c', `${key}=`);
|
|
86
|
+
if (key.startsWith('filter.')) args.push('-c', `filter.${m[1]}.required=false`);
|
|
87
|
+
}
|
|
88
|
+
return (closing = args);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A git command, run in the workspace with every program git's settings name closed (above). */
|
|
92
|
+
export const git = (...args) => runGit([...closed(), ...args]);
|
|
93
|
+
/**
|
|
94
|
+
* A git command run as it would be by hand, the repository's hooks and settings in force: only for what a release
|
|
95
|
+
* commits, tags and pushes (the cad README, "Programs cad starts"), where the user's own hooks and signing are wanted.
|
|
96
|
+
*/
|
|
97
|
+
export const gitAsByHand = (...args) => runGit(args);
|
|
21
98
|
|
|
22
99
|
// ---------- semver: @rulecad/versions ----------
|
|
23
100
|
export { parse, bump, newer, bigger, compare } from '@rulecad/versions';
|
|
@@ -128,11 +205,31 @@ const importedBy = (from, text) =>
|
|
|
128
205
|
// projects/<project>/objects/<object>.js, …/rules/x1c.json), so that what the design imports by a path is found there
|
|
129
206
|
// as it was, and what it imports from a package is found in the workspace's node_modules, above.
|
|
130
207
|
// put: a file of the release written there from its tag (false when the tag has no such file)
|
|
208
|
+
// A workspace cloned from someone else may bring a .versions/ of its own, and a link there (.versions itself, a
|
|
209
|
+
// version's folder, a folder in its tree, one of its files) would have the tool write a file of the stranger's choosing
|
|
210
|
+
// outside the workspace, remove a folder there, or read a file from elsewhere as the release's design (and send it out
|
|
211
|
+
// in a model link). So nothing under .versions/ is written, read or removed through a link (cache-file.mjs notOwn,
|
|
212
|
+
// the guard .cache/ has too): one found is refused, named, and nothing is written or removed (issue #696).
|
|
213
|
+
function linkRefused(bad) {
|
|
214
|
+
const name = path.relative(WS, bad) || bad;
|
|
215
|
+
throw Object.assign(
|
|
216
|
+
new Error(
|
|
217
|
+
`${name} is a link (or not the plain folder or file cad makes there: a file with a second name elsewhere, say), so cad does not unpack or read a released version through it: it could lead outside this workspace (${WS}). cad keeps its copies of released versions in .versions/ and makes them again when they are needed: look at where the link leads, then remove the .versions folder and run the command again`,
|
|
218
|
+
),
|
|
219
|
+
{ code: 'versions.link', file: bad },
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
// A path under .versions/ that may be read or written: it, and every folder on its way, plain or not there yet
|
|
223
|
+
const ownVersionPath = (p, folder = false) => {
|
|
224
|
+
const bad = notOwn(p, WS, { folder });
|
|
225
|
+
if (bad) linkRefused(bad);
|
|
226
|
+
return p;
|
|
227
|
+
};
|
|
131
228
|
function treeOf(tag, base) {
|
|
132
229
|
const show = (f) => git('show', `${tag}:./${f}`); // (./: from the workspace, which may be below the repository's root)
|
|
133
230
|
const put = (f) => {
|
|
134
231
|
if (!isWorkspacePath(f)) return false;
|
|
135
|
-
const to = path.join(base, ...f.split('/'));
|
|
232
|
+
const to = ownVersionPath(path.join(base, ...f.split('/'))); // (a file there already is read: not through a link)
|
|
136
233
|
if (fs.existsSync(to)) return true;
|
|
137
234
|
let text;
|
|
138
235
|
try {
|
|
@@ -140,8 +237,7 @@ function treeOf(tag, base) {
|
|
|
140
237
|
} catch {
|
|
141
238
|
return false;
|
|
142
239
|
}
|
|
143
|
-
|
|
144
|
-
fs.writeFileSync(to, text);
|
|
240
|
+
writeOwnFile(to, text, linkRefused);
|
|
145
241
|
return true;
|
|
146
242
|
};
|
|
147
243
|
// A design's file, and those it imports by a path, in turn (as written: what it imports as it runs is in the record)
|
|
@@ -210,11 +306,11 @@ export function materialise(d, version) {
|
|
|
210
306
|
const base = paths.versions(d.project, `${d.name}@${version}`);
|
|
211
307
|
// A file already in the folder is used as it is (treeOf), so the folder is made afresh when it was written from
|
|
212
308
|
// another tag of the same name (one deleted and made again) or by an earlier version of the tool (another layout)
|
|
213
|
-
|
|
309
|
+
// (.versions, the project's folder there and the version's: plain folders, or not there yet; the same for its stamp)
|
|
310
|
+
const stamp = ownVersionPath(path.join(ownVersionPath(base, true), '.tag'));
|
|
214
311
|
if (fs.existsSync(base) && (!fs.existsSync(stamp) || fs.readFileSync(stamp, 'utf8') !== id))
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
fs.writeFileSync(stamp, id);
|
|
312
|
+
removeOwnFolder(base, linkRefused);
|
|
313
|
+
writeOwnFile(stamp, id, linkRefused);
|
|
218
314
|
const t = treeOf(tag, base);
|
|
219
315
|
// (names from the release's own file, each written as a file here: plain names only, never ../)
|
|
220
316
|
const object = JSON.parse(t.show(rel(d.json)));
|
package/lib/workspace.mjs
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// and what's generated) are separate folders.
|
|
3
3
|
// The workspace is $CAD_WORKSPACE, else the nearest folder at or above the current one with a cad.config.json, else
|
|
4
4
|
// the current folder.
|
|
5
|
-
// cad.config.json: { "printer": "x1c", "viewUrl": "https://…/", "targets": { "show": "targets/show.mjs", "shorten": "gist" } }
|
|
5
|
+
// cad.config.json: { "printer": "x1c", "engine": "manifold", "viewUrl": "https://…/", "targets": { "show": "targets/show.mjs", "shorten": "gist" } }
|
|
6
6
|
// printer the rule set a print is checked against (bed, overhang, bridges); a project.json's "printer" wins
|
|
7
|
+
// engine the boolean engine of designs whose object names none: "manifold" (with manifold-3d installed; a new
|
|
8
|
+
// workspace's) or "jscad", the default (design.mjs booleans)
|
|
7
9
|
// viewUrl the model page that links open (the public one by default: it reads links as data, nothing is uploaded)
|
|
8
10
|
// targets optional extras: show (put a model on display: a module of your own) and shorten (short links: "gist")
|
|
9
11
|
// designTimeout, designMemory how long a design may run in the sandbox (seconds) and the memory it may hold
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/cad",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "The cad command: JSCAD designs from traceable rules, checked for printing and fit, versioned by what fits, shared as links and exported as Bambu 3MF.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -60,25 +60,25 @@
|
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@rulecad/variables": "^0.4.0",
|
|
62
62
|
"@rulecad/keyboard-layout": "^0.2.0",
|
|
63
|
-
"@rulecad/printcheck": "^0.
|
|
64
|
-
"@rulecad/fit": "^0.5.
|
|
63
|
+
"@rulecad/printcheck": "^0.5.0",
|
|
64
|
+
"@rulecad/fit": "^0.5.1",
|
|
65
65
|
"@rulecad/model-link": "^0.4.0",
|
|
66
66
|
"@rulecad/versions": "^0.3.0",
|
|
67
|
-
"@rulecad/shapes": "^0.
|
|
68
|
-
"@rulecad/rules": "^0.
|
|
67
|
+
"@rulecad/shapes": "^0.7.0",
|
|
68
|
+
"@rulecad/rules": "^0.6.0",
|
|
69
69
|
"@jscad/modeling": "^2.12.0",
|
|
70
70
|
"three": "^0.186.1",
|
|
71
|
-
"@rulecad/fea": "^0.
|
|
72
|
-
"@rulecad/topopt": "^0.4.
|
|
73
|
-
"@rulecad/flexures": "^0.
|
|
74
|
-
"@rulecad/gears": "^0.
|
|
75
|
-
"@rulecad/motors": "^0.
|
|
76
|
-
"@rulecad/fixings": "^0.3.
|
|
71
|
+
"@rulecad/fea": "^0.5.0",
|
|
72
|
+
"@rulecad/topopt": "^0.4.2",
|
|
73
|
+
"@rulecad/flexures": "^0.8.0",
|
|
74
|
+
"@rulecad/gears": "^0.5.0",
|
|
75
|
+
"@rulecad/motors": "^0.3.0",
|
|
76
|
+
"@rulecad/fixings": "^0.3.1",
|
|
77
77
|
"@rulecad/robots": "^0.2.0",
|
|
78
|
-
"@rulecad/frames": "^0.
|
|
78
|
+
"@rulecad/frames": "^0.4.0"
|
|
79
79
|
},
|
|
80
80
|
"peerDependencies": {
|
|
81
|
-
"@rulecad/pcb": "^0.
|
|
81
|
+
"@rulecad/pcb": "^0.8.0"
|
|
82
82
|
},
|
|
83
83
|
"peerDependenciesMeta": {
|
|
84
84
|
"@rulecad/pcb": {
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"id": { "type": "string" },
|
|
49
49
|
"object": { "type": "string" },
|
|
50
50
|
"assembly": { "type": "string" },
|
|
51
|
-
"motor": { "type": "object", "required": ["model"], "additionalProperties": false, "description": "A motor from @rulecad/motors: bought, not printed; its shaft turns the joint it drives", "properties": { "model": { "type": "string" }, "drives": { "type": "string" }, "set": { "type": "object", "description": "Figures that differ from the catalogue's, by name" }, "speed": { "$ref": "#/$defs/expr", "description": "The motor's speed, rpm (default: what it does comfortably)" }, "margin": { "$ref": "#/$defs/expr", "description": "How many times over the torque must be (default 2)" }, "duty": { "$ref": "#/$defs/expr", "description": "The share of the time it runs, 0 to 1 (default 1), for its heat" }, "screw": { "type": "string", "description": "The screws that hold the motor, by a fixing's name: a thread through its plain holes, if not the size they take (M3), or a whole screw with its length and head (M3x8 hex), whose head sets the tool the order names" }, "driver": { "type": "string", "description": "The bought driver item this motor plugs into" }, "leadscrew": { "type": "object", "additionalProperties": false, "required": ["lead"], "properties": { "lead": { "$ref": "#/$defs/expr" }, "diameter": { "$ref": "#/$defs/expr" }, "friction": { "$ref": "#/$defs/expr" } } }, "belt": { "type": "object", "additionalProperties": false, "required": ["teeth"], "properties": { "teeth": { "$ref": "#/$defs/expr" }, "pitch": { "$ref": "#/$defs/expr" }, "stiffness": { "$ref": "#/$defs/expr", "description": "N per mm of strain, per mm of belt width" }, "width": { "$ref": "#/$defs/expr" }, "length": { "$ref": "#/$defs/expr" }, "load": { "$ref": "#/$defs/expr" } } }, "current": { "type": "number", "description": "
|
|
51
|
+
"motor": { "type": "object", "required": ["model"], "additionalProperties": false, "description": "A motor from @rulecad/motors: bought, not printed; its shaft turns the joint it drives", "properties": { "model": { "type": "string" }, "drives": { "type": "string" }, "set": { "type": "object", "description": "Figures that differ from the catalogue's, by name" }, "speed": { "$ref": "#/$defs/expr", "description": "The motor's speed, rpm (default: what it does comfortably)" }, "margin": { "$ref": "#/$defs/expr", "description": "How many times over the torque must be (default 2)" }, "duty": { "$ref": "#/$defs/expr", "description": "The share of the time it runs, 0 to 1 (default 1), for its heat" }, "screw": { "type": "string", "description": "The screws that hold the motor, by a fixing's name: a thread through its plain holes, if not the size they take (M3), or a whole screw with its length and head (M3x8 hex), whose head sets the tool the order names" }, "driver": { "type": "string", "description": "The bought driver item this motor plugs into" }, "leadscrew": { "type": "object", "additionalProperties": false, "required": ["lead"], "properties": { "lead": { "$ref": "#/$defs/expr" }, "diameter": { "$ref": "#/$defs/expr" }, "friction": { "$ref": "#/$defs/expr" } } }, "belt": { "type": "object", "additionalProperties": false, "required": ["teeth"], "properties": { "teeth": { "$ref": "#/$defs/expr" }, "pitch": { "$ref": "#/$defs/expr" }, "stiffness": { "$ref": "#/$defs/expr", "description": "N per mm of strain, per mm of belt width" }, "width": { "$ref": "#/$defs/expr" }, "length": { "$ref": "#/$defs/expr" }, "load": { "$ref": "#/$defs/expr" } } }, "current": { "type": "number", "description": "The peak current per coil, A, as set on the driver (default its rated current per phase); a TMC2209's trimmer is set for its RMS, about 0.71 of the peak" }, "cooled": { "type": "boolean", "description": "The driver has a heat sink and moving air" }, "microsteps": { "type": "number", "description": "Steps to a full step (default 16): resolution, not accuracy" } } },
|
|
52
52
|
"bought": { "type": "string", "description": "A part from a rule set (\"608\", \"drv8825\", \"fitting-motedis-end-cap-20-b6\"): bought, not printed; drawn faint from its figures, with anchors from what it is. A frame's bought fitting (an end cap, a levelling foot, a hinge) placed at a beam's address is checked against that beam's profile, and its price goes on cad list" },
|
|
53
53
|
"variant": { "type": "string" },
|
|
54
54
|
"set": { "type": "object" },
|
|
@@ -94,8 +94,9 @@
|
|
|
94
94
|
"fastener": { "description": "What does the fastening: in words (two push pins), named on the shopping list and the assembly step as written; or pins, { \"pin\": \"snap pin 4x6\", \"count\": 2 }, found in the parts' holes and checked (fit.pin): each hole against the one @rulecad/fixings draws for the pin and the printer, the pin's length against the parts, and the load; or magnets, { \"magnet\": \"disc magnet 8x3\", \"count\": 2 }, found in the parts' pockets and checked (fit.magnet): each pocket against the one @rulecad/fixings draws for the magnet and the printer, its depth and floor, and their pull against the load. A screw is a fasten's, which chooses its length and counts it", "anyOf": [{ "$ref": "#/$defs/pins" }, { "$ref": "#/$defs/magnets" }, { "type": "string" }] },
|
|
95
95
|
"count": { "$ref": "#/$defs/expr", "description": "With fastener: how many" },
|
|
96
96
|
"access": { "type": "string", "description": "<item>.<anchor>: where the screw's tool comes from, its z axis pointing out along the screw. Whatever stands on that line goes on after the screw; if nothing can, assembly.order says so" },
|
|
97
|
-
"min": { "$ref": "#/$defs/expr" },
|
|
98
|
-
"max": { "$ref": "#/$defs/expr" },
|
|
97
|
+
"min": { "$ref": "#/$defs/expr", "description": "The smallest gap between a and b (mm). Negative for a press fit: they may run into each other no deeper than this, as measured (-0.1 for a tenth of a millimetre). A min below -1 is refused: where they may overlap by any amount, give \"overlap\": \"allowed\" in its place" },
|
|
98
|
+
"max": { "$ref": "#/$defs/expr", "description": "The largest gap between a and b (mm). A gap outside min to max fails. Negative for a press fit that must grip: they run into each other at least this deep" },
|
|
99
|
+
"overlap": { "enum": ["allowed"], "description": "a and b may overlap by any amount (a rod through a part, an end cap's pegs in a beam, a reference part drawn into another): the overlap check leaves the pair alone, and a gap is still held to min and max where they are given" },
|
|
99
100
|
"note": { "type": "string" },
|
|
100
101
|
"label": { "type": "string" },
|
|
101
102
|
"poses": { "type": "array", "items": { "type": "string" } },
|
|
@@ -221,7 +222,7 @@
|
|
|
221
222
|
"backlash": { "$ref": "#/$defs/expr" },
|
|
222
223
|
"ring": { "type": "boolean" },
|
|
223
224
|
"bevel": { "type": "boolean" },
|
|
224
|
-
"worm": { "anyOf": [{ "type": "boolean" }, { "type": "object", "additionalProperties": false, "properties": { "diameter": { "$ref": "#/$defs/expr" }, "hand": { "enum": ["right", "left"] } } }] }
|
|
225
|
+
"worm": { "anyOf": [{ "type": "boolean" }, { "type": "object", "additionalProperties": false, "properties": { "diameter": { "$ref": "#/$defs/expr" }, "hand": { "enum": ["right", "left"] }, "flanks": { "enum": ["printed", "standard"] }, "overhang": { "$ref": "#/$defs/expr" } } }] }
|
|
225
226
|
}
|
|
226
227
|
}
|
|
227
228
|
}
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"material": { "type": "string" },
|
|
18
18
|
"orientation": { "type": "string", "description": "How it sits on the bed, and anything the printer or the person assembling it should know" },
|
|
19
19
|
"role": { "enum": ["part", "reference", "board"], "description": "reference: drawn faint and never printed (a switch, a tablet); board: a circuit board" },
|
|
20
|
-
"engine": { "enum": ["jscad", "manifold", "pcb"], "description": "manifold: v.ops booleans by Manifold (npm install manifold-3d); pcb: a circuit board" },
|
|
20
|
+
"engine": { "enum": ["jscad", "manifold", "pcb"], "description": "manifold: v.ops booleans by Manifold (npm install manifold-3d); jscad: by JSCAD, whatever cad.config.json's engine; left out, cad.config.json's engine (JSCAD if it names none); pcb: a circuit board" },
|
|
21
21
|
"printInPlace": { "type": "boolean", "description": "The parts print together, in the pose they are drawn in, and move apart once freed (a gearbox printed whole). cad check holds each part to a face of its own on the bed, every two parts to the printer's print_gap apart (measured by Manifold), and nothing to a support, since none can come out from between them" },
|
|
22
22
|
"variables": { "type": "object", "additionalProperties": { "$ref": "#/$defs/variable" } },
|
|
23
23
|
"display": { "type": "object", "description": "Settings the mirror and model page use instead (lighter meshes, no legends)" },
|