iso-explainer 1.0.0 → 1.1.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 (31) hide show
  1. package/README.md +17 -8
  2. package/README.pt-BR.md +14 -13
  3. package/package.json +1 -1
  4. package/skills/iso-explainer/SKILL.md +24 -17
  5. package/skills/iso-explainer/assets/figure/figure.mjs +93 -0
  6. package/skills/iso-explainer/assets/runtime/ANATOMY-LICENSE.txt +21 -0
  7. package/skills/iso-explainer/assets/runtime/app.js +60 -20
  8. package/skills/iso-explainer/assets/runtime/engine.js +113 -66
  9. package/skills/iso-explainer/assets/runtime/kit.js +676 -0
  10. package/skills/iso-explainer/assets/runtime/looks-core.js +162 -0
  11. package/skills/iso-explainer/assets/runtime/looks.js +508 -0
  12. package/skills/iso-explainer/assets/runtime/styles.css +461 -447
  13. package/skills/iso-explainer/assets/templates/starter.course.json +6 -6
  14. package/skills/iso-explainer/examples/arquitetura-pedidos.docs.json +12 -10
  15. package/skills/iso-explainer/examples/catalogo-looks.course.json +599 -0
  16. package/skills/iso-explainer/examples/engenharia-na-pratica.course.json +11 -6
  17. package/skills/iso-explainer/examples/figures/token-bucket.build.mjs +111 -0
  18. package/skills/iso-explainer/examples/figures/token-bucket.html +959 -95
  19. package/skills/iso-explainer/examples/historia-pedidos.story.json +12 -10
  20. package/skills/iso-explainer/references/engine.md +1 -1
  21. package/skills/iso-explainer/references/entities.md +5 -3
  22. package/skills/iso-explainer/references/figure-craft.md +62 -0
  23. package/skills/iso-explainer/references/inside.md +20 -10
  24. package/skills/iso-explainer/references/layout.md +12 -0
  25. package/skills/iso-explainer/references/looks.md +60 -0
  26. package/skills/iso-explainer/references/story-mode.md +2 -2
  27. package/skills/iso-explainer/scripts/build.mjs +5 -3
  28. package/skills/iso-explainer/scripts/lib/layout.mjs +111 -0
  29. package/skills/iso-explainer/scripts/lib/resolve.mjs +20 -2
  30. package/skills/iso-explainer/scripts/snapshot.mjs +159 -97
  31. package/skills/iso-explainer/scripts/validate.mjs +33 -16
@@ -179,7 +179,8 @@
179
179
  "label": [
180
180
  "API 1"
181
181
  ],
182
- "cpu": 0.4
182
+ "cpu": 0.4,
183
+ "pills": "below"
183
184
  },
184
185
  "apiB": {
185
186
  "type": "block",
@@ -191,7 +192,8 @@
191
192
  "label": [
192
193
  "API 2"
193
194
  ],
194
- "cpu": 0.4
195
+ "cpu": 0.4,
196
+ "pills": "below"
195
197
  },
196
198
  "apiC": {
197
199
  "type": "block",
@@ -203,7 +205,8 @@
203
205
  "label": [
204
206
  "API 3"
205
207
  ],
206
- "cpu": 0.4
208
+ "cpu": 0.4,
209
+ "pills": "below"
207
210
  },
208
211
  "tier": {
209
212
  "type": "zone",
@@ -225,7 +228,8 @@
225
228
  "Redis"
226
229
  ],
227
230
  "sub": "cache",
228
- "inside": "cache-aside"
231
+ "inside": "cache-aside",
232
+ "look": "cache"
229
233
  },
230
234
  "replica": {
231
235
  "type": "db",
@@ -1848,7 +1852,8 @@
1848
1852
  "glyph": "zap",
1849
1853
  "label": [
1850
1854
  "Redis"
1851
- ]
1855
+ ],
1856
+ "look": "cache"
1852
1857
  },
1853
1858
  "db": {
1854
1859
  "type": "db",
@@ -2071,4 +2076,4 @@
2071
2076
  ]
2072
2077
  }
2073
2078
  }
