@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
@@ -11,48 +11,145 @@ import * as gears from '@rulecad/gears';
11
11
  import * as motors from '@rulecad/motors';
12
12
  import { designFrames } from './frames.mjs';
13
13
  import { designFixings } from './fixings.mjs';
14
+ import { designShapes } from './designshapes.mjs';
14
15
  import { named, faces, unpack, watch } from './parts.mjs';
15
16
  import { lineOf } from './errors.mjs';
17
+ import { keeper, STAGES_MOST, ROUTES_MOST } from './kept.mjs';
18
+ import { IMPORTED } from './sandbox-hooks.mjs';
19
+ import { shutLoader, shutProcess, shutStreams, shutWebClient, writeAll } from './sandbox-lock.mjs';
16
20
 
21
+ shutWebClient(); // (first: touching Headers and the like makes an agent that connects anywhere: sandbox-lock.mjs)
17
22
  for (const k of ['fetch', 'WebSocket', 'EventSource', 'XMLHttpRequest', 'Request', 'Response']) delete globalThis[k];
18
23
  // and the ways round the import hook that process itself offers: Node's own modules by name, its internals, other
19
- // processes. (sandbox.mjs's header says what this does and doesn't promise.)
24
+ // processes, files (sandbox-lock.mjs; sandbox.mjs's header says what this does and doesn't promise)
20
25
  const exit = process.exit.bind(process);
21
- for (const k of ['getBuiltinModule', 'binding', '_linkedBinding', 'dlopen', 'kill', '_kill', '_getActiveHandles', '_getActiveRequests', '_debugProcess', '_startProfilerIdleNotifier']) {
22
- if (k in process) Object.defineProperty(process, k, { value: undefined, configurable: false, writable: false });
23
- }
24
- const chunks = [];
25
- for await (const c of process.stdin) chunks.push(c);
26
- const { js, values, meta, engine, optional, stages, bound, board } = JSON.parse(Buffer.concat(chunks).toString('utf8')), context = bound?.fixings;
26
+ shutProcess();
27
+ shutStreams(); // (the request is read, and the reply written, by file descriptor: sandbox-lock.mjs says why)
28
+ const { js, values, meta, engine, optional, stages, bound, board } = JSON.parse(fs.readFileSync(0, 'utf8')),
29
+ context = bound?.fixings;
27
30
  // A lamina's slow stages: read from the tool's cache (the workspace is readable), and what this run makes handed back
28
- // with the parts for the tool to write (this process can't)
29
- const kept = {}; // (what this run made of them)
30
- if (stages) flexures.laminaCache({ salt: stages.salt, get: (k) => { try { return fs.readFileSync(path.join(stages.dir, `${k}.json`), 'utf8'); } catch { return undefined; } }, put: (k, text) => { kept[k] = text; } });
31
+ // 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
32
+ // is used but not kept)
33
+ const stageKeeper = keeper(STAGES_MOST),
34
+ kept = stageKeeper.kept; // (what this run made of them)
35
+ // A file of a cache folder, read only where it really is in that folder: a cloned workspace may hold its own .cache/,
36
+ // 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
37
+ // the permission model, which follows links, and handed to the design. Such a file is a miss: the stage is made afresh.
38
+ // The folder itself the tool hands over only where its real path is inside the workspace (sandbox.mjs ownFolder), so
39
+ // 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
40
+ // native one spells a Windows folder by its long name where the permission model was given its short one, and refuses
41
+ // the read; Node's own is refused the lstat of /var on macOS.)
42
+ const fromFolder = (dir) => (k) => {
43
+ if (!dir) return undefined;
44
+ const f = path.join(dir, `${k}.json`);
45
+ if (path.dirname(f) !== path.resolve(dir)) return undefined;
46
+ try {
47
+ return fs.lstatSync(f).isFile() ? fs.readFileSync(f, 'utf8') : undefined;
48
+ } catch {
49
+ return undefined;
50
+ }
51
+ };
52
+ if (stages)
53
+ flexures.laminaCache({
54
+ salt: stages.salt,
55
+ get: fromFolder(stages.dir),
56
+ put: stageKeeper.put,
57
+ });
31
58
  // A board's engine: @rulecad/pcb, loaded only for a board design, its mount bound to the workspace's thread sets (as
32
- // data, like v.fixings), and its routes kept as the stages are
33
- const routes = {};
34
- let pcb = null, mounted = false;
59
+ // data, like v.fixings), and its routes kept as the stages are: read from the design's own folder of the route cache,
60
+ // then from the one the tool's own process writes (board.routes, in that order), and handed back to go in the first
61
+ const routeKeeper = keeper(ROUTES_MOST),
62
+ routes = routeKeeper.kept,
63
+ routeFolders = (board?.routes || []).map(fromFolder),
64
+ routeOf = (k) => {
65
+ for (const get of routeFolders) {
66
+ const text = get(k);
67
+ if (text !== undefined) return text;
68
+ }
69
+ return undefined;
70
+ };
71
+ let pcb = null,
72
+ mounted = false;
35
73
  if (board) {
36
74
  pcb = await import('@rulecad/pcb');
37
- if (pcb.routeStore) pcb.routeStore({ get: (k) => { if (!board.routes) return undefined; try { return fs.readFileSync(path.join(board.routes, `${k}.json`), 'utf8'); } catch { return undefined; } }, put: (k, text) => { routes[k] = text; } });
75
+ if (pcb.routeStore)
76
+ pcb.routeStore({
77
+ get: routeOf,
78
+ put: routeKeeper.put,
79
+ });
38
80
  }
