@margoengine/loaders 0.0.0-stage → 0.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 (184) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1198 -3
  4. package/dist/3dm.d.ts +161 -0
  5. package/dist/3dm.js +289 -0
  6. package/dist/3ds.d.ts +124 -0
  7. package/dist/3ds.js +392 -0
  8. package/dist/acclaim.d.ts +90 -0
  9. package/dist/acclaim.js +312 -0
  10. package/dist/amf.d.ts +45 -0
  11. package/dist/amf.js +256 -0
  12. package/dist/babylon.d.ts +197 -0
  13. package/dist/babylon.js +701 -0
  14. package/dist/common.d.ts +7 -0
  15. package/dist/common.js +6 -0
  16. package/dist/dae-animation.d.ts +66 -0
  17. package/dist/dae-animation.js +266 -0
  18. package/dist/dae-common.d.ts +95 -0
  19. package/dist/dae-common.js +226 -0
  20. package/dist/dae-controllers.d.ts +57 -0
  21. package/dist/dae-controllers.js +164 -0
  22. package/dist/dae-pose.d.ts +131 -0
  23. package/dist/dae-pose.js +381 -0
  24. package/dist/dae-scene.d.ts +203 -0
  25. package/dist/dae-scene.js +279 -0
  26. package/dist/dae.d.ts +161 -0
  27. package/dist/dae.js +633 -0
  28. package/dist/detect.d.ts +20 -0
  29. package/dist/detect.js +72 -0
  30. package/dist/dxf.d.ts +81 -0
  31. package/dist/dxf.js +774 -0
  32. package/dist/engine.d.ts +36 -0
  33. package/dist/engine.js +117 -0
  34. package/dist/font-opentype.d.ts +14 -0
  35. package/dist/font-opentype.js +33 -0
  36. package/dist/font-woff.d.ts +23 -0
  37. package/dist/font-woff.js +293 -0
  38. package/dist/font.d.ts +41 -0
  39. package/dist/font.js +26 -0
  40. package/dist/gcode.d.ts +55 -0
  41. package/dist/gcode.js +278 -0
  42. package/dist/geo-model.d.ts +84 -0
  43. package/dist/geo-model.js +220 -0
  44. package/dist/geo-mvt.d.ts +15 -0
  45. package/dist/geo-mvt.js +302 -0
  46. package/dist/geo-shp.d.ts +15 -0
  47. package/dist/geo-shp.js +234 -0
  48. package/dist/geo-xml.d.ts +6 -0
  49. package/dist/geo-xml.js +165 -0
  50. package/dist/geo.d.ts +8 -0
  51. package/dist/geo.js +59 -0
  52. package/dist/geometry.d.ts +60 -0
  53. package/dist/geometry.js +273 -0
  54. package/dist/index.d.ts +54 -0
  55. package/dist/index.js +28 -0
  56. package/dist/kmz.d.ts +78 -0
  57. package/dist/kmz.js +173 -0
  58. package/dist/las.d.ts +92 -0
  59. package/dist/las.js +110 -0
  60. package/dist/ldraw.d.ts +118 -0
  61. package/dist/ldraw.js +474 -0
  62. package/dist/load.d.ts +72 -0
  63. package/dist/load.js +205 -0
  64. package/dist/lottie.d.ts +154 -0
  65. package/dist/lottie.js +893 -0
  66. package/dist/lwo.d.ts +81 -0
  67. package/dist/lwo.js +454 -0
  68. package/dist/lzf.d.ts +13 -0
  69. package/dist/lzf.js +56 -0
  70. package/dist/materialx.d.ts +120 -0
  71. package/dist/materialx.js +563 -0
  72. package/dist/md2.d.ts +52 -0
  73. package/dist/md2.js +112 -0
  74. package/dist/mmcif.d.ts +14 -0
  75. package/dist/mmcif.js +246 -0
  76. package/dist/mmd.d.ts +244 -0
  77. package/dist/mmd.js +958 -0
  78. package/dist/mocap.d.ts +71 -0
  79. package/dist/mocap.js +178 -0
  80. package/dist/molecule-common.d.ts +4 -0
  81. package/dist/molecule-common.js +54 -0
  82. package/dist/mtl.d.ts +120 -0
  83. package/dist/mtl.js +469 -0
  84. package/dist/normals.d.ts +7 -0
  85. package/dist/normals.js +31 -0
  86. package/dist/nrrd.d.ts +63 -0
  87. package/dist/nrrd.js +743 -0
  88. package/dist/nurbs.d.ts +118 -0
  89. package/dist/nurbs.js +265 -0
  90. package/dist/obj.d.ts +143 -0
  91. package/dist/obj.js +456 -0
  92. package/dist/off.d.ts +10 -0
  93. package/dist/off.js +155 -0
  94. package/dist/outline-font.d.ts +87 -0
  95. package/dist/outline-font.js +110 -0
  96. package/dist/outline.d.ts +20 -0
  97. package/dist/outline.js +215 -0
  98. package/dist/pcd.d.ts +34 -0
  99. package/dist/pcd.js +258 -0
  100. package/dist/pdb.d.ts +67 -0
  101. package/dist/pdb.js +126 -0
  102. package/dist/ply.d.ts +35 -0
  103. package/dist/ply.js +392 -0
  104. package/dist/polygon.d.ts +9 -0
  105. package/dist/polygon.js +49 -0
  106. package/dist/prwm.d.ts +24 -0
  107. package/dist/prwm.js +114 -0
  108. package/dist/render.d.ts +27 -0
  109. package/dist/render.js +153 -0
  110. package/dist/sfnt-cff.d.ts +37 -0
  111. package/dist/sfnt-cff.js +749 -0
  112. package/dist/sfnt-layout.d.ts +24 -0
  113. package/dist/sfnt-layout.js +266 -0
  114. package/dist/sfnt-reader.d.ts +28 -0
  115. package/dist/sfnt-reader.js +87 -0
  116. package/dist/sfnt-variation.d.ts +40 -0
  117. package/dist/sfnt-variation.js +318 -0
  118. package/dist/shared/asset-url.js +82 -0
  119. package/dist/shared/crc32.d.ts +2 -0
  120. package/dist/shared/crc32.js +47 -0
  121. package/dist/shared/errors.js +36 -0
  122. package/dist/shared/inflate.js +284 -0
  123. package/dist/shared/normals.js +26 -0
  124. package/dist/shared/scene-scope.js +56 -0
  125. package/dist/shared/trailing-data.js +42 -0
  126. package/dist/stl.d.ts +64 -0
  127. package/dist/stl.js +309 -0
  128. package/dist/svg.d.ts +90 -0
  129. package/dist/svg.js +1007 -0
  130. package/dist/tds.d.ts +2 -0
  131. package/dist/tds.js +2 -0
  132. package/dist/textures.d.ts +42 -0
  133. package/dist/textures.js +53 -0
  134. package/dist/threemf-lattice.d.ts +139 -0
  135. package/dist/threemf-lattice.js +514 -0
  136. package/dist/threemf-materials.d.ts +75 -0
  137. package/dist/threemf-materials.js +314 -0
  138. package/dist/threemf-mesh.d.ts +60 -0
  139. package/dist/threemf-mesh.js +201 -0
  140. package/dist/threemf.d.ts +101 -0
  141. package/dist/threemf.js +415 -0
  142. package/dist/tilt.d.ts +62 -0
  143. package/dist/tilt.js +186 -0
  144. package/dist/tmx.d.ts +151 -0
  145. package/dist/tmx.js +394 -0
  146. package/dist/ttf.d.ts +78 -0
  147. package/dist/ttf.js +568 -0
  148. package/dist/types.d.ts +90 -0
  149. package/dist/types.js +35 -0
  150. package/dist/usd.d.ts +2 -0
  151. package/dist/usd.js +2 -0
  152. package/dist/usd.worker.d.ts +1 -0
  153. package/dist/usdz.d.ts +50 -0
  154. package/dist/usdz.js +646 -0
  155. package/dist/util.d.ts +75 -0
  156. package/dist/util.js +241 -0
  157. package/dist/vdb.d.ts +111 -0
  158. package/dist/vdb.js +663 -0
  159. package/dist/vrml.d.ts +53 -0
  160. package/dist/vrml.js +532 -0
  161. package/dist/vtk-cells.d.ts +29 -0
  162. package/dist/vtk-cells.js +211 -0
  163. package/dist/vtk-legacy.d.ts +57 -0
  164. package/dist/vtk-legacy.js +439 -0
  165. package/dist/vtk-reader.d.ts +19 -0
  166. package/dist/vtk-reader.js +134 -0
  167. package/dist/vtk-xml.d.ts +96 -0
  168. package/dist/vtk-xml.js +578 -0
  169. package/dist/vtk.d.ts +13 -0
  170. package/dist/vtk.js +234 -0
  171. package/dist/woff-glyf.d.ts +17 -0
  172. package/dist/woff-glyf.js +215 -0
  173. package/dist/worker.d.ts +10 -0
  174. package/dist/worker.js +93 -0
  175. package/dist/workers/usd.js +3 -0
  176. package/dist/xfile.d.ts +99 -0
  177. package/dist/xfile.js +706 -0
  178. package/dist/xml.d.ts +107 -0
  179. package/dist/xml.js +843 -0
  180. package/dist/xyz.d.ts +16 -0
  181. package/dist/xyz.js +110 -0
  182. package/dist/zip.d.ts +38 -0
  183. package/dist/zip.js +150 -0
  184. package/package.json +246 -6
