@rulecad/cad 0.7.97 → 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 +26 -10
- package/CHANGELOG.md +442 -2
- package/EXAMPLES.md +48 -42
- package/README.md +226 -73
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2253 -514
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1854 -437
- 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 +734 -113
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +900 -191
- package/lib/errors.mjs +16 -3
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2710 -502
- package/lib/fittings.mjs +715 -121
- package/lib/fixings.mjs +21 -9
- package/lib/framebuild.mjs +792 -176
- 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 +149 -18
- 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 +3 -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 +1022 -212
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +41 -0
- package/lib/optimise.mjs +979 -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 +224 -36
- 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 +38 -4
- package/lib/sandbox-hooks.mjs +154 -12
- package/lib/sandbox-preload.mjs +2 -2
- package/lib/sandbox-run.mjs +127 -12
- package/lib/sandbox.mjs +632 -35
- package/lib/schema.mjs +43 -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 +42 -6
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +113 -28
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +216 -17
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
- package/schemas/object.schema.json +1 -1
- package/schemas/project.schema.json +1 -1
package/lib/rules.mjs
CHANGED
|
@@ -1,27 +1,61 @@
|
|
|
1
1
|
// Rules from files: the variables and expressions of @rulecad/variables, with rule sets read from the workspace's rules/
|
|
2
2
|
// (then the tool's) by name. Everything but loading is in @rulecad/variables, which needs no files and runs anywhere.
|
|
3
|
+
import fs from 'node:fs';
|
|
3
4
|
import path from 'node:path';
|
|
4
5
|
import { host, readJson } from './host.mjs';
|
|
5
|
-
import { RULE_DIRS, WS } from './workspace.mjs';
|
|
6
|
+
import { RULE_DIRS, WS, readOwnText } from './workspace.mjs';
|
|
7
|
+
import { plainName } from './names.mjs';
|
|
6
8
|
import { resolve as resolveWith } from '@rulecad/variables';
|
|
7
9
|
|
|
8
10
|
export { ANCHORS, isName, options, checkName, evaluate, fmt } from '@rulecad/variables';
|
|
9
11
|
|
|
10
12
|
// dir: one folder (a release's own rules), else the workspace's rules, then the tool's. A rule set the tool provides
|
|
11
13
|
// is copied into the workspace the first time it's used: the workspace keeps every rule its designs depend on (so a
|
|
12
|
-
// release can record them in git), and your own measurements are edits to your copy.
|
|
14
|
+
// release can record them in git), and your own measurements are edits to your copy. The name is joined into both
|
|
15
|
+
// paths, so it must be plain (lib/names.mjs): a name with ../ would read a file from elsewhere and write its copy
|
|
16
|
+
// outside the workspace.
|
|
13
17
|
export function loadRuleSet(name, dir) {
|
|
18
|
+
plainName(name, 'rule set name');
|
|
14
19
|
const file = (dir ? [dir] : RULE_DIRS).map((d) => path.join(d, `${name}.json`)).find((f) => host.exists(f));
|
|
15
20
|
if (!file) throw new Error(`no rule set "${name}" (rules/${name}.json)`);
|
|
16
21
|
const own = path.join(WS, 'rules', `${name}.json`);
|
|
17
22
|
if (!dir && file !== own) {
|
|
18
|
-
|
|
23
|
+
insideWorkspace(own);
|
|
24
|
+
host.writeText(own, readOwnText(file));
|
|
19
25
|
console.error(`copied the ${name} rule set into rules/${name}.json: yours to keep and edit`);
|
|
20
26
|
}
|
|
21
27
|
return readJson(own);
|
|
22
28
|
}
|
|
23
|
-
|
|
29
|
+
/**
|
|
30
|
+
* `file` (a rule set about to be written into the workspace's rules/) if no folder or file on its way down from the
|
|
31
|
+
* workspace is a link, else an error naming the link. A workspace can come from anyone, and git can carry a link: a
|
|
32
|
+
* rules/ folder that links to a folder elsewhere, or a rules/<name>.json that links to a file not yet made, would have
|
|
33
|
+
* the copy written outside the workspace.
|
|
34
|
+
* @param {string} file
|
|
35
|
+
* @returns {string}
|
|
36
|
+
*/
|
|
37
|
+
export function insideWorkspace(file) {
|
|
38
|
+
if (host.name !== 'node') return file; // (a workspace in memory has no links)
|
|
39
|
+
const top = path.resolve(WS); // (the workspace's own folder may be reached by a link: only what is inside it counts)
|
|
40
|
+
for (let p = path.resolve(file); p.startsWith(top + path.sep); p = path.dirname(p)) {
|
|
41
|
+
let link = false;
|
|
42
|
+
try {
|
|
43
|
+
link = fs.lstatSync(p).isSymbolicLink();
|
|
44
|
+
} catch {
|
|
45
|
+
// (not there yet: nothing to follow)
|
|
46
|
+
}
|
|
47
|
+
if (link)
|
|
48
|
+
throw new Error(
|
|
49
|
+
`${path.relative(WS, p)} is a link to somewhere else, so nothing was written to ${path.relative(WS, file)}: it could land outside the workspace. Make ${path.relative(WS, p)} a folder or file of the workspace's own`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return file;
|
|
53
|
+
}
|
|
54
|
+
// A rule set read as it is, the workspace's if it has one: for figures a check only reads, leaving the workspace alone.
|
|
55
|
+
// (Both read through readJson, which reads a file of the workspace only where it really is inside it: a rules/x.json
|
|
56
|
+
// that is a link to a file elsewhere is refused, code workspace.outside. workspace.mjs ownFile.)
|
|
24
57
|
export function peekRuleSet(name) {
|
|
58
|
+
plainName(name, 'rule set name');
|
|
25
59
|
const file = RULE_DIRS.map((d) => path.join(d, `${name}.json`)).find((f) => host.exists(f));
|
|
26
60
|
if (!file) throw new Error(`no rule set "${name}" (rules/${name}.json)`);
|
|
27
61
|
return readJson(file);
|
package/lib/sandbox-hooks.mjs
CHANGED
|
@@ -1,35 +1,177 @@
|
|
|
1
1
|
// The sandbox's import hook: code in the design's project may import @jscad/modeling, @rulecad/shapes, @rulecad/flexures, @rulecad/gears, @rulecad/motors, @rulecad/frames, @rulecad/fixings and files of
|
|
2
2
|
// its own project, nothing else (no node: modules: no files, network or processes). The tool's own modules are free.
|
|
3
|
+
// An allowed package is named plainly and must lead inside that package: '@jscad/modeling/../../graceful-fs' would
|
|
4
|
+
// otherwise climb out of it to any file of the workspace, and a file outside the project isn't held to these rules.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
3
7
|
import { builtinModules } from 'node:module';
|
|
8
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
4
9
|
|
|
5
|
-
|
|
6
|
-
|
|
10
|
+
// (Not @rulecad/pcb: a board design reaches the board engine as v.pcb, which the runner loads as the tool's own
|
|
11
|
+
// module. The engine imports Node's modules, which a package a design may import may not, and the permission model
|
|
12
|
+
// holds it to reads only.)
|
|
13
|
+
const ALLOWED = [
|
|
14
|
+
'@jscad/modeling',
|
|
15
|
+
'@rulecad/shapes',
|
|
16
|
+
'@rulecad/flexures',
|
|
17
|
+
'@rulecad/gears',
|
|
18
|
+
'@rulecad/motors',
|
|
19
|
+
'@rulecad/frames',
|
|
20
|
+
'@rulecad/fixings',
|
|
21
|
+
];
|
|
7
22
|
|
|
8
|
-
|
|
9
|
-
|
|
23
|
+
// Each allowed package's real folder, as the tool itself finds it (from its entry up to the package.json of that
|
|
24
|
+
// name: exports may hide that file), found once; null where the tool can't find the package
|
|
25
|
+
function home(name) {
|
|
26
|
+
try {
|
|
27
|
+
for (let d = path.dirname(fileURLToPath(import.meta.resolve(name))); path.dirname(d) !== d; d = path.dirname(d)) {
|
|
28
|
+
const pj = path.join(d, 'package.json');
|
|
29
|
+
if (fs.existsSync(pj) && JSON.parse(fs.readFileSync(pj, 'utf8')).name === name)
|
|
30
|
+
return pathToFileURL(fs.realpathSync(d) + path.sep).href;
|
|
31
|
+
}
|
|
32
|
+
} catch {
|
|
33
|
+
/* not where the tool can find it */
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// What a file of each package may import by name: the allowed packages and what its package.json says it depends on
|
|
39
|
+
function needs(url) {
|
|
40
|
+
try {
|
|
41
|
+
const j = JSON.parse(fs.readFileSync(new URL('package.json', url), 'utf8'));
|
|
42
|
+
return [
|
|
43
|
+
...ALLOWED,
|
|
44
|
+
...['dependencies', 'peerDependencies', 'optionalDependencies'].flatMap((k) => Object.keys(j[k] || {})),
|
|
45
|
+
];
|
|
46
|
+
} catch {
|
|
47
|
+
return ALLOWED;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Each allowed package's real folder (a file: URL ending in a slash), or null where the tool can't find it. Found in
|
|
53
|
+
* the tool's own thread: older Node runs these hooks in a worker, where import.meta.resolve can't find packages, so
|
|
54
|
+
* the preload works them out and hands them over.
|
|
55
|
+
* @returns {Record<string, string | null>}
|
|
56
|
+
*/
|
|
57
|
+
export const homesOf = () => Object.fromEntries(ALLOWED.map((a) => [a, home(a)]));
|
|
58
|
+
|
|
59
|
+
let project = '',
|
|
60
|
+
homes = {},
|
|
61
|
+
deps = {};
|
|
62
|
+
/**
|
|
63
|
+
* Set the hooks up: the design's project (a file: URL ending in a slash) and, from the preload, the packages' folders.
|
|
64
|
+
* @param {{ project?: string, homes?: Record<string, string | null> }} [data]
|
|
65
|
+
*/
|
|
66
|
+
export async function initialize(data) {
|
|
67
|
+
project = data?.project || '';
|
|
68
|
+
homes = data?.homes || homesOf();
|
|
69
|
+
deps = Object.fromEntries(ALLOWED.filter((a) => homes[a]).map((a) => [a, needs(homes[a])]));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const deny = (spec, why) =>
|
|
73
|
+
Object.assign(new Error(`designs can't import "${spec}": ${why}`), { code: 'design.sandbox' });
|
|
10
74
|
|
|
11
75
|
// The same check, for in-thread hooks (synchronous) and worker hooks (async)
|
|
12
76
|
export function resolveHook(spec, context, next) {
|
|
13
|
-
const fromDesign = check(spec, context),
|
|
14
|
-
|
|
77
|
+
const fromDesign = check(spec, context),
|
|
78
|
+
r = next(spec, context);
|
|
79
|
+
return fromDesign ? after(spec, r) : inPackage(spec, context, r);
|
|
15
80
|
}
|
|
16
81
|
export async function resolve(spec, context, next) {
|
|
17
|
-
const fromDesign = check(spec, context),
|
|
18
|
-
|
|
82
|
+
const fromDesign = check(spec, context),
|
|
83
|
+
r = await next(spec, context);
|
|
84
|
+
return fromDesign ? after(spec, r) : inPackage(spec, context, r);
|
|
85
|
+
}
|
|
86
|
+
// A design may reach any file of an allowed package, and such a file isn't the design's, so it is free of the rules
|
|
87
|
+
// above: one that imports something the package doesn't depend on would hand that to the design. @jscad/modeling's
|
|
88
|
+
// own tests require('ava'), and an "ava" the workspace installs (a file: dependency, say) may do anything. So a file
|
|
89
|
+
// of an allowed package (not of a package kept in its node_modules) may import only the allowed packages, what its
|
|
90
|
+
// package.json depends on, and its own files.
|
|
91
|
+
const packageAt = (url) =>
|
|
92
|
+
ALLOWED.find(
|
|
93
|
+
(a) =>
|
|
94
|
+
homes[a] &&
|
|
95
|
+
url?.startsWith(homes[a]) &&
|
|
96
|
+
!url
|
|
97
|
+
.slice(homes[a].length)
|
|
98
|
+
.split('/')
|
|
99
|
+
.some((s) => s.toLowerCase() === 'node_modules'),
|
|
100
|
+
);
|
|
101
|
+
// (before Node looks for a name, and after it finds a relative file)
|
|
102
|
+
function packageCheck(spec, context) {
|
|
103
|
+
const name = packageAt(context.parentURL);
|
|
104
|
+
if (
|
|
105
|
+
name &&
|
|
106
|
+
!isRelative(spec) &&
|
|
107
|
+
(spec.startsWith('node:') ||
|
|
108
|
+
builtinModules.includes(spec) ||
|
|
109
|
+
!deps[name].some((d) => spec === d || spec.startsWith(`${d}/`)))
|
|
110
|
+
) {
|
|
111
|
+
throw deny(
|
|
112
|
+
spec,
|
|
113
|
+
`${fileURLToPath(context.parentURL)} would import it, and its package ${name} doesn't depend on it (a design may use a package's own files, not the tools its tests use)`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function inPackage(spec, context, r) {
|
|
118
|
+
const name = isRelative(spec) && packageAt(context.parentURL);
|
|
119
|
+
if (name && !r.url.startsWith(homes[name]))
|
|
120
|
+
throw deny(spec, `${fileURLToPath(context.parentURL)} would reach outside its package, ${name}`);
|
|
121
|
+
return r;
|
|
19
122
|
}
|
|
20
123
|
const isRelative = (spec) => spec.startsWith('./') || spec.startsWith('../');
|
|
124
|
+
const packageOf = (spec) => ALLOWED.find((a) => spec === a || spec.startsWith(`${a}/`));
|
|
21
125
|
// A design's own files are its project's. A path that leaves it is refused wherever it leads: into the workspace's
|
|
22
|
-
// node_modules, say, where any package may be.
|
|
126
|
+
// node_modules, say, where any package may be. An allowed package must lead into its own folder, the one the tool
|
|
127
|
+
// uses, or a copy in the project: not through a link out of the project to some other folder of the same name.
|
|
23
128
|
const after = (spec, r) => {
|
|
24
|
-
if (isRelative(spec)
|
|
129
|
+
if (isRelative(spec)) {
|
|
130
|
+
if (!r.url.startsWith(project)) throw deny(spec, 'it is outside the project');
|
|
131
|
+
return r;
|
|
132
|
+
}
|
|
133
|
+
const name = packageOf(spec),
|
|
134
|
+
inside = homes[name];
|
|
135
|
+
if (!r.url.startsWith(project) && !(inside && r.url.startsWith(inside))) {
|
|
136
|
+
const to = r.url.startsWith('file:') ? fileURLToPath(r.url) : r.url;
|
|
137
|
+
const own = inside ? ` (${fileURLToPath(inside)})` : '';
|
|
138
|
+
throw deny(
|
|
139
|
+
spec,
|
|
140
|
+
`it leads outside the package ${name}, to ${to}. A design may use only the copy rulecad itself uses${own} or one inside its project: if the workspace installs another copy, give it the version rulecad uses (npm ls ${name} lists the copies)`,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
25
143
|
return r;
|
|
26
144
|
};
|
|
145
|
+
// A file inside a package is named plainly: no '.' or '..' among the folders, no backslash, no %-encoded letters, and
|
|
146
|
+
// never in the package's own node_modules (another package, which may read files: a nested graceful-fs, say). Windows
|
|
147
|
+
// drops a name's trailing dots and spaces and knows NODE_M~1 for node_modules, so those spellings are refused too.
|
|
148
|
+
const odd = (s) => {
|
|
149
|
+
const t = s.replace(/[. ]+$/, '').toLowerCase();
|
|
150
|
+
return t === '' || t === 'node_modules' || s.includes('~');
|
|
151
|
+
};
|
|
152
|
+
const climbs = (spec, name) =>
|
|
153
|
+
/[\\%]/.test(spec) ||
|
|
154
|
+
(spec !== name &&
|
|
155
|
+
spec
|
|
156
|
+
.slice(name.length + 1)
|
|
157
|
+
.split('/')
|
|
158
|
+
.some(odd));
|
|
27
159
|
// Everything under the project is the design's code, a node_modules folder there included
|
|
28
160
|
function check(spec, context) {
|
|
29
161
|
const fromDesign = project && context.parentURL?.startsWith(project);
|
|
162
|
+
if (!fromDesign) packageCheck(spec, context);
|
|
30
163
|
if (fromDesign) {
|
|
31
|
-
if (spec.startsWith('node:') || builtinModules.includes(spec))
|
|
32
|
-
|
|
164
|
+
if (spec.startsWith('node:') || builtinModules.includes(spec))
|
|
165
|
+
throw deny(spec, 'a design only makes shapes (no files, network or programs)');
|
|
166
|
+
if (!isRelative(spec)) {
|
|
167
|
+
const name = packageOf(spec);
|
|
168
|
+
if (!name) throw deny(spec, `only ${ALLOWED.join(', ')} and files in its project`);
|
|
169
|
+
if (climbs(spec, name))
|
|
170
|
+
throw deny(
|
|
171
|
+
spec,
|
|
172
|
+
`a file of ${name} is named by its path inside the package, with no "." or ".." folders, node_modules, backslashes, "%" or "~", and no folder whose name ends in a dot or a space`,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
33
175
|
}
|
|
34
176
|
return fromDesign;
|
|
35
177
|
}
|
package/lib/sandbox-preload.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Loaded first in the sandbox (node --import): the import hook that keeps designs to their own imports. In-thread
|
|
2
2
|
// (module.registerHooks) where Node has it; older Node runs hooks in a worker, which the sandbox then allows.
|
|
3
3
|
import * as mod from 'node:module';
|
|
4
|
-
import { resolveHook, initialize } from './sandbox-hooks.mjs';
|
|
4
|
+
import { resolveHook, initialize, homesOf } from './sandbox-hooks.mjs';
|
|
5
5
|
|
|
6
6
|
const project = process.env.CAD_SANDBOX_PROJECT;
|
|
7
7
|
if (mod.registerHooks) {
|
|
8
8
|
initialize({ project });
|
|
9
9
|
mod.registerHooks({ resolve: resolveHook });
|
|
10
|
-
} else mod.register('./sandbox-hooks.mjs', import.meta.url, { data: { project } });
|
|
10
|
+
} else mod.register('./sandbox-hooks.mjs', import.meta.url, { data: { project, homes: homesOf() } });
|
package/lib/sandbox-run.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// The sandbox's runner: reads { js, values, meta } on stdin, runs the design's main(values), and writes its parts'
|
|
2
2
|
// faces (and the rules it read) as JSON on stdout. Started by sandbox.mjs, under Node's permission model.
|
|
3
|
+
// A board design (board set) gets v.pcb, the board engine, and what it hands back is built into Circuit JSON here:
|
|
4
|
+
// the reply carries that circuit (and the matrix, and a cloned board's source) in place of parts, and the tool makes
|
|
5
|
+
// the board's solid from it. Routes the autorouter found are read from the tool's route cache and handed back.
|
|
3
6
|
import fs from 'node:fs';
|
|
4
7
|
import path from 'node:path';
|
|
5
8
|
import * as shapes from '@rulecad/shapes';
|
|
@@ -10,33 +13,145 @@ import { designFrames } from './frames.mjs';
|
|
|
10
13
|
import { designFixings } from './fixings.mjs';
|
|
11
14
|
import { named, faces, unpack, watch } from './parts.mjs';
|
|
12
15
|
import { lineOf } from './errors.mjs';
|
|
16
|
+
import { keeper, STAGES_MOST, ROUTES_MOST } from './kept.mjs';
|
|
13
17
|
|
|
14
18
|
for (const k of ['fetch', 'WebSocket', 'EventSource', 'XMLHttpRequest', 'Request', 'Response']) delete globalThis[k];
|
|
15
19
|
// and the ways round the import hook that process itself offers: Node's own modules by name, its internals, other
|
|
16
20
|
// processes. (sandbox.mjs's header says what this does and doesn't promise.)
|
|
17
21
|
const exit = process.exit.bind(process);
|
|
18
|
-
for (const k of [
|
|
22
|
+
for (const k of [
|
|
23
|
+
'getBuiltinModule',
|
|
24
|
+
'binding',
|
|
25
|
+
'_linkedBinding',
|
|
26
|
+
'dlopen',
|
|
27
|
+
'kill',
|
|
28
|
+
'_kill',
|
|
29
|
+
'_getActiveHandles',
|
|
30
|
+
'_getActiveRequests',
|
|
31
|
+
'_debugProcess',
|
|
32
|
+
'_startProfilerIdleNotifier',
|
|
33
|
+
]) {
|
|
19
34
|
if (k in process) Object.defineProperty(process, k, { value: undefined, configurable: false, writable: false });
|
|
20
35
|
}
|
|
21
36
|
const chunks = [];
|
|
22
37
|
for await (const c of process.stdin) chunks.push(c);
|
|
23
|
-
const { js, values, meta, engine, optional, stages, bound } = JSON.parse(Buffer.concat(chunks).toString('utf8')),
|
|
38
|
+
const { js, values, meta, engine, optional, stages, bound, board } = JSON.parse(Buffer.concat(chunks).toString('utf8')),
|
|
39
|
+
context = bound?.fixings;
|
|
24
40
|
// A lamina's slow stages: read from the tool's cache (the workspace is readable), and what this run makes handed back
|
|
25
|
-
// with the parts for the tool to write (this process can't)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
41
|
+
// with the parts for the tool to write (this process can't), up to the cap the tool takes (kept.mjs: past it, a stage
|
|
42
|
+
// is used but not kept)
|
|
43
|
+
const stageKeeper = keeper(STAGES_MOST),
|
|
44
|
+
kept = stageKeeper.kept; // (what this run made of them)
|
|
45
|
+
// A file of a cache folder, read only where it really is in that folder: a cloned workspace may hold its own .cache/,
|
|
46
|
+
// and a stage's file there that is a link to a file elsewhere (a key in the user's home folder) would be read, under
|
|
47
|
+
// the permission model, which follows links, and handed to the design. Such a file is a miss: the stage is made afresh.
|
|
48
|
+
// The folder itself the tool hands over only where its real path is inside the workspace (sandbox.mjs ownFolder), so
|
|
49
|
+
// here a key must name a file directly in it (no folder in the key) that is not itself a link. (Not by real path: the
|
|
50
|
+
// native one spells a Windows folder by its long name where the permission model was given its short one, and refuses
|
|
51
|
+
// the read; Node's own is refused the lstat of /var on macOS.)
|
|
52
|
+
const fromFolder = (dir) => (k) => {
|
|
53
|
+
if (!dir) return undefined;
|
|
54
|
+
const f = path.join(dir, `${k}.json`);
|
|
55
|
+
if (path.dirname(f) !== path.resolve(dir)) return undefined;
|
|
56
|
+
try {
|
|
57
|
+
return fs.lstatSync(f).isFile() ? fs.readFileSync(f, 'utf8') : undefined;
|
|
58
|
+
} catch {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
if (stages)
|
|
63
|
+
flexures.laminaCache({
|
|
64
|
+
salt: stages.salt,
|
|
65
|
+
get: fromFolder(stages.dir),
|
|
66
|
+
put: stageKeeper.put,
|
|
67
|
+
});
|
|
68
|
+
// A board's engine: @rulecad/pcb, loaded only for a board design, its mount bound to the workspace's thread sets (as
|
|
69
|
+
// data, like v.fixings), and its routes kept as the stages are: read from the design's own folder of the route cache,
|
|
70
|
+
// then from the one the tool's own process writes (board.routes, in that order), and handed back to go in the first
|
|
71
|
+
const routeKeeper = keeper(ROUTES_MOST),
|
|
72
|
+
routes = routeKeeper.kept,
|
|
73
|
+
routeFolders = (board?.routes || []).map(fromFolder),
|
|
74
|
+
routeOf = (k) => {
|
|
75
|
+
for (const get of routeFolders) {
|
|
76
|
+
const text = get(k);
|
|
77
|
+
if (text !== undefined) return text;
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
};
|
|
81
|
+
let pcb = null,
|
|
82
|
+
mounted = false;
|
|
83
|
+
if (board) {
|
|
84
|
+
pcb = await import('@rulecad/pcb');
|
|
85
|
+
if (pcb.routeStore)
|
|
86
|
+
pcb.routeStore({
|
|
87
|
+
get: routeOf,
|
|
88
|
+
put: routeKeeper.put,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const engineFor = () => ({
|
|
92
|
+
...pcb,
|
|
93
|
+
mount: (thread, x, y, o = {}) => {
|
|
94
|
+
mounted = true;
|
|
95
|
+
return pcb.mount(thread, x, y, { sets: context?.sets, ...o });
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
const stdout = process.stdout,
|
|
99
|
+
done = (o) => stdout.write(JSON.stringify(o), () => exit(0));
|
|
29
100
|
// stdout carries the result, so what a design prints (console.log, to see a value) goes to stderr, which the tool passes on
|
|
30
101
|
console.log = console.info = console.debug = (...a) => console.error(...a);
|
|
31
102
|
try {
|
|
32
103
|
const mod = await import(js);
|
|
33
|
-
if (typeof mod.main !== 'function')
|
|
34
|
-
|
|
35
|
-
const
|
|
104
|
+
if (typeof mod.main !== 'function')
|
|
105
|
+
throw Object.assign(new Error('the design has no exported main(v)'), { code: 'design.main' });
|
|
106
|
+
const ops = await shapes.ops(engine || 'jscad'),
|
|
107
|
+
df = designFixings({ ...(context || {}), ops }); // (v.fixings: bound to the project, as the build's own is)
|
|
108
|
+
const { v, read } = watch(
|
|
109
|
+
values,
|
|
110
|
+
{
|
|
111
|
+
shapes,
|
|
112
|
+
flexures,
|
|
113
|
+
gears,
|
|
114
|
+
motors,
|
|
115
|
+
frames: designFrames(bound?.frames),
|
|
116
|
+
fixings: df.fixings,
|
|
117
|
+
ops,
|
|
118
|
+
...(pcb ? { pcb: engineFor() } : {}),
|
|
119
|
+
},
|
|
120
|
+
optional,
|
|
121
|
+
);
|
|
36
122
|
const { shapes: made, mechanism } = unpack(await mod.main(v));
|
|
37
|
-
const
|
|
38
|
-
|
|
123
|
+
const found = {
|
|
124
|
+
read: [...read],
|
|
125
|
+
...(mechanism ? { mechanism } : {}),
|
|
126
|
+
...(df.used() ? { fixings: df.found() } : {}),
|
|
127
|
+
...(Object.keys(kept).length ? { stages: kept } : {}),
|
|
128
|
+
};
|
|
129
|
+
if (pcb) {
|
|
130
|
+
// a circuit board: its Circuit JSON, for the tool to make the board's solid and files from
|
|
131
|
+
const { board: b, matrix, clone } = made && made.board ? made : { board: made };
|
|
132
|
+
const circuit = await pcb.render(b);
|
|
133
|
+
done({
|
|
134
|
+
parts: [],
|
|
135
|
+
...found,
|
|
136
|
+
board: { circuit, ...(matrix ? { matrix } : {}), ...(clone ? { source: clone } : {}) },
|
|
137
|
+
...(mounted ? { mounted } : {}),
|
|
138
|
+
...(Object.keys(routes).length ? { routes } : {}),
|
|
139
|
+
});
|
|
140
|
+
} else done({ parts: named(made, meta).map(({ geometry, ...p }) => ({ ...p, solid: faces(geometry) })), ...found });
|
|
39
141
|
} catch (e) {
|
|
40
142
|
const line = lineOf(e, js);
|
|
41
|
-
done({
|
|
143
|
+
done({
|
|
144
|
+
error: {
|
|
145
|
+
message: e?.message || String(e),
|
|
146
|
+
...(line ? { line } : {}),
|
|
147
|
+
...(e?.name === 'SyntaxError' ? { syntax: true } : {}),
|
|
148
|
+
code:
|
|
149
|
+
e?.code && typeof e.code === 'string' && !/^ERR_/.test(e.code)
|
|
150
|
+
? e.code
|
|
151
|
+
: e?.code === 'ERR_ACCESS_DENIED'
|
|
152
|
+
? 'design.sandbox'
|
|
153
|
+
: 'design.run',
|
|
154
|
+
...(e?.code === 'ERR_ACCESS_DENIED' && e.resource ? { resource: e.resource } : {}),
|
|
155
|
+
},
|
|
156
|
+
});
|
|
42
157
|
}
|