@rulecad/cad 0.9.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.
Files changed (68) hide show
  1. package/AGENTS.md +29 -10
  2. package/CHANGELOG.md +604 -0
  3. package/EXAMPLES.md +122 -105
  4. package/README.md +292 -63
  5. package/bin/cad.mjs +500 -87
  6. package/lib/assembly.mjs +486 -134
  7. package/lib/bays.mjs +124 -0
  8. package/lib/cache-file.mjs +148 -0
  9. package/lib/clone.mjs +93 -11
  10. package/lib/code-text.mjs +28 -0
  11. package/lib/design-imports.mjs +127 -0
  12. package/lib/design.mjs +291 -59
  13. package/lib/designshapes.mjs +142 -0
  14. package/lib/edit.mjs +29 -5
  15. package/lib/estimates.mjs +51 -0
  16. package/lib/fasten.mjs +11 -5
  17. package/lib/fe-key.mjs +29 -0
  18. package/lib/fittings.mjs +20 -15
  19. package/lib/fixings.mjs +17 -4
  20. package/lib/framebuild.mjs +73 -92
  21. package/lib/framecheck.mjs +154 -77
  22. package/lib/framegen.mjs +9 -1
  23. package/lib/frames.mjs +6 -2
  24. package/lib/framesize.mjs +45 -82
  25. package/lib/gait.mjs +17 -7
  26. package/lib/guides.mjs +84 -50
  27. package/lib/host.mjs +7 -3
  28. package/lib/kit.mjs +5 -5
  29. package/lib/list.mjs +29 -21
  30. package/lib/live-guard.mjs +18 -8
  31. package/lib/loads.mjs +35 -23
  32. package/lib/measured.mjs +740 -99
  33. package/lib/mesh.mjs +156 -56
  34. package/lib/motors.mjs +26 -33
  35. package/lib/names.mjs +15 -0
  36. package/lib/node-version.mjs +52 -0
  37. package/lib/optimise.mjs +34 -25
  38. package/lib/order.mjs +5 -12
  39. package/lib/parts.mjs +3 -0
  40. package/lib/picture-servers.mjs +66 -0
  41. package/lib/power.mjs +14 -7
  42. package/lib/preview.mjs +12 -25
  43. package/lib/programs.mjs +113 -0
  44. package/lib/rules.mjs +2 -1
  45. package/lib/sandbox-guard.mjs +43 -0
  46. package/lib/sandbox-hooks.mjs +109 -15
  47. package/lib/sandbox-lock.mjs +225 -0
  48. package/lib/sandbox-preload.mjs +2 -2
  49. package/lib/sandbox-run.mjs +22 -24
  50. package/lib/sandbox.mjs +345 -42
  51. package/lib/sheet.mjs +114 -3
  52. package/lib/targets/gist.mjs +9 -6
  53. package/lib/versions.mjs +294 -35
  54. package/lib/workspace.mjs +6 -3
  55. package/package.json +14 -14
  56. package/schemas/assembly.schema.json +6 -5
  57. package/schemas/object.schema.json +1 -1
  58. package/schemas/project.schema.json +6 -0
  59. package/templates/coupons/MEASURE.md +109 -0
  60. package/templates/coupons/assemblies/fixings-test.json +20 -0
  61. package/templates/coupons/assemblies/slot-test.json +11 -0
  62. package/templates/coupons/objects/fixings-coupon.js +88 -0
  63. package/templates/coupons/objects/fixings-coupon.json +26 -0
  64. package/templates/coupons/objects/holds-coupon.js +130 -0
  65. package/templates/coupons/objects/holds-coupon.json +42 -0
  66. package/templates/coupons/objects/slot-coupon.js +89 -0
  67. package/templates/coupons/objects/slot-coupon.json +29 -0
  68. package/templates/coupons/project.json +10 -0
@@ -15,6 +15,8 @@
15
15
  // says it may not be framed.
16
16
  // The port is not checked: the name is what a page can't fake, and a view reached through a forwarded port (ssh -L
17
17
  // 9000:localhost:8090) arrives with another port in its Host.
18
+ // The two servers a picture is taken from (picture-servers.mjs) run the same checks, for the few seconds they are up,
19
+ // and tell a refused request what they are in words of their own (says).
18
20
  import net from 'node:net';
19
21
 
20
22
  const LOCAL = ['127.0.0.1', 'localhost', '::1'];
@@ -33,16 +35,27 @@ export function hostName(host) {
33
35
  return name.endsWith('.') ? name.slice(0, -1) : name;
34
36
  }
