@margoengine/loaders 0.1.0 → 1.0.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 (82) hide show
  1. package/LICENSE +201 -201
  2. package/NOTICE +4 -4
  3. package/README.md +1666 -1198
  4. package/dist/crease.d.ts +7 -0
  5. package/dist/crease.js +67 -0
  6. package/dist/dae-common.js +1 -1
  7. package/dist/dae-kinematics.d.ts +65 -0
  8. package/dist/dae-kinematics.js +461 -0
  9. package/dist/dae.d.ts +9 -4
  10. package/dist/dae.js +16 -5
  11. package/dist/dxf-hatch.d.ts +8 -0
  12. package/dist/dxf-hatch.js +346 -0
  13. package/dist/dxf-leaders.d.ts +59 -0
  14. package/dist/dxf-leaders.js +258 -0
  15. package/dist/dxf.d.ts +42 -1
  16. package/dist/dxf.js +226 -15
  17. package/dist/engine.d.ts +5 -0
  18. package/dist/engine.js +3 -0
  19. package/dist/gcode.d.ts +37 -0
  20. package/dist/gcode.js +91 -0
  21. package/dist/geometry.d.ts +1 -1
  22. package/dist/index.d.ts +1 -1
  23. package/dist/mtl.d.ts +8 -3
  24. package/dist/mtl.js +91 -10
  25. package/dist/nrrd.d.ts +12 -5
  26. package/dist/nrrd.js +68 -26
  27. package/dist/obj.d.ts +3 -1
  28. package/dist/obj.js +7 -1
  29. package/dist/render.d.ts +9 -4
  30. package/dist/render.js +31 -3
  31. package/dist/shared/asset-url.js +82 -82
  32. package/dist/shared/crc32.d.ts +2 -2
  33. package/dist/shared/crc32.js +47 -47
  34. package/dist/shared/errors.js +36 -36
  35. package/dist/shared/inflate.js +284 -284
  36. package/dist/shared/normals.js +26 -26
  37. package/dist/shared/scene-scope.js +54 -54
  38. package/dist/shared/trailing-data.js +45 -42
  39. package/dist/threemf-beam-reader.d.ts +19 -0
  40. package/dist/threemf-beam-reader.js +165 -0
  41. package/dist/threemf-beams.d.ts +52 -0
  42. package/dist/threemf-beams.js +268 -0
  43. package/dist/threemf-boolean-read.d.ts +10 -0
  44. package/dist/threemf-boolean-read.js +113 -0
  45. package/dist/threemf-boolean.d.ts +59 -0
  46. package/dist/threemf-boolean.js +20 -0
  47. package/dist/threemf-csg.d.ts +14 -0
  48. package/dist/threemf-csg.js +193 -0
  49. package/dist/threemf-displacement-read.d.ts +8 -0
  50. package/dist/threemf-displacement-read.js +213 -0
  51. package/dist/threemf-displacement.d.ts +71 -0
  52. package/dist/threemf-displacement.js +18 -0
  53. package/dist/threemf-evaluate.d.ts +36 -0
  54. package/dist/threemf-evaluate.js +86 -0
  55. package/dist/threemf-functions.d.ts +33 -0
  56. package/dist/threemf-functions.js +359 -0
  57. package/dist/threemf-lattice-mesh.d.ts +9 -0
  58. package/dist/threemf-lattice-mesh.js +115 -0
  59. package/dist/threemf-lattice.js +10 -145
  60. package/dist/threemf-levelset.d.ts +23 -0
  61. package/dist/threemf-levelset.js +105 -0
  62. package/dist/threemf-materials.d.ts +89 -2
  63. package/dist/threemf-materials.js +194 -11
  64. package/dist/threemf-mesh.d.ts +21 -6
  65. package/dist/threemf-mesh.js +266 -45
  66. package/dist/threemf-secure.d.ts +50 -0
  67. package/dist/threemf-secure.js +153 -0
  68. package/dist/threemf-tessellate.d.ts +37 -0
  69. package/dist/threemf-tessellate.js +277 -0
  70. package/dist/threemf-volume-colour.d.ts +25 -0
  71. package/dist/threemf-volume-colour.js +233 -0
  72. package/dist/threemf-volume-sample.d.ts +8 -0
  73. package/dist/threemf-volume-sample.js +65 -0
  74. package/dist/threemf-volumetric-read.d.ts +15 -0
  75. package/dist/threemf-volumetric-read.js +539 -0
  76. package/dist/threemf-volumetric.d.ts +177 -0
  77. package/dist/threemf-volumetric.js +28 -0
  78. package/dist/threemf.d.ts +73 -4
  79. package/dist/threemf.js +235 -41
  80. package/dist/types.d.ts +18 -0
  81. package/dist/workers/usd.js +1 -1
  82. package/package.json +291 -246