package/dist/xyz.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { type LoadedGeometry, type ParseLimits } from './types.js';
2
+ import { type BinaryInput } from './util.js';
3
+ /** Column meaning for XYZ rows. `'auto'` decides from the column count of the first row. */
4
+ export type XYZLayout = 'auto' | 'xyz' | 'xyz-rgb' | 'xyz-normal' | 'xyz-normal-rgb' | 'xyz-intensity' | 'xyz-intensity-rgb';
5
+ /** Options of parseXYZ: the shared size limits and the column layout. */
6
+ export interface XYZOptions extends ParseLimits {
7
+ layout?: XYZLayout;
8
+ }
9
+ /**
10
+ * Parses an ASCII XYZ point cloud (one point per line, separated by spaces, tabs, commas or semicolons).
11
+ * Auto layout: 3 columns xyz, 4 xyz+intensity, 6 xyz+rgb (xyz+normal when any
12
+ * extra value is negative), 7 xyz+intensity+rgb (PTS), 9 xyz+normal+rgb; other counts keep xyz only.
13
+ * A leading count line (PTS, chemistry XYZ) and a leading element label per row are skipped. Colors given
14
+ * as 0..255 integers are scaled to [0,1].
15
+ */
16
+ export declare function parseXYZ(input: BinaryInput, options?: XYZOptions): LoadedGeometry;
package/dist/xyz.js ADDED
@@ -0,0 +1,110 @@
1
+ import { LoaderError } from './types.js';
2
+ import { floats, latin1, limits, normalizeColors, splitFields, TextScanner, toBytes, } from './util.js';
3
+ const F = 'XYZ';
4
+ const COLUMNS = {
5
+ 'xyz': 3,
6
+ 'xyz-rgb': 6,
7
+ 'xyz-normal': 6,
8
+ 'xyz-normal-rgb': 9,
9
+ 'xyz-intensity': 4,
10
+ 'xyz-intensity-rgb': 7,
11
+ };
12
+ const numeric = (token) => !Number.isNaN(Number(token)) || /^[+-]?nan$/i.test(token);
13
+ /**
14
+ * Parses an ASCII XYZ point cloud (one point per line, separated by spaces, tabs, commas or semicolons).
15
+ * Auto layout: 3 columns xyz, 4 xyz+intensity, 6 xyz+rgb (xyz+normal when any
16
+ * extra value is negative), 7 xyz+intensity+rgb (PTS), 9 xyz+normal+rgb; other counts keep xyz only.
17
+ * A leading count line (PTS, chemistry XYZ) and a leading element label per row are skipped. Colors given
18
+ * as 0..255 integers are scaled to [0,1].
19
+ */
20
+ export function parseXYZ(input, options = {}) {
21
+ const bytes = toBytes(input, F), limit = limits(options, F);
22
+ let layout = options.layout ?? 'auto';
23
+ if (layout !== 'auto' && !(layout in COLUMNS))
24
+ throw new LoaderError(F, 'range', `unknown layout "${String(layout)}"`);
25
+ const scanner = new TextScanner(latin1(bytes), F);
26
+ const positions = floats(), extra = floats();
27
+ let columns = -1, lineNumber = 0, first = true, labelled = false, count = 0, skipComment = false;
28
+ for (let line = scanner.line(); line !== null; line = scanner.line()) {
29
+ lineNumber++;
30
+ const trimmed = line.trim();
31
+ if (skipComment) {
32
+ skipComment = false;
33
+ if (!trimmed || !numeric(splitFields(trimmed)[0] ?? ''))
34
+ continue;
35
+ }
36
+ if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('//'))
37
+ continue;
38
+ let fields = splitFields(trimmed);
39
+ if (first) {
40
+ first = false;
41
+ if (fields.length === 1 && /^\d+$/.test(fields[0])) {
42
+ skipComment = true;
43
+ continue;
44
+ }
45
+ }
46
+ if (columns < 0 && fields.every((field) => !numeric(field)))
47
+ continue; // Column names, such as swisstopo's `X Y Z`.
48
+ if (columns < 0)
49
+ labelled = fields.length >= 4 && !numeric(fields[0]) && fields.slice(1, 4).every(numeric);
50
+ if (labelled)
51
+ fields = fields.slice(1);
52
+ if (columns < 0) {
53
+ columns = fields.length;
54
+ if (columns < 3)
55
+ throw new LoaderError(F, 'syntax', `line ${lineNumber} has ${columns} values, expected at least 3`);
56
+ if (layout !== 'auto' && columns < COLUMNS[layout])
57
+ throw new LoaderError(F, 'syntax', `layout ${layout} needs ${COLUMNS[layout]} values, line ${lineNumber} has ${columns}`);
58
+ }
59
+ else if (fields.length !== columns)
60
+ throw new LoaderError(F, 'syntax', `line ${lineNumber} has ${fields.length} values, expected ${columns}`);
61
+ if (++count > limit.maxVertices)
62
+ throw new LoaderError(F, 'limit', `point count exceeds the limit of ${limit.maxVertices}`);
63
+ for (let c = 0; c < columns && c < 9; c++) {
64
+ const value = Number(fields[c]);
65
+ if (Number.isNaN(value) && !/^[+-]?nan$/i.test(fields[c]))
66
+ throw new LoaderError(F, 'syntax', `invalid number "${fields[c].slice(0, 32)}" on line ${lineNumber}`);
67
+ if (c < 3)
68
+ positions.push(value);
69
+ else
70
+ extra.push(value);
71
+ }
72
+ }
73
+ if (count === 0)
74
+ throw new LoaderError(F, 'syntax', 'no points found');
75
+ const stride = Math.min(columns, 9) - 3, values = extra.array;
76
+ if (layout === 'auto') {
77
+ if (columns === 4)
78
+ layout = 'xyz-intensity';
79
+ else if (columns === 6) {
80
+ let negative = false;
81
+ for (let i = 0; i < extra.length && !negative; i++)
82
+ negative = values[i] < 0;
83
+ layout = negative ? 'xyz-normal' : 'xyz-rgb';
84
+ }
85
+ else if (columns === 7)
86
+ layout = 'xyz-intensity-rgb';
87
+ else if (columns >= 9)
88
+ layout = 'xyz-normal-rgb';
89
+ else
90
+ layout = 'xyz';
91
+ }
92
+ const result = { kind: 'points', positions: positions.result() };
93
+ const take = (offset, size) => {
94
+ const out = new Float32Array(count * size);
95
+ for (let i = 0; i < count; i++)
96
+ for (let a = 0; a < size; a++)
97
+ out[i * size + a] = values[i * stride + offset + a];
98
+ return out;
99
+ };
100
+ if (layout === 'xyz-normal' || layout === 'xyz-normal-rgb')
101
+ result.normals = take(0, 3);
102
+ if (layout === 'xyz-intensity' || layout === 'xyz-intensity-rgb')
103
+ result.attributes = { intensity: take(0, 1) };
104
+ const colorOffset = layout === 'xyz-rgb' ? 0 : layout === 'xyz-intensity-rgb' ? 1 : layout === 'xyz-normal-rgb' ? 3 : -1;
105
+ if (colorOffset >= 0) {
106
+ result.colors = take(colorOffset, 3);
107
+ normalizeColors(result.colors);
108
+ }
109
+ return result;
110
+ }
package/dist/zip.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { type BinaryInput } from './util.js';
2
+ import { crc32 } from './shared/crc32.js';
3
+ /** One entry of a zip central directory: its name, method, sizes, CRC and header offset. */
4
+ export interface ZipEntry {
5
+ /** Entry path as stored (forward slashes). */
6
+ name: string;
7
+ /** 0 stored, 8 deflate. */
8
+ method: number;
9
+ compressedSize: number;
10
+ size: number;
11
+ crc32: number;
12
+ /** Offset of the local file header. */
13
+ headerOffset: number;
14
+ }
15
+ /** Limits when reading a zip: entry count, entry size, expansion ratio and an AbortSignal. */
16
+ export interface ZipOptions {
17
+ /** Maximum entries in the central directory. Default 10,000. */
18
+ maxEntries?: number;
19
+ /** Maximum uncompressed size of one entry. Default 512 MiB. */
20
+ maxEntryBytes?: number;
21
+ /**
22
+ * Maximum expansion of a deflated entry over 1 MiB, uncompressed : compressed. Default 200 (model XML and USD
23
+ * layers compress about 3-20x; a zip bomb reaches deflate's 1032x).
24
+ */
25
+ maxRatio?: number;
26
+ signal?: AbortSignal;
27
+ }
28
+ /** CRC-32 (IEEE) of `bytes`, continuing from `crc` (the result of a previous call) when given. */
29
+ export { crc32 };
30
+ /** Lists the entries of a ZIP archive from its central directory. ZIP64, encryption and multi-disk archives are rejected. */
31
+ export declare function readZipDirectory(input: BinaryInput, options?: ZipOptions): ZipEntry[];
32
+ /**
33
+ * Inflates `data` to exactly `size` bytes, failing as soon as it would grow past them (zip-bomb safe). `subject` names
34
+ * the data in errors and `owner` is their format.
35
+ */
36
+ export declare function inflate(data: Uint8Array, size: number, subject: string, signal?: AbortSignal, format?: 'deflate-raw' | 'deflate' | 'gzip', owner?: string): Promise<Uint8Array>;
37
+ /** Reads and verifies (size and CRC-32) one entry. Supports stored (0) and deflate (8) entries. */
38
+ export declare function readZipEntry(input: BinaryInput, entry: ZipEntry, options?: ZipOptions): Promise<Uint8Array>;
package/dist/zip.js ADDED
@@ -0,0 +1,150 @@
1
+ import { LoaderError } from './types.js';
2
+ import { throwIfAborted, toBytes } from './util.js';
3
+ import { crc32 } from './shared/crc32.js';
4
+ import { hasTrailingData } from './shared/trailing-data.js';
5
+ const F = 'ZIP';
6
+ // Read against the PKWARE .ZIP File Format Specification (APPNOTE.TXT) 6.3.10 (PKWARE Inc., 2022),
7
+ // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT: section numbers below are that document's.
8
+ /** IBM Code Page 437 bytes 0x80 to 0xFF: a file name's encoding when general purpose bit 11 is clear (appendix D.1). */
9
+ const CP437 = 'ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ';
10
+ const decoder = new TextDecoder();
11
+ /** An entry name: UTF-8 when bit 11 (the language encoding flag, 4.4.4) is set, otherwise Code Page 437. */
12
+ function entryName(bytes, flags) {
13
+ if (flags & 0x800 || bytes.every((b) => b < 0x80))
14
+ return decoder.decode(bytes);
15
+ let name = '';
16
+ for (const b of bytes)
17
+ name += b < 0x80 ? String.fromCharCode(b) : CP437[b - 0x80];
18
+ return name;
19
+ }
20
+ /** CRC-32 (IEEE) of `bytes`, continuing from `crc` (the result of a previous call) when given. */
21
+ export { crc32 };
22
+ /** Lists the entries of a ZIP archive from its central directory. ZIP64, encryption and multi-disk archives are rejected. */
23
+ export function readZipDirectory(input, options = {}) {
24
+ const bytes = toBytes(input, F), maxEntries = options.maxEntries ?? 10_000;
25
+ throwIfAborted(options.signal, F);
26
+ if (Number.isNaN(maxEntries))
27
+ throw new LoaderError(F, 'range', 'maxEntries must not be NaN');
28
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
29
+ if (bytes.length < 22)
30
+ throw new LoaderError(F, 'truncated', 'file is too small to be a ZIP archive');
31
+ let eocd = -1;
32
+ for (let i = bytes.length - 22, stop = Math.max(0, bytes.length - 22 - 65535); i >= stop; i--) {
33
+ if (view.getUint32(i, true) === 0x06054b50 && i + 22 + view.getUint16(i + 20, true) <= bytes.length) {
34
+ eocd = i;
35
+ break;
36
+ }
37
+ }
38
+ if (eocd < 0)
39
+ throw new LoaderError(F, 'zip', 'end of central directory not found (not a ZIP archive or truncated)');
40
+ const disk = view.getUint16(eocd + 4, true), cdDisk = view.getUint16(eocd + 6, true);
41
+ const count = view.getUint16(eocd + 10, true), cdSize = view.getUint32(eocd + 12, true), cdOffset = view.getUint32(eocd + 16, true);
42
+ if (count === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff)
43
+ throw new LoaderError(F, 'unsupported', 'ZIP64 archives are not supported');
44
+ if (disk !== 0 || cdDisk !== 0)
45
+ throw new LoaderError(F, 'unsupported', 'multi-disk archives are not supported');
46
+ if (count > maxEntries)
47
+ throw new LoaderError(F, 'limit', `archive has ${count} entries, more than the limit of ${maxEntries}`);
48
+ if (cdOffset + cdSize > eocd)
49
+ throw new LoaderError(F, 'zip', 'central directory lies outside the archive');
50
+ const entries = [];
51
+ let p = cdOffset;
52
+ for (let i = 0; i < count; i++) {
53
+ if (p + 46 > cdOffset + cdSize || view.getUint32(p, true) !== 0x02014b50)
54
+ throw new LoaderError(F, 'zip', `central directory entry ${i} is corrupt`);
55
+ const flags = view.getUint16(p + 8, true), method = view.getUint16(p + 10, true);
56
+ const nameLength = view.getUint16(p + 28, true), extraLength = view.getUint16(p + 30, true), commentLength = view.getUint16(p + 32, true);
57
+ if (p + 46 + nameLength + extraLength + commentLength > cdOffset + cdSize)
58
+ throw new LoaderError(F, 'zip', `central directory entry ${i} name, extra field or comment overflows`);
59
+ const entry = {
60
+ name: entryName(bytes.subarray(p + 46, p + 46 + nameLength), flags),
61
+ method,
62
+ crc32: view.getUint32(p + 16, true),
63
+ compressedSize: view.getUint32(p + 20, true),
64
+ size: view.getUint32(p + 24, true),
65
+ headerOffset: view.getUint32(p + 42, true),
66
+ };
67
+ if (flags & 1)
68
+ throw new LoaderError(F, 'unsupported', `entry "${entry.name}" is encrypted`);
69
+ if (entry.compressedSize === 0xffffffff || entry.size === 0xffffffff || entry.headerOffset === 0xffffffff)
70
+ throw new LoaderError(F, 'unsupported', 'ZIP64 entries are not supported');
71
+ entries.push(entry);
72
+ p += 46 + nameLength + extraLength + commentLength;
73
+ }
74
+ return entries;
75
+ }
76
+ /**
77
+ * Inflates `data` to exactly `size` bytes, failing as soon as it would grow past them (zip-bomb safe). `subject` names
78
+ * the data in errors and `owner` is their format.
79
+ */
80
+ export async function inflate(data, size, subject, signal, format = 'deflate-raw', owner = F) {
81
+ if (typeof DecompressionStream !== 'function')
82
+ throw new LoaderError(owner, 'unsupported', 'DecompressionStream is not available in this runtime');
83
+ const stream = new ReadableStream({
84
+ start(controller) {
85
+ controller.enqueue(data);
86
+ controller.close();
87
+ },
88
+ }).pipeThrough(new DecompressionStream(format));
89
+ const reader = stream.getReader(), output = new Uint8Array(size);
90
+ let length = 0;
91
+ try {
92
+ for (;;) {
93
+ if (signal?.aborted)
94
+ throw new LoaderError(owner, 'aborted', 'aborted');
95
+ const { done, value } = await reader.read();
96
+ if (done)
97
+ break;
98
+ if (length + value.length > size)
99
+ throw new LoaderError(owner, 'zip', `${subject} inflates past its declared size of ${size} bytes`);
100
+ output.set(value, length);
101
+ length += value.length;
102
+ }
103
+ }
104
+ catch (error) {
105
+ void reader.cancel().catch(() => { });
106
+ if (error instanceof LoaderError)
107
+ throw error;
108
+ throw new LoaderError(owner, 'zip', `${subject} has corrupt deflate data (${error?.message ?? error})`);
109
+ }
110
+ if (length !== size)
111
+ throw new LoaderError(owner, 'zip', `${subject} inflated to ${length} bytes, expected ${size}`);
112
+ if (await hasTrailingData(data, format))
113
+ throw new LoaderError(owner, 'zip', `${subject} has corrupt deflate data (data after the end of the stream)`);
114
+ return output;
115
+ }
116
+ /** Reads and verifies (size and CRC-32) one entry. Supports stored (0) and deflate (8) entries. */
117
+ export async function readZipEntry(input, entry, options = {}) {
118
+ const bytes = toBytes(input, F), maxBytes = options.maxEntryBytes ?? 512 * 1024 * 1024;
119
+ throwIfAborted(options.signal, F);
120
+ if (Number.isNaN(maxBytes))
121
+ throw new LoaderError(F, 'range', 'maxEntryBytes must not be NaN');
122
+ if (entry.size > maxBytes)
123
+ throw new LoaderError(F, 'limit', `entry "${entry.name}" is ${entry.size} bytes, more than the limit of ${maxBytes}`);
124
+ // Checked before the output is allocated: a declared size the compressed bytes cannot plausibly produce is a bomb.
125
+ const maxRatio = options.maxRatio ?? 200;
126
+ if (!(maxRatio >= 1))
127
+ throw new LoaderError(F, 'range', 'maxRatio must be at least 1');
128
+ if (entry.method === 8 && entry.size > 1 << 20 && entry.size > entry.compressedSize * maxRatio)
129
+ throw new LoaderError(F, 'limit', `entry "${entry.name}" expands ${entry.compressedSize} bytes to ${entry.size}, more than ${maxRatio}x (raise maxRatio)`);
130
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), p = entry.headerOffset;
131
+ if (p + 30 > bytes.length || view.getUint32(p, true) !== 0x04034b50)
132
+ throw new LoaderError(F, 'zip', `local header of "${entry.name}" is missing`);
133
+ const start = p + 30 + view.getUint16(p + 26, true) + view.getUint16(p + 28, true), end = start + entry.compressedSize;
134
+ if (end > bytes.length)
135
+ throw new LoaderError(F, 'truncated', `data of "${entry.name}" runs past the end of the archive`);
136
+ const data = bytes.subarray(start, end);
137
+ let output;
138
+ if (entry.method === 0) {
139
+ if (entry.compressedSize !== entry.size)
140
+ throw new LoaderError(F, 'zip', `stored entry "${entry.name}" has mismatched sizes`);
141
+ output = data;
142
+ }
143
+ else if (entry.method === 8)
144
+ output = await inflate(data, entry.size, `entry "${entry.name}"`, options.signal);
145
+ else
146
+ throw new LoaderError(F, 'unsupported', `entry "${entry.name}" uses compression method ${entry.method}; only stored and deflate are supported`);
147
+ if (crc32(output) !== entry.crc32)
148
+ throw new LoaderError(F, 'zip', `entry "${entry.name}" fails its CRC-32 check`);
149
+ return output;
150
+ }
package/package.json CHANGED
@@ -1,6 +1,246 @@
1
- {
2
- "name": "@margoengine/loaders",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@margoengine/loaders",
3
+ "version": "0.1.0",
4
+ "description": "PLY, STL, 3MF, PCD, OFF, XYZ, OBJ/MTL, COLLADA, Tiled TMX, VTK, NRRD, LDraw, Rhino 3DM, 3DS, LWO, AMF, MD2, PDB, KMZ, NURBS, LAS, OpenVDB, MaterialX, Lottie, static USDA/USDZ, SVG and TrueType font loaders for Margo Engine.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "webgpu",
8
+ "3d",
9
+ "loaders"
10
+ ],
11
+ "homepage": "https://margoengine.com",
12
+ "bugs": {
13
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
18
+ "directory": "packages/loaders"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "author": "Scott Campbell",
22
+ "type": "module",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "scripts": {
27
+ "build": "node scripts/build.js",
28
+ "test": "npm run build && node --experimental-test-module-mocks --test tests/*.test.js ../../examples/package-benchmarks/proofs/package-guard.mjs",
29
+ "test:browser": "npm run build && node scripts/test-browser.js",
30
+ "test:gpu": "cd ../.. && node packages/loaders/tests/split-scale-check.mjs && node packages/loaders/tests/materialx-reference-check.mjs && node packages/loaders/tests/mtl-gpu-check.mjs && node packages/loaders/tests/threemf-layers-gpu-check.mjs",
31
+ "test:render:browser": "npm run build && node scripts/test-render-browser.mjs",
32
+ "test:real-files": "npm run build && node scripts/test-real-files.mjs",
33
+ "test:real-files:runtimes": "node scripts/fetch-real-files-runtimes.mjs",
34
+ "fixtures": "node scripts/fetch-fixtures.js",
35
+ "prepack": "npm run build",
36
+ "test:gpu:split-scale": "npm run build && node tests/split-scale-check.mjs",
37
+ "test:gpu:materialx-reference": "npm run build && node tests/materialx-reference-check.mjs",
38
+ "test:gpu:mtl": "npm run build && node tests/mtl-gpu-check.mjs",
39
+ "test:gpu:threemf-layers": "npm run build && node tests/threemf-layers-gpu-check.mjs"
40
+ },
41
+ "sideEffects": false,
42
+ "exports": {
43
+ "./ply": {
44
+ "types": "./dist/ply.d.ts",
45
+ "import": "./dist/ply.js"
46
+ },
47
+ "./off": {
48
+ "types": "./dist/off.d.ts",
49
+ "import": "./dist/off.js"
50
+ },
51
+ ".": {
52
+ "types": "./dist/index.d.ts",
53
+ "import": "./dist/index.js"
54
+ },
55
+ "./worker": {
56
+ "types": "./dist/worker.d.ts",
57
+ "import": "./dist/worker.js"
58
+ },
59
+ "./common": {
60
+ "types": "./dist/common.d.ts",
61
+ "import": "./dist/common.js"
62
+ },
63
+ "./usd": {
64
+ "types": "./dist/usd.d.ts",
65
+ "import": "./dist/usd.js"
66
+ },
67
+ "./font": {
68
+ "types": "./dist/font.d.ts",
69
+ "import": "./dist/font.js"
70
+ },
71
+ "./font-opentype": {
72
+ "types": "./dist/font-opentype.d.ts",
73
+ "import": "./dist/font-opentype.js"
74
+ },
75
+ "./font-woff": {
76
+ "types": "./dist/font-woff.d.ts",
77
+ "import": "./dist/font-woff.js"
78
+ },
79
+ "./svg": {
80
+ "types": "./dist/svg.d.ts",
81
+ "import": "./dist/svg.js"
82
+ },
83
+ "./mtl": {
84
+ "types": "./dist/mtl.d.ts",
85
+ "import": "./dist/mtl.js"
86
+ },
87
+ "./render": {
88
+ "types": "./dist/render.d.ts",
89
+ "import": "./dist/render.js"
90
+ },
91
+ "./mocap": {
92
+ "types": "./dist/mocap.d.ts",
93
+ "import": "./dist/mocap.js"
94
+ },
95
+ "./vrml": {
96
+ "types": "./dist/vrml.d.ts",
97
+ "import": "./dist/vrml.js"
98
+ },
99
+ "./mmd": {
100
+ "types": "./dist/mmd.d.ts",
101
+ "import": "./dist/mmd.js"
102
+ },
103
+ "./xfile": {
104
+ "types": "./dist/xfile.d.ts",
105
+ "import": "./dist/xfile.js"
106
+ },
107
+ "./babylon": {
108
+ "types": "./dist/babylon.d.ts",
109
+ "import": "./dist/babylon.js"
110
+ },
111
+ "./tilt": {
112
+ "types": "./dist/tilt.d.ts",
113
+ "import": "./dist/tilt.js"
114
+ },
115
+ "./prwm": {
116
+ "types": "./dist/prwm.d.ts",
117
+ "import": "./dist/prwm.js"
118
+ },
119
+ "./gcode": {
120
+ "types": "./dist/gcode.d.ts",
121
+ "import": "./dist/gcode.js"
122
+ },
123
+ "./textures": {
124
+ "types": "./dist/textures.d.ts",
125
+ "import": "./dist/textures.js"
126
+ },
127
+ "./nrrd": {
128
+ "types": "./dist/nrrd.d.ts",
129
+ "import": "./dist/nrrd.js"
130
+ },
131
+ "./ldraw": {
132
+ "types": "./dist/ldraw.d.ts",
133
+ "import": "./dist/ldraw.js"
134
+ },
135
+ "./threemf-materials": {
136
+ "types": "./dist/threemf-materials.d.ts",
137
+ "import": "./dist/threemf-materials.js"
138
+ },
139
+ "./threemf-mesh": {
140
+ "types": "./dist/threemf-mesh.d.ts",
141
+ "import": "./dist/threemf-mesh.js"
142
+ },
143
+ "./vtk-xml": {
144
+ "types": "./dist/vtk-xml.d.ts",
145
+ "import": "./dist/vtk-xml.js"
146
+ },
147
+ "./3dm": {
148
+ "types": "./dist/3dm.d.ts",
149
+ "import": "./dist/3dm.js"
150
+ },
151
+ "./3ds": {
152
+ "types": "./dist/3ds.d.ts",
153
+ "import": "./dist/3ds.js"
154
+ },
155
+ "./lwo": {
156
+ "types": "./dist/lwo.d.ts",
157
+ "import": "./dist/lwo.js"
158
+ },
159
+ "./amf": {
160
+ "types": "./dist/amf.d.ts",
161
+ "import": "./dist/amf.js"
162
+ },
163
+ "./md2": {
164
+ "types": "./dist/md2.d.ts",
165
+ "import": "./dist/md2.js"
166
+ },
167
+ "./pdb": {
168
+ "types": "./dist/pdb.d.ts",
169
+ "import": "./dist/pdb.js"
170
+ },
171
+ "./tds": {
172
+ "types": "./dist/tds.d.ts",
173
+ "import": "./dist/tds.js"
174
+ },
175
+ "./kmz": {
176
+ "types": "./dist/kmz.d.ts",
177
+ "import": "./dist/kmz.js"
178
+ },
179
+ "./nurbs": {
180
+ "types": "./dist/nurbs.d.ts",
181
+ "import": "./dist/nurbs.js"
182
+ },
183
+ "./las": {
184
+ "types": "./dist/las.d.ts",
185
+ "import": "./dist/las.js"
186
+ },
187
+ "./vdb": {
188
+ "types": "./dist/vdb.d.ts",
189
+ "import": "./dist/vdb.js"
190
+ },
191
+ "./materialx": {
192
+ "types": "./dist/materialx.d.ts",
193
+ "import": "./dist/materialx.js"
194
+ },
195
+ "./lottie": {
196
+ "types": "./dist/lottie.d.ts",
197
+ "import": "./dist/lottie.js"
198
+ },
199
+ "./xml": {
200
+ "types": "./dist/xml.d.ts",
201
+ "import": "./dist/xml.js"
202
+ },
203
+ "./vtk-legacy": {
204
+ "types": "./dist/vtk-legacy.d.ts",
205
+ "import": "./dist/vtk-legacy.js"
206
+ },
207
+ "./geo": {
208
+ "types": "./dist/geo.d.ts",
209
+ "import": "./dist/geo.js"
210
+ },
211
+ "./threemf-lattice": {
212
+ "types": "./dist/threemf-lattice.d.ts",
213
+ "import": "./dist/threemf-lattice.js"
214
+ },
215
+ "./mmcif": {
216
+ "types": "./dist/mmcif.d.ts",
217
+ "import": "./dist/mmcif.js"
218
+ }
219
+ },
220
+ "files": [
221
+ "dist",
222
+ "NOTICE"
223
+ ],
224
+ "peerDependencies": {
225
+ "@margoengine/core": "^0.1.0",
226
+ "@margoengine/decoders": "^0.1.0",
227
+ "@margoengine/gltf": "^0.1.0",
228
+ "@margoengine/materials": "^0.1.0"
229
+ },
230
+ "peerDependenciesMeta": {
231
+ "@margoengine/decoders": {
232
+ "optional": true
233
+ },
234
+ "@margoengine/gltf": {
235
+ "optional": true
236
+ },
237
+ "@margoengine/materials": {
238
+ "optional": true
239
+ }
240
+ },
241
+ "dependencies": {
242
+ "@margoengine/geometry": "^0.1.0",
243
+ "@margoengine/math": "^0.1.0",
244
+ "@margoengine/points": "^0.1.0"
245
+ }
246
+ }