@pascal-app/geometry-script 1.0.5 → 1.0.7

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.
@@ -1 +1 @@
1
- {"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;AAKjE,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK1D;AA2BD,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,MAAM,CAmC1E"}
1
+ {"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;AAKjE,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK1D;AAmID,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,MAAM,CAmC1E"}
package/dist/transform.js CHANGED
@@ -39,9 +39,118 @@ function bindingsFor(clause, moduleRef) {
39
39
  }
40
40
  return out.join(' ');
41
41
  }
42
+ /** Before a `/`, these leave an operand expected: the slash opens a regular expression. */
43
+ const BEFORE_REGEX = new Set([...'(,=:[!&|?{};+-*%<>~^']);
44
+ const KEYWORD_BEFORE_REGEX = /(?:^|[^\w$])(?:return|typeof|case|in|of|new|delete|void|throw|else|do|yield|await)$/;
45
+ /**
46
+ * Which characters of a module are its own code: not inside a string, a template literal's text,
47
+ * a comment or a regular expression. A module may carry other modules as text (an adapter writes
48
+ * the modules of a scene), and their `import` and `export` are its data, not its statements.
49
+ */
50
+ function codeMask(code) {
51
+ const mask = new Uint8Array(code.length);
52
+ // Each open template expression (`${`) keeps the brace depth it returns at.
53
+ const templates = [];
54
+ let depth = 0;
55
+ let last = '';
56
+ let i = 0;
57
+ const skipQuoted = (quote) => {
58
+ for (i++; i < code.length && code[i] !== quote && code[i] !== '\n'; i++)
59
+ if (code[i] === '\\')
60
+ i++;
61
+ i++;
62
+ };
63
+ /** Template text from i (just past a backtick or a closing brace) to its end or its next `${`. */
64
+ const skipTemplate = () => {
65
+ for (; i < code.length; i++) {
66
+ if (code[i] === '\\')
67
+ i++;
68
+ else if (code[i] === '`') {
69
+ i++;
70
+ return;
71
+ }
72
+ else if (code[i] === '$' && code[i + 1] === '{') {
73
+ templates.push(depth);
74
+ depth++;
75
+ i += 2;
76
+ return;
77
+ }
78
+ }
79
+ };
80
+ while (i < code.length) {
81
+ const c = code[i];
82
+ const next = code[i + 1];
83
+ if (c === '/' && next === '/') {
84
+ while (i < code.length && code[i] !== '\n')
85
+ i++;
86
+ }
87
+ else if (c === '/' && next === '*') {
88
+ const end = code.indexOf('*/', i + 2);
89
+ i = end < 0 ? code.length : end + 2;
90
+ }
91
+ else if (c === "'" || c === '"') {
92
+ skipQuoted(c);
93
+ last = c;
94
+ }
95
+ else if (c === '`') {
96
+ i++;
97
+ skipTemplate();
98
+ last = '`';
99
+ }
100
+ else if (c === '/' &&
101
+ (last === '' ||
102
+ BEFORE_REGEX.has(last) ||
103
+ KEYWORD_BEFORE_REGEX.test(code.slice(Math.max(0, i - 8), i).trimEnd()))) {
104
+ let inClass = false;
105
+ for (i++; i < code.length && code[i] !== '\n'; i++) {
106
+ if (code[i] === '\\')
107
+ i++;
108
+ else if (code[i] === '[')
109
+ inClass = true;
110
+ else if (code[i] === ']')
111
+ inClass = false;
112
+ else if (code[i] === '/' && !inClass)
113
+ break;
114
+ }
115
+ i++;
116
+ last = '/';
117
+ }
118
+ else if (c === '}' && templates.length && templates.at(-1) === depth - 1) {
119
+ templates.pop();
120
+ depth--;
121
+ i++;
122
+ skipTemplate();
123
+ last = '`';
124
+ }
125
+ else {
126
+ mask[i] = 1;
127
+ if (c === '{')
128
+ depth++;
129
+ else if (c === '}')
130
+ depth--;
131
+ if (!/\s/.test(c))
132
+ last = c;
133
+ i++;
134
+ }
135
+ }
136
+ return mask;
137
+ }
138
+ /** `text.replace(pattern, replace)` for the matches whose first word is the module's own code. */
139
+ function replaceInCode(text, pattern, replace) {
140
+ const mask = codeMask(text);
141
+ let done = false;
142
+ return text.replace(new RegExp(pattern.source, `${pattern.flags.replace('g', '')}g`), (...args) => {
143
+ const match = args[0];
144
+ const offset = args.at(-2);
145
+ if ((done && !pattern.global) || !mask[offset + match.search(/\S/)])
146
+ return match;
147
+ done = true;
148
+ return replace(match, ...args.slice(1, -2));
149
+ });
150
+ }
42
151
  export function transformModule(code, modules) {
43
152
  const unknown = [];
44
- let body = code.replace(IMPORT_RE, (_all, clause, specifier) => {
153
+ let body = replaceInCode(code, IMPORT_RE, (_all, clause, specifier) => {
45
154
  const key = moduleKey(specifier);
46
155
  if (!key || !(key in modules)) {
47
156
  unknown.push(specifier);
@@ -49,18 +158,17 @@ export function transformModule(code, modules) {
49
158
  }
50
159
  return bindingsFor(clause, `__modules[${JSON.stringify(key)}]`);
51
160
  });
52
- body = body.replace(SIDE_EFFECT_IMPORT_RE, '');
161
+ body = replaceInCode(body, SIDE_EFFECT_IMPORT_RE, () => '');
53
162
  if (unknown.length > 0) {
54
163
  const available = Object.keys(modules)
55
164
  .map((key) => (key === 'three' || key === 'three-bvh-csg' ? key : `three/addons/…/${key}.js`))
56
165
  .join(', ');
57
166
  throw new Error(`Unsupported import ${unknown.join(', ')}. Available: ${available}.`);
58
167
  }
59
- body = body
60
- .replace(/export\s+default\s+(async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)?\s*\(/, (_m, a, n) => n ? `const __default = ${a ?? ''}function ${n}(` : `const __default = ${a ?? ''}function (`)
61
- .replace(/export\s+default\s+/, 'const __default = ')
62
- .replace(/export\s+(const|let|var|function|class)\s/g, '$1 ')
63
- .replace(/export\s*\{[^}]*\}\s*;?/g, '');
168
+ body = replaceInCode(body, /export\s+default\s+(async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)?\s*\(/, (_m, a, n) => n ? `const __default = ${a ?? ''}function ${n}(` : `const __default = ${a ?? ''}function (`);
169
+ body = replaceInCode(body, /export\s+default\s+/, () => 'const __default = ');
170
+ body = replaceInCode(body, /export\s+(const|let|var|function|class)\s/g, (_m, kind) => `${kind} `);
171
+ body = replaceInCode(body, /export\s*\{[^}]*\}\s*;?/g, () => '');
64
172
  // A block scope lets `import * as THREE from 'three'` shadow the injected THREE.
65
173
  return `{
66
174
  ${body}
@@ -0,0 +1,15 @@
1
+ import type { GeometryShape } from '@pascal-app/core/schema';
2
+ import type * as THREE from 'three';
3
+ /** What the shape may weigh in the manifest, which rides inline in the node (24 KiB in all). */
4
+ export declare const SHAPE_MAX_BYTES: number;
5
+ /**
6
+ * What the build occupies in plan at each height, for the usage checks: the footprint of everything
7
+ * it has in each 2 cm layer, runs of near-identical layers read as bands, each band traced to
8
+ * outlines. The fill is conservative, so an outline is never inside the build and at worst a cell
9
+ * (5 mm) outside it, plus the simplification; `tolerance` states that bound. A build too intricate
10
+ * for its budget is read ever more coarsely, bands merged and outlines simplified, and in the end as
11
+ * one box. Frames are the meshes' world frame, so call it on a built object in the artifact's frame.
12
+ * Null for an empty one.
13
+ */
14
+ export declare function readUsageShape(meshes: THREE.Mesh[]): GeometryShape | null;
15
+ //# sourceMappingURL=usage-shape.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage-shape.d.ts","sourceRoot":"","sources":["../src/usage-shape.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAA;AAKnC,gGAAgG;AAChG,eAAO,MAAM,eAAe,QAAY,CAAA;AAmqBxC;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,GAAG,aAAa,GAAG,IAAI,CAqCzE"}