@margoengine/loaders 0.0.0-stage → 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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1665 -2
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +166 -0
- package/dist/dae.js +644 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +122 -0
- package/dist/dxf.js +985 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.js +120 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +92 -0
- package/dist/gcode.js +369 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +125 -0
- package/dist/mtl.js +550 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +70 -0
- package/dist/nrrd.js +785 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +145 -0
- package/dist/obj.js +462 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +32 -0
- package/dist/render.js +181 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +45 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +379 -0
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +162 -0
- package/dist/threemf-materials.js +497 -0
- package/dist/threemf-mesh.d.ts +75 -0
- package/dist/threemf-mesh.js +422 -0
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +170 -0
- package/dist/threemf.js +609 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +289 -4
package/dist/load.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { LoaderError, httpRefusal } from './types.js';
|
|
2
|
+
const F = 'load';
|
|
3
|
+
/** The default response size limit of fetchBytes: 256 MiB. */
|
|
4
|
+
export const DEFAULT_MAX_BYTES = 256 * 1024 * 1024;
|
|
5
|
+
const short = (url) => String(url).slice(0, 200);
|
|
6
|
+
/** An IPv4 dotted quad as a number, or undefined. */
|
|
7
|
+
function ipv4(host) {
|
|
8
|
+
const parts = host.split('.');
|
|
9
|
+
if (parts.length !== 4 || parts.some((p) => !/^\d{1,3}$/.test(p) || +p > 255))
|
|
10
|
+
return undefined;
|
|
11
|
+
return parts.reduce((n, p) => n * 256 + +p, 0);
|
|
12
|
+
}
|
|
13
|
+
// Base (as a number) and prefix length of each IPv4 range publicOnly refuses.
|
|
14
|
+
const PRIVATE4 = [
|
|
15
|
+
['0.0.0.0', 8],
|
|
16
|
+
['10.0.0.0', 8],
|
|
17
|
+
['100.64.0.0', 10],
|
|
18
|
+
['127.0.0.0', 8],
|
|
19
|
+
['169.254.0.0', 16],
|
|
20
|
+
['172.16.0.0', 12],
|
|
21
|
+
['192.168.0.0', 16],
|
|
22
|
+
].map(([base, bits]) => [ipv4(base), bits]);
|
|
23
|
+
const private4 = (n) => PRIVATE4.some(([base, bits]) => n >= base && n < base + 2 ** (32 - bits));
|
|
24
|
+
/**
|
|
25
|
+
* True for a host that names this computer or a private network as written (FetchOptions.publicOnly). WHATWG URL
|
|
26
|
+
* parsing has already turned short and hexadecimal IPv4 forms into dotted quads, and IPv6 into its bracketed,
|
|
27
|
+
* compressed, lower-case text.
|
|
28
|
+
*/
|
|
29
|
+
export function isPrivateHost(hostname) {
|
|
30
|
+
const host = hostname.toLowerCase().replace(/\.$/, '');
|
|
31
|
+
if (host === 'localhost' || host.endsWith('.localhost'))
|
|
32
|
+
return true;
|
|
33
|
+
const v4 = ipv4(host);
|
|
34
|
+
if (v4 !== undefined)
|
|
35
|
+
return private4(v4);
|
|
36
|
+
if (!host.startsWith('['))
|
|
37
|
+
return false;
|
|
38
|
+
const v6 = host.slice(1, -1);
|
|
39
|
+
if (v6 === '::1' || v6 === '::')
|
|
40
|
+
return true;
|
|
41
|
+
const mapped = /^::ffff:(.+)$/.exec(v6);
|
|
42
|
+
if (mapped) {
|
|
43
|
+
const hex = /^([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(mapped[1]), n = ipv4(mapped[1]) ?? (hex ? parseInt(hex[1], 16) * 65536 + parseInt(hex[2], 16) : undefined);
|
|
44
|
+
return n !== undefined && private4(n);
|
|
45
|
+
}
|
|
46
|
+
const first = parseInt(v6.split(':')[0] || '0', 16);
|
|
47
|
+
return (first & 0xfe00) === 0xfc00 || (first & 0xffc0) === 0xfe80;
|
|
48
|
+
}
|
|
49
|
+
/** Throws when `url` fails the guards of `options`. */
|
|
50
|
+
function guard(url, options, hop) {
|
|
51
|
+
const where = hop ? `a redirect to ${short(url.href)}` : short(url.href);
|
|
52
|
+
if (options.protocols && !options.protocols.includes(url.protocol))
|
|
53
|
+
throw new LoaderError(F, 'url', `${where} uses ${url.protocol}; allowed: ${options.protocols.join(', ')}`);
|
|
54
|
+
if (options.publicOnly) {
|
|
55
|
+
if (url.username || url.password)
|
|
56
|
+
throw new LoaderError(F, 'url', `${where} carries a user name or password`);
|
|
57
|
+
if (isPrivateHost(url.hostname))
|
|
58
|
+
throw new LoaderError(F, 'url', `${where} is this computer or a private network; public hosts only`);
|
|
59
|
+
}
|
|
60
|
+
if (options.allowHost && !options.allowHost(url.hostname, url))
|
|
61
|
+
throw new LoaderError(F, 'url', `${where}: host ${url.hostname} refused by allowHost`);
|
|
62
|
+
}
|
|
63
|
+
/** The `Content-Type` essence (WHATWG MIME Sniffing): type/subtype, lower case, without parameters. */
|
|
64
|
+
const essence = (type) => type.split(';')[0].trim().toLowerCase();
|
|
65
|
+
const accepted = (type, accept) => accept.some((a) => {
|
|
66
|
+
const want = a.trim().toLowerCase();
|
|
67
|
+
return want.endsWith('/*') ? essence(type).startsWith(want.slice(0, -1)) : essence(type) === want;
|
|
68
|
+
});
|
|
69
|
+
/** Fetches `url` into memory, enforcing `maxBytes` on both Content-Length and the streamed body. */
|
|
70
|
+
export async function fetchBytes(url, options = {}) {
|
|
71
|
+
return (await fetchResource(url, options)).bytes;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Fetches `url` like fetchBytes and also returns the final URL, media type, status and redirects followed. With
|
|
75
|
+
* `protocols`, `publicOnly`, `allowHost` or `maxRedirects` it follows redirects by hand and checks every hop, for links
|
|
76
|
+
* from untrusted sources; `accept` refuses other media types before the body is read. An error the custom `fetch`
|
|
77
|
+
* throws is kept as the LoaderError's `cause`.
|
|
78
|
+
*/
|
|
79
|
+
export async function fetchResource(url, options = {}) {
|
|
80
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES, maxRedirects = options.maxRedirects ?? 20;
|
|
81
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0)
|
|
82
|
+
throw new LoaderError(F, 'range', 'maxBytes must be a non-negative integer');
|
|
83
|
+
if (!Number.isSafeInteger(maxRedirects) || maxRedirects < 0)
|
|
84
|
+
throw new LoaderError(F, 'range', 'maxRedirects must be a non-negative integer');
|
|
85
|
+
const guarded = !!(options.protocols ||
|
|
86
|
+
options.publicOnly ||
|
|
87
|
+
options.allowHost ||
|
|
88
|
+
options.maxRedirects !== undefined), doFetch = options.fetch ?? globalThis.fetch, signal = options.signal;
|
|
89
|
+
if (typeof doFetch !== 'function')
|
|
90
|
+
throw new LoaderError(F, 'unsupported', 'fetch is not available');
|
|
91
|
+
if (signal?.aborted)
|
|
92
|
+
throw new LoaderError(F, 'aborted', `aborted before fetching ${short(url)}`);
|
|
93
|
+
let target = url, redirects = 0, response;
|
|
94
|
+
for (;;) {
|
|
95
|
+
if (guarded) {
|
|
96
|
+
try {
|
|
97
|
+
target = new URL(target);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
throw new LoaderError(F, 'url', `${short(target)} is not an absolute URL`, { cause: error });
|
|
101
|
+
}
|
|
102
|
+
guard(target, options, redirects);
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
response = await doFetch(target, {
|
|
106
|
+
...options.init,
|
|
107
|
+
...(guarded ? { redirect: 'manual' } : {}),
|
|
108
|
+
signal,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (signal?.aborted)
|
|
113
|
+
throw new LoaderError(F, 'aborted', `aborted while fetching ${short(target)}`, { cause: error });
|
|
114
|
+
throw new LoaderError(F, 'fetch', `request for ${short(target)} failed (${error?.message ?? error})`, {
|
|
115
|
+
cause: error,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (!guarded)
|
|
119
|
+
break;
|
|
120
|
+
if (response.type === 'opaqueredirect') {
|
|
121
|
+
void response.body?.cancel().catch(() => { });
|
|
122
|
+
throw new LoaderError(F, 'unsupported', `${short(target)} redirects, and this fetch hides redirects from checks`);
|
|
123
|
+
}
|
|
124
|
+
if (response.status < 300 || response.status > 399 || response.status === 304)
|
|
125
|
+
break;
|
|
126
|
+
const location = response.headers.get('location');
|
|
127
|
+
void response.body?.cancel().catch(() => { });
|
|
128
|
+
if (!location)
|
|
129
|
+
throw new LoaderError(F, 'fetch', `${short(target)} answered HTTP ${response.status} with no Location`, {
|
|
130
|
+
status: response.status,
|
|
131
|
+
});
|
|
132
|
+
if (redirects >= maxRedirects)
|
|
133
|
+
throw new LoaderError(F, 'limit', `more than ${maxRedirects} redirects from ${short(url)}`);
|
|
134
|
+
target = new URL(location, target);
|
|
135
|
+
redirects++;
|
|
136
|
+
}
|
|
137
|
+
const where = response.url || String(target), type = response.headers.get('content-type') ?? '';
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
void response.body?.cancel().catch(() => { });
|
|
140
|
+
throw new LoaderError(F, 'fetch', `request for ${short(where)} returned HTTP ${response.status}`, httpRefusal(response));
|
|
141
|
+
}
|
|
142
|
+
if (options.accept && !accepted(type, options.accept)) {
|
|
143
|
+
void response.body?.cancel().catch(() => { });
|
|
144
|
+
throw new LoaderError(F, 'unsupported', `${short(where)} is ${essence(type) || 'of no stated type'}; accepted: ${options.accept.join(', ')}`);
|
|
145
|
+
}
|
|
146
|
+
const bytes = await readBody(response, maxBytes, signal, where);
|
|
147
|
+
return { bytes, url: where, type, status: response.status, redirects };
|
|
148
|
+
}
|
|
149
|
+
/** The body of `response`, at most `maxBytes` by its Content-Length and as it streams. */
|
|
150
|
+
async function readBody(response, maxBytes, signal, url) {
|
|
151
|
+
const declared = Number(response.headers.get('content-length'));
|
|
152
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
153
|
+
const error = new LoaderError(F, 'limit', `response is ${declared} bytes, more than the limit of ${maxBytes}`);
|
|
154
|
+
void response.body?.cancel(error).catch(() => { });
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
if (!response.body) {
|
|
158
|
+
const buffer = new Uint8Array(await response.arrayBuffer());
|
|
159
|
+
if (buffer.length > maxBytes)
|
|
160
|
+
throw new LoaderError(F, 'limit', `response is ${buffer.length} bytes, more than the limit of ${maxBytes}`);
|
|
161
|
+
return buffer;
|
|
162
|
+
}
|
|
163
|
+
const reader = response.body.getReader(), chunks = [];
|
|
164
|
+
let length = 0;
|
|
165
|
+
try {
|
|
166
|
+
for (;;) {
|
|
167
|
+
const { done, value } = await reader.read();
|
|
168
|
+
if (done)
|
|
169
|
+
break;
|
|
170
|
+
length += value.length;
|
|
171
|
+
if (length > maxBytes)
|
|
172
|
+
throw new LoaderError(F, 'limit', `response exceeds the limit of ${maxBytes} bytes`);
|
|
173
|
+
chunks.push(value);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
void reader.cancel(error).catch(() => { });
|
|
178
|
+
if (error instanceof LoaderError)
|
|
179
|
+
throw error;
|
|
180
|
+
if (signal?.aborted)
|
|
181
|
+
throw new LoaderError(F, 'aborted', `aborted while reading ${short(url)}`, { cause: error });
|
|
182
|
+
throw new LoaderError(F, 'fetch', `reading ${short(url)} failed (${error?.message ?? error})`, {
|
|
183
|
+
cause: error,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
if (chunks.length === 1)
|
|
187
|
+
return chunks[0];
|
|
188
|
+
const out = new Uint8Array(length);
|
|
189
|
+
let offset = 0;
|
|
190
|
+
for (const chunk of chunks) {
|
|
191
|
+
out.set(chunk, offset);
|
|
192
|
+
offset += chunk.length;
|
|
193
|
+
}
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Fetches `url` and parses it with `parse`, for example `load('/scan.ply', parsePLY, {signal, maxVertices: 1e6})`.
|
|
198
|
+
* The same options object goes to the parser, so limits and `signal` apply to both steps.
|
|
199
|
+
*/
|
|
200
|
+
export async function load(url, parse, options) {
|
|
201
|
+
const bytes = await fetchBytes(url, options);
|
|
202
|
+
if (options?.signal?.aborted)
|
|
203
|
+
throw new LoaderError(F, 'aborted', 'aborted');
|
|
204
|
+
return parse(bytes, options);
|
|
205
|
+
}
|
package/dist/lottie.d.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { Shape } from '@margoengine/geometry/shapes';
|
|
2
|
+
import { type OutlineCommand, type OutlineFillRule } from './outline.js';
|
|
3
|
+
import { type BinaryInput } from './util.js';
|
|
4
|
+
export type { OutlineCommand, OutlineFillRule } from './outline.js';
|
|
5
|
+
/** Options of {@link parseLottie}. */
|
|
6
|
+
export interface LottieOptions {
|
|
7
|
+
/** Maximum layers, counted in every composition. Default 100,000. */
|
|
8
|
+
maxLayers?: number;
|
|
9
|
+
}
|
|
10
|
+
/** A gradient paint: positions in composition units. */
|
|
11
|
+
export interface LottieGradient {
|
|
12
|
+
kind: 'linear' | 'radial';
|
|
13
|
+
start: [number, number];
|
|
14
|
+
end: [number, number];
|
|
15
|
+
/** `[offset, r, g, b, a]`, every number 0..1, by rising offset. */
|
|
16
|
+
stops: [number, number, number, number, number][];
|
|
17
|
+
}
|
|
18
|
+
/** One filled or stroked path of a frame. */
|
|
19
|
+
export interface LottiePath {
|
|
20
|
+
kind: 'path';
|
|
21
|
+
/** Outline in composition units, y down, every transform applied. */
|
|
22
|
+
commands: OutlineCommand[];
|
|
23
|
+
/** Solid fill, sRGB 0..1, or null. */
|
|
24
|
+
fill: [number, number, number] | null;
|
|
25
|
+
/** Gradient fill or stroke paint, or null. */
|
|
26
|
+
gradient: LottieGradient | null;
|
|
27
|
+
/** Solid stroke, sRGB 0..1, or null. */
|
|
28
|
+
stroke: [number, number, number] | null;
|
|
29
|
+
/** The paint's opacity times every layer and group above it, 0..1. */
|
|
30
|
+
opacity: number;
|
|
31
|
+
fillRule: OutlineFillRule;
|
|
32
|
+
/** Stroke width in composition units, with the average scale of the transforms applied. 0 for a fill. */
|
|
33
|
+
strokeWidth: number;
|
|
34
|
+
lineCap: 'butt' | 'round' | 'square';
|
|
35
|
+
lineJoin: 'miter' | 'round' | 'bevel';
|
|
36
|
+
miterLimit: number;
|
|
37
|
+
/** Dash pattern and offset in composition units, or null. */
|
|
38
|
+
dash: {
|
|
39
|
+
pattern: number[];
|
|
40
|
+
offset: number;
|
|
41
|
+
} | null;
|
|
42
|
+
/**
|
|
43
|
+
* Clip outlines in composition units, one per precomposition above the item: its `w` by `h` rectangle, every
|
|
44
|
+
* transform applied. The item shows only inside all of them. Absent when nothing clips it.
|
|
45
|
+
*/
|
|
46
|
+
clip?: OutlineCommand[][];
|
|
47
|
+
/** The filled outline as shapes, with holes decided by the fill rule. `flipY` negates y for a y-up world. */
|
|
48
|
+
toShapes(options?: {
|
|
49
|
+
flipY?: boolean;
|
|
50
|
+
}): Shape[];
|
|
51
|
+
}
|
|
52
|
+
/** One image layer of a frame. */
|
|
53
|
+
export interface LottieImage {
|
|
54
|
+
kind: 'image';
|
|
55
|
+
/** The asset's id in {@link LottieAnimation.images}. */
|
|
56
|
+
asset: string;
|
|
57
|
+
/** `[a, b, c, d, e, f]` from image pixels to composition units, as `setTransform` takes it. */
|
|
58
|
+
matrix: [number, number, number, number, number, number];
|
|
59
|
+
opacity: number;
|
|
60
|
+
width: number;
|
|
61
|
+
height: number;
|
|
62
|
+
/**
|
|
63
|
+
* Clip outlines in composition units, one per precomposition above the item: its `w` by `h` rectangle, every
|
|
64
|
+
* transform applied. The item shows only inside all of them. Absent when nothing clips it.
|
|
65
|
+
*/
|
|
66
|
+
clip?: OutlineCommand[][];
|
|
67
|
+
}
|
|
68
|
+
/** What {@link LottieAnimation.frame} returns: everything drawn at a frame, back to front. */
|
|
69
|
+
export type LottieItem = LottiePath | LottieImage;
|
|
70
|
+
/** An image asset of the file. */
|
|
71
|
+
export interface LottieImageAsset {
|
|
72
|
+
id: string;
|
|
73
|
+
width: number;
|
|
74
|
+
height: number;
|
|
75
|
+
/** The folder and file name, or the whole `data:` URL when the file embeds it. */
|
|
76
|
+
path: string;
|
|
77
|
+
embedded: boolean;
|
|
78
|
+
}
|
|
79
|
+
/** The part of `CanvasRenderingContext2D` that {@link LottieAnimation.draw} uses. */
|
|
80
|
+
export interface LottieCanvas {
|
|
81
|
+
save(): void;
|
|
82
|
+
restore(): void;
|
|
83
|
+
beginPath(): void;
|
|
84
|
+
moveTo(x: number, y: number): void;
|
|
85
|
+
lineTo(x: number, y: number): void;
|
|
86
|
+
bezierCurveTo(a: number, b: number, c: number, d: number, e: number, f: number): void;
|
|
87
|
+
quadraticCurveTo(a: number, b: number, c: number, d: number): void;
|
|
88
|
+
closePath(): void;
|
|
89
|
+
/** Needed for files whose precompositions clip their layers; `draw` throws `unsupported` without it. */
|
|
90
|
+
clip?(): void;
|
|
91
|
+
fill(rule?: 'nonzero' | 'evenodd'): void;
|
|
92
|
+
stroke(): void;
|
|
93
|
+
transform(a: number, b: number, c: number, d: number, e: number, f: number): void;
|
|
94
|
+
setLineDash(pattern: number[]): void;
|
|
95
|
+
drawImage(image: never, x: number, y: number, width: number, height: number): void;
|
|
96
|
+
createLinearGradient(x0: number, y0: number, x1: number, y1: number): LottieCanvasGradient;
|
|
97
|
+
createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): LottieCanvasGradient;
|
|
98
|
+
fillStyle: unknown;
|
|
99
|
+
strokeStyle: unknown;
|
|
100
|
+
globalAlpha: number;
|
|
101
|
+
lineWidth: number;
|
|
102
|
+
lineCap: string;
|
|
103
|
+
lineJoin: string;
|
|
104
|
+
miterLimit: number;
|
|
105
|
+
lineDashOffset: number;
|
|
106
|
+
}
|
|
107
|
+
/** The gradient object of a {@link LottieCanvas}. */
|
|
108
|
+
export interface LottieCanvasGradient {
|
|
109
|
+
addColorStop(offset: number, color: string): void;
|
|
110
|
+
}
|
|
111
|
+
/** Options of {@link LottieAnimation.draw}. */
|
|
112
|
+
export interface LottieDrawOptions {
|
|
113
|
+
/** Loaded images by asset id, for `drawImage`. An image layer with none is left out. */
|
|
114
|
+
images?: Record<string, unknown>;
|
|
115
|
+
}
|
|
116
|
+
/** What {@link parseLottie} returns. */
|
|
117
|
+
export interface LottieAnimation {
|
|
118
|
+
/** The Lottie version the file names, such as `5.7.4`. */
|
|
119
|
+
version: string;
|
|
120
|
+
name: string;
|
|
121
|
+
/** Composition size in pixels. */
|
|
122
|
+
width: number;
|
|
123
|
+
height: number;
|
|
124
|
+
/** Frames per second. */
|
|
125
|
+
frameRate: number;
|
|
126
|
+
/** First frame, inclusive. */
|
|
127
|
+
inFrame: number;
|
|
128
|
+
/** End frame, exclusive. */
|
|
129
|
+
outFrame: number;
|
|
130
|
+
/** Seconds from first to end frame. */
|
|
131
|
+
duration: number;
|
|
132
|
+
images: LottieImageAsset[];
|
|
133
|
+
/** Named time ranges, in frames. */
|
|
134
|
+
markers: {
|
|
135
|
+
name: string;
|
|
136
|
+
time: number;
|
|
137
|
+
duration: number;
|
|
138
|
+
}[];
|
|
139
|
+
/** Features of the file that frames leave out, such as `text layer` or `mask`. Empty when none. */
|
|
140
|
+
unsupported: string[];
|
|
141
|
+
/** Everything drawn at `frame`, back to front. Any number: a fraction blends between keyframes. */
|
|
142
|
+
frame(frame: number): LottieItem[];
|
|
143
|
+
/** Draws `frame` onto a 2D canvas context, in composition units. Upload the canvas to get a texture. */
|
|
144
|
+
draw(context: LottieCanvas, frame: number, options?: LottieDrawOptions): void;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Reads a Lottie JSON animation. `frame()` gives the vector shapes of any frame, for `toShapes()` or your own drawing,
|
|
148
|
+
* and `draw()` paints a frame onto a 2D canvas context, which uploads as a texture. What the file uses and a frame
|
|
149
|
+
* leaves out is listed in `unsupported`.
|
|
150
|
+
*
|
|
151
|
+
* @throws LoaderError code `syntax` for bad JSON or a missing asset, `header` when the root is not a Lottie animation,
|
|
152
|
+
* `range` for a precomposition that contains itself or nesting past 64, `limit` over `maxLayers`.
|
|
153
|
+
*/
|
|
154
|
+
export declare function parseLottie(input: BinaryInput | string, options?: LottieOptions): LottieAnimation;
|