39
- const engineFor = () => ({ ...pcb, mount: (thread, x, y, o = {}) => { mounted = true; return pcb.mount(thread, x, y, { sets: context?.sets, ...o }); } });
40
- const stdout = process.stdout, done = (o) => stdout.write(JSON.stringify(o), () => exit(0));
41
- // stdout carries the result, so what a design prints (console.log, to see a value) goes to stderr, which the tool passes on
42
- console.log = console.info = console.debug = (...a) => console.error(...a);
81
+ const engineFor = () => ({
82
+ ...pcb,
83
+ mount: (thread, x, y, o = {}) => {
84
+ mounted = true;
85
+ return pcb.mount(thread, x, y, { sets: context?.sets, ...o });
86
+ },
87
+ });
88
+ // stdout carries the result, so what a design prints (console.log, to see a value) goes to stderr (shutStreams), which
89
+ // the tool passes on
90
+ const done = (o) => {
91
+ writeAll(1, JSON.stringify(o));
92
+ exit(0);
93
+ };
43
94
  try {
95
+ shutLoader(process.env.CAD_SANDBOX_PROJECT); // (the CommonJS loader: no design reaches Node's own modules by it)
44
96
  const mod = await import(js);
45
- if (typeof mod.main !== 'function') throw Object.assign(new Error('the design has no exported main(v)'), { code: 'design.main' });
46
- const ops = await shapes.ops(engine || 'jscad'), df = designFixings({ ...(context || {}), ops }); // (v.fixings: bound to the project, as the build's own is)
47
- const { v, read } = watch(values, { shapes, flexures, gears, motors, frames: designFrames(bound?.frames), fixings: df.fixings, ops, ...(pcb ? { pcb: engineFor() } : {}) }, optional);
97
+ if (typeof mod.main !== 'function')
98
+ throw Object.assign(new Error('the design has no exported main(v)'), { code: 'design.main' });
99
+ const ops = await shapes.ops(engine || 'jscad'),
100
+ df = designFixings({ ...(context || {}), ops }), // (v.fixings: bound to the project, as the build's own is)
101
+ ds = designShapes(shapes, { printer: context?.printer }); // (v.shapes: its seats said, as the build's own are)
102
+ const { v, read } = watch(
103
+ values,
104
+ {
105
+ shapes: ds.shapes,
106
+ flexures,
107
+ gears,
108
+ motors,
109
+ frames: designFrames(bound?.frames),
110
+ fixings: df.fixings,
111
+ ops,
112
+ ...(pcb ? { pcb: engineFor() } : {}),
113
+ },
114
+ optional,
115
+ );
48
116
  const { shapes: made, mechanism } = unpack(await mod.main(v));
49
- const found = { read: [...read], ...(mechanism ? { mechanism } : {}), ...(df.used() ? { fixings: df.found() } : {}), ...(Object.keys(kept).length ? { stages: kept } : {}) };
50
- if (pcb) { // a circuit board: its Circuit JSON, for the tool to make the board's solid and files from
117
+ // the files of its project the design imported, as the import hook saw them (after main: it may import as it runs)
118
+ const imports = (await import(IMPORTED)).default;
119
+ const found = {
120
+ read: [...read],
121
+ imports,
122
+ ...(mechanism ? { mechanism } : {}),
123
+ ...(df.used() ? { fixings: df.found() } : {}),
124
+ ...(ds.used() ? { seats: ds.found() } : {}),
125
+ ...(Object.keys(kept).length ? { stages: kept } : {}),
126
+ };
127
+ if (pcb) {
128
+ // a circuit board: its Circuit JSON, for the tool to make the board's solid and files from
51
129
  const { board: b, matrix, clone } = made && made.board ? made : { board: made };
52
130
  const circuit = await pcb.render(b);
53
- done({ parts: [], ...found, board: { circuit, ...(matrix ? { matrix } : {}), ...(clone ? { source: clone } : {}) }, ...(mounted ? { mounted } : {}), ...(Object.keys(routes).length ? { routes } : {}) });
131
+ done({
132
+ parts: [],
133
+ ...found,
134
+ board: { circuit, ...(matrix ? { matrix } : {}), ...(clone ? { source: clone } : {}) },
135
+ ...(mounted ? { mounted } : {}),
136
+ ...(Object.keys(routes).length ? { routes } : {}),
137
+ });
54
138
  } else done({ parts: named(made, meta).map(({ geometry, ...p }) => ({ ...p, solid: faces(geometry) })), ...found });
55
139
  } catch (e) {
56
140
  const line = lineOf(e, js);
57
- done({ error: { message: e?.message || String(e), ...(line ? { line } : {}), ...(e?.name === 'SyntaxError' ? { syntax: true } : {}), code: e?.code && typeof e.code === 'string' && !/^ERR_/.test(e.code) ? e.code : e?.code === 'ERR_ACCESS_DENIED' ? 'design.sandbox' : 'design.run', ...(e?.code === 'ERR_ACCESS_DENIED' && e.resource ? { resource: e.resource } : {}) } });
141
+ done({
142
+ error: {
143
+ message: e?.message || String(e),
144
+ ...(line ? { line } : {}),
145
+ ...(e?.name === 'SyntaxError' ? { syntax: true } : {}),
146
+ code:
147
+ e?.code && typeof e.code === 'string' && !/^ERR_/.test(e.code)
148
+ ? e.code
149
+ : e?.code === 'ERR_ACCESS_DENIED'
150
+ ? 'design.sandbox'
151
+ : 'design.run',
152
+ ...(e?.code === 'ERR_ACCESS_DENIED' && e.resource ? { resource: e.resource } : {}),
153
+ },
154
+ });
58
155
  }