@rulecad/model-link 0.2.2 → 0.3.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/model-link.d.mts +10 -1
- package/model-link.mjs +10 -3
- package/package.json +1 -1
package/model-link.d.mts
CHANGED
|
@@ -29,7 +29,16 @@ export function parseHash(hash: string): {
|
|
|
29
29
|
export function fromGist(id: string): Promise<string>;
|
|
30
30
|
/** A link token back to a model, checked: damaged, newer or oversized links are refused. @param {string} token @returns {Promise<Model>} */
|
|
31
31
|
export function decode(token: string): Promise<Model>;
|
|
32
|
-
|
|
32
|
+
/**
|
|
33
|
+
* What's wrong with a model's data, as findings (the kit's one result shape): [{ level: 'fail', text, code: 'link.data' }],
|
|
34
|
+
* empty when it's sound.
|
|
35
|
+
* @param {any} m @returns {{ level: 'fail', text: string, code: string }[]}
|
|
36
|
+
*/
|
|
37
|
+
export function check(m: any): {
|
|
38
|
+
level: "fail";
|
|
39
|
+
text: string;
|
|
40
|
+
code: string;
|
|
41
|
+
}[];
|
|
33
42
|
export function expand(m: any): any;
|
|
34
43
|
export function fromShapes(list: any): {
|
|
35
44
|
shapes: {
|
package/model-link.mjs
CHANGED
|
@@ -113,12 +113,12 @@ export async function decode(token) {
|
|
|
113
113
|
try { model = JSON.parse(new TextDecoder().decode(await pipe(unb64(m[2]), new DecompressionStream('deflate-raw'), LIMITS.bytes))); }
|
|
114
114
|
catch (e) { if (e?.tooBig) throw e; throw new Error('This link is damaged: it may have been cut short when it was shared. Copy the whole link again.'); }
|
|
115
115
|
if (model && Array.isArray(model.shapes)) model = expand(model);
|
|
116
|
-
const errors =
|
|
116
|
+
const errors = problems(model);
|
|
117
117
|
if (errors.length) throw new Error(errors[0]);
|
|
118
118
|
return model;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
|
|
121
|
+
function problems(m) {
|
|
122
122
|
if (!m || typeof m !== 'object' || !Array.isArray(m.p) || !Array.isArray(m.t)) return ['no model in the link'];
|
|
123
123
|
if (typeof m.q !== 'number' || !Number.isFinite(m.q) || !(m.q > 0)) return ['bad grid size'];
|
|
124
124
|
if (m.p.length % 3 || m.t.length % 3) return ['broken model data'];
|
|
@@ -136,6 +136,13 @@ export function check(m) {
|
|
|
136
136
|
return [];
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
+
/**
|
|
140
|
+
* What's wrong with a model's data, as findings (the kit's one result shape): [{ level: 'fail', text, code: 'link.data' }],
|
|
141
|
+
* empty when it's sound.
|
|
142
|
+
* @param {any} m @returns {{ level: 'fail', text: string, code: string }[]}
|
|
143
|
+
*/
|
|
144
|
+
export function check(m) { return problems(m).map((text) => ({ level: 'fail', text, code: 'link.data' })); }
|
|
145
|
+
|
|
139
146
|
// A link's motion, as data: numbers where numbers go, items and joints that exist, nothing that could run
|
|
140
147
|
const nums = (a, n) => Array.isArray(a) && (n == null || a.length === n) && a.every((x) => typeof x === 'number' && Number.isFinite(x));
|
|
141
148
|
function motionOk(mo, places) {
|
|
@@ -157,7 +164,7 @@ function motionOk(mo, places) {
|
|
|
157
164
|
export function expand(m) {
|
|
158
165
|
const { shapes, place, ...meta } = m;
|
|
159
166
|
if (!Array.isArray(place) || shapes.length > LIMITS.shapes || place.length > LIMITS.places) throw new Error('model too big for a link');
|
|
160
|
-
for (const sh of shapes) { const e =
|
|
167
|
+
for (const sh of shapes) { const e = problems(sh); if (e.length) throw new Error(e[0]); }
|
|
161
168
|
// (counted before any copy is made: a few placements of a big shape would otherwise be built in full, then refused)
|
|
162
169
|
let count = 0;
|
|
163
170
|
for (const [i] of place) { count += (shapes[i]?.t.length || 0) / 3; if (count > LIMITS.triangles) throw new Error('model too big for a link'); }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/model-link",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Share a printable model as a link (the model lives in the URL), and write STL or multi-colour Bambu Studio 3MF files, in the browser or Node.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|