35
37
 
38
+ // What a refused request is told, unless the server gives words of its own (says): a live view's person can ask for
39
+ // another name, and its page has a stop button
40
+ const LIVE_VIEW = {
41
+ host: (given) =>
42
+ `This live view answers only to this computer's own names and addresses, not "${given}". ` +
43
+ 'To open it by another name (a Tailscale name, say), give that name with --allow-host.\n',
44
+ post: 'Only the live view itself can stop the run: this request came from another page.\n',
45
+ };
46
+
36
47
  /**
37
48
  * Makes the check a live view's server runs on each request. It answers a refused request itself (421 for a Host
38
49
  * that isn't this computer's, 403 for another page's POST) and says whether to go on. Every answer refuses to be shown
39
50
  * in another page's frame.
40
- * @param {{ host?: string, allowHosts?: string[] }} [o] host: the address the server listens on; allowHosts: more
41
- * names to accept, such as a Tailscale MagicDNS name (my-laptop)
51
+ * @param {{ host?: string, allowHosts?: string[], says?: { host: (given: string) => string, post: string } }} [o]
52
+ * host: the address the server listens on; allowHosts: more names to accept, such as a Tailscale MagicDNS name
53
+ * (my-laptop); says: what a refused request is told, for a server that is not a live view (host: for a Host that
54
+ * isn't this computer's, given the Host it named; post: for another page's POST), each ending in a new line
42
55
  * @returns {(req: import('node:http').IncomingMessage, res: import('node:http').ServerResponse) => boolean} true when
43
56
  * the request may go on
44
57
  */
