@loaders.gl/tile-converter 5.0.0-alpha.11 → 5.0.0-alpha.12

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 (83) hide show
  1. package/README.md +178 -2
  2. package/bin/converter-v5.js +2 -0
  3. package/dist/converter.min.cjs +162 -163
  4. package/dist/i3s-server/bin/i3s-server.min.cjs +73 -73
  5. package/dist/index.cjs +1749 -2152
  6. package/dist/index.js +1748 -2162
  7. package/dist/v5/adapters.cjs +1550 -128
  8. package/dist/v5/adapters.d.ts +16 -0
  9. package/dist/v5/adapters.js +1558 -117
  10. package/dist/v5/archive-source.d.ts +36 -0
  11. package/dist/v5/browser-tileset-source.d.ts +55 -11
  12. package/dist/v5/browser.cjs +18 -0
  13. package/dist/v5/browser.d.ts +14 -0
  14. package/dist/v5/browser.js +26 -0
  15. package/dist/v5/conversion-api.d.ts +9 -0
  16. package/dist/v5/converter-cli.d.ts +1 -0
  17. package/dist/v5/converter-cli.js +1214 -0
  18. package/dist/v5/core.cjs +471 -60
  19. package/dist/v5/core.d.ts +2 -0
  20. package/dist/v5/core.js +471 -60
  21. package/dist/v5/i3s-mesh-conversion.d.ts +4 -0
  22. package/dist/v5/i3s-selection-source.d.ts +70 -0
  23. package/dist/v5/index.cjs +10 -0
  24. package/dist/v5/index.d.ts +8 -0
  25. package/dist/v5/index.js +14 -0
  26. package/dist/v5/mesh-conversion.d.ts +18 -0
  27. package/dist/v5/mesh-hierarchy.d.ts +47 -0
  28. package/dist/v5/mesh-source-features.d.ts +2 -1
  29. package/dist/v5/mesh-source-schema.d.ts +12 -0
  30. package/dist/v5/mesh-source.d.ts +5 -2
  31. package/dist/v5/mesh-tileset-sink.d.ts +5 -0
  32. package/dist/v5/node-cli.d.ts +9 -0
  33. package/dist/v5/node-conversion.d.ts +32 -0
  34. package/dist/v5/node-feature-schema.d.ts +3 -0
  35. package/dist/v5/node-i3s-source.d.ts +41 -0
  36. package/dist/v5/node-source.d.ts +47 -0
  37. package/dist/v5/node.cjs +900 -0
  38. package/dist/v5/node.d.ts +6 -0
  39. package/dist/v5/node.js +877 -0
  40. package/dist/v5/point-cloud-file.d.ts +83 -0
  41. package/dist/v5/point-cloud-potree.d.ts +47 -0
  42. package/dist/v5/point-cloud-tileset.d.ts +44 -0
  43. package/dist/v5/resource-manifest.d.ts +10 -1
  44. package/dist/v5/selected-mesh-source.d.ts +53 -0
  45. package/package.json +33 -25
  46. package/src/index.ts +4 -1
  47. package/src/v4/3d-tiles-converter/helpers/load-i3s.ts +2 -4
  48. package/src/v4/converter-cli.ts +8 -2
  49. package/src/v4/i3s-converter/helpers/load-3d-tiles.ts +3 -51
  50. package/src/v4/i3s-converter/helpers/node-index-document.ts +5 -2
  51. package/src/v4/i3s-converter/helpers/preprocess-3d-tiles.ts +1 -1
  52. package/src/v4/i3s-converter/json-templates/store.ts +2 -1
  53. package/src/v5/adapters.ts +48 -0
  54. package/src/v5/archive-source.ts +235 -0
  55. package/src/v5/browser-tileset-source.ts +418 -48
  56. package/src/v5/browser.ts +45 -0
  57. package/src/v5/conversion-api.ts +69 -9
  58. package/src/v5/converter-cli.ts +28 -0
  59. package/src/v5/core.ts +6 -0
  60. package/src/v5/i3s-mesh-conversion.ts +40 -2
  61. package/src/v5/i3s-mesh-sink.ts +1 -1
  62. package/src/v5/i3s-mesh-source.ts +1 -0
  63. package/src/v5/i3s-selection-source.ts +298 -0
  64. package/src/v5/index.ts +26 -0
  65. package/src/v5/mesh-conversion.ts +20 -0
  66. package/src/v5/mesh-hierarchy.ts +648 -0
  67. package/src/v5/mesh-source-features.ts +39 -7
  68. package/src/v5/mesh-source-schema.ts +134 -0
  69. package/src/v5/mesh-source.ts +16 -4
  70. package/src/v5/mesh-tileset-sink.ts +1 -1
  71. package/src/v5/node-cli.ts +197 -0
  72. package/src/v5/node-conversion.ts +345 -0
  73. package/src/v5/node-feature-schema.ts +155 -0
  74. package/src/v5/node-i3s-source.ts +181 -0
  75. package/src/v5/node-source.ts +328 -0
  76. package/src/v5/node.ts +16 -0
  77. package/src/v5/point-cloud-file.ts +306 -0
  78. package/src/v5/point-cloud-potree.ts +130 -0
  79. package/src/v5/point-cloud-tileset.ts +137 -0
  80. package/src/v5/resource-fetcher.ts +4 -1
  81. package/src/v5/resource-manifest.ts +40 -5
  82. package/src/v5/selected-mesh-source.ts +384 -0
  83. package/src/v5/spatial-conversion.ts +88 -17