2074
- }
2079
+ }
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env node
2
+ // Figure: rate limit by token bucket, drawn as a coin-tube dispenser on a bench.
3
+ // node examples/figures/token-bucket.build.mjs → examples/figures/token-bucket.html
4
+ // The object is the explanation: the glass tube is the bucket (capacity 10 coins), the dispenser on the
5
+ // column refills one coin at the refill rate, each request on the rail spends one coin at the
6
+ // turnstile; with the tube empty the request is turned aside into the 429 tray.
7
+ import fs from 'node:fs';
8
+ import path from 'node:path';
9
+ import { fileURLToPath } from 'node:url';
10
+ import { figurePage, kit as K, paint as C } from '../../assets/figure/figure.mjs';
11
+
12
+ const OUT = path.join(path.dirname(fileURLToPath(import.meta.url)), 'token-bucket.html');
13
+ const VIEW = [640, 360];
14
+ const BASE = { x: -120, y: -54, w: 240, d: 108, r: 10 };
15
+ const P = K.fitProjection(K.boxCorners(BASE, -5, 112), VIEW[0], VIEW[1], { pad: 14 });
16
+ const AMBER = { top: '#fee7b8', shade: ['#f0bf6a', '#f4cb80', '#f8d796', '#fbe1ac'] };
17
+ const ROSE = C.toneOf({ top: ['#fde4e8', '#f9d0d8'], side: ['#f3bcc7', '#eaa8b6'] });
18
+ const GREEN = C.toneOf({ top: ['#e2f2d7', '#d1e9c2'], side: ['#c3e1b2', '#b3d79f'] });
19
+ const DARK = C.solidTone('#4A433C');
20
+ const TUBE = [0, -30], CAP = 10, COIN = 4.4, Z0 = 10;
21
+
22
+ const art = [];
23
+ // bench: feet, plate, groove, screws, a ruled strip along the front edge, a label plate
24
+ for (const [x, y] of K.corners(BASE, 14)) art.push(C.solid(K.cylinder(x, y, 6, -5, 5, P, 18), DARK, { crease: 'mid' }));
25
+ art.push(C.solid(K.slabOf(BASE, 0, Z0, P, 8, 1.6), C.NEUTRAL, { crease: 'lo' }) + C.line(K.planOutline(K.insetPlan(BASE, 6), Z0, P), 'lo'));
26
+ for (const [x, y] of K.corners(BASE, 9)) art.push(C.screw(P, x, y, Z0));
27
+ const strip = { x: -104, y: 40, w: 150, d: 7, r: 1 };
28
+ const ticks = K.topTicks(-102, 44, 4, 5, 40.5, Z0 + 1, [2.5, 4.5], P);
29
+ art.push(C.solid(K.slabOf(strip, Z0, 1, P, 3), C.solidTone('#f6efe4'), { line: 'mid' }) + C.line(ticks.minor, 'lo') + C.line(ticks.major, 'mid'));
30
+ const plate = { x: 64, y: 30, w: 34, d: 16, r: 2 };
31
+ art.push(C.solid(K.slabOf(plate, Z0, 1.2, P, 4), C.solidTone('#ebe3d7'), { line: 'mid' }) + C.line([0, 1, 2].map((i) => K.lineOnTop([68, 34.5 + i * 3.6], [92 - i * 6, 34.5 + i * 3.6], Z0 + 1.2, P)).join(''), 'lo'));
32
+ // dispenser: column, coin hopper, arm and nozzle over the tube
33
+ art.push(C.solid(K.slabOf({ x: -34, y: -48, w: 8, d: 8, r: 1.5 }, Z0, 86, P, 4, 0.6), C.NEUTRAL));
34
+ art.push(C.solid(K.slabOf({ x: -42, y: -56, w: 24, d: 24, r: 3 }, Z0 + 86, 14, P, 6, 0.8), AMBER, { crease: 'lo', inner: C.fillPath(K.planOutline({ x: -38, y: -52, w: 16, d: 16, r: 2 }, Z0 + 100, P), '#4A433C', 'mid') }));
35
+ // tube: collar, then the coins (live), then glass, rim and level ticks
36
+ art.push(C.solid(K.cylinder(...TUBE, 14, Z0, 6, P, 36, 0.8), AMBER, { crease: 'lo' }));
37
+ art.push('<g id="coins"></g>');
38
+ art.push(C.glass(K.cylinder(...TUBE, 10, Z0 + 6, CAP * COIN + 6, P, 36)));
39
+ art.push(C.solid(K.cylinder(...TUBE, 11.6, Z0 + 6 + CAP * COIN + 6, 2.4, P, 36), C.NEUTRAL, { crease: 'faint', inner: C.fillPath(K.ring(...TUBE, 9.6, Z0 + 8.4 + CAP * COIN + 6, P), '#4A433C', 'mid') }));
40
+ const front = [TUBE[0] + 10 * Math.SQRT1_2, TUBE[1] + 10 * Math.SQRT1_2];
41
+ art.push(C.line(Array.from({ length: CAP }, (_, i) => K.segment([front[0], front[1], Z0 + 6 + (i + 1) * COIN], [front[0] - 2.4, front[1] + 2.4, Z0 + 6 + (i + 1) * COIN], P)).join(''), 'mid'));
42
+ art.push(C.beam([-30, -44, Z0 + 82], [TUBE[0], TUBE[1], Z0 + 82], [0, 0, 1], 5, 5, P, C.NEUTRAL) + C.solid(K.cylinder(...TUBE, 3.2, Z0 + 72, 8, P, 16), DARK, { crease: 'mid' }));
43
+ art.push('<g id="drop"></g>');
44
+ // rail: back rail, sleepers, live traffic, front rail
45
+ const rail = (y) => C.solid(K.slabOf({ x: -114, y: y - 1.4, w: 210, d: 2.8, r: 1 }, Z0, 3, P, 3), C.solidTone('#d6cabb'), { line: 'mid' });
46
+ art.push(C.line(Array.from({ length: 18 }, (_, i) => K.lineOnTop([-110 + i * 12, -12], [-110 + i * 12, 12], Z0, P)).join(''), 'faint') + rail(-9));
47
+ art.push('<g id="traffic"></g>');
48
+ art.push(rail(9));
49
+ // 429 tray (front), the rejected live layer, then the turnstile and the exit box
50
+ art.push(C.solid(K.slabOf({ x: 8, y: 30, w: 34, d: 20, r: 2 }, Z0, 7, P, 4, 0.6), ROSE, { crease: 'lo', inner: C.fillPath(K.planOutline({ x: 10.5, y: 32.5, w: 29, d: 15, r: 1 }, Z0 + 7, P), '#5F574E', 'mid') }));
51
+ art.push('<g id="rejects"></g>');
52
+ art.push(C.solid(K.cylinder(0, 22, 4.6, Z0, 3, P, 20), DARK, { crease: 'mid' }) + C.solid(K.cylinder(0, 22, 3, Z0 + 3, 22, P, 18), C.NEUTRAL));
53
+ art.push('<g id="gate"></g>');
54
+ art.push(C.solid(K.slabOf({ x: 96, y: -16, w: 20, d: 32, r: 3 }, Z0, 24, P, 6, 0.8), GREEN, { crease: 'lo', inner: C.line(K.sideSeam({ x: 96, y: -16, w: 20, d: 32, r: 3 }, Z0 + 12, P), 'lo') }));
55
+
56
+ const live = `
57
+ const TUBE = ${JSON.stringify(TUBE)}, CAP = ${CAP}, COIN = ${COIN}, Z0 = ${Z0};
58
+ const AMBER = ${JSON.stringify(AMBER)};
59
+ const S = { tokens: CAP, refill: 0, ok: 0, no: 0, spawn: 0, balls: [], drops: [], flies: [], gate: 0, gateTo: 0 };
60
+ const ctl = (id) => +$('#' + id).value;
61
+ for (const id of ['rate', 'rps']) $('#' + id).addEventListener('input', () => { $('#' + id + '-v').textContent = $('#' + id).value + '/s'; });
62
+ const ball = (x, y, z, fill, r = 5.2) => { const [[sx, sy]] = pointsAt([[x, y, z]], P); return '<circle cx="' + sx.toFixed(1) + '" cy="' + (sy - r * 0.3).toFixed(1) + '" r="' + r + '" fill="' + fill + '" stroke="#211e1b" stroke-width="1.2"/>'; };
63
+ function draw() {
64
+ let coins = '';
65
+ const n = Math.floor(S.tokens);
66
+ for (let i = 0; i < n; i++) coins += solid(cylinder(TUBE[0], TUBE[1], 8.4, Z0 + 6.4 + i * COIN, COIN - 0.7, P, 28, 0.4), AMBER, { crease: 'lo' });
67
+ put('coins', coins);
68
+ put('drop', S.drops.map((d) => solid(cylinder(TUBE[0], TUBE[1], 6, d.z, 2.2, P, 20), AMBER, { crease: 'lo' })).join(''));
69
+ put('traffic', S.balls.filter((b) => !b.rej).map((b) => ball(b.x, 0, Z0 + 8, b.ok ? '#9fd08f' : '#f3b47f')).join('') + S.flies.map((f) => '<g opacity="' + (1 - f.t).toFixed(2) + '">' + solid(cylinder(f.x, f.y, 4, f.z, 1.6, P, 16), AMBER, { crease: 'lo' }) + '</g>').join(''));
70
+ put('rejects', S.balls.filter((b) => b.rej).map((b) => '<g opacity="' + Math.min(1, b.life).toFixed(2) + '">' + ball(b.x, b.y, b.z, '#ec7a6f') + '</g>').join(''));
71
+ let arms = '';
72
+ for (let k = 0; k < 3; k++) { const a = S.gate + k * 2.0944; arms += beam([0, 22, Z0 + 23], [Math.cos(a) * 15, 22 + Math.sin(a) * 15, Z0 + 23], [0, 0, 1], 2.4, 2.4, P, NEUTRAL); }
73
+ put('gate', arms + solid(cylinder(0, 22, 4.2, Z0 + 21, 5, P, 18), AMBER, { crease: 'lo' }));
74
+ readout('fichas ' + n + '/' + CAP + ' · aceitas ' + S.ok + ' · 429 ' + S.no);
75
+ }
76
+ loop((dt) => {
77
+ const rate = ctl('rate'), rps = ctl('rps');
78
+ // refill: one coin leaves the nozzle every 1/rate s while the tube has room
79
+ S.refill += dt * rate;
80
+ while (S.refill >= 1) { S.refill -= 1; if (S.tokens + S.drops.length < CAP) S.drops.push({ z: Z0 + 72 }); }
81
+ for (const d of S.drops) d.z -= dt * 120;
82
+ S.drops = S.drops.filter((d) => { if (d.z <= Z0 + 6.4 + Math.floor(S.tokens) * COIN) { S.tokens = Math.min(CAP, Math.floor(S.tokens) + 1); return false; } return true; });
83
+ // traffic: requests roll in from the left; at the turnstile each one spends a coin or is turned aside
84
+ S.spawn += dt * rps;
85
+ while (S.spawn >= 1) { S.spawn -= 1; S.balls.push({ x: -112, y: 0, z: Z0 + 8, ok: false, rej: false, life: 1 }); }
86
+ for (const b of S.balls) {
87
+ if (b.rej) { b.y = Math.min(40, b.y + dt * 60); b.x = Math.min(24, b.x + dt * 25); if (b.y >= 40) { b.z = Math.max(Z0 + 6, b.z - dt * 30); b.life -= dt * 0.7; } continue; }
88
+ const before = b.x; b.x += dt * 80;
89
+ if (before < 0 && b.x >= 0) {
90
+ if (S.tokens >= 1) { S.tokens -= 1; b.ok = true; S.ok++; S.gateTo += 2.0944; S.flies.push({ x: TUBE[0], y: TUBE[1], z: Z0 + 8, t: 0 }); }
91
+ else { b.rej = true; S.no++; }
92
+ }
93
+ }
94
+ S.balls = S.balls.filter((b) => b.x < 98 && b.life > 0);
95
+ for (const f of S.flies) { f.t += dt * 2.2; f.y = TUBE[1] + (0 - TUBE[1]) * f.t; f.z = Z0 + 8 + Math.sin(f.t * Math.PI) * 16; }
96
+ S.flies = S.flies.filter((f) => f.t < 1);
97
+ S.gate += (S.gateTo - S.gate) * (1 - Math.exp(-10 * dt));
98
+ draw();
99
+ });
100
+ draw();
101
+ `;
102
+
103
+ const controls = `<label>reposição <input id="rate" type="range" min="0.5" max="5" step="0.5" value="2"><output id="rate-v">2/s</output></label>
104
+ <label>tráfego <input id="rps" type="range" min="0.5" max="8" step="0.5" value="3"><output id="rps-v">3/s</output></label>`;
105
+
106
+ fs.writeFileSync(OUT, figurePage({
107
+ title: 'Rate limit · token bucket', fig: 'Fig 1', hint: 'suba o tráfego para esvaziar o tubo',
108
+ readout: `fichas ${CAP}/${CAP} · aceitas 0 · 429 0`, svg: art.join('\n'), view: VIEW, P, live, controls,
109
+ caption: 'Cada cliente tem um tubo com 10 fichas, reposto pelo dispensador. Cada requisição gasta uma ficha na catraca; com o tubo vazio, o gateway responde 429.',
110
+ }));
111
+ console.log(`✓ ${path.relative(process.cwd(), OUT)} · ${(fs.statSync(OUT).size / 1024).toFixed(0)} KB`);