@@ -1,82 +1,82 @@
1
- // URIs read from inside a model file (glTF buffers and images, FBX/MTL texture names, 3MF/USDZ references) are
2
- // untrusted. They resolve against the file's own URL and must stay inside its directory on the same origin; anything
3
- // else (absolute or cross-origin URLs, `../` past the base, non-HTTP schemes such as javascript:, file: or blob:) is
4
- // refused unless it lies under one of the app's `allowedURLs` entries. data: URIs are decoded by each loader within
5
- // its byte budget and never reach this function.
6
-
7
- // Encoded separators and encoded dot segments are refused on every branch: some servers decode `..%2F` or `%2e%2e`
8
- // into a parent step after this check, so a path that looks contained here could leave the directory there.
9
- // A proxy chain may decode more than once, so the same rule is applied after each decoding round (`%252e%252e`
10
- // becomes `%2e%2e`, which the rule refuses). A path still changing after 8 rounds is refused.
11
- // An encoded NUL (`%00`, at any depth, or the NUL itself once decoded) is refused too: it ends the name in C-based
12
- // file APIs, so `ok%00.png` can name `ok` there (audit 2026-10-28).
13
- const ENCODED_ESCAPE = /%2f|%5c|%00|\u0000|(^|\/)(\.|%2e){1,2}(\/|$)/i;
14
-
15
- /** True when the path, at any depth of percent-decoding, holds an encoded separator or a dot segment. */
16
- function hidesEscape(path) {
17
- for (let round = 0; round < 8; round++) {
18
- if (ENCODED_ESCAPE.test(path)) return true;
19
- const next = path.replace(/%[\da-f]{2}/gi, (triplet) => String.fromCharCode(`0x${triplet.slice(1)}`));
20
- if (next === path) return false;
21
- path = next;
22
- }
23
- return true;
24
- }
25
-
26
- /**
27
- * True when url lies under the allowedURLs entry prefix. An entry ending in `/` is a directory and matches everything
28
- * below it; any other entry matches that exact path, or a directory of that name (`/assets` matches `/assets`,
29
- * `/assets/x.png` and `/assets?v=1`, never `/assets-other`). An entry with a query string matches only that query.
30
- */
31
- function underPrefix(url, prefix) {
32
- let entry;
33
- try {
34
- entry = new URL(prefix);
35
- } catch {
36
- throw new TypeError(`allowedURLs entry ${String(prefix)} is not an absolute URL; pass a full http(s) URL`);
37
- }
38
- if (entry.origin !== url.origin || (entry.protocol !== 'http:' && entry.protocol !== 'https:')) return false;
39
- if (entry.search && entry.search !== url.search) return false;
40
- const dir = entry.pathname;
41
- if (dir.endsWith('/')) return url.pathname.startsWith(dir);
42
- return url.pathname === dir || url.pathname.startsWith(`${dir}/`);
43
- }
44
-
45
- /** Resolve a model-file URI against base (the model's URL), or throw a TypeError naming the refused URL. */
46
- export function assetURL(uri, base, allowedURLs = [], label = 'Asset') {
47
- if (typeof uri !== 'string' || !uri || uri.length > 8192)
48
- throw new TypeError(`Invalid ${label} URI; pass a shorter string`);
49
- let url;
50
- try {
51
- url = new URL(uri, base);
52
- } catch {
53
- throw new TypeError(`Invalid ${label} URI; pass a valid URL`);
54
- }
55
- if ((url.protocol === 'http:' || url.protocol === 'https:') && !hidesEscape(url.pathname)) {
56
- for (const prefix of allowedURLs) if (underPrefix(url, prefix)) return url;
57
- const root = base === undefined ? undefined : new URL('.', base);
58
- if (root && url.origin === root.origin && url.pathname.startsWith(root.pathname)) return url;
59
- }
60
- throw new TypeError(
61
- `${label} URI ${url.href} leaves the model's directory; add its prefix to allowedURLs to load it`,
62
- );
63
- }
64
-
65
- /**
66
- * Check where a fetch of a model-file resource ended up: fetch follows redirects, so the final `response.url` passes
67
- * the same assetURL policy (base and allowedURLs) as the URI did. On a violation the body is cancelled unread and a
68
- * TypeError names the final URL. A response with no url (a custom fetch that builds its own Response) is taken as the
69
- * URL it was asked for.
70
- */
71
- export function checkRedirect(response, base, allowedURLs = [], label = 'Asset') {
72
- if (!response.url) return response;
73
- try {
74
- assetURL(response.url, base, allowedURLs, label);
75
- } catch {
76
- response.body?.cancel().catch(() => {});
77
- throw new TypeError(
78
- `${label} URI redirected to ${response.url}, which leaves the model's directory; add its prefix to allowedURLs to load it`,
79
- );
80
- }
81
- return response;
82
- }
1
+ // URIs read from inside a model file (glTF buffers and images, FBX/MTL texture names, 3MF/USDZ references) are
2
+ // untrusted. They resolve against the file's own URL and must stay inside its directory on the same origin; anything
3
+ // else (absolute or cross-origin URLs, `../` past the base, non-HTTP schemes such as javascript:, file: or blob:) is
4
+ // refused unless it lies under one of the app's `allowedURLs` entries. data: URIs are decoded by each loader within
5
+ // its byte budget and never reach this function.
6
+
7
+ // Encoded separators and encoded dot segments are refused on every branch: some servers decode `..%2F` or `%2e%2e`
8
+ // into a parent step after this check, so a path that looks contained here could leave the directory there.
9
+ // A proxy chain may decode more than once, so the same rule is applied after each decoding round (`%252e%252e`
10
+ // becomes `%2e%2e`, which the rule refuses). A path still changing after 8 rounds is refused.
11
+ // An encoded NUL (`%00`, at any depth, or the NUL itself once decoded) is refused too: it ends the name in C-based
12
+ // file APIs, so `ok%00.png` can name `ok` there (audit 2026-10-28).
13
+ const ENCODED_ESCAPE = /%2f|%5c|%00|\u0000|(^|\/)(\.|%2e){1,2}(\/|$)/i;
14
+
15
+ /** True when the path, at any depth of percent-decoding, holds an encoded separator or a dot segment. */
16
+ function hidesEscape(path) {
17
+ for (let round = 0; round < 8; round++) {
18
+ if (ENCODED_ESCAPE.test(path)) return true;
19
+ const next = path.replace(/%[\da-f]{2}/gi, (triplet) => String.fromCharCode(`0x${triplet.slice(1)}`));
20
+ if (next === path) return false;
21
+ path = next;
22
+ }
23
+ return true;
24
+ }
25
+
26
+ /**
27
+ * True when url lies under the allowedURLs entry prefix. An entry ending in `/` is a directory and matches everything
28
+ * below it; any other entry matches that exact path, or a directory of that name (`/assets` matches `/assets`,
29
+ * `/assets/x.png` and `/assets?v=1`, never `/assets-other`). An entry with a query string matches only that query.
30
+ */
31
+ function underPrefix(url, prefix) {
32
+ let entry;
33
+ try {
34
+ entry = new URL(prefix);
35
+ } catch {
36
+ throw new TypeError(`allowedURLs entry ${String(prefix)} is not an absolute URL; pass a full http(s) URL`);
37
+ }
38
+ if (entry.origin !== url.origin || (entry.protocol !== 'http:' && entry.protocol !== 'https:')) return false;
39
+ if (entry.search && entry.search !== url.search) return false;
40
+ const dir = entry.pathname;
41
+ if (dir.endsWith('/')) return url.pathname.startsWith(dir);
42
+ return url.pathname === dir || url.pathname.startsWith(`${dir}/`);
43
+ }
44
+
45
+ /** Resolve a model-file URI against base (the model's URL), or throw a TypeError naming the refused URL. */
46
+ export function assetURL(uri, base, allowedURLs = [], label = 'Asset') {
47
+ if (typeof uri !== 'string' || !uri || uri.length > 8192)
48
+ throw new TypeError(`Invalid ${label} URI; pass a shorter string`);
49
+ let url;
50
+ try {
51
+ url = new URL(uri, base);
52
+ } catch {
53
+ throw new TypeError(`Invalid ${label} URI; pass a valid URL`);
54
+ }
55
+ if ((url.protocol === 'http:' || url.protocol === 'https:') && !hidesEscape(url.pathname)) {
56
+ for (const prefix of allowedURLs) if (underPrefix(url, prefix)) return url;
57
+ const root = base === undefined ? undefined : new URL('.', base);
58
+ if (root && url.origin === root.origin && url.pathname.startsWith(root.pathname)) return url;
59
+ }
60
+ throw new TypeError(
61
+ `${label} URI ${url.href} leaves the model's directory; add its prefix to allowedURLs to load it`,
62
+ );
63
+ }
64
+
65
+ /**
66
+ * Check where a fetch of a model-file resource ended up: fetch follows redirects, so the final `response.url` passes
67
+ * the same assetURL policy (base and allowedURLs) as the URI did. On a violation the body is cancelled unread and a
68
+ * TypeError names the final URL. A response with no url (a custom fetch that builds its own Response) is taken as the
69
+ * URL it was asked for.
70
+ */
71
+ export function checkRedirect(response, base, allowedURLs = [], label = 'Asset') {
72
+ if (!response.url) return response;
73
+ try {
74
+ assetURL(response.url, base, allowedURLs, label);
75
+ } catch {
76
+ response.body?.cancel().catch(() => {});
77
+ throw new TypeError(
78
+ `${label} URI redirected to ${response.url}, which leaves the model's directory; add its prefix to allowedURLs to load it`,
79
+ );
80
+ }
81
+ return response;
82
+ }
@@ -1,2 +1,2 @@
1
- /** CRC-32 of `bytes`, continuing from `crc` (the result of a previous call) when given. */
2
- export function crc32(bytes: Uint8Array, crc?: number): number;
1
+ /** CRC-32 of `bytes`, continuing from `crc` (the result of a previous call) when given. */
2
+ export function crc32(bytes: Uint8Array, crc?: number): number;
@@ -1,47 +1,47 @@
1
- // CRC-32 (ISO 3309, reflected polynomial 0xEDB88320): the exporters ZIP, USDZ and PNG writers, the decoders PNG reader,
2
- // the headless PNG reader, the loaders ZIP reader (its public crc32), and the voxel-pathtracer PNG writer.
3
-
4
- /** Eight 256-entry tables: table k advances a byte through k further zero bytes ("slicing-by-8"). */
5
- let tables;
6
-
7
- function build() {
8
- tables = new Int32Array(256 * 8);
9
- for (let n = 0; n < 256; n++) {
10
- let c = n;
11
- for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
12
- tables[n] = c;
13
- }
14
- for (let n = 0; n < 256; n++)
15
- for (let k = 1; k < 8; k++) {
16
- const previous = tables[(k - 1) * 256 + n];
17
- tables[k * 256 + n] = (previous >>> 8) ^ tables[previous & 255];
18
- }
19
- return tables;
20
- }
21
-
22
- /**
23
- * CRC-32 of `bytes`, continuing from `crc` (the result of a previous call) when given. Eight bytes per step through
24
- * the sliced tables (about 4x the byte-at-a-time loop: the USDZ writer spent 8 of its 20 ms here), the tail a byte at a
25
- * time; the result is the same.
26
- */
27
- export function crc32(bytes, crc = 0) {
28
- const t = tables ?? build(),
29
- n = bytes.length,
30
- end = n - (n & 7);
31
- let c = ~crc,
32
- i = 0;
33
- for (; i < end; i += 8) {
34
- c ^= bytes[i] | (bytes[i + 1] << 8) | (bytes[i + 2] << 16) | (bytes[i + 3] << 24);
35
- c =
36
- t[1792 + (c & 255)] ^
37
- t[1536 + ((c >>> 8) & 255)] ^
38
- t[1280 + ((c >>> 16) & 255)] ^
39
- t[1024 + (c >>> 24)] ^
40
- t[768 + bytes[i + 4]] ^
41
- t[512 + bytes[i + 5]] ^
42
- t[256 + bytes[i + 6]] ^
43
- t[bytes[i + 7]];
44
- }
45
- for (; i < n; i++) c = t[(c ^ bytes[i]) & 255] ^ (c >>> 8);
46
- return ~c >>> 0;
47
- }
1
+ // CRC-32 (ISO 3309, reflected polynomial 0xEDB88320): the exporters ZIP, USDZ and PNG writers, the decoders PNG reader,
2
+ // the headless PNG reader, the loaders ZIP reader (its public crc32), and the voxel-pathtracer PNG writer.
3
+
4
+ /** Eight 256-entry tables: table k advances a byte through k further zero bytes ("slicing-by-8"). */
5
+ let tables;
6
+
7
+ function build() {
8
+ tables = new Int32Array(256 * 8);
9
+ for (let n = 0; n < 256; n++) {
10
+ let c = n;
11
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
12
+ tables[n] = c;
13
+ }
14
+ for (let n = 0; n < 256; n++)
15
+ for (let k = 1; k < 8; k++) {
16
+ const previous = tables[(k - 1) * 256 + n];
17
+ tables[k * 256 + n] = (previous >>> 8) ^ tables[previous & 255];
18
+ }
19
+ return tables;
20
+ }
21
+
22
+ /**
23
+ * CRC-32 of `bytes`, continuing from `crc` (the result of a previous call) when given. Eight bytes per step through
24
+ * the sliced tables (about 4x the byte-at-a-time loop: the USDZ writer spent 8 of its 20 ms here), the tail a byte at a
25
+ * time; the result is the same.
26
+ */
27
+ export function crc32(bytes, crc = 0) {
28
+ const t = tables ?? build(),
29
+ n = bytes.length,
30
+ end = n - (n & 7);
31
+ let c = ~crc,
32
+ i = 0;
33
+ for (; i < end; i += 8) {
34
+ c ^= bytes[i] | (bytes[i + 1] << 8) | (bytes[i + 2] << 16) | (bytes[i + 3] << 24);
35
+ c =
36
+ t[1792 + (c & 255)] ^
37
+ t[1536 + ((c >>> 8) & 255)] ^
38
+ t[1280 + ((c >>> 16) & 255)] ^
39
+ t[1024 + (c >>> 24)] ^
40
+ t[768 + bytes[i + 4]] ^
41
+ t[512 + bytes[i + 5]] ^
42
+ t[256 + bytes[i + 6]] ^
43
+ t[bytes[i + 7]];
44
+ }
45
+ for (; i < n; i++) c = t[(c ^ bytes[i]) & 255] ^ (c >>> 8);
46
+ return ~c >>> 0;
47
+ }
@@ -1,36 +1,36 @@
1
- // The error convention (docs/conventions.md) for code in packages/shared, which is bundled into packages that may not depend
2
- // on @margoengine/math (importing it here would make every such package build after math without declaring it).
3
- // The result has MargoError's name and `code`, so `error instanceof MargoError` (it matches by name) and
4
- // `error.code` behave exactly as for the class.
5
- export const margoError = (code, message) => Object.assign(new Error(message), { name: 'MargoError', code });
6
-
7
- // Core and the stage keep their messages short and link each one to its entry on the error help page
8
- // (site/errors.html, generated from scripts/errors/entries.js; tests/errors-page.test.js keeps them in step).
9
- const linked = (message, anchor) => `${message}; see https://margoengine.com/errors#${anchor}`;
10
- /** `margoError` whose message links to `anchor` (default the code) on the error help page. */
11
- export const fault = (code, message, anchor = code) => margoError(code, linked(message, anchor));
12
- /** TypeError whose message links to `anchor` on the error help page. */
13
- export const typeError = (message, anchor) => new TypeError(linked(message, anchor));
14
- /** RangeError whose message links to `anchor` on the error help page. */
15
- export const rangeError = (message, anchor) => new RangeError(linked(message, anchor));
16
-
17
- /** Reject replacement of a retained transform vector, including from non-module callers. */
18
- export const vectorAssignment = () => {
19
- throw Object.assign(typeError('Retained vector: use a transform setter', 'transform'), { code: 'transform' });
20
- };
21
-
22
- /** Translate only policy-blocked Wasm initialization, including Emscripten's wrapped abort. Keep other failures intact. */
23
- export function wasmError(error) {
24
- if (
25
- !/^(Compile|Eval|Runtime)Error$/.test(error?.name) ||
26
- !/wasm-unsafe-eval|content security policy|\bCSP\b|disallowed by embedder/i.test(error.message)
27
- )
28
- return error;
29
- return Object.assign(
30
- margoError(
31
- 'failed',
32
- "Wasm compilation blocked by CSP; add 'wasm-unsafe-eval' to script-src; see https://margoengine.com/llms/docs/csp.md",
33
- ),
34
- { cause: error },
35
- );
36
- }
1
+ // The error convention (docs/conventions.md) for code in packages/shared, which is bundled into packages that may not depend
2
+ // on @margoengine/math (importing it here would make every such package build after math without declaring it).
3
+ // The result has MargoError's name and `code`, so `error instanceof MargoError` (it matches by name) and
4
+ // `error.code` behave exactly as for the class.
5
+ export const margoError = (code, message) => Object.assign(new Error(message), { name: 'MargoError', code });
6
+
7
+ // Core and the stage keep their messages short and link each one to its entry on the error help page
8
+ // (site/errors.html, generated from scripts/errors/entries.js; tests/errors-page.test.js keeps them in step).
9
+ const linked = (message, anchor) => `${message}; see https://margoengine.com/errors#${anchor}`;
10
+ /** `margoError` whose message links to `anchor` (default the code) on the error help page. */
11
+ export const fault = (code, message, anchor = code) => margoError(code, linked(message, anchor));
12
+ /** TypeError whose message links to `anchor` on the error help page. */
13
+ export const typeError = (message, anchor) => new TypeError(linked(message, anchor));
14
+ /** RangeError whose message links to `anchor` on the error help page. */
15
+ export const rangeError = (message, anchor) => new RangeError(linked(message, anchor));
16
+
17
+ /** Reject replacement of a retained transform vector, including from non-module callers. */
18
+ export const vectorAssignment = () => {
19
+ throw Object.assign(typeError('Retained vector: use a transform setter', 'transform'), { code: 'transform' });
20
+ };
21
+
22
+ /** Translate only policy-blocked Wasm initialization, including Emscripten's wrapped abort. Keep other failures intact. */
23
+ export function wasmError(error) {
24
+ if (
25
+ !/^(Compile|Eval|Runtime)Error$/.test(error?.name) ||
26
+ !/wasm-unsafe-eval|content security policy|\bCSP\b|disallowed by embedder/i.test(error.message)
27
+ )
28
+ return error;
29
+ return Object.assign(
30
+ margoError(
31
+ 'failed',
32
+ "Wasm compilation blocked by CSP; add 'wasm-unsafe-eval' to script-src; see https://margoengine.com/llms/docs/csp.md",
33
+ ),
34
+ { cause: error },
35
+ );
36
+ }