@@ -0,0 +1,328 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {open, realpath} from 'node:fs/promises';
6
+ import {basename, dirname, isAbsolute, relative, resolve, sep} from 'node:path';
7
+ import {fileURLToPath} from 'node:url';
8
+ import type {ReadableFile} from '@loaders.gl/loader-utils';
9
+ import {parse3DTilesArchive} from '@loaders.gl/3d-tiles';
10
+ import {
11
+ createBrowserTilesetConversionSource,
12
+ TileConversionError
13
+ } from '@loaders.gl/tile-converter/v5/core';
14
+ import type {
15
+ BrowserTilesetConversionInspection,
16
+ BrowserTilesetConversionInputResource,
17
+ TileConversionSource
18
+ } from '@loaders.gl/tile-converter/v5/core';
19
+ import {createTileConversionResourceFetcher} from './resource-fetcher.js';
20
+ import {openTileConversionArchive} from './archive-source.js';
21
+
22
+ /** Maximum budgets for the initial Node source profile; callers may lower them. */
23
+ export const NODE_TILESET_LIMITS = {
24
+ /** Aggregate response bytes and, separately, indexed archive read bytes. */
25
+ maxInputBytes: 16 * 1024 * 1024,
26
+ /** Declared content placements in the explicit root document. */
27
+ maxInputResources: 1000,
28
+ /** Maximum root/external document placements inspected or traversed. */
29
+ maxTilesetDocuments: 32
30
+ } as const;
31
+
32
+ /** Platform I/O options for bounded explicit 3D Tiles inspection and raw content reads. */
33
+ export interface NodeTilesetConversionSourceOptions {
34
+ /** Local tileset JSON/3TZ path, file URL, or HTTP(S) tileset JSON URL. */
35
+ readonly input: string;
36
+ /** Explicit JSON or indexed archive input format. */
37
+ readonly format?: '3d-tiles' | '3tz';
38
+ /** Positive response/indexed-read budget, at most 16 MiB. */
39
+ readonly maxInputBytes?: number;
40
+ /** Positive content-placement budget, at most 1,000. */
41
+ readonly maxInputResources?: number;
42
+ /** Positive document budget, at most 32; defaults to root-only traversal. */
43
+ readonly maxTilesetDocuments?: number;
44
+ /** Follow declared .json external documents during metadata-only inspection. */
45
+ readonly inspectNestedTilesets?: boolean;
46
+ /** Cancellation for opening, inspection and subsequent reads. */
47
+ readonly signal?: AbortSignal;
48
+ /** HTTP(S) transport; local dependencies never fall back to it. */
49
+ readonly fetcher?: typeof fetch;
50
+ }
51
+
52
+ /** A dedicated Node source whose owner must close it after inspection or conversion. */
53
+ export interface NodeTilesetConversionSource
54
+ extends TileConversionSource<
55
+ BrowserTilesetConversionInspection,
56
+ BrowserTilesetConversionInputResource
57
+ > {
58
+ /** Aborts further I/O and releases the indexed archive handle; safe to call repeatedly. */
59
+ close(): Promise<void>;
60
+ /** Confined, lifetime-budgeted transport for content-level buffers and images. */
61
+ readonly fetcher: typeof fetch;
62
+ }
63
+
64
+ /**
65
+ * Opens bounded Node I/O and delegates explicit traversal to the portable source.
66
+ * Local resources stay below the root document's real directory, including symlinks;
67
+ * archive resources stay inside the archive. Inspection reads metadata only. Reading yields
68
+ * raw content for an application codec, without CRS inference or content-format qualification.
69
+ * The source is sequential and owns one lifetime-wide response/read budget. Close it in finally.
70
+ */
71
+ export async function createNodeTilesetConversionSource(
72
+ options: NodeTilesetConversionSourceOptions
73
+ ): Promise<NodeTilesetConversionSource> {
74
+ const maxInputBytes = options.maxInputBytes ?? NODE_TILESET_LIMITS.maxInputBytes;
75
+ const maxInputResources = options.maxInputResources ?? NODE_TILESET_LIMITS.maxInputResources;
76
+ const maxTilesetDocuments = options.maxTilesetDocuments ?? 1;
77
+ if (
78
+ !Number.isSafeInteger(maxInputBytes) ||
79
+ maxInputBytes < 1 ||
80
+ maxInputBytes > NODE_TILESET_LIMITS.maxInputBytes ||
81
+ !Number.isSafeInteger(maxInputResources) ||
82
+ maxInputResources < 1 ||
83
+ maxInputResources > NODE_TILESET_LIMITS.maxInputResources ||
84
+ !Number.isSafeInteger(maxTilesetDocuments) ||
85
+ maxTilesetDocuments < 1 ||
86
+ maxTilesetDocuments > NODE_TILESET_LIMITS.maxTilesetDocuments ||
87
+ !['3d-tiles', '3tz'].includes(options.format ?? '3d-tiles')
88
+ )
89
+ throw new TileConversionError(
90
+ 'INVALID_NODE_SOURCE_OPTIONS',
91
+ 'Node sources require positive byte/resource budgets within the initial profile limits'
92
+ );
93
+ options.signal?.throwIfAborted();
94
+ const controller = new AbortController();
95
+ const lifetimeSignal = options.signal
96
+ ? AbortSignal.any([controller.signal, options.signal])
97
+ : controller.signal;
98
+ let archiveFile: ReadableFile | undefined;
99
+ let closeArchive: (() => Promise<void>) | undefined;
100
+ let consumedReadBytes = 0;
101
+ /** Reserves physical file bytes before allocation, including repeated indexed reads. */
102
+ const reserveRead = (length: number): void => {
103
+ if (length > maxInputBytes - consumedReadBytes)
104
+ throw new TileConversionError(
105
+ 'INPUT_RESOURCE_TOO_LARGE',
106
+ 'Local reads exceed the aggregate input budget'
107
+ );
108
+ consumedReadBytes += length;
109
+ };
110
+ try {
111
+ const inputUrl = /^(?:https?|file):/i.test(options.input) ? new URL(options.input) : undefined;
112
+ if (
113
+ !inputUrl &&
114
+ /^[a-z][a-z\d+.-]*:/i.test(options.input) &&
115
+ !/^[a-z]:[\\/]/i.test(options.input)
116
+ )
117
+ throw new TileConversionError(
118
+ 'UNSUPPORTED_NODE_SOURCE',
119
+ 'Use a local path, file URL or HTTP(S) tileset URL'
120
+ );
121
+ const remote = inputUrl && inputUrl.protocol !== 'file:';
122
+ let rootUrl: string;
123
+ let transport: typeof fetch;
124
+ let virtualRoot: URL | undefined;
125
+ if (remote && options.format === '3tz') {
126
+ const archive = await openTileConversionArchive(
127
+ {input: inputUrl.href, format: '3tz'},
128
+ maxInputBytes,
129
+ lifetimeSignal,
130
+ options.fetcher
131
+ );
132
+ rootUrl = archive.rootUrl;
133
+ transport = archive.fetcher;
134
+ closeArchive = archive.close;
135
+ } else if (remote) {
136
+ rootUrl = inputUrl.href;
137
+ transport = options.fetcher ?? fetch;
138
+ } else {
139
+ const inputPath = await realpath(inputUrl ? fileURLToPath(inputUrl) : resolve(options.input));
140
+ lifetimeSignal.throwIfAborted();
141
+ const directory = dirname(inputPath);
142
+ virtualRoot = new URL('https://conversion.invalid/input/');
143
+ rootUrl = new URL(
144
+ options.format === '3tz' ? 'tileset.json' : encodeURIComponent(basename(inputPath)),
145
+ virtualRoot
146
+ ).href;
147
+ if (options.format === '3tz') {
148
+ archiveFile = await openLocalFile(inputPath, maxInputBytes, lifetimeSignal, reserveRead);
149
+ const archive = await parse3DTilesArchive(archiveFile);
150
+ transport = async (input, requestOptions) => {
151
+ lifetimeSignal.throwIfAborted();
152
+ requestOptions?.signal?.throwIfAborted();
153
+ const path = getLocalResourcePath(input, virtualRoot!);
154
+ const bytes = await archive.getFile(path);
155
+ lifetimeSignal.throwIfAborted();
156
+ requestOptions?.signal?.throwIfAborted();
157
+ return new Response(bytes);
158
+ };
159
+ } else {
160
+ transport = async (input, requestOptions) => {
161
+ const path = getLocalResourcePath(input, virtualRoot!);
162
+ const resolvedPath = await realpath(resolve(directory, path));
163
+ const relativePath = relative(directory, resolvedPath);
164
+ if (
165
+ relativePath === '..' ||
166
+ relativePath.startsWith(`..${sep}`) ||
167
+ isAbsolute(relativePath)
168
+ )
169
+ throw new TileConversionError(
170
+ 'EXTERNAL_RESOURCE_UNSUPPORTED',
171
+ 'Local dependencies must remain inside the tileset directory'
172
+ );
173
+ const signal = requestOptions?.signal
174
+ ? AbortSignal.any([lifetimeSignal, requestOptions.signal])
175
+ : lifetimeSignal;
176
+ const file = await openLocalFile(resolvedPath, maxInputBytes, signal, reserveRead);
177
+ try {
178
+ return new Response(await file.read());
179
+ } finally {
180
+ await file.close();
181
+ }
182
+ };
183
+ }
184
+ }
185
+ const resourceFetcher = createTileConversionResourceFetcher({
186
+ maxInputBytes,
187
+ signal: lifetimeSignal,
188
+ fetcher: transport
189
+ });
190
+ const source = createBrowserTilesetConversionSource({
191
+ input: rootUrl,
192
+ maxInputBytes,
193
+ maxInputResources,
194
+ maxTilesetDocuments,
195
+ inspectNestedTilesets: options.inspectNestedTilesets,
196
+ fetcher: resourceFetcher
197
+ });
198
+ return {
199
+ fetcher: resourceFetcher,
200
+ /** Inspects declarations without opening tile payloads or decoding coordinates. */
201
+ async inspect(signal) {
202
+ const inspection = await source.inspect(
203
+ signal ? AbortSignal.any([lifetimeSignal, signal]) : lifetimeSignal
204
+ );
205
+ if (virtualRoot)
206
+ for (const resource of inspection.resources)
207
+ getLocalResourcePath(
208
+ new URL(resource.uri, resource.tilesetUrl ?? inspection.rootUrl),
209
+ virtualRoot
210
+ );
211
+ return inspection;
212
+ },
213
+ /** Reads each declared placement with the same lifetime-wide input budget. */
214
+ read: (inspection, signal) =>
215
+ source.read(
216
+ inspection,
217
+ signal ? AbortSignal.any([lifetimeSignal, signal]) : lifetimeSignal
218
+ ),
219
+ /** Cancels future reads and releases the archive handle. */
220
+ async close() {
221
+ controller.abort(new TileConversionError('NODE_SOURCE_CLOSED', 'Node source is closed'));
222
+ const file = archiveFile;
223
+ archiveFile = undefined;
224
+ await closeArchive?.();
225
+ await file?.close();
226
+ }
227
+ };
228
+ } catch (error) {
229
+ await closeArchive?.();
230
+ await archiveFile?.close();
231
+ throw error;
232
+ }
233
+ }
234
+
235
+ /** Resolves a canonical in-source URL and rejects external or encoded escaping paths. */
236
+ function getLocalResourcePath(input: RequestInfo | URL, root: URL): string {
237
+ const url = new URL(input instanceof Request ? input.url : String(input));
238
+ if (url.origin !== root.origin || !url.pathname.startsWith(root.pathname))
239
+ throw new TileConversionError(
240
+ 'EXTERNAL_RESOURCE_UNSUPPORTED',
241
+ 'Dependencies must resolve inside the selected local input'
242
+ );
243
+ const path = decodeURIComponent(url.pathname.slice(root.pathname.length));
244
+ if (
245
+ !path ||
246
+ path.startsWith('/') ||
247
+ path.includes('\\') ||
248
+ path.split('/').some(segment => segment === '..' || segment === '.')
249
+ )
250
+ throw new TileConversionError(
251
+ 'EXTERNAL_RESOURCE_UNSUPPORTED',
252
+ 'Dependency paths cannot escape the selected input'
253
+ );
254
+ return path;
255
+ }
256
+
257
+ /** Opens a real Node file and provides checked typed-array reads without Node Buffer. */
258
+ export async function openLocalFile(
259
+ path: string,
260
+ maxInputBytes: number,
261
+ signal: AbortSignal,
262
+ reserveRead: (length: number) => void
263
+ ): Promise<ReadableFile> {
264
+ signal.throwIfAborted();
265
+ const handle = await open(path, 'r');
266
+ try {
267
+ signal.throwIfAborted();
268
+ const information = await handle.stat();
269
+ if (
270
+ !information.isFile() ||
271
+ !Number.isSafeInteger(information.size) ||
272
+ information.size > maxInputBytes
273
+ )
274
+ throw new TileConversionError(
275
+ 'INPUT_RESOURCE_TOO_LARGE',
276
+ 'Local input must be a regular file within the byte budget'
277
+ );
278
+ const size = information.size;
279
+ return {
280
+ handle,
281
+ size,
282
+ bigsize: BigInt(size),
283
+ url: path,
284
+ /** Reads a bounded range, checking cancellation and unexpected EOF between native reads. */
285
+ async read(start = 0, length = size - Number(start), readSignal) {
286
+ const operationSignal = readSignal ? AbortSignal.any([signal, readSignal]) : signal;
287
+ operationSignal.throwIfAborted();
288
+ const offset = Number(start);
289
+ if (
290
+ !Number.isSafeInteger(offset) ||
291
+ offset < 0 ||
292
+ !Number.isSafeInteger(length) ||
293
+ length < 0 ||
294
+ offset > size - length
295
+ )
296
+ throw new TileConversionError(
297
+ 'INVALID_INPUT_RANGE',
298
+ 'Local file reads require a range inside the file'
299
+ );
300
+ reserveRead(length);
301
+ const bytes = new Uint8Array(length);
302
+ let readBytes = 0;
303
+ while (readBytes < length) {
304
+ operationSignal.throwIfAborted();
305
+ const result = await handle.read(
306
+ bytes,
307
+ readBytes,
308
+ length - readBytes,
309
+ offset + readBytes
310
+ );
311
+ if (result.bytesRead === 0)
312
+ throw new TileConversionError(
313
+ 'INPUT_FILE_TRUNCATED',
314
+ 'Local input ended before its declared size'
315
+ );
316
+ readBytes += result.bytesRead;
317
+ }
318
+ operationSignal.throwIfAborted();
319
+ return bytes.buffer;
320
+ },
321
+ /** Releases the Node file handle. */
322
+ close: () => handle.close()
323
+ };
324
+ } catch (error) {
325
+ await handle.close();
326
+ throw error;
327
+ }
328
+ }
package/src/v5/node.ts ADDED
@@ -0,0 +1,16 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ // Intentionally Node-only. Portable entrypoints do not import this adapter.
6
+ export {createNodeTilesetConversionSource, NODE_TILESET_LIMITS} from './node-source.js';
7
+ export type {
8
+ NodeTilesetConversionSource,
9
+ NodeTilesetConversionSourceOptions
10
+ } from './node-source.js';
11
+
12
+ export {convertNodeTileset, writeNodeTileConversionOutput} from './node-conversion.js';
13
+ export type {NodeTileConversionOptions, NodeTileConversionOutputFormat} from './node-conversion.js';
14
+
15
+ export {createNodeI3SConversionSource} from './node-i3s-source.js';
16
+ export type {NodeI3SConversionSourceOptions} from './node-i3s-source.js';
@@ -0,0 +1,306 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {COPCSourceLoader} from '@loaders.gl/copc';
6
+ import type {COPCTileSource, COPCSourceLoaderOptions} from '@loaders.gl/copc';
7
+ import {PotreeSourceLoader, encodePotreeDataset} from '@loaders.gl/potree';
8
+ import type {
9
+ PotreeDataset,
10
+ PotreeWriterNode,
11
+ PotreeWriterOptions,
12
+ PotreeSourceLoaderOptions
13
+ } from '@loaders.gl/potree';
14
+ import type {Mesh, MeshArrowTable, MeshGeometry} from '@loaders.gl/schema';
15
+ import {deduceMeshSchema} from '@loaders.gl/schema-utils';
16
+ import {encodeZip, openZipArchive} from '@loaders.gl/zip';
17
+ import type {ZipArchive, ZipEntry} from '@loaders.gl/zip';
18
+
19
+ /** Explicit local-file input; no format is inferred from an extension. */
20
+ export type OpenPointCloudFileOptions = {
21
+ /** Maximum retained input Blob size. This does not bound decoded heap. */
22
+ readonly maxFileBytes: number;
23
+ /** Cancellation before/after initialization and during archived resource reads. */
24
+ readonly signal?: AbortSignal;
25
+ } & (
26
+ | {
27
+ /** COPC is a native single LAZ file. */
28
+ readonly format: 'copc';
29
+ /** Existing COPC source coordinate and decoder controls. */
30
+ readonly sourceOptions?: COPCSourceLoaderOptions;
31
+ }
32
+ | {
33
+ /** A loaders.gl STORE ZIP convention, not a native Potree archive standard. */
34
+ readonly format: 'potree-zip';
35
+ /** Maximum ZIP entry count, including metadata; default 100000. */
36
+ readonly maxEntries?: number;
37
+ /** Maximum requested ZIP resource/range bytes; default 64 MiB. */
38
+ readonly maxResourceBytes?: number;
39
+ /** Explicit metadata path when it is not at the ZIP root. */
40
+ readonly metadataPath?: string;
41
+ /** Existing Potree parser controls; the bundle owns its isolated fetch transport. */
42
+ readonly sourceOptions?: PotreeSourceLoaderOptions;
43
+ }
44
+ );
45
+
46
+ /** An initialized source with an owned local file lifetime. */
47
+ export interface OpenedPointCloudFile {
48
+ /** Lazy node access through the existing unified source APIs. */
49
+ readonly source: ReturnType<typeof PotreeSourceLoader.createDataSource> | COPCTileSource;
50
+ /** Closes the source and rejects subsequent bundle reads; idempotent. */
51
+ readonly close: () => Promise<void>;
52
+ }
53
+
54
+ /** Complete Potree dataset packaging controls. */
55
+ export interface EncodePotreeArchiveOptions {
56
+ /** Existing dataset version, precision, hierarchy and encoded-byte limits. */
57
+ readonly potree?: PotreeWriterOptions['potree'];
58
+ /** Maximum completed STORE ZIP32 bytes, checked before archive allocation. */
59
+ readonly maxArchiveBytes: number;
60
+ }
61
+
62
+ /**
63
+ * Writes a complete Potree 1.x/2.0 dataset to a deterministic uncompressed ZIP32 Blob.
64
+ * This is a loaders.gl bundle convention, not a native Potree archive standard. Metadata sits
65
+ * at the root with original relative resource paths. Writer version/attribute/precision gates
66
+ * apply. Inputs, dataset bytes, ZIP temporaries and the returned Blob are separate memory.
67
+ * @param input - Decoded points or caller-organized additive nodes; no LOD sampling is inferred.
68
+ * @param options - Dataset writer controls and final archive budget.
69
+ * @returns A downloadable ZIP Blob that openPointCloudFile can read incrementally.
70
+ */
71
+ export async function encodePotreeArchive(
72
+ input: Mesh | MeshArrowTable | readonly PotreeWriterNode[],
73
+ options: EncodePotreeArchiveOptions
74
+ ): Promise<Blob> {
75
+ validateLimit(options.maxArchiveBytes);
76
+ const dataset = await encodePotreeDataset(input, {potree: options.potree});
77
+ return encodePotreeDatasetArchive(dataset, options.maxArchiveBytes, options.potree?.signal);
78
+ }
79
+
80
+ /** Packages already encoded Potree files without encoding or sampling points a second time. */
81
+ export async function encodePotreeDatasetArchive(
82
+ dataset: PotreeDataset,
83
+ maxArchiveBytes: number,
84
+ signal?: AbortSignal
85
+ ): Promise<Blob> {
86
+ validateLimit(maxArchiveBytes);
87
+ for (const path of dataset.files.keys()) validatePath(path);
88
+ signal?.throwIfAborted();
89
+ const archive = await encodeZip(Object.fromEntries(dataset.files), {
90
+ compression: 'store',
91
+ zip64: 'never',
92
+ maxEntries: 65535,
93
+ maxArchiveBytes: BigInt(maxArchiveBytes),
94
+ signal
95
+ });
96
+ signal?.throwIfAborted();
97
+ return new Blob([archive], {type: 'application/zip'});
98
+ }
99
+
100
+ /**
101
+ * Opens a COPC Blob or a Potree STORE ZIP without extracting the complete dataset.
102
+ * ZIP directory/local headers are checked before constructing the Potree source; modern node
103
+ * requests slice only their archived byte ranges. DEFLATE, encrypted entries, ambiguous
104
+ * metadata and noncanonical paths fail explicitly. Partial reads do not verify whole-entry CRCs.
105
+ * Legacy Potree retains its existing coordinate behavior; modern Potree retains native coordinates.
106
+ * COPC uses its existing source coordinate policy. Applications own CRS preparation and conversion.
107
+ * @param input - Immutable local file/Blob, bounded by maxFileBytes.
108
+ * @param options - Explicit format, source controls and archive transport limits.
109
+ * @returns An initialized lazy source; close it when conversion finishes.
110
+ */
111
+ export async function openPointCloudFile(
112
+ input: Blob,
113
+ options: OpenPointCloudFileOptions
114
+ ): Promise<OpenedPointCloudFile> {
115
+ validateLimit(options.maxFileBytes);
116
+ if (input.size > options.maxFileBytes) throw new RangeError('Point file input budget exceeded');
117
+ options.signal?.throwIfAborted();
118
+ if (options.format === 'copc') {
119
+ const source = COPCSourceLoader.createDataSource(input, options.sourceOptions || {});
120
+ try {
121
+ await source.metadata;
122
+ options.signal?.throwIfAborted();
123
+ return {source, close: () => source.close()};
124
+ } catch (error) {
125
+ await source.close();
126
+ throw error;
127
+ }
128
+ }
129
+ if (options.format !== 'potree-zip') throw new TypeError('Unsupported point file format');
130
+ const maxEntries = options.maxEntries ?? 100000;
131
+ const maxResourceBytes = options.maxResourceBytes ?? 64 * 1024 * 1024;
132
+ validateLimit(maxEntries);
133
+ validateLimit(maxResourceBytes);
134
+ const controller = new AbortController();
135
+ const signals = [controller.signal, options.signal, options.sourceOptions?.potree?.signal].filter(
136
+ (value): value is AbortSignal => Boolean(value)
137
+ );
138
+ const signal = AbortSignal.any(signals);
139
+ let archive: ZipArchive | undefined;
140
+ const entries = new Map<string, ZipEntry>();
141
+ let source: ReturnType<typeof PotreeSourceLoader.createDataSource> | undefined;
142
+ try {
143
+ archive = await openZipArchive(input, {
144
+ maxArchiveBytes: BigInt(options.maxFileBytes),
145
+ maxEntries,
146
+ maxReadBytes: Math.max(65557, maxResourceBytes),
147
+ maxCompressedBytes: BigInt(maxResourceBytes),
148
+ maxDecodedBytes: BigInt(maxResourceBytes),
149
+ signal
150
+ });
151
+ for await (const entry of archive.entries()) {
152
+ if (entry.directory) continue;
153
+ validatePath(entry.path);
154
+ if (entry.compression !== 'store')
155
+ throw new Error('Potree bundles require unencrypted STORE entries');
156
+ entries.set(entry.path, entry);
157
+ }
158
+ const candidates = ['metadata.json', 'cloud.js'].filter(path => entries.has(path));
159
+ const metadataPath = options.metadataPath || (candidates.length === 1 ? candidates[0] : '');
160
+ validatePath(metadataPath);
161
+ if (!entries.has(metadataPath) || !/(^|\/)(metadata\.json|cloud\.js)$/.test(metadataPath))
162
+ throw new Error('Potree ZIP requires one metadata entry or an explicit metadataPath');
163
+ const prefix = 'https://point-bundle.loaders.gl.invalid/';
164
+ /** Isolated archive transport; it never falls back to network access. */
165
+ const fetchResource = async (
166
+ resource: string,
167
+ request: RequestInit = {}
168
+ ): Promise<Response> => {
169
+ signal.throwIfAborted();
170
+ request.signal?.throwIfAborted();
171
+ const url = new URL(resource);
172
+ if (url.origin !== new URL(prefix).origin || url.search || url.hash)
173
+ throw new Error('Resource outside the Potree bundle');
174
+ const path = decodeURIComponent(url.pathname.slice(1));
175
+ validatePath(path);
176
+ const entry = entries.get(path);
177
+ if (!entry) throw new Error(`Missing Potree bundle resource: ${path}`);
178
+ const range = new Headers(request.headers).get('range');
179
+ const match = range?.match(/^bytes=(\d+)-(\d+)$/);
180
+ if (range && !match) throw new RangeError('Unsupported Potree bundle byte range');
181
+ const begin = match ? Number(match[1]) : 0;
182
+ const end = match ? Number(match[2]) + 1 : Number(entry.size);
183
+ if (
184
+ !Number.isSafeInteger(begin) ||
185
+ !Number.isSafeInteger(end) ||
186
+ begin < 0 ||
187
+ end < begin ||
188
+ BigInt(end) > entry.size ||
189
+ end - begin > maxResourceBytes
190
+ )
191
+ throw new RangeError('Potree bundle resource/range budget exceeded');
192
+ const headers = new Headers({
193
+ 'content-length': String(end - begin),
194
+ 'accept-ranges': 'bytes'
195
+ });
196
+ if (match) headers.set('content-range', `bytes ${begin}-${end - 1}/${entry.size}`);
197
+ const bytes = match
198
+ ? await archive!.readRange(entry, {
199
+ start: BigInt(begin),
200
+ end: BigInt(end),
201
+ signal: request.signal ?? undefined
202
+ })
203
+ : await archive!.read(entry, {signal: request.signal ?? undefined});
204
+ const response = new Response(bytes, {
205
+ status: match ? 206 : 200,
206
+ headers
207
+ });
208
+ Object.defineProperty(response, 'url', {value: url.href});
209
+ signal.throwIfAborted();
210
+ return response;
211
+ };
212
+ source = PotreeSourceLoader.createDataSource(`${prefix}${metadataPath}`, {
213
+ ...options.sourceOptions,
214
+ core: {...options.sourceOptions?.core, fetch: fetchResource},
215
+ potree: {...options.sourceOptions?.potree, signal}
216
+ });
217
+ await source.initialize();
218
+ signal.throwIfAborted();
219
+ return {
220
+ source,
221
+ async close() {
222
+ controller.abort();
223
+ source?.close();
224
+ await archive?.close();
225
+ }
226
+ };
227
+ } catch (error) {
228
+ controller.abort();
229
+ source?.close();
230
+ await archive?.close();
231
+ throw error;
232
+ }
233
+ }
234
+
235
+ /** Explicit opt-in mesh-to-point selection. */
236
+ export interface ExtractMeshVerticesOptions {
237
+ /** Attribute names to retain in addition to POSITION; all other semantics are omitted explicitly. */
238
+ readonly attributes: readonly string[];
239
+ /** Maximum stored vertices, not expanded triangle corners. */
240
+ readonly maxPoints: number;
241
+ }
242
+
243
+ /**
244
+ * Harvests one point per stored vertex from already placed triangle geometry.
245
+ * Does not expand indices, deduplicate coincident vertices or sample triangle interiors. Output
246
+ * shares immutable attribute arrays. Materials, features, topology and unselected attributes are
247
+ * discarded by this explicit opt-in; select compatible attributes before destination encoding.
248
+ * For tiles, use the mesh source adapters first so node/RTC/tile transforms are applied once.
249
+ * @param mesh - Decoded triangle geometry in the desired absolute coordinate frame.
250
+ * @param options - Explicit retained attributes and stored-vertex count budget.
251
+ * @returns An unindexed point Mesh ready for point-cloud writers or the dynamic tiler.
252
+ */
253
+ export function extractMeshVertices(mesh: MeshGeometry, options: ExtractMeshVerticesOptions): Mesh {
254
+ validateLimit(options.maxPoints);
255
+ const position = mesh.attributes.POSITION;
256
+ const pointCount = position?.value.length / 3;
257
+ if (
258
+ mesh.topology !== 'triangle-list' ||
259
+ mesh.mode !== 4 ||
260
+ !position ||
261
+ position.size !== 3 ||
262
+ position.normalized ||
263
+ position.transform ||
264
+ position.byteStride ||
265
+ position.byteOffset ||
266
+ !(position.value instanceof Float32Array || position.value instanceof Float64Array) ||
267
+ !Number.isSafeInteger(pointCount) ||
268
+ pointCount > options.maxPoints ||
269
+ position.value.some(value => !Number.isFinite(value))
270
+ )
271
+ throw new Error(
272
+ 'Mesh vertex harvesting requires packed finite triangle XYZ within the point budget'
273
+ );
274
+ const attributes: MeshGeometry['attributes'] = {POSITION: position};
275
+ for (const name of options.attributes) {
276
+ const attribute = mesh.attributes[name];
277
+ if (
278
+ !attribute ||
279
+ attribute.transform ||
280
+ attribute.byteStride ||
281
+ attribute.byteOffset ||
282
+ !Number.isSafeInteger(attribute.size) ||
283
+ attribute.size < 1 ||
284
+ attribute.value.length !== pointCount * attribute.size
285
+ )
286
+ throw new Error(`Invalid selected mesh vertex attribute: ${name}`);
287
+ attributes[name] = attribute;
288
+ }
289
+ return {topology: 'point-list', mode: 0, attributes, schema: deduceMeshSchema(attributes)};
290
+ }
291
+
292
+ /** Rejects unsafe or unsupported archive paths before URL resolution. */
293
+ function validatePath(path: string): void {
294
+ if (
295
+ !path ||
296
+ !/^[A-Za-z0-9_.\/-]+$/.test(path) ||
297
+ path.split('/').some(part => !part || part === '.' || part === '..')
298
+ )
299
+ throw new Error('Potree bundle paths must be canonical relative ASCII paths');
300
+ }
301
+
302
+ /** Validates caller budgets before file reads or output allocation. */
303
+ function validateLimit(value: number): void {
304
+ if (!Number.isSafeInteger(value) || value < 1)
305
+ throw new RangeError('Point file limits must be positive safe integers');
306
+ }