45
- export function liveGuard({ host, allowHosts = [] } = {}) {
58
+ export function liveGuard({ host, allowHosts = [], says = LIVE_VIEW } = {}) {
46
59
  const names = new Set([...LOCAL, ...[host, ...allowHosts].filter(Boolean).map(hostName)]);
47
60
  const accepted = (name) => names.has(name) || net.isIP(name) !== 0;
48
61
  return (req, res) => {
@@ -51,16 +64,13 @@ export function liveGuard({ host, allowHosts = [] } = {}) {
51
64
  const given = req.headers.host;
52
65
  if (!given || !accepted(hostName(given))) {
53
66
  res.writeHead(421, { 'content-type': 'text/plain; charset=utf-8' });
54
- res.end(
55
- `This live view answers only to this computer's own names and addresses, not "${given ?? ''}". ` +
56
- 'To open it by another name (a Tailscale name, say), give that name with --allow-host.\n',
57
- );
67
+ res.end(says.host(given ?? ''));
58
68
  return false;
59
69
  }
60
70
  const origin = req.headers.origin;
61
71
  if (req.method === 'POST' && origin !== undefined && origin.toLowerCase() !== `http://${given.toLowerCase()}`) {
62
72
  res.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' });
63
- res.end('Only the live view itself can stop the run: this request came from another page.\n');
73
+ res.end(says.post);
64
74
  return false;
65
75
  }
66
76
  return true;
package/lib/loads.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  // What the machine's own weight and its payload do to it: the force and tipping moment each joint's bearing or printed
2
2
  // location takes, how far the links bend, how fast the arm rings, and what acceleration the motors can give it. Masses come
3
3
  // from the parts as drawn (volume and density) and the bought parts' rules. Findings in the assembly's shape.
4
- import crypto from 'node:crypto';
5
4
  import { grid as makeGrid, analyse, positions } from '@rulecad/fea';
6
5
  import { jacobian } from '@rulecad/robots';
7
6
  import { performance } from '@rulecad/motors';
8
- import { host, cached } from './host.mjs';
7
+ import { writeCacheFile, readCacheJson } from './cache-file.mjs';
8
+ import { feKey } from './fe-key.mjs';
9
9
  import { paths } from './workspace.mjs';
10
10
  import { peekRuleSet } from './rules.mjs';
11
11
  import { solidFill } from './optimise.mjs';
@@ -117,15 +117,20 @@ function bearingMargin(rows, loads) {
117
117
  return { P0, C0: first.C0, margin: first.C0 / P0, span };
118
118
  }
119
119
 
120
- // A printed location's weakest clip, from FE on the part as drawn: fixed at its foot, the load at its head
121
- function clipStress(tris, force, modulus, nu, cell) {
122
- const key = crypto
123
- .createHash('sha256')
124
- .update(JSON.stringify(['surface', tris.length, tris[0], force, modulus, cell]))
125
- .digest('hex')
126
- .slice(0, 20),
127
- file = paths.cache('clip', key);
128
- if (!process.env.CAD_NO_CACHE && cached(file)) return JSON.parse(host.readText(file));
120
+ /**
121
+ * A printed location's weakest clip, from FE on the part as drawn: fixed at its foot, the load at its head. Kept in
122
+ * .cache under a hash of every triangle and the material (fe-key.mjs). Exported for its tests.
123
+ * @param {number[][][]} tris the clip's triangles, in mm
124
+ * @param {number[]} force the load at its head, N
125
+ * @param {number} modulus its material's stiffness, MPa
126
+ * @param {number} nu its material's Poisson's ratio
127
+ * @param {number} cell the FE cells' size, mm
128
+ * @returns {{ stress: number, deflection?: number, thin?: boolean }}
129
+ */
130
+ export function clipStress(tris, force, modulus, nu, cell) {
131
+ const file = paths.cache('clip', feKey('clip', [tris, force, modulus, nu, cell])),
132
+ kept = !process.env.CAD_NO_CACHE && readCacheJson(file);
133
+ if (kept) return kept;
129
134
  const { lo, hi } = extent(tris),
130
135
  n = hi.map((h, k) => Math.max(1, Math.ceil((h - lo[k]) / cell)));
131
136
  const gr = { nx: n[0], ny: n[1], nz: n[2], h: cell },
@@ -154,7 +159,7 @@ function clipStress(tris, force, modulus, nu, cell) {
154
159
  let most = 0;
155
160
  for (let e = 0; e < density.length; e++) if (density[e] >= 0.5 && a.surface[0][e] > most) most = a.surface[0][e];
156
161
  const out = { stress: most, deflection: a.deflection[0].most };
157
- host.writeText(file, JSON.stringify(out));
162
+ writeCacheFile(file, JSON.stringify(out));
158
163
  return out;
159
164
  }
160
165
 
@@ -255,16 +260,23 @@ export function momentChecks(s, f, helpers, masses, payload) {
255
260
  return out;
256
261
  }
257
262
 
258
- // A link bent: held at its parent joint, the carried weight at its far end. δ from the FE, the tilt from a cantilever's
259
- // relation (1.5 δ / L), carried on to the tool. Kept in .cache, as this is the slow part.
260
- function linkFE(tris, O, far, force, E, nu, cell) {
261
- const key = crypto
262
- .createHash('sha256')
263
- .update(JSON.stringify(['surface', tris.length, tris.slice(0, 4), O, far, force, E, cell]))
264
- .digest('hex')
265
- .slice(0, 20),
266
- file = paths.cache('link', key);
267
- if (!process.env.CAD_NO_CACHE && cached(file)) return JSON.parse(host.readText(file));
263
+ /**
264
+ * A link bent: held at its parent joint, the carried weight at its far end. δ from the FE, the tilt from a cantilever's
265
+ * relation (1.5 δ / L), carried on to the tool. Kept in .cache, as this is the slow part, under a hash of every
266
+ * triangle and the material (fe-key.mjs). Exported for its tests.
267
+ * @param {number[][][]} tris the link's triangles, in mm
268
+ * @param {number[]} O where it is held (its parent joint), mm
269
+ * @param {number[]} far where the load is put on it, mm
270
+ * @param {number[]} force the load, N
271
+ * @param {number} E its material's stiffness, MPa
272
+ * @param {number} nu its material's Poisson's ratio
273
+ * @param {number} cell the FE cells' size, mm
274
+ * @returns {{ deflection: number, stress?: number, cell: number, missing?: boolean }}
275
+ */
276
+ export function linkFE(tris, O, far, force, E, nu, cell) {
277
+ const file = paths.cache('link', feKey('link', [tris, O, far, force, E, nu, cell])),
278
+ kept = !process.env.CAD_NO_CACHE && readCacheJson(file);
279
+ if (kept) return kept;
268
280
  const { lo, hi } = extent(tris),
269
281
  n = hi.map((h, k) => Math.max(1, Math.ceil((h - lo[k]) / cell)));
270
282
  const gr = { nx: n[0], ny: n[1], nz: n[2], h: cell },
@@ -297,7 +309,7 @@ function linkFE(tris, O, far, force, E, nu, cell) {
297
309
  stress: Array.from(a.surface[0]).reduce((m, x, e) => (density[e] >= 0.5 && x > m ? x : m), 0),
298
310
  cell,
299
311
  };
300
- host.writeText(file, JSON.stringify(out));
312
+ writeCacheFile(file, JSON.stringify(out));
301
313
  return out;
302
314
  }
303
315